From 3cf316fb62639dddad766c9691125856cadaaea4 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Wed, 23 Aug 2023 14:20:31 -0700 Subject: [PATCH] FIX: Context menu not appearing in Firefox (#154) --- .../after-d-editor/ai-helper-context-menu.js | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.js b/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.js index b679d832..21fa97af 100644 --- a/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.js +++ b/assets/javascripts/discourse/connectors/after-d-editor/ai-helper-context-menu.js @@ -85,12 +85,25 @@ export default class AiHelperContextMenu extends Component { } @bind - selectionChanged(event) { - if (!event.target.activeElement.classList.contains("d-editor-input")) { + selectionChanged() { + if (document.activeElement !== this._dEditorInput) { return; } - if (window.getSelection().toString().length === 0) { + const canSelect = Boolean( + window.getSelection() && + document.activeElement && + document.activeElement.value + ); + + this.selectedText = canSelect + ? document.activeElement.value.substring( + document.activeElement.selectionStart, + document.activeElement.selectionEnd + ) + : ""; + + if (this.selectedText.length === 0) { if (this.loading) { // prevent accidentally closing context menu while results loading return; @@ -100,7 +113,6 @@ export default class AiHelperContextMenu extends Component { return; } - this.selectedText = event.target.getSelection().toString(); this._onSelectionChanged(); }