diff --git a/src/elements/prompt.js b/src/elements/prompt.js index 4a570a3c7..65f6ea7e7 100644 --- a/src/elements/prompt.js +++ b/src/elements/prompt.js @@ -162,10 +162,18 @@ export class LexicalPromptElement extends HTMLElement { const showId = ++this.showPopoverId this.popoverElement ??= await this.#buildPopover() if (this.showPopoverId !== showId) return + if (!this.#isTriggerPresent()) { + this.#addTriggerListener() + return + } this.#resetPopoverPosition() await this.#filterOptions() if (this.showPopoverId !== showId) return + if (!this.#isTriggerPresent()) { + this.#addTriggerListener() + return + } this.popoverElement.classList.toggle("lexxy-prompt-menu--visible", true) this.#selectFirstOption() @@ -485,6 +493,10 @@ export class LexicalPromptElement extends HTMLElement { this.disconnectedCallback() this.connectedCallback() } + + #isTriggerPresent() { + return this.#editorContents.containsTextBackUntil(this.trigger) + } } export default LexicalPromptElement diff --git a/test/dummy/app/controllers/people_controller.rb b/test/dummy/app/controllers/people_controller.rb index c998115a7..8d6045967 100644 --- a/test/dummy/app/controllers/people_controller.rb +++ b/test/dummy/app/controllers/people_controller.rb @@ -1,5 +1,7 @@ class PeopleController < ApplicationController def index + sleep params[:delay].to_f if params[:delay].present? + @people = Person.all if params[:filter].present? diff --git a/test/dummy/app/views/posts/_form.html.erb b/test/dummy/app/views/posts/_form.html.erb index fc7e59dbc..1aa040f76 100644 --- a/test/dummy/app/views/posts/_form.html.erb +++ b/test/dummy/app/views/posts/_form.html.erb @@ -53,6 +53,11 @@ + <% if params[:slow_prompt] %> + + + <% end %> + diff --git a/test/dummy/app/views/sandbox/show.html.erb b/test/dummy/app/views/sandbox/show.html.erb index b4b35022e..e547c3db0 100644 --- a/test/dummy/app/views/sandbox/show.html.erb +++ b/test/dummy/app/views/sandbox/show.html.erb @@ -20,7 +20,7 @@
<%= rich_text_area_tag :sandbox, render(@template), placeholder: "Write something...", data: { lexxy_output_target: "editor" } do %> - + <%= render "emoji_prompt", trigger: ":" %> <% end %>
diff --git a/test/system/prompts_test.rb b/test/system/prompts_test.rb index edac3893b..7a0927c85 100644 --- a/test/system/prompts_test.rb +++ b/test/system/prompts_test.rb @@ -167,6 +167,21 @@ class ActionTextLoadTest < ApplicationSystemTestCase assert_no_selector %(action-text-attachment[content-type="application/vnd.actiontext.mention"]) end + test "removing trigger during slow remote load does not cause errors" do + visit edit_post_path(posts(:empty), slow_prompt: true) + + # Quickly remove the trigger + find_editor.send "4" + sleep 0.2 + find_editor.send :backspace + + # Editor should remain functional after deleting the trigger mid-load + # No console errors should occur, and we should be able to type and see the text in the editor + wait_until { !find_editor.open_prompt? } + find_editor.send "Hello" + find_editor.within_contents { assert_text "Hello" } + end + private def start_bold_prompt_insertion find_editor.toggle_command("bold")