electron-editor-context-menu

Enable the native right-click menu in Electron.

  • 所有者: mixmaxhq/electron-editor-context-menu
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

electron-editor-context-menu

In Electron, right-clicking in text editors does… nothing.

This module enables the menu you'd expect, with optional
spell-checker integration.

Installation

npm install electron-editor-context-menu

or

npm install electron-editor-context-menu --save

Usage

// In the renderer process:
var remote = require('electron').remote;
// `remote.require` since `Menu` is a main-process module.
var buildEditorContextMenu = remote.require('electron-editor-context-menu');

window.addEventListener('contextmenu', function(e) {
  // Only show the context menu in text editors.
  if (!e.target.closest('textarea, input, [contenteditable="true"]')) return;

  var menu = buildEditorContextMenu();

  // The 'contextmenu' event is emitted after 'selectionchange' has fired but possibly before the
  // visible selection has changed. Try to wait to show the menu until after that, otherwise the
  // visible selection will update after the menu dismisses and look weird.
  setTimeout(function() {
    menu.popup(remote.getCurrentWindow());
  }, 30);
});

Spell-checker integration

Show spelling suggestions by passing a selection object when building the menu:

var selection = {
  isMisspelled: true,
  spellingSuggestions: [
    'men',
    'mean',
    'menu'
  ]
};

var menu = buildEditorContextMenu(selection);

Get these suggestions when your spell-check provider runs
—Electron will poll it immediately before the 'contextmenu' event fires.

For a complete example using electron-spell-check-provider, see
here.

Customizing the menu

You can add or remove items to the menu, or replace it entirely, by providing
mainTemplate and/or suggestionsTemplate parameters when building the menu:

var menu = buildEditorContextMenu(selection, mainTemplate, suggestionsTemplate);

The mainTemplate parameter customizes the always-present menu items; the
suggestionsTemplate parameter customizes the spelling suggestion items.
Pass an array of items to replace the default items entirely; pass a function
to add/remove/edit the default items. The function will be passed the default
array of items as a parameter and should return an array of items.

Credits

Created by Jeff Wear.

Thanks to https://github.com/atom/electron/pull/942#issuecomment-171445954 for
the initial sketch of this.

Copyright 2016 Mixmax, Inc., licensed under the MIT License.

Release History

  • 1.1.1 Fix compatibility with electron-builder
  • 1.1.0 Add the ability to customize the main template and the suggestions template.
  • 1.0.0 Initial release.

主要指標

概覽
名稱與所有者mixmaxhq/electron-editor-context-menu
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-02-06 00:56:10
推送於2021-06-25 19:01:09
最后一次提交2021-06-25 12:01:06
發布數3
最新版本名稱v1.1.1 (發布於 2016-03-09 11:47:05)
第一版名稱v1.0.0 (發布於 )
用户参与
星數49
關注者數6
派生數9
提交數24
已啟用問題?
問題數7
打開的問題數3
拉請求數7
打開的拉請求數0
關閉的拉請求數3
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?