官术网_书友最值得收藏!

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:

  1. 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.

  1. 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.

  1. 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:
  1. You can also switch to the Text files group by means of the native Open dialog:
  1. 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.
  2. 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
}
});
  1. 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.
  2. 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.
  3. 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);
}
});
  1. 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);
}
});
});
  1. Open the index.html file for editing and scroll to the scripts section, where we already have some process communication handling in place.
  2. 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
}
});
  1. 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);
}
});
  1. 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.

主站蜘蛛池模板: 易门县| 阿图什市| 贡嘎县| 玉溪市| 沭阳县| 齐齐哈尔市| 海安县| 新民市| 绩溪县| 介休市| 沽源县| 衡阳市| 灵璧县| 周至县| 临颍县| 拉萨市| 宜宾市| 临夏市| 库车县| 维西| 册亨县| 吕梁市| 娱乐| 垫江县| 遂昌县| 财经| 万年县| 民县| 安塞县| 碌曲县| 鸡东县| 昭苏县| 西青区| 浦城县| 易门县| 黎城县| 阜康市| 凤庆县| 舞钢市| 什邡市| 岳阳市|