This commit is contained in:
Vanessa 2023-10-08 20:02:30 +08:00
parent 2ca7253d1f
commit 7a70b53289
5 changed files with 84 additions and 78 deletions

View file

@ -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) => {

View file

@ -347,57 +347,57 @@ export const initWindow = (app: App) => {
${response.data.replace("%pages", "<span class=totalPages></span>").replace("%page", "<span class=pageNumber></span>")}
</div>`;
}
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", () => {

View file

@ -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";

View file

@ -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);
});
</script></body></html>`;
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);
});
};

View file

@ -327,7 +327,6 @@ interface ISiyuan {
storage?: {
[key: string]: any
},
printWin?: import("electron").BrowserWindow
transactions?: {
protyle: IProtyle,
doOperations: IOperation[],