fix(autocomplete): prevent blur-on-click race condition causing dropped selection (#939)

Add mousedown(e.preventDefault()) on dropdown items to prevent the textarea blur event from firing before click. Without this, the blur handler's formatAutocompleteTextOnBlur() modifies text with unmatched commas (e.g. "<lora:X:1>,search") and triggers hide() via suppressAutocompleteOnce, removing the item from the DOM before the click handler can execute.

Fixes #939
This commit is contained in:
Will Miao
2026-05-22 21:50:26 +08:00
parent 3b602a3698
commit 5cd7204101

View File

@@ -1461,6 +1461,11 @@ class AutoComplete {
box-sizing: border-box;
`;
// Prevent textarea from losing focus - same fix as createItemElement
itemEl.addEventListener('mousedown', (e) => {
e.preventDefault();
});
itemEl.addEventListener('mouseenter', () => {
this.selectItem(index, { manual: true });
});
@@ -2189,6 +2194,16 @@ class AutoComplete {
item.appendChild(nameSpan);
}
// Prevent textarea from losing focus when clicking dropdown items.
// Without this, the blur event fires before click, and the blur handler's
// formatAutocompleteTextOnBlur() modifies the text and triggers hide()
// via suppressAutocompleteOnce, removing this item from the DOM before
// the click handler can execute. This specifically breaks the case where
// the text has a comma not followed by a space (e.g. "<lora:X:1>,search").
item.addEventListener('mousedown', (e) => {
e.preventDefault();
});
// Hover and selection handlers
item.addEventListener('mouseenter', () => {
this.selectItem(index, { manual: true });