@devries-cameron20 / t3code-1 / commits / bc6062805d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): preserve file preview selection after scrolling back (#39057)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

magnificentcritter22 committed Nov 19, 2026 at 13:08 UTC bc6062805db775019cf6f13cc543e694ad9e20fc
2 files changed +63 -28
apps/web/src/components/files/ReadOnlySourcePreview.browser.test.tsx
+33 -25
@@ -88,31 +88,39 @@ describe("ReadOnlySourcePreview", () => {
88 expect(copiedText(rows()[0]!)).toBe(contents);
89 });
90
91 - it("copies a partial selection after its rows scroll out of the mounted window", async () => {
92 - await getSyntaxHighlighterPromise("text");
93 - await using screen = await render(
94 - <div className="flex h-[400px] flex-col">
95 - <ReadOnlySourcePreview name="notes.txt" text={text} wordWrap={false} />
96 - </div>,
97 - );
98 - const root = screen.container.querySelector("diffs-container")!.shadowRoot!;
99 - const rows = () => root.querySelectorAll("[data-line]");
100 - await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
101 -
102 - const rowText = (line: number) =>
103 - [...rows()].find((row) => row.textContent === `line ${line}`)!.querySelector("span")!
104 - .firstChild!;
105 - document.getSelection()!.setBaseAndExtent(rowText(5), 0, rowText(20), "line 20".length);
106 - const selected = Array.from({ length: 16 }, (_, index) => `line ${index + 5}`).join("\n");
107 -
108 - const scroller = screen.container.querySelector(".file-preview-virtualizer")!;
109 - scroller.scrollTop = scroller.scrollHeight;
110 - await vi.waitFor(() => expect(rows()[0]?.textContent).not.toBe("line 1"));
111 - scroller.scrollTop = 0;
112 - await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
113 -
114 - expect(copiedText(rows()[0]!) ?? document.getSelection()!.toString()).toBe(selected);
115 - });
91 + it.each([
92 + { contents: text, height: 400 },
93 + // Short enough that rows of the selection stay mounted with the preview scrolled to the bottom.
94 + { contents: text.split("\n").slice(0, 163).join("\n"), height: 800 },
95 + ])(
96 + "restores and copies a partial selection after scrolling away and back ($height px)",
97 + async ({ contents, height }) => {
98 + await getSyntaxHighlighterPromise("text");
99 + await using screen = await render(
100 + <div className="flex flex-col" style={{ height }}>
101 + <ReadOnlySourcePreview name="notes.txt" text={contents} wordWrap={false} />
102 + </div>,
103 + );
104 + const root = screen.container.querySelector("diffs-container")!.shadowRoot!;
105 + const rows = () => root.querySelectorAll("[data-line]");
106 + await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
107 +
108 + const rowText = (line: number) =>
109 + [...rows()].find((row) => row.textContent === `line ${line}`)!.querySelector("span")!
110 + .firstChild!;
111 + document.getSelection()!.setBaseAndExtent(rowText(5), 0, rowText(20), "line 20".length);
112 + const selected = Array.from({ length: 16 }, (_, index) => `line ${index + 5}`).join("\n");
113 +
114 + const scroller = screen.container.querySelector(".file-preview-virtualizer")!;
115 + scroller.scrollTop = scroller.scrollHeight;
116 + await vi.waitFor(() => expect(rows()[0]?.textContent).not.toBe("line 1"));
117 + scroller.scrollTop = 0;
118 + await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
119 +
120 + expect(document.getSelection()!.toString()).toBe(selected);
121 + expect(copiedText(rows()[0]!) ?? document.getSelection()!.toString()).toBe(selected);
122 + },
123 + );
124
125 it("drops a selection scrolled out of the mounted window when a click lands on its edge", async () => {
126 await getSyntaxHighlighterPromise("text");
apps/web/src/components/files/sourceSelection.ts
+30 -3
@@ -51,8 +51,13 @@ export class SourceSelection {
51 #pressed = false;
52 /** A press that places a caret, where a collapsed selection is the click's and never a kept one. */
53 #clicking = false;
54 - /** The browser's selection as a render left it during a press, when nothing was drawn over it. */
55 - #parked: { readonly range: StaticRange | undefined } | undefined;
54 + /**
55 + * The browser's selection as last left: by a render during a press when nothing was drawn over it,
56 + * or (`clippedByRender`) as drawn clipped to the mounted rows, which must not be read back as the ends.
57 + */
58 + #parked:
59 + | { readonly range: StaticRange | undefined; readonly clippedByRender: boolean }
60 + | undefined;
61
62 #use(text: string) {
63 if (text === this.#text) return;
@@ -156,6 +161,14 @@ export class SourceSelection {
161 this.#ends = undefined;
162 return undefined;
163 }
164 + const parked = this.#parked;
165 + if (
166 + parked?.clippedByRender &&
167 + (sameRange(range, parked.range) || (afterRender && !this.#pressed))
168 + ) {
169 + return this.#mounted(root);
170 + }
171 + if (parked?.clippedByRender) this.#parked = undefined;
172 if (range && !(root.contains(range.startContainer) && root.contains(range.endContainer))) {
173 this.#ends = undefined;
174 return undefined;
@@ -222,10 +235,15 @@ export class SourceSelection {
235 const focus = this.#pointAt(this.#ends.focus, mounted);
236 const selection = document.getSelection();
237 const range = selection?.getComposedRanges({ shadowRoots: [root] })[0];
238 + const { anchor: anchorOffset, focus: focusOffset } = this.#ends;
239 + const clipped =
240 + anchorOffset !== focusOffset &&
241 + (Math.min(anchorOffset, focusOffset) < mounted.start ||
242 + Math.max(anchorOffset, focusOffset) > mounted.end);
243 if (this.#pressed && anchor.node === focus.node && anchor.offset === focus.offset) {
244 // Chromium stops extending a drag once a script collapses its selection, so a drag whose
245 // ends are both unmounted keeps what the browser has left of it.
228 - this.#parked = { range };
246 + this.#parked = { range, clippedByRender: false };
247 return;
248 }
249 const [start, end] = selection?.direction === "backward" ? [focus, anchor] : [anchor, focus];
@@ -235,9 +253,17 @@ export class SourceSelection {
253 range.endContainer === end.node &&
254 range.endOffset === end.offset
255 ) {
256 + if (clipped) this.#parked = { range, clippedByRender: true };
257 return;
258 }
259 selection?.setBaseAndExtent(anchor.node, anchor.offset, focus.node, focus.offset);
260 + if (clipped) {
261 + // A projected endpoint can become an interior point when its DOM row survives the next render.
262 + this.#parked = {
263 + range: selection?.getComposedRanges({ shadowRoots: [root] })[0],
264 + clippedByRender: true,
265 + };
266 + }
267 }
268
269 /** Call after rows mount or unmount, to draw the selection onto the rows now there. */
@@ -249,6 +275,7 @@ export class SourceSelection {
275 press(extending: boolean) {
276 this.#pressed = true;
277 this.#clicking = !extending;
278 + if (!extending) this.#parked = undefined;
279 }
280
281 release(root: ShadowRoot, text: string) {