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
115 lines (113 loc) · 4.31 KB
/
Copy pathopenLink.ts
File metadata and controls
115 lines (113 loc) · 4.31 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
import {isLocalPath, pathPosix} from "../util/pathName";
/// #if !BROWSER
import {shell} from "electron";
/// #endif
import {getSearch} from "../util/functions";
import {Constants} from "../constants";
/// #if MOBILE
import {processSiYuanUri} from "../util/uri";
/// #else
import {openAsset, openBy} from "./util";
/// #endif
import {showMessage} from "../dialog/message";
import {isInIOS, isInAndroid, isInHarmony} from "../protyle/util/compatibility";
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
};
export const openByMobile = (uri: string) => {
if (!uri) {
return;
}
/// #if MOBILE
if (processSiYuanUri(window.siyuan.ws.app, uri)) {
return;
}
/// #endif
if (isInIOS()) {
if (uri.startsWith("assets/")) {
// iOS 16.7 之前的版本,uri 需要 encodeURIComponent
window.webkit.messageHandlers.openLink.postMessage(location.origin + "/assets/" + encodeURIComponent(uri.replace("assets/", "")));
} else if (uri.startsWith("/")) {
// 导出 zip 返回的是已经 encode 过的,因此不能再 encode
window.webkit.messageHandlers.openLink.postMessage(location.origin + uri);
} else {
try {
new URL(uri);
window.webkit.messageHandlers.openLink.postMessage(uri);
} catch (e) {
window.webkit.messageHandlers.openLink.postMessage("https://" + uri);
}
}
} else if (isInAndroid()) {
window.JSAndroid.openExternal(uri);
} else if (isInHarmony()) {
window.JSHarmony.openExternal(uri);
} else {
window.open(uri);
}
};