perf(webui): avoid redundant math layout setup

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-24 20:56:12 +08:00
1 parent 336572de29
commit 24dd740d5b
5 files changed
+118 -15

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
import { useState } from "react";
import { MarkdownText } from "@/components/MarkdownText";
export function DisplayMathFixture() {
const [step, setStep] = useState(0);
const [streaming, setStreaming] = useState(true);
const terms = step === 0 ? "x + y" : Array.from({ length: 24 }, (_, index) => `x_{${index}}`).join(" + ");
const tag = step < 2 ? "1" : "1000000";
return <main className="mx-auto max-w-3xl p-6">
<button onClick={() => setStep((value) => value + 1)}>Advance stream</button>
<button onClick={() => setStreaming(false)}>Complete</button>
<div data-testid="math-reply"><MarkdownText streaming={streaming} preserveStreamingLayout>
{`$$${terms} = 1 \\tag{${tag}}$$`}
</MarkdownText></div>
</main>;
}
+42
View File
@@ -0,0 +1,42 @@
import { expect, test, type Page } from "@playwright/test";
async function expectFittedFormula(page: Page) {
const frame = page.locator(".math-fit");
await expect(frame).toHaveCount(1);
await expect.poll(async () => frame.evaluate((element) => {
const box = element.getBoundingClientRect();
const content = element.querySelector<HTMLElement>(".math-fit-content")!.getBoundingClientRect();
const tag = element.querySelector<HTMLElement>(".katex-html > .tag")!.getBoundingClientRect();
return box.height > 0 && content.width <= box.width + 1
&& Math.abs(content.height - box.height) <= 1
&& Math.abs((content.left + content.right) - (box.left + box.right)) <= 2
&& tag.left >= box.left - 1 && tag.right <= box.right + 1;
})).toBe(true);
}
for (const width of [1280, 390]) {
for (const reducedMotion of ["no-preference", "reduce"] as const) {
test(`fits tagged streaming math at ${width}px with motion ${reducedMotion}`, async ({ page }) => {
const errors: string[] = [];
page.on("pageerror", (error) => errors.push(error.message));
await page.setViewportSize({ width, height: 844 });
await page.emulateMedia({ reducedMotion });
await page.goto("/browser/fixture.html?case=display-math");
await page.evaluate(() => document.fonts.ready);
await expectFittedFormula(page);
await page.getByRole("button", { name: "Advance stream" }).click();
await expectFittedFormula(page);
await expect.poll(() => page.locator(".math-fit").getAttribute("data-math-scale").then(Number)).toBeLessThan(1);
await page.getByRole("button", { name: "Advance stream" }).click();
await expect(page.locator(".katex-html > .tag")).toHaveText("(1000000)");
await expectFittedFormula(page);
await page.getByRole("button", { name: "Complete", exact: true }).click();
await expectFittedFormula(page);
await page.setViewportSize({ width: width === 390 ? 1280 : 390, height: 844 });
await expectFittedFormula(page);
await page.locator(".math-fit-content").evaluate((element) => { element.style.fontSize = "24px"; });
await expectFittedFormula(page);
expect(errors).toEqual([]);
});
}
}
+3
View File
@@ -36,6 +36,7 @@ import { QueuedGuidanceFixture } from "./queued-guidance-fixture";
import { WorkspaceAlignmentFixture } from "./workspace-alignment-fixture";
import { ComposerActionFixture } from "./composer-action-fixture";
import { MobileChatLayoutFixture } from "./mobile-chat-layout-fixture";
import { DisplayMathFixture } from "./display-math-fixture";
const history: UIMessage[] = [
{ id: "question", role: "user", content: "Earlier question", createdAt: 1_700_000_000_000 },
@@ -225,6 +226,8 @@ createRoot(document.getElementById("root")!).render(<TooltipProvider>
? <SidebarActivityFixture />
: parameters.get("case") === "chat-failure"
? <ChatFailureFixture parameters={parameters} />
: parameters.get("case") === "display-math"
? <DisplayMathFixture />
: parameters.get("case") === "markdown-emphasis"
? <MarkdownEmphasisFixture />
: parameters.get("case") === "typography"