diff --git a/app/electron/main.js b/app/electron/main.js index 5cbec9f24..84e7fe17d 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -680,17 +680,26 @@ app.whenReady().then(() => { if (data.cmd === "askMicrophone") { return systemPreferences.askForMediaAccess("microphone"); } + if (data.cmd === "printToPDF") { + return getWindowByContentId(data.webContentsId).webContents.printToPDF(data.pdfOptions); + } }); - ipcMain.on("siyuan-cmd", (event, cmd) => { + ipcMain.on("siyuan-cmd", (event, data) => { + let cmd = data; + let webContentsId = event.sender.id + if (typeof data !== "string") { + cmd = data.cmd; + webContentsId = data.webContentsId + } switch (cmd) { case "openDevTools": event.sender.openDevTools({mode: "bottom"}); break; case "show": - showWindow(getWindowByContentId(event.sender.id)); + showWindow(getWindowByContentId(webContentsId)); break; case "hide": - getWindowByContentId(event.sender.id).hide(); + getWindowByContentId(webContentsId).hide(); break; case "redo": event.sender.redo(); @@ -698,6 +707,9 @@ app.whenReady().then(() => { case "undo": event.sender.undo(); break; + case "destroy": + getWindowByContentId(webContentsId).destroy(); + break; } }); ipcMain.on("siyuan-config-tray", (event, data) => { @@ -721,20 +733,37 @@ app.whenReady().then(() => { return; } data.filePaths = result.filePaths; + data.webContentsId = event.sender.id; getWindowByContentId(event.sender.id).getParentWindow().send("siyuan-export-pdf", data); }); }); - ipcMain.on("siyuan-export-close", (event) => { - event.sender.destroy(); - }); - ipcMain.on("siyuan-export-prevent", (event, id) => { - BrowserWindow.fromId(id).webContents.on("will-navigate", (event) => { - const url = event.url; - event.preventDefault(); - if (url.startsWith(localServer)) { + ipcMain.on("siyuan-export-newwindow", (event, data) => { + const printWin = new BrowserWindow({ + parent: getWindowByContentId(event.sender.id), + modal: true, + show: true, + width: 1032, + height: 650, + resizable: false, + frame: "darwin" === process.platform, + icon: path.join(appDir, "stage", "icon-large.png"), + titleBarStyle: "hidden", + webPreferences: { + contextIsolation: false, + nodeIntegration: true, + webviewTag: true, + webSecurity: false, + autoplayPolicy: "user-gesture-required" // 桌面端禁止自动播放多媒体 https://github.com/siyuan-note/siyuan/issues/7587 + }, + }); + printWin.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + printWin.webContents.userAgent; + printWin.loadURL(data); + printWin.webContents.on("will-navigate", (nEvent) => { + nEvent.preventDefault(); + if (nEvent.url.startsWith(localServer)) { return; } - shell.openExternal(url); + shell.openExternal(nEvent.url); }); }); ipcMain.on("siyuan-quit", (event, port) => { diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index 93baec780..fdb31d56f 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -347,57 +347,57 @@ export const initWindow = (app: App) => { ${response.data.replace("%pages", "").replace("%page", "")} `; } - window.siyuan.printWin.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => { - fetchPost("/api/export/exportHTML", { + const pdfData = await ipcRenderer.invoke(Constants.SIYUAN_GET, { + cmd: "printToPDF", + pdfOptions: ipcData.pdfOptions, + webContentsId: ipcData.webContentsId + }); + fetchPost("/api/export/exportHTML", { + id: ipcData.rootId, + pdf: true, + removeAssets: ipcData.removeAssets, + merge: ipcData.mergeSubdocs, + savePath: ipcData.filePaths[0] + }, () => { + const pdfFilePath = path.join(ipcData.filePaths[0], replaceLocalPath(ipcData.rootTitle) + ".pdf"); + fs.writeFileSync(pdfFilePath, pdfData); + ipcRenderer.send(Constants.SIYUAN_CMD, {cmd: "destroy", webContentsId: ipcData.webContentsId}); + fetchPost("/api/export/processPDF", { id: ipcData.rootId, - pdf: true, - removeAssets: ipcData.removeAssets, merge: ipcData.mergeSubdocs, - savePath: ipcData.filePaths[0] + path: pdfFilePath, + removeAssets: ipcData.removeAssets, }, () => { - const pdfFilePath = path.join(ipcData.filePaths[0], replaceLocalPath(ipcData.rootTitle) + ".pdf"); - fs.writeFileSync(pdfFilePath, pdfData); - window.siyuan.printWin.destroy(); - fetchPost("/api/export/processPDF", { - id: ipcData.rootId, - merge: ipcData.mergeSubdocs, - path: pdfFilePath, - removeAssets: ipcData.removeAssets, - }, () => { - 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); - }); + 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); - } + }); + } else { + fs.unlink(dir, resolve); } - }); + } }); - }; - removePromise(path.join(ipcData.filePaths[0], "assets")); - } - }); + }); + }; + removePromise(path.join(ipcData.filePaths[0], "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(); + ipcRenderer.send(Constants.SIYUAN_CMD, {cmd: "destroy", webContentsId: ipcData.webContentsId}); } - window.siyuan.printWin.hide(); + ipcRenderer.send(Constants.SIYUAN_CMD, {cmd: "hide", webContentsId: ipcData.webContentsId}); }); window.addEventListener("beforeunload", () => { diff --git a/app/src/constants.ts b/app/src/constants.ts index b0aa2fb60..5d1d6c5f4 100644 --- a/app/src/constants.ts +++ b/app/src/constants.ts @@ -41,8 +41,7 @@ export abstract class Constants { public static readonly SIYUAN_OPEN_FOLDER: string = "siyuan-open-folder"; public static readonly SIYUAN_EXPORT_PDF: string = "siyuan-export-pdf"; - public static readonly SIYUAN_EXPORT_CLOSE: string = "siyuan-export-close"; - public static readonly SIYUAN_EXPORT_PREVENT: string = "siyuan-export-prevent"; + public static readonly SIYUAN_EXPORT_NEWWINDOW: string = "siyuan-export-newwindow"; // custom public static readonly CUSTOM_SY_READONLY: string = "custom-sy-readonly"; diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.ts index ee90e2000..d7e9ca2a4 100644 --- a/app/src/protyle/export/index.ts +++ b/app/src/protyle/export/index.ts @@ -2,7 +2,6 @@ import {hideMessage, showMessage} from "../../dialog/message"; import {Constants} from "../../constants"; /// #if !BROWSER import {ipcRenderer} from "electron"; -import {app, BrowserWindow, getCurrentWindow} from "@electron/remote"; import * as fs from "fs"; import * as path from "path"; import {afterExport} from "./util"; @@ -463,7 +462,7 @@ const renderPDF = (id: string) => { }); actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => { const {ipcRenderer} = require("electron"); - ipcRenderer.send("${Constants.SIYUAN_EXPORT_CLOSE}") + ipcRenderer.send("${Constants.SIYUAN_CMD}", "destroy") }); actionElement.querySelector('.b3-button--text').addEventListener('click', () => { const {ipcRenderer} = require("electron"); @@ -499,28 +498,8 @@ const renderPDF = (id: string) => { renderPreview(response.data.content); }); `; - window.siyuan.printWin = new BrowserWindow({ - parent: getCurrentWindow(), - modal: true, - show: true, - width: 1032, - height: 650, - resizable: false, - frame: "darwin" === window.siyuan.config.system.os, - icon: path.join(window.siyuan.config.system.appDir, "stage", "icon-large.png"), - titleBarStyle: "hidden", - webPreferences: { - contextIsolation: false, - nodeIntegration: true, - webviewTag: true, - webSecurity: false, - autoplayPolicy: "user-gesture-required" // 桌面端禁止自动播放多媒体 https://github.com/siyuan-note/siyuan/issues/7587 - }, - }); - ipcRenderer.send(Constants.SIYUAN_EXPORT_PREVENT, window.siyuan.printWin.id); - window.siyuan.printWin.webContents.userAgent = `SiYuan/${app.getVersion()} https://b3log.org/siyuan Electron`; fetchPost("/api/export/exportTempContent", {content: html}, (response) => { - window.siyuan.printWin.loadURL(response.data.url); + ipcRenderer.send(Constants.SIYUAN_EXPORT_NEWWINDOW, response.data.url); }); }; diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index d8d76c616..d06185dfb 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -327,7 +327,6 @@ interface ISiyuan { storage?: { [key: string]: any }, - printWin?: import("electron").BrowserWindow transactions?: { protyle: IProtyle, doOperations: IOperation[],