[autocomplete] complete negative searches, fixes #1028

This commit is contained in:
Shish 2024-02-11 16:40:44 +00:00
parent 7ee4152942
commit 4b8a6c6c43

View file

@ -42,7 +42,8 @@ function updateCompletions(element) {
} }
else { else {
element.completer_timeout = setTimeout(() => { element.completer_timeout = setTimeout(() => {
fetch((document.body.getAttribute("data-base-href") ?? "") + '/api/internal/autocomplete?s=' + word).then( const wordWithoutMinus = word.replace(/^-/, '');
fetch((document.body.getAttribute("data-base-href") ?? "") + '/api/internal/autocomplete?s=' + wordWithoutMinus).then(
(response) => response.json() (response) => response.json()
).then((json) => { ).then((json) => {
if(element.selected_completion !== -1) { if(element.selected_completion !== -1) {
@ -97,7 +98,7 @@ function renderCompletions(element) {
Object.keys(completions).filter( Object.keys(completions).filter(
(key) => { (key) => {
let k = key.toLowerCase(); let k = key.toLowerCase();
let w = word.toLowerCase(); let w = word.replace(/^-/, '').toLowerCase();
return (k.startsWith(w) || k.split(':').some((k) => k.startsWith(w))) return (k.startsWith(w) || k.split(':').some((k) => k.startsWith(w)))
} }
).slice(0, 100).forEach((key, i) => { ).slice(0, 100).forEach((key, i) => {
@ -176,6 +177,9 @@ function setCompletion(element, new_word) {
} }
// replace the word with the completion // replace the word with the completion
if(text[start] === '-') {
new_word = '-' + new_word;
}
new_word += ' '; new_word += ' ';
element.value = text.substring(0, start) + new_word + text.substring(end); element.value = text.substring(0, start) + new_word + text.substring(end);
element.selectionStart = start + new_word.length; element.selectionStart = start + new_word.length;