From f2d015267d45a992c6b347a448932de7ad142277 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 18 Aug 2023 17:38:11 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/8944 --- app/src/boot/onGetConfig.ts | 33 ++++++++++++++------------------- app/src/card/openCard.ts | 2 +- app/src/editor/util.ts | 34 ++++++++++++++++++++++++++++------ app/src/layout/util.ts | 2 +- app/src/plugin/API.ts | 8 +++----- app/src/types/index.d.ts | 4 ++-- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index d86a1b8f9..8a30b281c 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -259,25 +259,20 @@ export const initWindow = (app: App) => { // 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."} - 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; - } + 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, + id: pluginId + }, }); return true; } diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts index 09a7b4cf7..acc42cb00 100644 --- a/app/src/card/openCard.ts +++ b/app/src/card/openCard.ts @@ -221,7 +221,7 @@ export const bindCardEvent = (options: { id: filterElement.getAttribute("data-id"), title: options.title }, - fn: newCardModel + id: "siyuan-card" }, }); if (options.dialog) { diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index 4e467661a..ad67a3dec 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -28,6 +28,7 @@ import {objEquals} from "../util/functions"; import {resize} from "../protyle/util/resize"; import {Search} from "../search"; import {App} from "../index"; +import {newCardModel} from "../card/newCardTab"; export const openFileById = async (options: { app: App, @@ -100,7 +101,7 @@ export const openFile = (options: IOpenFileOptions) => { } } else if (options.custom) { const custom = allModels.custom.find((item) => { - if (objEquals(item.data, options.custom.data)) { + if (objEquals(item.data, options.custom.data) && (!options.custom.id || options.custom.id === item.type)) { if (!pdfIsLoading(item.parent.parent.element)) { item.parent.parent.switchTab(item.parent.headElement); item.parent.parent.showHeading(); @@ -427,11 +428,32 @@ const newTab = (options: IOpenFileOptions) => { icon: options.custom.icon, title: options.custom.title, callback(tab) { - tab.addModel(options.custom.fn({ - app: options.app, - tab, - data: options.custom.data - })); + if (options.custom.id) { + if (options.custom.id === "siyuan-card") { + tab.addModel(newCardModel({ + app: options.app, + tab, + data: options.custom.data + })); + } else { + options.app.plugins.find(p => { + if (p.models[options.custom.id]) { + tab.addModel(p.models[options.custom.id]({ + tab, + data: options.custom.data + })); + return true; + } + }) + } + } else { + // plugin 0.8.3 历史兼容 + console.warn("0.8.3 将移除 custom.fn 参数,请参照 https://github.com/siyuan-note/plugin-sample/blob/91a716358941791b4269241f21db25fd22ae5ff5/src/index.ts 将其修改为 custom.id"); + tab.addModel(options.custom.fn({ + tab, + data: options.custom.data + })); + } setPanelFocus(tab.panelElement.parentElement.parentElement); } }); diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index 7fe32ccb4..ba9fad22f 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -412,7 +412,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => { const initData = item.getAttribute("data-initdata"); if (initData) { const initDataObj = JSON.parse(initData); - if (initDataObj.instance === "Custom") { + if (initDataObj.instance === "Custom" && initDataObj.customModelType !== "siyuan-card") { let hasPlugin = false; app.plugins.find(plugin => { if (Object.keys(plugin.models).includes(initDataObj.customModelType)) { diff --git a/app/src/plugin/API.ts b/app/src/plugin/API.ts index dff75e0d9..5c45bd229 100644 --- a/app/src/plugin/API.ts +++ b/app/src/plugin/API.ts @@ -8,13 +8,11 @@ import {getBackend, getFrontend} from "../util/functions"; import {openFile, openFileById} from "../editor/util"; /// #endif import {updateHotkeyTip} from "../protyle/util/compatibility"; -import {newCardModel} from "../card/newCardTab"; import {App} from "../index"; import {Constants} from "../constants"; -import {Model} from "../layout/Model"; import {Setting} from "./Setting"; import {Menu} from "./Menu"; -import { Protyle } from "../protyle"; +import {Protyle} from "../protyle"; let openTab; /// #if MOBILE @@ -47,7 +45,7 @@ openTab = (options: { title: string, icon: string, data?: any - fn?: () => Model, + id: string } position?: "right" | "bottom", keepCursor?: boolean // 是否跳转到新 tab 上 @@ -128,7 +126,7 @@ openTab = (options: { id: options.card.id || "", title: options.card.title, }, - fn: newCardModel + id: "siyuan-card" }, }); } diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 8969a37c3..25d56b5eb 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -437,11 +437,11 @@ interface IOpenFileOptions { title: string, icon: string, data?: any + id: string, fn?: (options: { tab: import("../layout/Tab").Tab, data: any, - app: import("../index").App - }) => import("../layout/Model").Model, + }) => import("../layout/Model").Model, // plugin 0.8.3 历史兼容 } assetPath?: string, // asset 必填 fileName?: string, // file 必填