This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
159 lines (135 loc) · 3.72 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
const { app, BrowserWindow } = require("electron");
let win;
const PATH_TO_APP = `${__dirname}/dist/writerey`;
function createWindow() {
app.on("window-all-closed", () => {
app.quit();
});
const isBackendRunning = startupPythonBackend();
if (!isBackendRunning) {
errorWindow = new BrowserWindow();
errorWindow.loadURL(`${PATH_TO_APP}/assets/python_error.html`);
errorWindow.on("closed", function () {
errorWindow = null;
});
return;
}
// Create the browser window.
win = new BrowserWindow({
show: false,
icon: `${PATH_TO_APP}/assets/writerey.ico`,
webPreferences: {
nodeIntegration: true,
spellcheck: true,
},
});
win.loadURL(`${PATH_TO_APP}/index.html`);
win.maximize();
win.show();
// uncomment below to open the DevTools.
// win.webContents.openDevTools();
// Event when the window is closed.
win.on("closed", function () {
shell.end();
win = null;
});
// Open clicked urls in default Browser and not a electron child instance
openUrlsInDefaultBrowser();
// Enable spellchecking
enableSpellChecking();
}
// Create window on electron intialization
app.on("ready", createWindow);
// macOS specific processes
app.on("window-all-closed", function () {
// On macOS specific close process
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", function () {
if (win === null) {
createWindow();
}
});
/**
* Startup python backend in python shell
*/
function startupPythonBackend() {
try {
let {
PythonShell,
} = require(`${PATH_TO_APP}/assets/thirdparty/python-shell`);
shell = new PythonShell(`${PATH_TO_APP}/server/app.py`, {
pythonOptions: ["-u"],
parser: function (message) {
if (!message) return;
console.log("[Py] Log", message);
},
stderrParser: function (stderr) {
if (!stderr) return;
console.error(
"[Py] [[ERR]]",
(JSON.stringify(stderr) || "").substring(0, 300)
);
},
});
shell.on("close", function (message) {
if (!message) return;
console.log("[Python] [CLOSED]", message);
});
shell.on("stdout", function (message) {
if (!message) return;
console.log("[Python] stdout", message);
});
return true;
} catch (err) {
console.error("Failed to launch python server", err);
return false;
}
}
/**
* Opens URLs in the default browser instead of the electron instance
*/
function openUrlsInDefaultBrowser() {
const handleRedirect = (e, url) => {
if (url != win.webContents.getURL()) {
e.preventDefault();
require("electron").shell.openExternal(url);
}
};
win.webContents.on("will-navigate", handleRedirect);
win.webContents.on("new-window", handleRedirect);
}
/**
* Enables chrome integrated spell checking and shows corrections in context menu
*/
function enableSpellChecking() {
const { Menu, MenuItem } = require("electron");
win.webContents.session.setSpellCheckerLanguages(["en-US", app.getLocale()]);
win.webContents.on("context-menu", (event, params) => {
const menu = new Menu();
// Add each spelling suggestion
for (const suggestion of params.dictionarySuggestions) {
menu.append(
new MenuItem({
label: suggestion,
click: () => win.webContents.replaceMisspelling(suggestion),
})
);
}
// Allow users to add the misspelled word to the dictionary
if (params.misspelledWord) {
menu.append(
new MenuItem({
label: "Add to dictionary",
click: () =>
win.webContents.session.addWordToSpellCheckerDictionary(
params.misspelledWord
),
})
);
}
menu.popup();
});
}