From 379a6867f203815edb92beeee74449aeadcd26c9 Mon Sep 17 00:00:00 2001 From: Renfei Zhou Date: Thu, 19 Nov 2026 04:44:35 +0000 Subject: [PATCH 1/2] test(web): a selection dragged to the chat's top edge releases follow The browser autoscrolls the timeline while the reader drags a selection at its top edge. The viewport sends no navigation for that scroll, so the chat keeps following the end. A clamp during a press, or after the press ends, must still send none. Co-Authored-By: Claude Opus 5.5 --- .../timelineSelectionDrag.browser.test.tsx | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 apps/web/src/fork/timelineSelectionDrag.browser.test.tsx diff --git a/apps/web/src/fork/timelineSelectionDrag.browser.test.tsx b/apps/web/src/fork/timelineSelectionDrag.browser.test.tsx new file mode 100644 index 0000000000000..ce59a975f3107 --- /dev/null +++ b/apps/web/src/fork/timelineSelectionDrag.browser.test.tsx @@ -0,0 +1,114 @@ +import { describe, expect, it, vi } from "vite-plus/test"; +import { cdp } from "vite-plus/test/browser"; + +import { render } from "../../test/render"; +import { attachTimelineViewport } from "../components/chat/timelineViewport"; + +const words = "the reader selects a line of this answer to cite it in a reply ".repeat(40); + +describe("timeline selection drag", () => { + it("reports the autoscroll of a selection dragged to the top edge, not a clamp during a press", async () => { + await using screen = await render( +
+
+
+

{words}

+

+ {words} +

