Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/develop' into find_replace

Elian Doran553b07ab37b1
diff --cc apps/client/src/widgets/find_in_text.ts
index 9ae0e20077,248c0bc0b1..959fcdf34e
--- a/apps/client/src/widgets/find_in_text.ts
+++ b/apps/client/src/widgets/find_in_text.ts
@@@ -52,28 -53,17 +53,28 @@@ export default class FindInText
// let m = text.match(re);
// totalFound = m ? m.length : 0;
const options = { matchCase: matchCase, wholeWords: wholeWord };
- findResult = textEditor.execute<CKFindResult>("find", searchTerm, options);
+ findResult = textEditor.execute("find", searchTerm, options);
totalFound = findResult.results.length;
- // Find the result beyond the cursor
- const cursorPos = model.document.selection.getLastPosition();
- for (let i = 0; i < findResult.results.length; ++i) {
- const marker = findResult.results.get(i)?.marker;
- const fromPos = marker?.getStart();
- if (cursorPos && fromPos && fromPos.compareWith(cursorPos) !== "before") {
- currentFound = i;
- break;
+ const selection = model.document.selection;
+ // If text is selected, highlight the corresponding result;
+ // otherwise, highlight the first visible result in the scrolling container.
+ if (!selection.isCollapsed) {
+ const cursorPos = selection.getFirstPosition();
+ for (let i = 0; i < findResult.results.length; ++i) {
- const marker = findResult.results.get(i).marker;
- const fromPos = marker.getStart();
- if (cursorPos && fromPos.compareWith(cursorPos) !== "before") {
++ const marker = findResult.results.get(i)?.marker;
++ const fromPos = marker?.getStart();
++ if (cursorPos && fromPos?.compareWith(cursorPos) !== "before") {
+ currentFound = i;
+ break;
+ }
}
+ } else {
+ const editorEl = textEditor?.sourceElement;
- const findResultElement = editorEl.querySelectorAll(".ck-find-result");
- const scrollingContainer = editorEl.closest('.scrolling-container');
++ const findResultElement = editorEl?.querySelectorAll(".ck-find-result");
++ const scrollingContainer = editorEl?.closest('.scrolling-container');
+ const containerTop = scrollingContainer?.getBoundingClientRect().top ?? 0;
+ const closestIndex = Array.from(findResultElement ?? []).findIndex((el) => el.getBoundingClientRect().top >= containerTop);
+ currentFound = closestIndex >= 0 ? closestIndex : 0;
}
}