2014-10-03 18:52:37 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2017-01-09 13:12:26 +03:00
|
|
|
var { InlineSpellChecker, SpellCheckHelper } = ChromeUtils.import(
|
2019-01-17 21:18:31 +03:00
|
|
|
"resource://gre/modules/InlineSpellChecker.jsm"
|
|
|
|
);
|
2014-10-03 18:52:37 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["InlineSpellCheckerContent"];
|
2014-10-03 18:52:37 +04:00
|
|
|
|
|
|
|
var InlineSpellCheckerContent = {
|
|
|
|
_spellChecker: null,
|
2019-06-08 04:09:46 +03:00
|
|
|
_actor: null,
|
2014-10-03 18:52:37 +04:00
|
|
|
|
2019-06-08 04:09:46 +03:00
|
|
|
initContextMenu(event, editFlags, actor) {
|
|
|
|
this._actor = actor;
|
2014-10-03 18:52:37 +04:00
|
|
|
|
|
|
|
let spellChecker;
|
|
|
|
if (!(editFlags & (SpellCheckHelper.TEXTAREA | SpellCheckHelper.INPUT))) {
|
|
|
|
// Get the editor off the window.
|
2017-01-27 12:51:03 +03:00
|
|
|
let win = event.target.ownerGlobal;
|
2018-08-02 22:15:27 +03:00
|
|
|
let editingSession = win.docShell.editingSession;
|
2014-10-03 18:52:37 +04:00
|
|
|
spellChecker = this._spellChecker = new InlineSpellChecker(
|
|
|
|
editingSession.getEditorForWindow(win)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Use the element's editor.
|
|
|
|
spellChecker = this._spellChecker = new InlineSpellChecker(
|
2019-06-03 12:34:44 +03:00
|
|
|
event.composedTarget.editor
|
|
|
|
);
|
2014-10-03 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2017-10-15 21:50:30 +03:00
|
|
|
this._spellChecker.initFromEvent(event.rangeParent, event.rangeOffset);
|
2014-10-03 18:52:37 +04:00
|
|
|
|
|
|
|
if (!spellChecker.canSpellCheck) {
|
|
|
|
return {
|
|
|
|
canSpellCheck: false,
|
|
|
|
initialSpellCheckPending: true,
|
|
|
|
enableRealTimeSpell: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!spellChecker.mInlineSpellChecker.enableRealTimeSpell) {
|
|
|
|
return {
|
|
|
|
canSpellCheck: true,
|
|
|
|
initialSpellCheckPending: spellChecker.initialSpellCheckPending,
|
|
|
|
enableRealTimeSpell: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-31 02:15:11 +03:00
|
|
|
if (spellChecker.initialSpellCheckPending) {
|
|
|
|
return {
|
|
|
|
canSpellCheck: true,
|
|
|
|
initialSpellCheckPending: true,
|
|
|
|
enableRealTimeSpell: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-10-03 18:52:37 +04:00
|
|
|
let realSpellChecker = spellChecker.mInlineSpellChecker.spellChecker;
|
2019-06-05 07:07:28 +03:00
|
|
|
let dictionaryList = realSpellChecker.GetDictionaryList();
|
2014-10-03 18:52:37 +04:00
|
|
|
|
|
|
|
return {
|
|
|
|
canSpellCheck: spellChecker.canSpellCheck,
|
|
|
|
initialSpellCheckPending: spellChecker.initialSpellCheckPending,
|
|
|
|
enableRealTimeSpell: spellChecker.enabled,
|
|
|
|
overMisspelling: spellChecker.overMisspelling,
|
|
|
|
misspelling: spellChecker.mMisspelling,
|
|
|
|
spellSuggestions: this._generateSpellSuggestions(),
|
|
|
|
currentDictionary: spellChecker.mInlineSpellChecker.spellChecker.GetCurrentDictionary(),
|
2019-06-05 07:07:28 +03:00
|
|
|
dictionaryList,
|
|
|
|
};
|
2014-10-03 18:52:37 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
uninitContextMenu() {
|
2019-06-08 04:09:46 +03:00
|
|
|
this._actor = null;
|
2014-10-03 18:52:37 +04:00
|
|
|
this._spellChecker = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_generateSpellSuggestions() {
|
|
|
|
let spellChecker = this._spellChecker.mInlineSpellChecker.spellChecker;
|
|
|
|
try {
|
|
|
|
spellChecker.CheckCurrentWord(this._spellChecker.mMisspelling);
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let suggestions = new Array(5);
|
|
|
|
for (let i = 0; i < 5; ++i) {
|
|
|
|
suggestions[i] = spellChecker.GetSuggestedWord();
|
|
|
|
if (suggestions[i].length === 0) {
|
|
|
|
suggestions.length = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._spellChecker.mSpellSuggestions = suggestions;
|
|
|
|
return suggestions;
|
|
|
|
},
|
|
|
|
|
2019-06-08 04:09:46 +03:00
|
|
|
selectDictionary(localeCode) {
|
|
|
|
this._spellChecker.selectDictionary(localeCode);
|
2019-06-07 17:28:59 +03:00
|
|
|
},
|
2019-06-07 06:15:16 +03:00
|
|
|
|
2019-06-08 04:09:46 +03:00
|
|
|
replaceMisspelling(index) {
|
|
|
|
this._spellChecker.replaceMisspelling(index);
|
|
|
|
},
|
2019-06-07 19:19:14 +03:00
|
|
|
|
2019-06-08 04:09:46 +03:00
|
|
|
toggleEnabled() {
|
|
|
|
this._spellChecker.toggleEnabled();
|
|
|
|
},
|
2019-06-07 19:19:14 +03:00
|
|
|
|
2019-06-08 04:09:46 +03:00
|
|
|
recheck() {
|
|
|
|
this._spellChecker.mInlineSpellChecker.enableRealTimeSpell = true;
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2014-10-03 18:52:37 +04:00
|
|
|
};
|