diff --git a/app/pnpm-lock.yaml b/app/pnpm-lock.yaml index eed988a4f..cd45f4b2a 100644 --- a/app/pnpm-lock.yaml +++ b/app/pnpm-lock.yaml @@ -1906,10 +1906,10 @@ packages: resolution: {integrity: sha512-m0+M53+HYMzqKxwNQZT143K7WwXEGUy9LY31l8dJphXx2P/FQod615mVbxHyqbDCG4J5bHdWm21qZ0e2DVY6CQ==} engines: {node: '>=14.0.0'} dependencies: - 7zip-bin: 5.1.1 '@develar/schema-utils': 2.6.5 '@electron/universal': 1.2.1 '@malept/flatpak-bundler': 0.4.0 + 7zip-bin: 5.1.1 async-exit-hook: 2.0.1 bluebird-lst: 1.0.9 builder-util: 23.3.3 @@ -2193,9 +2193,9 @@ packages: /builder-util/23.3.3: resolution: {integrity: sha512-MJZlUiq2PY5hjYv9+XNaoYdsITqvLgRDoHSFg/4nzpInbNxNjLQOolL04Zsyp+hgfcbFvMC4h0KkR1CMPHLWbA==} dependencies: - 7zip-bin: 5.1.1 '@types/debug': 4.1.7 '@types/fs-extra': 9.0.13 + 7zip-bin: 5.1.1 app-builder-bin: 4.0.0 bluebird-lst: 1.0.9 builder-util-runtime: 9.0.3 diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.ts index 31661e3b7..aa243cd61 100644 --- a/app/src/protyle/export/index.ts +++ b/app/src/protyle/export/index.ts @@ -1,7 +1,7 @@ import {hideMessage, showMessage} from "../../dialog/message"; import {Constants} from "../../constants"; /// #if !BROWSER -import {ipcRenderer, OpenDialogReturnValue} from "electron"; +import {OpenDialogReturnValue} from "electron"; import {BrowserWindow, dialog} from "@electron/remote"; import * as fs from "fs"; import * as path from "path"; @@ -61,10 +61,6 @@ export const saveExport = (option: { type: string, id: string }) => { }; /// #if !BROWSER -const destroyWin = (win: Electron.BrowserWindow) => { - win.destroy(); -}; - const renderPDF = (id: string) => { const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_EXPORTPDF) || JSON.stringify({ printBackground: true, @@ -75,7 +71,7 @@ const renderPDF = (id: string) => { removeAssets: true, })); const servePath = window.location.protocol + "//" + window.location.host; - const win = new BrowserWindow({ + window.siyuan.printWin = new BrowserWindow({ show: true, width: 1032, resizable: false, @@ -89,74 +85,6 @@ const renderPDF = (id: string) => { webSecurity: false, }, }); - ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => { - win.destroy(); - }); - ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => { - dialog.showOpenDialog({ - title: window.siyuan.languages.export + " PDF", - properties: ["createDirectory", "openDirectory"], - }).then((result: OpenDialogReturnValue) => { - if (result.canceled) { - destroyWin(win); - return; - } - - setTimeout(() => { - const msgId = showMessage(window.siyuan.languages.exporting, -1); - const filePath = result.filePaths[0].endsWith(ipcData.rootTitle) ? result.filePaths[0] : path.join(result.filePaths[0], replaceLocalPath(ipcData.rootTitle)); - localStorage.setItem(Constants.LOCAL_EXPORTPDF, JSON.stringify(Object.assign(ipcData.pdfOptions, {removeAssets: ipcData.removeAssets}))); - try { - win.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => { - fetchPost("/api/export/exportHTML", { - id: ipcData.rootId, - pdf: true, - removeAssets: ipcData.removeAssets, - savePath: filePath - }, () => { - const pdfFilePath = path.join(filePath, path.basename(filePath) + ".pdf"); - fs.writeFileSync(pdfFilePath, pdfData); - destroyWin(win); - fetchPost("/api/export/addPDFOutline", { - id: ipcData.rootId, - path: pdfFilePath - }, () => { - 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); - }); - }); - } else { - fs.unlink(dir, resolve); - } - } - }); - }); - }; - removePromise(path.join(filePath, "assets")); - } - }); - }); - }).catch((error: string) => { - showMessage("Export PDF error:" + error, 0, "error", msgId); - destroyWin(win); - }); - } catch (e) { - showMessage("Export PDF failed: " + e, 0, "error", msgId); - destroyWin(win); - } - }, 200); - }); - }); let pdfWidth = ""; if (localData.pageSize === "A3") { if (localData.landscape) { @@ -218,50 +146,57 @@ const renderPDF = (id: string) => { ${window.siyuan.languages.export} PDF - {tpl.name} @@ -399,7 +334,7 @@ const renderPDF = (id: string) => { previewElement.classList.add("exporting"); }); `; - win.loadURL("data:text/html;charset=UTF-8," + encodeURIComponent(` + window.siyuan.printWin.loadURL("data:text/html;charset=UTF-8," + encodeURIComponent(` @@ -416,8 +351,8 @@ const renderPDF = (id: string) => {
`)); - win.webContents.on("did-finish-load", () => { - if (win.webContents.getURL().startsWith("data:text/html;charset=UTF-8,")) { + window.siyuan.printWin.webContents.on("did-finish-load", () => { + if (window.siyuan.printWin.webContents.getURL().startsWith("data:text/html;charset=UTF-8,")) { fetchPost("/api/export/exportPreviewHTML", { id, tpl: html @@ -425,10 +360,10 @@ const renderPDF = (id: string) => { if (response.code === 1) { document.getElementById("message").firstElementChild.innerHTML === ""; showMessage(response.msg, undefined, "error"); - destroyWin(win); + window.siyuan.printWin.destroy() return; } - win.loadURL(response.data.url); + window.siyuan.printWin.loadURL(response.data.url); }); } }); diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 3e5cd58c0..778cf1a8b 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -89,6 +89,7 @@ interface INotebook { } interface ISiyuan { + printWin?: import("electron").BrowserWindow transactionsTimeout?: number, transactions?: { protyle: IProtyle, diff --git a/app/src/util/onGetConfig.ts b/app/src/util/onGetConfig.ts index 586e1ea48..b738d9432 100644 --- a/app/src/util/onGetConfig.ts +++ b/app/src/util/onGetConfig.ts @@ -2,8 +2,12 @@ import {openSearch} from "../search/spread"; import {exportLayout, JSONToLayout, resizeDrag, resizeTabs} from "../layout/util"; import {hotKey2Electron, updateHotkeyTip} from "../protyle/util/compatibility"; /// #if !BROWSER -import {ipcRenderer} from "electron"; import {getCurrentWindow} from "@electron/remote"; +import {ipcRenderer, OpenDialogReturnValue} from "electron"; +import {dialog} from "@electron/remote"; +import * as fs from "fs"; +import * as path from "path"; +import {afterExport} from "../protyle/export/util"; /// #endif import {Constants} from "../constants"; import {appearance} from "../config/appearance"; @@ -22,6 +26,8 @@ import {getSearch} from "./functions"; import {openHistory} from "./history"; import {initStatus} from "../layout/status"; import {syncGuide} from "../sync/syncGuide"; +import {showMessage} from "../dialog/message"; +import {replaceLocalPath} from "../editor/rename"; const matchKeymap = (keymap: Record, key1: "general" | "editor", key2?: "general" | "insert" | "heading" | "list" | "table") => { if (key1 === "general") { @@ -340,6 +346,74 @@ const initWindow = () => { ipcRenderer.on(Constants.SIYUAN_SAVE_CLOSE, (event, close) => { winOnClose(currentWindow, close); }); + ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => { + window.siyuan.printWin.destroy(); + }); + ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => { + dialog.showOpenDialog({ + title: window.siyuan.languages.export + " PDF", + properties: ["createDirectory", "openDirectory"], + }).then((result: OpenDialogReturnValue) => { + if (result.canceled) { + window.siyuan.printWin.destroy(); + return; + } + + setTimeout(() => { + const msgId = showMessage(window.siyuan.languages.exporting, -1); + const filePath = result.filePaths[0].endsWith(ipcData.rootTitle) ? result.filePaths[0] : path.join(result.filePaths[0], replaceLocalPath(ipcData.rootTitle)); + localStorage.setItem(Constants.LOCAL_EXPORTPDF, JSON.stringify(Object.assign(ipcData.pdfOptions, {removeAssets: ipcData.removeAssets}))); + try { + window.siyuan.printWin.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => { + fetchPost("/api/export/exportHTML", { + id: ipcData.rootId, + pdf: true, + removeAssets: ipcData.removeAssets, + savePath: filePath + }, () => { + const pdfFilePath = path.join(filePath, path.basename(filePath) + ".pdf"); + fs.writeFileSync(pdfFilePath, pdfData); + window.siyuan.printWin.destroy(); + fetchPost("/api/export/addPDFOutline", { + id: ipcData.rootId, + path: pdfFilePath + }, () => { + 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); + }); + }); + } else { + fs.unlink(dir, resolve); + } + } + }); + }); + }; + removePromise(path.join(filePath, "assets")); + } + }); + }); + }).catch((error: string) => { + showMessage("Export PDF error:" + error, 0, "error", msgId); + window.siyuan.printWin.destroy(); + }); + } catch (e) { + showMessage("Export PDF failed: " + e, 0, "error", msgId); + window.siyuan.printWin.destroy(); + } + }, 200); + }); + }); window.addEventListener("beforeunload", () => { currentWindow.off("focus", winOnFocus); }, false);