-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
75 lines (55 loc) · 2.37 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { contextBridge, ipcRenderer } = require("electron");
const { join, dirname, basename, extname } = require("path");
const fs = require("node:fs/promises");
const Store = require("electron-store");
const filesStore = new Store({ name: "FilesData" })
const settingStore = new Store({ name: "SettingConfig" })
const ipcTypes = require("./ipcTypes");
const tranObjToArr = objList => Object.keys(objList).map((item) => (objList[item]))
contextBridge.exposeInMainWorld("myApp", {
readFile: (path) => {
return fs.readFile(path, { encoding: "utf-8" });
},
writeFile: (path, content) => {
return fs.writeFile(path, content, { encoding: "utf-8" })
},
renameFile: (path, newPath) => {
return fs.rename(path, newPath);
},
deleteFile: (path) => {
return fs.unlink(path);
},
joinPath: (path, title) => join(path, `${title}.md`),
dirPath: (path) => dirname(path),
basePath: (path) => basename(path, extname(path)),
getPath: (title) => ipcRenderer.invoke(ipcTypes.GET_PATH_NAME, title),
getFilesData: () => filesStore.get("files"),
saveFilesData: (files) => {
const filesStoreObj = tranObjToArr(files).reduce((result, file) => {
const { id, path, title } = file;
result[id] = {
id,
path,
title
};
return result;
}, {});
filesStore.set("files", filesStoreObj);
return;
},
showImportDialog: () => ipcRenderer.invoke(ipcTypes.OPEN_DIALOG),
showMessageBox: (num) => ipcRenderer.invoke(ipcTypes.IMPORT_MESSAGE, num),
showErrorBox: () => ipcRenderer.invoke(ipcTypes.IMPORT_ERROR),
openSettingWindowIPC: () => ipcRenderer.send(ipcTypes.OPEN_SETTING_WINDOW),
listenIPC: (key, cb) => ipcRenderer.on(key, cb),
removeListenIPC: (key, cb) => ipcRenderer.removeListener(key, cb),
openContextDialog: () => ipcRenderer.invoke(ipcTypes.OPEN_CONTEXT_MENU),
// self saved location
getSavedPath: () => settingStore.get("savedLocation"),
// setting window
openSettingDialog: () => ipcRenderer.invoke(ipcTypes.OPEN_LOCATION_DIALOG),
saveSettingPath: (path) => settingStore.set("savedLocation", path),
// more details
showSaveBox: () => ipcRenderer.invoke(ipcTypes.SAVE_EDIT_FILE),
showRenameErrorBox: () => ipcRenderer.invoke(ipcTypes.RENAME_ERROR),
})