Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit aecde96

Browse files
committed
highlight the first visible find result in scrolling container.
1 parent 67e84d9 commit aecde96

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

apps/client/src/widgets/find_in_html.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,16 @@ export default class FindInHtml {
3838
caseSensitive: matchCase,
3939
done: async () => {
4040
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
41-
let closestIndex = 0;
42-
let minTop = Infinity;
43-
44-
this.$results.each((i, el) => {
45-
const rect = el.getBoundingClientRect();
46-
const top = rect.top;
47-
48-
if (top >= 0 && top < minTop) {
49-
minTop = top;
50-
closestIndex = i;
51-
}
52-
});
53-
54-
this.currentIndex = closestIndex;
41+
const scrollingContainer = $content[0].closest('.scrolling-container');
42+
const containerTop = scrollingContainer?.getBoundingClientRect().top ?? 0;
43+
const closestIndex = this.$results.toArray().findIndex(el => el.getBoundingClientRect().top >= containerTop);
44+
this.currentIndex = closestIndex >= 0 ? closestIndex : 0;
45+
5546
await this.jumpTo();
5647

5748
res({
5849
totalFound: this.$results.length,
59-
currentFound: this.$results.length > 0 ? closestIndex + 1 : 0
50+
currentFound: this.$results.length > 0 ? this.currentIndex + 1 : 0
6051
});
6152
}
6253
});

apps/client/src/widgets/find_in_text.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,26 @@ export default class FindInText {
5454
const options = { matchCase: matchCase, wholeWords: wholeWord };
5555
findResult = textEditor.execute<CKFindResult>("find", searchTerm, options);
5656
totalFound = findResult.results.length;
57-
// Find the result beyond the cursor
58-
const cursorPos = model.document.selection.getLastPosition();
59-
for (let i = 0; i < findResult.results.length; ++i) {
60-
const marker = findResult.results.get(i).marker;
61-
const fromPos = marker.getStart();
62-
if (cursorPos && fromPos.compareWith(cursorPos) !== "before") {
63-
currentFound = i;
64-
break;
57+
const selection = model.document.selection;
58+
// If text is selected, highlight the corresponding result;
59+
// otherwise, highlight the first visible result in the scrolling container.
60+
if (!selection.isCollapsed) {
61+
const cursorPos = selection.getFirstPosition();
62+
for (let i = 0; i < findResult.results.length; ++i) {
63+
const marker = findResult.results.get(i).marker;
64+
const fromPos = marker.getStart();
65+
if (cursorPos && fromPos.compareWith(cursorPos) !== "before") {
66+
currentFound = i;
67+
break;
68+
}
6569
}
70+
} else {
71+
const editorEl = textEditor?.sourceElement;
72+
const findResultElement = editorEl.querySelectorAll(".ck-find-result");
73+
const scrollingContainer = editorEl.closest('.scrolling-container');
74+
const containerTop = scrollingContainer?.getBoundingClientRect().top ?? 0;
75+
const closestIndex = Array.from(findResultElement ?? []).findIndex((el) => el.getBoundingClientRect().top >= containerTop);
76+
currentFound = closestIndex >= 0 ? closestIndex : 0;
6677
}
6778
}
6879

0 commit comments

Comments
 (0)