Vanessa 2023-05-11 12:22:53 +08:00
parent 7a68c8ad1b
commit f06a2e62a5
11 changed files with 175 additions and 71 deletions

View file

@ -172,7 +172,11 @@ export const initWindow = () => {
focusByRange(getSelection().getRangeAt(0)); focusByRange(getSelection().getRangeAt(0));
} }
} }
exportLayout(false); exportLayout({
reload: false,
onlyData: false,
errorExit: false
});
window.siyuan.altIsPressed = false; window.siyuan.altIsPressed = false;
window.siyuan.ctrlIsPressed = false; window.siyuan.ctrlIsPressed = false;
window.siyuan.shiftIsPressed = false; window.siyuan.shiftIsPressed = false;
@ -184,26 +188,31 @@ export const initWindow = () => {
}; };
const winOnClose = (currentWindow: Electron.BrowserWindow, close = false) => { const winOnClose = (currentWindow: Electron.BrowserWindow, close = false) => {
exportLayout(false, () => { exportLayout({
if (window.siyuan.config.appearance.closeButtonBehavior === 1 && !close) { reload: false,
// 最小化 cb() {
if ("windows" === window.siyuan.config.system.os) { if (window.siyuan.config.appearance.closeButtonBehavior === 1 && !close) {
ipcRenderer.send(Constants.SIYUAN_CONFIG_TRAY, { // 最小化
id: getCurrentWindow().id, if ("windows" === window.siyuan.config.system.os) {
languages: window.siyuan.languages["_trayMenu"], ipcRenderer.send(Constants.SIYUAN_CONFIG_TRAY, {
}); id: getCurrentWindow().id,
} else { languages: window.siyuan.languages["_trayMenu"],
if (currentWindow.isFullScreen()) { });
currentWindow.once("leave-full-screen", () => currentWindow.hide());
currentWindow.setFullScreen(false);
} else { } else {
currentWindow.hide(); if (currentWindow.isFullScreen()) {
currentWindow.once("leave-full-screen", () => currentWindow.hide());
currentWindow.setFullScreen(false);
} else {
currentWindow.hide();
}
} }
} else {
exitSiYuan();
} }
} else { },
exitSiYuan(); onlyData: false,
} errorExit: true
}, false, true); });
}; };
const winOnMaxRestore = () => { const winOnMaxRestore = () => {
@ -469,10 +478,18 @@ ${response.data.replace("%pages", "<span class=totalPages></span>").replace("%pa
document.querySelector(".toolbar").classList.add("toolbar--browser"); document.querySelector(".toolbar").classList.add("toolbar--browser");
} }
window.addEventListener("beforeunload", () => { window.addEventListener("beforeunload", () => {
exportLayout(false); exportLayout({
reload: false,
onlyData: false,
errorExit: false
});
}, false); }, false);
window.addEventListener("pagehide", () => { window.addEventListener("pagehide", () => {
exportLayout(false); exportLayout({
reload: false,
onlyData: false,
errorExit: false
});
}, false); }, false);
/// #endif /// #endif
}; };

View file

