From cee1b3d275b6076c6f15703608d4dec66298f818 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 4 Mar 2024 13:33:31 -0800 Subject: [PATCH] FIX: Backspace in composer custom prompt closes menu (#505) --- .../after-d-editor/ai-helper-context-menu.js | 7 +++++-- spec/system/ai_helper/ai_composer_helper_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 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 193ee32d..6bb40500 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 @@ -154,8 +154,11 @@ export default class AiHelperContextMenu extends Component { if (event.key === "Escape") { return this.closeContextMenu(); } - - if (event.key === "Backspace" && this.selectedText) { + if ( + event.key === "Backspace" && + this.selectedText && + this.menuState === this.CONTEXT_MENU_STATES.triggers + ) { return this.closeContextMenu(); } } diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index d0edaf48..4a727d00 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -65,6 +65,13 @@ RSpec.describe "AI Composer helper", type: :system, js: true do expect(ai_helper_context_menu).to have_no_context_menu end + it "closes the context menu when selected text is deleted" do + trigger_context_menu(input) + expect(ai_helper_context_menu).to have_context_menu + page.send_keys(:backspace) + expect(ai_helper_context_menu).to have_no_context_menu + end + context "when using custom prompt" do let(:mode) { CompletionPrompt::CUSTOM_PROMPT } @@ -98,6 +105,15 @@ RSpec.describe "AI Composer helper", type: :system, js: true do expect(composer.composer_input.value).to eq(custom_prompt_response) end end + + it "should not close the context menu if backspace is pressed" do + trigger_context_menu(input) + ai_helper_context_menu.click_ai_button + expect(ai_helper_context_menu).to have_context_menu + ai_helper_context_menu.fill_custom_prompt(custom_prompt_input) + page.find(".ai-custom-prompt__input").send_keys(:backspace) + expect(ai_helper_context_menu).to have_context_menu + end end context "when not a member of custom prompt group" do