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

Commit 8588143

Browse files
committed
Make the find function for read-only code scroll correctly.
1 parent f0c735e commit 8588143

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

apps/client/src/stylesheets/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ body .CodeMirror {
438438
background-color: #eeeeee;
439439
}
440440

441+
.cm-matchhighlight.ck-find-result{
442+
background: var(--ck-color-highlight-background);
443+
}
444+
445+
.cm-matchhighlight.ck-find-result_selected {
446+
background-color: #ff9633;
447+
}
448+
441449
.CodeMirror pre.CodeMirror-placeholder {
442450
color: #999 !important;
443451
}

apps/client/src/widgets/find.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ export default class FindWidget extends NoteContextAwareWidget {
143143
this.$currentFound = this.$widget.find(".find-widget-current-found");
144144
this.$totalFound = this.$widget.find(".find-widget-total-found");
145145
this.$caseSensitiveCheckbox = this.$widget.find(".find-widget-case-sensitive-checkbox");
146-
this.$caseSensitiveCheckbox.change(() => this.performFind());
146+
this.$caseSensitiveCheckbox.on("change", () => this.performFind());
147147
this.$matchWordsCheckbox = this.$widget.find(".find-widget-match-words-checkbox");
148-
this.$matchWordsCheckbox.change(() => this.performFind());
148+
this.$matchWordsCheckbox.on("change", () => this.performFind());
149149
this.$previousButton = this.$widget.find(".find-widget-previous-button");
150150
this.$previousButton.on("click", () => this.findNext(-1));
151151
this.$nextButton = this.$widget.find(".find-widget-next-button");
@@ -160,7 +160,7 @@ export default class FindWidget extends NoteContextAwareWidget {
160160
this.$replaceButton = this.$widget.find(".replace-widget-replace-button");
161161
this.$replaceButton.on("click", () => this.replace());
162162

163-
this.$input.keydown(async (e) => {
163+
this.$input.on("keydown", async (e) => {
164164
if ((e.metaKey || e.ctrlKey) && (e.key === "F" || e.key === "f")) {
165165
// If ctrl+f is pressed when the findbox is shown, select the
166166
// whole input to find
@@ -172,7 +172,7 @@ export default class FindWidget extends NoteContextAwareWidget {
172172
}
173173
});
174174

175-
this.$widget.keydown(async (e) => {
175+
this.$widget.on("keydown", async (e) => {
176176
if (e.key === "Escape") {
177177
await this.closeSearch();
178178
}
@@ -197,9 +197,14 @@ export default class FindWidget extends NoteContextAwareWidget {
197197
const isReadOnly = await this.noteContext?.isReadOnly();
198198

199199
let selectedText = "";
200-
if (this.note?.type === "code" && !isReadOnly && this.noteContext) {
201-
const codeEditor = await this.noteContext.getCodeEditor();
202-
selectedText = codeEditor.getSelection();
200+
if (this.note?.type === "code" && this.noteContext) {
201+
if (isReadOnly){
202+
const $content = await this.noteContext.getContentElement();
203+
selectedText = $content.find('.cm-matchhighlight').first().text();
204+
} else {
205+
const codeEditor = await this.noteContext.getCodeEditor();
206+
selectedText = codeEditor.getSelection();
207+
}
203208
} else {
204209
selectedText = window.getSelection()?.toString() || "";
205210
}

apps/client/src/widgets/find_in_html.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// uses for highlighting matches, use the same one on CodeMirror
33
// for consistency
44
import utils from "../services/utils.js";
5-
import appContext from "../components/app_context.js";
65
import type FindWidget from "./find.js";
76
import type { FindResult } from "./find.js";
87

@@ -39,12 +38,25 @@ export default class FindInHtml {
3938
caseSensitive: matchCase,
4039
done: async () => {
4140
this.$results = $content.find(`.${FIND_RESULT_CSS_CLASSNAME}`);
42-
this.currentIndex = 0;
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;
4355
await this.jumpTo();
4456

4557
res({
4658
totalFound: this.$results.length,
47-
currentFound: Math.min(1, this.$results.length)
59+
currentFound: this.$results.length > 0 ? closestIndex + 1 : 0
4860
});
4961
}
5062
});
@@ -78,20 +90,10 @@ export default class FindInHtml {
7890

7991
async jumpTo() {
8092
if (this.$results?.length) {
81-
const offsetTop = 100;
8293
const $current = this.$results.eq(this.currentIndex);
8394
this.$results.removeClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
84-
85-
if ($current.length) {
86-
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
87-
const position = $current.position().top - offsetTop;
88-
89-
const $content = await this.parent.noteContext?.getContentElement();
90-
if ($content) {
91-
const $contentWidget = appContext.getComponentByEl($content[0]);
92-
$contentWidget.triggerCommand("scrollContainerTo", { position });
93-
}
94-
}
95+
$current[0].scrollIntoView();
96+
$current.addClass(FIND_RESULT_SELECTED_CSS_CLASSNAME);
9597
}
9698
}
9799
}

0 commit comments

Comments
 (0)