Add desktop save function

This commit is contained in:
2022-04-26 15:17:24 +08:00
parent 0ab53c1800
commit ec3aaa5e3a
8 changed files with 147 additions and 51 deletions
+53 -1
View File
@@ -1,6 +1,7 @@
import { app, BrowserWindow, ipcMain } from "electron";
import { app, BrowserWindow, ipcMain, dialog } from "electron";
import { Service } from "@Service/Service";
import { join as pathJoin } from "path";
import { writeFile } from "fs";
const ENV = process.env ?? {};
class ElectronApp {
@@ -55,6 +56,7 @@ class ElectronApp {
this.simulatorWindow.loadURL(this.serviceUrl + (ENV.LIVING_TOGETHER_WEB_PATH ?? "/resources/app.asar/"));
this.handelSimulatorWindowBehavior();
this.handelFileChange();
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
@@ -90,6 +92,56 @@ class ElectronApp {
this.simulatorWindow?.on("maximize", sendWindowsChangeMessage);
this.simulatorWindow?.on("unmaximize", sendWindowsChangeMessage);
}
private handelFileChange() {
// 文件保存
const saveFile = async (path: string, text: string) => {
return new Promise((r) => {
writeFile(path ?? "", text, {}, (e) => {
this.simulatorWindow?.webContents.send(
"windows.EndFileSave",
(path.match(/.+(\/|\\)(.+)$/) ?? [])[2],
path, !e
);
r(undefined);
});
})
};
// 处理文件保存事件
ipcMain.on("windows.fileSave",
(_, text: string, name: string, title: string, button: string, url?: string) => {
// 如果没有路径,询问新的路径
if (url) {
saveFile(url, text);
}
// 询问保存位置
else {
dialog.showSaveDialog(this.simulatorWindow!, {
title: title,
buttonLabel: button,
filters: [
{ name: name, extensions: ["ltss"] }
]
}).then(res => {
// 用户选择后继续保存
if (!res.canceled && res.filePath) {
saveFile(res.filePath, text);
} else {
this.simulatorWindow?.webContents.send(
"windows.EndFileSave",
undefined, undefined, false
);
}
});
}
}
);
}
}
new ElectronApp().runMainThread();
+6
View File
@@ -2,6 +2,7 @@ import { Emitter } from "@Model/Emitter";
type IApiEmitterEvent = {
windowsSizeStateChange: void;
fileSave: {success: boolean, name: string, url: string};
}
interface ISimulatorAPI extends Emitter<IApiEmitterEvent> {
@@ -30,6 +31,11 @@ interface ISimulatorAPI extends Emitter<IApiEmitterEvent> {
* 是否处于最大化状态
*/
minimize: () => void;
/**
* 存档
*/
fileSave: (text: string, name: string, title: string, button: string, url?: string) => void;
}
export { ISimulatorAPI, IApiEmitterEvent }
+15 -30
View File
@@ -1,23 +1,11 @@
import { contextBridge, ipcRenderer } from "electron";
import { ISimulatorAPI } from "@Electron/SimulatorAPI"
import { ISimulatorAPI } from "@Electron/SimulatorAPI";
const emitterMap: Array<[key: string, value: Function[]]> = [];
const queryEmitter = (key: string) => {
let res: (typeof emitterMap)[0] | undefined;
emitterMap.forEach((item) => {
if (item[0] === key) res = item;
});
const emitterMap: { fn?: Function } = { fn: undefined };
if (res) {
if (Array.isArray(res[1])) return res[1];
res[1] = [];
return res[1];
}
else {
res = [key, []];
emitterMap.push(res);
return res[1];
const emit = (type: string, evt?: any) => {
if (emitterMap.fn) {
emitterMap.fn(type, evt);
}
}
@@ -43,22 +31,19 @@ const API: ISimulatorAPI = {
ipcRenderer.send("windows.minimize");
},
all: new Map() as any,
resetAll: () => emitterMap.splice(0),
reset: (type) => queryEmitter(type).splice(0),
on: (type, handler) => queryEmitter(type).push(handler),
off: (type, handler) => {
const handlers = queryEmitter(type);
handlers.splice(handlers.indexOf(handler!) >>> 0, 1);
fileSave(text: string, name: string, title: string, button: string, url?: string) {
ipcRenderer.send("windows.fileSave", text, name, title, button, url);
},
emit: ((type: string, evt: any) => {
queryEmitter(type).slice().map((handler: any) => { handler(evt) });
}) as any,
}
mapEmit: (fn: Function) => { emitterMap.fn = fn },
} as any;
ipcRenderer.on("windows.windowsSizeStateChange", () => {
API.emit("windowsSizeStateChange");
emit("windowsSizeStateChange");
});
ipcRenderer.on("windows.EndFileSave", (_, name: string, url: string, success: boolean) => {
emit("fileSave", {name, url, success});
});
contextBridge.exposeInMainWorld("API", API);