Vanessa 2022-09-08 16:37:59 +08:00
parent 19176e5bc0
commit 012d1d113e
4 changed files with 333 additions and 260 deletions

View file

@ -84,7 +84,9 @@ try {
}
} catch (e) {
console.error(e)
require('electron').dialog.showErrorBox('创建配置目录失败 Failed to create config directory',
require('electron').
dialog.
showErrorBox('创建配置目录失败 Failed to create config directory',
'思源需要在用户家目录下创建配置文件夹(~/.config/siyuan请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.')
app.exit()
}
@ -186,7 +188,8 @@ const boot = () => {
})
require('@electron/remote/main').enable(mainWindow.webContents)
mainWindow.webContents.userAgent = 'SiYuan/' + appVer + ' https://b3log.org/siyuan Electron'
mainWindow.webContents.userAgent = 'SiYuan/' + appVer +
' https://b3log.org/siyuan Electron'
// 发起互联网服务请求时绕过安全策略 https://github.com/siyuan-note/siyuan/issues/5516
mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
@ -348,6 +351,12 @@ const boot = () => {
tray.destroy()
}
})
ipcMain.on('siyuan-export-pdf', (event, data) => {
mainWindow.webContents.send('siyuan-export-pdf', data)
})
ipcMain.on('siyuan-export-close', (event, data) => {
mainWindow.webContents.send('siyuan-export-close', data)
})
ipcMain.on('siyuan-quit', () => {
try {
const bounds = mainWindow.getBounds()
@ -520,11 +529,13 @@ const initKernel = (initData) => {
'<pre><code>net stop winnat\nnetsh interface ipv4 add excludedportrange protocol=tcp startport=6806 numberofports=1\nnet start winnat</code></pre></div>')
break
case 22:
showErrorWindow('⚠️ 创建配置目录失败 Failed to create config directory',
showErrorWindow(
'⚠️ 创建配置目录失败 Failed to create config directory',
`<div>思源需要在用户家目录下创建配置文件夹(~/.config/siyuan请确保该路径具有写入权限。</div><div>SiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.</div>`)
break
case 23:
showErrorWindow('⚠️ 无法读写块树文件 Failed to access blocktree file',
showErrorWindow(
'⚠️ 无法读写块树文件 Failed to access blocktree file',
`<div>块树文件正在被其他程序锁定或者已经损坏,请删除 工作空间/temp/ 文件夹后重启</div><div>The block tree file is being locked by another program or is damaged, please delete the workspace/temp/ folder and restart.</div>`)
break
case 0:

View file

@ -64,7 +64,8 @@ export const initConfigSearch = (element: HTMLElement) => {
// 关于
getLang(["about", "about1", "about2", "about3", "about4", "about5", "about6", "about7", "about8",
"about11", "about12", "about13", "about14", "about15", "about16",
"slogan", "currentVer", "checkUpdate", "updatePath", "snapshotPassword", "systemLog"]),
"slogan", "currentVer", "checkUpdate", "updatePath", "snapshotPassword", "systemLog",
"autoDownloadUpdatePkg"]),
];
const inputElement = element.querySelector(".b3-form__icon input") as HTMLInputElement;
if (window.siyuan.config.system.container !== "ios") {

View file

@ -29,6 +29,8 @@ export abstract class Constants {
public static readonly SIYUAN_INIT: string = "siyuan-init";
public static readonly SIYUAN_OPENURL: string = "siyuan-openurl";
public static readonly SIYUAN_SAVE_CLOSE: string = "siyuan-save-close";
public static readonly SIYUAN_EXPORT_PDF: string = "siyuan-export-pdf";
public static readonly SIYUAN_EXPORT_CLOSE: string = "siyuan-export-close";
// size
public static readonly SIZE_TOOLBAR_HEIGHT: number = 30;

View file

@ -1,7 +1,7 @@
import {hideMessage, showMessage} from "../../dialog/message";
import {Constants} from "../../constants";
/// #if !BROWSER
import {PrintToPDFOptions, OpenDialogReturnValue} from "electron";
import {OpenDialogReturnValue, ipcRenderer} from "electron";
import {BrowserWindow, dialog} from "@electron/remote";
import * as fs from "fs";
import * as path from "path";
@ -18,6 +18,50 @@ 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: `<div class="b3-dialog__content">
<label class="fn__flex b3-label">
<div class="fn__flex-1">
${window.siyuan.languages.exportPDF4}
</div>
<span class="fn__space"></span>
<input id="removeAssets" class="b3-switch" type="checkbox" ${localData === "true" ? "checked" : ""}>
</label>
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
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, removeAssets);
wordDialog.destroy();
});
} else {
getExportPath(option);
}
/// #endif
};
/// #if !BROWSER
const renderPDF = (id: string) => {
const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_EXPORTPDF) || JSON.stringify({
printBackground: true,
landscape: false,
@ -26,9 +70,164 @@ export const saveExport = (option: { type: string, id: string }) => {
pageSize: "A4",
removeAssets: true,
}));
const pdfDialog = new Dialog({
title: "PDF " + window.siyuan.languages.config,
content: `<div class="b3-dialog__content">
const servePath = window.location.protocol + "//" + window.location.host;
const win = new BrowserWindow({
show: true,
width: 1024, // 860
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
webviewTag: true,
webSecurity: false,
},
});
ipcRenderer.on(Constants.SIYUAN_EXPORT_CLOSE, () => {
win.destroy();
});
ipcRenderer.on(Constants.SIYUAN_EXPORT_PDF, (e, ipcData) => {
dialog.showOpenDialog({
title: window.siyuan.languages.export + " PDF",
properties: ["createDirectory", "openDirectory"],
}).then((result: OpenDialogReturnValue) => {
if (!result.canceled) {
const msgId = showMessage(window.siyuan.languages.exporting, -1);
const filePath = result.filePaths[0].endsWith(ipcData.rootTitle) ? result.filePaths[0] : path.join(result.filePaths[0], replaceLocalPath(ipcData.rootTitle));
localStorage.setItem(Constants.LOCAL_EXPORTPDF, JSON.stringify(Object.assign(ipcData.pdfOptions, {removeAssets: ipcData.removeAssets})));
try {
win.webContents.printToPDF(ipcData.pdfOptions).then((pdfData) => {
fetchPost("/api/export/exportHTML", {
id: ipcData.rootId,
pdf: true,
removeAssets: ipcData.removeAssets,
savePath: filePath
}, () => {
const pdfFilePath = path.join(filePath, path.basename(filePath) + ".pdf");
fs.writeFileSync(pdfFilePath, pdfData);
fetchPost("/api/export/addPDFOutline", {
id: ipcData.rootId,
path: pdfFilePath
}, () => {
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(filePath, "assets"));
}
win.destroy();
});
});
}).catch((error: string) => {
showMessage("Export PDF error:" + error, 0, "error", msgId);
win.destroy();
});
} catch (e) {
showMessage("Export PDF error:" + e + ". Export HTML and use Chrome's printing function to convert to PDF", 0, "error", msgId);
}
}
})
})
fetchPost("/api/export/exportPreviewHTML", {
id,
}, response => {
let pdfWidth = "";
if (localData.pageSize === "A3") {
if (localData.landscape) {
pdfWidth = "16.5";
} else {
pdfWidth = "11.7";
}
} else if (localData.pageSize === "A4") {
if (localData.landscape) {
pdfWidth = "11.7";
} else {
pdfWidth = "8.8";
}
} else if (localData.pageSize === "A5") {
if (localData.landscape) {
pdfWidth = "8.3";
} else {
pdfWidth = "5.8";
}
} else if (localData.pageSize === "Legal") {
if (localData.landscape) {
pdfWidth = "14";
} else {
pdfWidth = "8.5";
}
} else if (localData.pageSize === "Letter") {
if (localData.landscape) {
pdfWidth = "11";
} else {
pdfWidth = "8.5";
}
} else if (localData.pageSize === "Tabloid") {
if (localData.landscape) {
pdfWidth = "17";
} else {
pdfWidth = "11";
}
}
let pdfMargin = "0.66";
if (localData.landscape) {
pdfMargin = "1.69";
}
if (localData.marginsType !== 0) {
if (localData.landscape) {
pdfMargin = "1.69";
} else {
pdfMargin = "0.3";
}
}
const html = `<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" type="text/css" id="themeDefaultStyle" href="${servePath}/stage/build/export/base.css?${Constants.SIYUAN_VERSION}"/>
<link rel="stylesheet" type="text/css" id="themeStyle" href="${servePath}/appearance/themes/${window.siyuan.config.appearance.themeLight}/${window.siyuan.config.appearance.customCSS ? "custom" : "theme"}.css?${Constants.SIYUAN_VERSION}"/>
<title>${response.data.name} - ${window.siyuan.languages.siyuanNote} v${Constants.SIYUAN_VERSION}</title>
<style>
::-webkit-scrollbar {
width: 0px;
height: 0px;
}
pre code {
max-height: none !important;
word-break: break-all !important;
white-space: pre-wrap !important;
}
.protyle-wysiwyg {
box-sizing: border-box;
padding: 34px ${pdfMargin}in 16px;
width: ${pdfWidth}in
}
${setInlineStyle(false)}
</style>
</head>
<body>
<div class="fn__flex">
<div class="protyle-wysiwyg protyle-wysiwyg--attr fn__flex-1" style="max-width: 800px;margin: 0 auto;" id="preview">${response.data.content.replace(/<iframe /g, "<no-iframe ")}</div>
<div style="width: 200px" id="action">
<label class="fn__flex b3-label">
<div class="fn__flex-1">
${window.siyuan.languages.exportPDF0}
@ -75,74 +274,76 @@ export const saveExport = (option: { type: string, id: string }) => {
<span class="fn__space"></span>
<input id="removeAssets" class="b3-switch" type="checkbox" ${localData.removeAssets ? "checked" : ""}>
</label>
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
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: `<div class="b3-dialog__content">
<label class="fn__flex b3-label">
<div class="fn__flex-1">
${window.siyuan.languages.exportPDF4}
</div>
<span class="fn__space"></span>
<input id="removeAssets" class="b3-switch" type="checkbox" ${localData === "true" ? "checked" : ""}>
</label>
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
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);
<script src="${servePath}/appearance/icons/${window.siyuan.config.appearance.icon}/icon.js?${Constants.SIYUAN_VERSION}"></script>
<script src="${servePath}/stage/build/export/protyle-method.js?${Constants.SIYUAN_VERSION}"></script>
<script src="${servePath}/stage/protyle/js/lute/lute.min.js?${Constants.SIYUAN_VERSION}"></script>
<script>
window.siyuan = {
config: {
appearance: { mode: 0, codeBlockThemeDark: "${window.siyuan.config.appearance.codeBlockThemeDark}", codeBlockThemeLight: "${window.siyuan.config.appearance.codeBlockThemeLight}" },
editor: {
codeLineWrap: true,
codeLigatures: ${window.siyuan.config.editor.codeLigatures},
plantUMLServePath: "${window.siyuan.config.editor.plantUMLServePath}",
codeSyntaxHighlightLineNum: ${window.siyuan.config.editor.codeSyntaxHighlightLineNum},
katexMacros: JSON.stringify(${window.siyuan.config.editor.katexMacros}),
}
/// #endif
};
},
languages: {copy:"${window.siyuan.languages.copy}"}
};
const previewElement = document.getElementById('preview');
Protyle.highlightRender(previewElement, "${servePath}/stage/protyle");
Protyle.mathRender(previewElement, "${servePath}/stage/protyle", true);
Protyle.mermaidRender(previewElement, "${servePath}/stage/protyle");
Protyle.flowchartRender(previewElement, "${servePath}/stage/protyle");
Protyle.graphvizRender(previewElement, "${servePath}/stage/protyle");
Protyle.chartRender(previewElement, "${servePath}/stage/protyle");
Protyle.mindmapRender(previewElement, "${servePath}/stage/protyle");
Protyle.abcRender(previewElement, "${servePath}/stage/protyle");
Protyle.plantumlRender(previewElement, "${servePath}/stage/protyle");
previewElement.querySelectorAll(".protyle-action__copy").forEach((item) => {
item.addEventListener("click", (event) => {
navigator.clipboard.writeText(item.parentElement.nextElementSibling.textContent.trimEnd());
event.preventDefault();
event.stopPropagation();
})
});
previewElement.querySelectorAll("table").forEach(item => {
if (item.clientWidth > item.parentElement.clientWidth) {
item.style.zoom = item.parentElement.clientWidth / item.clientWidth;
}
})
const actionElement = document.getElementById('action');
actionElement.querySelector('.b3-button--cancel').addEventListener('click', () => {
const {ipcRenderer} = require("electron");
ipcRenderer.send("${Constants.SIYUAN_EXPORT_CLOSE}")
});
actionElement.querySelector('.b3-button--text').addEventListener('click', () => {
const {ipcRenderer} = require("electron");
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", {
pdfOptions:{
printBackground: true,
landscape: actionElement.querySelector("#landscape").checked,
marginsType: parseInt(actionElement.querySelector("#marginsType").value),
scaleFactor: parseInt(actionElement.querySelector("#scaleFactor").value),
pageSize: actionElement.querySelector("#pageSize").value,
},
removeAssets: actionElement.querySelector("#removeAssets").checked,
rootId: "${id}",
rootTitle: "${response.data.name}"
})
actionElement.remove();
});
</script></body></html>`;
win.loadURL("data:text/html;charset=UTF-8," + encodeURIComponent(html));
})
}
/// #if !BROWSER
const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintToPDFOptions, removeAssets?: boolean) => {
const getExportPath = (option: { type: string, id: string }, removeAssets?: boolean) => {
fetchPost("/api/block/getBlockInfo", {
id: option.id
}, (response) => {
@ -192,7 +393,7 @@ const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintTo
}
afterExport(savePath, msgId);
} else {
onExport(exportResponse, savePath, option.type, pdfOption, removeAssets, msgId);
onExport(exportResponse, savePath, option.type, removeAssets, msgId);
}
});
}
@ -200,97 +401,14 @@ const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintTo
});
};
const onExport = (data: IWebSocketData, filePath: string, type: string, pdfOptions?: PrintToPDFOptions, removeAssets?: boolean, msgId?: string) => {
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;
}
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(/<iframe /g, "<no-iframe ");
let pdfWidth = "";
if (pdfOptions.pageSize === "A3") {
if (pdfOptions.landscape) {
pdfWidth = "16.5";
} else {
pdfWidth = "11.7";
}
} else if (pdfOptions.pageSize === "A4") {
if (pdfOptions.landscape) {
pdfWidth = "11.7";
} else {
pdfWidth = "8.8";
}
} else if (pdfOptions.pageSize === "A5") {
if (pdfOptions.landscape) {
pdfWidth = "8.3";
} else {
pdfWidth = "5.8";
}
} else if (pdfOptions.pageSize === "Legal") {
if (pdfOptions.landscape) {
pdfWidth = "14";
} else {
pdfWidth = "8.5";
}
} else if (pdfOptions.pageSize === "Letter") {
if (pdfOptions.landscape) {
pdfWidth = "11";
} else {
pdfWidth = "8.5";
}
} else if (pdfOptions.pageSize === "Tabloid") {
if (pdfOptions.landscape) {
pdfWidth = "17";
} else {
pdfWidth = "11";
}
}
let pdfMargin = "0.66";
if (pdfOptions.landscape) {
pdfMargin = "1.69";
}
if (pdfOptions.marginsType !== 0) {
if (pdfOptions.landscape) {
pdfMargin = "1.69";
} else {
pdfMargin = "0.3";
}
}
style = `::-webkit-scrollbar {
width: 0px;
height: 0px;
}
pre code {
max-height: none !important;
word-break: break-all !important;
white-space: pre-wrap !important;
}
.protyle-wysiwyg {
box-sizing: border-box;
padding: 34px ${pdfMargin}in 16px;
width: ${pdfWidth}in
}`;
tableZoom = `previewElement.querySelectorAll("table").forEach(item => {
if (item.clientWidth > item.parentElement.clientWidth) {
item.style.zoom = item.parentElement.clientWidth / item.clientWidth;
}
})`;
} else {
mediaRenderScript = "Protyle.mediaRender(previewElement);";
style = "body {background-color: var(--b3-theme-background);color: var(--b3-theme-on-background)}";
urlPrefix = "";
cdn = "stage/protyle";
}
// 导出 html、pdf
const html = `<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
@ -299,19 +417,19 @@ pre code {
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" type="text/css" id="themeDefaultStyle" href="${urlPrefix}stage/build/export/base.css?${Constants.SIYUAN_VERSION}"/>
<link rel="stylesheet" type="text/css" id="themeStyle" href="${urlPrefix}appearance/themes/${themeName}/${window.siyuan.config.appearance.customCSS ? "custom" : "theme"}.css?${Constants.SIYUAN_VERSION}"/>
<link rel="stylesheet" type="text/css" id="themeDefaultStyle" href="stage/build/export/base.css?${Constants.SIYUAN_VERSION}"/>
<link rel="stylesheet" type="text/css" id="themeStyle" href="appearance/themes/${themeName}/${window.siyuan.config.appearance.customCSS ? "custom" : "theme"}.css?${Constants.SIYUAN_VERSION}"/>
<title>${pathPosix().basename(filePath)} - ${window.siyuan.languages.siyuanNote} v${Constants.SIYUAN_VERSION}</title>
<style>
${style}
body {background-color: var(--b3-theme-background);color: var(--b3-theme-on-background)}
${setInlineStyle(false)}
</style>
</head>
<body>
<div class="${["htmlmd", "word"].includes(type) ? "b3-typography" : "protyle-wysiwyg protyle-wysiwyg--attr"}" style="max-width: 800px;margin: 0 auto;" id="preview">${data.data.content}</div>
<script src="${urlPrefix}appearance/icons/${window.siyuan.config.appearance.icon}/icon.js?${Constants.SIYUAN_VERSION}"></script>
<script src="${urlPrefix}stage/build/export/protyle-method.js?${Constants.SIYUAN_VERSION}"></script>
<script src="${urlPrefix}stage/protyle/js/lute/lute.min.js?${Constants.SIYUAN_VERSION}"></script>
<script src="appearance/icons/${window.siyuan.config.appearance.icon}/icon.js?${Constants.SIYUAN_VERSION}"></script>
<script src="stage/build/export/protyle-method.js?${Constants.SIYUAN_VERSION}"></script>
<script src="stage/protyle/js/lute/lute.min.js?${Constants.SIYUAN_VERSION}"></script>
<script>
window.siyuan = {
config: {
@ -327,16 +445,16 @@ pre code {
languages: {copy:"${window.siyuan.languages.copy}"}
};
const previewElement = document.getElementById('preview');
Protyle.highlightRender(previewElement, "${cdn}");
Protyle.mathRender(previewElement, "${cdn}", ${type === "pdf"});
Protyle.mermaidRender(previewElement, "${cdn}");
Protyle.flowchartRender(previewElement, "${cdn}");
Protyle.graphvizRender(previewElement, "${cdn}");
Protyle.chartRender(previewElement, "${cdn}");
Protyle.mindmapRender(previewElement, "${cdn}");
Protyle.abcRender(previewElement, "${cdn}");
Protyle.plantumlRender(previewElement, "${cdn}");
${mediaRenderScript}
Protyle.highlightRender(previewElement, "stage/protyle");
Protyle.mathRender(previewElement, "stage/protyle", ${type === "pdf"});
Protyle.mermaidRender(previewElement, "stage/protyle");
Protyle.flowchartRender(previewElement, "stage/protyle");
Protyle.graphvizRender(previewElement, "stage/protyle");
Protyle.chartRender(previewElement, "stage/protyle");
Protyle.mindmapRender(previewElement, "stage/protyle");
Protyle.abcRender(previewElement, "stage/protyle");
Protyle.plantumlRender(previewElement, "stage/protyle");
Protyle.mediaRender(previewElement);
document.querySelectorAll(".protyle-action__copy").forEach((item) => {
item.addEventListener("click", (event) => {
navigator.clipboard.writeText(item.parentElement.nextElementSibling.textContent.trimEnd());
@ -344,68 +462,9 @@ pre code {
event.stopPropagation();
})
});
${tableZoom}
</script></body></html>`;
if (type === "pdf") {
const win = new BrowserWindow({
show: false,
width: 860,
webPreferences: {
nodeIntegration: true,
webviewTag: true,
webSecurity: false,
},
});
require("@electron/remote/main").enable(win.webContents);
win.loadURL("data:text/html;charset=UTF-8," + encodeURIComponent(html));
const timeout = 1000 + (html.split('data-type="NodeCodeBlock"').length - 1) * 360;
win.webContents.on("did-finish-load", () => {
setTimeout(() => {
try {
win.webContents.printToPDF(pdfOptions).then((pdfData) => {
const pdfFilePath = path.join(filePath, path.basename(filePath) + ".pdf");
fs.writeFileSync(pdfFilePath, pdfData);
fetchPost("/api/export/addPDFOutline", {
id: data.data.id,
path: pdfFilePath
}, () => {
afterExport(pdfFilePath, msgId);
if (removeAssets) {
const removePromise = (dir: string) => {
return new Promise(function (resolve) {
//先读文件夹
fs.stat(dir, function (err, 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(filePath, "assets"));
}
});
win.destroy();
}).catch((error: string) => {
showMessage("Export PDF error:" + error, 0, "error", msgId);
win.destroy();
});
} catch (e) {
showMessage("Export PDF error:" + e + ". Export HTML and use Chrome's printing function to convert to PDF", 0, "error", msgId);
}
}, Math.min(timeout, 10000));
});
} else {
const htmlPath = path.join(filePath, "index.html");
fs.writeFileSync(htmlPath, html);
afterExport(htmlPath, msgId);
}
};
/// #endif