import {hideMessage, showMessage} from "../../dialog/message";
import {Constants} from "../../constants";
/// #if !BROWSER
import {ipcRenderer, 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";
import {replaceLocalPath} from "../../editor/rename";
export const saveExport = (option: { type: string, id: string }) => {
/// #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 = localStorage.getItem(Constants.LOCAL_EXPORTWORD);
const wordDialog = new Dialog({
title: "Word " + window.siyuan.languages.config,
content: `
`;
fetchPost("/api/export/exportPreviewHTML", {
id,
tpl: html
}, response => {
if (response.code === 1) {
document.getElementById("message").firstElementChild.innerHTML === "";
showMessage(response.msg, undefined, "error");
destroyWin(win);
return;
}
win.loadURL(response.data.url);
});
};
const getExportPath = (option: { type: string, id: string }, 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], replaceLocalPath(response.data.rootTitle));
fetchPost(url, {
id: option.id,
pdf: option.type === "pdf",
removeAssets,
savePath
}, exportResponse => {
if (option.type === "word") {
if (exportResponse.code === 1) {
showMessage(exportResponse.msg, undefined, "error");
hideMessage(msgId);
return;
}
afterExport(savePath, msgId);
} else {
onExport(exportResponse, savePath, option.type, removeAssets, msgId);
}
});
}
});
});
};
const onExport = (data: IWebSocketData, filePath: string, type: string, 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;
}
const html = `