forked from Laverna/laverna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron.js
354 lines (297 loc) · 9.4 KB
/
electron.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
const electron = require('electron'),
windowStateKeeper = require('electron-window-state'),
path = require('path'),
openUrl = require('open');
const {app, BrowserWindow, Menu, Tray, protocol} = electron;
let argv = require('minimist')(process.argv.slice(1)),
contextMenu = require('electron-context-menu')({}),
win = null,
appHelper;
// Show command line help
if (argv.help || argv.h) {
console.log('Usage: laverna [options]\r\n');
console.log('--help', 'Show this help');
console.log('--dev ', 'Show developer tools automatically on start');
console.log('--tray', 'Hide to tray on start');
console.log(
'--data-dir',
'Directory where data is stored.',
'Example: laverna --data-dir=../data'
);
return app.quit();
}
// Allow to change the directory where the data is stored
if (argv['data-dir'] && argv['data-dir'].trim()) {
let dataDir = argv['data-dir'].trim();
// The same or parent directory
if (dataDir.search(/^\./) > - 1) {
// Always store data in a separate directory
dataDir += (dataDir.search(/^\.{1,2}$/) > -1 ? '/laverna-data' : '');
dataDir = path.join(__dirname, dataDir);
}
app.setPath('userData', path.normalize(dataDir));
}
appHelper = {
// The page which will be loaded in the window
page: 'http://localhost:9000/',
// Icon of the app
icon: path.join(__dirname, '/dist/images/icon/',
process.platform === 'darwin' ? 'IconMenubarTemplate.png' : 'icon-120x120.png'
),
// The main menu
menu: [
{
label : '&File',
submenu : [
{
label : 'Quit',
accelerator : 'CmdOrCtrl+Q',
click : function() {
app.quit();
}
}
]
},
{
label: '&Edit',
submenu: [
{
label : 'Undo',
accelerator : 'CmdOrCtrl+Z',
role : 'undo'
},
{
label : 'Redo',
accelerator : 'Shift+CmdOrCtrl+Z',
role : 'redo'
},
{type: 'separator'},
{
label : 'Cut',
accelerator : 'CmdOrCtrl+X',
role : 'cut'
},
{
label : 'Copy',
accelerator : 'CmdOrCtrl+C',
role : 'copy'
},
{
label : 'Paste',
accelerator : 'CmdOrCtrl+V',
role : 'paste'
},
{type: 'separator'},
{
label : 'Select All',
accelerator : 'CmdOrCtrl+A',
role : 'selectall'
},
]
},
{
label: '&View',
submenu: [
{
label : 'Reload',
accelerator : 'CmdOrCtrl+R',
click : function(item, focusedWindow) {
if (focusedWindow) { focusedWindow.reload(); }
}
},
{
label : 'Toggle Developer Tools',
accelerator : process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click : function(item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
}
}
]
},
],
// Tray menu (shown on right click)
menuTray: [
{
label: 'Quit',
click: function() {
app.quit();
}
}
],
/**
* Electron is ready.
*/
onReady: function() {
this
.startServer()
// .interceptProtocol()
.createWindow()
.createMenu()
.createTray()
.registerEvents();
},
startServer() {
require('./server')(9000);
return this;
},
/**
* Override HTTP protocol. The reason:
* In order to make oAuth authentifications to Dropbox/RemoteStorage,
* we need to serve the app from https? protocol and have relative paths.
*/
interceptProtocol: function() {
protocol.interceptHttpProtocol('http', function(req, callback) {
if (req.url.search(appHelper.page) > - 1) {
// Remove the domain, path, and hash location
req.url = req.url.replace(appHelper.page, '');
req.url = req.url.split('#')[0] || 'index.html';
// Serve the resource from the file system
req.url = 'file:///' + path.normalize(__dirname + '/dist/' + req.url);
}
callback(req);
});
return this;
},
/**
* Create the main window.
*/
createWindow: function() {
// Recover window state (width, height, and x&y position)
this.state = windowStateKeeper('main', {
width : 1000,
height : 600
});
// Create new browser window
win = new BrowserWindow({
width : this.state.width,
height : this.state.height,
x : this.state.x,
y : this.state.y,
title : 'Laverna',
icon : this.icon,
autoHideMenuBar : true,
backgroundColor : '#00a693',
webPreferences : {
nodeIntegration : true,
preload : path.resolve(path.join(__dirname, 'preload.js')),
},
});
if (this.state.isMaximized) {
win.maximize();
}
// Load the app
win.loadURL(this.page);
// Show development tools
if (process.env.NODE_ENV === 'dev' || argv.dev) {
win.webContents.openDevTools();
}
return this;
},
/**
* Create the main menu.
*/
createMenu: function() {
// Slightly different menu on OS X
if (process.platform === 'darwin') {
this.menu[0] = {
label : 'Laverna',
submenu : this.menu[0].submenu
};
}
let menu = Menu.buildFromTemplate(this.menu);
Menu.setApplicationMenu(menu);
return this;
},
/**
* Create tray icon.
*/
createTray: function() {
let icon = new Tray(this.icon),
menu = Menu.buildFromTemplate(this.menuTray);
icon.setToolTip('Laverna');
icon.setContextMenu(menu);
// Auto hide to tray on start
if (argv.tray) {
win.hide();
}
// Hide the window into tray on click
icon.on('click', function() {
if (win.isVisible()) {
return win.hide();
}
win.show();
});
return this;
},
/**
* Listen to window events.
*/
registerEvents: function() {
// Save window state (width, height, and x&y position)
win.on('close', function() {
this.state.saveState(win);
}.bind(appHelper));
win.on('closed', function() {
win = null;
});
win.webContents.on('will-navigate', appHelper.onNavigate.bind(appHelper));
win.webContents.on('new-window', appHelper.onNavigate.bind(appHelper));
// Disable nodeIntegration
// win.webContents.on('new-window', appHelper.onNewWindow.bind(appHelper));
},
/**
* Open URLs in an external browser.
*/
onNavigate: function(e, url) {
if (url.search(this.page) === -1 &&
url.search(/(oauth|sign_in)/) === -1 &&
url.search(/^blob:/) === -1) {
e.preventDefault();
return openUrl(url);
}
},
/**
* Disable nodeIntegration on all pages except for app's.
*/
onNewWindow: function(e, url) {
if (url.search(this.page) === -1 &&
url.search(/^blob:/) === -1) {
e.preventDefault();
let extWin = new BrowserWindow({
width : 600,
height : 600,
autoHideMenuBar : true,
webPreferences : {
nodeIntegration : false,
},
});
// Load the app
extWin.loadURL(url);
extWin.on('closed', function() { extWin = null; });
}
},
};
// Create browser window when Electron is ready
app.on('ready', appHelper.onReady.bind(appHelper));
// Quit when all windows are closed.
app.on('window-all-closed', function() {
/*
* On OS X it is common for applications and their menu bar
* to stay active until the user quits explicitly with Cmd + Q
*/
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function() {
/*
* On OS X it's common to re-create a window in the app when the
* dock icon is clicked and there are no other windows open.
*/
if (win === null) {
appHelper.onReady();
}
});