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