From 0c50d7d5537ce01618d45eaff8d51cd1a14529a0 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Wed, 27 Sep 2023 11:39:51 -0700 Subject: [PATCH] DEV: Improvements to context menu (#232) --- .../after-d-editor/ai-helper-context-menu.js | 6 +++++- spec/system/ai_helper/ai_composer_helper_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 f8d9269d..8f344a8c 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 @@ -42,6 +42,7 @@ export default class AiHelperContextMenu extends Component { }; prompts = []; promptTypes = {}; + minSelectionChars = 3; @tracked _menuState = this.CONTEXT_MENU_STATES.triggers; @tracked _popper; @@ -107,7 +108,6 @@ export default class AiHelperContextMenu extends Component { @bind selectionChanged() { if (document.activeElement !== this._dEditorInput) { - this.closeContextMenu(); return; } @@ -129,6 +129,10 @@ export default class AiHelperContextMenu extends Component { return; } + if (this.selectedText?.length < this.minSelectionChars) { + return; + } + this._onSelectionChanged(); } diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index 48c8ae87..9bd39f81 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -37,6 +37,16 @@ RSpec.describe "AI Composer helper", type: :system, js: true do expect(ai_helper_context_menu).to have_context_menu end + it "does not show the context menu when selecting insuffient text" do + visit("/latest") + page.find("#create-topic").click + composer.fill_content(OpenAiCompletionsInferenceStubs.translated_response) + page.execute_script( + "const input = document.querySelector('.d-editor-input'); input.setSelectionRange(0, 2);", + ) + expect(ai_helper_context_menu).to have_no_context_menu + end + it "shows context menu in 'trigger' state when first showing" do trigger_context_menu(OpenAiCompletionsInferenceStubs.translated_response) expect(ai_helper_context_menu).to be_showing_triggers