-
Notifications
You must be signed in to change notification settings - Fork 280
/
main.js
518 lines (407 loc) · 13.7 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
const electron = require('electron');
const fspath = require('path');
const url = require('url');
const {DiskDatastore} = require("./web/js/datastore/DiskDatastore");
const app = electron.app;
const shell = electron.shell;
const Menu = electron.Menu;
const Tray = electron.Tray;
const dialog = electron.dialog;
const ipcMain = electron.ipcMain;
const crashReporter = electron.crashReporter;
const BrowserWindow = electron.BrowserWindow;
const nativeImage = require('electron').nativeImage;
const options = { extraHeaders: 'pragma: no-cache\n' }
const app_icon = nativeImage.createFromPath(fspath.join(__dirname, 'icon.png'));
const {WebserverConfig} = require("./web/js/backend/WebserverConfig");
const {Webserver} = require("./web/js/backend/Webserver");
const {FileRegistry} = require("./web/js/backend/FileRegistry");
let mainWindow, splashwindow;
let contextMenu = null;
let filepath = null;
let quitapp, URL;
// share the disk datastore with the remote.
global.diskDatastore = new DiskDatastore();
const BROWSER_WINDOW_OPTIONS = {
minWidth: 400,
minHeight: 300,
width: 1280,
height: 1024,
show: false,
// https://electronjs.org/docs/api/browser-window#new-browserwindowoptions
icon: app_icon,
webPreferences: {
// TODO:
// https://github.com/electron/electron/pull/794
//
// reconsider using nodeIntegration here as this might be a security
// issue
nodeIntegration: true,
defaultEncoding: 'UTF-8'
}
};
// enable the debugging port for chrome for now. We should probably have an
// --enable-remote-debugging command line flag that would need to be set
// because I don't want to have to keep this port open all the time.
const REMOTE_DEBUGGING_PORT = '8315';
const WEBSERVER_PORT = 8500;
const DEFAULT_HOST = "127.0.0.1";
const DEFAULT_URL = `http://${DEFAULT_HOST}:${WEBSERVER_PORT}/default.html`;
// TODO: I think we need to wait until the webserver port is available before
// continuing.
console.log("Electron app path is: " + app.getAppPath());
const webserverConfig = new WebserverConfig(app.getAppPath(), WEBSERVER_PORT);
const fileRegistry = new FileRegistry(webserverConfig);
const webserver = new Webserver(webserverConfig, fileRegistry);
webserver.start();
//const DEFAULT_URL = 'file://' + __dirname + '/default.html';
let args = parseArgs();
if (args.enableRemoteDebugging) {
console.log(`Remote debugging port enabled on port ${REMOTE_DEBUGGING_PORT}.`);
console.log(`You may connect via http://${DEFAULT_HOST}:${REMOTE_DEBUGGING_PORT}`);
app.commandLine.appendSwitch('remote-debugging-port', REMOTE_DEBUGGING_PORT);
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1');
}
// TODO: enable this again but only when we have a good receiver URL.
//crashReporter.start({ productName: 'Polar eBook Reader',
// companyName: 'Polar Contributors',
// submitURL: 'https://praharsh.xyz/projects/PDFViewer/crash',
// autoSubmit: false });
//creating menus for menu bar
const template = [{
label: 'File',
submenu: [
{
label: 'New Window',
accelerator: 'CmdOrCtrl+N',
click: cmdNewWindow
},
{
type: 'separator'
},
{
label: 'Open',
accelerator: 'CmdOrCtrl+O',
click: cmdOpen
},
{
label: 'Open in New Window',
//accelerator: 'CmdOrCtrl+O',
click: cmdOpenInNewWindow
},
// {
// label: 'Open Containing Folder',
// accelerator: 'CmdOrCtrl+F',
// click: function(item, focusedWindow) {
// if (focusedWindow && filepath)
// shell.showItemInFolder("file:///" + filepath);
// }
// },
{
type: 'separator'
},
{
label: 'Print',
accelerator: 'CmdOrCtrl+P',
click: function(item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.print();
}
},
{
label: 'Close',
accelerator: 'Shift+CmdOrCtrl+Z',
click: function(item, focusedWindow) {
if (focusedWindow) focusedWindow.loadURL(DEFAULT_URL, options);
}
},
{
type: 'separator'
},
{
label: 'Exit',
accelerator: 'Alt+F4',
role: 'close'
},
]
},
{
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
{ label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
{ type: 'separator' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectall' },
]
},
{
label: 'View',
submenu: [{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.webContents.reloadIgnoringCache();
}
},
{
label: 'Toggle Full Screen',
accelerator: (function() {
if (process.platform === 'darwin')
return 'Ctrl+Command+F';
else
return 'F11';
})(),
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
},
]
},
{
label: 'Window',
role: 'window',
submenu: [
{ label: 'Minimize', accelerator: 'CmdOrCtrl+M', role: 'minimize' },
{ label: 'Close', accelerator: 'CmdOrCtrl+W', role: 'close' },
]
},
{
label: 'Tools',
submenu: [
{ label: 'Toggle Developer Tools', click: cmdToggleDevTools },
]
},
{
label: 'Help',
role: 'help',
submenu: [{
label: 'About',
click: function(item, focusedWindow) {
dialog.showMessageBox(focusedWindow, {
type: 'info',
buttons: ['OK'],
title: 'Polar Bookshelf',
message: 'Version 1.0',
detail: '',
icon: app_icon
});
}
},
{ label: 'Learn More', click: function() { shell.openExternal('https://github.com/burtonator/polar-bookshelf'); } },
]
},
];
let menu = Menu.buildFromTemplate(template);
// Code to determine how we should handle other attempts to open more instances
//
let shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
// Someone tried to run a second instance, we should focus our window.
// I'm not sure if this is the right strategy for now.
console.log("Second instance asked to load.");
if(! handleCmdLinePDF(commandLine, true)) {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
}
});
if (shouldQuit) { app.quit(); return; }
app.on('ready', function() {
contextMenu = Menu.buildFromTemplate([
{ label: 'Minimize', type: 'radio', role: 'minimize' },
{ type: 'separator' },
{ label: 'Exit', type: 'radio', role: 'close' },
]);
//for OS-X
//if (app.dock) {
// app.dock.setIcon(app_icon);
// app.dock.setMenu(contextMenu);
//}
Menu.setApplicationMenu(menu);
// NOTE: removing the next three lines removes the colors in the toolbar.
//const appIcon = new Tray(app_icon);
//appIcon.setToolTip('Polar Bookshelf');
//appIcon.setContextMenu(contextMenu);
mainWindow = createWindow();
if(args.enableDevTools) {
mainWindow.toggleDevTools();
}
// if there is a PDF file to open, load that, otherwise, load the default URL.
handleCmdLinePDF(process.argv, false);
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
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 (mainWindow === null) { createWindow(); }
});
app.on('open-file', function() {
console.log("Open file called.");
});
/**
* Listen to messages generated in the console so that we can log them to the
* main console when --enable-console is used.
*
* https://github.com/electron/electron/blob/master/docs/api/web-contents.md#event-console-message
*
*
* Returns:
*
* event Event
* level Integer
* message String
* line Integer
* sourceId String
*
*/
function consoleListener(event, level, message, line, sourceId) {
console.log(`level=${level} ${sourceId}:${line}: ${message}`);
}
function cmdNewWindow(item, focusedWindow) {
createWindow();
}
/**
* Open a dialog box for a PDF file.
*/
async function promptPDF() {
return new Promise(function (resolve) {
dialog.showOpenDialog({
filters: [
{ name: 'PDF', extensions: ['pdf'] }
],
properties: ['openFile']
}, function(path) {
if (path) {
filepath = path;
if (path.constructor === Array) {
// TODO: we should probably support multi-file in the future
path = path[0];
}
resolve(path);
}
});
});
}
/**
* Handle a command line PDF by loading it and returning true if one was loaded.
*/
function handleCmdLinePDF(commandLine, createNewWindow) {
let fileArg = getFileArg(commandLine);
if(fileArg) {
openFileCmdline(fileArg, createNewWindow);
}
}
/**
* Load the given PDF file in the given target window.
*/
function loadPDF(path, targetWindow) {
if(!targetWindow) {
throw new Error("No target window given");
}
let fileMeta = fileRegistry.registerFile(path);
console.log("Loading PDF via HTTP server: " + JSON.stringify(fileMeta));
targetWindow.loadURL(`http://${DEFAULT_HOST}:${WEBSERVER_PORT}/pdfviewer/web/viewer.html?file=` + encodeURIComponent(fileMeta.url), options);
if(args.enableConsoleLogging) {
console.log("Console logging enabled.");
targetWindow.webContents.on("console-message", consoleListener);
}
targetWindow.webContents.on('did-finish-load', function() {
console.log("Finished loading. Now injecting customizations.");
console.log("Toggling dev tools...");
//targetWindow.toggleDevTools();
});
}
function cmdToggleDevTools(item, focusedWindow) {
focusedWindow.toggleDevTools();
}
/**
* Load a PDF file when given a full URL. May be file, http, or https URL.
*/
async function cmdOpen(item, focusedWindow) {
let targetWindow = focusedWindow;
let path = await promptPDF();
loadPDF(path, targetWindow);
}
async function cmdOpenInNewWindow(item, focusedWindow) {
let path = await promptPDF();
let targetWindow = createWindow();
loadPDF(path, targetWindow);
}
/**
* The user asked to open a file from the command line.
*
* @return {Promise<void>}
*/
function openFileCmdline(path, createNewWindow) {
console.log("Opening file given on the command line: " + path);
if(createNewWindow) {
loadPDF(path, createWindow());
} else {
loadPDF(path, mainWindow);
}
}
/**
* Get the file name from the command line, if any.
*
* @param cmdline
*/
function getFileArg(cmdline) {
let fileArg = cmdline[cmdline.length - 1];
if (fileArg && fileArg.endsWith(".pdf")) {
return fileArg;
}
return null;
}
/**
* Process app command line args and return an object to work with them
* directly.
*/
function parseArgs() {
return {
enableConsoleLogging: process.argv.includes("--enable-console-logging"),
enableRemoteDebugging: process.argv.includes("--enable-remote-debugging"),
enableDevTools: process.argv.includes("--enable-dev-tools")
};
}
function createWindow() {
// Create the browser window.
let newWindow = new BrowserWindow(BROWSER_WINDOW_OPTIONS);
newWindow.on('close', function(e) {
e.preventDefault();
newWindow.webContents.clearHistory();
newWindow.webContents.session.clearCache(function() {
newWindow.destroy();
});
});
newWindow.on('closed', function() {
if(BrowserWindow.getAllWindows().length === 0) {
// determine if we need to quit:
console.log("No windows left. Quitting app.");
app.quit();
}
});
newWindow.webContents.on('new-window', function(e, url) {
e.preventDefault();
shell.openExternal(url);
});
// TODO: we need SANE handling of dev tools. Having it forced on us isn't fun.
// newWindow.webContents.on('devtools-opened', function(e) {
// e.preventDefault();
// this.closeDevTools();
// });
newWindow.webContents.on('will-navigate', function(e, url) {
e.preventDefault();
shell.openExternal(url);
});
newWindow.loadURL(DEFAULT_URL, options);
newWindow.once('ready-to-show', () => {
//newWindow.maximize();
newWindow.show();
});
return newWindow;
}