- Electron Projects
- Denys Vuika
- 672字
- 2021-06-24 12:14:39
Loading files from a local system
Now that you have got the Open File functionality and registered the global keyboard shortcut for it, let's see what it takes to load a file from the local filesystem back into the editor component:
- Let's start by updating the menu.js file and registering a second global shortcut for Cmd + O or Ctrl + O, depending on the user's desktop platform:
globalShortcut.register('CommandOrControl+O', () => {
// show open dialog
});
We have already imported the dialog object from the Electron framework. You can use it to invoke the system's Open dialog as well.
- Update the menu.js file according to the following code:
globalShortcut.register('CommandOrControl+O', () => {
const window = BrowserWindow.getFocusedWindow();
const options = {
title: 'Pick a markdown file',
filters: [
{ name: 'Markdown files', extensions: ['md'] },
{ name: 'Text files', extensions: ['txt'] }
]
};
dialog.showOpenDialog(window, options);
});
Note that, this time, we are providing more than one file filter. This allows users to open multiple file formats in a grouped fashion. For the sake of simplicity, we are allowing our users to open markdown and plain text files.
- Run the application and press Cmd + O or Ctrl + O, depending on the platform you are using for development. Note that the system dialog appears and allows us to select markdown files by default:

- You can also switch to the Text files group by means of the native Open dialog:

- Now, let's get back to the menu.js file. Similar to the Save dialog, the Open dialog supports a callback function that provides us with information about selected files. The user can also close the dialog without picking anything, so you should always validate the results.
- Given the nature of our editor application, we are only providing support for editing one file at a time. That 's why you only need to pick the first file if the user performs multi-selection, as follows:
dialog.showOpenDialog(window, options, paths => {
if (paths && paths.length > 0) {
// read file and send to the renderer process
}
});
- Finally, we use the fs object that we imported from Node.js earlier to support the Save dialog. This time, however, we are looking for the fs.readFileSync method.
- As soon as we've read the file, we need to emit the cross-process event via the load channel so that the rendering process can listen and perform additional actions.
- Update the dialog.showOpenDialog call so that it looks as follows:
dialog.showOpenDialog(window, options, paths => {
if (paths && paths.length > 0) {
const content = fs.readFileSync(paths[0]).toString();
window.webContents.send('load', content);
}
});
- Before we move on to the rendering side, please ensure that the implementation of your new global shortcut looks as follows:
globalShortcut.register('CommandOrControl+O', () => {
const window = BrowserWindow.getFocusedWindow();
const options = {
title: 'Pick a markdown file',
filters: [
{ name: 'Markdown files', extensions: ['md'] },
{ name: 'Text files', extensions: ['txt'] }
]
};
dialog.showOpenDialog(window, options, paths => {
if (paths && paths.length > 0) {
const content = fs.readFileSync(paths[0]).toString();
window.webContents.send('load', content);
}
});
});
- Open the index.html file for editing and scroll to the scripts section, where we already have some process communication handling in place.
- Add a new handler that listens to the load channel and the corresponding messages coming from the renderer process:
ipcRenderer.on('load', (event, content) => {
if (content) {
// do something with content
}
});
- As you can see, we're validating the input to ensure that the text content is indeed there and using the editor.value(<text>) method to replace the markdown editor content with new text:
ipcRenderer.on('load', (event, content) => {
if (content) {
editor.value(content);
}
});
- This is all we need to implement for the Open File feature. Run or restart your Electron application, press Cmd + O or Ctrl + O, and select a markdown file:

You should now see the content of the file on the screen. As soon as we call the value() function, the SimpleMDE component will reformat everything according to the markdown rules.
推薦閱讀
- Deploying Node.js
- Java面向對象思想與程序設計
- Python自動化運維快速入門(第2版)
- PyQt從入門到精通
- Rust編程從入門到實戰
- Python網絡爬蟲從入門到實踐(第2版)
- Java應用開發技術實例教程
- Apex Design Patterns
- Apache Mahout Clustering Designs
- Getting Started with Gulp
- 前端HTML+CSS修煉之道(視頻同步+直播)
- AIRIOT物聯網平臺開發框架應用與實戰
- Visual Basic程序設計習題與上機實踐
- RESTful Web Clients:基于超媒體的可復用客戶端
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)