-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
169 lines (144 loc) · 5.22 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron')
const { initialize, enable } = require("@electron/remote/main");
const path = require('path')
const { default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = require('electron-devtools-installer');
const isDev = require('electron-is-dev');
const Store = require("electron-store");
const ipcTypes = require("./ipcTypes")
const AppWindow = require("./AppWindow/AppWindow");
let templateMenu = require("./templates");
let mainWindow, settingWindow;
const showContextMenu = (event) => {
const template = [
{
label: "打开",
click: () => {
mainWindow.webContents.send(ipcTypes.OPEN_FILE);
}
},
{
label: "重命名",
click: () => {
mainWindow.webContents.send(ipcTypes.RENAME_FILE);
}
},
{
label: "删除",
click: () => {
mainWindow.webContents.send(ipcTypes.DELETE_FILE);
}
},
];
const menu = Menu.buildFromTemplate(template);
menu.popup(BrowserWindow.fromWebContents(event.sender));
}
const createWindow = () => {
const mainWindowConfig = {
width: 1200,
height: 680,
minWidth: 1076,
minHeight: 680,
maxHeight: 680,
webPreferences: {
nodeIntegration: true,
preload: isDev ? path.join(__dirname, 'preload.js') : path.join(__dirname, "../preload.js")
},
}
// 加载 index.html
const urlLocation = isDev ? "http://localhost:3000" : `file://${path.join(__dirname, './index.html')}`;
mainWindow = new AppWindow(mainWindowConfig, urlLocation);
mainWindow.loadURL(urlLocation);
mainWindow.on("closed", () => {
mainWindow = null;
})
initialize();
enable(mainWindow.webContents);
// 打开开发工具
// mainWindow.webContents.openDevTools()
// installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS], options)
// .then((name) => console.log(`Added Extension: ${name}`))
// .catch((err) => console.log('An error occurred: ', err));
// let menu = Menu.buildFromTemplate(template);
// Menu.setApplicationMenu(menu);
// conceal menu
// // mainWindow.removeMenu();
// 加载菜单
let menu = Menu.buildFromTemplate(templateMenu)
Menu.setApplicationMenu(menu)
}
const options = {
loadExtensionOptions: { allowFileAccess: true },
}
app.whenReady().then(() => {
Store.initRenderer()
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
ipcMain.on(ipcTypes.OPEN_SETTING_WINDOW, () => {
const settingWindowConfig = {
width: 600,
height: 400,
parent: mainWindow,
webPreferences: {
nodeIntegration: true,
preload: isDev ? path.join(__dirname, 'preload.js') : path.join(__dirname, "../preload.js")
}
}
const settingsFileLocation = isDev ? `file://${path.join(__dirname, "./settings/index.html")}` : `file://${path.join(__dirname, "./settings/index.html")}`;
settingWindow = new AppWindow(settingWindowConfig, settingsFileLocation);
settingWindow.on("closed", () => {
settingWindow = null;
})
// settingWindow.webContents.openDevTools()
enable(settingWindow.webContents)
})
ipcMain.handle(ipcTypes.OPEN_LOCATION_DIALOG, () => {
return dialog.showOpenDialog({
properties: ["openDirectory"],
message: "选择文件的存储路径"
})
})
// ipc message
// get path name
ipcMain.handle(ipcTypes.GET_PATH_NAME, async (_e, title) => await app.getPath(title));
ipcMain.handle(ipcTypes.OPEN_DIALOG, (_e) => {
return dialog.showOpenDialog({
title: "选择导入的markdown",
properties: ["openFile", "multiSelections"],
filters: [
{
name: "markdown files",
extensions: ["md"],
},
],
})
});
ipcMain.handle(ipcTypes.IMPORT_MESSAGE, (_e, num) => {
return dialog.showMessageBox({
type: "info",
title: "文件导入成功",
message: `成功导入了${num}个文件`
})
});
ipcMain.handle(ipcTypes.IMPORT_ERROR, (_e) => {
return dialog.showErrorBox("导入错误", "导入错误,请查看路径或已导入文件")
})
ipcMain.handle(ipcTypes.OPEN_CONTEXT_MENU, (_e) => { return showContextMenu(_e) })
ipcMain.handle(ipcTypes.SAVE_EDIT_FILE, (_e) => {
return dialog.showMessageBox({
type: "question",
buttons: ["取消", "确定"],
title: "文件修改未保存",
message: "是否要保存正在关闭文件的修改"
})
})
ipcMain.handle(ipcTypes.RENAME_ERROR, (_e) => {
return dialog.showErrorBox("重命名错误", "重命名错误,已有同名的文件存在。在确定后再按一次Enter键重新命名")
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})