@ -323,23 +323,33 @@ export const about = {
const networkServeElement = about.element.querySelector("#networkServe") as HTMLInputElement; const networkServeElement = about.element.querySelector("#networkServe") as HTMLInputElement;
networkServeElement.addEventListener("change", () => { networkServeElement.addEventListener("change", () => {
fetchPost("/api/system/setNetworkServe", {networkServe: networkServeElement.checked}, () => { fetchPost("/api/system/setNetworkServe", {networkServe: networkServeElement.checked}, () => {
exportLayout(false, () => { exportLayout({
exitSiYuan(); reload: false,
}, false, true); onlyData: false,
errorExit: true,
cb: exitSiYuan
});
}); });
}); });
const googleAnalyticsElement = about.element.querySelector("#googleAnalytics") as HTMLInputElement; const googleAnalyticsElement = about.element.querySelector("#googleAnalytics") as HTMLInputElement;
googleAnalyticsElement.addEventListener("change", () => { googleAnalyticsElement.addEventListener("change", () => {
fetchPost("/api/system/setGoogleAnalytics", {googleAnalytics: googleAnalyticsElement.checked}, () => { fetchPost("/api/system/setGoogleAnalytics", {googleAnalytics: googleAnalyticsElement.checked}, () => {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
}); });
}); });
const uploadErrLogElement = about.element.querySelector("#uploadErrLog") as HTMLInputElement; const uploadErrLogElement = about.element.querySelector("#uploadErrLog") as HTMLInputElement;
uploadErrLogElement.addEventListener("change", () => { uploadErrLogElement.addEventListener("change", () => {
fetchPost("/api/system/setUploadErrLog", {uploadErrLog: uploadErrLogElement.checked}, () => { fetchPost("/api/system/setUploadErrLog", {uploadErrLog: uploadErrLogElement.checked}, () => {
exportLayout(false, () => { exportLayout({
exitSiYuan(); reload: false,
}, false, true); onlyData: false,
errorExit: true,
cb: exitSiYuan
});
}); });
}); });
const downloadInstallPkgElement = about.element.querySelector("#downloadInstallPkg") as HTMLInputElement; const downloadInstallPkgElement = about.element.querySelector("#downloadInstallPkg") as HTMLInputElement;

View file

@ -177,14 +177,22 @@ export const appearance = {
window.siyuan.config.appearance.themeLight !== response.data.themeLight || window.siyuan.config.appearance.themeLight !== response.data.themeLight ||
window.siyuan.config.appearance.themeDark !== response.data.themeDark window.siyuan.config.appearance.themeDark !== response.data.themeDark
)) { )) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
const OSTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; const OSTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
if (response.data.modeOS && ( if (response.data.modeOS && (
(response.data.mode === 1 && OSTheme === "light") || (response.data.mode === 0 && OSTheme === "dark") (response.data.mode === 1 && OSTheme === "light") || (response.data.mode === 0 && OSTheme === "dark")
)) { )) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
} }
@ -215,7 +223,11 @@ export const appearance = {
shell.openPath(path.join(window.siyuan.config.system.dataDir, "emojis")); shell.openPath(path.join(window.siyuan.config.system.dataDir, "emojis"));
}); });
appearance.element.querySelector("#appearanceRefresh").addEventListener("click", () => { appearance.element.querySelector("#appearanceRefresh").addEventListener("click", () => {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
}); });
/// #endif /// #endif
appearance.element.querySelectorAll("select").forEach(item => { appearance.element.querySelectorAll("select").forEach(item => {
@ -231,7 +243,11 @@ export const appearance = {
}, },
onSetappearance(data: IAppearance) { onSetappearance(data: IAppearance) {
if (data.lang !== window.siyuan.config.appearance.lang || data.nativeEmoji !== window.siyuan.config.appearance.nativeEmoji) { if (data.lang !== window.siyuan.config.appearance.lang || data.nativeEmoji !== window.siyuan.config.appearance.nativeEmoji) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
window.siyuan.config.appearance = data; window.siyuan.config.appearance = data;

View file

@ -482,7 +482,11 @@ export const bazaar = {
mode: dataObj.themeMode === "dark" ? 1 : 0, mode: dataObj.themeMode === "dark" ? 1 : 0,
}, response => { }, response => {
if (window.siyuan.config.appearance.themeJS && bazaarType === "themes") { if (window.siyuan.config.appearance.themeJS && bazaarType === "themes") {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
bazaar._genMyHTML(bazaarType, app); bazaar._genMyHTML(bazaarType, app);
@ -525,7 +529,11 @@ export const bazaar = {
(window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === dataObj.name) (window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === dataObj.name)
)) { )) {
if (window.siyuan.config.appearance.themeJS) { if (window.siyuan.config.appearance.themeJS) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
} else { } else {
const linkElement = (document.getElementById("themeDefaultStyle") as HTMLLinkElement); const linkElement = (document.getElementById("themeDefaultStyle") as HTMLLinkElement);
linkElement.href = linkElement.href + "1"; linkElement.href = linkElement.href + "1";
@ -563,7 +571,11 @@ export const bazaar = {
bazaar._onBazaar(response, bazaarType, ["themes", "icons"].includes(bazaarType)); bazaar._onBazaar(response, bazaarType, ["themes", "icons"].includes(bazaarType));
// TODO destroy plugin // TODO destroy plugin
if (bazaarType === "plugins") { if (bazaarType === "plugins") {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
} }
}); });
} }
@ -596,7 +608,11 @@ export const bazaar = {
(mode === 1 && window.siyuan.config.appearance.themeDark !== packageName) || (mode === 1 && window.siyuan.config.appearance.themeDark !== packageName) ||
(mode === 0 && window.siyuan.config.appearance.themeLight !== packageName)) && (mode === 0 && window.siyuan.config.appearance.themeLight !== packageName)) &&
window.siyuan.config.appearance.themeJS) { window.siyuan.config.appearance.themeJS) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
} else { } else {
this._genMyHTML("themes", app); this._genMyHTML("themes", app);
fetchPost("/api/bazaar/getBazaarTheme", {}, response => { fetchPost("/api/bazaar/getBazaarTheme", {}, response => {
@ -628,7 +644,11 @@ export const bazaar = {
enabled: (target as HTMLInputElement).checked enabled: (target as HTMLInputElement).checked
}, () => { }, () => {
// TODO destroy plugin // TODO destroy plugin
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
}); });
} }
event.stopPropagation(); event.stopPropagation();

View file

@ -196,7 +196,11 @@ export const keymap = {
}, },
bindEvent() { bindEvent() {
keymap.element.querySelector("#keymapRefreshBtn").addEventListener("click", () => { keymap.element.querySelector("#keymapRefreshBtn").addEventListener("click", () => {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
}); });
const searchElement = keymap.element.querySelector("#keymapInput") as HTMLInputElement; const searchElement = keymap.element.querySelector("#keymapInput") as HTMLInputElement;
const searchKeymapElement = keymap.element.querySelector("#searchByKey") as HTMLInputElement; const searchKeymapElement = keymap.element.querySelector("#searchByKey") as HTMLInputElement;

View file

@ -245,9 +245,12 @@ export const transactionError = () => {
/// #if MOBILE /// #if MOBILE
exitSiYuan(); exitSiYuan();
/// #else /// #else
exportLayout(false, () => { exportLayout({
exitSiYuan(); reload: false,
}, false, true); onlyData: false,
errorExit: true,
cb: exitSiYuan
});
/// #endif /// #endif
}); });
btnsElement[1].addEventListener("click", () => { btnsElement[1].addEventListener("click", () => {

View file

@ -187,20 +187,26 @@ export const resetLayout = () => {
}); });
}; };
export const exportLayout = (reload: boolean, cb?: () => void, onlyData = false, errorExit = false) => { export const exportLayout = (options: {
reload: boolean,
cb?: () => void,
onlyData: boolean,
errorExit: boolean,
dropEditScroll?: boolean
}) => {
if (isWindow()) { if (isWindow()) {
const layoutJSON: any = { const layoutJSON: any = {
layout: {}, layout: {},
}; };
layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout); layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout, !!options.dropEditScroll);
if (onlyData) { if (options.onlyData) {
return layoutJSON; return layoutJSON;
} }
sessionStorage.setItem("layout", JSON.stringify(layoutJSON)); sessionStorage.setItem("layout", JSON.stringify(layoutJSON));
if (reload) { if (options.reload) {
window.location.reload(); window.location.reload();
} else if (cb) { } else if (options.cb) {
cb(); options.cb();
} }
return; return;
} }
@ -215,18 +221,18 @@ export const exportLayout = (reload: boolean, cb?: () => void, onlyData = false,
left: dockToJSON(window.siyuan.layout.leftDock), left: dockToJSON(window.siyuan.layout.leftDock),
right: dockToJSON(window.siyuan.layout.rightDock), right: dockToJSON(window.siyuan.layout.rightDock),
}; };
layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout); layoutToJSON(window.siyuan.layout.layout, layoutJSON.layout, !!options.dropEditScroll);
if (onlyData) { if (options.onlyData) {
return layoutJSON; return layoutJSON;
} }
fetchPost("/api/system/setUILayout", { fetchPost("/api/system/setUILayout", {
layout: layoutJSON, layout: layoutJSON,
errorExit // 后台不接受该参数,用于请求发生错误时退出程序 errorExit: options.errorExit // 后台不接受该参数,用于请求发生错误时退出程序
}, () => { }, () => {
if (reload) { if (options.reload) {
window.location.reload(); window.location.reload();
} else if (cb) { } else if (options.cb) {
cb(); options.cb();
} }
}); });
}; };
@ -404,7 +410,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
} }
}; };
export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => { export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any, dropEditScroll = false) => {
if (layout instanceof Layout) { if (layout instanceof Layout) {
json.direction = layout.direction; json.direction = layout.direction;
if (layout.parent) { if (layout.parent) {
@ -453,7 +459,9 @@ export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => {
json.mode = layout.editor.protyle.preview.element.classList.contains("fn__none") ? "wysiwyg" : "preview"; json.mode = layout.editor.protyle.preview.element.classList.contains("fn__none") ? "wysiwyg" : "preview";
json.action = layout.editor.protyle.block.showAll ? Constants.CB_GET_ALL : ""; json.action = layout.editor.protyle.block.showAll ? Constants.CB_GET_ALL : "";
json.instance = "Editor"; json.instance = "Editor";
json.scrollAttr = saveScroll(layout.editor.protyle, true); if (!dropEditScroll) {
json.scrollAttr = saveScroll(layout.editor.protyle, true);
}
} else if (layout instanceof Asset) { } else if (layout instanceof Asset) {
json.path = layout.path; json.path = layout.path;
if (layout.pdfObject) { if (layout.pdfObject) {
@ -517,13 +525,13 @@ export const layoutToJSON = (layout: Layout | Wnd | Tab | Model, json: any) => {
layout.children.forEach((item: Layout | Wnd | Tab) => { layout.children.forEach((item: Layout | Wnd | Tab) => {
const itemJSON = {}; const itemJSON = {};
json.children.push(itemJSON); json.children.push(itemJSON);
layoutToJSON(item, itemJSON); layoutToJSON(item, itemJSON, dropEditScroll);
}); });
} }
} else if (layout instanceof Tab) { } else if (layout instanceof Tab) {
if (layout.model) { if (layout.model) {
json.children = {}; json.children = {};
layoutToJSON(layout.model, json.children); layoutToJSON(layout.model, json.children, dropEditScroll);
} else if (layout.headElement) { } else if (layout.headElement) {
// 当前页签没有激活时编辑器没有初始化 // 当前页签没有激活时编辑器没有初始化
json.children = JSON.parse(layout.headElement.getAttribute("data-initdata") || "{}"); json.children = JSON.parse(layout.headElement.getAttribute("data-initdata") || "{}");

View file

@ -137,7 +137,11 @@ export const workspaceMenu = (app:App, rect: DOMRect) => {
if (item.name === value) { if (item.name === value) {
saveDialog.destroy(); saveDialog.destroy();
confirmDialog(window.siyuan.languages.save, window.siyuan.languages.exportTplTip, () => { confirmDialog(window.siyuan.languages.save, window.siyuan.languages.exportTplTip, () => {
item.layout = exportLayout(false, undefined, true); item.layout = exportLayout({
reload: false,
onlyData: true,
errorExit: false,
});
setStorageVal(Constants.LOCAL_LAYOUTS, window.siyuan.storage[Constants.LOCAL_LAYOUTS]); setStorageVal(Constants.LOCAL_LAYOUTS, window.siyuan.storage[Constants.LOCAL_LAYOUTS]);
}); });
return true; return true;
@ -148,7 +152,11 @@ export const workspaceMenu = (app:App, rect: DOMRect) => {
} }
window.siyuan.storage[Constants.LOCAL_LAYOUTS].push({ window.siyuan.storage[Constants.LOCAL_LAYOUTS].push({
name: value, name: value,
layout: exportLayout(false, undefined, true) layout: exportLayout({
reload: false,
onlyData: true,
errorExit: false,
})
}); });
setStorageVal(Constants.LOCAL_LAYOUTS, window.siyuan.storage[Constants.LOCAL_LAYOUTS]); setStorageVal(Constants.LOCAL_LAYOUTS, window.siyuan.storage[Constants.LOCAL_LAYOUTS]);
saveDialog.destroy(); saveDialog.destroy();

View file

@ -145,7 +145,11 @@ export const initAssets = () => {
}, response => { }, response => {
if (window.siyuan.config.appearance.themeJS) { if (window.siyuan.config.appearance.themeJS) {
/// #if !MOBILE /// #if !MOBILE
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
/// #else /// #else
window.location.reload(); window.location.reload();
/// #endif /// #endif
@ -261,14 +265,22 @@ export const setMode = (modeElementValue: number) => {
window.siyuan.config.appearance.themeLight !== response.data.themeLight || window.siyuan.config.appearance.themeLight !== response.data.themeLight ||
window.siyuan.config.appearance.themeDark !== response.data.themeDark window.siyuan.config.appearance.themeDark !== response.data.themeDark
)) { )) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
const OSTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; const OSTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
if (response.data.modeOS && ( if (response.data.modeOS && (
(response.data.mode === 1 && OSTheme === "light") || (response.data.mode === 0 && OSTheme === "dark") (response.data.mode === 1 && OSTheme === "light") || (response.data.mode === 0 && OSTheme === "dark")
)) { )) {
exportLayout(true); exportLayout({
reload: true,
onlyData: false,
errorExit: false,
});
return; return;
} }
} }

View file

@ -23,11 +23,12 @@ export const processMessage = (response: IWebSocketData) => {
/// #if MOBILE /// #if MOBILE
window.location.reload(); window.location.reload();
/// #else /// #else
if (response.data?.dropCurrentLayout) { exportLayout({
window.location.reload(); reload: true,
} else { onlyData: false,
exportLayout(true); errorExit: false,
} dropEditScroll: response.data?.resetScroll,
});
/// #endif /// #endif
return false; return false;
} }

View file

@ -15,11 +15,16 @@ export const onWindowsMsg = (ipcData: IWebSocketData) => {
closeTab(ipcData); closeTab(ipcData);
break; break;
case "lockscreen": case "lockscreen":
exportLayout(false, () => { exportLayout({
fetchPost("/api/system/logoutAuth", {}, () => { reload: false,
redirectToCheckAuth(); onlyData: false,
}); errorExit: false,
}, false, false); cb() {
fetchPost("/api/system/logoutAuth", {}, () => {
redirectToCheckAuth();
});
}
});
break; break;
} }
}; };