1- const { app, Menu, BrowserWindow} = require ( 'electron' ) ;
2- const path = require ( 'path' ) ;
3- const { createWindow } = require ( './window' ) ;
1+ const { ipcMain, ipcRenderer } = require ( 'electron' ) ;
2+ const consumer = require ( './ipc/consumer' ) ;
3+
4+ const Menu = require ( 'electron' ) . Menu || require ( 'electron' ) . remote . Menu ;
5+ const path = require ( 'path' ) || require ( 'electron' ) . remote . require ( 'path' ) ;
6+ const os = require ( 'os' ) || require ( 'electron' ) . remote . require ( 'os' ) ;
7+
8+ const isMainProcess = typeof ipcMain !== 'undefined' ;
9+
10+ function exec ( commandId , args = { } ) {
11+ if ( isMainProcess ) {
12+ consumer ( commandId , args ) ;
13+ } else {
14+ ipcRenderer . send ( 'main:command' , { commandId, args } ) ;
15+ }
16+ }
417
518const template = [
619 {
@@ -10,14 +23,14 @@ const template = [
1023 label : 'New File' ,
1124 accelerator : 'CmdOrCtrl+N' ,
1225 click ( ) {
13- createWindow ( { url : `file://${ path . join ( __dirname , 'index.html?target=https://hackmd.io/new' ) } ` } ) ;
26+ exec ( 'createWindow' , { url : `file://${ path . join ( __dirname , 'index.html?target=https://hackmd.io/new' ) } ` } )
1427 }
1528 } ,
1629 {
1730 label : 'New Window' ,
1831 accelerator : 'CmdOrCtrl+Shift+N' ,
1932 click ( ) {
20- createWindow ( { url : `file://${ path . join ( __dirname , 'index.html' ) } ` } ) ;
33+ exec ( 'createWindow' , { url : `file://${ path . join ( __dirname , 'index.html' ) } ` } )
2134 }
2235 } ,
2336 {
@@ -70,8 +83,7 @@ const template = [
7083 label : 'Refresh' ,
7184 accelerator : 'CmdOrCtrl+R' ,
7285 click ( ) {
73- const win = BrowserWindow . getFocusedWindow ( ) ;
74- win . webContents . send ( 'web:refresh' ) ;
86+ exec ( 'refreshWindow' ) ;
7587 }
7688 } ,
7789 {
@@ -90,13 +102,15 @@ const template = [
90102 submenu : [
91103 {
92104 label : 'Learn More' ,
93- click ( ) { require ( 'electron' ) . shell . openExternal ( 'https://hackmd.io' ) }
105+ click ( ) {
106+ exec ( 'learnMore' ) ;
107+ }
94108 }
95109 ]
96110 }
97111]
98112
99- if ( process . platform === 'darwin' ) {
113+ if ( os . platform ( ) === 'darwin' ) {
100114 template . unshift ( {
101115 label : app . getName ( ) ,
102116 submenu : [
0 commit comments