diff options
author | vantezzen <hello@vantezzen.io> | 2019-10-19 22:25:40 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2019-10-19 22:25:40 +0200 |
commit | 64824a692f566a01cc208dd82239ad643b69093e (patch) | |
tree | 37ef1a1cfd30333a1d5d169a9f1febc973c9f532 /src/webview | |
parent | Merge pull request #134 from getferdi/l10n_develop (diff) | |
download | ferdium-app-64824a692f566a01cc208dd82239ad643b69093e.tar.gz ferdium-app-64824a692f566a01cc208dd82239ad643b69093e.tar.zst ferdium-app-64824a692f566a01cc208dd82239ad643b69093e.zip |
Use electron-spellchecker instead of electron-hunspell
Diffstat (limited to 'src/webview')
-rw-r--r-- | src/webview/spellchecker.js | 78 |
1 files changed, 29 insertions, 49 deletions
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 @@ | |||
1 | import { webFrame } from 'electron'; | 1 | import { webFrame } from 'electron'; |
2 | import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell'; | 2 | import {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} from 'electron-spellchecker'; |
3 | import path from 'path'; | ||
4 | import { readFileSync } from 'fs'; | ||
5 | 3 | ||
6 | import { DICTIONARY_PATH } from '../config'; | ||
7 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; | 4 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; |
8 | 5 | ||
9 | const debug = require('debug')('Ferdi:spellchecker'); | 6 | const debug = require('debug')('Franz:spellchecker'); |
10 | 7 | ||
11 | let provider; | 8 | let handler; |
12 | let currentDict; | 9 | let currentDict; |
10 | let contextMenuBuilder; | ||
13 | let _isEnabled = false; | 11 | let _isEnabled = false; |
14 | let attached; | ||
15 | 12 | ||
16 | const DEFAULT_LOCALE = 'en-us'; | 13 | export async function switchDict(locale) { |
17 | |||
18 | async function loadDictionary(locale) { | ||
19 | try { | ||
20 | const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); | ||
21 | debug('Loaded dictionary', locale, 'from', fileLocation); | ||
22 | return provider.loadDictionary(locale, readFileSync(`${fileLocation}.dic`), readFileSync(`${fileLocation}.aff`)); | ||
23 | } catch (err) { | ||
24 | console.error('Could not load dictionary', err); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | export async function switchDict(locale = DEFAULT_LOCALE) { | ||
29 | try { | 14 | try { |
30 | debug('Trying to load dictionary', locale); | 15 | debug('Trying to load dictionary', locale); |
31 | 16 | ||
32 | if (!provider) { | 17 | if (!handler) { |
33 | console.warn('SpellcheckProvider not initialized'); | 18 | console.warn('SpellcheckHandler not initialized'); |
34 | 19 | ||
35 | return; | 20 | return; |
36 | } | 21 | } |
@@ -41,11 +26,7 @@ export async function switchDict(locale = DEFAULT_LOCALE) { | |||
41 | return; | 26 | return; |
42 | } | 27 | } |
43 | 28 | ||
44 | if (currentDict) { | 29 | handler.switchLanguage(locale); |
45 | provider.unloadDictionary(locale); | ||
46 | } | ||
47 | await loadDictionary(locale); | ||
48 | await attached.switchLanguage(locale); | ||
49 | 30 | ||
50 | debug('Switched dictionary to', locale); | 31 | debug('Switched dictionary to', locale); |
51 | 32 | ||
@@ -56,34 +37,22 @@ export async function switchDict(locale = DEFAULT_LOCALE) { | |||
56 | } | 37 | } |
57 | } | 38 | } |
58 | 39 | ||
59 | export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { | 40 | export default async function initialize(languageCode = 'en-us') { |
60 | const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); | ||
61 | |||
62 | if (locales.length >= 1) { | ||
63 | return locales[0]; | ||
64 | } | ||
65 | |||
66 | return null; | ||
67 | } | ||
68 | |||
69 | export default async function initialize(languageCode = DEFAULT_LOCALE) { | ||
70 | try { | 41 | try { |
71 | provider = new SpellCheckerProvider(); | 42 | handler = new SpellCheckHandler(); |
72 | const locale = getSpellcheckerLocaleByFuzzyIdentifier(languageCode); | 43 | handler.attachToInput(); |
44 | const locale = languageCode.toLowerCase(); | ||
73 | 45 | ||
74 | debug('Init spellchecker'); | 46 | debug('Init spellchecker'); |
75 | await provider.initialize(); | ||
76 | |||
77 | debug('Attaching spellcheck provider'); | ||
78 | attached = await attachSpellCheckProvider(provider); | ||
79 | |||
80 | const availableDictionaries = await provider.getAvailableDictionaries(); | ||
81 | 47 | ||
82 | debug('Available spellchecker dictionaries', availableDictionaries); | 48 | switchDict(locale); |
83 | 49 | ||
84 | await switchDict(locale); | 50 | contextMenuBuilder = new ContextMenuBuilder(handler); |
51 | new ContextMenuListener((info) => { | ||
52 | contextMenuBuilder.showPopupMenu(info); | ||
53 | }); | ||
85 | 54 | ||
86 | return provider; | 55 | return handler; |
87 | } catch (err) { | 56 | } catch (err) { |
88 | console.error(err); | 57 | console.error(err); |
89 | return false; | 58 | return false; |
@@ -96,8 +65,19 @@ export function isEnabled() { | |||
96 | 65 | ||
97 | export function disable() { | 66 | export function disable() { |
98 | if (isEnabled()) { | 67 | if (isEnabled()) { |
68 | handler.unsubscribe(); | ||
99 | webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true }); | 69 | webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true }); |
100 | _isEnabled = false; | 70 | _isEnabled = false; |
101 | currentDict = null; | 71 | currentDict = null; |
102 | } | 72 | } |
103 | } | 73 | } |
74 | |||
75 | export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { | ||
76 | const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase()); | ||
77 | |||
78 | if (locales.length >= 1) { | ||
79 | return locales[0]; | ||
80 | } | ||
81 | |||
82 | return null; | ||
83 | } | ||