-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
134 lines (115 loc) · 3.13 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
const { app, BrowserWindow, dialog, Menu, ipcMain } = require("electron");
const fs = require("fs");
ipcMain.on("open-output", (e, srcPath) => {
openOutPutWindow(srcPath);
});
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win.loadFile("index.html");
const menu = Menu.buildFromTemplate([
{
label: "File",
submenu: [
{
label: "New File",
accelerator: "CommandOrControl+N",
click: () => newFileDialog(win),
},
{
label: "Open",
accelerator: "CommandOrControl+O",
click: () => openFileDialog(win),
},
{
label: "Save",
accelerator: "CommandOrControl+S",
click: () => emitSaveFileEvent(win),
},
],
},
]);
win.setMenu(menu);
win.webContents.openDevTools();
ipcMain.on("open-save-as", (_, fileContent) => {
const filePath = dialog.showSaveDialogSync({ title: "Save new file as" });
if (filePath !== undefined) {
fs.writeFileSync(filePath, fileContent);
win.webContents.send("new-file-created", filePath);
}
});
}
app.whenReady().then(() => {
createWindow();
// macOS specific behaviour
// can be removed because not going to support macOS
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
// windows and linux users expect to close app when all windows are closed
if (process.platform !== "darwin") {
app.quit();
}
});
let outputWin = null;
function openOutPutWindow(srcPath) {
const editorWin = BrowserWindow.getFocusedWindow();
if (editorWin === null) {
console.log("Unexpected error: mainWin is null");
app.quit();
return;
}
let { x, y } = editorWin.getBounds();
outputWin = new BrowserWindow({
x: x + 10,
y: y + 10,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
outputWin.title = srcPath;
outputWin.loadFile("output.html");
outputWin.once("ready-to-show", () => {
if (outputWin === null) {
console.log("Unexpected error: ouputWin is null");
app.quit();
} else {
outputWin.show();
ipcMain.send;
outputWin.webContents.send("spawn", srcPath);
}
});
outputWin.on("closed", () => {
outputWin = null;
});
}
function newFileDialog(browserWin) {
// no new file is created on disk
// empty fileContect and empty filePath should be assumed
// as a newly created unnamed file
browserWin.webContents.send("open-file", "", "");
}
function openFileDialog(browserWin) {
let openedFiles = dialog.showOpenDialogSync(browserWin, {
properties: ["openFile"],
});
const filePath = openedFiles[0];
const fileContent = fs.readFileSync(filePath, { encoding: "utf8" });
browserWin.webContents.send("open-file", fileContent, filePath);
}
// Sends save-file event to renderer process
function emitSaveFileEvent(browserWin) {
browserWin.webContents.send("save-file");
}