Skip to content

Commit 4a78be1

Browse files
committed
fix(web): the Cite button comes back once the timeline stops scrolling
Any scroll of the timeline hid the Cite button and latched the selection observer as dismissed, so the button never returned for that selection. observeSelectionActions gets an opt-in restoreAfterScroll option: a scroll still hides the actions, then the selection is re-read 150 ms after the last scroll event and the button is placed at the selection's new position. An Escape dismissal still holds. Only the assistant selection toolbar opts in. The toolbar's visibility check now also covers horizontal clipping by nested scrollers such as code blocks, so the button is not restored for text that is scrolled out of view.
1 parent fe417df commit 4a78be1

3 files changed

Lines changed: 127 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type AssistantCitationSourceAnchor,
1313
} from "~/lib/assistantTextSelection";
1414
import {
15+
isSelectionRectVisible,
1516
observeSelectionActions,
1617
resolveSelectionActionPosition,
1718
type SelectionActionPoint,
@@ -56,7 +57,16 @@ export function AssistantSelectionToolbar({
5657
}
5758
const rect = captured.range.getBoundingClientRect();
5859
const viewportRect = viewport.getBoundingClientRect();
59-
if (rect.bottom < viewportRect.top || rect.top > viewportRect.bottom || rect.width === 0) {
60+
// Code blocks and other nested scrollers can clip the range inside the timeline.
61+
const clips = [viewportRect];
62+
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());
67+
}
68+
}
69+
if (!isSelectionRectVisible(rect, clips)) {
6070
clear();
6171
return;
6272
}
@@ -80,6 +90,7 @@ export function AssistantSelectionToolbar({
8090
const actions = observeSelectionActions({
8191
element: viewport,
8292
getActionElement: () => toolbarRef.current,
93+
restoreAfterScroll: true,
8394
onSelection: update,
8495
onDismiss: clear,
8596
});

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

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { observeSelectionActions, resolveSelectionActionPosition } from "./selectionActions";
3+
import {
4+
isSelectionRectVisible,
5+
observeSelectionActions,
6+
resolveSelectionActionPosition,
7+
} from "./selectionActions";
48

59
function event(type: string, values: Record<string, unknown> = {}) {
610
return Object.assign(new Event(type, { cancelable: true }), values);
711
}
812

9-
function createSelectionSurface({ interactiveActions = false } = {}) {
13+
function createSelectionSurface({ interactiveActions = false, restoreAfterScroll = false } = {}) {
1014
const frames = new Map<number, FrameRequestCallback>();
1115
let frameId = 0;
1216
const view = Object.assign(new EventTarget(), {
@@ -42,6 +46,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
4246
...(interactiveActions
4347
? { getActionElement: () => actionElement as unknown as HTMLElement }
4448
: {}),
49+
restoreAfterScroll,
4550
onSelection,
4651
onDismiss,
4752
});
@@ -294,6 +299,58 @@ describe("selection action gestures", () => {
294299
expect(surface.onSelection).toHaveBeenCalledOnce();
295300
});
296301

302+
it("re-reads the selection once scrolling settles when restoring after scroll", () => {
303+
const surface = createSelectionSurface({ restoreAfterScroll: true });
304+
surface.down();
305+
surface.up({ x: 420, y: 310 });
306+
surface.flush();
307+
surface.onDismiss.mockClear();
308+
surface.element.dispatchEvent(event("scroll"));
309+
surface.flush(100);
310+
surface.element.dispatchEvent(event("scroll"));
311+
surface.flush(100);
312+
expect(surface.onDismiss).toHaveBeenCalledWith("cancel");
313+
expect(surface.onSelection).toHaveBeenCalledOnce();
314+
surface.flush(50);
315+
expect(surface.onSelection).toHaveBeenLastCalledWith(null);
316+
expect(surface.onSelection).toHaveBeenCalledTimes(2);
317+
});
318+
319+
it("keeps an Escape dismissal through a later scroll when restoring after scroll", () => {
320+
const surface = createSelectionSurface({ restoreAfterScroll: true });
321+
surface.down();
322+
surface.up();
323+
surface.flush();
324+
surface.key("Escape");
325+
surface.element.dispatchEvent(event("scroll"));
326+
surface.change();
327+
surface.flush(1000);
328+
expect(surface.onSelection).toHaveBeenCalledOnce();
329+
});
330+
331+
it("keeps an Escape during a settling scroll dismissed when restoring after scroll", () => {
332+
const surface = createSelectionSurface({ restoreAfterScroll: true });
333+
surface.down();
334+
surface.up();
335+
surface.flush();
336+
surface.element.dispatchEvent(event("scroll"));
337+
surface.key("Escape");
338+
surface.change();
339+
surface.flush(1000);
340+
expect(surface.onSelection).toHaveBeenCalledOnce();
341+
});
342+
343+
it("teardown cancels a pending scroll restore", () => {
344+
const surface = createSelectionSurface({ restoreAfterScroll: true });
345+
surface.down();
346+
surface.up();
347+
surface.flush();
348+
surface.element.dispatchEvent(event("scroll"));
349+
surface.actions.dispose();
350+
surface.flush(1000);
351+
expect(surface.onSelection).toHaveBeenCalledOnce();
352+
});
353+
297354
it("cancels a queued animation frame when a new press supersedes the release", () => {
298355
const surface = createSelectionSurface();
299356
surface.down();
@@ -387,6 +444,39 @@ describe("interactive selection actions", () => {
387444
});
388445
});
389446

447+
describe("selection visibility", () => {
448+
const rect = (left: number, top: number, right: number, bottom: number) => ({
449+
left,
450+
top,
451+
right,
452+
bottom,
453+
width: right - left,
454+
});
455+
const timeline = rect(0, 0, 800, 600);
456+
const codeBlock = rect(100, 200, 400, 260);
457+
458+
it("shows a selection inside every clipping box", () => {
459+
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", () => {
476+
expect(isSelectionRectVisible(rect(380, 210, 460, 226), [timeline, codeBlock])).toBe(true);
477+
});
478+
});
479+
390480
describe("selection action positioning", () => {
391481
const bounds = { left: 100, top: 50, width: 500, height: 220 };
392482
const viewport = { width: 1024, height: 768 };

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type SelectionActionPoint = { x: number; y: number };
22

33
export const SELECTION_MULTI_CLICK_INTERVAL_MS = 500;
4+
const SELECTION_SCROLL_SETTLE_MS = 150;
45

56
export function resolveSelectionActionPosition(options: {
67
bounds: { left: number; top: number; width: number; height: number };
@@ -25,21 +26,40 @@ export function resolveSelectionActionPosition(options: {
2526
};
2627
}
2728

29+
type SelectionRect = { left: number; top: number; right: number; bottom: number; width: number };
30+
31+
/** Whether a selection still shows inside every box that clips it. */
32+
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+
);
43+
}
44+
2845
/**
2946
* Opens selection actions after release, leaving multi-clicks time to finish.
3047
* DOM selections call selectionChanged; canvas selections can use their own
3148
* change notification. Each surface still owns reading and acting on its text.
3249
* Interactive popovers pass getActionElement to keep editing their fields from
33-
* replacing the captured source selection.
50+
* replacing the captured source selection. Surfaces that can reposition their
51+
* actions pass restoreAfterScroll to re-read the selection once scrolling settles.
3452
*/
3553
export function observeSelectionActions({
3654
element,
3755
getActionElement,
56+
restoreAfterScroll = false,
3857
onSelection,
3958
onDismiss,
4059
}: {
4160
element: HTMLElement;
4261
getActionElement?: () => HTMLElement | null;
62+
restoreAfterScroll?: boolean;
4363
onSelection: (pointer: SelectionActionPoint | null) => void;
4464
onDismiss: (reason: "interaction" | "cancel") => void;
4565
}) {
@@ -141,7 +161,8 @@ export function observeSelectionActions({
141161
// Autoscroll during a drag must not cancel that gesture's eventual release.
142162
if (!pointerDown) {
143163
pointer = null;
144-
dismissed = true;
164+
if (restoreAfterScroll && !dismissed) schedule(SELECTION_SCROLL_SETTLE_MS);
165+
else dismissed = true;
145166
}
146167
onDismiss("cancel");
147168
};

0 commit comments

Comments
 (0)