Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/elements/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -485,6 +493,10 @@ export class LexicalPromptElement extends HTMLElement {
this.disconnectedCallback()
this.connectedCallback()
}

#isTriggerPresent() {
return this.#editorContents.containsTextBackUntil(this.trigger)
}
}

export default LexicalPromptElement
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<lexxy-prompt trigger="2" src="<%= people_path %>" name="mention">
</lexxy-prompt>

<% if params[:slow_prompt] %>
<lexxy-prompt trigger="4" src="<%= people_path(delay: 2) %>" name="mention">
</lexxy-prompt>
<% end %>

<lexxy-prompt trigger="3" src="<%= people_path %>" name="mention" remote-filtering insert-editable-text supports-space-in-searches>
</lexxy-prompt>

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/sandbox/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<turbo-frame id="editor" target="_top">
<div>
<%= rich_text_area_tag :sandbox, render(@template), placeholder: "Write something...", data: { lexxy_output_target: "editor" } do %>
<lexxy-prompt trigger="@" src="<%= people_path %>" name="mention"></lexxy-prompt>
<lexxy-prompt trigger="@" src="<%= people_path(delay: params[:delay]) %>" name="mention"></lexxy-prompt>
<%= render "emoji_prompt", trigger: ":" %>
<% end %>
</div>
Expand Down
15 changes: 15 additions & 0 deletions test/system/prompts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down