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, fetchSyncPost} from "../../util/fetch";
import {Dialog} from "../../dialog";
import {replaceLocalPath} from "../../editor/rename";
import {getScreenWidth, isInAndroid, isInHarmony, setStorageVal} from "../util/compatibility";
import {getFrontend} from "../../util/functions";
const getPluginStyle = async () => {
const response = await fetchSyncPost("/api/petal/loadPetals", {frontend: getFrontend()});
let css = "";
// 为加快启动速度,不进行 await
response.data.forEach((item: IPluginData) => {
css += item.css || "";
});
return css;
};
const getIconScript = (servePath: string) => {
const isBuiltInIcon = ["ant", "material"].includes(window.siyuan.config.appearance.icon);
const html = isBuiltInIcon ? "" : ``;
return html + ``;
}
export const saveExport = (option: IExportOptions) => {
/// #if BROWSER
if (["html", "htmlmd"].includes(option.type)) {
const msgId = showMessage(window.siyuan.languages.exporting, -1);
// 浏览器环境:先调用 API 生成资源文件,再在前端生成完整的 HTML
const url = option.type === "htmlmd" ? "/api/export/exportMdHTML" : "/api/export/exportHTML";
fetchPost(url, {
id: option.id,
pdf: false,
removeAssets: false,
merge: true,
savePath: ""
}, async exportResponse => {
const html = await onExport(exportResponse, undefined, "", option);
fetchPost("/api/export/exportBrowserHTML", {
folder: exportResponse.data.folder,
html: html,
name: exportResponse.data.name
}, zipResponse => {
hideMessage(msgId);
if (zipResponse.code === -1) {
showMessage(window.siyuan.languages._kernel[14] + ": " + zipResponse.msg, 0, "error");
return;
}
window.open(zipResponse.data.zip);
showMessage(window.siyuan.languages.exported);
});
});
return;
}
/// #else
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, false, true);
}
/// #endif
};
const getSnippetCSS = () => {
let snippetCSS = "";
document.querySelectorAll("style").forEach((item) => {
if (item.id.startsWith("snippet")) {
snippetCSS += item.outerHTML;
}
});
return snippetCSS;
};
/// #if !BROWSER
const renderPDF = async (id: string) => {
const localData = window.siyuan.storage[Constants.LOCAL_EXPORTPDF];
const servePathWithoutTrailingSlash = window.location.protocol + "//" + window.location.host;
const servePath = servePathWithoutTrailingSlash + "/";
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
${getSnippetCSS()}
${getIconScript(servePath)}
`;
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, msgId);
}
});
}
});
};
/// #endif
export const onExport = async (data: IWebSocketData, filePath: string, servePath: string, exportOption: IExportOptions, 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 screenWidth = getScreenWidth();
const minWidthHtml = isInAndroid() || isInHarmony() ? `document.body.style.minWidth = "${screenWidth}px"` : "";
const html = `
${themeStyle}
${data.data.name}
${getSnippetCSS()}
${data.data.content}
${getIconScript(servePath)}
`;
// 移动端导出 pdf、浏览器导出 HTML
if (typeof filePath === "undefined") {
return html;
}
/// #if !BROWSER
const htmlPath = path.join(filePath, "index.html");
fs.writeFileSync(htmlPath, html);
afterExport(htmlPath, msgId);
/// #endif
};