From 9a0ae3971bee8acd7bc56f993fd9ef365d620f40 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sat, 14 Dec 2024 22:58:40 +0400 Subject: [PATCH 1/4] 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); } }); From ea69049e21f5d5bc67663cc2d18fe600fe00707e Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sat, 14 Dec 2024 23:47:35 +0400 Subject: [PATCH 2/4] Fixed lint issue about overwriting the argument --- assets/js/autocomplete.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/assets/js/autocomplete.ts b/assets/js/autocomplete.ts index 12a19e55..f027e0d2 100644 --- a/assets/js/autocomplete.ts +++ b/assets/js/autocomplete.ts @@ -44,11 +44,13 @@ function applySelectedValue(selection: string) { if (!inputField) return; if (!isSearchField(inputField)) { + let resultValue = selection; + if (originalTerm?.startsWith('-')) { - selection = `-${selection}`; + resultValue = `-${selection}`; } - inputField.value = selection; + inputField.value = resultValue; return; } From 5c8ae5f54b5458264774a2ecbe5bb170ae0ce2f6 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sat, 14 Dec 2024 23:48:16 +0400 Subject: [PATCH 3/4] Restore `-` prefix when sending autocomplete from mouse clicks --- assets/js/autocomplete.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/js/autocomplete.ts b/assets/js/autocomplete.ts index f027e0d2..c26140c0 100644 --- a/assets/js/autocomplete.ts +++ b/assets/js/autocomplete.ts @@ -232,8 +232,12 @@ function listenAutocomplete() { const originalSuggestion = event.detail; applySelectedValue(originalSuggestion.value); + if (originalTerm?.startsWith('-')) { + originalSuggestion.value = `-${originalSuggestion.value}`; + } + inputField.dispatchEvent( - new CustomEvent('autocomplete', { + new CustomEvent('autocomplete', { detail: Object.assign( { type: 'click', From 3a4b240c3e703c3b19c15adf6dc22d86c7e899cf Mon Sep 17 00:00:00 2001 From: KoloMl Date: Sat, 14 Dec 2024 23:57:20 +0400 Subject: [PATCH 4/4] Clearing the input after successfully removing the tag --- assets/js/tagsinput.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/js/tagsinput.ts b/assets/js/tagsinput.ts index 74574906..8a7d4b06 100644 --- a/assets/js/tagsinput.ts +++ b/assets/js/tagsinput.ts @@ -112,7 +112,10 @@ export function setupTagsInput(tagBlock: HTMLDivElement) { name = name.slice(1); // eslint-disable-line no-param-reassign 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);