import {hideMessage, showMessage} from "../../dialog/message"; import {Constants} from "../../constants"; /// #if !BROWSER import {PrintToPDFOptions, OpenDialogReturnValue} from "electron"; import {BrowserWindow, dialog} from "@electron/remote"; import * as fs from "fs"; import * as path from "path"; import {afterExport} from "./util"; /// #endif import {confirmDialog} from "../../dialog/confirmDialog"; import {setInlineStyle} from "../../util/assets"; import {fetchPost} from "../../util/fetch"; import {Dialog} from "../../dialog"; import {lockFile} from "../../dialog/processSystem"; import {pathPosix} from "../../util/pathName"; export const saveExport = (option: { type: string, id: string }) => { /// #if !BROWSER if (option.type === "pdf") { const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_EXPORTPDF) || JSON.stringify({ printBackground: true, landscape: false, marginsType: 0, scaleFactor: 100, pageSize: "A4", removeAssets: true, })); const pdfDialog = new Dialog({ title: "PDF " + window.siyuan.languages.config, content: `
`, width: "520px", }); const btnsElement = pdfDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { pdfDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { const options: PrintToPDFOptions = { printBackground: true, landscape: (pdfDialog.element.querySelector("#landscape") as HTMLInputElement).checked, marginsType: parseInt((pdfDialog.element.querySelector("#marginsType") as HTMLInputElement).value), scaleFactor: parseInt((pdfDialog.element.querySelector("#scaleFactor") as HTMLInputElement).value), pageSize: (pdfDialog.element.querySelector("#pageSize") as HTMLSelectElement).value, }; const removeAssets = (pdfDialog.element.querySelector("#removeAssets") as HTMLInputElement).checked; localStorage.setItem(Constants.LOCAL_EXPORTPDF, JSON.stringify(Object.assign(options, {removeAssets}))); if (window.siyuan.config.appearance.mode === 1) { confirmDialog(window.siyuan.languages.pdfTip, window.siyuan.languages.pdfConfirm, () => { getExportPath(option, options, removeAssets); pdfDialog.destroy(); }); } else { getExportPath(option, options, removeAssets); pdfDialog.destroy(); } }); } else if (option.type === "word") { const localData = localStorage.getItem(Constants.LOCAL_EXPORTWORD); const wordDialog = new Dialog({ title: "Word " + window.siyuan.languages.config, content: `
`, width: "520px", }); const btnsElement = wordDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { wordDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { const removeAssets = (wordDialog.element.querySelector("#removeAssets") as HTMLInputElement).checked; localStorage.setItem(Constants.LOCAL_EXPORTWORD, removeAssets.toString()); getExportPath(option, undefined, removeAssets); wordDialog.destroy(); }); } else { getExportPath(option); } /// #endif }; /// #if !BROWSER const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintToPDFOptions, removeAssets?: boolean) => { fetchPost("/api/block/getBlockInfo", { id: option.id }, (response) => { if (response.code === 2) { // 文件被锁定 lockFile(response.data); return; } let exportType = "HTML (SiYuan)"; switch (option.type) { case "htmlmd": exportType = "HTML (Markdown)"; break; case "word": exportType = "Word .docx"; break; case "pdf": exportType = "PDF"; break; } dialog.showOpenDialog({ title: window.siyuan.languages.export + " " + exportType, properties: ["createDirectory", "openDirectory"], }).then((result: OpenDialogReturnValue) => { if (!result.canceled) { const msgId = showMessage(window.siyuan.languages.exporting, -1); let url = "/api/export/exportHTML"; if (option.type === "htmlmd") { url = "/api/export/exportMdHTML"; } else if (option.type === "word") { url = "/api/export/exportDocx"; } const savePath = result.filePaths[0].endsWith(response.data.rootTitle) ? result.filePaths[0] : path.join(result.filePaths[0], response.data.rootTitle); fetchPost(url, { id: option.id, pdf: option.type === "pdf", removeAssets, savePath }, exportResponse => { if (option.type === "word") { if (exportResponse.code === 1) { hideMessage(msgId); showMessage(exportResponse.msg, undefined, "error"); return; } afterExport(savePath, msgId); } else { onExport(exportResponse, savePath, option.type, pdfOption, removeAssets, msgId); } }); } }); }); }; const onExport = (data: IWebSocketData, filePath: string, type: string, pdfOptions?: PrintToPDFOptions, removeAssets?: boolean, msgId?: string) => { let themeName = window.siyuan.config.appearance.themeLight; let mode = 0; if (["html", "htmlmd"].includes(type) && window.siyuan.config.appearance.mode === 1) { themeName = window.siyuan.config.appearance.themeDark; mode = 1; } let style = ""; const servePath = window.location.protocol + "//" + window.location.host; let cdn = servePath + "/stage/protyle"; let mediaRenderScript = ""; let urlPrefix = servePath + "/"; let tableZoom = ""; if (type === "pdf") { data.data.content = data.data.content.replace(/