-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (52 loc) · 1.7 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
const electron = require('electron');
const url = require('url');
const path = require('path');
const fs = require("fs");
const { app, BrowserWindow, Menu, ipcMain, dialog } = electron;
let appWindow;
let file_path;
const appMenuTemplate = [
{
label: 'File',
submenu: [
{
label: 'Open',
accelerator:process.platform == 'darwin' ? 'Command+O' : 'Ctrl+O',
click() {
file_path = dialog.showOpenDialog({ filters: [{ name: "Text File", extensions: 'txt' }] }, { properties: ['openFile', 'openDirectory'] });
fs.readFile(file_path[0], "utf8", function (error, data) {
if (error) throw error;
appWindow.webContents.send('file:load', data);
});
}
},
{
label: 'Save',
accelerator:process.platform == 'darwin' ? 'Command+S' : 'Ctrl+S',
click() {
appWindow.webContents.send('file:save');
}
},
{
label: 'Quit',
click() {
app.quit();
}
}
]
}
];
app.on('ready', () => {
appWindow = new BrowserWindow({});
appWindow.loadURL(url.format({
pathname: path.join(__dirname, 'appWindow.html'),
protocol: "file:",
slashes: true
}))
const mainMenu = Menu.buildFromTemplate(appMenuTemplate);
Menu.setApplicationMenu(mainMenu);
appWindow.on('close', () => app.quit());
})
ipcMain.on("save", (e, data) => {
fs.writeFileSync(file_path[0], data.split("\n").join('\r\n'));
})