aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/spellchecker.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/webview/spellchecker.js')
-rw-r--r--src/webview/spellchecker.js77
1 files changed, 29 insertions, 48 deletions
diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js
index d8f71f61f..47fc6dcd1 100644
--- a/src/webview/spellchecker.js
+++ b/src/webview/spellchecker.js
@@ -1,36 +1,21 @@
1import { webFrame } from 'electron'; 1import { webFrame } from 'electron';
2import { attachSpellCheckProvider, SpellCheckerProvider } from 'electron-hunspell'; 2import { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker';
3import path from 'path';
4import { readFileSync } from 'fs';
5 3
6import { DICTIONARY_PATH } from '../config';
7import { SPELLCHECKER_LOCALES } from '../i18n/languages'; 4import { SPELLCHECKER_LOCALES } from '../i18n/languages';
8 5
9const debug = require('debug')('Franz:spellchecker'); 6const debug = require('debug')('Franz:spellchecker');
10 7
11let provider; 8let handler;
12let currentDict; 9let currentDict;
10let contextMenuBuilder;
13let _isEnabled = false; 11let _isEnabled = false;
14let attached;
15 12
16const DEFAULT_LOCALE = 'en-us'; 13export async function switchDict(locale) {
17
18async 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
28export 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,23 @@ export async function switchDict(locale = DEFAULT_LOCALE) {
56 } 37 }
57} 38}
58 39
59export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) { 40export 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
69export 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 // eslint-disable-next-line no-new
52 new ContextMenuListener((info) => {
53 contextMenuBuilder.showPopupMenu(info);
54 });
85 55
86 return provider; 56 return handler;
87 } catch (err) { 57 } catch (err) {
88 console.error(err); 58 console.error(err);
89 return false; 59 return false;
@@ -96,8 +66,19 @@ export function isEnabled() {
96 66
97export function disable() { 67export function disable() {
98 if (isEnabled()) { 68 if (isEnabled()) {
69 handler.unsubscribe();
99 webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true }); 70 webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true });
100 _isEnabled = false; 71 _isEnabled = false;
101 currentDict = null; 72 currentDict = null;
102 } 73 }
103} 74}
75
76export function getSpellcheckerLocaleByFuzzyIdentifier(identifier) {
77 const locales = Object.keys(SPELLCHECKER_LOCALES).filter(key => key === identifier.toLowerCase() || key.split('-')[0] === identifier.toLowerCase());
78
79 if (locales.length >= 1) {
80 return locales[0];
81 }
82
83 return null;
84}