forked from siyuan-note/siyuan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenLink.ts
More file actions
191 lines (184 loc) · 6.96 KB
/
Copy pathopenLink.ts
File metadata and controls
191 lines (184 loc) · 6.96 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {isLocalPath, parseSYProtocolBlockInfo, 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";
export const processSYLinkBlocks = (app: App, urlObj: URL): boolean => {
const blockInfo = parseSYProtocolBlockInfo(urlObj);
if (blockInfo != null) {
const {id, focus} = blockInfo;
window.siyuan.editorIsFullscreen = blockInfo.fullscreen;
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
}
app.plugins.forEach(plugin => {
plugin.eventBus.emit("open-siyuan-url-block", {
url: urlObj.href,
id,
focus,
exist: existResponse.data,
});
});
});
return true;
}
return false;
};
export const processSYLinkPlugins = (app: App, urlObj: URL): boolean => {
const pluginNameOrTabType: string | null = (() => {
const name = urlObj.pathname.split("/")[1];
if (!name) {
return null;
}
try {
return decodeURIComponent(name);
} catch (error) {
return null;
}
})();
if (!pluginNameOrTabType) {
return false;
}
const plugin = app.plugins.find(plugin => pluginNameOrTabType === plugin.name);
if (plugin) {
// siyuan://plugins/plugin-name/foo?bar=baz
plugin.eventBus.emit("open-siyuan-url-plugin", {url: urlObj.href});
} else {
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
/// #if !MOBILE
// https://github.com/siyuan-note/siyuan/pull/9256
const data = (() => {
try {
return JSON.parse(urlObj.searchParams.get("data") || "{}");
} catch (e) {
console.log("Error open plugin tab with protocol:", e);
return undefined;
}
})();
// id 不存在时无副作用
openFile({
app,
custom: {
title: urlObj.searchParams.get("title") ?? pluginNameOrTabType,
icon: urlObj.searchParams.get("icon") ?? "iconPlugin",
data,
id: pluginNameOrTabType
},
});
/// #endif
}
return true;
};
export const processSYLink = (app: App, url: string) => {
let urlObj: URL;
try {
urlObj = new URL(url);
if (urlObj.protocol !== "siyuan:" && urlObj.protocol !== "web+siyuan:") {
return false;
}
} catch (error) {
return false;
}
switch (urlObj.hostname) {
case "blocks":
return processSYLinkBlocks(app, urlObj);
case "plugins":
return processSYLinkPlugins(app, urlObj);
default:
break;
}
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
};