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

Wiring the toggle bold menu

Finally, let's wire the command with the toggle-bold functionality:

  1. The markdown editor component we are using for this application provides multiple functions that developers can invoke from code. One of those functions is toggleBold(). Our code can check the content of the message, and if it's the toggle-bold one, it will run the corresponding component function:
      if (arg === 'toggle-bold') {
editor.toggleBold();
}
  1. The whole script section should look as follows:
      <script>
var editor = new SimpleMDE({
element: document.getElementById('editor')
});

const { ipcRenderer } = require('electron');

ipcRenderer.on('editor-event', (event, arg) => {
console.log(arg);
event.sender.send('editor-reply', `Received ${arg}`);

if (arg === 'toggle-bold') {
editor.toggleBold();
}
});

ipcRenderer.send('editor-reply', 'Page Loaded');
</script>
  1. Restart the application once again, type something into the editor, and then select the text. Next, click the Format | Toggle Bold menu item and see what happens. The text you previously selected will be emboldened and the markdown editor will render special ** symbols around the selection, as shown in the following screenshot:

Congratulations! You have got cross-process messaging up and running in your Electron application.

You have also integrated the Electron application menu with the web component hosted inside the application. This employs specific messages that allow Javascript code to trigger formatting features.

As an exercise, try to provide support for more formatting features, such as italic and strikethrough, styles. The markdown editor functions of interest are editor.toggleItalic() and editor.toggleStrikethrough().

The editor component supports many other useful functions. For a list of available methods and properties, please refer to the corresponding documentation: https://github.com/sparksuite/simplemde-markdown-editor#toolbar-icons.
主站蜘蛛池模板: 曲阜市| 宾阳县| 邢台市| 蒲江县| 墨玉县| 贵溪市| 英德市| 河津市| 上高县| 合江县| 常宁市| 横山县| 柯坪县| 石柱| 深州市| 保德县| 嘉祥县| 德格县| 大关县| 兴业县| 枣阳市| 通许县| 舒城县| 巴林右旗| 常德市| 乌拉特中旗| 德化县| 彭水| 神农架林区| 获嘉县| 泉州市| 朝阳区| 扎赉特旗| 黄大仙区| 西畴县| 讷河市| 碌曲县| 四会市| 朝阳县| 昌吉市| 绍兴市|