Support suggesting tags when - syntax used in tag editor

This commit is contained in:
KoloMl 2024-12-14 22:58:40 +04:00
parent 1853c8984f
commit 9a0ae3971b

View file

@ -44,6 +44,10 @@ function applySelectedValue(selection: string) {
if (!inputField) return; if (!inputField) return;
if (!isSearchField(inputField)) { if (!isSearchField(inputField)) {
if (originalTerm?.startsWith('-')) {
selection = `-${selection}`;
}
inputField.value = selection; inputField.value = selection;
return; return;
} }
@ -121,6 +125,10 @@ function toggleSearchAutocomplete() {
} }
} }
function trimPrefixes(targetTerm: string): string {
return targetTerm.trim().replace(/^-/, '');
}
function listenAutocomplete() { function listenAutocomplete() {
let serverSideSuggestionsTimeout: number | undefined; let serverSideSuggestionsTimeout: number | undefined;
@ -162,7 +170,7 @@ function listenAutocomplete() {
} }
const suggestions = localAc const suggestions = localAc
.matchPrefix(originalTerm) .matchPrefix(trimPrefixes(originalTerm))
.topK(suggestionsCount) .topK(suggestionsCount)
.map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name })); .map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name }));
@ -181,13 +189,13 @@ function listenAutocomplete() {
inputField = targetedInput; inputField = targetedInput;
originalTerm = inputField.value; originalTerm = inputField.value;
const fetchedTerm = inputField.value; const fetchedTerm = trimPrefixes(inputField.value);
if (minTermLength && fetchedTerm.length < parseInt(minTermLength, 10)) return; if (minTermLength && fetchedTerm.length < parseInt(minTermLength, 10)) return;
fetchSuggestions(endpointUrl, fetchedTerm).then(suggestions => { fetchSuggestions(endpointUrl, fetchedTerm).then(suggestions => {
// inputField could get overwritten while the suggestions are being fetched - use previously targeted input // 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); popup.renderSuggestions(suggestions).showForField(targetedInput);
} }
}); });