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.js50
1 files changed, 18 insertions, 32 deletions
diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js
index a33a506b2..287c9cf11 100644
--- a/src/webview/spellchecker.js
+++ b/src/webview/spellchecker.js
@@ -1,53 +1,42 @@
1import { webFrame } from 'electron'; 1import { webFrame } from 'electron';
2import { SpellCheckHandler } from 'electron-spellchecker';
3import { SPELLCHECKER_LOCALES } from '../i18n/languages'; 2import { SPELLCHECKER_LOCALES } from '../i18n/languages';
4import setupContextMenu from './contextMenu'; 3import setupContextMenu from './contextMenu';
5 4
6const debug = require('debug')('Franz:spellchecker'); 5const debug = require('debug')('Franz:spellchecker');
7 6
8let handler;
9let currentDict;
10let _isEnabled = false; 7let _isEnabled = false;
11 8
12export async function switchDict(locale) { 9export async function switchDict(locales) {
13 try { 10 const { platform } = process;
14 debug('Trying to load dictionary', locale); 11 if (platform === 'darwin') {
15 12 // MacOS uses the build-in languages which cannot be changed
16 if (!handler) { 13 return;
17 console.warn('SpellcheckHandler not initialized'); 14 }
18
19 return;
20 }
21
22 if (locale === currentDict) {
23 console.warn('Dictionary is already used', currentDict);
24 15
25 return; 16 try {
26 } 17 debug('Trying to load dictionary', locales);
27 18
28 handler.switchLanguage(locale); 19 webFrame.session.setSpellCheckerLanguages([...locales, 'en-US']);
29 20
30 debug('Switched dictionary to', locale); 21 debug('Switched dictionary to', locales);
31 22
32 currentDict = locale;
33 _isEnabled = true; 23 _isEnabled = true;
34 } catch (err) { 24 } catch (err) {
35 console.error(err); 25 console.error(err);
36 } 26 }
37} 27}
38 28
39export default async function initialize(languageCode = 'en-us') { 29export default async function initialize(languages = ['en-us']) {
40 try { 30 try {
41 handler = new SpellCheckHandler();
42 setTimeout(() => handler.attachToInput(), 1000);
43 const locale = languageCode.toLowerCase();
44
45 debug('Init spellchecker'); 31 debug('Init spellchecker');
46 32
47 switchDict(locale); 33 switchDict([
48 setupContextMenu(handler); 34 navigator.language,
35 ...languages,
36 ]);
37 setupContextMenu();
49 38
50 return handler; 39 return true;
51 } catch (err) { 40 } catch (err) {
52 console.error(err); 41 console.error(err);
53 return false; 42 return false;
@@ -60,10 +49,7 @@ export function isEnabled() {
60 49
61export function disable() { 50export function disable() {
62 if (isEnabled()) { 51 if (isEnabled()) {
63 handler.unsubscribe(); 52 // TODO: How to disable build-in spellchecker?
64 webFrame.setSpellCheckProvider(currentDict, { spellCheck: () => true });
65 _isEnabled = false;
66 currentDict = null;
67 } 53 }
68} 54}
69 55