-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
261 lines (217 loc) · 8.25 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
'use strict';
const log = require('electron-log');
// Settings
const {DEVELOPMENT}=require('./settings.js');
if(DEVELOPMENT && process.platform == 'darwin') require('electron-reload')(__dirname);
// Required Modules
const {app, BrowserWindow, Menu, MenuItem, shell, ipcRenderer, protocol, ipcMain, dialog} = require('electron');
// https://github.com/electron/electron/issues/18214#issuecomment-495043193
app.commandLine.appendSwitch('disable-site-isolation-trials');
// console.log(require.resolve('electron'))
const path = require('path');
// Startup
// if(app.requestSingleInstanceLock()) {
// app.on('second-instance', (event, argv, cwd) => {
// console.log(JSON.stringify(argv));
// window.webContents.send('DOIT','message',JSON.stringify(argv));
// });
// }
// else {
// app.quit();
// }
// Global Variables
var window, menu;
// Menu
// click: function (menuItem, focusedWindow) { focusedWindow.webContents.undo(); }
function send(menuItem) {
window.webContents.send('MENU',menuItem.id);
}
menu=[
{
label: 'Document Pager',
submenu: [
// { label: `New Document`, accelerator: 'CmdOrCtrl+N', id:'NEW', click: send },
{ label: `Open …`, accelerator: 'CmdOrCtrl+O', id:'OPEN', click: send },
{ label: `Reload`, accelerator: 'CmdOrCtrl+R', id:'LOAD', click: send },
// { label: `Open URL …`, accelerator: 'CmdOrCtrl+Shift+O', id:'URL', click: send },
{ label: `Close`, accelerator: 'CmdOrCtrl+W', id:'CLOSE', click: send },
{ label: `Save`, accelerator: 'CmdOrCtrl+S', id:'SAVE', click: send },
// { label: `Save As …`, accelerator: 'CmdOrCtrl+Shift+S', id:'SAVEAS', click: send },
{ type:'separator' },
{ label: `Show Documents`, accelerator: 'CmdOrCtrl+D', id:'DOCUMENTS', click: send},
{ label: `Set as Favourite`, accelerator: 'CmdOrCtrl+Y', id:'FAVOURITE', click: send},
{ label: `Unset as Favourite`, accelerator: 'CmdOrCtrl+Shift+Y', id:'UNFAVOURITE', click: send},
{ type:'separator' },
{ label: `Print Page`, accelerator: 'CmdOrCtrl+P', id:'PRINTPAGE', click: send},
{ label: `Print Document`, accelerator: 'Shift+CmdOrCtrl+P', id:'PRINTDOCUMENT', click: send},
{ type:'separator' },
{ role: `quit`, accelerator: 'CmdOrCtrl+Q' }
]
},
{
label: 'Edit',
submenu: [
{ role: `undo`, accelerator: 'CmdOrCtrl+Z' },
{ type:'separator' },
{ role: 'copy', accelerator: 'CmdOrCtrl+C' },
{ role: 'paste', accelerator: 'CmdOrCtrl+V' },
{ role: 'selectAll', accelerator: 'CmdOrCtrl+A' },
{ type:'separator' },
{ label: 'Highlight', type: 'checkbox', checked: true, accelerator: 'CmdOrCtrl+T', id: 'HIGHLIGHT', click: (item)=>{
window.webContents.send('MENU','HIGHLIGHT',item.checked);
}
},
// { type:'separator' },
// { label: 'Find …', accelerator: 'CmdOrCtrl+F', id: 'FIND', click: send },
// { label: 'Find Again', accelerator: 'CmdOrCtrl+G', id:'FINDAGAIN', click: send },
{ type:'separator' },
{ label: 'Zoom In', accelerator: 'CmdOrCtrl+plus', id: 'ZOOM', click: ()=>{window.webContents.send('MENU','ZOOM',1);} },
{ label: 'Zoom Out', accelerator: 'CmdOrCtrl+-', id: 'ZOOM', click: ()=>{window.webContents.send('MENU','ZOOM',-1);} },
{ label: 'Reset Zoom', accelerator: 'CmdOrCtrl+0', id:'ZOOM', click: ()=>{window.webContents.send('MENU','ZOOM',0);} },
]
},
{
role: 'help',
submenu: [
{ label: 'About …', id: 'ABOUT', click: send },
{ label: 'Instructions …', id: 'INSTRUCTIONS', click: send },
{ type:'separator' },
{ label: 'Document Pager Home', icon: path.join(__dirname, 'images/external.png'), click: () => { shell.openExternal('https://github.com/manngo/document-pager'); } },
{ label: 'Internotes Pager', icon: path.join(__dirname,'images/external.png'), click: () => { shell.openExternal('https://pager.internotes.net/'); } },
{ id: 'debug-separator', type:'separator' },
{ id: 'debug-developer-tools', label: 'Show Development Tools', click: function (menuItem, focusedWindow) { window.webContents.openDevTools({mode: 'detach'}); } },
// { id: 'debug-developer-tools', label: JSON.stringify(process.argv), click: function (menuItem, focusedWindow) { window.webContents.openDevTools({mode: 'detach'}); } },
]
}
];
var developmentMenu=[{
label: 'Development',
submenu: [
{ label: 'Show Development Tools', click: function (menuItem, focusedWindow) { window.webContents.openDevTools(); } },
{ label: 'Show Development Detached', click: function (menuItem, focusedWindow) { window.webContents.openDevTools({mode: 'detach'}); } },
]
}];
// if(DEVELOPMENT) menu=menu.concat(developmentMenu);
//if(DEVELOPMENT) window.webContents.openDevTools({mode: 'detach'});
//if(process.argv.includes('debug')) window.webContents.openDevTools({mode: 'detach'});
if(process.argv.includes('debug')) menu=menu.concat(developmentMenu);
// Init
function init() {
window = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
});
// window.webContents.send('debug-data', process);
protocol.registerStringProtocol('doit',(request,callback)=>{
// console.log(request);
// console.log(callback);
var [dummy,action,data,more]=request.url.split(/:/);
window.webContents.send('DOIT',action,data,more);
},(error)=> {});
window.once('ready-to-show', () => {
window.show();
});
window.setTitle('Document Pager');
menu=Menu.buildFromTemplate(menu);
/* not working for main menuy
menu.addListener('menu-will-show',event=>{
console.log(event);
});
*/
Menu.setApplicationMenu(menu);
window.loadURL(path.join('file://', __dirname, '/index.html'));
if(DEVELOPMENT) window.webContents.openDevTools({mode: 'detach'});
// if(DEVELOPMENT) window.webContents.openDevTools();
// window.webContents.setDevToolsWebContents(devtools.webContents);
window.on('closed', function () {
window = null;
});
}
// Events
app.on('ready', init);
app.on('window-all-closed', function () {
// if (process.platform !== 'darwin')
app.quit();
});
app.on('activate', function () {
if (window === null) init();
});
// process.argv.forEach(onOpen);
app.on('open-file', onOpen);
app.on('open-url', onOpen);
function onOpen(path) {
if(!path) return;
console.log(JSON.stringify(arguments));
window.webContents.send('DOIT','open',path);
}
// Prompt
var prompt, promptResponse;
var promptOptions={
message: 'Enter a URL:',
match: /https?:\/\//,
error: 'URL must begin with http:// or https://'
};
function doPrompt(parent,callback) {
prompt=new BrowserWindow({
// width: 1400, height: 200,
width: 400,
frame: false,
parent,
show: true,
modal: true,
alwaysOnTop: true,
// title: options.title,
title: 'This space for rent …',
webPreferences : {
nodeIntegration: true,
sandbox : false,
}
});
prompt.on('closed',()=>{
prompt=null;
callback(promptResponse);
});
prompt.loadURL(`file://${path.join(__dirname,'content/prompt.html')}`);
prompt.once('read-to-show',()=>prompt.show());
}
ipcMain.on('prompt-ok',(event,data)=>{log.info(data);});
ipcMain.on('prompt-ok',(event,data)=>{promptResponse=data;});
ipcMain.on('prompt-cancel',(event,data)=>{promptResponse=undefined;});
ipcMain.on('prompt-size',(event,data)=>{
data=JSON.parse(data);
prompt.setBounds({
// width: data.width, height: data.height
height: parseInt(data.height+1)
});
});
ipcMain.on('prompt-init',(event,data)=>{
event.returnValue=JSON.stringify(promptOptions);
});
ipcMain.on('prompt',(event,options)=>{
promptOptions=options;
doPrompt(window,data=>event.returnValue=data);
});
// ipcMain.on('message-box',(event,data)=>{
// dialog.showMessageBoxSync(window,data);
// });
ipcMain.handle('message-box', (event, data) => {
dialog.showMessageBox(window,data);
});
ipcMain.on('open-file',(event,data)=>{
dialog.showOpenDialog(null, data).then(filePaths => {
event.sender.send('open-file-paths', filePaths);
});
});
ipcMain.on('home',(event,options)=>{
var home=`${app.getPath('home')}/.document-pager`;
event.returnValue = home;
});
ipcMain.on('init',(event,data)=>{
var home=`${app.getPath('home')}`;
event.returnValue = JSON.stringify({home});
});