Merge pull request from koloml/support-minus-prefix-for-single-tags

Support suggestions when using `-tag` syntax in tag editor
This commit is contained in:
liamwhite 2024-12-14 19:20:33 -05:00 committed by GitHub
commit 38af26db71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 6 deletions

View file

@ -44,7 +44,13 @@ function applySelectedValue(selection: string) {
if (!inputField) return; if (!inputField) return;
if (!isSearchField(inputField)) { if (!isSearchField(inputField)) {
inputField.value = selection; let resultValue = selection;
if (originalTerm?.startsWith('-')) {
resultValue = `-${selection}`;
}
inputField.value = resultValue;
return; return;
} }
@ -121,6 +127,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 +172,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 +191,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);
} }
}); });
@ -222,8 +232,12 @@ function listenAutocomplete() {
const originalSuggestion = event.detail; const originalSuggestion = event.detail;
applySelectedValue(originalSuggestion.value); applySelectedValue(originalSuggestion.value);
if (originalTerm?.startsWith('-')) {
originalSuggestion.value = `-${originalSuggestion.value}`;
}
inputField.dispatchEvent( inputField.dispatchEvent(
new CustomEvent('autocomplete', { new CustomEvent<TermSuggestion>('autocomplete', {
detail: Object.assign( detail: Object.assign(
{ {
type: 'click', type: 'click',

View file

@ -112,7 +112,10 @@ export function setupTagsInput(tagBlock: HTMLDivElement) {
name = name.slice(1); // eslint-disable-line no-param-reassign name = name.slice(1); // eslint-disable-line no-param-reassign
const tagLink = assertNotNull($(`[data-tag-name="${escapeCss(name)}"]`, container)); const tagLink = assertNotNull($(`[data-tag-name="${escapeCss(name)}"]`, container));
return removeTag(name, assertNotNull(tagLink.parentElement)); removeTag(name, assertNotNull(tagLink.parentElement));
inputField.value = '';
return;
} }
tags.push(name); tags.push(name);