From 9a0ae3971bee8acd7bc56f993fd9ef365d620f40 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sat, 14 Dec 2024 22:58:40 +0400 Subject: [PATCH] Support suggesting tags when `-` syntax used in tag editor --- assets/js/autocomplete.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/assets/js/autocomplete.ts b/assets/js/autocomplete.ts index 61340b48..12a19e55 100644 --- a/assets/js/autocomplete.ts +++ b/assets/js/autocomplete.ts @@ -44,6 +44,10 @@ function applySelectedValue(selection: string) { if (!inputField) return; if (!isSearchField(inputField)) { + if (originalTerm?.startsWith('-')) { + selection = `-${selection}`; + } + inputField.value = selection; return; } @@ -121,6 +125,10 @@ function toggleSearchAutocomplete() { } } +function trimPrefixes(targetTerm: string): string { + return targetTerm.trim().replace(/^-/, ''); +} + function listenAutocomplete() { let serverSideSuggestionsTimeout: number | undefined; @@ -162,7 +170,7 @@ function listenAutocomplete() { } const suggestions = localAc - .matchPrefix(originalTerm) + .matchPrefix(trimPrefixes(originalTerm)) .topK(suggestionsCount) .map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name })); @@ -181,13 +189,13 @@ function listenAutocomplete() { inputField = targetedInput; originalTerm = inputField.value; - const fetchedTerm = inputField.value; + const fetchedTerm = trimPrefixes(inputField.value); if (minTermLength && fetchedTerm.length < parseInt(minTermLength, 10)) return; fetchSuggestions(endpointUrl, fetchedTerm).then(suggestions => { // inputField could get overwritten while the suggestions are being fetched - use previously targeted input - if (fetchedTerm === targetedInput.value) { + if (fetchedTerm === trimPrefixes(targetedInput.value)) { popup.renderSuggestions(suggestions).showForField(targetedInput); } });