From 0cdc165ace53019371fc126d8d76817f1f2ed027 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 7 Dec 2018 13:47:00 +0100 Subject: fix(Spellchecker): Dictionaries are now part of app instead of dynamic download --- src/config.js | 3 ++- src/stores/DictionaryStore.js | 45 ------------------------------------------- src/stores/index.js | 2 -- src/webview/spellchecker.js | 34 +++++++++++++------------------- 4 files changed, 15 insertions(+), 69 deletions(-) delete mode 100644 src/stores/DictionaryStore.js (limited to 'src') diff --git a/src/config.js b/src/config.js index d981f9c6a..789ddd1a0 100644 --- a/src/config.js +++ b/src/config.js @@ -56,4 +56,5 @@ export const FILE_SYSTEM_SETTINGS_TYPES = [ export const SETTINGS_PATH = path.join(app.getPath('userData'), 'config'); -export const DICTIONARY_PATH = path.join(app.getPath('userData'), 'dicts'); +// Replacing app.asar is not beautiful but unforunately necessary +export const DICTIONARY_PATH = path.join(__dirname, 'dictionaries').replace('app.asar', 'app.asar.unpacked'); diff --git a/src/stores/DictionaryStore.js b/src/stores/DictionaryStore.js deleted file mode 100644 index b9c5f2abf..000000000 --- a/src/stores/DictionaryStore.js +++ /dev/null @@ -1,45 +0,0 @@ -import { observable } from 'mobx'; -import { createDownloader } from 'hunspell-dict-downloader'; - -import Store from './lib/Store'; - -import { DICTIONARY_PATH } from '../config'; - -const debug = require('debug')('Franz:DictionaryStore'); - -export default class DictionaryStore extends Store { - @observable available = [] - @observable installed = [] - - _dictDownloader = null - - constructor(...args) { - super(...args); - - this.registerReactions([ - this._downloadDictForUserLocale.bind(this), - ]); - } - - async setup() { - this._dictDownloader = await createDownloader(DICTIONARY_PATH); - debug('dicts', this._dictDownloader); - - this.available = this._dictDownloader.availableDictionaries; - this.installed = this._dictDownloader.installedDictionaries; - - if (!this.installed.includes('en-us')) { - this._dictDownloader.installDictionary('en-us'); - } - } - - _downloadDictForUserLocale() { - const spellcheckerLanguage = this.stores.settings.app.spellcheckerLanguage; - - debug('trying to Downloading dict for', spellcheckerLanguage); - if (!this.installed.includes(spellcheckerLanguage) && this.available.includes(spellcheckerLanguage) && spellcheckerLanguage !== 'en-us') { - debug('Downloading dict for', spellcheckerLanguage); - this._dictDownloader.installDictionary(spellcheckerLanguage); - } - } -} diff --git a/src/stores/index.js b/src/stores/index.js index f547d0a7a..96b844c95 100644 --- a/src/stores/index.js +++ b/src/stores/index.js @@ -9,7 +9,6 @@ import UIStore from './UIStore'; import PaymentStore from './PaymentStore'; import NewsStore from './NewsStore'; import RequestStore from './RequestStore'; -import DictionaryStore from './DictionaryStore'; import GlobalErrorStore from './GlobalErrorStore'; export default (api, actions, router) => { @@ -27,7 +26,6 @@ export default (api, actions, router) => { payment: new PaymentStore(stores, api, actions), news: new NewsStore(stores, api, actions), requests: new RequestStore(stores, api, actions), - dictionary: new DictionaryStore(stores, api, actions), globalError: new GlobalErrorStore(stores, api, actions), }); // Initialize all stores diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index b0192b7ef..ab0cc9a90 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js @@ -1,28 +1,24 @@ import { webFrame } from 'electron'; -import fs from 'fs'; -import path from 'path'; import { SpellCheckerProvider } from 'electron-hunspell'; +import path from 'path'; import { DICTIONARY_PATH } from '../config'; + const debug = require('debug')('Franz:spellchecker'); let provider; let currentDict; let _isEnabled = false; -async function loadDictionaries() { - const rawList = fs.readdirSync(DICTIONARY_PATH); - - const dicts = rawList.filter(item => !item.startsWith('.') && fs.lstatSync(path.join(DICTIONARY_PATH, item)).isDirectory()); - - debug('Found dictionaries', dicts); - - for (let i = 0; i < dicts.length; i += 1) { - const fileLocation = `${DICTIONARY_PATH}/${dicts[i]}/${dicts[i]}`; - debug('Trying to load', fileLocation); - // eslint-disable-next-line - await provider.loadDictionary(dicts[i], `${fileLocation}.dic`, `${fileLocation}.aff`); +async function loadDictionary(locale) { + try { + // Replacing app.asar is not beautiful but unforunately necessary + const fileLocation = path.join(DICTIONARY_PATH, `hunspell-dict-${locale}/${locale}`); + console.log(fileLocation, __dirname); + await provider.loadDictionary(locale, `${fileLocation}.dic`, `${fileLocation}.aff`); + } catch (err) { + console.error('Could not load dictionary', err); } } @@ -30,12 +26,6 @@ export async function switchDict(locale) { try { debug('Trying to load dictionary', locale); - if (!provider.availableDictionaries.includes(locale)) { - console.warn('Dict not available', locale); - - return; - } - if (!provider) { console.warn('SpellcheckProvider not initialized'); @@ -48,6 +38,8 @@ export async function switchDict(locale) { return; } + provider.unloadDictionary(locale); + loadDictionary(locale); provider.switchDictionary(locale); debug('Switched dictionary to', locale); @@ -66,7 +58,7 @@ export default async function initialize(languageCode = 'en-us') { debug('Init spellchecker'); await provider.initialize(); - await loadDictionaries(); + // await loadDictionaries(); debug('Available spellchecker dictionaries', provider.availableDictionaries); -- cgit v1.2.3-70-g09d2