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); } });