-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
100 lines (78 loc) · 2.12 KB
/
main.js
File metadata and controls
100 lines (78 loc) · 2.12 KB
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
const { app, BrowserWindow, Menu, Tray, ipcMain, dialog } = require('electron');
const ipc = require('ipc');
const express = require('express')();
const fs = require('fs');
const url = require('url');
const path = require('path');
const menuTeplate = require('./application/menu');
let currentFile;
let mainWindow;
let addLoaderWindow;
let willQuitApp = false;
const actions = {
openFile: () => {
const file = dialog.showOpenDialog({properties: ['openFile' ]})
fs.readFile(file[0], 'utf8', (err, content) => {
if(err) return;
mainWindow.webContents.send('content', content, file);
})
},
new: () => {
currentFile = undefined;
mainWindow.webContents.send('content', '', currentFile);
},
saveCurrent: () => {
mainWindow.webContents.send('getContentForSave');
}
}
const createWindow = (props, window) => {
window = new BrowserWindow(props);
window.loadURL(url.format({
pathname: path.join(__dirname, 'windows', 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
};
app.on('ready', () => {
mainWindow = new BrowserWindow({
'minHeight': 600,
'minWidth': 900,
'height': 600,
'width': 900,
});
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'windows', 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
mainWindow.on('close', (e) => {
if (willQuitApp) {
mainWindow = null;
} else {
e.preventDefault();
mainWindow.hide();
}
});
ipcMain.on('open', (e, file) => {
fs.readFile(file, 'utf8', (err, content) => {
if(err) return;
mainWindow.webContents.send('content', content, file);
})
});
ipcMain.on('contentForSave', (e, content) => {
if(!currentFile) {
const directory = dialog.showSaveDialog();
currentFile = directory;
fs.writeFile(directory, content);
} else {
fs.writeFile(currentFile, content);
}
mainWindow.webContents.send('updateTitle', currentFile);
})
const menu = Menu.buildFromTemplate(menuTeplate(actions));
Menu.setApplicationMenu(menu);
})
app.on('activate', () => {
mainWindow.show();
});
app.on('before-quit', () => willQuitApp = true);