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

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.
主站蜘蛛池模板: 浮山县| 沁阳市| 利辛县| 大洼县| 乳源| 青州市| 安多县| 晋州市| 鞍山市| 焦作市| 保靖县| 湄潭县| 武乡县| 常州市| 平陆县| 呼和浩特市| 梁山县| 雷山县| 修武县| 桑植县| 古浪县| 四子王旗| 康平县| 临城县| 沿河| 鄂托克前旗| 昌邑市| 沁源县| 水富县| 陈巴尔虎旗| 邳州市| 包头市| 大港区| 同仁县| 松溪县| 青河县| 田林县| 巢湖市| 边坝县| 灵丘县| 高要市|