2023-05-27 18:34:39 +08:00
|
|
|
import {exportLayout, JSONToLayout, resetLayout, resizeTopbar, resizeTabs} from "../layout/util";
|
2023-03-03 10:39:05 +08:00
|
|
|
import {hotKey2Electron, setStorageVal} from "../protyle/util/compatibility";
|
2022-05-26 15:18:53 +08:00
|
|
|
/// #if !BROWSER
|
2022-10-12 09:40:12 +08:00
|
|
|
import {dialog, getCurrentWindow} from "@electron/remote";
|
2023-02-23 10:50:08 +08:00
|
|
|
import {ipcRenderer, OpenDialogReturnValue, webFrame} from "electron";
|
2022-09-20 01:12:28 +08:00
|
|
|
import * as fs from "fs";
|
|
|
|
|
import * as path from "path";
|
|
|
|
|
import {afterExport} from "../protyle/export/util";
|
2023-02-27 10:58:07 +08:00
|
|
|
import {onWindowsMsg} from "../window/onWindowsMsg";
|
2022-05-26 15:18:53 +08:00
|
|
|
/// #endif
|
|
|
|
|
import {Constants} from "../constants";
|
|
|
|
|
import {appearance} from "../config/appearance";
|
2023-04-06 22:25:50 +08:00
|
|
|
import {globalShortcut} from "../boot/globalShortcut";
|
|
|
|
|
import {fetchPost, fetchSyncPost} from "../util/fetch";
|
|
|
|
|
import {addGA, initAssets, setInlineStyle} from "../util/assets";
|
2022-10-27 19:34:26 +08:00
|
|
|
import {renderSnippet} from "../config/util/snippets";
|
2023-06-15 23:36:06 +08:00
|
|
|
import {openFile, openFileById} from "../editor/util";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {focusByRange} from "../protyle/util/selection";
|
2023-03-03 10:39:05 +08:00
|
|
|
import {exitSiYuan} from "../dialog/processSystem";
|
2023-04-19 11:27:32 +08:00
|
|
|
import {getSearch, isWindow} from "../util/functions";
|
2022-06-27 23:48:24 +08:00
|
|
|
import {initStatus} from "../layout/status";
|
2022-09-20 01:12:28 +08:00
|
|
|
import {showMessage} from "../dialog/message";
|
2022-12-10 16:26:12 +08:00
|
|
|
import {replaceLocalPath} from "../editor/rename";
|
2023-01-26 20:15:54 +08:00
|
|
|
import {setTabPosition} from "../window/setHeader";
|
2023-03-03 10:39:05 +08:00
|
|
|
import {initBar} from "../layout/topBar";
|
2023-06-21 11:47:26 +08:00
|
|
|
import {setProxy} from "../config/util/about";
|
2023-04-06 22:25:50 +08:00
|
|
|
import {openChangelog} from "./openChangelog";
|
2023-04-19 11:27:32 +08:00
|
|
|
import {getIdFromSYProtocol, isSYProtocol} from "../util/pathName";
|
2023-05-08 21:00:41 +08:00
|
|
|
import {App} from "../index";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
const matchKeymap = (keymap: Record<string, IKeymapItem>, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => {
|
|
|
|
|
if (key1 === "general") {
|
|
|
|
|
if (!window.siyuan.config.keymap[key1]) {
|
|
|
|
|
window.siyuan.config.keymap[key1] = keymap;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!window.siyuan.config.keymap[key1]) {
|
2022-11-05 17:03:12 +08:00
|
|
|
window.siyuan.config.keymap[key1] = JSON.parse(JSON.stringify(Constants.SIYUAN_KEYMAP.editor));
|
2022-05-26 15:18:53 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!window.siyuan.config.keymap[key1][key2]) {
|
|
|
|
|
window.siyuan.config.keymap[key1][key2] = keymap;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let match = true;
|
|
|
|
|
Object.keys(keymap).forEach(key => {
|
|
|
|
|
if (key1 === "general") {
|
|
|
|
|
if (!window.siyuan.config.keymap[key1][key] || window.siyuan.config.keymap[key1][key].default !== keymap[key].default) {
|
|
|
|
|
match = false;
|
|
|
|
|
window.siyuan.config.keymap[key1][key] = keymap[key];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!window.siyuan.config.keymap[key1][key2][key] || window.siyuan.config.keymap[key1][key2][key].default !== keymap[key].default) {
|
|
|
|
|
match = false;
|
|
|
|
|
window.siyuan.config.keymap[key1][key2][key] = keymap[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return match;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasKeymap = (keymap: Record<string, IKeymapItem>, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => {
|
|
|
|
|
let match = true;
|
|
|
|
|
if (key1 === "editor") {
|
|
|
|
|
if (Object.keys(window.siyuan.config.keymap[key1][key2]).length !== Object.keys(Constants.SIYUAN_KEYMAP[key1][key2]).length) {
|
|
|
|
|
Object.keys(window.siyuan.config.keymap[key1][key2]).forEach(item => {
|
|
|
|
|
if (!Constants.SIYUAN_KEYMAP[key1][key2][item]) {
|
|
|
|
|
match = false;
|
|
|
|
|
delete window.siyuan.config.keymap[key1][key2][item];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (Object.keys(window.siyuan.config.keymap[key1]).length !== Object.keys(Constants.SIYUAN_KEYMAP[key1]).length) {
|
|
|
|
|
Object.keys(window.siyuan.config.keymap[key1]).forEach(item => {
|
|
|
|
|
if (!Constants.SIYUAN_KEYMAP[key1][item]) {
|
|
|
|
|
match = false;
|
|
|
|
|
delete window.siyuan.config.keymap[key1][item];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return match;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-10 00:01:47 +08:00
|
|
|
export const onGetConfig = (isStart: boolean, app: App) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
const matchKeymap1 = matchKeymap(Constants.SIYUAN_KEYMAP.general, "general");
|
|
|
|
|
const matchKeymap2 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.general, "editor", "general");
|
|
|
|
|
const matchKeymap3 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.insert, "editor", "insert");
|
|
|
|
|
const matchKeymap4 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.heading, "editor", "heading");
|
|
|
|
|
const matchKeymap5 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.list, "editor", "list");
|
|
|
|
|
const matchKeymap6 = matchKeymap(Constants.SIYUAN_KEYMAP.editor.table, "editor", "table");
|
|
|
|
|
|
|
|
|
|
const hasKeymap1 = hasKeymap(Constants.SIYUAN_KEYMAP.general, "general");
|
|
|
|
|
const hasKeymap2 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.general, "editor", "general");
|
|
|
|
|
const hasKeymap3 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.insert, "editor", "insert");
|
|
|
|
|
const hasKeymap4 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.heading, "editor", "heading");
|
|
|
|
|
const hasKeymap5 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.list, "editor", "list");
|
|
|
|
|
const hasKeymap6 = hasKeymap(Constants.SIYUAN_KEYMAP.editor.table, "editor", "table");
|
2023-01-26 18:53:57 +08:00
|
|
|
if (!window.siyuan.config.readonly &&
|
|
|
|
|
(!matchKeymap1 || !matchKeymap2 || !matchKeymap3 || !matchKeymap4 || !matchKeymap5 || !matchKeymap6 ||
|
2023-01-26 18:55:17 +08:00
|
|
|
!hasKeymap1 || !hasKeymap2 || !hasKeymap3 || !hasKeymap4 || !hasKeymap5 || !hasKeymap6)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
fetchPost("/api/setting/setKeymap", {
|
|
|
|
|
data: window.siyuan.config.keymap
|
|
|
|
|
}, () => {
|
|
|
|
|
/// #if !BROWSER
|
2023-01-04 23:03:08 +08:00
|
|
|
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
|
|
|
|
|
languages: window.siyuan.languages["_trayMenu"],
|
|
|
|
|
id: getCurrentWindow().id,
|
|
|
|
|
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
/// #endif
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/// #if !BROWSER
|
2023-01-04 23:03:08 +08:00
|
|
|
ipcRenderer.send(Constants.SIYUAN_INIT, {
|
|
|
|
|
languages: window.siyuan.languages["_trayMenu"],
|
|
|
|
|
workspaceDir: window.siyuan.config.system.workspaceDir,
|
|
|
|
|
id: getCurrentWindow().id,
|
2023-01-06 16:44:54 +08:00
|
|
|
port: location.port
|
2023-01-04 23:03:08 +08:00
|
|
|
});
|
|
|
|
|
ipcRenderer.send(Constants.SIYUAN_HOTKEY, {
|
|
|
|
|
languages: window.siyuan.languages["_trayMenu"],
|
|
|
|
|
id: getCurrentWindow().id,
|
|
|
|
|
hotkey: hotKey2Electron(window.siyuan.config.keymap.general.toggleWin.custom)
|
|
|
|
|
});
|
2023-01-06 20:26:14 +08:00
|
|
|
webFrame.setZoomFactor(window.siyuan.storage[Constants.LOCAL_ZOOM]);
|
2022-05-26 15:18:53 +08:00
|
|
|
/// #endif
|
|
|
|
|
if (!window.siyuan.config.uiLayout || (window.siyuan.config.uiLayout && !window.siyuan.config.uiLayout.left)) {
|
|
|
|
|
window.siyuan.config.uiLayout = Constants.SIYUAN_EMPTY_LAYOUT;
|
|
|
|
|
}
|
2023-05-08 21:00:41 +08:00
|
|
|
globalShortcut(app);
|
2022-05-26 15:18:53 +08:00
|
|
|
fetchPost("/api/system/getEmojiConf", {}, response => {
|
|
|
|
|
window.siyuan.emojis = response.data as IEmoji[];
|
|
|
|
|
try {
|
2023-05-10 00:01:47 +08:00
|
|
|
JSONToLayout(app, isStart);
|
2023-04-06 22:25:50 +08:00
|
|
|
openChangelog();
|
2022-05-26 15:18:53 +08:00
|
|
|
} catch (e) {
|
2022-10-30 09:37:46 +08:00
|
|
|
resetLayout();
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
});
|
2023-05-08 21:00:41 +08:00
|
|
|
initBar(app);
|
2023-04-06 22:25:50 +08:00
|
|
|
setProxy();
|
2022-06-25 20:40:07 +08:00
|
|
|
initStatus();
|
2023-05-18 19:27:21 +08:00
|
|
|
initWindow(app);
|
2023-01-06 22:50:11 +08:00
|
|
|
appearance.onSetappearance(window.siyuan.config.appearance);
|
2022-05-26 15:18:53 +08:00
|
|
|
initAssets();
|
2022-10-12 09:23:25 +08:00
|
|
|
renderSnippet();
|
2022-05-26 15:18:53 +08:00
|
|
|
setInlineStyle();
|
|
|
|
|
let resizeTimeout = 0;
|
|
|
|
|
window.addEventListener("resize", () => {
|
|
|
|
|
window.clearTimeout(resizeTimeout);
|
|
|
|
|
resizeTimeout = window.setTimeout(() => {
|
|
|
|
|
resizeTabs();
|
2023-05-27 18:34:39 +08:00
|
|
|
resizeTopbar();
|
2022-05-26 15:18:53 +08:00
|
|
|
}, 200);
|
|
|
|
|
});
|
2022-10-30 09:37:46 +08:00
|
|
|
addGA();
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|
|
|
|
|
|
2023-05-18 19:27:21 +08:00
|
|
|
export const initWindow = (app: App) => {
|
2023-04-26 10:25:26 +08:00
|
|
|
/// #if !BROWSER
|
|
|
|
|
const winOnFocus = () => {
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
|
const startNode = range.startContainer.childNodes[range.startOffset] as HTMLElement;
|
|
|
|
|
if (startNode && startNode.nodeType !== 3 && (startNode.tagName === "TEXTAREA" || startNode.tagName === "INPUT")) {
|
|
|
|
|
startNode.focus();
|
|
|
|
|
} else {
|
|
|
|
|
focusByRange(getSelection().getRangeAt(0));
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-05-11 12:22:53 +08:00
|
|
|
exportLayout({
|
|
|
|
|
reload: false,
|
|
|
|
|
onlyData: false,
|
|
|
|
|
errorExit: false
|
|
|
|
|
});
|
2023-04-26 10:25:26 +08:00
|
|
|
window.siyuan.altIsPressed = false;
|
|
|
|
|
window.siyuan.ctrlIsPressed = false;
|
|
|
|
|
window.siyuan.shiftIsPressed = false;
|
|
|
|
|
document.body.classList.remove("body--blur");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const winOnBlur = () => {
|
|
|
|
|
document.body.classList.add("body--blur");
|
2023-04-27 11:19:21 +08:00
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2023-04-26 10:25:26 +08:00
|
|
|
const winOnClose = (currentWindow: Electron.BrowserWindow, close = false) => {
|
2023-05-11 12:22:53 +08:00
|
|
|
exportLayout({
|
|
|
|
|
reload: false,
|
|
|
|
|
cb() {
|
|
|
|
|
if (window.siyuan.config.appearance.closeButtonBehavior === 1 && !close) {
|
|
|
|
|
// 最小化
|
|
|
|
|
if ("windows" === window.siyuan.config.system.os) {
|
|
|
|
|
ipcRenderer.send(Constants.SIYUAN_CONFIG_TRAY, {
|
|
|
|
|
id: getCurrentWindow().id,
|
|
|
|
|
languages: window.siyuan.languages["_trayMenu"],
|
|
|
|
|
});
|
2023-04-26 10:25:26 +08:00
|
|
|
} else {
|
2023-05-11 12:22:53 +08:00
|
|
|
if (currentWindow.isFullScreen()) {
|
|
|
|
|
currentWindow.once("leave-full-screen", () => currentWindow.hide());
|
|
|
|
|
currentWindow.setFullScreen(false);
|
|
|
|
|
} else {
|
|
|
|
|
currentWindow.hide();
|
|
|
|
|
}
|
2023-04-26 10:25:26 +08:00
|
|
|
}
|
2023-05-11 12:22:53 +08:00
|
|
|
} else {
|
|
|
|
|
exitSiYuan();
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-05-11 12:22:53 +08:00
|
|
|
},
|
|
|
|
|
onlyData: false,
|
|
|
|
|
errorExit: true
|
|
|
|
|
});
|
2023-04-26 10:25:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const winOnMaxRestore = () => {
|
|
|
|
|
const currentWindow = getCurrentWindow();
|
|
|
|
|
const maxBtnElement = document.getElementById("maxWindow");
|
|
|
|
|
const restoreBtnElement = document.getElementById("restoreWindow");
|
|
|
|
|
if (currentWindow.isMaximized() || currentWindow.isFullScreen()) {
|
|
|
|
|
restoreBtnElement.style.display = "flex";
|
|
|
|
|
maxBtnElement.style.display = "none";
|
2022-05-26 15:18:53 +08:00
|
|
|
} else {
|
2023-04-26 10:25:26 +08:00
|
|
|
restoreBtnElement.style.display = "none";
|
|
|
|
|
maxBtnElement.style.display = "flex";
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2023-04-26 10:25:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const winOnEnterFullscreen = () => {
|
|
|
|
|
if (isWindow()) {
|
|
|
|
|
setTabPosition();
|
|
|
|
|
} else {
|
|
|
|
|
document.getElementById("toolbar").style.paddingLeft = "0";
|
|
|
|
|
}
|
2023-04-27 11:19:21 +08:00
|
|
|
};
|
2023-04-26 10:25:26 +08:00
|
|
|
|
|
|
|
|
const winOnLeaveFullscreen = () => {
|
|
|
|
|
if (isWindow()) {
|
|
|
|
|
setTabPosition();
|
|
|
|
|
} else {
|
|
|
|
|
document.getElementById("toolbar").setAttribute("style", "");
|
|
|
|
|
}
|
2023-04-27 11:19:21 +08:00
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
const currentWindow = getCurrentWindow();
|
|
|
|
|
currentWindow.on("focus", winOnFocus);
|
2023-04-26 10:25:26 +08:00
|
|
|
currentWindow.on("blur", winOnBlur);
|
2023-01-26 18:55:17 +08:00
|
|
|
if (!isWindow()) {
|
|
|
|
|
ipcRenderer.on(Constants.SIYUAN_OPENURL, (event, url) => {
|
2023-08-12 00:05:19 +08:00
|
|
|
if (url.startsWith("siyuan://plugins/")) {
|
2023-08-12 12:01:21 +08:00
|
|
|
const pluginId = url.replace("siyuan://plugins/", "").split("?")[0];
|
|
|
|
|
if (!pluginId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
app.plugins.find(plugin => {
|
|
|
|
|
if (pluginId.startsWith(plugin.name)) {
|
2023-08-12 00:05:19 +08:00
|
|
|
// siyuan://plugins/plugin-name/foo?bar=baz
|
2023-08-12 12:01:21 +08:00
|
|
|
plugin.eventBus.emit("open-siyuan-url-plugin", {url});
|
2023-08-12 00:05:19 +08:00
|
|
|
// siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
|
2023-08-12 12:01:21 +08:00
|
|
|
Object.keys(plugin.models).find(key => {
|
2023-08-12 00:05:19 +08:00
|
|
|
if (key === pluginId) {
|
|
|
|
|
let data = getSearch("data", url);
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(data || "{}");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("Error open plugin tab with protocol:", e);
|
|
|
|
|
}
|
|
|
|
|
openFile({
|
|
|
|
|
app,
|
|
|
|
|
custom: {
|
|
|
|
|
title: getSearch("title", url),
|
|
|
|
|
icon: getSearch("icon", url),
|
|
|
|
|
data,
|
|
|
|
|
fn: plugin.models[key]
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return true;
|
2023-06-15 23:36:06 +08:00
|
|
|
}
|
2023-08-12 00:05:19 +08:00
|
|
|
});
|
2023-08-12 12:01:21 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
2023-06-15 23:36:06 +08:00
|
|
|
}
|
|
|
|
|
if (isSYProtocol(url)) {
|
|
|
|
|
const id = getIdFromSYProtocol(url);
|
2023-08-12 00:05:19 +08:00
|
|
|
const focus = getSearch("focus", url) === "1";
|
2023-06-15 23:36:06 +08:00
|
|
|
fetchPost("/api/block/checkBlockExist", {id}, existResponse => {
|
|
|
|
|
if (existResponse.data) {
|
|
|
|
|
openFileById({
|
|
|
|
|
app,
|
|
|
|
|
id,
|
|
|
|
|
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2023-08-12 00:05:19 +08:00
|
|
|
zoomIn: focus,
|
2023-06-15 23:36:06 +08:00
|
|
|
});
|
|
|
|
|
ipcRenderer.send(Constants.SIYUAN_SHOW, getCurrentWindow().id);
|
|
|
|
|
}
|
2023-08-12 00:05:19 +08:00
|
|
|
app.plugins.forEach(plugin => {
|
2023-08-12 12:01:21 +08:00
|
|
|
plugin.eventBus.emit("open-siyuan-url-block", {
|
2023-08-12 00:05:19 +08:00
|
|
|
url,
|
|
|
|
|
id,
|
|
|
|
|
focus,
|
|
|
|
|
exist: existResponse.data,
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-06-15 23:36:06 +08:00
|
|
|
});
|
2023-01-26 18:55:17 +08:00
|
|
|
return;
|
2023-01-04 23:03:08 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
2023-01-26 18:55:17 +08:00
|
|
|
ipcRenderer.on(Constants.SIYUAN_SAVE_CLOSE, (event, close) => {
|
|
|
|
|
winOnClose(currentWindow, close);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-02-27 10:58:07 +08:00
|
|
|
ipcRenderer.on(Constants.SIYUAN_SEND_WINDOWS, (e, ipcData: IWebSocketData) => {
|
|
|
|
|
onWindowsMsg(ipcData);
|
2023-01-12 16:36:50 +08:00
|
|
|
});
|
2022-09-20 01:12:28 +08:00
|
|
|
ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => {
|
2023-01-06 20:26:14 +08:00
|
|
|
window.siyuan.printWin.destroy();
|
2022-09-20 01:12:28 +08:00
|
|
|
});
|
|
|
|
|
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => {
|
|
|
|
|
dialog.showOpenDialog({
|
|
|
|
|
title: window.siyuan.languages.export + " PDF",
|
|
|
|
|
properties: ["createDirectory", "openDirectory"],
|
2023-04-02 10:41:40 +08:00
|
|
|
}).then(async (result: OpenDialogReturnValue) => {
|
2022-09-20 01:12:28 +08:00
|
|
|
if (result.canceled) {
|
2023-01-06 20:26:14 +08:00
|
|
|
window.siyuan.printWin.destroy();
|
2022-09-20 01:12:28 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-09-20 10:33:00 +08:00
|
|
|
const msgId = showMessage(window.siyuan.languages.exporting, -1);
|
2022-12-31 22:14:42 +08:00
|
|
|
window.siyuan.storage[Constants.LOCAL_EXPORTPDF] = {
|
2022-10-27 09:55:34 +08:00
|
|
|
removeAssets: ipcData.removeAssets,
|
2022-12-11 10:03:47 +08:00
|
|
|
keepFold: ipcData.keepFold,
|
2022-12-11 10:05:57 +08:00
|
|
|
mergeSubdocs: ipcData.mergeSubdocs,
|
2022-12-31 22:14:42 +08:00
|
|
|
landscape: ipcData.pdfOptions.landscape,
|
|
|
|
|
marginType: ipcData.pdfOptions.marginType,
|
|
|
|
|
pageSize: ipcData.pdfOptions.pageSize,
|
|
|
|
|
scale: ipcData.pdfOptions.scale,
|
2023-03-29 16:06:30 +08:00
|
|
|
marginTop: ipcData.pdfOptions.margins.top,
|
|
|
|
|
marginRight: ipcData.pdfOptions.margins.right,
|
|
|
|
|
marginBottom: ipcData.pdfOptions.margins.bottom,
|
|
|
|
|
marginLeft: ipcData.pdfOptions.margins.left,
|
2022-12-31 22:14:42 +08:00
|
|
|
};
|
2023-01-01 15:18:32 +08:00
|
|
|
setStorageVal(Constants.LOCAL_EXPORTPDF, window.siyuan.storage[Constants.LOCAL_EXPORTPDF]);
|
2022-09-20 10:33:00 +08:00
|
|
|
try {
|
2023-04-02 10:41:40 +08:00
|
|
|
if (window.siyuan.config.export.pdfFooter.trim()) {
|
2023-04-26 10:25:26 +08:00
|
|
|
const response = await fetchSyncPost("/api/template/renderSprig", {template: window.siyuan.config.export.pdfFooter});
|
2023-04-02 10:41:40 +08:00
|
|
|
ipcData.pdfOptions.displayHeaderFooter = true;
|
|
|
|
|
ipcData.pdfOptions.headerTemplate = "<span></span>";
|
|
|
|
|
ipcData.pdfOptions.footerTemplate = `<div style="text-align:center;width:100%;font-size:8px;line-height:12px;">
|
2023-04-02 10:42:42 +08:00
|
|
|
${response.data.replace("%pages", "<span class=totalPages></span>").replace("%page", "<span class=pageNumber></span>")}
|
2023-04-02 10:41:40 +08:00
|
|
|
</div>`;
|
|
|
|
|
}
|
2022-09-20 10:33:00 +08:00
|
|
|
window.siyuan.printWin.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => {
|
|
|
|
|
fetchPost("/api/export/exportHTML", {
|
|
|
|
|
id: ipcData.rootId,
|
|
|
|
|
pdf: true,
|
|
|
|
|
removeAssets: ipcData.removeAssets,
|
2022-12-11 11:47:01 +08:00
|
|
|
merge: ipcData.mergeSubdocs,
|
2022-12-10 16:26:12 +08:00
|
|
|
savePath: result.filePaths[0]
|
2022-09-20 10:33:00 +08:00
|
|
|
}, () => {
|
2022-12-10 16:26:12 +08:00
|
|
|
const pdfFilePath = path.join(result.filePaths[0], replaceLocalPath(ipcData.rootTitle) + ".pdf");
|
2022-09-20 10:33:00 +08:00
|
|
|
fs.writeFileSync(pdfFilePath, pdfData);
|
2023-01-06 20:26:14 +08:00
|
|
|
window.siyuan.printWin.destroy();
|
2023-02-23 10:50:08 +08:00
|
|
|
fetchPost("/api/export/processPDF", {
|
2022-09-20 01:12:28 +08:00
|
|
|
id: ipcData.rootId,
|
2022-12-11 12:17:43 +08:00
|
|
|
merge: ipcData.mergeSubdocs,
|
2023-02-23 11:18:05 +08:00
|
|
|
path: pdfFilePath,
|
|
|
|
|
removeAssets: ipcData.removeAssets,
|
2022-09-20 01:12:28 +08:00
|
|
|
}, () => {
|
2022-09-20 10:33:00 +08:00
|
|
|
afterExport(pdfFilePath, msgId);
|
|
|
|
|
if (ipcData.removeAssets) {
|
|
|
|
|
const removePromise = (dir: string) => {
|
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
|
//先读文件夹
|
|
|
|
|
fs.stat(dir, function (err, stat) {
|
|
|
|
|
if (stat) {
|
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
|
fs.readdir(dir, function (err, files) {
|
|
|
|
|
files = files.map(file => path.join(dir, file)); // a/b a/m
|
|
|
|
|
Promise.all(files.map(file => removePromise(file))).then(function () {
|
|
|
|
|
fs.rmdir(dir, resolve);
|
2022-09-20 01:12:28 +08:00
|
|
|
});
|
2022-09-20 10:33:00 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
fs.unlink(dir, resolve);
|
2022-09-20 01:12:28 +08:00
|
|
|
}
|
2022-09-20 10:33:00 +08:00
|
|
|
}
|
2022-09-20 01:12:28 +08:00
|
|
|
});
|
2022-09-20 10:33:00 +08:00
|
|
|
});
|
|
|
|
|
};
|
2022-12-10 16:26:12 +08:00
|
|
|
removePromise(path.join(result.filePaths[0], "assets"));
|
2022-09-20 10:33:00 +08:00
|
|
|
}
|
2022-09-20 01:12:28 +08:00
|
|
|
});
|
|
|
|
|
});
|
2022-09-20 10:33:00 +08:00
|
|
|
}).catch((error: string) => {
|
|
|
|
|
showMessage("Export PDF error:" + error, 0, "error", msgId);
|
2023-01-06 20:26:14 +08:00
|
|
|
window.siyuan.printWin.destroy();
|
2022-09-20 10:33:00 +08:00
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMessage("Export PDF failed: " + e, 0, "error", msgId);
|
2023-01-06 20:26:14 +08:00
|
|
|
window.siyuan.printWin.destroy();
|
2022-09-20 10:33:00 +08:00
|
|
|
}
|
|
|
|
|
window.siyuan.printWin.hide();
|
2022-09-20 01:12:28 +08:00
|
|
|
});
|
|
|
|
|
});
|
2023-04-26 10:25:26 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
window.addEventListener("beforeunload", () => {
|
|
|
|
|
currentWindow.off("focus", winOnFocus);
|
2023-04-26 10:25:26 +08:00
|
|
|
currentWindow.off("blur", winOnBlur);
|
|
|
|
|
if ("darwin" === window.siyuan.config.system.os) {
|
|
|
|
|
currentWindow.off("enter-full-screen", winOnEnterFullscreen);
|
|
|
|
|
currentWindow.off("leave-full-screen", winOnLeaveFullscreen);
|
|
|
|
|
} else {
|
|
|
|
|
currentWindow.off("enter-full-screen", winOnMaxRestore);
|
|
|
|
|
currentWindow.off("leave-full-screen", winOnMaxRestore);
|
|
|
|
|
currentWindow.off("maximize", winOnMaxRestore);
|
|
|
|
|
currentWindow.off("unmaximize", winOnMaxRestore);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
}, false);
|
2023-04-26 10:25:26 +08:00
|
|
|
|
2023-02-13 23:16:01 +08:00
|
|
|
if (isWindow()) {
|
|
|
|
|
document.body.insertAdjacentHTML("beforeend", `<div class="toolbar__window">
|
|
|
|
|
<div class="toolbar__item b3-tooltips b3-tooltips__sw" aria-label="${window.siyuan.languages.pin}" id="pinWindow">
|
|
|
|
|
<svg>
|
|
|
|
|
<use xlink:href="#iconPin"></use>
|
|
|
|
|
</svg>
|
|
|
|
|
</div></div>`);
|
2023-02-17 15:46:46 +08:00
|
|
|
const pinElement = document.getElementById("pinWindow");
|
2023-02-13 23:16:01 +08:00
|
|
|
pinElement.addEventListener("click", () => {
|
2023-02-17 15:46:46 +08:00
|
|
|
pinElement.classList.toggle("toolbar__item--active");
|
2023-02-13 23:16:01 +08:00
|
|
|
if (pinElement.classList.contains("toolbar__item--active")) {
|
|
|
|
|
pinElement.setAttribute("aria-label", window.siyuan.languages.unpin);
|
2023-02-17 15:46:46 +08:00
|
|
|
currentWindow.setAlwaysOnTop(true, "pop-up-menu");
|
2023-02-13 23:16:01 +08:00
|
|
|
} else {
|
|
|
|
|
pinElement.setAttribute("aria-label", window.siyuan.languages.pin);
|
2023-02-17 15:46:46 +08:00
|
|
|
currentWindow.setAlwaysOnTop(false);
|
2023-02-13 23:16:01 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-26 18:55:17 +08:00
|
|
|
if ("darwin" === window.siyuan.config.system.os) {
|
2023-01-26 18:53:57 +08:00
|
|
|
document.getElementById("drag")?.addEventListener("dblclick", () => {
|
2022-05-26 15:18:53 +08:00
|
|
|
if (currentWindow.isMaximized()) {
|
|
|
|
|
currentWindow.unmaximize();
|
|
|
|
|
} else {
|
|
|
|
|
currentWindow.maximize();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const toolbarElement = document.getElementById("toolbar");
|
2023-04-26 10:25:26 +08:00
|
|
|
currentWindow.on("enter-full-screen", winOnEnterFullscreen);
|
|
|
|
|
currentWindow.on("leave-full-screen", winOnLeaveFullscreen);
|
2023-01-26 20:15:54 +08:00
|
|
|
if (currentWindow.isFullScreen() && !isWindow()) {
|
2022-05-26 15:18:53 +08:00
|
|
|
toolbarElement.style.paddingLeft = "0";
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-26 10:25:26 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
document.body.classList.add("body--win32");
|
2023-01-12 11:35:34 +08:00
|
|
|
|
2023-01-26 18:53:57 +08:00
|
|
|
// 添加窗口控件
|
2023-06-26 23:56:48 +08:00
|
|
|
const controlsHTML = `<div class="toolbar__item b3-tooltips b3-tooltips__sw toolbar__item--win" aria-label="${window.siyuan.languages.min}" id="minWindow">
|
2022-05-26 15:18:53 +08:00
|
|
|
<svg>
|
|
|
|
|
<use xlink:href="#iconMin"></use>
|
|
|
|
|
</svg>
|
|
|
|
|
</div>
|
2023-06-26 23:56:48 +08:00
|
|
|
<div aria-label="${window.siyuan.languages.max}" class="b3-tooltips b3-tooltips__sw toolbar__item toolbar__item--win" id="maxWindow">
|
2022-10-14 16:49:43 +08:00
|
|
|
<svg>
|
2022-05-26 15:18:53 +08:00
|
|
|
<use xlink:href="#iconMax"></use>
|
|
|
|
|
</svg>
|
|
|
|
|
</div>
|
2023-06-26 23:56:48 +08:00
|
|
|
<div aria-label="${window.siyuan.languages.restore}" class="b3-tooltips b3-tooltips__sw toolbar__item toolbar__item--win" id="restoreWindow">
|
2022-05-26 15:18:53 +08:00
|
|
|
<svg>
|
|
|
|
|
<use xlink:href="#iconRestore"></use>
|
|
|
|
|
</svg>
|
|
|
|
|
</div>
|
|
|
|
|
<div aria-label="${window.siyuan.languages.close}" class="b3-tooltips b3-tooltips__sw toolbar__item toolbar__item--close" id="closeWindow">
|
|
|
|
|
<svg>
|
|
|
|
|
<use xlink:href="#iconClose"></use>
|
|
|
|
|
</svg>
|
|
|
|
|
</div>`;
|
2023-01-26 20:15:54 +08:00
|
|
|
if (isWindow()) {
|
2023-02-13 23:16:01 +08:00
|
|
|
document.querySelector(".toolbar__window").insertAdjacentHTML("beforeend", controlsHTML);
|
2023-01-26 20:15:54 +08:00
|
|
|
} else {
|
|
|
|
|
document.getElementById("windowControls").innerHTML = controlsHTML;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
const maxBtnElement = document.getElementById("maxWindow");
|
|
|
|
|
const restoreBtnElement = document.getElementById("restoreWindow");
|
|
|
|
|
|
|
|
|
|
restoreBtnElement.addEventListener("click", () => {
|
|
|
|
|
if (currentWindow.isFullScreen()) {
|
|
|
|
|
currentWindow.setFullScreen(false);
|
|
|
|
|
} else {
|
|
|
|
|
currentWindow.unmaximize();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
maxBtnElement.addEventListener("click", () => {
|
|
|
|
|
currentWindow.maximize();
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-26 10:25:26 +08:00
|
|
|
winOnMaxRestore();
|
|
|
|
|
currentWindow.on("maximize", winOnMaxRestore);
|
|
|
|
|
currentWindow.on("unmaximize", winOnMaxRestore);
|
|
|
|
|
currentWindow.on("enter-full-screen", winOnMaxRestore);
|
|
|
|
|
currentWindow.on("leave-full-screen", winOnMaxRestore);
|
2022-05-26 15:18:53 +08:00
|
|
|
const minBtnElement = document.getElementById("minWindow");
|
|
|
|
|
const closeBtnElement = document.getElementById("closeWindow");
|
|
|
|
|
minBtnElement.addEventListener("click", () => {
|
|
|
|
|
if (minBtnElement.classList.contains("window-controls__item--disabled")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
currentWindow.minimize();
|
|
|
|
|
});
|
|
|
|
|
closeBtnElement.addEventListener("click", () => {
|
2023-01-26 20:59:58 +08:00
|
|
|
if (isWindow()) {
|
|
|
|
|
currentWindow.destroy();
|
|
|
|
|
} else {
|
|
|
|
|
winOnClose(currentWindow);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
|
/// #else
|
2023-01-26 20:59:58 +08:00
|
|
|
if (!isWindow()) {
|
|
|
|
|
document.querySelector(".toolbar").classList.add("toolbar--browser");
|
|
|
|
|
}
|
2023-04-09 17:29:19 +08:00
|
|
|
window.addEventListener("beforeunload", () => {
|
2023-05-11 12:22:53 +08:00
|
|
|
exportLayout({
|
|
|
|
|
reload: false,
|
|
|
|
|
onlyData: false,
|
|
|
|
|
errorExit: false
|
|
|
|
|
});
|
2023-04-09 17:29:19 +08:00
|
|
|
}, false);
|
|
|
|
|
window.addEventListener("pagehide", () => {
|
2023-05-11 12:22:53 +08:00
|
|
|
exportLayout({
|
|
|
|
|
reload: false,
|
|
|
|
|
onlyData: false,
|
|
|
|
|
errorExit: false
|
|
|
|
|
});
|
2023-04-09 17:29:19 +08:00
|
|
|
}, false);
|
2022-05-26 15:18:53 +08:00
|
|
|
/// #endif
|
|
|
|
|
};
|