Skip to content

Commit 102c3dd

Browse files
fix(web): restore Cite after scrolling without undoing dismissal
1 parent 8363a2d commit 102c3dd

3 files changed

Lines changed: 97 additions & 58 deletions

File tree

‎apps/web/src/components/chat/AssistantSelectionToolbar.tsx‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,53 @@ export function AssistantSelectionToolbar({
5757
}
5858
const rect = captured.range.getBoundingClientRect();
5959
const viewportRect = viewport.getBoundingClientRect();
60-
// Code blocks and other nested scrollers can clip the range inside the timeline.
61-
const clips = [viewportRect];
60+
if (!isSelectionRectVisible(rect, [viewportRect])) {
61+
clear();
62+
return;
63+
}
64+
// A selection can cross blocks with different clipping ancestors.
6265
const common = captured.range.commonAncestorContainer;
63-
let element = common instanceof Element ? common : common.parentElement;
64-
for (; element && element !== viewport; element = element.parentElement) {
65-
if (getComputedStyle(element).overflow !== "visible") {
66-
clips.push(element.getBoundingClientRect());
66+
const walker = document.createTreeWalker(common, NodeFilter.SHOW_TEXT);
67+
let node = common.nodeType === Node.TEXT_NODE ? common : walker.nextNode();
68+
let visible = false;
69+
for (; node && !visible; node = walker.nextNode()) {
70+
if (!captured.range.intersectsNode(node)) continue;
71+
if (node.parentElement && getComputedStyle(node.parentElement).userSelect === "none")
72+
continue;
73+
const fragment = document.createRange();
74+
fragment.selectNodeContents(node);
75+
if (node === captured.range.startContainer) {
76+
fragment.setStart(node, captured.range.startOffset);
77+
}
78+
if (node === captured.range.endContainer) {
79+
fragment.setEnd(node, captured.range.endOffset);
80+
}
81+
if (!fragment.toString().trim()) continue;
82+
const lines = Array.from(fragment.getClientRects());
83+
if (!lines.some((line) => isSelectionRectVisible(line, [viewportRect]))) continue;
84+
const clips: Pick<DOMRect, "left" | "right" | "top" | "bottom">[] = [viewportRect];
85+
for (
86+
let element = node.parentElement;
87+
element && element !== viewport;
88+
element = element.parentElement
89+
) {
90+
const style = getComputedStyle(element);
91+
const clipX = style.overflowX !== "visible";
92+
const clipY = style.overflowY !== "visible";
93+
if (!clipX && !clipY) continue;
94+
const bounds = element.getBoundingClientRect();
95+
const left = bounds.left + element.clientLeft;
96+
const top = bounds.top + element.clientTop;
97+
clips.push({
98+
left: clipX ? left : -Infinity,
99+
right: clipX ? left + element.clientWidth : Infinity,
100+
top: clipY ? top : -Infinity,
101+
bottom: clipY ? top + element.clientHeight : Infinity,
102+
});
67103
}
104+
visible = lines.some((line) => isSelectionRectVisible(line, clips));
68105
}
69-
if (!isSelectionRectVisible(rect, clips)) {
106+
if (!visible) {
70107
clear();
71108
return;
72109
}

‎apps/web/src/lib/selectionActions.test.ts‎

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -299,36 +299,41 @@ describe("selection action gestures", () => {
299299
expect(surface.onSelection).toHaveBeenCalledOnce();
300300
});
301301

302-
it("re-reads the selection once scrolling settles when restoring after scroll", () => {
302+
it("restores after scrolling settles with a fresh anchor, without interrupting a held drag", () => {
303303
const surface = createSelectionSurface({ restoreAfterScroll: true });
304304
surface.down();
305+
surface.element.dispatchEvent(event("scroll"));
306+
surface.change();
307+
surface.flush(1000);
308+
expect(surface.onSelection).not.toHaveBeenCalled();
305309
surface.up({ x: 420, y: 310 });
306310
surface.flush();
311+
expect(surface.onSelection.mock.calls).toEqual([[{ x: 420, y: 310 }]]);
312+
307313
surface.onDismiss.mockClear();
308314
surface.element.dispatchEvent(event("scroll"));
315+
expect(surface.onDismiss).toHaveBeenCalledWith("cancel");
309316
surface.flush(100);
310317
surface.element.dispatchEvent(event("scroll"));
318+
surface.change();
311319
surface.flush(100);
312-
expect(surface.onDismiss).toHaveBeenCalledWith("cancel");
313320
expect(surface.onSelection).toHaveBeenCalledOnce();
314321
surface.flush(50);
315-
expect(surface.onSelection).toHaveBeenLastCalledWith(null);
322+
expect(surface.onSelection.mock.calls).toEqual([[{ x: 420, y: 310 }], [null]]);
323+
surface.flush(1000);
316324
expect(surface.onSelection).toHaveBeenCalledTimes(2);
317-
});
318325

319-
it("keeps an Escape dismissal through a later scroll when restoring after scroll", () => {
320-
const surface = createSelectionSurface({ restoreAfterScroll: true });
321326
surface.down();
322327
surface.up();
323-
surface.flush();
324-
surface.key("Escape");
328+
vi.advanceTimersByTime(0);
325329
surface.element.dispatchEvent(event("scroll"));
326-
surface.change();
327-
surface.flush(1000);
328-
expect(surface.onSelection).toHaveBeenCalledOnce();
330+
surface.flush(149);
331+
expect(surface.onSelection).toHaveBeenCalledTimes(2);
332+
surface.flush(1);
333+
expect(surface.onSelection.mock.calls).toEqual([[{ x: 420, y: 310 }], [null], [null]]);
329334
});
330335

331-
it("keeps an Escape during a settling scroll dismissed when restoring after scroll", () => {
336+
it("keeps Escape during scroll settling dismissed through later scrolling", () => {
332337
const surface = createSelectionSurface({ restoreAfterScroll: true });
333338
surface.down();
334339
surface.up();
@@ -338,17 +343,23 @@ describe("selection action gestures", () => {
338343
surface.change();
339344
surface.flush(1000);
340345
expect(surface.onSelection).toHaveBeenCalledOnce();
341-
});
342346

343-
it("teardown cancels a pending scroll restore", () => {
344-
const surface = createSelectionSurface({ restoreAfterScroll: true });
345-
surface.down();
346-
surface.up();
347-
surface.flush();
347+
surface.key("PageDown");
348348
surface.element.dispatchEvent(event("scroll"));
349-
surface.actions.dispose();
349+
surface.change();
350350
surface.flush(1000);
351351
expect(surface.onSelection).toHaveBeenCalledOnce();
352+
353+
surface.document.dispatchEvent(event("keydown", { key: "ArrowRight", shiftKey: true }));
354+
surface.change();
355+
surface.element.dispatchEvent(event("scroll"));
356+
surface.flush(150);
357+
expect(surface.onSelection.mock.calls).toEqual([[{ x: 300, y: 200 }], [null]]);
358+
surface.key("Escape");
359+
surface.down();
360+
surface.up();
361+
surface.flush();
362+
expect(surface.onSelection).toHaveBeenCalledTimes(3);
352363
});
353364

354365
it("cancels a queued animation frame when a new press supersedes the release", () => {
@@ -455,25 +466,16 @@ describe("selection visibility", () => {
455466
const timeline = rect(0, 0, 800, 600);
456467
const codeBlock = rect(100, 200, 400, 260);
457468

458-
it("shows a selection inside every clipping box", () => {
469+
it("only shows text overlapping both the timeline and its nested scroller", () => {
459470
expect(isSelectionRectVisible(rect(150, 210, 220, 226), [timeline, codeBlock])).toBe(true);
460-
});
461-
462-
it.each([
463-
["scrolled left out of a code block", rect(40, 210, 90, 226)],
464-
["scrolled right out of a code block", rect(420, 210, 480, 226)],
465-
["touching the code block's edge", rect(60, 210, 100, 226)],
466-
])("hides a selection %s", (_, selection) => {
467-
expect(isSelectionRectVisible(selection, [timeline, codeBlock])).toBe(false);
468-
});
469-
470-
it("hides a selection above the timeline or with no width", () => {
471-
expect(isSelectionRectVisible(rect(150, -40, 220, -24), [timeline])).toBe(false);
472-
expect(isSelectionRectVisible(rect(150, 210, 150, 226), [timeline])).toBe(false);
473-
});
474-
475-
it("shows a selection that is only partly clipped", () => {
471+
expect(isSelectionRectVisible(rect(420, 210, 480, 226), [timeline, codeBlock])).toBe(false);
476472
expect(isSelectionRectVisible(rect(380, 210, 460, 226), [timeline, codeBlock])).toBe(true);
473+
expect(
474+
isSelectionRectVisible(rect(150, -40, 220, -24), [timeline, rect(100, -50, 400, 260)]),
475+
).toBe(false);
476+
expect(
477+
isSelectionRectVisible(rect(-200, 210, 220, 226), [timeline, rect(-200, 200, -20, 260)]),
478+
).toBe(false);
477479
});
478480
});
479481

‎apps/web/src/lib/selectionActions.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,22 @@ export function resolveSelectionActionPosition(options: {
2626
};
2727
}
2828

29-
type SelectionRect = { left: number; top: number; right: number; bottom: number; width: number };
29+
type SelectionRect = { left: number; top: number; right: number; bottom: number };
3030

31-
/** Whether a selection still shows inside every box that clips it. */
3231
export function isSelectionRectVisible(rect: SelectionRect, clips: readonly SelectionRect[]) {
33-
return (
34-
rect.width > 0 &&
35-
clips.every(
36-
(clip) =>
37-
rect.right > clip.left &&
38-
rect.left < clip.right &&
39-
rect.bottom > clip.top &&
40-
rect.top < clip.bottom,
41-
)
42-
);
32+
let { left, top, right, bottom } = rect;
33+
for (const clip of clips) {
34+
left = Math.max(left, clip.left);
35+
top = Math.max(top, clip.top);
36+
right = Math.min(right, clip.right);
37+
bottom = Math.min(bottom, clip.bottom);
38+
}
39+
return right > left && bottom > top;
4340
}
4441

4542
/**
4643
* Interactive popovers pass getActionElement to keep editing their fields from replacing the
47-
* captured source selection. Surfaces that can reposition their actions pass
48-
* restoreAfterScroll to re-read the selection once scrolling settles.
44+
* captured source selection.
4945
*/
5046
export function observeSelectionActions({
5147
element,
@@ -65,6 +61,7 @@ export function observeSelectionActions({
6561
let pointerDown = false;
6662
let gestureActive = false;
6763
let dismissed = false;
64+
let restorable = false;
6865
let pointer: SelectionActionPoint | null = null;
6966
let timer: number | null = null;
7067
let frame: number | null = null;
@@ -82,6 +79,7 @@ export function observeSelectionActions({
8279
pointerDown = gestureActive = false;
8380
pointer = null;
8481
dismissed = true;
82+
restorable = false;
8583
};
8684
const dismiss = () => {
8785
cancel();
@@ -92,6 +90,7 @@ export function observeSelectionActions({
9290
onDismiss("interaction");
9391
};
9492
const schedule = (delay: number) => {
93+
restorable = true;
9594
cancelPending();
9695
timer = view.setTimeout(() => {
9796
timer = null;
@@ -112,6 +111,7 @@ export function observeSelectionActions({
112111
const inside = element.contains(event.target as Node);
113112
gestureActive = false;
114113
dismissed = true;
114+
restorable = false;
115115
pointer = null;
116116
if (inside) onDismiss("interaction");
117117
else if (getActionElement?.()) onDismiss("cancel");
@@ -155,7 +155,7 @@ export function observeSelectionActions({
155155
// Autoscroll during a drag must not cancel that gesture's eventual release.
156156
if (!pointerDown) {
157157
pointer = null;
158-
if (restoreAfterScroll && !dismissed) schedule(SELECTION_SCROLL_SETTLE_MS);
158+
if (restoreAfterScroll && !dismissed && restorable) schedule(SELECTION_SCROLL_SETTLE_MS);
159159
else dismissed = true;
160160
}
161161
onDismiss("cancel");

0 commit comments

Comments
 (0)