aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/spellchecker.js
blob: 5704779d7aec5b6d5c3b73ff6ff2f672ba7fe824 (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
import { SpellCheckHandler } from 'electron-spellchecker';

import { isMac } from '../environment';

export default class Spellchecker {
  isInitialized = false;
  handler = null;
  initRetries = 0;

  initialize() {
    this.handler = new SpellCheckHandler();

    if (!isMac) {
      this.attach();
    } else {
      this.isInitialized = true;
    }
  }

  attach() {
    let initFailed = false;

    if (this.initRetries > 3) {
      console.error('Could not initialize spellchecker');
      return;
    }

    try {
      this.handler.attachToInput();
      this.handler.switchLanguage(navigator.language);
    } catch (err) {
      initFailed = true;
      this.initRetries = +1;
      setTimeout(() => { this.attach(); console.warn('Spellchecker init failed, trying again in 5s'); }, 5000);
    }

    if (!initFailed) {
      this.isInitialized = true;
    }
  }
}