mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-13 09:24:22 +01:00
Finding the active line in textarea, treating each line as different queries
This commit is contained in:
parent
9ec847e1c9
commit
fefa7b51d7
1 changed files with 27 additions and 2 deletions
|
@ -111,9 +111,34 @@ function findSelectedTerm(targetInput: InputFieldElement, searchQuery: string):
|
|||
if (targetInput.selectionStart === null || targetInput.selectionEnd === null) return null;
|
||||
|
||||
const selectionIndex = Math.min(targetInput.selectionStart, targetInput.selectionEnd);
|
||||
const terms = getTermContexts(searchQuery);
|
||||
const isMultiline = targetInput instanceof HTMLTextAreaElement;
|
||||
|
||||
return terms.find(([range]) => range[0] < selectionIndex && range[1] >= selectionIndex) ?? null;
|
||||
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
|
||||
// actively edited line and use it instead of the whole value.
|
||||
if (isMultiline) {
|
||||
const activeLineStart = searchQuery.slice(0, selectionIndex).lastIndexOf('\n') + 1;
|
||||
const lengthAfterSelectionIndex = Math.max(searchQuery.slice(selectionIndex).indexOf('\n'), 0);
|
||||
|
||||
targetQuery = searchQuery.slice(activeLineStart, selectionIndex + lengthAfterSelectionIndex);
|
||||
lineOffset = activeLineStart;
|
||||
}
|
||||
|
||||
const terms = getTermContexts(targetQuery);
|
||||
const searchIndex = selectionIndex - lineOffset;
|
||||
const term = terms.find(([range]) => range[0] < searchIndex && range[1] >= searchIndex) ?? null;
|
||||
|
||||
// Converting line-specific indexes back to absolute ones.
|
||||
if (isMultiline && term) {
|
||||
const [range] = term;
|
||||
|
||||
range[0] += lineOffset;
|
||||
range[1] += lineOffset;
|
||||
}
|
||||
|
||||
return term;
|
||||
}
|
||||
|
||||
function toggleSearchAutocomplete() {
|
||||
|
|
Loading…
Reference in a new issue