GeezerVison is showing github.com — fix(web): the Cite button comes back once the timeline stops scrollin… · pingdotgg/t3code@cd8c2c5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/AssistantSelectionToolbar.tsx +49-1Lines changed: 49 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import { | 12 | 12 | type AssistantCitationSourceAnchor, | 13 | 13 | } from "~/lib/assistantTextSelection"; | 14 | 14 | import { | | 15 | + isSelectionRectVisible, | 15 | 16 | observeSelectionActions, | 16 | 17 | resolveSelectionActionPosition, | 17 | 18 | type SelectionActionPoint, | @@ -56,7 +57,53 @@ export function AssistantSelectionToolbar({ | 56 | 57 | } | 57 | 58 | const rect = captured.range.getBoundingClientRect(); | 58 | 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) { | 60 | 107 | clear(); | 61 | 108 | return; | 62 | 109 | } | @@ -89,6 +136,7 @@ export function AssistantSelectionToolbar({ | 89 | 136 | const actions = observeSelectionActions({ | 90 | 137 | element: viewport, | 91 | 138 | getActionElement: () => toolbarRef.current, | | 139 | + restoreAfterScroll: true, | 92 | 140 | onSelection: update, | 93 | 141 | onDismiss: clear, | 94 | 142 | }); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/lib/selectionActions.test.ts +94-2Lines changed: 94 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | 2 | 2 |
| 3 | | -import { observeSelectionActions, resolveSelectionActionPosition } from "./selectionActions"; | | 3 | +import { | | 4 | + isSelectionRectVisible, | | 5 | + observeSelectionActions, | | 6 | + resolveSelectionActionPosition, | | 7 | +} from "./selectionActions"; | 4 | 8 |
| 5 | 9 | function event(type: string, values: Record<string, unknown> = {}) { | 6 | 10 | return Object.assign(new Event(type, { cancelable: true }), values); | 7 | 11 | } | 8 | 12 |
| 9 | | -function createSelectionSurface({ interactiveActions = false } = {}) { | | 13 | +function createSelectionSurface({ interactiveActions = false, restoreAfterScroll = false } = {}) { | 10 | 14 | const frames = new Map<number, FrameRequestCallback>(); | 11 | 15 | let frameId = 0; | 12 | 16 | const view = Object.assign(new EventTarget(), { | @@ -42,6 +46,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) { | 42 | 46 | ...(interactiveActions | 43 | 47 | ? { getActionElement: () => actionElement as unknown as HTMLElement } | 44 | 48 | : {}), | | 49 | + restoreAfterScroll, | 45 | 50 | onSelection, | 46 | 51 | onDismiss, | 47 | 52 | }); | @@ -294,6 +299,69 @@ describe("selection action gestures", () => { | 294 | 299 | expect(surface.onSelection).toHaveBeenCalledOnce(); | 295 | 300 | }); | 296 | 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 | + | 297 | 365 | it("cancels a queued animation frame when a new press supersedes the release", () => { | 298 | 366 | const surface = createSelectionSurface(); | 299 | 367 | surface.down(); | @@ -387,6 +455,30 @@ describe("interactive selection actions", () => { | 387 | 455 | }); | 388 | 456 | }); | 389 | 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 | + | 390 | 482 | describe("selection action positioning", () => { | 391 | 483 | const bounds = { left: 100, top: 50, width: 500, height: 220 }; | 392 | 484 | const viewport = { width: 1024, height: 768 }; | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/lib/selectionActions.ts +22-1Lines changed: 22 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | export type SelectionActionPoint = { x: number; y: number }; | 2 | 2 |
| 3 | 3 | export const SELECTION_MULTI_CLICK_INTERVAL_MS = 500; | | 4 | +const SELECTION_SCROLL_SETTLE_MS = 150; | 4 | 5 |
| 5 | 6 | export function resolveSelectionActionPosition(options: { | 6 | 7 | bounds: { left: number; top: number; width: number; height: number }; | @@ -25,18 +26,33 @@ export function resolveSelectionActionPosition(options: { | 25 | 26 | }; | 26 | 27 | } | 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 | + | 28 | 42 | /** | 29 | 43 | * Interactive popovers pass getActionElement to keep editing their fields from replacing the | 30 | 44 | * captured source selection. | 31 | 45 | */ | 32 | 46 | export function observeSelectionActions({ | 33 | 47 | element, | 34 | 48 | getActionElement, | | 49 | + restoreAfterScroll = false, | 35 | 50 | onSelection, | 36 | 51 | onDismiss, | 37 | 52 | }: { | 38 | 53 | element: HTMLElement; | 39 | 54 | getActionElement?: () => HTMLElement | null; | | 55 | + restoreAfterScroll?: boolean; | 40 | 56 | onSelection: (pointer: SelectionActionPoint | null) => void; | 41 | 57 | onDismiss: (reason: "interaction" | "cancel") => void; | 42 | 58 | }) { | @@ -45,6 +61,7 @@ export function observeSelectionActions({ | 45 | 61 | let pointerDown = false; | 46 | 62 | let gestureActive = false; | 47 | 63 | let dismissed = false; | | 64 | + let restorable = false; | 48 | 65 | let pointer: SelectionActionPoint | null = null; | 49 | 66 | let timer: number | null = null; | 50 | 67 | let frame: number | null = null; | @@ -62,6 +79,7 @@ export function observeSelectionActions({ | 62 | 79 | pointerDown = gestureActive = false; | 63 | 80 | pointer = null; | 64 | 81 | dismissed = true; | | 82 | + restorable = false; | 65 | 83 | }; | 66 | 84 | const dismiss = () => { | 67 | 85 | cancel(); | @@ -72,6 +90,7 @@ export function observeSelectionActions({ | 72 | 90 | onDismiss("interaction"); | 73 | 91 | }; | 74 | 92 | const schedule = (delay: number) => { | | 93 | + restorable = true; | 75 | 94 | cancelPending(); | 76 | 95 | timer = view.setTimeout(() => { | 77 | 96 | timer = null; | @@ -92,6 +111,7 @@ export function observeSelectionActions({ | 92 | 111 | const inside = element.contains(event.target as Node); | 93 | 112 | gestureActive = false; | 94 | 113 | dismissed = true; | | 114 | + restorable = false; | 95 | 115 | pointer = null; | 96 | 116 | if (inside) onDismiss("interaction"); | 97 | 117 | else if (getActionElement?.()) onDismiss("cancel"); | @@ -135,7 +155,8 @@ export function observeSelectionActions({ | 135 | 155 | // Autoscroll during a drag must not cancel that gesture's eventual release. | 136 | 156 | if (!pointerDown) { | 137 | 157 | pointer = null; | 138 | | - dismissed = true; | | 158 | + if (restoreAfterScroll && !dismissed && restorable) schedule(SELECTION_SCROLL_SETTLE_MS); | | 159 | + else dismissed = true; | 139 | 160 | } | 140 | 161 | onDismiss("cancel"); | 141 | 162 | }; | |
|