diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/modules/InlineSpellCheckerContent.sys.mjs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/InlineSpellCheckerContent.sys.mjs')
-rw-r--r-- | toolkit/modules/InlineSpellCheckerContent.sys.mjs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/toolkit/modules/InlineSpellCheckerContent.sys.mjs b/toolkit/modules/InlineSpellCheckerContent.sys.mjs new file mode 100644 index 0000000000..b910a54304 --- /dev/null +++ b/toolkit/modules/InlineSpellCheckerContent.sys.mjs @@ -0,0 +1,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; + }, +}; |