Vanessa 2025-12-01 20:24:03 +08:00
parent 348950ab51
commit 341ef4ed4c
3 changed files with 98 additions and 102 deletions

View file

@ -1,12 +1,99 @@
import {isLocalPath, pathPosix} from "../util/pathName";
import {getIdFromSYProtocol, isLocalPath, isSYProtocol, pathPosix} from "../util/pathName";
/// #if !BROWSER
import {shell} from "electron";
import {shell, ipcRenderer} from "electron";
/// #endif
import {getSearch} from "../util/functions";
import {openByMobile} from "../protyle/util/compatibility";
import {Constants} from "../constants";
import {showMessage} from "../dialog/message";
import {openAsset, openBy} from "./util";
/// #if !MOBILE
import {openAsset, openBy, openFile, openFileById} from "./util";
/// #endif
import {App} from "../index";
import {fetchPost} from "../util/fetch";
import {checkFold} from "../util/noRelyPCFunction";
import {openMobileFileById} from "../mobile/editor";
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";
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_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
zoomIn: zoomIn || focus
});
/// #else
openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, 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,
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);
@ -28,7 +115,7 @@ export const openLink = (protyle: IProtyle, aLink: string, event?: MouseEvent, c
if (Constants.SIYUAN_ASSETS_EXTS.includes(pathPosix().extname(linkAddress)) &&
(
!linkAddress.endsWith(".pdf") ||
// 本地 pdf 仅 assets/ 开头的才使用 siyuan 打开
// 本地 pdf 仅 assets/ 开头的才使用 siyuan 打开
(linkAddress.endsWith(".pdf") && linkAddress.startsWith("assets/"))
)
) {