You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// The app module controls the application lifecycle (like reacting to the ready state of the application).
6
-
BrowserWindow,
7
-
// BrowserWindow - allows for window creation via Electron. This is what actually shows something for the user. T
8
-
Menu,
9
-
// Menu - allows you to create native application menus and context menus.
10
-
shell,
11
-
// shell - Manage files and URLs using their default applications — The shell module provides functions related to desktop integration. An example of opening a URL in the user's default browser.
12
-
dialog,
13
-
// dialog - Display native system dialogs for opening and saving files, alerting, etc
14
-
ipcMain
15
-
// ipcMain - Communicate asynchronously from the main process to renderer processes. It is an event emitter.
16
-
// IPC stands for Inter-Process Communication. Electron uses IPC to send serialized JSON messages between the main and renderer processes.
// Keep a global reference of the window object, if you don't, the window will be closed automatically when the JavaScript object is garbage collected.
34
-
// We'll be working with one Window (mainWindow) in our application
11
+
// Keep a global reference of the window object, if you don't, the window will
12
+
// be closed automatically when the JavaScript object is garbage collected.
35
13
letmainWindow;
36
14
37
-
constcreateWindow=()=>{
38
-
// Creates the browser window. This is triggered in an event listener that ensures that the content is fully loaded before creating the Window.
39
-
const{ width, height }=require('electron').
40
-
screen.getPrimaryDisplay().size;
41
-
// screen is an event emitter in electron. It can grant you information about the user's screen (or screens in the case that they have external monitors)
42
-
// getPrimaryDisplay() - returns the display object which contains properties like size (which gives us the size of the screen)
15
+
// Open image file
16
+
functionopenFile(){
17
+
// Opens file dialog looking for markdown
18
+
constfiles=dialog.showOpenDialog(mainWindow,{
19
+
properties: ['openFile'],
20
+
filters: [
21
+
{
22
+
name: 'Images',
23
+
extensions: ['jpeg','jpg','png','gif','pdf'],
24
+
},
25
+
],
26
+
});
27
+
28
+
// if no files
29
+
if(!files)return;
30
+
constfile=files[0];
31
+
32
+
// Send fileContent to renderer
33
+
mainWindow.webContents.send('new-file',file);
34
+
}
43
35
44
-
// create a new BrowserWindow within the createWindow function
// Menu.buildFromTemplate adds to the default Menu using properties from the template passed into it
169
-
// the default menu props: File, Edit, View, Window and Help
170
188
constmenu=Menu.buildFromTemplate(template);
171
-
// Menu.setApplicationMenu - Sets menu as the application menu on macOS. On Windows and Linux, the menu will be set as each window's top menu.
172
189
Menu.setApplicationMenu(menu);
173
190
191
+
// Emitted when the window is closed.
174
192
mainWindow.on('closed',()=>{
175
-
// Emitted when the window is going to be closed. It's emitted before the beforeunload and unload event of the DOM.
193
+
// Dereference the window object, usually you would store windows
194
+
// in an array if your app supports multi windows, this is the time
195
+
// when you should delete the corresponding element.
176
196
mainWindow=null;
177
197
});
178
-
179
198
};
180
199
181
-
// Open local image file for prototyping
182
-
functionopenFile(){
183
-
// Opens file dialog looking for markdown
184
-
constfiles=dialog.showOpenDialog(mainWindow,{
185
-
// dialog.showOpenDialog - allows you to open files / folders
186
-
// properties: ['openFile'] - allows files to be selected
187
-
properties: ['openFile'],
188
-
filters: [
189
-
{
190
-
// ensure only image files with specific extensions can be selected and uploaded into the app
191
-
name: 'Images',
192
-
extensions: ['jpeg','jpg','png','gif','pdf']
193
-
}
194
-
]
195
-
});
196
-
197
-
// if no files
198
-
if(!files)return;
199
-
constfile=files[0];
200
-
201
-
// Send fileContent to renderer
202
-
mainWindow.webContents.send('new-file',file);
203
-
// webContents is an EventEmitter. It is responsible for rendering and controlling a web page and is a property of the BrowserWindow object
204
-
}
205
-
206
-
// Update file
207
-
ipcMain.on('update-file',()=>{
208
-
// openFile defined above - allows user to open a file from their local file system
209
-
openFile();
210
-
});
211
-
212
-
// Choose directory to save / export your project
213
-
ipcMain.on('choose_app_dir',(event)=>{
214
-
constdirectory=dialog.showOpenDialog(mainWindow,{
215
-
// openDirectory allows directories to be opened - 'Export' button appears from the directory selection pop-up after the user has clicked `export project` and selected their preferred export type (`Export components` or `Export components with application files`)
216
-
properties: ['openDirectory'],
217
-
buttonLabel: 'Export'
218
-
});
219
-
// if no directory is selected, do not export the files
220
-
if(!directory)return;
221
-
// event.sender.send sends to the main frame. Allows user to send the file to their file system / save it locally
// shell.openItem - opens the given file in the desktop's default manner
227
-
shell.openItem(appDir);
228
-
});
229
-
230
-
231
-
// This method will be called when Electron has finished initialization and is ready to create browser windows. Some APIs can only be used after this event occurs.
200
+
// This method will be called when Electron has finished
201
+
// initialization and is ready to create browser windows.
202
+
// Some APIs can only be used after this event occurs.
// activate can be triggered when launching the application for the first time, attempting to re-launch the application when it's already running, or clicking on the application's dock or taskbar icon.
255
-
if(mainWindow===null){
256
-
// we're only working with one main window in our application, so if that is set to null it means the window is not open. We want to open it when the user clicks on the icon in the dock.
0 commit comments