diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index 477c3ecc8..688c7a40b 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -9,7 +9,7 @@ import {Constants} from "../constants"; import {setEditMode} from "../protyle/util/setEditMode"; import {Files} from "../layout/dock/Files"; 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 {onGet} from "../protyle/util/onGet"; /// #if !BROWSER @@ -40,25 +40,24 @@ export const openFileById = async (options: { removeCurrentTab?: boolean 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) { options.afterOpen(); } - return; + return asset.parent; } } else if (options.custom) { const custom = allModels.custom.find((item) => { @@ -113,14 +112,14 @@ export const openFile = (options: IOpenFileOptions) => { if (options.afterOpen) { options.afterOpen(); } - return; + return custom.parent; } const hasModel = getUnInitTab(options); if (hasModel) { if (options.afterOpen) { options.afterOpen(); } - return; + return hasModel; } } else if (options.searchData) { const search = allModels.search.find((item) => { @@ -133,7 +132,7 @@ export const openFile = (options: IOpenFileOptions) => { } }); if (search) { - return; + return search.parent; } } else if (!options.position) { let editor: Editor; @@ -159,7 +158,7 @@ export const openFile = (options: IOpenFileOptions) => { if (options.afterOpen) { options.afterOpen(); } - return true; + return editor.parent; } // 没有初始化的页签无法检测到 const hasEditor = getUnInitTab(options); @@ -167,7 +166,7 @@ export const openFile = (options: IOpenFileOptions) => { if (options.afterOpen) { options.afterOpen(); } - return; + return hasEditor; } } @@ -208,6 +207,7 @@ export const openFile = (options: IOpenFileOptions) => { wnd = getWndByLayout(window.siyuan.layout.centerLayout); } if (wnd) { + let createdTab: Tab; if ((options.position === "right" || options.position === "bottom") && wnd.children[0].headElement) { const direction = options.position === "right" ? "lr" : "tb"; let targetWnd: Wnd; @@ -239,18 +239,21 @@ export const openFile = (options: IOpenFileOptions) => { }); if (!hasEditor) { hasEditor = getUnInitTab(options); + createdTab = hasEditor; } if (!hasEditor) { - targetWnd.addTab(newTab(options)); + createdTab = newTab(options); + targetWnd.addTab(createdTab); } } else { - wnd.split(direction).addTab(newTab(options)); + createdTab = newTab(options); + wnd.split(direction).addTab(createdTab); } wnd.showHeading(); if (options.afterOpen) { options.afterOpen(); } - return; + return createdTab; } if (pdfIsLoading(wnd.element)) { if (options.afterOpen) { @@ -259,9 +262,9 @@ export const openFile = (options: IOpenFileOptions) => { return; } 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) { let unUpdateTab: Tab; // 不能 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) { wnd.removeTab(unUpdateTab.id, false, true, false); } } else { - wnd.addTab(newTab(options)); + createdTab = newTab(options) + wnd.addTab(createdTab); } wnd.showHeading(); if (options.afterOpen) { options.afterOpen(); } + return createdTab; } }; diff --git a/app/src/layout/Tab.ts b/app/src/layout/Tab.ts index 247653d1d..7b4a99201 100644 --- a/app/src/layout/Tab.ts +++ b/app/src/layout/Tab.ts @@ -210,4 +210,8 @@ export class Tab { this.headElement.querySelector(".item__text").classList.remove("fn__none"); } } + + public close() { + this.parent.removeTab(this.id); + } } diff --git a/app/src/plugin/API.ts b/app/src/plugin/API.ts index 4e25f6122..7bbeb1146 100644 --- a/app/src/plugin/API.ts +++ b/app/src/plugin/API.ts @@ -12,6 +12,7 @@ import {updateHotkeyTip} from "../protyle/util/compatibility"; import {newCardModel} from "../card/newCardTab"; import {App} from "../index"; import {Constants} from "../constants"; +import {Model} from "../layout/Model"; export class Menu { private menu: SiyuanMenu; @@ -102,7 +103,7 @@ openTab = (options: { title: string, icon: string, data?: any - fn?: () => any, + fn?: () => Model, } position?: "right" | "bottom", keepCursor?: boolean // 是否跳转到新 tab 上 @@ -117,7 +118,10 @@ openTab = (options: { options.doc.action = [Constants.CB_GET_ALL]; } } - openFileById({ + if (!options.doc.action) { + options.doc.action = [] + } + return openFileById({ app: options.app, keepCursor: options.keepCursor, removeCurrentTab: options.removeCurrentTab, @@ -127,10 +131,9 @@ openTab = (options: { action: options.doc.action, zoomIn: options.doc.zoomIn }); - return; } if (options.asset) { - openFile({ + return openFile({ app: options.app, keepCursor: options.keepCursor, removeCurrentTab: options.removeCurrentTab, @@ -138,10 +141,9 @@ openTab = (options: { afterOpen: options.afterOpen, assetPath: options.asset.path, }); - return; } if (options.pdf) { - openFile({ + return openFile({ app: options.app, keepCursor: options.keepCursor, removeCurrentTab: options.removeCurrentTab, @@ -150,7 +152,6 @@ openTab = (options: { assetPath: options.pdf.path, page: options.pdf.id || options.pdf.page, }); - return; } if (options.search) { if (!options.search.idPath) { @@ -159,7 +160,7 @@ openTab = (options: { if (!options.search.hPath) { options.search.hPath = ""; } - openFile({ + return openFile({ app: options.app, keepCursor: options.keepCursor, removeCurrentTab: options.removeCurrentTab, @@ -167,10 +168,9 @@ openTab = (options: { afterOpen: options.afterOpen, searchData: options.search, }); - return; } if (options.card) { - openFile({ + return openFile({ app: options.app, keepCursor: options.keepCursor, removeCurrentTab: options.removeCurrentTab, @@ -187,11 +187,9 @@ openTab = (options: { fn: newCardModel }, }); - return; } if (options.custom) { - openFile(options); - return; + return openFile(options); } };