Browse Source

:art: https://github.com/siyuan-note/siyuan/issues/5066 openTab

Vanessa 2 years ago
parent
commit
9c45a296ae
3 changed files with 55 additions and 47 deletions
  1. 40 34
      app/src/editor/util.ts
  2. 4 0
      app/src/layout/Tab.ts
  3. 11 13
      app/src/plugin/API.ts

+ 40 - 34
app/src/editor/util.ts

@@ -9,7 +9,7 @@ import {Constants} from "../constants";
 import {setEditMode} from "../protyle/util/setEditMode";
 import {setEditMode} from "../protyle/util/setEditMode";
 import {Files} from "../layout/dock/Files";
 import {Files} from "../layout/dock/Files";
 import {setPadding} from "../protyle/ui/initUI";
 import {setPadding} from "../protyle/ui/initUI";
-import {fetchPost} from "../util/fetch";
+import {fetchPost, fetchSyncPost} from "../util/fetch";
 import {focusBlock, focusByRange} from "../protyle/util/selection";
 import {focusBlock, focusByRange} from "../protyle/util/selection";
 import {onGet} from "../protyle/util/onGet";
 import {onGet} from "../protyle/util/onGet";
 /// #if !BROWSER
 /// #if !BROWSER
@@ -40,25 +40,24 @@ export const openFileById = async (options: {
     removeCurrentTab?: boolean
     removeCurrentTab?: boolean
     afterOpen?: () => void
     afterOpen?: () => void
 }) => {
 }) => {
-    fetchPost("/api/block/getBlockInfo", {id: options.id}, (data) => {
-        if (data.code === 3) {
-            showMessage(data.msg);
-            return;
-        }
-        openFile({
-            app: options.app,
-            fileName: data.data.rootTitle,
-            rootIcon: data.data.rootIcon,
-            rootID: data.data.rootID,
-            id: options.id,
-            position: options.position,
-            mode: options.mode,
-            action: options.action,
-            zoomIn: options.zoomIn,
-            keepCursor: options.keepCursor,
-            removeCurrentTab: options.removeCurrentTab,
-            afterOpen: options.afterOpen
-        });
+    const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
+    if (response.code === 3) {
+        showMessage(response.msg);
+        return;
+    }
+    return openFile({
+        app: options.app,
+        fileName: response.data.rootTitle,
+        rootIcon: response.data.rootIcon,
+        rootID: response.data.rootID,
+        id: options.id,
+        position: options.position,
+        mode: options.mode,
+        action: options.action,
+        zoomIn: options.zoomIn,
+        keepCursor: options.keepCursor,
+        removeCurrentTab: options.removeCurrentTab,
+        afterOpen: options.afterOpen
     });
     });
 };
 };
 
 
@@ -97,7 +96,7 @@ export const openFile = (options: IOpenFileOptions) => {
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return;
+            return asset.parent;
         }
         }
     } else if (options.custom) {
     } else if (options.custom) {
         const custom = allModels.custom.find((item) => {
         const custom = allModels.custom.find((item) => {
@@ -113,14 +112,14 @@ export const openFile = (options: IOpenFileOptions) => {
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return;
+            return custom.parent;
         }
         }
         const hasModel = getUnInitTab(options);
         const hasModel = getUnInitTab(options);
         if (hasModel) {
         if (hasModel) {
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return;
+            return hasModel;
         }
         }
     } else if (options.searchData) {
     } else if (options.searchData) {
         const search = allModels.search.find((item) => {
         const search = allModels.search.find((item) => {
@@ -133,7 +132,7 @@ export const openFile = (options: IOpenFileOptions) => {
             }
             }
         });
         });
         if (search) {
         if (search) {
-            return;
+            return search.parent;
         }
         }
     } else if (!options.position) {
     } else if (!options.position) {
         let editor: Editor;
         let editor: Editor;
@@ -159,7 +158,7 @@ export const openFile = (options: IOpenFileOptions) => {
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return true;
+            return editor.parent;
         }
         }
         // 没有初始化的页签无法检测到
         // 没有初始化的页签无法检测到
         const hasEditor = getUnInitTab(options);
         const hasEditor = getUnInitTab(options);
@@ -167,7 +166,7 @@ export const openFile = (options: IOpenFileOptions) => {
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return;
+            return hasEditor;
         }
         }
     }
     }
 
 
