Skip to content

Commit

Permalink
🎨 #9866
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Dec 17, 2023
1 parent 40b9c62 commit e24522b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 62 deletions.
17 changes: 1 addition & 16 deletions 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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -514,7 +501,5 @@ ${response.data.replace("%pages", "<span class=totalPages></span>").replace("%pa
if (!isWindow()) {
document.querySelector(".toolbar").classList.add("toolbar--browser");
}
window.addEventListener("beforeunload", saveUI, false);
window.addEventListener("pagehide", saveUI, false);
/// #endif
};
23 changes: 7 additions & 16 deletions 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";
Expand Down Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -566,12 +561,8 @@ export class Wnd {
setTabPosition();
setModelsHash();
/// #endif
if (saveLayout) {
exportLayout({
reload: false,
onlyData: false,
errorExit: false
});
if (isSaveLayout) {
saveLayout();
}
}

Expand Down Expand Up @@ -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;
}
});
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/layout/dock/index.ts
Expand Up @@ -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);
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions 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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
Expand Down
50 changes: 47 additions & 3 deletions app/src/layout/util.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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") || "{}");
Expand Down
1 change: 0 additions & 1 deletion app/src/menus/workspace.ts
Expand Up @@ -303,7 +303,6 @@ export const workspaceMenu = (app: App, rect: DOMRect) => {
return;
}
fetchPost("/api/system/setUILayout", {layout: item.layout}, () => {
unbindSaveUI();
window.location.reload();
});
});
Expand Down
9 changes: 1 addition & 8 deletions app/src/plugin/loader.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
};

Expand Down
10 changes: 1 addition & 9 deletions 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) => {
Expand Down Expand Up @@ -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;
}
});
Expand Down

0 comments on commit e24522b

Please sign in to comment.