Bug 1586801 - Use the contextual WalkerFront in _duplicateNode(). r=pbro
[gecko.git] / toolkit / modules / InlineSpellCheckerContent.jsm
blob7284dca69003aa6bb222c9e20e5bf2abdf843f46
1 /* vim: set ts=2 sw=2 sts=2 tw=80: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 "use strict";
8 var { InlineSpellChecker, SpellCheckHelper } = ChromeUtils.import(
9   "resource://gre/modules/InlineSpellChecker.jsm"
12 var EXPORTED_SYMBOLS = ["InlineSpellCheckerContent"];
14 var InlineSpellCheckerContent = {
15   _spellChecker: null,
16   _actor: null,
18   initContextMenu(event, editFlags, actor) {
19     this._actor = actor;
21     let spellChecker;
22     if (!(editFlags & (SpellCheckHelper.TEXTAREA | SpellCheckHelper.INPUT))) {
23       // Get the editor off the window.
24       let win = event.target.ownerGlobal;
25       let editingSession = win.docShell.editingSession;
26       spellChecker = this._spellChecker = new InlineSpellChecker(
27         editingSession.getEditorForWindow(win)
28       );
29     } else {
30       // Use the element's editor.
31       spellChecker = this._spellChecker = new InlineSpellChecker(
32         event.composedTarget.editor
33       );
34     }
36     this._spellChecker.initFromEvent(event.rangeParent, event.rangeOffset);
38     if (!spellChecker.canSpellCheck) {
39       return {
40         canSpellCheck: false,
41         initialSpellCheckPending: true,
42         enableRealTimeSpell: false,
43       };
44     }
46     if (!spellChecker.mInlineSpellChecker.enableRealTimeSpell) {
47       return {
48         canSpellCheck: true,
49         initialSpellCheckPending: spellChecker.initialSpellCheckPending,
50         enableRealTimeSpell: false,
51       };
52     }
54     if (spellChecker.initialSpellCheckPending) {
55       return {
56         canSpellCheck: true,
57         initialSpellCheckPending: true,
58         enableRealTimeSpell: true,
59       };
60     }
62     let realSpellChecker = spellChecker.mInlineSpellChecker.spellChecker;
63     let dictionaryList = realSpellChecker.GetDictionaryList();
65     return {
66       canSpellCheck: spellChecker.canSpellCheck,
67       initialSpellCheckPending: spellChecker.initialSpellCheckPending,
68       enableRealTimeSpell: spellChecker.enabled,
69       overMisspelling: spellChecker.overMisspelling,
70       misspelling: spellChecker.mMisspelling,
71       spellSuggestions: this._generateSpellSuggestions(),
72       currentDictionary: spellChecker.mInlineSpellChecker.spellChecker.GetCurrentDictionary(),
73       dictionaryList,
74     };
75   },
77   uninitContextMenu() {
78     this._actor = null;
79     this._spellChecker = null;
80   },
82   _generateSpellSuggestions() {
83     let spellChecker = this._spellChecker.mInlineSpellChecker.spellChecker;
84     try {
85       spellChecker.CheckCurrentWord(this._spellChecker.mMisspelling);
86     } catch (e) {
87       return [];
88     }
90     let suggestions = new Array(5);
91     for (let i = 0; i < 5; ++i) {
92       suggestions[i] = spellChecker.GetSuggestedWord();
93       if (suggestions[i].length === 0) {
94         suggestions.length = i;
95         break;
96       }
97     }
99     this._spellChecker.mSpellSuggestions = suggestions;
100     return suggestions;
101   },
103   selectDictionary(localeCode) {
104     this._spellChecker.selectDictionary(localeCode);
105   },
107   replaceMisspelling(index) {
108     this._spellChecker.replaceMisspelling(index);
109   },
111   toggleEnabled() {
112     this._spellChecker.toggleEnabled();
113   },
115   recheck() {
116     this._spellChecker.mInlineSpellChecker.enableRealTimeSpell = true;
117   },