@@ -208,6 +207,7 @@ export const openFile = (options: IOpenFileOptions) => {
         wnd = getWndByLayout(window.siyuan.layout.centerLayout);
         wnd = getWndByLayout(window.siyuan.layout.centerLayout);
     }
     }
     if (wnd) {
     if (wnd) {
+        let createdTab: Tab;
         if ((options.position === "right" || options.position === "bottom") && wnd.children[0].headElement) {
         if ((options.position === "right" || options.position === "bottom") && wnd.children[0].headElement) {
             const direction = options.position === "right" ? "lr" : "tb";
             const direction = options.position === "right" ? "lr" : "tb";
             let targetWnd: Wnd;
             let targetWnd: Wnd;
@@ -239,18 +239,21 @@ export const openFile = (options: IOpenFileOptions) => {
                 });
                 });
                 if (!hasEditor) {
                 if (!hasEditor) {
                     hasEditor = getUnInitTab(options);
                     hasEditor = getUnInitTab(options);
+                    createdTab = hasEditor;
                 }
                 }
                 if (!hasEditor) {
                 if (!hasEditor) {
-                    targetWnd.addTab(newTab(options));
+                    createdTab = newTab(options);
+                    targetWnd.addTab(createdTab);
                 }
                 }
             } else {
             } else {
-                wnd.split(direction).addTab(newTab(options));
+                createdTab = newTab(options);
+                wnd.split(direction).addTab(createdTab);
             }
             }
             wnd.showHeading();
             wnd.showHeading();
             if (options.afterOpen) {
             if (options.afterOpen) {
                 options.afterOpen();
                 options.afterOpen();
             }
             }
-            return;
+            return createdTab;
         }
         }
         if (pdfIsLoading(wnd.element)) {
         if (pdfIsLoading(wnd.element)) {
             if (options.afterOpen) {
             if (options.afterOpen) {
@@ -259,9 +262,9 @@ export const openFile = (options: IOpenFileOptions) => {
             return;
             return;
         }
         }
         if (options.keepCursor && wnd.children[0].headElement) {
         if (options.keepCursor && wnd.children[0].headElement) {
-            const tab = newTab(options);
-            tab.headElement.setAttribute("keep-cursor", options.id);
-            wnd.addTab(tab, options.keepCursor);
+            createdTab = newTab(options);
+            createdTab.headElement.setAttribute("keep-cursor", options.id);
+            wnd.addTab(createdTab, options.keepCursor);
         } else if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
         } else if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
             let unUpdateTab: Tab;
             let unUpdateTab: Tab;
             // 不能 reverse, 找到也不能提前退出循环,否则 https://github.com/siyuan-note/siyuan/issues/3271
             // 不能 reverse, 找到也不能提前退出循环,否则 https://github.com/siyuan-note/siyuan/issues/3271
@@ -274,17 +277,20 @@ export const openFile = (options: IOpenFileOptions) => {
                     }
                     }
                 }
                 }
             });
             });
-            wnd.addTab(newTab(options));
+            createdTab = newTab(options)
+            wnd.addTab(createdTab);
             if (unUpdateTab && options.removeCurrentTab) {
             if (unUpdateTab && options.removeCurrentTab) {
                 wnd.removeTab(unUpdateTab.id, false, true, false);
                 wnd.removeTab(unUpdateTab.id, false, true, false);
             }
             }
         } else {
         } else {
-            wnd.addTab(newTab(options));
+            createdTab = newTab(options)
+            wnd.addTab(createdTab);
         }
         }
         wnd.showHeading();
         wnd.showHeading();
         if (options.afterOpen) {
         if (options.afterOpen) {
             options.afterOpen();
             options.afterOpen();
         }
         }
+        return createdTab;
     }
     }
 };
 };
 
 

+ 4 - 0
app/src/layout/Tab.ts

@@ -210,4 +210,8 @@ export class Tab {
             this.headElement.querySelector(".item__text").classList.remove("fn__none");
             this.headElement.querySelector(".item__text").classList.remove("fn__none");
         }
         }
     }
     }
+
+    public close() {
+        this.parent.removeTab(this.id);
+    }
 }
 }

