better autocomplete keyboard interaction. see #157

This commit is contained in:
mic 2024-10-15 22:14:54 +02:00
parent a5600cdf75
commit 57e45b3df8

View file

@ -701,6 +701,9 @@ function textareaAutocomplete(textarea, suggestions) {
});
textarea.addEventListener('keydown', (e) => {
if (autocompleteList.classList.contains('hidden')) {
return;
}
const items = autocompleteList.querySelectorAll('li');
if (items.length > 0) {
if (e.key === 'ArrowDown') {
@ -713,6 +716,7 @@ function textareaAutocomplete(textarea, suggestions) {
updateActiveSuggestion(items, activeIndex);
} else if (e.key === 'Enter') {
e.preventDefault();
if(activeIndex === -1) activeIndex = 0;
if (activeIndex >= 0 && activeIndex < items.length) {
insertAutocomplete(items[activeIndex].textContent, textarea);
hideSuggestions(autocompleteList);