electron-spell-check-provider
Electron's spell-check API looks straightforward… until
you have to pull in and configure node-spellchecker
:
webFrame.setSpellCheckProvider('en-US', true, {
spellCheck: function(text) {
+ return !(require('spellchecker').isMisspelled(text));
}
});
Which improperly flags contractions:
And only underlines misspelled words, leaving you to show suggestions yourself…
Or you can use this module:
webFrame.setSpellCheckProvider('en-US', true, new SpellCheckProvider('en-US'));
Installation
yarn add electron-spell-check-provider
or
npm install electron-spell-check-provider --save
Note: This uses a native module, so you will need to rebuild your modules after installing.
Usage
var webFrame = require('electron').webFrame;
var SpellCheckProvider = require('electron-spell-check-provider');
webFrame.setSpellCheckProvider('en-US', true, new SpellCheckProvider('en-US'));
'en-US' is the only supported language at present.
As the text selection changes, Electron will poll the spell-check provider.
If the current word is misspelled, the provider will emit a 'misspelling'
event
with spelling suggestions:
webFrame.setSpellCheckProvider('en-US', true,
new SpellCheckProvider('en-US').on('misspelling', function(suggestions) {
console.log('The text at the site of the cursor is misspelled.',
'Maybe the user meant to type:', suggestions);
}
}));
If you save these suggestions, you can then show them in a menu when the
'contextmenu'
event next fires.
Here's a full implementation that uses
electron-editor-context-menu
to build the menu
for you in addition to handling some other gotchas:
var remote = require('electron').remote;
var webFrame = require('electron').webFrame;
var SpellCheckProvider = require('electron-spell-check-provider');
var buildEditorContextMenu = remote.require('electron-editor-context-menu');
var selection;
function resetSelection() {
selection = {
isMisspelled: false,
spellingSuggestions: []
};
}
resetSelection();
window.addEventListener('mousedown', resetSelection);
webFrame.setSpellCheckProvider(
'en-US',
true,
new SpellCheckProvider('en-US').on('misspelling', function(suggestions) {
if (window.getSelection().toString()) {
selection.isMisspelled = true;
selection.spellingSuggestions = suggestions.slice(0, 3);
}
}));
window.addEventListener('contextmenu', function(e) {
if (!e.target.closest('textarea, input, [contenteditable="true"]')) return;
var menu = buildEditorContextMenu(selection);
setTimeout(function() {
menu.popup(remote.getCurrentWindow());
}, 30);
});
Adding words to the dictionary
You can add a word to the spell-check dictionary by calling the add
instance method. Pass the word to add as an argument. Additions to the dictionary are persistent.
let provider = new SpellCheckProvider('en-US');
let newWord = window.getSelection().toString();
provider.add(newWord);
You can do this in a context menu click handler function for an "Add to Dictionary" menu item.
Contributing
We welcome pull requests! In particular, we'd love to see support for additional
languages.
Please lint your code.
Copyright and License
Copyright 2016 Mixmax, Inc., licensed under the MIT License.
Release History
- 1.1.0 Support adding words to the dictionary (#6)
- 1.0.0 Initial release.