From 64824a692f566a01cc208dd82239ad643b69093e Mon Sep 17 00:00:00 2001 From: vantezzen Date: Sat, 19 Oct 2019 22:25:40 +0200 Subject: Use electron-spellchecker instead of electron-hunspell --- src/webview/spellchecker.js | 78 +++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 49 deletions(-) (limited to 'src/webview/spellchecker.js') diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index 1cb449110..2062429fd 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js @@ -1,36 +1,21 @@ import { webFrame } from 'electron'; -import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell'; -import path from 'path'; -import { readFileSync } from 'fs'; +import {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} from 'electron-spellchecker'; -import { DICTIONARY_PATH } from '../config'; import { SPELLCHECKER_LOCALES } from '../i18n/languages'; -const debug = require('debug')('Ferdi:spellchecker'); +const debug = require('debug')('Franz:spellchecker'); -let provider; +let handler; let currentDict; +let contextMenuBuilder; let _isEnabled = false; -let attached; -const DEFAULT_LOCALE = 'en-us'; - -async function loadDictionary(locale) { - try { - const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); - debug('Loaded dictionary', locale, 'from', fileLocation); - return provider.loadDictionary(locale, readFileSync(`${fileLocation}.dic`), readFileSync(`${fileLocation}.aff`)); - } catch (err) { - console.error('Could not load dictionary', err); - } -} - -export async function switchDict(locale = DEFAULT_LOCALE) { +export async function switchDict(locale) { try { debug('Trying to load dictionary', locale); - if (!provider) { - console.warn('SpellcheckProvider not initialized'); + if (!handler) { + console.warn('SpellcheckHandler not initialized'); return; } @@ -41,11 +26,7 @@ export async function switchDict(locale = DEFAULT_LOCALE) { return; } - if (currentDict) { - provider.unloadDictionary(locale); - } - await loadDictionary(locale); - await attached.switchLanguage(locale); + handler.switchLanguage(locale); debug('Switched dictionary to', locale); @@ -56,34 +37,22 @@ export async function switchDict(locale = DEFAULT_LOCALE) { } } -export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { - const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); - - if (locales.length >= 1) { - return locales[0]; - } - - return null; -} - -export default async function initialize(languageCode = DEFAULT_LOCALE) { +export default async function initialize(languageCode = 'en-us') { try { - provider = new SpellCheckerProvider(); - const locale = getSpellcheckerLocaleByFuzzyIdentifier(languageCode); + handler = new SpellCheckHandler(); + handler.attachToInput(); + const locale = languageCode.toLowerCase(); debug('Init spellchecker'); - await provider.initialize(); - - debug('Attaching spellcheck provider'); - attached = await attachSpellCheckProvider(provider); - - const availableDictionaries = await provider.getAvailableDictionaries(); - debug('Available spellchecker dictionaries', availableDictionaries); + switchDict(locale); - await switchDict(locale); + contextMenuBuilder = new ContextMenuBuilder(handler); + new ContextMenuListener((info) => { + contextMenuBuilder.showPopupMenu(info); + }); - return provider; + return handler; } catch (err) { console.error(err); return false; @@ -96,8 +65,19 @@ export function isEnabled() { export function disable() { if (isEnabled()) { + handler.unsubscribe(); webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true }); _isEnabled = false; currentDict = null; } } + +export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { + const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); + + if (locales.length >= 1) { + return locales[0]; + } + + return null; +} -- cgit v1.2.3-54-g00ecf