mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-16 17:10:53 +08:00
This commit is contained in:
parent
3127ff1e52
commit
c1e0b4ff60
@ -15,7 +15,7 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
const {
|
const {
|
||||||
net, app, BrowserWindow, shell, Menu, screen, ipcMain, globalShortcut, Tray,
|
net, app, BrowserWindow, shell, Menu, screen, ipcMain, globalShortcut, Tray, dialog
|
||||||
} = require("electron");
|
} = require("electron");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
@ -390,7 +390,7 @@ const boot = () => {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
workspaces.push({
|
workspaces.push({
|
||||||
browserWindow: currentWindow, id: currentWindow.id,
|
browserWindow: currentWindow,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -667,7 +667,7 @@ app.whenReady().then(() => {
|
|||||||
});
|
});
|
||||||
ipcMain.on("siyuan-config-tray", (event, data) => {
|
ipcMain.on("siyuan-config-tray", (event, data) => {
|
||||||
workspaces.find(item => {
|
workspaces.find(item => {
|
||||||
if (item.id === data.id) {
|
if (item.browserWindow.webContents.id === event.sender.id) {
|
||||||
hideWindow(item.browserWindow);
|
hideWindow(item.browserWindow);
|
||||||
if ("win32" === process.platform || "linux" === process.platform) {
|
if ("win32" === process.platform || "linux" === process.platform) {
|
||||||
resetTrayMenu(item.tray, data.languages, item.browserWindow);
|
resetTrayMenu(item.tray, data.languages, item.browserWindow);
|
||||||
@ -677,10 +677,20 @@ app.whenReady().then(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
ipcMain.on("siyuan-export-pdf", (event, data) => {
|
ipcMain.on("siyuan-export-pdf", (event, data) => {
|
||||||
BrowserWindow.fromId(data.id).webContents.send("siyuan-export-pdf", data);
|
dialog.showOpenDialog({
|
||||||
|
title: data.title,
|
||||||
|
properties: ["createDirectory", "openDirectory"],
|
||||||
|
}).then((result) => {
|
||||||
|
if (result.canceled) {
|
||||||
|
event.sender.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data.filePaths = result.filePaths;
|
||||||
|
BrowserWindow.fromId(BrowserWindow.getAllWindows().find((win) => win.webContents.id === event.sender.id).id).getParentWindow().send("siyuan-export-pdf", data);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
ipcMain.on("siyuan-export-close", (event, id) => {
|
ipcMain.on("siyuan-export-close", (event) => {
|
||||||
BrowserWindow.fromId(id).webContents.send("siyuan-export-close", id);
|
event.sender.destroy();
|
||||||
});
|
});
|
||||||
ipcMain.on("siyuan-export-prevent", (event, id) => {
|
ipcMain.on("siyuan-export-prevent", (event, id) => {
|
||||||
BrowserWindow.fromId(id).webContents.on("will-navigate", (event) => {
|
BrowserWindow.fromId(id).webContents.on("will-navigate", (event) => {
|
||||||
|
@ -188,7 +188,6 @@ export const initWindow = (app: App) => {
|
|||||||
// 最小化
|
// 最小化
|
||||||
if ("windows" === window.siyuan.config.system.os) {
|
if ("windows" === window.siyuan.config.system.os) {
|
||||||
ipcRenderer.send(Constants.SIYUAN_CONFIG_TRAY, {
|
ipcRenderer.send(Constants.SIYUAN_CONFIG_TRAY, {
|
||||||
id: getCurrentWindow().id,
|
|
||||||
languages: window.siyuan.languages["_trayMenu"],
|
languages: window.siyuan.languages["_trayMenu"],
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -308,9 +307,6 @@ export const initWindow = (app: App) => {
|
|||||||
ipcRenderer.on(Constants.SIYUAN_SEND_WINDOWS, (e, ipcData: IWebSocketData) => {
|
ipcRenderer.on(Constants.SIYUAN_SEND_WINDOWS, (e, ipcData: IWebSocketData) => {
|
||||||
onWindowsMsg(ipcData);
|
onWindowsMsg(ipcData);
|
||||||
});
|
});
|
||||||
ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => {
|
|
||||||
window.siyuan.printWin.destroy();
|
|
||||||
});
|
|
||||||
ipcRenderer.on(Constants.SIYUAN_HOTKEY, (e, data) => {
|
ipcRenderer.on(Constants.SIYUAN_HOTKEY, (e, data) => {
|
||||||
let matchCommand = false;
|
let matchCommand = false;
|
||||||
app.plugins.find(item => {
|
app.plugins.find(item => {
|
||||||
@ -326,91 +322,82 @@ export const initWindow = (app: App) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => {
|
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, async (e, ipcData) => {
|
||||||
dialog.showOpenDialog({
|
const msgId = showMessage(window.siyuan.languages.exporting, -1);
|
||||||
title: window.siyuan.languages.export + " PDF",
|
window.siyuan.storage[Constants.LOCAL_EXPORTPDF] = {
|
||||||
properties: ["createDirectory", "openDirectory"],
|
removeAssets: ipcData.removeAssets,
|
||||||
}).then(async (result: OpenDialogReturnValue) => {
|
keepFold: ipcData.keepFold,
|
||||||
if (result.canceled) {
|
mergeSubdocs: ipcData.mergeSubdocs,
|
||||||
window.siyuan.printWin.destroy();
|
landscape: ipcData.pdfOptions.landscape,
|
||||||
return;
|
marginType: ipcData.pdfOptions.marginType,
|
||||||
}
|
pageSize: ipcData.pdfOptions.pageSize,
|
||||||
const msgId = showMessage(window.siyuan.languages.exporting, -1);
|
scale: ipcData.pdfOptions.scale,
|
||||||
window.siyuan.storage[Constants.LOCAL_EXPORTPDF] = {
|
marginTop: ipcData.pdfOptions.margins.top,
|
||||||
removeAssets: ipcData.removeAssets,
|
marginRight: ipcData.pdfOptions.margins.right,
|
||||||
keepFold: ipcData.keepFold,
|
marginBottom: ipcData.pdfOptions.margins.bottom,
|
||||||
mergeSubdocs: ipcData.mergeSubdocs,
|
marginLeft: ipcData.pdfOptions.margins.left,
|
||||||
landscape: ipcData.pdfOptions.landscape,
|
};
|
||||||
marginType: ipcData.pdfOptions.marginType,
|
setStorageVal(Constants.LOCAL_EXPORTPDF, window.siyuan.storage[Constants.LOCAL_EXPORTPDF]);
|
||||||
pageSize: ipcData.pdfOptions.pageSize,
|
try {
|
||||||
scale: ipcData.pdfOptions.scale,
|
if (window.siyuan.config.export.pdfFooter.trim()) {
|
||||||
marginTop: ipcData.pdfOptions.margins.top,
|
const response = await fetchSyncPost("/api/template/renderSprig", {template: window.siyuan.config.export.pdfFooter});
|
||||||
marginRight: ipcData.pdfOptions.margins.right,
|
ipcData.pdfOptions.displayHeaderFooter = true;
|
||||||
marginBottom: ipcData.pdfOptions.margins.bottom,
|
ipcData.pdfOptions.headerTemplate = "<span></span>";
|
||||||
marginLeft: ipcData.pdfOptions.margins.left,
|
ipcData.pdfOptions.footerTemplate = `<div style="text-align:center;width:100%;font-size:8px;line-height:12px;">
|
||||||
};
|
|
||||||
setStorageVal(Constants.LOCAL_EXPORTPDF, window.siyuan.storage[Constants.LOCAL_EXPORTPDF]);
|
|
||||||
try {
|
|
||||||
if (window.siyuan.config.export.pdfFooter.trim()) {
|
|
||||||
const response = await fetchSyncPost("/api/template/renderSprig", {template: window.siyuan.config.export.pdfFooter});
|
|
||||||
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;">
|
|
||||||
${response.data.replace("%pages", "<span class=totalPages></span>").replace("%page", "<span class=pageNumber></span>")}
|
${response.data.replace("%pages", "<span class=totalPages></span>").replace("%page", "<span class=pageNumber></span>")}
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
|
||||||
window.siyuan.printWin.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => {
|
|
||||||
fetchPost("/api/export/exportHTML", {
|
|
||||||
id: ipcData.rootId,
|
|
||||||
pdf: true,
|
|
||||||
removeAssets: ipcData.removeAssets,
|
|
||||||
merge: ipcData.mergeSubdocs,
|
|
||||||
savePath: result.filePaths[0]
|
|
||||||
}, () => {
|
|
||||||
const pdfFilePath = path.join(result.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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
fs.unlink(dir, resolve);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
removePromise(path.join(result.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();
|
|
||||||
}
|
}
|
||||||
window.siyuan.printWin.hide();
|
window.siyuan.printWin.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => {
|
||||||
});
|
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);
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
fs.unlink(dir, resolve);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
window.siyuan.printWin.hide();
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("beforeunload", () => {
|
window.addEventListener("beforeunload", () => {
|
||||||
|
@ -461,15 +461,14 @@ const renderPDF = (id: string) => {
|
|||||||
actionElement.querySelector("#landscape").addEventListener('change', () => {
|
actionElement.querySelector("#landscape").addEventListener('change', () => {
|
||||||
setPadding();
|
setPadding();
|
||||||
});
|
});
|
||||||
const currentWindowId = ${getCurrentWindow().id};
|
|
||||||
actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => {
|
actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => {
|
||||||
const {ipcRenderer} = require("electron");
|
const {ipcRenderer} = require("electron");
|
||||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_CLOSE}", currentWindowId)
|
ipcRenderer.send("${Constants.SIYUAN_EXPORT_CLOSE}")
|
||||||
});
|
});
|
||||||
actionElement.querySelector('.b3-button--text').addEventListener('click', () => {
|
actionElement.querySelector('.b3-button--text').addEventListener('click', () => {
|
||||||
const {ipcRenderer} = require("electron");
|
const {ipcRenderer} = require("electron");
|
||||||
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", {
|
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", {
|
||||||
id: currentWindowId,
|
title: "${window.siyuan.languages.export} PDF",
|
||||||
pdfOptions:{
|
pdfOptions:{
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
landscape: actionElement.querySelector("#landscape").checked,
|
landscape: actionElement.querySelector("#landscape").checked,
|
||||||
|
Loading…
Reference in New Issue
Block a user