瀏覽代碼

:art: fix https://github.com/siyuan-note/siyuan/issues/8944

Vanessa 1 年之前
父節點
當前提交
f2d015267d
共有 6 個文件被更改,包括 49 次插入34 次删除
  1. 14 19
      app/src/boot/onGetConfig.ts
  2. 1 1
      app/src/card/openCard.ts
  3. 28 6
      app/src/editor/util.ts
  4. 1 1
      app/src/layout/util.ts
  5. 3 5
      app/src/plugin/API.ts
  6. 2 2
      app/src/types/index.d.ts

+ 14 - 19
app/src/boot/onGetConfig.ts

@@ -259,25 +259,20 @@ export const initWindow = (app: App) => {
                         // 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});
                         // 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."}
-                        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;
                         return true;
                     }
                     }

+ 1 - 1
app/src/card/openCard.ts

@@ -221,7 +221,7 @@ export const bindCardEvent = (options: {
                             id: filterElement.getAttribute("data-id"),
                             id: filterElement.getAttribute("data-id"),
                             title: options.title
                             title: options.title
                         },
                         },
-                        fn: newCardModel
+                        id: "siyuan-card"
                     },
                     },
                 });
                 });
                 if (options.dialog) {
                 if (options.dialog) {

+ 28 - 6
app/src/editor/util.ts

@@ -28,6 +28,7 @@ import {objEquals} from "../util/functions";
 import {resize} from "../protyle/util/resize";
 import {resize} from "../protyle/util/resize";
 import {Search} from "../search";
 import {Search} from "../search";
 import {App} from "../index";
 import {App} from "../index";
+import {newCardModel} from "../card/newCardTab";
 
 
 export const openFileById = async (options: {
 export const openFileById = async (options: {
     app: App,
     app: App,
@@ -100,7 +101,7 @@ export const openFile = (options: IOpenFileOptions) => {
         }
         }
     } else if (options.custom) {
     } else if (options.custom) {
         const custom = allModels.custom.find((item) => {
         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)) {
                 if (!pdfIsLoading(item.parent.parent.element)) {
                     item.parent.parent.switchTab(item.parent.headElement);
                     item.parent.parent.switchTab(item.parent.headElement);
                     item.parent.parent.showHeading();
                     item.parent.parent.showHeading();
@@ -427,11 +428,32 @@ const newTab = (options: IOpenFileOptions) => {
             icon: options.custom.icon,
             icon: options.custom.icon,
             title: options.custom.title,
             title: options.custom.title,
             callback(tab) {
             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);
                 setPanelFocus(tab.panelElement.parentElement.parentElement);
             }
             }
         });
         });

+ 1 - 1
app/src/layout/util.ts

@@ -412,7 +412,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
         const initData = item.getAttribute("data-initdata");
         const initData = item.getAttribute("data-initdata");
         if (initData) {
         if (initData) {
             const initDataObj = JSON.parse(initData);
             const initDataObj = JSON.parse(initData);
-            if (initDataObj.instance === "Custom") {
+            if (initDataObj.instance === "Custom" && initDataObj.customModelType !== "siyuan-card") {
                 let hasPlugin = false;
                 let hasPlugin = false;
                 app.plugins.find(plugin => {
                 app.plugins.find(plugin => {
                     if (Object.keys(plugin.models).includes(initDataObj.customModelType)) {
                     if (Object.keys(plugin.models).includes(initDataObj.customModelType)) {

+ 3 - 5
app/src/plugin/API.ts

@@ -8,13 +8,11 @@ import {getBackend, getFrontend} from "../util/functions";
 import {openFile, openFileById} from "../editor/util";
 import {openFile, openFileById} from "../editor/util";
 /// #endif
 /// #endif
 import {updateHotkeyTip} from "../protyle/util/compatibility";
 import {updateHotkeyTip} from "../protyle/util/compatibility";
-import {newCardModel} from "../card/newCardTab";
 import {App} from "../index";
 import {App} from "../index";
 import {Constants} from "../constants";
 import {Constants} from "../constants";
-import {Model} from "../layout/Model";
 import {Setting} from "./Setting";
 import {Setting} from "./Setting";
 import {Menu} from "./Menu";
 import {Menu} from "./Menu";
-import { Protyle } from "../protyle";
+import {Protyle} from "../protyle";
 
 
 let openTab;
 let openTab;
 /// #if MOBILE
 /// #if MOBILE
@@ -47,7 +45,7 @@ openTab = (options: {
         title: string,
         title: string,
         icon: string,
         icon: string,
         data?: any
         data?: any
-        fn?: () => Model,
+        id: string
     }
     }
     position?: "right" | "bottom",
     position?: "right" | "bottom",
     keepCursor?: boolean // 是否跳转到新 tab 上
     keepCursor?: boolean // 是否跳转到新 tab 上
@@ -128,7 +126,7 @@ openTab = (options: {
                     id: options.card.id || "",
                     id: options.card.id || "",
                     title: options.card.title,
                     title: options.card.title,
                 },
                 },
-                fn: newCardModel
+                id: "siyuan-card"
             },
             },
         });
         });
     }
     }

+ 2 - 2
app/src/types/index.d.ts

@@ -437,11 +437,11 @@ interface IOpenFileOptions {
         title: string,
         title: string,
         icon: string,
         icon: string,
         data?: any
         data?: any
+        id: string,
         fn?: (options: {
         fn?: (options: {
             tab: import("../layout/Tab").Tab,
             tab: import("../layout/Tab").Tab,
             data: any,
             data: any,
-            app: import("../index").App
-        }) => import("../layout/Model").Model,
+        }) => import("../layout/Model").Model,   // plugin 0.8.3 历史兼容
     }
     }
     assetPath?: string, // asset 必填
     assetPath?: string, // asset 必填
     fileName?: string, // file 必填
     fileName?: string, // file 必填