fix(webui): retain history touch gestures across message replacement

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-27 21:05:32 +08:00
1 parent 927eba89ea
commit b07b3eac24
2 files changed
+178 -11

No files matched your search

+130
View File
@@ -421,6 +421,136 @@ test("touch reveals a static partial ready header, arms only on release, and ret
} finally { await context.close(); }
});
for (const replacement of ["before move", "before release", "before cancel"] as const) {
test(`history touch continues when the touched message is replaced ${replacement}`, async ({ browser, baseURL }) => {
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, baseURL });
const page = await context.newPage();
const history = await installHistory(page);
let allowRenderer = () => {};
let rendererRequested = () => {};
const rendererGate = new Promise<void>((resolve) => { allowRenderer = resolve; });
const requested = new Promise<void>((resolve) => { rendererRequested = resolve; });
await page.route("**/src/components/MarkdownTextRenderer.tsx*", async (route) => {
rendererRequested();
await rendererGate;
await route.continue();
});
try {
const { body, header } = await openSettledHistory(page);
await positionAtTop(body);
await stableLayout(body);
await requested;
const fallback = body.locator('[data-assistant-selectable="true"] > .whitespace-pre-wrap:visible').first();
await expect(fallback).toBeVisible();
await expect(fallback).toContainText("Historical answer.");
const target = await fallback.elementHandle();
const box = await target?.boundingBox();
if (!target || !box) throw new Error("The Markdown fallback must be visible before the touch gesture");
const x = box.x + Math.min(50, box.width / 2), y = box.y + Math.min(10, box.height / 2);
expect(await target.evaluate((node, point) => document.elementFromPoint(point.x, point.y) === node, { x, y })).toBe(true);
const cdp = await context.newCDPSession(page);
const touch = (type: "touchStart" | "touchMove" | "touchEnd" | "touchCancel", touchY = y) => cdp.send("Input.dispatchTouchEvent", {
type, touchPoints: type === "touchEnd" || type === "touchCancel" ? [] : [{ x, y: touchY, id: 1 }],
});
await touch("touchStart");
if (replacement !== "before move") {
await touch("touchMove", y + (replacement === "before cancel" ? 200 : 40));
if (replacement === "before cancel") expect(await displacement(body)).toBeGreaterThanOrEqual(56);
else expect(await displacement(body)).toBeGreaterThan(0);
expect(history.reads).toHaveLength(0);
}
allowRenderer();
await expect.poll(() => target.evaluate((node) => node?.isConnected)).toBe(false);
if (replacement === "before move") {
await touch("touchMove", y + 200);
expect(await displacement(body)).toBeGreaterThanOrEqual(56);
expect(history.reads).toHaveLength(0);
}
await touch(replacement === "before cancel" ? "touchCancel" : "touchEnd");
if (replacement !== "before move") {
await expect.poll(() => displacement(body)).toBe(0);
await expect(header).not.toBeInViewport();
expect(history.reads).toHaveLength(0);
}
if (replacement === "before cancel") {
// Cancellation must not leave a stale gesture that prevents the next pull.
await touch("touchStart");
await touch("touchMove", y + 200);
expect(await displacement(body)).toBeGreaterThanOrEqual(56);
expect(history.reads).toHaveLength(0);
await touch("touchEnd");
}
if (replacement !== "before release") {
await expect.poll(() => history.reads.length).toBe(1);
await expect(header).toHaveAttribute("data-state", "loading");
history.complete();
}
await target.dispose();
} finally {
allowRenderer();
await context.close();
}
});
}
test("a second finger cancels an active history pull without loading", async ({ browser, baseURL }) => {
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, baseURL });
const page = await context.newPage();
const history = await installHistory(page);
try {
const { body, header } = await openSettledHistory(page);
await positionAtTop(body);
const cdp = await context.newCDPSession(page);
const dispatch = (type: "touchStart" | "touchMove" | "touchEnd", touchPoints: { id: number; x: number; y: number }[]) =>
cdp.send("Input.dispatchTouchEvent", { type, touchPoints });
await dispatch("touchStart", [{ id: 1, x: 180, y: 200 }]);
await dispatch("touchMove", [{ id: 1, x: 180, y: 240 }]);
expect(await displacement(body)).toBeGreaterThan(0);
await dispatch("touchStart", [{ id: 1, x: 180, y: 240 }, { id: 2, x: 220, y: 240 }]);
await expect.poll(() => displacement(body)).toBe(0);
await expect(header).not.toBeInViewport();
expect(history.reads).toHaveLength(0);
await dispatch("touchEnd", []);
await dispatch("touchStart", [{ id: 1, x: 180, y: 200 }]);
await dispatch("touchMove", [{ id: 1, x: 180, y: 400 }]);
expect(await displacement(body)).toBeGreaterThanOrEqual(56);
await dispatch("touchEnd", []);
await expect.poll(() => history.reads.length).toBe(1);
history.complete();
} finally { await context.close(); }
});
test("switching conversations cancels an in-progress history pull", async ({ browser, baseURL }) => {
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, baseURL });
const page = await context.newPage();
const history = await installHistory(page);
try {
const { body } = await openSettledHistory(page);
await positionAtTop(body);
const cdp = await context.newCDPSession(page);
const touch = (type: "touchStart" | "touchMove" | "touchEnd", y = 200) => cdp.send("Input.dispatchTouchEvent", {
type, touchPoints: type === "touchEnd" ? [] : [{ id: 1, x: 180, y }],
});
await touch("touchStart");
await touch("touchMove", 240);
expect(await displacement(body)).toBeGreaterThan(0);
await page.getByRole("button", { name: "Switch conversation" }).click();
await expect(body.getByText("Question 1", { exact: true })).toBeVisible();
await touch("touchEnd");
await expect.poll(() => displacement(body)).toBe(0);
expect(history.reads).toHaveLength(0);
await page.getByRole("button", { name: "Switch conversation" }).click();
await expect(body.getByText("Question 40", { exact: true })).toBeVisible();
await positionAtTop(body);
await touch("touchStart");
await touch("touchMove", 400);
expect(await displacement(body)).toBeGreaterThanOrEqual(56);
await touch("touchEnd");
await expect.poll(() => history.reads.length).toBe(1);
history.complete();
} finally { await context.close(); }
});
test("reduced-motion failure keeps its functional 48px slot and retry until naturally scrolled away", async ({ page }) => {
await page.emulateMedia({ reducedMotion: "reduce", colorScheme: "dark" });
const history = await installHistory(page);
+48 -11
View File
@@ -321,8 +321,13 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
const historySlotRef = useRef<HTMLDivElement | null>(null);
const historyInputRef = useRef<"touch" | "other">("other");
const historyTouchRef = useRef<{
lastY: number; startX: number; startY: number; pulling: boolean; horizontal: boolean;
target: Node; lastY: number; startX: number; startY: number; pulling: boolean; horizontal: boolean;
} | null>(null);
const historyTouchTargetCleanupRef = useRef<(() => void) | null>(null);
const clearHistoryTouchTarget = useCallback(() => {
historyTouchTargetCleanupRef.current?.();
historyTouchTargetCleanupRef.current = null;
}, []);
const historyDistanceRef = useRef(0);
const historyStatusRef = useRef<HTMLDivElement | null>(null);
const historySpringRef = useRef<ThreadHistorySpring | null>(null);
@@ -693,6 +698,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
useLayoutEffect(() => {
if (lastConversationKeyRef.current === conversationKey) return;
lastConversationKeyRef.current = conversationKey;
clearHistoryTouchTarget();
historyTouchRef.current = null;
historyInputRef.current = "other";
resetHistoryHeader();
@@ -719,7 +725,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
fill: "forwards",
},
);
}, [conversationKey, hasMessages, resetHistoryHeader]);
}, [clearHistoryTouchTarget, conversationKey, hasMessages, resetHistoryHeader]);
useLayoutEffect(() => {
if (!conversationReady) {
@@ -991,21 +997,28 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
}
};
const handleTouchCancel = () => {
clearHistoryTouchTarget();
historyTouchRef.current = null;
historySpringRef.current?.endTouch(true);
};
const handleTouchStart = (event: TouchEvent) => {
historyInputRef.current = "touch";
const touch = event.touches[0];
historyTouchRef.current = event.touches.length === 1 && touch
? {
lastY: touch.clientY,
startX: touch.clientX,
startY: touch.clientY,
pulling: false,
horizontal: false,
}
: null;
if (event.touches.length !== 1 || !touch) {
handleTouchCancel();
return;
}
clearHistoryTouchTarget();
const target = event.target as Node;
historyTouchRef.current = {
target,
lastY: touch.clientY,
startX: touch.clientX,
startY: touch.clientY,
pulling: false,
horizontal: false,
};
observeHistoryTouchTarget(target);
};
const handleTouchMove = (event: TouchEvent) => {
const gesture = historyTouchRef.current;
@@ -1037,9 +1050,30 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
};
const handleTouchEnd = () => {
const pulling = historyTouchRef.current?.pulling;
clearHistoryTouchTarget();
historyTouchRef.current = null;
if (historySpringRef.current?.endTouch() && pulling && canLoadAtTop()) loadEarlierMessages();
};
const observeHistoryTouchTarget = (target: Node) => {
if (target === el) return;
// Touch events remain targeted at the original node even after React replaces it.
// Keep ordinary connected events on the existing viewport path and only forward
// events that can no longer reach it through DOM propagation.
const detached = (handler: (event: TouchEvent) => void) => (event: Event) => {
if (historyTouchRef.current?.target === target && !event.composedPath().includes(el)) handler(event as TouchEvent);
};
const move = detached(handleTouchMove);
const end = detached(handleTouchEnd);
const cancel = detached(handleTouchCancel);
target.addEventListener("touchmove", move, { passive: false });
target.addEventListener("touchend", end, { passive: true });
target.addEventListener("touchcancel", cancel, { passive: true });
historyTouchTargetCleanupRef.current = () => {
target.removeEventListener("touchmove", move);
target.removeEventListener("touchend", end);
target.removeEventListener("touchcancel", cancel);
};
};
const handleKeyDown = (event: KeyboardEvent) => {
if (
event.defaultPrevented
@@ -1061,6 +1095,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
historyInputRef.current = "other";
handleDirectionalInput(keyboardScrollDirection(event));
};
if (historyTouchRef.current) observeHistoryTouchTarget(historyTouchRef.current.target);
el.addEventListener("scroll", handleScroll, { passive: true });
el.addEventListener("wheel", handleWheel, { passive: true });
el.addEventListener("touchstart", handleTouchStart, { passive: true });
@@ -1070,6 +1105,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
el.addEventListener("pointerdown", handlePointerDown);
el.addEventListener("keydown", handleKeyDown);
return () => {
clearHistoryTouchTarget();
el.removeEventListener("scroll", handleScroll);
el.removeEventListener("wheel", handleWheel);
el.removeEventListener("touchstart", handleTouchStart);
@@ -1081,6 +1117,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
};
}, [
captureHistoryScrollAnchor,
clearHistoryTouchTarget,
conversationReady,
hasMessages,
hasMoreBefore,