Skip to content

Commit 75ad2a2

Browse files
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`
1 parent ec4e0c0 commit 75ad2a2

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

app/src/boot/onGetConfig.ts

+46-26
Original file line numberDiff line numberDiff line change
@@ -249,48 +249,68 @@ export const initWindow = (app: App) => {
249249
currentWindow.on("blur", winOnBlur);
250250
if (!isWindow()) {
251251
ipcRenderer.on(Constants.SIYUAN_OPENURL, (event, url) => {
252-
if (/^siyuan:\/\/plugins\//.test(url)) {
253-
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
254-
const pluginId = url.replace("siyuan://plugins/", "").split("?")[0];
255-
app.plugins.find(plugin => {
256-
const match = Object.keys(plugin.models).find(key => {
257-
if (key === pluginId) {
258-
let data = getSearch("data", url);
259-
try {
260-
data = JSON.parse(data || "{}");
261-
} catch (e) {
262-
console.log("Error open plugin tab with protocol:", e);
252+
app.plugins.forEach(plugin => {
253+
plugin.eventBus.emit("open-siyuan-url", { url });
254+
});
255+
if (url.startsWith("siyuan://plugins/")) {
256+
const urlObj = new URL(url);
257+
const pluginId = urlObj.pathname.split("/")[3];
258+
if (pluginId) {
259+
app.plugins.find(plugin => {
260+
// siyuan://plugins/plugin-name/foo?bar=baz
261+
if (pluginId.startsWith(plugin.name)) {
262+
plugin.eventBus.emit("open-siyuan-url-plugins", { url });
263+
}
264+
265+
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
266+
const match = Object.keys(plugin.models).find(key => {
267+
if (key === pluginId) {
268+
let data = getSearch("data", url);
269+
try {
270+
data = JSON.parse(data || "{}");
271+
} catch (e) {
272+
console.log("Error open plugin tab with protocol:", e);
273+
}
274+
openFile({
275+
app,
276+
custom: {
277+
title: getSearch("title", url),
278+
icon: getSearch("icon", url),
279+
data,
280+
fn: plugin.models[key]
281+
},
282+
});
283+
return true;
263284
}
264-
openFile({
265-
app,
266-
custom: {
267-
title: getSearch("title", url),
268-
icon: getSearch("icon", url),
269-
data,
270-
fn: plugin.models[key]
271-
},
272-
});
285+
});
286+
if (match) {
273287
return true;
274288
}
275289
});
276-
if (match) {
277-
return true;
278-
}
279-
});
280-
return;
290+
return;
291+
}
281292
}
282293
if (isSYProtocol(url)) {
283294
const id = getIdFromSYProtocol(url);
295+
const focus = getSearch("focus", url) === "1";
284296
fetchPost("/api/block/checkBlockExist", {id}, existResponse => {
285297
if (existResponse.data) {
286298
openFileById({
287299
app,
288300
id,
289301
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
290-
zoomIn: getSearch("focus", url) === "1"
302+
zoomIn: focus,
291303
});
292304
ipcRenderer.send(Constants.SIYUAN_SHOW, getCurrentWindow().id);
293305
}
306+
app.plugins.forEach(plugin => {
307+
plugin.eventBus.emit("open-siyuan-url-blocks", {
308+
url,
309+
id,
310+
focus,
311+
exist: existResponse.data,
312+
});
313+
});
294314
});
295315
return;
296316
}

app/src/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type TEventBus = "ws-main" |
4444
"open-noneditableblock" |
4545
"open-menu-blockref" | "open-menu-fileannotationref" | "open-menu-tag" | "open-menu-link" | "open-menu-image" |
4646
"open-menu-av" | "open-menu-content" | "open-menu-breadcrumbmore" |
47+
"open-siyuan-url" | "open-siyuan-url-blocks" | "open-siyuan-url-plugins" |
4748
"input-search" |
4849
"loaded-protyle"
4950
type TAVCol =

0 commit comments

Comments
 (0)