aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/DictionaryStore.js
blob: b9c5f2abf872d84a98b83cfdc078bf011092a5f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
    }
  }
}