- No native module dependencies
- No platform specific code, consistent behavior via hunspell
- Low level explicit api surface
There are couple of modules to improve spell checking experiences via
electron-hunspell to check out if you're interested
hunspell-dict-downloader: Downloader for hunspell dict around several available locales
electron-hunspell only supports electron@5 and above supports async spellchecker interface. For previous version of electron, use 0.x version.
npm install electron-hunspell
Creating spellchecker provider
SpellCheckerProvider, class to manage spellchecker instance for several dictionaries.
initialize accepts options to pass into
hunspell-asm binary if needed.
initializeinitOptions?: Partial<>: Promise<void>;
Once you have provider instance, you can manage each dictionary based on locale key.
;;await provider.loadDictionary'en', new Uint8Arraydic, new Uint8Arrayaff;
loadDictionary creates spellchecker instance to corresponding locale key.
public loadDictionarykey: string, dicBuffer: ArrayBufferView, affBuffer: ArrayBufferView: Promise<void>;
When dictionary is no longer needed it should be manually disposed via
public unloadDictionarykey: string: Promise<void>
To get suggested text for misspelled text use
public getSuggestiontext: string: Promise<Readonly<Array<string>>>
It'll ask currently selected spellchecker to get suggestion for misspelling.
To add a word to the dictionary, use
addWord. This is runtime behavior, so it doesn't
persist over once instance is disposed.
public addWordlanguageKey: string, text: string: Promise<void>
Few other convenient interfaces are available as well.
//Returns array of key for currently loaded dictionaries//in desecending order of how long it has been used.public getAvailableDictionaries: Promise<Readonly<Array<string>>>;//Returns key of currently selected dictionary.public getSelectedDictionary: Promise<string | null>;
Attach provider to webFrame
Once provider instance is ready,
attachSpellCheckProvider can actually attach those into current webFrame.
;//Change language for spellchecker attached to webFrame.await attached.switchLanguage'en';//Teardown webFrame's spellchecker based on current language.await attached.unsubscribe;
attachSpellCheckProvider relies on
provider to get current language's dictionary. If dictionary is not loaded via
loadDictionary, spellcheck won't work.
Put provider to another thread
attachSpellCheckProvider not only accepts instance of
SpellCheckerProvider but also accepts any proxy object can commnuicate to actual provider instance. Since Electron's spellchecker is now async, it is possible to place provider instnace to other than main thread like web worker.
//pseudo code//provider.js;self.onmessage =//renderer.js;//proxy object implements necessary interfaces to attach spellchecker;// use proxy object to attach spellchecker to webframeawait attachSpellCheckProviderproviderProxy;
attachSpellCheckProvider does not aware where does provider placed - it can be other process communicates via IPC, or webworker, or something else and does not provide any proxy implementation by default. Also note using IPC for proxy need caution, as spellcheck can cause amount of IPC request based on user typings.
Example provides simple code how to use SpellCheckerProvider in general.
npm run example:browserwindow will executes example for general browserWindow,
npm run example:browserview provides example for loading external page via
browserView with preload scripts.
npm run example:worker will load example running provider under web worker.
Building / Testing
Few npm scripts are supported for build / test code.
build: Transpiles code to ES5 commonjs to
test: Run test cases.
lint: Run lint over all codebases
lint:staged: Run lint only for staged changes. This'll be executed automatically with precommit hook.
commit: Commit wizard to write commit message