From fd30fda8a295cd5d20910ab6bb50c9686079fb78 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 21 Oct 2026 10:32:00 +0800 Subject: [PATCH] fix(webui): stabilize transcript while typing multiline drafts --- webui/browser/composer-layout.spec.ts | 134 ++++++++++++++++++ .../src/components/thread/ThreadComposer.tsx | 74 +++++----- .../src/components/thread/ThreadViewport.tsx | 15 -- webui/src/tests/thread-viewport.test.tsx | 30 ---- 4 files changed, 167 insertions(+), 86 deletions(-) create mode 100644 webui/browser/composer-layout.spec.ts diff --git a/webui/browser/composer-layout.spec.ts b/webui/browser/composer-layout.spec.ts new file mode 100644 index 000000000..9eef51384 --- /dev/null +++ b/webui/browser/composer-layout.spec.ts @@ -0,0 +1,134 @@ +import { expect, test, type Page } from "@playwright/test"; +import { canonicalThreadPayload } from "../src/tests/thread-test-payload"; + +const history = canonicalThreadPayload({ + schemaVersion: 3, + messages: [ + { id: "question", role: "user", content: "Explain stable layout.", createdAt: 1_700_000_000_000 }, + { + id: "answer", role: "assistant", createdAt: 1_700_000_001_000, + content: Array.from({ length: 25 }, (_, index) => + `## Section ${index + 1}\n\nA completed answer stays in place while the next draft is edited. 中文与 English 混排,保持阅读位置稳定。`, + ).join("\n\n"), + }, + ], +}); + +async function openThread(page: Page) { + await page.addInitScript(() => localStorage.setItem("nanobot.locale", "en")); + await page.route("**/api/commands", route => route.fulfill({ json: { commands: [] } })); + await page.route("**/api/sessions/*/webui-thread?*", route => route.fulfill({ json: history })); + await page.goto("/browser/fixture.html?case=thread-loading"); + await expect(page.getByRole("heading", { name: "Section 25", exact: true })).toBeVisible(); + await page.evaluate(() => document.fonts.ready); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.fill("Next steps:\n1. Check layout\n2. "); + await input.focus(); + const scroller = page.getByTestId("thread-message-region"); + await expect.poll(() => scroller.evaluate(node => node.scrollHeight - node.clientHeight - node.scrollTop)).toBeLessThanOrEqual(1); + return { input, scroller }; +} + +async function sampleWhileTyping(page: Page) { + return page.evaluate(async () => { + const scroller = document.querySelector(".thread-message-viewport")!; + const input = document.querySelector("textarea")!; + const baseline = { top: scroller.scrollTop, inputHeight: input.clientHeight, viewportHeight: scroller.clientHeight }; + const samples: typeof baseline[] = []; + let active = true; + const sample = () => { + samples.push({ top: scroller.scrollTop, inputHeight: input.clientHeight, viewportHeight: scroller.clientHeight }); + if (active) requestAnimationFrame(sample); + }; + requestAnimationFrame(sample); + return new Promise<{ baseline: typeof baseline; samples: typeof samples }>(resolve => { + document.addEventListener("typing-sample-end", async () => { + await new Promise(requestAnimationFrame); + active = false; + resolve({ baseline, samples }); + }, { once: true }); + }); + }); +} + +for (const width of [1500, 390]) for (const readingHistory of [false, true]) { + test(`typing preserves every rendered transcript position at ${width}px ${readingHistory ? "in history" : "at the tail"}`, async ({ page }) => { + await page.setViewportSize({ width, height: 940 }); + const { input, scroller } = await openThread(page); + if (readingHistory) { + const box = (await scroller.boundingBox())!; + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); + await page.mouse.wheel(0, -350); + await expect.poll(() => scroller.evaluate(node => node.scrollHeight - node.clientHeight - node.scrollTop)).toBeGreaterThan(300); + } + const sampling = sampleWhileTyping(page); + await input.pressSequentially("abcdefghijklmnopqrstuvwxyz", { delay: 75 }); + await page.evaluate(() => document.dispatchEvent(new Event("typing-sample-end"))); + const { baseline, samples } = await sampling; + expect(samples.length).toBeGreaterThan(10); + expect(Math.max(...samples.map(sample => Math.abs(sample.top - baseline.top)))).toBeLessThanOrEqual(1); + expect(samples.every(sample => sample.inputHeight === baseline.inputHeight && sample.viewportHeight === baseline.viewportHeight)).toBe(true); + }); +} + +test("autosizing still grows, caps and shrinks the real input", async ({ page }) => { + const { input, scroller } = await openThread(page); + const initialHeight = await input.evaluate(node => node.clientHeight); + await input.fill(Array.from({ length: 6 }, (_, index) => `Line ${index + 1}`).join("\n")); + await expect.poll(() => input.evaluate(node => node.clientHeight)).toBeGreaterThan(initialHeight); + expect(await input.evaluate(node => node.scrollHeight <= node.clientHeight + 1)).toBe(true); + await input.fill(Array.from({ length: 20 }, (_, index) => `Line ${index + 1}`).join("\n")); + await expect(input).toHaveCSS("height", "260px"); + expect(await input.evaluate(node => node.scrollHeight > node.clientHeight)).toBe(true); + await input.press("Control+End"); + await expect.poll(() => input.evaluate(node => node.scrollTop)).toBeGreaterThan(0); + await input.fill("Short draft"); + await expect.poll(() => input.evaluate(node => node.clientHeight)).toBeLessThan(initialHeight); + expect(await input.evaluate(node => node.scrollHeight <= node.clientHeight + 1)).toBe(true); + await expect.poll(() => scroller.evaluate(node => node.scrollHeight - node.clientHeight - node.scrollTop)).toBeLessThanOrEqual(1); +}); + +test("Chromium IME composition and commit keep the transcript stationary", async ({ page }) => { + const { input } = await openThread(page); + const original = await input.inputValue(); + const cdp = await page.context().newCDPSession(page); + const sampling = sampleWhileTyping(page); + for (const text of ["z", "zhong", "zhongwen"]) { + await cdp.send("Input.imeSetComposition", { text, selectionStart: text.length, selectionEnd: text.length }); + await page.evaluate(() => new Promise(requestAnimationFrame)); + } + await expect(input).toHaveValue(`${original}zhongwen`); + await cdp.send("Input.insertText", { text: "中文" }); + await expect(input).toHaveValue(`${original}中文`); + await page.evaluate(() => document.dispatchEvent(new Event("typing-sample-end"))); + const { baseline, samples } = await sampling; + expect(samples.length).toBeGreaterThan(0); + expect(Math.max(...samples.map(sample => Math.abs(sample.top - baseline.top)))).toBeLessThanOrEqual(1); + expect(samples.every(sample => sample.inputHeight === baseline.inputHeight)).toBe(true); + await cdp.detach(); +}); + + +test("multiline drafts resize after reading typography changes", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + await page.addInitScript(() => localStorage.setItem("nanobot.locale", "en")); + await page.goto("/browser/fixture.html?case=typography"); + const input = page.getByRole("textbox", { name: "Message input", exact: true }); + await input.fill("First line\nSecond line\nThird line"); + const standardHeight = await input.evaluate(node => node.clientHeight); + const sizes = [ + ["large", "Large reading text", 20], + ["small", "Small reading text", 16], + ["standard", "Standard reading text", 18], + ] as const; + for (const [size, label, fontSize] of sizes) { + await page.getByRole("radio", { name: label, exact: true }).click(); + await expect(page.locator("html")).toHaveAttribute("data-reading-text-size", size); + await expect(input).toHaveCSS("font-size", `${fontSize}px`); + expect(await input.evaluate(node => node.scrollHeight <= node.clientHeight + 1)).toBe(true); + const height = await input.evaluate(node => node.clientHeight); + if (size === "large") expect(height).toBeGreaterThan(standardHeight); + else if (size === "small") expect(height).toBeLessThan(standardHeight); + else expect(height).toBe(standardHeight); + } +}); diff --git a/webui/src/components/thread/ThreadComposer.tsx b/webui/src/components/thread/ThreadComposer.tsx index 01c4010bc..c5a0a4b84 100644 --- a/webui/src/components/thread/ThreadComposer.tsx +++ b/webui/src/components/thread/ThreadComposer.tsx @@ -1024,6 +1024,20 @@ function GoalStateStrip({ ); } +function autosizeTextarea(input: HTMLTextAreaElement) { + const container = input.parentElement!; + const previousMinHeight = container.style.minHeight; + // Measuring the one-row textarea must not expand the sibling transcript and + // clamp its native scroll offset, even temporarily before paint. + container.style.minHeight = `${container.getBoundingClientRect().height}px`; + try { + input.style.height = "auto"; + input.style.height = `${Math.min(input.scrollHeight, 260)}px`; + } finally { + container.style.minHeight = previousMinHeight; + } +} + export function ThreadComposer({ onSend, disabled, @@ -1130,10 +1144,7 @@ export function ThreadComposer({ previousWidth = width; setCompactControls(width <= 512); const input = textareaRef.current; - if (input) { - input.style.height = "auto"; - input.style.height = `${Math.min(input.scrollHeight, 260)}px`; - } + if (input) autosizeTextarea(input); }; update(); const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update); @@ -1735,27 +1746,22 @@ export function ThreadComposer({ }; }, [filteredMentionCandidates.length, filteredSlashCommands.length, showAnyPalette]); - const resizeTextarea = useCallback((restoreFocus = true) => { - requestAnimationFrame(() => { - const el = textareaRef.current; - if (!el) return; - el.style.height = "auto"; - el.style.height = `${Math.min(el.scrollHeight, 260)}px`; - if (restoreFocus) el.focus(); - }); + const focusTextarea = useCallback(() => { + requestAnimationFrame(() => textareaRef.current?.focus()); }, []); useLayoutEffect(() => { const el = textareaRef.current; if (!el || mentionInput.isComposing) return; - el.style.height = "auto"; - el.style.height = `${Math.min(el.scrollHeight, 260)}px`; + autosizeTextarea(el); if (mentionOverlayRef.current) mentionOverlayRef.current.scrollTop = el.scrollTop; }, [compactControls, mentionInput.isComposing, mentionInput.value]); useEffect(() => { - resizeTextarea(false); - }, [readingTextSize, resizeTextarea]); + // The parent projects typography tokens in its layout effect; measure after it. + const input = textareaRef.current; + if (input) autosizeTextarea(input); + }, [readingTextSize]); // Runs before paint so switching sessions never flashes stale draft text. useLayoutEffect(() => { @@ -1768,12 +1774,6 @@ export function ThreadComposer({ setCliAppMenuDismissed(false); setCursorPosition(0); clear(); - requestAnimationFrame(() => { - const el = textareaRef.current; - if (!el) return; - el.style.height = "auto"; - el.style.height = `${Math.min(el.scrollHeight, 260)}px`; - }); }, [clear, pendingQueueKey]); const appendTranscription = useCallback((text: string) => { @@ -1788,8 +1788,8 @@ export function ThreadComposer({ setSlashMenuDismissed(false); setCliAppMenuDismissed(false); setInlineError(null); - resizeTextarea(); - }, [resizeTextarea]); + focusTextarea(); + }, [focusTextarea]); const clearVoiceErrorTimers = useCallback(() => { if (voiceErrorFadeTimerRef.current !== null) clearTimeout(voiceErrorFadeTimerRef.current); @@ -1872,7 +1872,7 @@ export function ThreadComposer({ setSlashMenuDismissed(true); setCliAppMenuDismissed(false); setInlineError(null); - resizeTextarea(); + focusTextarea(); return; } @@ -1895,9 +1895,9 @@ export function ThreadComposer({ setSlashMenuDismissed(true); setCliAppMenuDismissed(false); setInlineError(null); - resizeTextarea(); + focusTextarea(); }, - [handleStop, isStreaming, onStop, recentSlashCommands, replaceMentionInput, resizeTextarea, skillQuery, value], + [handleStop, isStreaming, onStop, recentSlashCommands, replaceMentionInput, focusTextarea, skillQuery, value], ); const insertMention = useCallback( @@ -1907,9 +1907,9 @@ export function ThreadComposer({ setCliAppMenuDismissed(true); setSlashMenuDismissed(false); setInlineError(null); - resizeTextarea(); + focusTextarea(); }, - [replaceMentionInput, resizeTextarea, value], + [replaceMentionInput, focusTextarea, value], ); const chooseMentionCandidate = useCallback( @@ -1982,8 +1982,8 @@ export function ThreadComposer({ setSlashMenuDismissed(false); setCliAppMenuDismissed(false); setCursorPosition(0); - resizeTextarea(restoreFocus); - }, [resizeTextarea]); + if (restoreFocus) focusTextarea(); + }, [focusTextarea]); const queuedPromptError = useCallback((prompt: QueuedPrompt): string | null => { if (utf8Bytes(formatQuotedUserMessage(prompt.text, prompt.quotedContext)) > maxTextBytes) { @@ -2053,8 +2053,8 @@ export function ThreadComposer({ } else { clear(); } - resizeTextarea(); - }, [clear, collectGuidancePrompt, hasComposerContent, normalizedQuotedContext, onQuotedContextChange, replaceMentionInput, resizeTextarea, restoreReadyImages, setQueuedPrompt]); + focusTextarea(); + }, [clear, collectGuidancePrompt, hasComposerContent, normalizedQuotedContext, onQuotedContextChange, replaceMentionInput, focusTextarea, restoreReadyImages, setQueuedPrompt]); const sendQueuedPrompt = useCallback( (prompt: QueuedPrompt, extraOptions?: SendOptions) => { @@ -2337,13 +2337,6 @@ export function ThreadComposer({ } }; - const onInput: React.FormEventHandler = (e) => { - if ((e.nativeEvent as InputEvent).isComposing) return; - const el = e.currentTarget; - el.style.height = "auto"; - el.style.height = `${Math.min(el.scrollHeight, 260)}px`; - }; - const onFilePick: React.ChangeEventHandler = (e) => { const files = Array.from(e.target.files ?? []); e.target.value = ""; @@ -2665,7 +2658,6 @@ export function ThreadComposer({ onBlur={() => { secondEnterPromptIdRef.current = null; }} - onInput={onInput} onKeyDown={onKeyDown} onKeyUp={() => setCursorPosition(rawSelection().start)} onSelect={() => setCursorPosition(rawSelection().start)} diff --git a/webui/src/components/thread/ThreadViewport.tsx b/webui/src/components/thread/ThreadViewport.tsx index 9d678742a..e9267949e 100644 --- a/webui/src/components/thread/ThreadViewport.tsx +++ b/webui/src/components/thread/ThreadViewport.tsx @@ -299,7 +299,6 @@ export const ThreadViewport = forwardRef(null); const historyScrollAnchorRef = useRef(null); - const composerInputScrollTopRef = useRef(null); const composerDockHeightRef = useRef(0); const [atBottom, setAtBottom] = useState(true); const [atHistoryTop, setAtHistoryTop] = useState(false); @@ -1116,23 +1115,9 @@ export const ThreadViewport = forwardRef { if (event.target instanceof HTMLTextAreaElement) { - composerInputScrollTopRef.current = scrollRef.current?.scrollTop ?? null; threadMotionRef.current?.handleComposerInput(); } }} - onInput={(event) => { - if (!(event.target instanceof HTMLTextAreaElement)) return; - const previousScrollTop = composerInputScrollTopRef.current; - composerInputScrollTopRef.current = null; - const scrollEl = scrollRef.current; - if (scrollEl && previousScrollTop !== null) { - // Textarea autosizing briefly collapses to `height: auto` while - // measuring. Chrome can clamp the sibling thread scrollport in - // that intermediate layout; restore it before paint, then let - // ResizeObserver handle any real final composer height change. - scrollEl.scrollTop = previousScrollTop; - } - }} className={cn( "row-start-2 z-10 w-full", hasThreadLayout ? "thread-composer-dock relative" : "relative self-center", diff --git a/webui/src/tests/thread-viewport.test.tsx b/webui/src/tests/thread-viewport.test.tsx index 9ced3e8a3..b0521ee2f 100644 --- a/webui/src/tests/thread-viewport.test.tsx +++ b/webui/src/tests/thread-viewport.test.tsx @@ -1234,36 +1234,6 @@ describe("ThreadViewport", () => { } }); - it("restores thread scroll after the textarea autosize measurement collapses it", () => { - let scroller: HTMLElement | null = null; - const { container } = render( - { - if (scroller) scroller.scrollTop = 692; - }} - /> - )} - />, - ); - scroller = getScroller(container); - Object.defineProperty(scroller, "scrollTop", { - configurable: true, - writable: true, - value: 700, - }); - - fireEvent.input(screen.getByLabelText("Message input"), { - target: { value: "中文" }, - }); - - expect(scroller.scrollTop).toBe(700); - }); - it("keeps the thread scrollport above a mobile soft keyboard", async () => { const visualViewport = stubVisualViewport({ innerHeight: 800, height: 480 }); try {