1
+
2
+ const electron = require ( 'electron' ) ;
3
+
1
4
/*
2
5
@description : main.js is what controls the lifecycle of the electron application from initialization to termination.
3
- @actions : codes for Github Oauth has been commented out because of lack of functionality.
6
+ @actions : codes for Github Oauth has been commented out because of lack of functionality.
4
7
*/
5
8
require ( 'dotenv' ) . config ( ) ;
6
9
const path = require ( 'path' ) ;
@@ -13,14 +16,16 @@ const {
13
16
ipcMain,
14
17
} = require ( 'electron' ) ;
15
18
19
+
20
+
16
21
// The splash screen is what appears while the app is loading
17
22
const { initSplashScreen, OfficeTemplate } = require ( 'electron-splashscreen' ) ;
18
23
const { resolve } = require ( 'app-root-path' ) ;
19
24
20
25
// to install react dev tool extension
21
26
const {
22
27
default : installExtension ,
23
- REACT_DEVELOPER_TOOLS
28
+ REACT_DEVELOPER_TOOLS ,
24
29
} = require ( 'electron-devtools-installer' ) ;
25
30
const debug = require ( 'electron-debug' ) ;
26
31
@@ -30,8 +35,7 @@ const Protocol = require('./protocol');
30
35
const MenuBuilder = require ( './menu' ) ;
31
36
32
37
// mode that the app is running in
33
- const isDev =
34
- process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
38
+ const isDev = process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
35
39
const port = 8080 ;
36
40
const selfHost = `http://localhost:${ port } ` ;
37
41
@@ -56,11 +60,13 @@ async function createWindow() {
56
60
// Create the browser window.
57
61
win = new BrowserWindow ( {
58
62
// full screen
59
- width : 1920 ,
60
- height : 1080 ,
63
+ // width: 1920,
64
+ // height: 1080,
65
+ width : 1024 ,
66
+ height : 576 ,
61
67
minWidth : 980 ,
62
68
// window title
63
- title : ` ReacType` ,
69
+ title : ' ReacType' ,
64
70
// the browser window will not display initially as it's loading
65
71
// once the browser window renders, a function is called below that hides the splash screen and displays the browser window
66
72
show : false ,
@@ -83,8 +89,8 @@ async function createWindow() {
83
89
enableRemoteModule : false ,
84
90
// path of preload script. preload is how the renderer page will have access to electron functionality
85
91
preload : path . join ( __dirname , 'preload.js' ) ,
86
- nativeWindowOpen : true
87
- }
92
+ nativeWindowOpen : true ,
93
+ } ,
88
94
} ) ;
89
95
90
96
// Splash screen that appears while loading
@@ -99,7 +105,7 @@ async function createWindow() {
99
105
logo : resolve ( 'app/src/public/icons/png/64x64.png' ) ,
100
106
color : '#3BBCAF' ,
101
107
website : 'www.reactype.io' ,
102
- text : 'Initializing ...'
108
+ text : 'Initializing ...' ,
103
109
} ) ;
104
110
105
111
// Load app
@@ -122,7 +128,7 @@ async function createWindow() {
122
128
if ( isDev ) {
123
129
win . webContents . once ( 'dom-ready' , ( ) => {
124
130
debug ( ) ;
125
- // win.webContents.openDevTools();
131
+ win . webContents . openDevTools ( ) ;
126
132
} ) ;
127
133
}
128
134
@@ -147,13 +153,13 @@ async function createWindow() {
147
153
ses
148
154
. fromPartition ( partition )
149
155
. setPermissionRequestHandler ( ( webContents , permission , callback ) => {
150
- let allowedPermissions = [ ] ; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest
156
+ const allowedPermissions = [ ] ; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest
151
157
152
158
if ( allowedPermissions . includes ( permission ) ) {
153
159
callback ( true ) ; // Approve permission request
154
160
} else {
155
161
console . error (
156
- `The application tried to request permission for '${ permission } '. This permission was not whitelisted and has been blocked.`
162
+ `The application tried to request permission for '${ permission } '. This permission was not whitelisted and has been blocked.` ,
157
163
) ;
158
164
159
165
callback ( false ) ; // Deny
@@ -165,10 +171,10 @@ async function createWindow() {
165
171
// we could use this over _all_ urls
166
172
ses
167
173
. fromPartition ( partition )
168
- . webRequest . onBeforeRequest ( { urls : [ 'http://localhost./*' ] } , listener => {
174
+ . webRequest . onBeforeRequest ( { urls : [ 'http://localhost./*' ] } , ( listener ) => {
169
175
if ( listener . url . indexOf ( 'http://' ) >= 0 ) {
170
176
listener . callback ( {
171
- cancel : true
177
+ cancel : true ,
172
178
} ) ;
173
179
}
174
180
} ) ;
@@ -185,16 +191,27 @@ protocol.registerSchemesAsPrivileged([
185
191
standard : true ,
186
192
secure : true ,
187
193
allowServiceWorkers : true ,
188
- supportFetchAPI : true
189
- }
190
- }
194
+ supportFetchAPI : true ,
195
+ } ,
196
+ } ,
191
197
] ) ;
192
198
193
199
// This method will be called when Electron has finished
194
200
// initialization and is ready to create browser windows.
195
201
// Some APIs can only be used after this event occurs.
196
202
app . on ( 'ready' , createWindow ) ;
197
203
204
+ // TRYING ELECTRON-WINDOW-MANAGER When the application is ready
205
+
206
+ // app.on('ready', () => {
207
+ // windowManager.setDefaultSetup({'width': 800, 'height': 450, 'position': 'right'});
208
+ // // Open Dashboard window
209
+ // const dashboard = windowManager.open('home', 'Welcome ...', 'http://localhost:5000/home.html');
210
+ // dashboard.toggleDevTools(false);
211
+ // // Load ReacType window
212
+ // createWindow();
213
+ // });
214
+
198
215
// Quit when all windows are closed.
199
216
app . on ( 'window-all-closed' , ( ) => {
200
217
app . quit ( ) ;
@@ -223,16 +240,15 @@ app.on('web-contents-created', (event, contents) => {
223
240
'https://www.facebook.com' ,
224
241
'https://developer.mozilla.org' ,
225
242
'https://www.smashingmagazine.com' ,
226
- 'https://www.html5rocks.com'
243
+ 'https://www.html5rocks.com' ,
227
244
] ;
228
245
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
229
246
if ( ! validOrigins . includes ( parsedUrl . origin ) ) {
230
247
console . error (
231
- `The application tried to navigate to the following address: '${ parsedUrl } '. This origin is not whitelisted attempt to navigate was blocked.`
248
+ `The application tried to navigate to the following address: '${ parsedUrl } '. This origin is not whitelisted attempt to navigate was blocked.` ,
232
249
) ;
233
250
// if the requested URL is not in the whitelisted array, then don't navigate there
234
251
event . preventDefault ( ) ;
235
- return ;
236
252
}
237
253
} ) ;
238
254
@@ -247,20 +263,19 @@ app.on('web-contents-created', (event, contents) => {
247
263
'https://developer.mozilla.org' ,
248
264
'https://www.facebook.com' ,
249
265
'https://www.smashingmagazine.com' ,
250
- 'https://www.html5rocks.com'
266
+ 'https://www.html5rocks.com' ,
251
267
] ;
252
268
253
269
// Log and prevent the app from redirecting to a new page
254
270
if (
255
- ! validOrigins . includes ( parsedUrl . origin ) &&
256
- ! validOrigins . includes ( parsedUrl . href )
271
+ ! validOrigins . includes ( parsedUrl . origin )
272
+ && ! validOrigins . includes ( parsedUrl . href )
257
273
) {
258
274
console . error (
259
- `The application tried to redirect to the following address: '${ navigationUrl } '. This attempt was blocked.`
275
+ `The application tried to redirect to the following address: '${ navigationUrl } '. This attempt was blocked.` ,
260
276
) ;
261
277
262
278
event . preventDefault ( ) ;
263
- return ;
264
279
}
265
280
} ) ;
266
281
@@ -290,16 +305,15 @@ app.on('web-contents-created', (event, contents) => {
290
305
'https://github.com' ,
291
306
'https://www.facebook.com' ,
292
307
'https://www.smashingmagazine.com' ,
293
- 'https://www.html5rocks.com'
308
+ 'https://www.html5rocks.com' ,
294
309
] ;
295
310
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
296
311
if ( ! validOrigins . includes ( parsedUrl . origin ) ) {
297
312
console . error (
298
- `The application tried to open a new window at the following address: '${ navigationUrl } '. This attempt was blocked.`
313
+ `The application tried to open a new window at the following address: '${ navigationUrl } '. This attempt was blocked.` ,
299
314
) ;
300
315
// if the requested URL is not in the whitelisted array, then don't navigate there
301
316
event . preventDefault ( ) ;
302
- return ;
303
317
}
304
318
} ) ;
305
319
} ) ;
@@ -331,15 +345,15 @@ app.on('remote-get-current-web-contents', (event, webContents) => {
331
345
// When a user selects "Export project", a function (chooseAppDir loaded via preload.js)
332
346
// is triggered that sends a "choose_app_dir" message to the main process
333
347
// when the "choose_app_dir" message is received it triggers this event listener
334
- ipcMain . on ( 'choose_app_dir' , event => {
348
+ ipcMain . on ( 'choose_app_dir' , ( event ) => {
335
349
// dialog displays the native system's dialogue for selecting files
336
350
// once a directory is chosen send a message back to the renderer with the path of the directory
337
351
dialog
338
352
. showOpenDialog ( win , {
339
353
properties : [ 'openDirectory' ] ,
340
- buttonLabel : 'Export'
354
+ buttonLabel : 'Export' ,
341
355
} )
342
- . then ( directory => {
356
+ . then ( ( directory ) => {
343
357
if ( ! directory ) return ;
344
358
event . sender . send ( 'app_dir_selected' , directory . filePaths [ 0 ] ) ;
345
359
} )
0 commit comments