From 3bea0851e8a485576ef8445fa8a63f01b8a47384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Sat, 23 Sep 2023 17:50:18 +0800 Subject: [PATCH 1/3] :art: Improve plugin event bus `open-siyuan-url-plugin` --- app/src/boot/onGetConfig.ts | 54 ++++++++++++++++++++++--------------- app/src/util/functions.ts | 8 ++++++ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 25973cafffd..2373f7571af 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -16,7 +16,7 @@ import {renderSnippet} from "../config/util/snippets"; import {openFile, openFileById} from "../editor/util"; import {focusByRange} from "../protyle/util/selection"; import {exitSiYuan} from "../dialog/processSystem"; -import {getSearch, isWindow} from "../util/functions"; +import {getSearch, isWindow, trimPrefix} from "../util/functions"; import {initStatus} from "../layout/status"; import {showMessage} from "../dialog/message"; import {replaceLocalPath} from "../editor/rename"; @@ -243,31 +243,43 @@ export const initWindow = (app: App) => { currentWindow.on("blur", winOnBlur); if (!isWindow()) { ipcRenderer.on(Constants.SIYUAN_OPEN_URL, (event, url) => { - if (url.startsWith("siyuan://plugins/")) { - const pluginId = url.replace("siyuan://plugins/", "").split("?")[0]; - if (!pluginId) { + try { + var urlObj = new URL(url); + if (urlObj.protocol !== "siyuan:") { + return; + } + } catch (error) { + return; + } + if (urlObj.pathname.startsWith("//plugins/")) { + const pluginPathname = trimPrefix(urlObj.pathname, "//plugins/"); + if (!pluginPathname) { return; } app.plugins.find(plugin => { - if (pluginId.startsWith(plugin.name)) { + if (pluginPathname.startsWith(plugin.name)) { // siyuan://plugins/plugin-name/foo?bar=baz plugin.eventBus.emit("open-siyuan-url-plugin", {url}); - // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."} - let data = getSearch("data", url); - try { - data = JSON.parse(data || "{}"); - } catch (e) { - console.log("Error open plugin tab with protocol:", e); + + const pluginTabId = pluginPathname.split("/")[0]; + if (pluginTabId !== 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: pluginTabId + }, + }); } - openFile({ - app, - custom: { - title: getSearch("title", url), - icon: getSearch("icon", url), - data, - id: pluginId - }, - }); return true; } }); @@ -275,7 +287,7 @@ export const initWindow = (app: App) => { } if (isSYProtocol(url)) { const id = getIdFromSYProtocol(url); - const focus = getSearch("focus", url) === "1"; + const focus = urlObj.searchParams.get("focus") === "1"; fetchPost("/api/block/checkBlockExist", {id}, existResponse => { if (existResponse.data) { openFileById({ diff --git a/app/src/util/functions.ts b/app/src/util/functions.ts index 3733d5225df..87a396fd25e 100644 --- a/app/src/util/functions.ts +++ b/app/src/util/functions.ts @@ -89,3 +89,11 @@ export const objEquals = (a: any, b: any): boolean => { if (keys.length !== Object.keys(b).length) return false; return keys.every(k => objEquals(a[k], b[k])); }; + +export const trimPrefix = (str: string, prefix: string) => { + return str.startsWith(prefix) ? str.slice(prefix.length) : str; +} + +export const trimSuffix = (str: string, suffix: string) => { + return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; +} From e2c6e3450b87bcb2ea105e751b59c40b6478e4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Sat, 23 Sep 2023 18:04:48 +0800 Subject: [PATCH 2/3] :bug: Avoid plug-in names with the same prefix --- app/src/boot/onGetConfig.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 2373f7571af..b795f3841ef 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -256,12 +256,13 @@ export const initWindow = (app: App) => { if (!pluginPathname) { return; } - app.plugins.find(plugin => { + const pluginTabId = pluginPathname.split("/")[0]; + app.plugins.forEach(plugin => { if (pluginPathname.startsWith(plugin.name)) { // siyuan://plugins/plugin-name/foo?bar=baz plugin.eventBus.emit("open-siyuan-url-plugin", {url}); - - const pluginTabId = pluginPathname.split("/")[0]; + + // https://github.com/siyuan-note/siyuan/pull/9256 if (pluginTabId !== 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"); @@ -280,7 +281,6 @@ export const initWindow = (app: App) => { }, }); } - return true; } }); return; From 847bcff77f9e7b05683ab21e331d76201df67c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Sat, 23 Sep 2023 18:08:09 +0800 Subject: [PATCH 3/3] Update onGetConfig.ts --- app/src/boot/onGetConfig.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index b795f3841ef..e9c2e8673be 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -256,14 +256,14 @@ export const initWindow = (app: App) => { if (!pluginPathname) { return; } - const pluginTabId = pluginPathname.split("/")[0]; + const pluginId = pluginPathname.split("/")[0]; app.plugins.forEach(plugin => { if (pluginPathname.startsWith(plugin.name)) { // siyuan://plugins/plugin-name/foo?bar=baz plugin.eventBus.emit("open-siyuan-url-plugin", {url}); // https://github.com/siyuan-note/siyuan/pull/9256 - if (pluginTabId !== plugin.name) { + if (pluginId !== 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 { @@ -277,7 +277,7 @@ export const initWindow = (app: App) => { title: urlObj.searchParams.get("title"), icon: urlObj.searchParams.get("icon"), data, - id: pluginTabId + id: pluginPathname }, }); }