npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-spell-check-provider

v1.1.1

Published

Because Electron's spell-check APIs are just a little too low-level.

Downloads

21

Readme

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

// In the renderer process:
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.

But how do I show spelling suggestions (in the context menu)?

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:

/**
 * Enables spell-checking and the right-click context menu in text editors.
 * Electron (`webFrame.setSpellCheckProvider`) only underlines misspelled words;
 * we must manage the menu ourselves.
 *
 * Run this in the renderer process.
 */
var remote = require('electron').remote;
var webFrame = require('electron').webFrame;
var SpellCheckProvider = require('electron-spell-check-provider');
// `remote.require` since `Menu` is a main-process module.
var buildEditorContextMenu = remote.require('electron-editor-context-menu');

var selection;
function resetSelection() {
  selection = {
    isMisspelled: false,
    spellingSuggestions: []
  };
}
resetSelection();

// Reset the selection when clicking around, before the spell-checker runs and the context menu shows.
window.addEventListener('mousedown', resetSelection);

// The spell-checker runs when the user clicks on text and before the 'contextmenu' event fires.
// Thus, we may retrieve spell-checking suggestions to put in the menu just before it shows.
webFrame.setSpellCheckProvider(
  'en-US',
  // Not sure what this parameter (`autoCorrectWord`) does: https://github.com/atom/electron/issues/4371
  // The documentation for `webFrame.setSpellCheckProvider` passes `true` so we do too.
  true,
  new SpellCheckProvider('en-US').on('misspelling', function(suggestions) {
    // Prime the context menu with spelling suggestions _if_ the user has selected text. Electron
    // may sometimes re-run the spell-check provider for an outdated selection e.g. if the user
    // right-clicks some misspelled text and then an image.
    if (window.getSelection().toString()) {
      selection.isMisspelled = true;
      // Take the first three suggestions if any.
      selection.spellingSuggestions = suggestions.slice(0, 3);
    }
  }));

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(selection);

  // 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);
});

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.