@devries-cameron20 / t3code Mirror · read-only / commits / cd8c2c5932
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 01:45 UTC

fix(web): the Cite button comes back once the timeline stops scrolling (#15818)

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

Igor Makowski committed Oct 19, 2026 at 22:22 UTC cd8c2c593275147d7f253ca0fd93dbe6088237b1
3 files changed +165 -4
apps/web/src/components/chat/AssistantSelectionToolbar.tsx
+49 -1
@@ -12,6 +12,7 @@ import {
12 type AssistantCitationSourceAnchor,
13 } from "~/lib/assistantTextSelection";
14 import {
15 + isSelectionRectVisible,
16 observeSelectionActions,
17 resolveSelectionActionPosition,
18 type SelectionActionPoint,
@@ -56,7 +57,53 @@ export function AssistantSelectionToolbar({
57 }
58 const rect = captured.range.getBoundingClientRect();
59 const viewportRect = viewport.getBoundingClientRect();
59 - if (rect.bottom < viewportRect.top || rect.top > viewportRect.bottom || rect.width === 0) {
60 + if (!isSelectionRectVisible(rect, [viewportRect])) {
61 + clear();
62 + return;
63 + }
64 + // A selection can cross blocks with different clipping ancestors.
65 + const common = captured.range.commonAncestorContainer;
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 + });
103 + }
104 + visible = lines.some((line) => isSelectionRectVisible(line, clips));
105 + }
106 + if (!visible) {
107 clear();
108 return;
109 }
@@ -89,6 +136,7 @@ export function AssistantSelectionToolbar({
136 const actions = observeSelectionActions({
137 element: viewport,
138 getActionElement: () => toolbarRef.current,
139 + restoreAfterScroll: true,
140 onSelection: update,
141 onDismiss: clear,
142 });
apps/web/src/lib/selectionActions.test.ts
+94 -2
@@ -1,12 +1,16 @@
1 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3 -import { observeSelectionActions, resolveSelectionActionPosition } from "./selectionActions";
3 +import {
4 + isSelectionRectVisible,
5 + observeSelectionActions,
6 + resolveSelectionActionPosition,
7 +} from "./selectionActions";
8
9 function event(type: string, values: Record<string, unknown> = {}) {
10 return Object.assign(new Event(type, { cancelable: true }), values);
11 }
12
9 -function createSelectionSurface({ interactiveActions = false } = {}) {
13 +function createSelectionSurface({ interactiveActions = false, restoreAfterScroll = false } = {}) {
14 const frames = new Map<number, FrameRequestCallback>();
15 let frameId = 0;
16 const view = Object.assign(new EventTarget(), {
@@ -42,6 +46,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
46 ...(interactiveActions
47 ? { getActionElement: () => actionElement as unknown as HTMLElement }
48 : {}),
49 + restoreAfterScroll,
50 onSelection,
51 onDismiss,
52 });
@@ -294,6 +299,69 @@ describe("selection action gestures", () => {
299 expect(surface.onSelection).toHaveBeenCalledOnce();
300 });
301
302 + it("restores after scrolling settles with a fresh anchor, without interrupting a held drag", () => {
303 + const surface = createSelectionSurface({ restoreAfterScroll: true });
304 + surface.down();
305 + surface.element.dispatchEvent(event("scroll"));
306 + surface.change();
307 + surface.flush(1000);
308 + expect(surface.onSelection).not.toHaveBeenCalled();
309 + surface.up({ x: 420, y: 310 });
310 + surface.flush();
311 + expect(surface.onSelection.mock.calls).toEqual([[{ x: 420, y: 310 }]]);
312 +
313 + surface.onDismiss.mockClear();
314 + surface.element.dispatchEvent(event("scroll"));
315 + expect(surface.onDismiss).toHaveBeenCalledWith("cancel");
316 + surface.flush(100);
317 + surface.element.dispatchEvent(event("scroll"));
318 + surface.change();
319 + surface.flush(100);
320 + expect(surface.onSelection).toHaveBeenCalledOnce();
321 + surface.flush(50);
322 + expect(surface.onSelection.mock.calls).toEqual([[{ x: 420, y: 310 }], [null]]);
323 + surface.flush(1000);
324 + expect(surface.onSelection).toHaveBeenCalledTimes(2);
325 +
326 + surface.down();
327 + surface.up();
328 + vi.advanceTimersByTime(0);
329 + surface.element.dispatchEvent(event("scroll"));
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]]);
334 + });
335 +
336 + it("keeps Escape during scroll settling dismissed through later scrolling", () => {
337 + const surface = createSelectionSurface({ restoreAfterScroll: true });
338 + surface.down();
339 + surface.up();
340 + surface.flush();
341 + surface.element.dispatchEvent(event("scroll"));
342 + surface.key("Escape");
343 + surface.change();
344 + surface.flush(1000);
345 + expect(surface.onSelection).toHaveBeenCalledOnce();
346 +
347 + surface.key("PageDown");
348 + surface.element.dispatchEvent(event("scroll"));
349 + surface.change();
350 + surface.flush(1000);
351 + 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);
363 + });
364 +
365 it("cancels a queued animation frame when a new press supersedes the release", () => {
366 const surface = createSelectionSurface();
367 surface.down();
@@ -387,6 +455,30 @@ describe("interactive selection actions", () => {
455 });
456 });
457
458 +describe("selection visibility", () => {
459 + const rect = (left: number, top: number, right: number, bottom: number) => ({
460 + left,
461 + top,
462 + right,
463 + bottom,
464 + width: right - left,
465 + });
466 + const timeline = rect(0, 0, 800, 600);
467 + const codeBlock = rect(100, 200, 400, 260);
468 +
469 + it("only shows text overlapping both the timeline and its nested scroller", () => {
470 + expect(isSelectionRectVisible(rect(150, 210, 220, 226), [timeline, codeBlock])).toBe(true);
471 + expect(isSelectionRectVisible(rect(420, 210, 480, 226), [timeline, codeBlock])).toBe(false);
472 + 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);
479 + });
480 +});
481 +
482 describe("selection action positioning", () => {
483 const bounds = { left: 100, top: 50, width: 500, height: 220 };
484 const viewport = { width: 1024, height: 768 };
apps/web/src/lib/selectionActions.ts
+22 -1
@@ -1,6 +1,7 @@
1 export type SelectionActionPoint = { x: number; y: number };
2
3 export const SELECTION_MULTI_CLICK_INTERVAL_MS = 500;
4 +const SELECTION_SCROLL_SETTLE_MS = 150;
5
6 export function resolveSelectionActionPosition(options: {
7 bounds: { left: number; top: number; width: number; height: number };
@@ -25,6 +26,19 @@ export function resolveSelectionActionPosition(options: {
26 };
27 }
28
29 +type SelectionRect = { left: number; top: number; right: number; bottom: number };
30 +
31 +export function isSelectionRectVisible(rect: SelectionRect, clips: readonly SelectionRect[]) {
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;
40 +}
41 +
42 /**
43 * Interactive popovers pass getActionElement to keep editing their fields from replacing the
44 * captured source selection.
@@ -32,11 +46,13 @@ export function resolveSelectionActionPosition(options: {
46 export function observeSelectionActions({
47 element,
48 getActionElement,
49 + restoreAfterScroll = false,
50 onSelection,
51 onDismiss,
52 }: {
53 element: HTMLElement;
54 getActionElement?: () => HTMLElement | null;
55 + restoreAfterScroll?: boolean;
56 onSelection: (pointer: SelectionActionPoint | null) => void;
57 onDismiss: (reason: "interaction" | "cancel") => void;
58 }) {
@@ -45,6 +61,7 @@ export function observeSelectionActions({
61 let pointerDown = false;
62 let gestureActive = false;
63 let dismissed = false;
64 + let restorable = false;
65 let pointer: SelectionActionPoint | null = null;
66 let timer: number | null = null;
67 let frame: number | null = null;
@@ -62,6 +79,7 @@ export function observeSelectionActions({
79 pointerDown = gestureActive = false;
80 pointer = null;
81 dismissed = true;
82 + restorable = false;
83 };
84 const dismiss = () => {
85 cancel();
@@ -72,6 +90,7 @@ export function observeSelectionActions({
90 onDismiss("interaction");
91 };
92 const schedule = (delay: number) => {
93 + restorable = true;
94 cancelPending();
95 timer = view.setTimeout(() => {
96 timer = null;
@@ -92,6 +111,7 @@ export function observeSelectionActions({
111 const inside = element.contains(event.target as Node);
112 gestureActive = false;
113 dismissed = true;
114 + restorable = false;
115 pointer = null;
116 if (inside) onDismiss("interaction");
117 else if (getActionElement?.()) onDismiss("cancel");
@@ -135,7 +155,8 @@ export function observeSelectionActions({
155 // Autoscroll during a drag must not cancel that gesture's eventual release.
156 if (!pointerDown) {
157 pointer = null;
138 - dismissed = true;
158 + if (restoreAfterScroll && !dismissed && restorable) schedule(SELECTION_SCROLL_SETTLE_MS);
159 + else dismissed = true;
160 }
161 onDismiss("cancel");
162 };