Run active line search unconditionally

This commit is contained in:
KoloMl 2025-02-11 12:23:00 +04:00
parent 0b04fba4b4
commit 63a9f0317f

View file

@ -111,27 +111,21 @@ function findSelectedTerm(targetInput: InputFieldElement, searchQuery: string):
if (targetInput.selectionStart === null || targetInput.selectionEnd === null) return null; if (targetInput.selectionStart === null || targetInput.selectionEnd === null) return null;
const selectionIndex = Math.min(targetInput.selectionStart, targetInput.selectionEnd); const selectionIndex = Math.min(targetInput.selectionStart, targetInput.selectionEnd);
const isMultiline = targetInput instanceof HTMLTextAreaElement;
let lineOffset = 0;
let targetQuery = searchQuery;
// Multi-line textarea elements should treat each line as the different search queries. Here we're looking for the // Multi-line textarea elements should treat each line as the different search queries. Here we're looking for the
// actively edited line and use it instead of the whole value. // actively edited line and use it instead of the whole value.
if (isMultiline) { const activeLineStart = searchQuery.slice(0, selectionIndex).lastIndexOf('\n') + 1;
const activeLineStart = searchQuery.slice(0, selectionIndex).lastIndexOf('\n') + 1; const lengthAfterSelectionIndex = Math.max(searchQuery.slice(selectionIndex).indexOf('\n'), 0);
const lengthAfterSelectionIndex = Math.max(searchQuery.slice(selectionIndex).indexOf('\n'), 0);
targetQuery = searchQuery.slice(activeLineStart, selectionIndex + lengthAfterSelectionIndex); const targetQuery = searchQuery.slice(activeLineStart, selectionIndex + lengthAfterSelectionIndex);
lineOffset = activeLineStart; const lineOffset = activeLineStart;
}
const terms = getTermContexts(targetQuery); const terms = getTermContexts(targetQuery);
const searchIndex = selectionIndex - lineOffset; const searchIndex = selectionIndex - lineOffset;
const term = terms.find(([range]) => range[0] < searchIndex && range[1] >= searchIndex) ?? null; const term = terms.find(([range]) => range[0] < searchIndex && range[1] >= searchIndex) ?? null;
// Converting line-specific indexes back to absolute ones. // Converting line-specific indexes back to absolute ones.
if (isMultiline && term) { if (term) {
const [range] = term; const [range] = term;
range[0] += lineOffset; range[0] += lineOffset;