summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/InlineSpellCheckerContent.sys.mjs
blob: b910a5430424ebb1940fae6955c1c77ca7a52a64 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* vim: set ts=2 sw=2 sts=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import {
  InlineSpellChecker,
  SpellCheckHelper,
} from "resource://gre/modules/InlineSpellChecker.sys.mjs";

export var InlineSpellCheckerContent = {
  _spellChecker: null,
  _actor: null,

  async initContextMenu(event, editFlags, actor) {
    this._actor = actor;
    this._actor.registerDestructionObserver(this);

    let spellChecker;
    if (!(editFlags & (SpellCheckHelper.TEXTAREA | SpellCheckHelper.INPUT))) {
      // Get the editor off the window.
      let win = event.target.ownerGlobal;
      let editingSession = win.docShell.editingSession;
      spellChecker = this._spellChecker = new InlineSpellChecker(
        editingSession.getEditorForWindow(win)
      );
    } else {
      // Use the element's editor.
      spellChecker = this._spellChecker = new InlineSpellChecker(
        event.composedTarget.editor
      );
    }

    this._spellChecker.initFromEvent(event.rangeParent, event.rangeOffset);

    if (!spellChecker.canSpellCheck) {
      return {
        canSpellCheck: false,
        initialSpellCheckPending: true,
        enableRealTimeSpell: false,
      };
    }

    if (!spellChecker.mInlineSpellChecker.enableRealTimeSpell) {
      return {
        canSpellCheck: true,
        initialSpellCheckPending: spellChecker.initialSpellCheckPending,
        enableRealTimeSpell: false,
      };
    }

    if (spellChecker.initialSpellCheckPending) {
      return {
        canSpellCheck: true,
        initialSpellCheckPending: true,
        enableRealTimeSpell: true,
      };
    }

    let realSpellChecker = spellChecker.mInlineSpellChecker.spellChecker;
    let dictionaryList = realSpellChecker.GetDictionaryList();
    let spellSuggestions = await this._generateSpellSuggestions();

    return {
      canSpellCheck: spellChecker.canSpellCheck,
      initialSpellCheckPending: spellChecker.initialSpellCheckPending,
      enableRealTimeSpell: spellChecker.enabled,
      overMisspelling: spellChecker.overMisspelling,
      misspelling: spellChecker.mMisspelling,
      spellSuggestions,
      currentDictionaries:
        spellChecker.mInlineSpellChecker.spellChecker.getCurrentDictionaries(),
      dictionaryList,
    };
  },

  uninitContextMenu() {
    if (this._actor) {
      this._actor.unregisterDestructionObserver(this);
    }
    this._actor = null;
    this._spellChecker = null;
  },

  actorDestroyed() {
    this.uninitContextMenu();
  },

  async _generateSpellSuggestions() {
    let spellChecker = this._spellChecker.mInlineSpellChecker.spellChecker;
    let suggestions = null;
    try {
      suggestions = await spellChecker.suggest(
        this._spellChecker.mMisspelling,
        5
      );
    } catch (e) {
      return [];
    }

    return suggestions;
  },

  selectDictionaries(localeCodes) {
    this._spellChecker.selectDictionaries(localeCodes);
  },

  replaceMisspelling(suggestion) {
    this._spellChecker.replaceMisspelling(suggestion);
  },

  toggleEnabled() {
    this._spellChecker.toggleEnabled();
  },

  recheck() {
    this._spellChecker.mInlineSpellChecker.enableRealTimeSpell = true;
  },
};