-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ts
181 lines (158 loc) · 4.88 KB
/
main.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
import { app, BrowserWindow, dialog, ipcMain, Menu, protocol, shell, net } from 'electron';
import * as path from 'path';
import ContextMenu from 'electron-context-menu';
import { getDicts } from './dicts';
import windowStateKeeper from 'electron-window-state';
const IS_DEV = process.argv.slice(2).includes('--is-dev');
let window: BrowserWindow | null;
function createWindow() {
protocol.handle('file', (req) => {
let file = new URL(req.url).pathname;
return net.fetch(new URL(path.join(__dirname, 'core', file), 'file:').toString(), {
bypassCustomProtocolHandlers: true
});
});
const windowState = windowStateKeeper({
defaultWidth: 1100,
defaultHeight: 800
})
const preloadPath: string = path.join(__dirname, 'preload.js');
window = new BrowserWindow({
width: windowState.width,
height: windowState.height,
backgroundColor: '#607d8b',
autoHideMenuBar: true,
webPreferences: {
/* This may seem bad, but I'm enabling most of the things this disables afterwards.
* Electron has added a new security setting blocking file loads from within iframes, which broke
* ASCIIMaths. There isn't a manual toggle for that change, so this has to be done.
*/
webSecurity: false,
allowRunningInsecureContent: false,
nodeIntegration: false,
contextIsolation: true,
spellcheck: false, // We handle spellcheck with custom code in `preload.ts`
sandbox: false,
preload: preloadPath
}
});
windowState.manage(window);
window.webContents.session.setSpellCheckerEnabled(false);
window.webContents.setUserAgent(window.webContents.getUserAgent().replaceAll('µPad', 'MicroPad'));
ipcMain.on('initialShouldSpellCheck', (_event, shouldSpellCheck) => {
const appMenu = Menu.buildFromTemplate([
{
label: (process.platform === 'darwin') ? app.getName() : 'File',
submenu: [
{ role: 'quit' }
]
},
{
id: 'edit-menu',
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'copy' },
{ role: 'cut' },
{ role: 'paste' },
{ role: 'selectAll' },
{ role: 'delete' },
{
type: 'checkbox',
label: 'Spell Checking',
checked: shouldSpellCheck,
click: async menuItem => {
window?.webContents.send('updateShouldSpellCheck', menuItem.checked);
await dialog.showMessageBox({ message: `When you restart µPad, the spell checker will be ${menuItem.checked ? 'enabled' : 'disabled'}.` });
}
},
{ type: 'separator' }
]
},
{
label: 'View',
submenu: [
{ role: 'zoomIn' },
// https://github.com/MicroPad/MicroPad-Electron/issues/47
{ role: 'zoomIn', accelerator: 'CommandOrControl+=', visible: false },
{ role: 'zoomOut' },
{ role: 'resetZoom' }
]
},
{ role: 'windowMenu' }
]);
Menu.setApplicationMenu(appMenu);
initSpellcheck(shouldSpellCheck)
.catch(e => console.error(e));
});
window.loadURL(new URL('index.html', 'file:').toString());
window.webContents.setWindowOpenHandler(details => {
shell.openExternal(details.url);
return { action: 'deny' };
});
window.on('closed', () => quitApp());
if (IS_DEV) window.webContents.openDevTools();
}
function quitApp() {
window = null;
app.quit();
}
async function initSpellcheck(shouldSpellCheck: boolean): Promise<void> {
if (!shouldSpellCheck) {
ContextMenu({ showInspectElement: false });
return;
}
const dicts = await getDicts();
(() => {
ContextMenu({
menu: (defaultActions, params) => {
const spellSuggestions = params.misspelledWord
? Array.from(new Set([...dicts.AU.suggest(params.misspelledWord), ...dicts.US.suggest(params.misspelledWord)]))
: [];
const spellSuggestionOptions = spellSuggestions.map(w => {
return {
label: w,
click(selected) {
window?.webContents.replaceMisspelling(selected.label)
}
};
});
const learnSpelling = params.misspelledWord ? [
defaultActions.separator(),
{
label: 'Add to dictionary',
click() {
window?.webContents.send('addToUserDict', params.misspelledWord);
}
}
] : [];
return [
...spellSuggestionOptions,
...learnSpelling,
defaultActions.separator(),
defaultActions.lookUpSelection({}),
defaultActions.separator(),
defaultActions.searchWithGoogle({}),
defaultActions.cut({}),
defaultActions.copy({}),
defaultActions.paste({}),
defaultActions.separator(),
defaultActions.saveImage({}),
defaultActions.saveImageAs({}),
...(params.linkURL.startsWith('file:///') ? [] : [defaultActions.copyLink({})]),
defaultActions.copyImage({}),
];
}
});
})();
}
if (!app.requestSingleInstanceLock()) {
// Another instance of this app is already running
quitApp();
} else {
app.name = 'µPad';
app.disableHardwareAcceleration(); // This should fix https://github.com/MicroPad/Electron/issues/2
app.on('ready', createWindow);
}