import {hideMessage, showMessage} from "../../dialog/message"; import {Constants} from "../../constants"; /// #if !BROWSER import {ipcRenderer} from "electron"; import * as fs from "fs"; import * as path from "path"; import {afterExport} from "./util"; /// #endif import {confirmDialog} from "../../dialog/confirmDialog"; import {getThemeMode, setInlineStyle} from "../../util/assets"; import {fetchPost} from "../../util/fetch"; import {Dialog} from "../../dialog"; import {replaceLocalPath} from "../../editor/rename"; import {setStorageVal} from "../util/compatibility"; import {isPaidUser} from "../../util/needSubscribe"; export const saveExport = (option: IExportOptions) => { /// #if !BROWSER if (option.type === "pdf") { if (window.siyuan.config.appearance.mode === 1) { confirmDialog(window.siyuan.languages.pdfTip, window.siyuan.languages.pdfConfirm, () => { renderPDF(option.id); }); } else { renderPDF(option.id); } } else if (option.type === "word") { const localData = window.siyuan.storage[Constants.LOCAL_EXPORTWORD]; const wordDialog = new Dialog({ title: "Word " + window.siyuan.languages.config, content: `
`, width: "520px", }); wordDialog.element.setAttribute("data-key", Constants.DIALOG_EXPORTWORD); 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; const mergeSubdocs = (wordDialog.element.querySelector("#mergeSubdocs") as HTMLInputElement).checked; window.siyuan.storage[Constants.LOCAL_EXPORTWORD] = {removeAssets, mergeSubdocs}; setStorageVal(Constants.LOCAL_EXPORTWORD, window.siyuan.storage[Constants.LOCAL_EXPORTWORD]); getExportPath(option, removeAssets, mergeSubdocs); wordDialog.destroy(); }); } else { getExportPath(option); } /// #endif }; const getSnippetCSS = () => { let snippetCSS = ""; document.querySelectorAll("style").forEach((item) => { if (item.id.startsWith("snippet")) { snippetCSS += item.innerHTML; } }); return snippetCSS; }; /// #if !BROWSER const renderPDF = async (id: string) => { const localData = window.siyuan.storage[Constants.LOCAL_EXPORTPDF]; const servePath = window.location.protocol + "//" + window.location.host; const isDefault = (window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === "midnight") || (window.siyuan.config.appearance.mode === 0 && window.siyuan.config.appearance.themeLight === "daylight"); let themeStyle = ""; if (!isDefault) { themeStyle = ``; } const currentWindowId = await ipcRenderer.invoke(Constants.SIYUAN_GET, { cmd: "getContentsId", }); // data-theme-mode="light" https://github.com/siyuan-note/siyuan/issues/7379 const html = ` ${themeStyle} ${window.siyuan.languages.export} PDF
${window.siyuan.languages.exportPDF0}
${window.siyuan.languages.exportPDF2}
${window.siyuan.languages.marginTop}
${window.siyuan.languages.marginRight}
${window.siyuan.languages.marginBottom}
${window.siyuan.languages.marginLeft}
${window.siyuan.languages.exportPDF3} ${localData.scale || 1}
`; fetchPost("/api/export/exportTempContent", {content: html}, (response) => { ipcRenderer.send(Constants.SIYUAN_EXPORT_NEWWINDOW, response.data.url); }); }; const getExportPath = (option: IExportOptions, removeAssets?: boolean, mergeSubdocs?: boolean) => { fetchPost("/api/block/getBlockInfo", { id: option.id }, async (response) => { if (response.code === 3) { showMessage(response.msg); 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; } const result = await ipcRenderer.invoke(Constants.SIYUAN_GET, { cmd: "showOpenDialog", title: window.siyuan.languages.export + " " + exportType, properties: ["createDirectory", "openDirectory"], }); 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"; } let savePath = result.filePaths[0]; if (option.type !== "word" && !savePath.endsWith(response.data.rootTitle)) { savePath = path.join(savePath, replaceLocalPath(response.data.rootTitle)); } savePath = savePath.trim(); fetchPost(url, { id: option.id, pdf: option.type === "pdf", removeAssets: removeAssets, merge: mergeSubdocs, savePath }, exportResponse => { if (option.type === "word") { if (exportResponse.code === 1) { showMessage(exportResponse.msg, undefined, "error"); hideMessage(msgId); return; } afterExport(exportResponse.data.path, msgId); } else { onExport(exportResponse, savePath, option, removeAssets, msgId); } }); } }); }; const onExport = (data: IWebSocketData, filePath: string, exportOption: IExportOptions, removeAssets?: boolean, msgId?: string) => { let themeName = window.siyuan.config.appearance.themeLight; let mode = 0; if (["html", "htmlmd"].includes(exportOption.type) && window.siyuan.config.appearance.mode === 1) { themeName = window.siyuan.config.appearance.themeDark; mode = 1; } const isDefault = (window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === "midnight") || (window.siyuan.config.appearance.mode === 0 && window.siyuan.config.appearance.themeLight === "daylight"); let themeStyle = ""; if (!isDefault) { themeStyle = ``; } const html = ` ${themeStyle} ${data.data.name}
${data.data.content}
`; const htmlPath = path.join(filePath, "index.html"); fs.writeFileSync(htmlPath, html); afterExport(htmlPath, msgId); }; /// #endif