This commit is contained in:
Vanessa 2023-10-08 16:17:33 +08:00
parent 844c5ce688
commit ec65118339
6 changed files with 56 additions and 52 deletions

View file

@ -665,7 +665,7 @@ app.whenReady().then(() => {
app.exit(); app.exit();
}); });
ipcMain.handle("siyuan-dialog", (event, data) => { ipcMain.handle("siyuan-dialog", (event, data) => {
return dialog.showOpenDialog(data); return dialog[data.type || "showOpenDialog"](data);
}); });
ipcMain.on("siyuan-cmd", (event, cmd) => { ipcMain.on("siyuan-cmd", (event, cmd) => {
switch (cmd) { switch (cmd) {

View file

@ -1,12 +1,13 @@
import {fetchPost} from "../util/fetch"; import {fetchPost} from "../util/fetch";
/// #if !BROWSER /// #if !BROWSER
import {dialog} from "@electron/remote";
import {afterExport} from "../protyle/export/util"; import {afterExport} from "../protyle/export/util";
import { ipcRenderer } from "electron";
import * as path from "path"; import * as path from "path";
/// #endif /// #endif
import {isBrowser} from "../util/functions"; import {isBrowser} from "../util/functions";
import {showMessage} from "../dialog/message"; import {showMessage} from "../dialog/message";
import {showFileInFolder} from "../util/pathName"; import {showFileInFolder} from "../util/pathName";
import {Constants} from "../constants";
export const exportConfig = { export const exportConfig = {
element: undefined as Element, element: undefined as Element,
@ -179,13 +180,14 @@ export const exportConfig = {
}); });
} }
}); });
exportConfig.element.querySelector("#exportData").addEventListener("click", () => { exportConfig.element.querySelector("#exportData").addEventListener("click", async () => {
/// #if BROWSER /// #if BROWSER
fetchPost("/api/export/exportData", {}, response => { fetchPost("/api/export/exportData", {}, response => {
window.location.href = response.data.zip; window.location.href = response.data.zip;
}); });
/// #else /// #else
const filePaths = dialog.showOpenDialogSync({ const filePaths = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG,{
type: "showOpenDialogSync",
title: window.siyuan.languages.export + " " + "Data", title: window.siyuan.languages.export + " " + "Data",
properties: ["createDirectory", "openDirectory"], properties: ["createDirectory", "openDirectory"],
}); });
@ -209,7 +211,8 @@ export const exportConfig = {
}); });
const pandocBinElement = exportConfig.element.querySelector("#pandocBin") as HTMLInputElement; const pandocBinElement = exportConfig.element.querySelector("#pandocBin") as HTMLInputElement;
pandocBinElement.addEventListener("click", async () => { pandocBinElement.addEventListener("click", async () => {
const localPath = await dialog.showOpenDialog({ const localPath = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG,{
type: "showOpenDialog",
defaultPath: window.siyuan.config.system.homeDir, defaultPath: window.siyuan.config.system.homeDir,
properties: ["openFile"], properties: ["openFile"],
}); });

View file

@ -643,6 +643,7 @@ export const genImportMenu = (notebookId: string, pathString: string) => {
filters = [{name: "Markdown", extensions: ["md", "markdown"]}]; filters = [{name: "Markdown", extensions: ["md", "markdown"]}];
} }
const localPath = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG, { const localPath = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG, {
type: "showOpenDialog",
defaultPath: window.siyuan.config.system.homeDir, defaultPath: window.siyuan.config.system.homeDir,
filters, filters,
properties: [isDoc ? "openFile" : "openDirectory"], properties: [isDoc ? "openFile" : "openDirectory"],

View file

@ -1,6 +1,5 @@
/// #if !BROWSER /// #if !BROWSER
import {dialog} from "@electron/remote"; import {ipcRenderer} from "electron";
import {SaveDialogReturnValue} from "electron";
import * as path from "path"; import * as path from "path";
/// #endif /// #endif
import {fetchPost} from "../util/fetch"; import {fetchPost} from "../util/fetch";
@ -17,15 +16,15 @@ export const exportAsset = (src: string) => {
return { return {
label: window.siyuan.languages.export, label: window.siyuan.languages.export,
icon: "iconUpload", icon: "iconUpload",
click() { async click() {
dialog.showSaveDialog({ const result = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG,{
type: "showSaveDialog",
defaultPath: getAssetName(src) + pathPosix().extname(src), defaultPath: getAssetName(src) + pathPosix().extname(src),
properties: ["showOverwriteConfirmation"], properties: ["showOverwriteConfirmation"],
}).then((result: SaveDialogReturnValue) => {
if (!result.canceled) {
fetchPost("/api/file/copyFile", {src, dest: result.filePath});
}
}); });
if (!result.canceled) {
fetchPost("/api/file/copyFile", {src, dest: result.filePath});
}
} }
}; };
/// #endif /// #endif

View file

@ -1,6 +1,6 @@
import {MenuItem} from "./Menu"; import {MenuItem} from "./Menu";
/// #if !BROWSER /// #if !BROWSER
import {dialog, getCurrentWindow} from "@electron/remote"; import {getCurrentWindow} from "@electron/remote";
import {ipcRenderer} from "electron"; import {ipcRenderer} from "electron";
/// #endif /// #endif
import {openHistory} from "../history/history"; import {openHistory} from "../history/history";
@ -85,7 +85,8 @@ export const workspaceMenu = (app: App, rect: DOMRect) => {
label: `${window.siyuan.languages.new} / ${window.siyuan.languages.openBy}`, label: `${window.siyuan.languages.new} / ${window.siyuan.languages.openBy}`,
iconHTML: "", iconHTML: "",
click: async () => { click: async () => {
const localPath = await dialog.showOpenDialog({ const localPath = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG,{
type: "showOpenDialog",
defaultPath: window.siyuan.config.system.homeDir, defaultPath: window.siyuan.config.system.homeDir,
properties: ["openDirectory", "createDirectory"], properties: ["openDirectory", "createDirectory"],
}); });

View file

@ -1,8 +1,8 @@
import {hideMessage, showMessage} from "../../dialog/message"; import {hideMessage, showMessage} from "../../dialog/message";
import {Constants} from "../../constants"; import {Constants} from "../../constants";
/// #if !BROWSER /// #if !BROWSER
import {ipcRenderer, OpenDialogReturnValue} from "electron"; import {ipcRenderer} from "electron";
import {app, BrowserWindow, dialog, getCurrentWindow} from "@electron/remote"; import {app, BrowserWindow, getCurrentWindow} from "@electron/remote";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import {afterExport} from "./util"; import {afterExport} from "./util";
@ -527,7 +527,7 @@ const renderPDF = (id: string) => {
const getExportPath = (option: { type: string, id: string }, removeAssets?: boolean, mergeSubdocs?: boolean) => { const getExportPath = (option: { type: string, id: string }, removeAssets?: boolean, mergeSubdocs?: boolean) => {
fetchPost("/api/block/getBlockInfo", { fetchPost("/api/block/getBlockInfo", {
id: option.id id: option.id
}, (response) => { }, async (response) => {
if (response.code === 3) { if (response.code === 3) {
showMessage(response.msg); showMessage(response.msg);
return; return;
@ -545,43 +545,43 @@ const getExportPath = (option: { type: string, id: string }, removeAssets?: bool
break; break;
} }
dialog.showOpenDialog({ const result = await ipcRenderer.invoke(Constants.SIYUAN_DIALOG, {
type: "showOpenDialog",
title: window.siyuan.languages.export + " " + exportType, title: window.siyuan.languages.export + " " + exportType,
properties: ["createDirectory", "openDirectory"], 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";
}
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(path.join(savePath, replaceLocalPath(response.data.rootTitle)) + ".docx", msgId);
} else {
onExport(exportResponse, savePath, option.type, removeAssets, msgId);
}
});
}
}); });
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(path.join(savePath, replaceLocalPath(response.data.rootTitle)) + ".docx", msgId);
} else {
onExport(exportResponse, savePath, option.type, removeAssets, msgId);
}
});
}
}); });
}; };