aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar vantezzen <hello@vantezzen.io>2019-10-19 22:25:40 +0200
committerLibravatar vantezzen <hello@vantezzen.io>2019-10-19 22:25:40 +0200
commit64824a692f566a01cc208dd82239ad643b69093e (patch)
tree37ef1a1cfd30333a1d5d169a9f1febc973c9f532 /src
parentMerge pull request #134 from getferdi/l10n_develop (diff)
downloadferdium-app-64824a692f566a01cc208dd82239ad643b69093e.tar.gz
ferdium-app-64824a692f566a01cc208dd82239ad643b69093e.tar.zst
ferdium-app-64824a692f566a01cc208dd82239ad643b69093e.zip
Use electron-spellchecker instead of electron-hunspell
Diffstat (limited to 'src')
-rw-r--r--src/config.js1
-rw-r--r--src/i18n/locales/zh-Hant.json4
-rw-r--r--src/webview/spellchecker.js78
3 files changed, 29 insertions, 54 deletions
diff --git a/src/config.js b/src/config.js
index b1dbab1ff..e762f879f 100644
--- a/src/config.js
+++ b/src/config.js
@@ -116,7 +116,6 @@ export const LOCAL_SERVER = 'You are using Ferdi without a server';
116export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config'); 116export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config');
117 117
118// Replacing app.asar is not beautiful but unforunately necessary 118// Replacing app.asar is not beautiful but unforunately necessary
119export const DICTIONARY_PATH = asarPath(path.join(__dirname, 'dictionaries'));
120export const RECIPES_PATH = asarPath(path.join(__dirname, 'recipes')); 119export const RECIPES_PATH = asarPath(path.join(__dirname, 'recipes'));
121 120
122export const ALLOWED_PROTOCOLS = [ 121export const ALLOWED_PROTOCOLS = [
diff --git a/src/i18n/locales/zh-Hant.json b/src/i18n/locales/zh-Hant.json
index c64830574..678554c05 100644
--- a/src/i18n/locales/zh-Hant.json
+++ b/src/i18n/locales/zh-Hant.json
@@ -54,7 +54,6 @@
54 "locked.invalidCredentials": "Password invalid", 54 "locked.invalidCredentials": "Password invalid",
55 "locked.password.label": "Password", 55 "locked.password.label": "Password",
56 "locked.submit.label": "Unlock", 56 "locked.submit.label": "Unlock",
57 "login.changeServer": "Change server",
58 "login.customServerQuestion": "Using a custom Ferdi server?", 57 "login.customServerQuestion": "Using a custom Ferdi server?",
59 "login.customServerSuggestion": "Try importing your Franz account", 58 "login.customServerSuggestion": "Try importing your Franz account",
60 "login.email.label": "電子郵件信箱", 59 "login.email.label": "電子郵件信箱",
@@ -187,7 +186,6 @@
187 "services.getStarted": "開始使用", 186 "services.getStarted": "開始使用",
188 "services.login": "Please login to use Ferdi.", 187 "services.login": "Please login to use Ferdi.",
189 "services.serverInfo": "Optionally, you can change your Ferdi server by clicking the cog in the bottom left corner.", 188 "services.serverInfo": "Optionally, you can change your Ferdi server by clicking the cog in the bottom left corner.",
190 "services.serverless": "Use Ferdi without an Account",
191 "services.welcome": "歡迎使用 Ferdi", 189 "services.welcome": "歡迎使用 Ferdi",
192 "settings.account.account.editButton": "更改帳戶資訊", 190 "settings.account.account.editButton": "更改帳戶資訊",
193 "settings.account.accountType.basic": "基本帳戶", 191 "settings.account.accountType.basic": "基本帳戶",
@@ -356,8 +354,6 @@
356 "settings.team.headline": "Team", 354 "settings.team.headline": "Team",
357 "settings.team.intro": "You and your team use Ferdi? You can now manage Premium subscriptions for as many colleagues, friends or family members as you want, all from within one account.", 355 "settings.team.intro": "You and your team use Ferdi? You can now manage Premium subscriptions for as many colleagues, friends or family members as you want, all from within one account.",
358 "settings.team.manageAction": "Manage your Team on getferdi.com", 356 "settings.team.manageAction": "Manage your Team on getferdi.com",
359 "settings.team.teamsUnavailible": "Teams are unavailible",
360 "settings.team.teamsUnavailibleInfo": "Teams are currently only availible when using the Franz Server and after paying for Franz Professional. Please change your server to https://api.franzinfra.com to use teams.",
361 "settings.team.upgradeAction": "Upgrade your Account", 357 "settings.team.upgradeAction": "Upgrade your Account",
362 "settings.user.form.accountType.company": "公司", 358 "settings.user.form.accountType.company": "公司",
363 "settings.user.form.accountType.individual": "個人", 359 "settings.user.form.accountType.individual": "個人",
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 @@
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')('Ferdi: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,22 @@ 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 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
97export function disable() { 66export 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
75export 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}