|
| 1 | +const { app, BrowserWindow, Menu, shell, dialog, ipcMain } = require('electron'); |
| 2 | + |
| 3 | +const isDev = true; |
| 4 | +// const isDev = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'; |
| 5 | + |
| 6 | +// Keep a global reference of the window object, if you don't, the window will |
| 7 | +// be closed automatically when the JavaScript object is garbage collected. |
| 8 | +let mainWindow; |
| 9 | + |
| 10 | +// Open image file |
| 11 | +function openFile() { |
| 12 | + // Opens file dialog looking for markdown |
| 13 | + const files = dialog.showOpenDialog(mainWindow, { |
| 14 | + properties: ['openFile'], |
| 15 | + filters: [ |
| 16 | + { |
| 17 | + name: 'Images', |
| 18 | + extensions: ['jpeg', 'jpg', 'png', 'gif', 'pdf'], |
| 19 | + }, |
| 20 | + ], |
| 21 | + }); |
| 22 | + |
| 23 | + // if no files |
| 24 | + if (!files) return; |
| 25 | + const file = files[0]; |
| 26 | + |
| 27 | + // Send fileContent to renderer |
| 28 | + mainWindow.webContents.send('new-file', file); |
| 29 | +} |
| 30 | + |
| 31 | +// Choose directory |
| 32 | +ipcMain.on('choose_app_dir', event => { |
| 33 | + const directory = dialog.showOpenDialog(mainWindow, { |
| 34 | + properties: ['openDirectory'], |
| 35 | + }); |
| 36 | + |
| 37 | + if (!directory) return; |
| 38 | + event.sender.send('app_dir_selected', directory[0]); |
| 39 | +}); |
| 40 | + |
| 41 | +ipcMain.on('view_app_dir', (event, appDir) => { |
| 42 | + shell.openItem(appDir); |
| 43 | +}); |
| 44 | + |
| 45 | +// Update file |
| 46 | +ipcMain.on('update-file', () => { |
| 47 | + openFile(); |
| 48 | +}); |
| 49 | + |
| 50 | +const createWindow = () => { |
| 51 | + // Create the browser window. |
| 52 | + // eslint-disable-next-line |
| 53 | + const { width, height } = require('electron').screen.getPrimaryDisplay().size; |
| 54 | + mainWindow = new BrowserWindow({ |
| 55 | + width, |
| 56 | + height, |
| 57 | + }); |
| 58 | + |
| 59 | + // and load the index.html of the app. |
| 60 | + mainWindow.loadURL(`file://${__dirname}/build/index.html`); |
| 61 | + |
| 62 | + const template = [ |
| 63 | + { |
| 64 | + label: 'File', |
| 65 | + submenu: [ |
| 66 | + { |
| 67 | + label: 'Open File', |
| 68 | + accelerator: process.platform === 'darwin' ? 'Cmd+O' : 'Ctrl+Shift+O', |
| 69 | + click() { |
| 70 | + openFile(); |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + label: 'Edit', |
| 77 | + submenu: [ |
| 78 | + { role: 'undo' }, |
| 79 | + { role: 'redo' }, |
| 80 | + { type: 'separator' }, |
| 81 | + { role: 'cut' }, |
| 82 | + { role: 'copy' }, |
| 83 | + { role: 'paste' }, |
| 84 | + { role: 'pasteandmatchstyle' }, |
| 85 | + { role: 'delete' }, |
| 86 | + { role: 'selectall' }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + { |
| 90 | + label: 'View', |
| 91 | + submenu: [ |
| 92 | + { role: 'reload' }, |
| 93 | + { role: 'forcereload' }, |
| 94 | + { role: 'toggledevtools' }, |
| 95 | + { type: 'separator' }, |
| 96 | + { role: 'resetzoom' }, |
| 97 | + { role: 'zoomin' }, |
| 98 | + { role: 'zoomout' }, |
| 99 | + { type: 'separator' }, |
| 100 | + { role: 'togglefullscreen' }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + { |
| 104 | + role: 'window', |
| 105 | + submenu: [{ role: 'minimize' }, { role: 'close' }], |
| 106 | + }, |
| 107 | + { |
| 108 | + role: 'help', |
| 109 | + submenu: [ |
| 110 | + { |
| 111 | + label: 'Learn More', |
| 112 | + click() { |
| 113 | + shell.openExternal('https://electronjs.org'); |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + { |
| 119 | + label: 'Developer', |
| 120 | + submenu: [ |
| 121 | + { |
| 122 | + label: 'Toggle Developer Tools', |
| 123 | + accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I', |
| 124 | + click() { |
| 125 | + mainWindow.webContents.toggleDevTools(); |
| 126 | + }, |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + ]; |
| 131 | + |
| 132 | + if (process.platform === 'darwin') { |
| 133 | + template.unshift({ |
| 134 | + label: app.getName(), |
| 135 | + submenu: [ |
| 136 | + { role: 'about' }, |
| 137 | + { type: 'separator' }, |
| 138 | + { role: 'services', submenu: [] }, |
| 139 | + { type: 'separator' }, |
| 140 | + { role: 'hide' }, |
| 141 | + { role: 'hideothers' }, |
| 142 | + { role: 'unhide' }, |
| 143 | + { type: 'separator' }, |
| 144 | + { role: 'quit' }, |
| 145 | + ], |
| 146 | + }); |
| 147 | + |
| 148 | + // Edit menu |
| 149 | + template[2].submenu.push( |
| 150 | + { |
| 151 | + type: 'separator', |
| 152 | + }, |
| 153 | + { |
| 154 | + label: 'Speech', |
| 155 | + submenu: [{ role: 'startspeaking' }, { role: 'stopspeaking' }], |
| 156 | + }, |
| 157 | + ); |
| 158 | + |
| 159 | + // Window menu |
| 160 | + template[4].submenu = [{ role: 'close' }, { role: 'minimize' }, { role: 'zoom' }, { type: 'separator' }, { role: 'front' }]; |
| 161 | + } |
| 162 | + |
| 163 | + const menu = Menu.buildFromTemplate(template); |
| 164 | + Menu.setApplicationMenu(menu); |
| 165 | + |
| 166 | + // Emitted when the window is closed. |
| 167 | + mainWindow.on('closed', () => { |
| 168 | + // Dereference the window object, usually you would store windows |
| 169 | + // in an array if your app supports multi windows, this is the time |
| 170 | + // when you should delete the corresponding element. |
| 171 | + mainWindow = null; |
| 172 | + }); |
| 173 | +}; |
| 174 | + |
| 175 | +// This method will be called when Electron has finished |
| 176 | +// initialization and is ready to create browser windows. |
| 177 | +// Some APIs can only be used after this event occurs. |
| 178 | +app.on('ready', () => { |
| 179 | + if (isDev) { |
| 180 | + const { default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = require('electron-devtools-installer'); |
| 181 | + |
| 182 | + installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS]) |
| 183 | + .then(() => { |
| 184 | + createWindow(); |
| 185 | + }) |
| 186 | + .catch(err => err); |
| 187 | + } else { |
| 188 | + createWindow(); |
| 189 | + } |
| 190 | +}); |
| 191 | + |
| 192 | +// Quit when all windows are closed. |
| 193 | +app.on('window-all-closed', () => { |
| 194 | + // On OS X it is common for applications and their menu bar |
| 195 | + // to stay active until the user quits explicitly with Cmd + Q |
| 196 | + if (process.platform !== 'darwin') { |
| 197 | + app.quit(); |
| 198 | + } |
| 199 | +}); |
| 200 | + |
| 201 | +app.on('activate', () => { |
| 202 | + // On OS X it's common to re-create a window in the app when the |
| 203 | + // dock icon is clicked and there are no other windows open. |
| 204 | + if (mainWindow === null) { |
| 205 | + createWindow(); |
| 206 | + } |
| 207 | +}); |
0 commit comments