Skip to content

Commit b1c4379

Browse files
fix(web): a click in a file preview drops a selection scrolled out of view
A selection whose rows are unmounted is parked on the edge of the mounted rows, and a collapsed selection found there was read as that parked one. A click landing on the same spot therefore left the old selection in place for the next copy. A press without Shift now always starts a new selection. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 8a9a38c commit b1c4379

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ describe("ReadOnlySourcePreview", () => {
114114
expect(copiedText(rows()[0]!) ?? document.getSelection()!.toString()).toBe(selected);
115115
});
116116

117+
it("drops a selection scrolled out of the mounted window when a click lands on its edge", async () => {
118+
await getSyntaxHighlighterPromise("text");
119+
await using screen = await render(
120+
<div className="flex h-[400px] flex-col">
121+
<ReadOnlySourcePreview name="notes.txt" text={text} wordWrap={false} />
122+
</div>,
123+
);
124+
const root = screen.container.querySelector("diffs-container")!.shadowRoot!;
125+
const rows = () => root.querySelectorAll("[data-line]");
126+
await vi.waitFor(() => expect(rows()[0]?.textContent).toBe("line 1"));
127+
128+
const rowText = (index: number) => rows()[index]!.querySelector("span")!.firstChild!;
129+
document.getSelection()!.setBaseAndExtent(rowText(4), 0, rowText(19), 4);
130+
const scroller = screen.container.querySelector(".file-preview-virtualizer")!;
131+
scroller.scrollTop = scroller.scrollHeight;
132+
await vi.waitFor(() => expect(rows()[0]?.textContent).not.toBe("line 1"));
133+
expect(copiedText(rows()[0]!)).toContain("line 5\n");
134+
135+
// The first mounted row is where the scrolled-out selection is parked, far above the viewport.
136+
rows()[0]!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, composed: true }));
137+
document.getSelection()!.collapse(rowText(0), 0);
138+
expect(copiedText(rows()[0]!)).toBeNull();
139+
});
140+
117141
it("copies a drag that autoscrolls past the edge of the preview", async () => {
118142
await getSyntaxHighlighterPromise("text");
119143
await using screen = await render(

‎apps/web/src/components/files/ReadOnlySourcePreview.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function ReadOnlySourcePreview(props: {
6262
ref={wrapper}
6363
className="contents"
6464
onCopy={copySelection}
65-
onPointerDown={() => selection.press()}
65+
onPointerDown={(event) => selection.press(event.button !== 0 || event.shiftKey)}
6666
// Scrolling unmounts rows, and a selection made in the same task has not been reported yet.
6767
onScrollCapture={(event) => {
6868
const root = event.currentTarget.querySelector("diffs-container")?.shadowRoot;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class SourceSelection {
4949
#lineStarts: number[] | undefined;
5050
#ends: Ends | undefined;
5151
#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;
5254
/** The browser's selection as a render left it during a press, when nothing was drawn over it. */
5355
#parked: { readonly range: StaticRange | undefined } | undefined;
5456

@@ -196,7 +198,7 @@ export class SourceSelection {
196198
const keepsFocus = keeps(focus, previous?.focus);
197199
// A click collapses the selection, and it is a new one unless both ends were already there.
198200
const collapsed = start.offset === end.offset && start.inRow && end.inRow;
199-
if (collapsed && !(keepsAnchor && keepsFocus)) {
201+
if (collapsed && (this.#clicking || !(keepsAnchor && keepsFocus))) {
200202
this.#ends = { anchor: start.offset, focus: start.offset };
201203
return mounted;
202204
}
@@ -244,15 +246,18 @@ export class SourceSelection {
244246
if (this.#ends) this.#draw(root, this.#read(root, true));
245247
}
246248

247-
press() {
249+
press(extending: boolean) {
248250
this.#pressed = true;
251+
this.#clicking = !extending;
249252
}
250253

251254
release(root: ShadowRoot, text: string) {
252255
if (!this.#pressed) return;
253-
this.#pressed = false;
254256
this.#use(text);
255-
this.#draw(root, this.#read(root, false));
257+
const mounted = this.#read(root, false);
258+
this.#pressed = false;
259+
this.#clicking = false;
260+
this.#draw(root, mounted);
256261
}
257262

258263
/** The text to copy when the browser's own copy would miss part of it, else `undefined`. */

0 commit comments

Comments
 (0)