-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathopenLink.ts
More file actions
165 lines (162 loc) · 6.55 KB
/
openLink.ts
File metadata and controls
165 lines (162 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {getIdFromSYProtocol, isLocalPath, isSYProtocol, pathPosix} from "../util/pathName";
/// #if !BROWSER
import {shell, ipcRenderer} from "electron";
/// #endif
import {getSearch} from "../util/functions";
import {Constants} from "../constants";
/// #if !MOBILE
import {openAsset, openBy, openFile, openFileById} from "./util";
/// #endif
import {showMessage} from "../dialog/message";
import {openByMobile} from "../protyle/util/compatibility";
import {App} from "../index";
import {fetchPost} from "../util/fetch";
import {checkFold} from "../util/noRelyPCFunction";
import {openMobileFileById} from "../mobile/editor";
import {emitToEventBus} from "../plugin/EventBus";
export const processSYLink = (app: App, url: string) => {
let urlObj: URL;
try {
urlObj = new URL(url);
if (urlObj.protocol !== "siyuan:") {
return false;
}
} catch (error) {
return false;
}
if (urlObj && urlObj.hostname === "plugins") {
const pluginNameType = urlObj.pathname.split("/")[1];
if (!pluginNameType) {
return false;
}
app.plugins.find(plugin => {
if (pluginNameType.startsWith(plugin.name)) {
// siyuan://plugins/plugin-name/foo?bar=baz
plugin.eventBus.emit("open-siyuan-url-plugin", {url});
/// #if !MOBILE
// https://github.com/siyuan-note/siyuan/pull/9256
if (pluginNameType.split("/")[0] !== plugin.name) {
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
let data = urlObj.searchParams.get("data");
try {
data = JSON.parse(data || "{}");
} catch (e) {
console.log("Error open plugin tab with protocol:", e);
}
openFile({
app,
custom: {
title: urlObj.searchParams.get("title"),
icon: urlObj.searchParams.get("icon"),
data,
id: pluginNameType
},
});
}
/// #endif
return true;
}
});
return true;
}
if (urlObj && isSYProtocol(url)) {
const id = getIdFromSYProtocol(url);
const focus = urlObj.searchParams.get("focus") === "1";
window.siyuan.editorIsFullscreen = urlObj.searchParams.get("fullscreen") === "1";
fetchPost("/api/block/checkBlockExist", {id}, existResponse => {
if (existResponse.data) {
checkFold(id, (zoomIn) => {
/// #if !MOBILE
openFileById({
app,
id,
action: (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
zoomIn: zoomIn || focus
});
/// #else
openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
/// #endif
});
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_CMD, "show");
/// #endif
}
emitToEventBus("open-siyuan-url-block", {
url,
id,
focus,
exist: existResponse.data,
});
});
return true;
}
return false;
};
export const openLink = (protyle: IProtyle, aLink: string, event?: MouseEvent, ctrlIsPressed = false) => {
let linkAddress = Lute.UnEscapeHTMLStr(aLink);
let pdfParams;
if (isLocalPath(linkAddress) && !linkAddress.startsWith("file://") && linkAddress.indexOf(".pdf") > -1) {
const pdfAddress = linkAddress.split("/");
if (pdfAddress.length === 3 && pdfAddress[0] === "assets" && pdfAddress[1].endsWith(".pdf") && /\d{14}-\w{7}/.test(pdfAddress[2])) {
linkAddress = `assets/${pdfAddress[1]}`;
pdfParams = pdfAddress[2];
} else {
pdfParams = parseInt(getSearch("page", linkAddress));
linkAddress = linkAddress.split("?page")[0];
}
}
/// #if MOBILE
openByMobile(linkAddress);
/// #else
if (isLocalPath(linkAddress)) {
if (Constants.SIYUAN_ASSETS_EXTS.includes(pathPosix().extname(linkAddress)) &&
(
!linkAddress.endsWith(".pdf") ||
// 本地 pdf 仅 assets/ 开头的才使用 siyuan 打开
(linkAddress.endsWith(".pdf") && linkAddress.startsWith("assets/"))
)
) {
if (event && event.altKey) {
openAsset(protyle.app, linkAddress, pdfParams);
} else if (event && event.shiftKey) {
/// #if !BROWSER
openBy(linkAddress, "app");
/// #else
openByMobile(linkAddress);
/// #endif
} else if (ctrlIsPressed) {
/// #if !BROWSER
openBy(linkAddress, "folder");
/// #else
openByMobile(linkAddress);
/// #endif
} else {
openAsset(protyle.app, linkAddress, pdfParams, !window.siyuan.config.fileTree.noSplitScreenWhenOpenTab ? "right" : null);
}
} else {
/// #if !BROWSER
if (ctrlIsPressed) {
openBy(linkAddress, "folder");
} else {
openBy(linkAddress, "app");
}
/// #else
openByMobile(linkAddress);
/// #endif
}
} else if (linkAddress) {
if (0 > linkAddress.indexOf(":")) {
// 使用 : 判断,不使用 :// 判断 Open external application protocol invalid https://github.com/siyuan-note/siyuan/issues/10075
// Support click to open hyperlinks like `www.foo.com` https://github.com/siyuan-note/siyuan/issues/9986
linkAddress = `https://${linkAddress}`;
}
/// #if !BROWSER
shell.openExternal(linkAddress).catch((e) => {
showMessage(e);
});
/// #else
openByMobile(linkAddress);
/// #endif
}
/// #endif
};