improve autocomplete show/hide logic and editor selection

This commit is contained in:
LenAnderson 2024-04-05 11:45:50 -04:00
parent 6440fd3840
commit 29d6ee45de
1 changed files with 13 additions and 4 deletions

View File

@ -2104,7 +2104,7 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) {
else { else {
const helpStrings = Object const helpStrings = Object
.keys(parser.commands) // Get all slash commands .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 // .sort((a, b) => a.localeCompare(b)) // Sort alphabetically
; ;
result = helpStrings result = helpStrings
@ -2144,7 +2144,14 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) {
li.classList.add('selected'); li.classList.add('selected');
} }
li.innerHTML = item.label; 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; selectedItem = item;
select(); select();
}); });
@ -2225,10 +2232,12 @@ export function setNewSlashCommandAutoComplete(textarea, isFloating = false) {
clone.remove(); clone.remove();
return location; return location;
}; };
const select = () => { let mouseup = Promise.resolve();
const select = async() => {
if (isReplacable) { if (isReplacable) {
textarea.focus();
textarea.value = `${text.slice(0, executor.start - 2)}${selectedItem.value}${text.slice(executor.start - 2 + executor.name.length + 1)}`; 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.selectionStart = executor.start - 2 + selectedItem.value.length;
textarea.selectionEnd = textarea.selectionStart; textarea.selectionEnd = textarea.selectionStart;
show(); show();