-
-
Notifications
You must be signed in to change notification settings - Fork 245
/
index.js
130 lines (100 loc) · 2.92 KB
/
index.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
'use strict';
const {app, BrowserWindow, ipcMain, Menu, shell} = require('electron');
const fs = require('fs');
const {is, formatTitle, formatURL, formatYinxiangURL, readSheet} = require('./src/util');
const file = require('./src/file');
const menu = require('./src/menu');
const pdf = require('./src/pdf');
const settings = require('./src/settings');
const shortcut = require('./src/keymap');
const time = require('./src/time');
const tray = require('./src/tray');
const update = require('./src/update');
const url = require('./src/url');
const win = require('./src/win');
const {log} = console;
require('electron-debug')({enabled: true});
require('electron-dl')();
require('electron-context-menu')();
let exiting = false;
let mainWindow;
if (!app.requestSingleInstanceLock()) {
app.quit();
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
function createMainWindow() {
const lastURL = settings.get('useYinxiang') ? url.yinxiang : url.evernote;
const tuskWindow = new BrowserWindow(win.defaultOpts);
tuskWindow.loadURL(lastURL);
tuskWindow.on('close', e => {
if (!exiting) {
e.preventDefault();
if (is.darwin) {
app.hide();
} else {
tuskWindow.hide();
}
}
});
tuskWindow.on('page-title-updated', e => {
e.preventDefault();
});
tuskWindow.on('unresponsive', log);
tuskWindow.webContents.on('did-navigate-in-page', (_, url) => {
settings.set('lastURL', url);
});
return tuskWindow;
}
app.on('ready', () => {
Menu.setApplicationMenu(menu);
mainWindow = createMainWindow();
if (settings.get('useGlobalShortcuts')) {
shortcut.registerGlobal();
}
if (!settings.get('hideTray')) {
tray.create();
}
const {webContents} = mainWindow;
webContents.on('dom-ready', () => {
const stylesheets = fs.readdirSync(file.style);
stylesheets.forEach(x => webContents.insertCSS(readSheet(x)));
if (settings.get('launchMinimized')) {
mainWindow.minimize();
} else {
mainWindow.show();
}
});
webContents.on('new-window', (e, url) => {
e.preventDefault();
url = settings.get('useYinxiang') ? formatYinxiangURL(url) : formatURL(url);
if (is.downloadURL(url)) {
webContents.downloadURL(url);
} else {
shell.openExternal(url);
}
});
webContents.on('crashed', log);
if (!settings.get('disableAutoUpdateCheck')) {
setInterval(() => update.auto(), time.ms(settings.get('updateCheckPeriod')));
}
});
ipcMain.on('print-to-pdf', pdf.print);
ipcMain.on('export-as-pdf', e => {
const {webContents} = mainWindow;
pdf.save(e, formatTitle(webContents.getTitle()));
});
process.on('uncaughtException', log);
app.on('activate', () => mainWindow.show());
app.on('before-quit', () => {
exiting = true;
if (!mainWindow.isFullScreen()) {
settings.set('lastWindowState', mainWindow.getBounds());
}
});