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`
This commit is contained in:
Yingyi / 颖逸 2023-08-12 00:05:19 +08:00 committed by GitHub
parent ec4e0c0d14
commit 75ad2a2986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 26 deletions

View file

@ -249,48 +249,68 @@ export const initWindow = (app: App) => {
currentWindow.on("blur", winOnBlur); currentWindow.on("blur", winOnBlur);
if (!isWindow()) { if (!isWindow()) {
ipcRenderer.on(Constants.SIYUAN_OPENURL, (event, url) => { ipcRenderer.on(Constants.SIYUAN_OPENURL, (event, url) => {
if (/^siyuan:\/\/plugins\//.test(url)) { app.plugins.forEach(plugin => {
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."} plugin.eventBus.emit("open-siyuan-url", { url });
const pluginId = url.replace("siyuan://plugins/", "").split("?")[0]; });
app.plugins.find(plugin => { if (url.startsWith("siyuan://plugins/")) {
const match = Object.keys(plugin.models).find(key => { const urlObj = new URL(url);
if (key === pluginId) { const pluginId = urlObj.pathname.split("/")[3];
let data = getSearch("data", url); if (pluginId) {
try { app.plugins.find(plugin => {
data = JSON.parse(data || "{}"); // siyuan://plugins/plugin-name/foo?bar=baz
} catch (e) { if (pluginId.startsWith(plugin.name)) {
console.log("Error open plugin tab with protocol:", e); 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, if (match) {
custom: {
title: getSearch("title", url),
icon: getSearch("icon", url),
data,
fn: plugin.models[key]
},
});
return true; return true;
} }
}); });
if (match) { return;
return true; }
}
});
return;
} }
if (isSYProtocol(url)) { if (isSYProtocol(url)) {
const id = getIdFromSYProtocol(url); const id = getIdFromSYProtocol(url);
const focus = getSearch("focus", url) === "1";
fetchPost("/api/block/checkBlockExist", {id}, existResponse => { fetchPost("/api/block/checkBlockExist", {id}, existResponse => {
if (existResponse.data) { if (existResponse.data) {
openFileById({ openFileById({
app, app,
id, id,
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
zoomIn: getSearch("focus", url) === "1" zoomIn: focus,
}); });
ipcRenderer.send(Constants.SIYUAN_SHOW, getCurrentWindow().id); 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; return;
} }

View file

@ -44,6 +44,7 @@ type TEventBus = "ws-main" |
"open-noneditableblock" | "open-noneditableblock" |
"open-menu-blockref" | "open-menu-fileannotationref" | "open-menu-tag" | "open-menu-link" | "open-menu-image" | "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-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" |
"open-siyuan-url" | "open-siyuan-url-blocks" | "open-siyuan-url-plugins" |
"input-search" | "input-search" |
"loaded-protyle" "loaded-protyle"
type TAVCol = type TAVCol =