Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/web/src/components/chat/timelineViewport.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 &&
Expand All @@ -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,
Expand Down Expand Up @@ -202,6 +205,7 @@ export function attachTimelineViewport(
},
dispose: () => {
window.removeEventListener("message", handleFrameResize, true);
selectionDrag.dispose();
listeners.clear();
followReleasedListeners.clear();
scrollNode.removeEventListener("scroll", handleScroll);
Expand Down
114 changes: 114 additions & 0 deletions apps/web/src/fork/timelineSelectionDrag.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<div data-testid="above" style={{ paddingTop: 120 }}>
<div
data-testid="timeline"
style={{ height: 300, width: 400, overflowY: "auto", overflowAnchor: "none" }}
>
<div data-testid="tail" style={{ height: 900 }} />
<p style={{ margin: 0 }}>{words}</p>
<p data-testid="last" style={{ margin: 0 }}>
{words}
</p>
</div>
</div>,
// 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<void>((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);
});
});
62 changes: 62 additions & 0 deletions apps/web/src/fork/timelineSelectionDrag.ts
Original file line number Diff line number Diff line change
@@ -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);
},
};
}
Loading