-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (88 loc) · 2.56 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
'use strict';
const {
app,
shell,
Menu,
globalShortcut,
ipcMain,
clipboard,
BrowserWindow
} = require("electron");
const path = require("path");
const windowStateKeeper = require('electron-window-state');
const defaultMenu = require('electron-default-menu');
const argv = require('minimist')(process.argv.slice(2));
const qs = require("querystring");
let openedFilePath;
app.once('open-file', function (event, filePath) {
openedFilePath = filePath;
});
const openURL = (URL) => {
if (/^https?:/.test(URL)) {
return shell.openExternal(URL, {
activate: true
});
}
};
let mainWindow = null;
app.on('ready', function () {
const mainWindowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 800,
webPreferences: {
nodeIntegration: false,
webSecurity: false,
}
});
mainWindow = new BrowserWindow({
'x': mainWindowState.x,
'y': mainWindowState.y,
'width': mainWindowState.width,
'height': mainWindowState.height,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webSecurity: false,
}
});
mainWindow.webContents.on('new-window', function (e) {
openURL(e.url);
});
const openHTML = (filePath) => {
const query = filePath ? qs.stringify({
file: filePath
}) : "";
return mainWindow.loadURL('file://' + __dirname + '/public/index.html?' + query);
};
if (argv._ && argv._.length > 0) {
const filePath = path.resolve(process.cwd(), argv._[0]);
openHTML(filePath);
} else if (argv.file) {
const filePath = path.resolve(process.cwd(), argv.file);
openHTML(filePath);
} else if (openedFilePath) {
openHTML(openedFilePath);
} else {
openHTML();
}
app.on('open-file', function (event, filePath) {
event.preventDefault();
openHTML(filePath);
});
if (process.env.NODE_ENV === "development") {
mainWindow.webContents.openDevTools();
}
// Get template for default menu
const menu = defaultMenu(app, shell);
// Set top-level application menu, using modified template
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
app.on('window-all-closed', function () {
app.quit();
});
ipcMain.on("reply-copy", (event, arg) => {
clipboard.writeHTML(arg);
});
globalShortcut.register('CommandOrControl+Shift+C', () => {
mainWindow.webContents.send('to-copy');
});
});