+
+
, + // A plain scroller; the viewport needs no routes. + { router: false }, + ); + const timeline = screen.getByTestId("timeline").element() as HTMLElement; + const last = screen.getByTestId("last").element() as HTMLElement; + const tail = screen.getByTestId("tail").element() as HTMLElement; + const above = screen.getByTestId("above").element() as HTMLElement; + const frame = window.frameElement?.getBoundingClientRect(); + const mouse = (type: "mousePressed" | "mouseMoved" | "mouseReleased", x: number, y: number) => + cdp().send("Input.dispatchMouseEvent", { + type, + x: x + (frame?.left ?? 0), + y: y + (frame?.top ?? 0), + button: "left", + buttons: type === "mouseReleased" ? 0 : 1, + clickCount: 1, + }); + const scrolled = () => + new Promise((resolve) => + timeline.addEventListener("scroll", () => resolve(), { once: true }), + ); + const toEnd = async () => { + const scroll = scrolled(); + timeline.scrollTop = timeline.scrollHeight; + await scroll; + }; + // Content that shrinks and grows back in one task clamps scrollTop with no input at all. + const clamp = async () => { + const scroll = scrolled(); + tail.style.height = "100px"; + void timeline.scrollTop; + tail.style.height = "900px"; + await scroll; + }; + // A line of the last paragraph in the scrolled-to-end view, below the autoscroll edge. + const textPoint = () => { + const box = timeline.getBoundingClientRect(); + return { x: box.left + 40, y: box.top + 60 }; + }; + + await using stack = new AsyncDisposableStack(); + await toEnd(); + const viewport = attachTimelineViewport(timeline); + stack.defer(() => viewport.dispose()); + stack.defer(async () => { + await mouse("mouseReleased", 0, 0); + document.getSelection()?.removeAllRanges(); + }); + const sources: string[] = []; + viewport.subscribe((navigation) => { + if (navigation.source === "scroll") sources.push(navigation.source); + }); + + const start = textPoint(); + // A still press near the top that layout moves above the timeline's edge is no drag. + const nearTop = { x: start.x, y: timeline.getBoundingClientRect().top + 10 }; + document.getSelection()!.selectAllChildren(last); + await mouse("mousePressed", nearTop.x, nearTop.y); + above.style.paddingTop = "160px"; + await clamp(); + await mouse("mouseReleased", nearTop.x, nearTop.y); + above.style.paddingTop = "120px"; + expect(sources).toEqual([]); + + await toEnd(); + await mouse("mousePressed", start.x, start.y); + await mouse("mouseMoved", start.x + 120, start.y + 60); + expect(document.getSelection()!.isCollapsed).toBe(false); + await clamp(); + await mouse("mouseReleased", start.x + 120, start.y + 60); + expect(sources).toEqual([]); + + await toEnd(); + const end = timeline.scrollTop; + // A press on selected text would start a drag of that text instead of a new selection. + document.getSelection()!.removeAllRanges(); + await mouse("mousePressed", start.x, start.y); + // Chromium autoscrolls while the pointer is just inside the top edge. + const edge = { x: start.x + 60, y: timeline.getBoundingClientRect().top + 5 }; + await mouse("mouseMoved", edge.x, edge.y); + await vi.waitFor(() => expect(timeline.scrollTop).toBeLessThan(end - 100), { timeout: 10_000 }); + await mouse("mouseReleased", edge.x, edge.y); + const dragScrolls = sources.length; + expect(dragScrolls).toBeGreaterThan(0); + + // The press ended, so a clamp with the selection still in place is no drag. + await toEnd(); + await clamp(); + expect(sources).toHaveLength(dragScrolls); + }); +}); From a5a58cac1e0686067aeb50d180968d25fbd32f87 Mon Sep 17 00:00:00 2001 From: Renfei Zhou Date: Thu, 19 Nov 2026 04:45:33 +0000 Subject: [PATCH 2/2] fix(web): the chat keeps the reader in place after a selection drag A reader who drags a text selection to the top of the chat makes the browser scroll it up, but follow stayed on. Narrowing the window or a new message then scrolled the chat to the end. An upward scroll now counts as the reader moving away while the primary press has moved into the browser's autoscroll band at the top edge and holds a text selection in the chat. A press held still during a content clamp still counts as nothing. Co-Authored-By: Claude Opus 5.5 --- .../src/components/chat/timelineViewport.ts | 10 ++- apps/web/src/fork/timelineSelectionDrag.ts | 62 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 apps/web/src/fork/timelineSelectionDrag.ts diff --git a/apps/web/src/components/chat/timelineViewport.ts b/apps/web/src/components/chat/timelineViewport.ts index 65e0f50f5ae9e..2649d2b6c89b7 100644 --- a/apps/web/src/components/chat/timelineViewport.ts +++ b/apps/web/src/components/chat/timelineViewport.ts @@ -1,5 +1,6 @@ import { readHtmlRenderContentHeight } from "@t3tools/shared/htmlRender"; +import { observeTimelineSelectionDrag } from "../../fork/timelineSelectionDrag"; import { isInSideChatPane } from "../../lib/sideChatPane"; import { registerCommands } from "../../shortcuts/dispatcher"; import { @@ -131,8 +132,10 @@ export function attachTimelineViewport( top: scrollNode.scrollTop, height: scrollNode.scrollHeight, }); - // Only a frame's input reaches the timeline unseen. Any other unexplained upward move is not - // the reader's: the browser clamps scrollTop when content briefly shrinks. + // A frame's input and a dragged text selection reach the timeline unseen. Any other + // unexplained upward move is not the reader's: the browser clamps scrollTop when content + // briefly shrinks. + const selectionDrag = observeTimelineSelectionDrag(scrollNode); const frameHasInput = () => scrollNode.querySelector("iframe:hover") !== null || (document.activeElement instanceof HTMLIFrameElement && @@ -144,7 +147,7 @@ export function attachTimelineViewport( height: scrollNode.scrollHeight, programmatic: programmaticHolds > 0, }) && - frameHasInput() && + (frameHasInput() || selectionDrag.isActive()) && // LegendList updates its scroll state on the next animation frame. resolveTimelineIsAtEnd({ contentLength: scrollNode.scrollHeight, @@ -202,6 +205,7 @@ export function attachTimelineViewport( }, dispose: () => { window.removeEventListener("message", handleFrameResize, true); + selectionDrag.dispose(); listeners.clear(); followReleasedListeners.clear(); scrollNode.removeEventListener("scroll", handleScroll); diff --git a/apps/web/src/fork/timelineSelectionDrag.ts b/apps/web/src/fork/timelineSelectionDrag.ts new file mode 100644 index 0000000000000..b4f7fdce8289d --- /dev/null +++ b/apps/web/src/fork/timelineSelectionDrag.ts @@ -0,0 +1,62 @@ +// Chromium starts selection autoscroll this many device px inside a scroller's edge. Zoomed +// out, that is more CSS px; zoomed in, 20 CSS px still covers it. +const AUTOSCROLL_EDGE_DEVICE_PX = 20; + +/** + * Whether the browser is autoscrolling the timeline for a text selection the reader drags + * to its top edge. That scroll comes with no wheel, key or touch event. + */ +export function observeTimelineSelectionDrag(scrollNode: HTMLElement): { + readonly isActive: () => boolean; + readonly dispose: () => void; +} { + // Measured on each move, so layout moving under a still pointer is no drag. + let press: { readonly pointerId: number; readonly atEdge: boolean } | null = null; + const end = () => { + press = null; + window.removeEventListener("pointermove", handlePointerMove, true); + window.removeEventListener("pointerup", handlePointerEnd, true); + window.removeEventListener("pointercancel", handlePointerEnd, true); + }; + const handlePointerMove = (event: PointerEvent) => { + if (event.pointerId !== press?.pointerId) return; + // A chorded press releases the primary button with a move, not a pointerup. + if ((event.buttons & 1) === 0) end(); + else { + const edge = AUTOSCROLL_EDGE_DEVICE_PX / Math.min(1, window.devicePixelRatio); + const atEdge = event.clientY <= scrollNode.getBoundingClientRect().top + edge; + press = { pointerId: event.pointerId, atEdge }; + } + }; + const handlePointerEnd = (event: PointerEvent) => { + if (event.pointerId === press?.pointerId) end(); + }; + const handlePointerDown = (event: PointerEvent) => { + if (event.button !== 0 || !event.isPrimary) return; + end(); + press = { pointerId: event.pointerId, atEdge: false }; + // The drag goes on outside the timeline, and the release can land outside the window. + window.addEventListener("pointermove", handlePointerMove, { capture: true, passive: true }); + window.addEventListener("pointerup", handlePointerEnd, { capture: true, passive: true }); + window.addEventListener("pointercancel", handlePointerEnd, { capture: true, passive: true }); + }; + scrollNode.addEventListener("pointerdown", handlePointerDown, { passive: true }); + // A release in another window or tab reaches this page as a blur only. + window.addEventListener("blur", end); + return { + // The browser's clamp after content shrinks moves scrollTop during a press too, but the + // pointer is not dragging at the top edge then. + isActive: () => { + if (!press?.atEdge) return false; + const selection = scrollNode.ownerDocument.getSelection(); + return ( + selection !== null && !selection.isCollapsed && scrollNode.contains(selection.anchorNode) + ); + }, + dispose: () => { + end(); + scrollNode.removeEventListener("pointerdown", handlePointerDown); + window.removeEventListener("blur", end); + }, + }; +}