From 29d6ee45de6f9e6908cfc4201608b6ac39276d43 Mon Sep 17 00:00:00 2001 From: LenAnderson Date: Fri, 5 Apr 2024 11:45:50 -0400 Subject: [PATCH] improve autocomplete show/hide logic and editor selection --- public/scripts/slash-commands.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 95bef534a..b635c6291 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2104,7 +2104,7 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) { else { const helpStrings = Object .keys(parser.commands) // Get all slash commands - .filter(it => executor.name == '' || isReplacable || isForced ? matchers[matchType](it) : it.toLowerCase() == slashCommand) // Filter by the input + .filter(it => executor.name == '' || isReplacable ? matchers[matchType](it) : it.toLowerCase() == slashCommand) // Filter by the input // .sort((a, b) => a.localeCompare(b)) // Sort alphabetically ; result = helpStrings @@ -2144,7 +2144,14 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) { li.classList.add('selected'); } li.innerHTML = item.label; - li.addEventListener('click', ()=>{ + li.addEventListener('pointerdown', ()=>{ + mouseup = new Promise(resolve=>{ + const resolver = ()=>{ + window.removeEventListener('mouseup', resolver); + resolve(); + }; + window.addEventListener('mouseup', resolver); + }); selectedItem = item; select(); }); @@ -2225,10 +2232,12 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) { clone.remove(); return location; }; - const select = () => { + let mouseup = Promise.resolve(); + const select = async() => { if (isReplacable) { - textarea.focus(); textarea.value = `${text.slice(0, executor.start - 2)}${selectedItem.value}${text.slice(executor.start - 2 + executor.name.length + 1)}`; + await mouseup; + textarea.focus(); textarea.selectionStart = executor.start - 2 + selectedItem.value.length; textarea.selectionEnd = textarea.selectionStart; show();