From 75ad2a298624271813aa7125b218fd8b84dec0d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yingyi=20/=20=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Sat, 12 Aug 2023 00:05:19 +0800 Subject: [PATCH] Add plugin event bus `open-siyuan-url` (#8927) * feat: Add plugin event bus `open-siyuan-url` * feat: Add plugin event bus `open-siyuan-url-blocks` and `open-siyuan-url-plugins` * perf: improve plugin event bus `open-siyuan-url-blocks` --- app/src/boot/onGetConfig.ts | 72 +++++++++++++++++++++++-------------- app/src/types/index.d.ts | 1 + 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 052d0cd89..d215af421 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -249,48 +249,68 @@ export const initWindow = (app: App) => { currentWindow.on("blur", winOnBlur); if (!isWindow()) { ipcRenderer.on(Constants.SIYUAN_OPENURL, (event, url) => { - if (/^siyuan:\/\/plugins\//.test(url)) { - // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."} - const pluginId = url.replace("siyuan://plugins/", "").split("?")[0]; - app.plugins.find(plugin => { - const match = Object.keys(plugin.models).find(key => { - if (key === pluginId) { - let data = getSearch("data", url); - try { - data = JSON.parse(data || "{}"); - } catch (e) { - console.log("Error open plugin tab with protocol:", e); + app.plugins.forEach(plugin => { + plugin.eventBus.emit("open-siyuan-url", { url }); + }); + if (url.startsWith("siyuan://plugins/")) { + const urlObj = new URL(url); + const pluginId = urlObj.pathname.split("/")[3]; + if (pluginId) { + app.plugins.find(plugin => { + // siyuan://plugins/plugin-name/foo?bar=baz + if (pluginId.startsWith(plugin.name)) { + plugin.eventBus.emit("open-siyuan-url-plugins", { url }); + } + + // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."} + const match = Object.keys(plugin.models).find(key => { + if (key === pluginId) { + let data = getSearch("data", url); + try { + data = JSON.parse(data || "{}"); + } catch (e) { + console.log("Error open plugin tab with protocol:", e); + } + openFile({ + app, + custom: { + title: getSearch("title", url), + icon: getSearch("icon", url), + data, + fn: plugin.models[key] + }, + }); + return true; } - openFile({ - app, - custom: { - title: getSearch("title", url), - icon: getSearch("icon", url), - data, - fn: plugin.models[key] - }, - }); + }); + if (match) { return true; } }); - if (match) { - return true; - } - }); - return; + return; + } } if (isSYProtocol(url)) { const id = getIdFromSYProtocol(url); + const focus = getSearch("focus", url) === "1"; fetchPost("/api/block/checkBlockExist", {id}, existResponse => { if (existResponse.data) { openFileById({ app, id, action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], - zoomIn: getSearch("focus", url) === "1" + zoomIn: focus, }); ipcRenderer.send(Constants.SIYUAN_SHOW, getCurrentWindow().id); } + app.plugins.forEach(plugin => { + plugin.eventBus.emit("open-siyuan-url-blocks", { + url, + id, + focus, + exist: existResponse.data, + }); + }); }); return; } diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 9766cd356..37084fc7e 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -44,6 +44,7 @@ type TEventBus = "ws-main" | "open-noneditableblock" | "open-menu-blockref" | "open-menu-fileannotationref" | "open-menu-tag" | "open-menu-link" | "open-menu-image" | "open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" | + "open-siyuan-url" | "open-siyuan-url-blocks" | "open-siyuan-url-plugins" | "input-search" | "loaded-protyle" type TAVCol =