Improve event bus open-siyuan-url-plugin (#9256)

* 🎨 Improve plugin event bus `open-siyuan-url-plugin`

* 🐛 Avoid plug-in names with the same prefix

* Update onGetConfig.ts
This commit is contained in:
Yingyi / 颖逸 2023-10-10 16:21:50 +08:00 committed by GitHub
parent 2c36af78bc
commit 11786381cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 23 deletions

View file

@ -15,7 +15,7 @@ import {renderSnippet} from "../config/util/snippets";
import {openFile, openFileById} from "../editor/util"; import {openFile, openFileById} from "../editor/util";
import {focusByRange} from "../protyle/util/selection"; import {focusByRange} from "../protyle/util/selection";
import {exitSiYuan} from "../dialog/processSystem"; import {exitSiYuan} from "../dialog/processSystem";
import {getSearch, isWindow} from "../util/functions"; import {getSearch, isWindow, trimPrefix} from "../util/functions";
import {initStatus} from "../layout/status"; import {initStatus} from "../layout/status";
import {showMessage} from "../dialog/message"; import {showMessage} from "../dialog/message";
import {replaceLocalPath} from "../editor/rename"; import {replaceLocalPath} from "../editor/rename";
@ -247,17 +247,29 @@ export const initWindow = async (app: App) => {
}); });
if (!isWindow()) { if (!isWindow()) {
ipcRenderer.on(Constants.SIYUAN_OPEN_URL, (event, url) => { ipcRenderer.on(Constants.SIYUAN_OPEN_URL, (event, url) => {
if (url.startsWith("siyuan://plugins/")) { try {
const pluginId = url.replace("siyuan://plugins/", "").split("?")[0]; var urlObj = new URL(url);
if (!pluginId) { if (urlObj.protocol !== "siyuan:") {
return; return;
} }
app.plugins.find(plugin => { } catch (error) {
if (pluginId.startsWith(plugin.name)) { return;
}
if (urlObj.pathname.startsWith("//plugins/")) {
const pluginPathname = trimPrefix(urlObj.pathname, "//plugins/");
if (!pluginPathname) {
return;
}
const pluginId = pluginPathname.split("/")[0];
app.plugins.forEach(plugin => {
if (pluginPathname.startsWith(plugin.name)) {
// siyuan://plugins/plugin-name/foo?bar=baz // siyuan://plugins/plugin-name/foo?bar=baz
plugin.eventBus.emit("open-siyuan-url-plugin", {url}); plugin.eventBus.emit("open-siyuan-url-plugin", {url});
// https://github.com/siyuan-note/siyuan/pull/9256
if (pluginId !== plugin.name) {
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."} // 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); let data = urlObj.searchParams.get("data");
try { try {
data = JSON.parse(data || "{}"); data = JSON.parse(data || "{}");
} catch (e) { } catch (e) {
@ -266,20 +278,20 @@ export const initWindow = async (app: App) => {
openFile({ openFile({
app, app,
custom: { custom: {
title: getSearch("title", url), title: urlObj.searchParams.get("title"),
icon: getSearch("icon", url), icon: urlObj.searchParams.get("icon"),
data, data,
id: pluginId id: pluginPathname
}, },
}); });
return true; }
} }
}); });
return; return;
} }
if (isSYProtocol(url)) { if (isSYProtocol(url)) {
const id = getIdFromSYProtocol(url); const id = getIdFromSYProtocol(url);
const focus = getSearch("focus", url) === "1"; const focus = urlObj.searchParams.get("focus") === "1";
fetchPost("/api/block/checkBlockExist", {id}, existResponse => { fetchPost("/api/block/checkBlockExist", {id}, existResponse => {
if (existResponse.data) { if (existResponse.data) {
openFileById({ openFileById({

View file

@ -90,3 +90,11 @@ export const objEquals = (a: any, b: any): boolean => {
if (keys.length !== Object.keys(b).length) return false; if (keys.length !== Object.keys(b).length) return false;
return keys.every(k => objEquals(a[k], b[k])); 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;
}