Skip to content

Commit 3b38337

Browse files
committed
fix(web): preserve scroll during file search updates
1 parent 68e06eb commit 3b38337

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎apps/web/src/components/files/SearchableMarkdownPreview.browser.test.tsx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,18 @@ it("reuses its DOM index while typing and its ranges while navigating", async ()
106106
});
107107

108108
it("reveals code matches inside horizontal and vertical scroll containers without moving the page", async () => {
109-
await using screen = await render(
109+
const content = (extra: string) => (
110110
<div style={{ display: "flex", width: 360, height: 240 }}>
111111
<SearchableMarkdownPreview>
112112
<p>Start</p>
113113
<div style={{ height: 600 }} />
114114
<pre style={{ overflow: "auto", whiteSpace: "pre", fontFamily: "monospace" }}>
115-
needle {"x".repeat(400)}needle
115+
needle {"x".repeat(400)}needle{extra}
116116
</pre>
117117
</SearchableMarkdownPreview>
118-
</div>,
118+
</div>
119119
);
120+
await using screen = await render(content(""));
120121
const paragraph = screen.container.querySelector("p")!;
121122
await act(() => {
122123
requestMarkdownFindOpen(paragraph);
@@ -141,6 +142,20 @@ it("reveals code matches inside horizontal and vertical scroll containers withou
141142
expect(window.scrollY).toBe(pageTop);
142143
await userEvent.keyboard("{Shift>}{Enter}{/Shift}");
143144
expect(pre.scrollLeft).toBe(0);
145+
viewport.scrollTop = 0;
146+
await screen.rerender(content(" needle"));
147+
await vi.waitFor(() => expect(highlighted("t3-markdown-find")).toHaveLength(2));
148+
expect(viewport.scrollTop).toBe(0);
149+
expect(pre.scrollLeft).toBe(0);
150+
await userEvent.keyboard("{Enter}");
151+
expect(viewport.scrollTop).toBeGreaterThan(0);
152+
expect(pre.scrollLeft).toBeGreaterThan(0);
153+
await userEvent.fill(screen.getByRole("searchbox"), "absent");
154+
await vi.waitFor(() => expect(highlighted()).toEqual([]));
155+
viewport.scrollTop = 0;
156+
await screen.rerender(content(" absent"));
157+
await vi.waitFor(() => expect(highlighted()).toEqual(["absent"]));
158+
expect(viewport.scrollTop).toBe(0);
144159
});
145160

146161
it("routes file toolbar focus to the visible preview, leaving hidden tabs and chat alone", async () => {

‎apps/web/src/components/files/markdownFind.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ function reveal(range: Range, container: HTMLElement) {
8686
folded = true;
8787
}
8888
}
89-
if (folded) return;
89+
if (folded) return true;
9090

9191
const viewport = container.closest<HTMLElement>('[data-slot="scroll-area-viewport"]');
92-
if (!viewport) return;
92+
if (!viewport) return false;
9393
for (
9494
let node = range.startContainer.parentElement;
9595
node && viewport.contains(node);
@@ -117,6 +117,7 @@ function reveal(range: Range, container: HTMLElement) {
117117
if (left || top) node.scrollBy({ left, top, behavior: "instant" });
118118
if (node === viewport) break;
119119
}
120+
return false;
120121
}
121122

122123
export function createMarkdownFind(
@@ -129,6 +130,7 @@ export function createMarkdownFind(
129130
let query = "";
130131
let frame: number | null = null;
131132
let matchesDirty = true;
133+
let needsReveal = false;
132134
searchCount++;
133135
const inactive = highlight(MATCH_HIGHLIGHT);
134136
const active = highlight(ACTIVE_HIGHLIGHT);
@@ -145,7 +147,9 @@ export function createMarkdownFind(
145147
if (selected) {
146148
inactive.delete(selected);
147149
active.add(selected);
148-
reveal(selected, container);
150+
if (needsReveal) needsReveal = reveal(selected, container);
151+
} else {
152+
needsReveal = false;
149153
}
150154
onChange({ matchCount: ranges.length, activeIndex });
151155
};
@@ -189,6 +193,7 @@ export function createMarkdownFind(
189193
});
190194
return {
191195
search(value: string) {
196+
needsReveal = true;
192197
query = value.trim();
193198
activeIndex = 0;
194199
matchesDirty = true;
@@ -204,6 +209,7 @@ export function createMarkdownFind(
204209
active.delete(previous);
205210
inactive.add(previous);
206211
activeIndex = (activeIndex + delta + ranges.length) % ranges.length;
212+
needsReveal = true;
207213
select();
208214
},
209215
dispose() {

0 commit comments

Comments
 (0)