🎨 导出 PDF 和 Word 时支持覆盖已有文件 https://github.com/siyuan-note/siyuan/issues/5309

This commit is contained in:
Liang Ding 2022-07-19 22:52:00 +08:00
parent 9a64c860ce
commit c0d5d3ba59
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 9 additions and 25 deletions

View File

@ -1,7 +1,7 @@
import {showMessage} from "../../dialog/message"; import {showMessage} from "../../dialog/message";
import {Constants} from "../../constants"; import {Constants} from "../../constants";
/// #if !BROWSER /// #if !BROWSER
import {PrintToPDFOptions, SaveDialogReturnValue} from "electron"; import {PrintToPDFOptions, OpenDialogReturnValue} from "electron";
import {BrowserWindow, dialog} from "@electron/remote"; import {BrowserWindow, dialog} from "@electron/remote";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
@ -138,15 +138,10 @@ const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintTo
break; break;
} }
dialog.showSaveDialog({ dialog.showOpenDialog({
title: window.siyuan.languages.export + " " + exportType, title: window.siyuan.languages.export + " " + exportType,
defaultPath: response.data.rootTitle + "." + extension, properties: ["createDirectory", "openDirectory"],
filters: [{ }).then((result: OpenDialogReturnValue) => {
name: exportType,
extensions: [extension]
}],
properties: ["showOverwriteConfirmation"],
}).then((result: SaveDialogReturnValue) => {
if (!result.canceled) { if (!result.canceled) {
const msgId = showMessage(window.siyuan.languages.exporting, -1); const msgId = showMessage(window.siyuan.languages.exporting, -1);
let url = "/api/export/exportHTML"; let url = "/api/export/exportHTML";
@ -158,12 +153,12 @@ const getExportPath = (option: { type: string, id: string }, pdfOption?: PrintTo
fetchPost(url, { fetchPost(url, {
id: option.id, id: option.id,
pdf: option.type === "pdf", pdf: option.type === "pdf",
savePath: result.filePath savePath: result.filePaths[0]
}, exportResponse => { }, exportResponse => {
if (option.type === "word") { if (option.type === "word") {
afterExport(result.filePath, msgId); afterExport(result.filePaths[0], msgId);
} else { } else {
onExport(exportResponse, result.filePath, option.type, pdfOption, removeAssets, msgId); onExport(exportResponse, result.filePaths[0], option.type, pdfOption, removeAssets, msgId);
} }
}); });
} }

View File

@ -180,17 +180,13 @@ func ExportDocx(id, savePath string) (err error) {
return errors.New(msg) return errors.New(msg)
} }
if 1 < strings.Count(savePath, filepath.Base(savePath)) { if err = gulu.File.Copy(tmpDocxPath, filepath.Join(savePath, name+".docx")); nil != err {
savePath = filepath.Dir(savePath)
}
if err = gulu.File.Copy(tmpDocxPath, savePath); nil != err {
logging.LogErrorf("export docx failed: %s", err) logging.LogErrorf("export docx failed: %s", err)
return errors.New(fmt.Sprintf(Conf.Language(14), err)) return errors.New(fmt.Sprintf(Conf.Language(14), err))
} }
tmpAssets := filepath.Join(tmpDir, "assets") tmpAssets := filepath.Join(tmpDir, "assets")
if gulu.File.IsDir(tmpAssets) { if gulu.File.IsDir(tmpAssets) {
if err = gulu.File.Copy(tmpAssets, filepath.Join(filepath.Dir(savePath), "assets")); nil != err { if err = gulu.File.Copy(tmpAssets, filepath.Join(savePath, "assets")); nil != err {
logging.LogErrorf("export docx failed: %s", err) logging.LogErrorf("export docx failed: %s", err)
return errors.New(fmt.Sprintf(Conf.Language(14), err)) return errors.New(fmt.Sprintf(Conf.Language(14), err))
} }
@ -203,9 +199,6 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
tree = exportTree(tree, true) tree = exportTree(tree, true)
name = path.Base(tree.HPath) name = path.Base(tree.HPath)
if 1 < strings.Count(savePath, filepath.Base(savePath)) {
savePath = filepath.Dir(savePath)
}
if err := os.MkdirAll(savePath, 0755); nil != err { if err := os.MkdirAll(savePath, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err) logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
@ -290,10 +283,6 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
tree = exportTree(tree, true) tree = exportTree(tree, true)
name = path.Base(tree.Path) name = path.Base(tree.Path)
if 1 < strings.Count(savePath, filepath.Base(savePath)) {
savePath = filepath.Dir(savePath)
}
if err := os.MkdirAll(savePath, 0755); nil != err { if err := os.MkdirAll(savePath, 0755); nil != err {
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err) logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
return return