From 1d6df88ef74d67cc76a4879d89e05b3bda1742a4 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Wed, 5 Jun 2019 17:18:52 +0200 Subject: Fix spellchecker integration --- src/webview/contextMenu.js | 5 +++-- src/webview/spellchecker.js | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'src/webview') diff --git a/src/webview/contextMenu.js b/src/webview/contextMenu.js index 83914f581..2fdbcdca6 100644 --- a/src/webview/contextMenu.js +++ b/src/webview/contextMenu.js @@ -297,12 +297,13 @@ const buildMenuTpl = (props, suggestions, isSpellcheckEnabled, defaultSpellcheck }; export default function contextMenu(spellcheckProvider, isSpellcheckEnabled, getDefaultSpellcheckerLanguage, getSpellcheckerLanguage) { - webContents.on('context-menu', (e, props) => { + webContents.on('context-menu', async (e, props) => { e.preventDefault(); let suggestions = []; if (spellcheckProvider && props.misspelledWord) { - suggestions = spellcheckProvider.getSuggestion(props.misspelledWord); + debug('Mispelled word', props.misspelledWord); + suggestions = await spellcheckProvider.getSuggestion(props.misspelledWord); debug('Suggestions', suggestions); } diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index 9158b3b94..64575753f 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js @@ -1,6 +1,8 @@ import { webFrame } from 'electron'; -import { SpellCheckerProvider } from 'electron-hunspell'; +import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell'; +import { ENVIRONMENT } from 'hunspell-asm'; import path from 'path'; +import { readFileSync } from 'fs'; import { DICTIONARY_PATH } from '../config'; import { SPELLCHECKER_LOCALES } from '../i18n/languages'; @@ -10,11 +12,12 @@ const debug = require('debug')('Franz:spellchecker'); let provider; let currentDict; let _isEnabled = false; +let attached; async function loadDictionary(locale) { try { const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); - await provider.loadDictionary(locale, `${fileLocation}.dic`, `${fileLocation}.aff`); + await provider.loadDictionary(locale, readFileSync(`${fileLocation}.dic`), readFileSync(`${fileLocation}.aff`)); debug('Loaded dictionary', locale, 'from', fileLocation); } catch (err) { console.error('Could not load dictionary', err); @@ -41,7 +44,7 @@ export async function switchDict(locale) { provider.unloadDictionary(locale); } loadDictionary(locale); - provider.switchDictionary(locale); + attached.switchLanguage(locale); debug('Switched dictionary to', locale); @@ -58,12 +61,15 @@ export default async function initialize(languageCode = 'en-us') { const locale = languageCode.toLowerCase(); debug('Init spellchecker'); - await provider.initialize(); - // await loadDictionaries(); + await provider.initialize({ environment: ENVIRONMENT.NODE }); + + debug('Attaching spellcheck provider'); + attached = await attachSpellCheckProvider(provider); debug('Available spellchecker dictionaries', provider.availableDictionaries); - switchDict(locale); + attached.switchLanguage(locale); + console.log('seas oida', attached, provider); return provider; } catch (err) { -- cgit v1.2.3-70-g09d2 From 9180318200dc2b398bc2ec248f9ff8fa3f035fbb Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 27 Sep 2019 13:56:49 +0200 Subject: Fix spellchecker --- package-lock.json | 43 ++++++++++++++++--------------------------- package.json | 2 +- src/webview/spellchecker.js | 32 ++++++++++++++++---------------- 3 files changed, 33 insertions(+), 44 deletions(-) (limited to 'src/webview') diff --git a/package-lock.json b/package-lock.json index e670c2a03..b0cc72ec7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7190,12 +7190,11 @@ } }, "electron-hunspell": { - "version": "1.0.0-beta.7", - "resolved": "https://registry.npmjs.org/electron-hunspell/-/electron-hunspell-1.0.0-beta.7.tgz", - "integrity": "sha512-BByCXqU8sZbuzyoxKcBYjycp8ORiqjeYZd2OT8RNjVW+XGPmZkHewXUu4tUa1IXW6ij6IqojV5kr+xhYKLN1DA==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/electron-hunspell/-/electron-hunspell-1.0.0.tgz", + "integrity": "sha512-egyioCtGkkNMOYdKBrZsva63JxbPmNjmQVFCZCcmr+uIUs6et6OUvqd6ac9/ujuchiEPDrTfF9gdR9+lPbVyPA==", "requires": { - "hunspell-asm": "^2.0.0", - "tslib": "1.9.3" + "hunspell-asm": "^4.0.0" } }, "electron-is-dev": { @@ -11504,32 +11503,22 @@ } }, "hunspell-asm": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/hunspell-asm/-/hunspell-asm-2.0.0.tgz", - "integrity": "sha512-FMIUBpy+bRREFAvC63RcYEK9U6+FdlEojGSVsvMTpuZ2CekYyUBwEfG4+ExIQOQyEN0U6f1MoVgrFqvgPNSA2g==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/hunspell-asm/-/hunspell-asm-4.0.0.tgz", + "integrity": "sha512-EcNwMx0Byq1JHMZiuATmqpMk41bOo+NH4yD5xJ3H0X403MoDBory3zri2lJzYSGj+z/6Bqr3EZn24syCjZoY1w==", "requires": { - "emscripten-wasm-loader": "^2.2.3", - "nanoid": "^2.0.1", - "tslib": "^1.9.3", - "unixify": "^1.0.0" + "emscripten-wasm-loader": "^3.0.3", + "nanoid": "^2.1.1" }, "dependencies": { "emscripten-wasm-loader": { - "version": "2.2.3", - "resolved": "https://registry.npmjs.org/emscripten-wasm-loader/-/emscripten-wasm-loader-2.2.3.tgz", - "integrity": "sha512-//gI0afvtVnY7smttrPOW/BM+pTJLmdIcXAQV3fgRdQ0o6wm2vdSI0kjLYVOeo/rbtRsGgVuMLu6fT+O//EfaA==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/emscripten-wasm-loader/-/emscripten-wasm-loader-3.0.3.tgz", + "integrity": "sha512-fyq2maBt5LOou27LEBlL5H6G04BxgSamXkvmMsAuIT6rd8ioH4BxNQhuyl6jVPeODh6U8Wk1BoFZxzHpg3o8wA==", "requires": { "getroot": "^1.0.0", - "nanoid": "^1.3.4", - "tslib": "^1.9.3", + "nanoid": "^2.0.3", "unixify": "^1.0.0" - }, - "dependencies": { - "nanoid": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-1.3.4.tgz", - "integrity": "sha512-4ug4BsuHxiVHoRUe1ud6rUFT3WUMmjXt1W0quL0CviZQANdan7D8kqN5/maw53hmAApY/jfzMRkC57BNNs60ZQ==" - } } } } @@ -14525,9 +14514,9 @@ "integrity": "sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw==" }, "nanoid": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.0.3.tgz", - "integrity": "sha512-NbaoqdhIYmY6FXDRB4eYtDVC9Z9eCbn8TyaiC16LNKtpPv/aqa0tOPD8y6gNE4yUNnaZ7LLhYtXOev/6+cBtfw==" + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.1.tgz", + "integrity": "sha512-0YbJdaL4JFoejIOoawgLcYValFGJ2iyUuVDIWL3g8Es87SSOWFbWdRUMV3VMSiyPs3SQ3QxCIxFX00q5DLkMCw==" }, "nanomatch": { "version": "1.2.13", diff --git a/package.json b/package.json index 2814df2ab..09874bc59 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "du": "^0.1.0", "electron-dl": "1.12.0", "electron-fetch": "1.3.0", - "electron-hunspell": "1.0.0-beta.7", + "electron-hunspell": "1.0.0", "electron-is-dev": "1.0.1", "electron-react-titlebar": "0.8.1", "electron-updater": "4.0.6", diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index 64575753f..d7bb983dc 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js @@ -1,6 +1,5 @@ import { webFrame } from 'electron'; import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell'; -import { ENVIRONMENT } from 'hunspell-asm'; import path from 'path'; import { readFileSync } from 'fs'; @@ -14,6 +13,8 @@ let currentDict; 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}`); @@ -24,7 +25,7 @@ async function loadDictionary(locale) { } } -export async function switchDict(locale) { +export async function switchDict(locale = DEFAULT_LOCALE) { try { debug('Trying to load dictionary', locale); @@ -55,13 +56,23 @@ export async function switchDict(locale) { } } -export default async function initialize(languageCode = 'en-us') { +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) { try { provider = new SpellCheckerProvider(); - const locale = languageCode.toLowerCase(); + const locale = getSpellcheckerLocaleByFuzzyIdentifier(languageCode); debug('Init spellchecker'); - await provider.initialize({ environment: ENVIRONMENT.NODE }); + await provider.initialize(); debug('Attaching spellcheck provider'); attached = await attachSpellCheckProvider(provider); @@ -69,7 +80,6 @@ export default async function initialize(languageCode = 'en-us') { debug('Available spellchecker dictionaries', provider.availableDictionaries); attached.switchLanguage(locale); - console.log('seas oida', attached, provider); return provider; } catch (err) { @@ -89,13 +99,3 @@ export function disable() { 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-70-g09d2 From cc038831ebea767e9a39354c9f5984d874b08377 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 27 Sep 2019 14:54:15 +0200 Subject: fix(Spell checking): Fix "undefined" language in context menu --- src/webview/contextMenu.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/webview') diff --git a/src/webview/contextMenu.js b/src/webview/contextMenu.js index 2fdbcdca6..d3b976554 100644 --- a/src/webview/contextMenu.js +++ b/src/webview/contextMenu.js @@ -255,9 +255,9 @@ const buildMenuTpl = (props, suggestions, isSpellcheckEnabled, defaultSpellcheck }, { id: 'resetToDefault', - label: `Reset to system default (${SPELLCHECKER_LOCALES[defaultSpellcheckerLanguage]})`, + label: `Reset to system default (${defaultSpellcheckerLanguage === 'automatic' ? 'Automatic' : SPELLCHECKER_LOCALES[defaultSpellcheckerLanguage]})`, type: 'radio', - visible: defaultSpellcheckerLanguage !== spellcheckerLanguage, + visible: defaultSpellcheckerLanguage !== spellcheckerLanguage || (defaultSpellcheckerLanguage !== 'automatic' && spellcheckerLanguage === 'automatic'), click() { debug('Resetting service spellchecker to system default'); ipcRenderer.sendToHost('set-service-spellchecker-language', 'reset'); -- cgit v1.2.3-70-g09d2 From 734732f1f901093ec78b166413aa3315f6f61248 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Wed, 2 Oct 2019 10:45:25 +0200 Subject: fix(Spell check): Fix spell checker to initialize without loaded dictionary --- src/webview/spellchecker.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/webview') diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index d7bb983dc..06cbd283a 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js @@ -18,8 +18,8 @@ const DEFAULT_LOCALE = 'en-us'; async function loadDictionary(locale) { try { const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); - await provider.loadDictionary(locale, readFileSync(`${fileLocation}.dic`), readFileSync(`${fileLocation}.aff`)); 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); } @@ -44,8 +44,8 @@ export async function switchDict(locale = DEFAULT_LOCALE) { if (currentDict) { provider.unloadDictionary(locale); } - loadDictionary(locale); - attached.switchLanguage(locale); + await loadDictionary(locale); + await attached.switchLanguage(locale); debug('Switched dictionary to', locale); @@ -77,9 +77,11 @@ export default async function initialize(languageCode = DEFAULT_LOCALE) { debug('Attaching spellcheck provider'); attached = await attachSpellCheckProvider(provider); - debug('Available spellchecker dictionaries', provider.availableDictionaries); + const availableDictionaries = await provider.getAvailableDictionaries(); - attached.switchLanguage(locale); + debug('Available spellchecker dictionaries', availableDictionaries); + + await switchDict(locale); return provider; } catch (err) { -- cgit v1.2.3-70-g09d2