Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions app/src/boot/onGetConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -243,39 +243,51 @@ 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)) {
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});
// 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);

// 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."}
let data = urlObj.searchParams.get("data");
try {
data = JSON.parse(data || "{}");
} catch (e) {
console.log("Error open plugin tab with protocol:", e);
}
openFile({
Zuoqiu-Yingyi marked this conversation as resolved.
Show resolved Hide resolved
app,
custom: {
title: urlObj.searchParams.get("title"),
icon: urlObj.searchParams.get("icon"),
data,
id: pluginPathname
},
});
}
openFile({
app,
custom: {
title: getSearch("title", url),
icon: getSearch("icon", url),
data,
id: pluginId
},
});
return true;
}
});
return;
}
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({
Expand Down
8 changes: 8 additions & 0 deletions app/src/util/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}