+ 11 - 13
app/src/plugin/API.ts

@@ -12,6 +12,7 @@ import {updateHotkeyTip} from "../protyle/util/compatibility";
 import {newCardModel} from "../card/newCardTab";
 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";
 
 
 export class Menu {
 export class Menu {
     private menu: SiyuanMenu;
     private menu: SiyuanMenu;
@@ -102,7 +103,7 @@ openTab = (options: {
         title: string,
         title: string,
         icon: string,
         icon: string,
         data?: any
         data?: any
-        fn?: () => any,
+        fn?: () => Model,
     }
     }
     position?: "right" | "bottom",
     position?: "right" | "bottom",
     keepCursor?: boolean // 是否跳转到新 tab 上
     keepCursor?: boolean // 是否跳转到新 tab 上
@@ -117,7 +118,10 @@ openTab = (options: {
                 options.doc.action = [Constants.CB_GET_ALL];
                 options.doc.action = [Constants.CB_GET_ALL];
             }
             }
         }
         }
-        openFileById({
+        if (!options.doc.action) {
+            options.doc.action = []
+        }
+        return openFileById({
             app: options.app,
             app: options.app,
             keepCursor: options.keepCursor,
             keepCursor: options.keepCursor,
             removeCurrentTab: options.removeCurrentTab,
             removeCurrentTab: options.removeCurrentTab,
@@ -127,10 +131,9 @@ openTab = (options: {
             action: options.doc.action,
             action: options.doc.action,
             zoomIn: options.doc.zoomIn
             zoomIn: options.doc.zoomIn
         });
         });
-        return;
     }
     }
     if (options.asset) {
     if (options.asset) {
-        openFile({
+        return openFile({
             app: options.app,
             app: options.app,
             keepCursor: options.keepCursor,
             keepCursor: options.keepCursor,
             removeCurrentTab: options.removeCurrentTab,
             removeCurrentTab: options.removeCurrentTab,
@@ -138,10 +141,9 @@ openTab = (options: {
             afterOpen: options.afterOpen,
             afterOpen: options.afterOpen,
             assetPath: options.asset.path,
             assetPath: options.asset.path,
         });
         });
-        return;
     }
     }
     if (options.pdf) {
     if (options.pdf) {
-        openFile({
+        return openFile({
             app: options.app,
             app: options.app,
             keepCursor: options.keepCursor,
             keepCursor: options.keepCursor,
             removeCurrentTab: options.removeCurrentTab,
             removeCurrentTab: options.removeCurrentTab,
@@ -150,7 +152,6 @@ openTab = (options: {
             assetPath: options.pdf.path,
             assetPath: options.pdf.path,
             page: options.pdf.id || options.pdf.page,
             page: options.pdf.id || options.pdf.page,
         });
         });
-        return;
     }
     }
     if (options.search) {
     if (options.search) {
         if (!options.search.idPath) {
         if (!options.search.idPath) {
@@ -159,7 +160,7 @@ openTab = (options: {
         if (!options.search.hPath) {
         if (!options.search.hPath) {
             options.search.hPath = "";
             options.search.hPath = "";
         }
         }
-        openFile({
+        return openFile({
             app: options.app,
             app: options.app,
             keepCursor: options.keepCursor,
             keepCursor: options.keepCursor,
             removeCurrentTab: options.removeCurrentTab,
             removeCurrentTab: options.removeCurrentTab,
@@ -167,10 +168,9 @@ openTab = (options: {
             afterOpen: options.afterOpen,
             afterOpen: options.afterOpen,
             searchData: options.search,
             searchData: options.search,
         });
         });
-        return;
     }
     }
     if (options.card) {
     if (options.card) {
-        openFile({
+        return openFile({
             app: options.app,
             app: options.app,
             keepCursor: options.keepCursor,
             keepCursor: options.keepCursor,
             removeCurrentTab: options.removeCurrentTab,
             removeCurrentTab: options.removeCurrentTab,
@@ -187,11 +187,9 @@ openTab = (options: {
                 fn: newCardModel
                 fn: newCardModel
             },
             },
         });
         });
-        return;
     }
     }
     if (options.custom) {
     if (options.custom) {
-        openFile(options);
-        return;
+        return openFile(options);
     }
     }
 
 
 };
 };