diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index e15d73acd10..38f10a788cf 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -1,4 +1,4 @@ -import {exportLayout, JSONToLayout, resetLayout, resizeTopBar} from "../layout/util"; +import {exportLayout, JSONToLayout, resetLayout, resizeTopBar, saveLayout} from "../layout/util"; import {resizeTabs} from "../layout/tabUtil"; import {setStorageVal} from "../protyle/util/compatibility"; /// #if !BROWSER @@ -172,19 +172,6 @@ const winOnMaxRestore = async () => { /// #endif }; -const saveUI = () => { - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); -}; - -export const unbindSaveUI = () => { - window.removeEventListener("beforeunload", saveUI); - window.removeEventListener("pagehide", saveUI); -}; - export const initWindow = async (app: App) => { /// #if !BROWSER const winOnClose = (close = false) => { @@ -514,7 +501,5 @@ ${response.data.replace("%pages", "").replace("%pa if (!isWindow()) { document.querySelector(".toolbar").classList.add("toolbar--browser"); } - window.addEventListener("beforeunload", saveUI, false); - window.addEventListener("pagehide", saveUI, false); /// #endif }; diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts index d9e0340ffc4..99c3a29477b 100644 --- a/app/src/layout/Wnd.ts +++ b/app/src/layout/Wnd.ts @@ -1,10 +1,9 @@ import {Layout} from "./index"; import {genUUID} from "../util/genID"; import { - exportLayout, getInstanceById, getWndByLayout, JSONToCenter, - newModelByInitData, pdfIsLoading, + newModelByInitData, pdfIsLoading, saveLayout, setPanelFocus, switchWnd } from "./util"; @@ -280,11 +279,7 @@ export class Wnd { } else { oldTab.parent.children.push(tempTab); } - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); + saveLayout() }); this.element.addEventListener("dragenter", (event: DragEvent & { target: HTMLElement }) => { @@ -498,7 +493,7 @@ export class Wnd { } } - public addTab(tab: Tab, keepCursor = false, saveLayout = true) { + public addTab(tab: Tab, keepCursor = false, isSaveLayout = true) { if (keepCursor) { tab.headElement?.classList.remove("item--focus"); tab.panelElement.classList.add("fn__none"); @@ -566,12 +561,8 @@ export class Wnd { setTabPosition(); setModelsHash(); /// #endif - if (saveLayout) { - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); + if (isSaveLayout) { + saveLayout(); } } @@ -773,7 +764,7 @@ export class Wnd { item.panelElement.remove(); this.destroyModel(item.model); this.children.splice(index, 1); - resizeTabs(); + resizeTabs(item.headElement ? true : false); return true; } }); @@ -789,7 +780,7 @@ export class Wnd { /// #endif const wnd = new Wnd(this.app); window.siyuan.layout.centerLayout.addWnd(wnd); - wnd.addTab(newCenterEmptyTab(this.app)); + wnd.addTab(newCenterEmptyTab(this.app), false, false); setTitle(window.siyuan.languages.siyuanNote); } } diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts index 76a580443d2..04012af4527 100644 --- a/app/src/layout/dock/index.ts +++ b/app/src/layout/dock/index.ts @@ -720,7 +720,7 @@ export class Dock { this.element.classList.remove("fn__none"); } if (data[0].show) { - this.toggleModel(data[0].type, true); + this.toggleModel(data[0].type, true, false, false, false); } } } diff --git a/app/src/layout/tabUtil.ts b/app/src/layout/tabUtil.ts index e69a3fb64e5..fac41732ec3 100644 --- a/app/src/layout/tabUtil.ts +++ b/app/src/layout/tabUtil.ts @@ -1,5 +1,5 @@ import {Tab} from "./Tab"; -import {exportLayout, getInstanceById, newModelByInitData} from "./util"; +import {exportLayout, getInstanceById, newModelByInitData, saveLayout} from "./util"; import {getAllModels, getAllTabs} from "./getAll"; import {hideAllElements, hideElements} from "../protyle/ui/hideElements"; import {pdfResize} from "../asset/renderAssets"; @@ -84,7 +84,7 @@ export const switchTabByIndex = (index: number) => { }; let resizeTimeout: number; -export const resizeTabs = (setLayout = true) => { +export const resizeTabs = (isSaveLayout = true) => { clearTimeout(resizeTimeout); // .layout .fn__flex-shrink {width .15s cubic-bezier(0, 0, .2, 1) 0ms} 时需要再次计算 padding // PDF 避免分屏多次调用后,页码跳转到1 https://github.com/siyuan-note/siyuan/issues/5646 @@ -117,12 +117,8 @@ export const resizeTabs = (setLayout = true) => { }); pdfResize(); hideAllElements(["gutter"]); - if (setLayout) { - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); + if (isSaveLayout) { + saveLayout(); } }, 200); }; diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index c97fae8b2c7..ee96ae633cd 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -173,6 +173,47 @@ export const resetLayout = () => { }); }; +let saveCount = 0; +export const saveLayout = () => { + const breakObj = {} + let layoutJSON: any = {}; + if (isWindow()) { + layoutJSON = { + layout: {}, + }; + layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout, breakObj); + } else { + const useElement = document.querySelector("#barDock use"); + if (useElement) { + layoutJSON = { + hideDock: useElement.getAttribute("xlink:href") === "#iconDock", + layout: {}, + bottom: dockToJSON(window.siyuan.layout.bottomDock), + left: dockToJSON(window.siyuan.layout.leftDock), + right: dockToJSON(window.siyuan.layout.rightDock), + }; + layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout, breakObj); + } + } + + if (Object.keys(breakObj).length > 0 && saveCount < 10) { + saveCount++; + setTimeout(() => { + saveLayout(); + }, Constants.TIMEOUT_LOAD); + } else { + saveCount = 0; + if (isWindow()) { + sessionStorage.setItem("layout", JSON.stringify(layoutJSON)); + } else { + fetchPost("/api/system/setUILayout", { + layout: layoutJSON, + errorExit: false // 后台不接受该参数,用于请求发生错误时退出程序 + }); + } + } +}; + export const exportLayout = (options: { reload: boolean, cb?: () => void, @@ -439,7 +480,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => { resizeTopBar(); }; -export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => { +export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any, breakObj?: IObject) => { if (layout instanceof Layout) { json.direction = layout.direction; if (layout.parent) { @@ -482,6 +523,9 @@ export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => { } json.instance = "Tab"; } else if (layout instanceof Editor) { + if (!layout.editor.protyle.notebookId && breakObj) { + breakObj.editor = "true"; + } json.notebookId = layout.editor.protyle.notebookId; json.blockId = layout.editor.protyle.block.id; json.rootId = layout.editor.protyle.block.rootID; @@ -555,13 +599,13 @@ export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => { layout.children.forEach((item: Layout | Wnd | Tab) => { const itemJSON = {}; json.children.push(itemJSON); - layoutToJSON(item, itemJSON); + layoutToJSON(item, itemJSON, breakObj); }); } } else if (layout instanceof Tab) { if (layout.model) { json.children = {}; - layoutToJSON(layout.model, json.children); + layoutToJSON(layout.model, json.children, breakObj); } else if (layout.headElement) { // 当前页签没有激活时编辑器没有初始化 json.children = JSON.parse(layout.headElement.getAttribute("data-initdata") || "{}"); diff --git a/app/src/menus/workspace.ts b/app/src/menus/workspace.ts index aed4951c952..50ffeb9ef69 100644 --- a/app/src/menus/workspace.ts +++ b/app/src/menus/workspace.ts @@ -303,7 +303,6 @@ export const workspaceMenu = (app: App, rect: DOMRect) => { return; } fetchPost("/api/system/setUILayout", {layout: item.layout}, () => { - unbindSaveUI(); window.location.reload(); }); }); diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts index 0afef2d8b7b..1ae81ff491a 100644 --- a/app/src/plugin/loader.ts +++ b/app/src/plugin/loader.ts @@ -2,7 +2,7 @@ import {fetchSyncPost} from "../util/fetch"; import {App} from "../index"; import {Plugin} from "./index"; /// #if !MOBILE -import {exportLayout, resizeTopBar} from "../layout/util"; +import {exportLayout, resizeTopBar, saveLayout} from "../layout/util"; /// #endif import {API} from "./API"; import {getFrontend, isMobile, isWindow} from "../util/functions"; @@ -80,13 +80,6 @@ export const loadPlugin = async (app: App, item: IPluginData) => { styleElement.textContent = item.css; document.head.append(styleElement); afterLoadPlugin(plugin); - /// #if !MOBILE - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); - /// #endif return plugin; }; diff --git a/app/src/plugin/uninstall.ts b/app/src/plugin/uninstall.ts index e52a9fbd9b9..3822142661a 100644 --- a/app/src/plugin/uninstall.ts +++ b/app/src/plugin/uninstall.ts @@ -1,7 +1,7 @@ import {App} from "../index"; import {Plugin} from "../plugin"; import {getAllModels} from "../layout/getAll"; -import {exportLayout, resizeTopBar} from "../layout/util"; +import {exportLayout, resizeTopBar, saveLayout} from "../layout/util"; import {Constants} from "../constants"; export const uninstall = (app: App, name: string) => { @@ -51,14 +51,6 @@ export const uninstall = (app: App, name: string) => { }); // rm plugin app.plugins.splice(index, 1); - - setTimeout(() => { - exportLayout({ - reload: false, - onlyData: false, - errorExit: false - }); - }, Constants.TIMEOUT_LOAD); // 移除页签时切换到新的文档页签,需等待新页签初始化完成,才有 editor.protyle.block 等数据 return true; } });