GithubHelp home page GithubHelp logo

kwonoj / electron-hunspell Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 2.0 1.85 MB

Providing hunspell based spellchecker for Electron applications

License: MIT License

JavaScript 3.17% TypeScript 94.95% HTML 1.88%
electron spellchecker hunspell webassembly spell electron-hunspell

electron-hunspell's Introduction

Build Status Build status codecov npm node

Deprecated: Electron provides built in spellchecker now: electron/electron#20897

Electron-hunspell

electron-hunspell provides hunspell based spell checker to Electron based applications with minimal, simple api. This module aims specific design goals compare to other spellchecker implementations

  • 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

From 1.0, electron-hunspell only supports electron@5 and above supports async spellchecker interface. For previous version of electron, use 0.x version.

Install

npm install electron-hunspell

Usage

Creating spellchecker provider

electron-hunspell exposes SpellCheckerProvider, class to manage spellchecker instance for several dictionaries.

import { SpellCheckerProvider } from 'electron-hunspell';

const provider = new SpellCheckerProvider();
await provider.initialize();

initialize accepts options to pass into hunspell-asm binary if needed.

initialize(initOptions?: Partial<{ timeout: number; }>): Promise<void>;

Once you have provider instance, you can manage each dictionary based on locale key.

const aff = await (await fetch('https://unpkg.com/[email protected]/en-us.aff')).arrayBuffer();
const dic = await (await fetch('https://unpkg.com/[email protected]/en-us.dic')).arrayBuffer();

await provider.loadDictionary('en', new Uint8Array(dic), new Uint8Array(aff));

loadDictionary creates spellchecker instance to corresponding locale key.

public loadDictionary(key: string, dicBuffer: ArrayBufferView, affBuffer: ArrayBufferView): Promise<void>;

When dictionary is no longer needed it should be manually disposed via unloadDictionary interface.

public unloadDictionary(key: string): Promise<void>

To get suggested text for misspelled text use getSuggestion

public getSuggestion(text: 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 addWord(languageKey: 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.

const attached = attachSpellCheckProvider(provider);

//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
const provider = new SpellCheckerProvider();
self.onmessage = (event) => {
  switch (type) {
    ...
    case 'spell':
      postMessage('message', provider.spell(...));
  }
}

//renderer.js
const worker = new Worker('provider.js');
//proxy object implements necessary interfaces to attach spellchecker
const providerProxy = {
  spell: (text) => {
    worker.addEventListener('message', onSpell);
    worker.postMessage(...);
  },
  ...
};

// use proxy object to attach spellchecker to webframe
await attachSpellCheckProvider(providerProxy);

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 dist.
  • 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

License

MIT

electron-hunspell's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar greenkeeper[bot] avatar greenkeeperio-bot avatar kwonoj avatar zeevl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vpetlovyi zeevl

electron-hunspell's Issues

An in-range update of conventional-changelog-cli is breaking the build 🚨

The devDependency conventional-changelog-cli was updated from 2.0.5 to 2.0.7.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

conventional-changelog-cli is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/appveyor/branch: Waiting for AppVeyor build to complete (Details).
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

SpellCheckerProvider.initialize() indirectly registers an unhandledRejection and an uncaughtException listener that terminate the process

If you load electron-hunspell into node and initialize SpellCheckerProvider, the underlying Emscripten build of hunspell-asm will have the side-effect of:

process["on"]("unhandledRejection", abort);

and

process['on']('uncaughtException', function(ex) {
    // suppress ExitStatus exceptions from showing an error
    if (!(ex instanceof ExitStatus)) {
      throw ex;
    }
  });

These are not desirable and are quite difficult/awkward to disable.

Recommend using a build with NODEJS_CATCH_REJECTION = 0 and NODEJS_CATCH_REJECTION = 0

Korean dictionary (fresh) doesn't work properly with electron-hunspell

I tried to electron-hunspell with Korean dictionary I've got from https://github.com/spellcheck-ko/hunspell-dict-ko

The related npm package is outdated (https://www.npmjs.com/package/hunspell-dict-ko) so I grabbed a dictionary from a source.

It didn't work, unfortunately, tho it worked fine with the CLI Hunspell so the dictionary itself seems to be fine. With electron-hunspell, it didn't marked anything unless you used a diacritic.

The dictionary itself looks like it contains every possible Korean and non-diacritic Latin character. Perhaps this was misinterpreted by electron-hunspell.

SpellCheckerProvider.initialize() timeout parameter

I'm trying to provide timeout as an optional parameter in SpellCheckerProvider.initialize() as I occasionally hit the default timeout and get an error. However, I can't seem to figure out what the default timeout value is or what unit the timeout is in? I assume the timeout is specified in milliseconds, but wanted to be sure.

Would be great to add to the docs as well.

Feature Request - Allow adding words to ignore spellcheck

Great job on the lib! Works really with electron.

Just wondering if there is a way to add words to ignore in the spellchecker? I.e. the word JSON will always trigger spellcheck to mark it as incorrect. It would be extremely helpful if there is a way to add it to ignore list.

Thanks

Find misspelled words

Hi! First of all, thanks a lot, this package is simply fantastic.
As you probably know, there isn't any way to see in the DOM a list of marked misspelled words. Do you think it's viable to add a findMisspelled method to the API. It could be very simple. It receives a string (or, If you'd like, a string array as well) and returns an array with all the words that the spellchecker would mark.

I'm not too familiar with typescript, but if you're ok with it I'd be willing to try to implement this!

Impossible to run with twitter?

Hello,

I'm using electron-hunspell and I'd like to say: great job!

Im wondering if there is a way to make it work when I have a webview with Twitter loaded inside, every time that I try to open twitter, I've faced this error which I think that is something particular from twitter

Uncaught (in promise) CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder

Any ideas?

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 23.3.6 to 23.3.7.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • βœ… continuous-integration/travis-ci/push: The Travis CI build passed (Details).
  • ❌ continuous-integration/appveyor/branch: AppVeyor build failed (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

todo

  • cannot compile wasm in browserView? πŸ‘ˆ falls back to asm.js?

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 10.11.7 to 10.12.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • βœ… continuous-integration/appveyor/branch: AppVeyor build succeeded (Details).
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.