From 07c4e3f0d191895dc2d3b2317481eb3016d5c2ff Mon Sep 17 00:00:00 2001 From: chengyongru <61816729+chengyongru@users.noreply.github.com> Date: Tue, 6 Oct 2026 02:16:04 +0800 Subject: [PATCH] fix(webui): fit wide equations and refine math spacing (#6075) * fix(webui): fit wide equations and refine math spacing * refactor(webui): colocate display math styles with its component * style(webui): trim math stylesheet trailing whitespace * fix(webui): tighten spacing after display equations * fix(webui): tighten spacing for equations within lists * fix(webui): center responsive display equations --- webui/src/components/DisplayMath.css | 76 +++++++++++++++++++ webui/src/components/DisplayMath.tsx | 31 ++++++++ webui/src/components/MarkdownTextRenderer.tsx | 8 ++ webui/src/tests/display-math.test.tsx | 66 ++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 webui/src/components/DisplayMath.css create mode 100644 webui/src/components/DisplayMath.tsx create mode 100644 webui/src/tests/display-math.test.tsx diff --git a/webui/src/components/DisplayMath.css b/webui/src/components/DisplayMath.css new file mode 100644 index 000000000..f5a295280 --- /dev/null +++ b/webui/src/components/DisplayMath.css @@ -0,0 +1,76 @@ +.math-fit { + position: relative; + display: block; + width: 100%; + min-width: 0; + margin-block: 0.45em 0.5em; +} + +.math-fit-content { + position: absolute; + top: 0; + left: 50%; + width: max-content; + padding-block: 0.1em; + transform-origin: top center; +} + +.math-fit .katex-display { + margin: 0; +} + +.math-fit .katex-display > .katex { + font-size: 1.1em; +} + +/* Math-heavy prose uses a tighter reading rhythm than ordinary chat. */ +.markdown-content:has(.math-fit) { + --cjk-line-height: 1.65; +} + +/* Keep list formulas close to their explanations. */ +.markdown-content li .math-fit { + margin-block: 0.3em 0.35em; +} + +.markdown-content li > p:has(.math-fit) { + margin-block: 0; +} + +.markdown-content li:has(.math-fit) { + margin-block: 0.35em; +} + +.markdown-content li:has(.math-fit) > p:first-child:not(:has(.math-fit)) { + margin-bottom: 0.3em; + font-weight: 500; +} + +/* Unindented Markdown equations may follow a list as sibling blocks. */ +.markdown-content :is(ol, ul):has(+ .math-fit) { + list-style-position: outside; + padding-left: 1.5em; + margin-top: 0.85em; + margin-bottom: 0.3em; +} + +.markdown-content :is(ol, ul):has(+ .math-fit) > li { + font-weight: 500; +} + +.markdown-content :is(ol, ul) + .math-fit:not([hidden]) { + width: calc(100% - 1.5em); + margin-left: 1.5em; + margin-top: 0.3em; + margin-bottom: 0.35em; +} + +.markdown-content .math-fit + :is(ol, ul):has(+ .math-fit) { + margin-top: 0.35em; +} + +/* Typography paragraph margins otherwise add a full line after equations. */ +.markdown-content .math-fit:not([hidden]) + p, +.markdown-content :is(ol, ul):has(> li:last-child > .math-fit:last-child) + p { + margin-top: 0.5em; +} diff --git a/webui/src/components/DisplayMath.tsx b/webui/src/components/DisplayMath.tsx new file mode 100644 index 000000000..09f613710 --- /dev/null +++ b/webui/src/components/DisplayMath.tsx @@ -0,0 +1,31 @@ +import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; + +import "./DisplayMath.css"; + +export function DisplayMath({ children }: { children: ReactNode }) { + const frame = useRef(null); + const content = useRef(null); + const [layout, setLayout] = useState({ scale: 1, height: 0 }); + useLayoutEffect(() => { + const update = () => { + if (!frame.current || !content.current) return; + const width = content.current.offsetWidth; + const scale = width ? Math.min(1, frame.current.clientWidth / width) : 1; + const height = content.current.offsetHeight * scale; + setLayout(previous => previous.scale === scale && previous.height === height ? previous : { scale, height }); + }; + update(); + const observer = new ResizeObserver(update); + observer.observe(frame.current!); + observer.observe(content.current!); + let cancelled = false; + void document.fonts?.ready.then(() => { if (!cancelled) update(); }); + return () => { cancelled = true; observer.disconnect(); }; + }, [children]); + return + + {children} + + ; +} diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsx index b609bed63..1541e1953 100644 --- a/webui/src/components/MarkdownTextRenderer.tsx +++ b/webui/src/components/MarkdownTextRenderer.tsx @@ -16,6 +16,7 @@ import remend from "remend"; import { parseMathAwareMarkdownBlocks } from "@/lib/markdown-streaming-blocks"; +import { DisplayMath } from "@/components/DisplayMath"; import { AttachmentTile } from "@/components/AttachmentTile"; import { CodeBlock } from "@/components/CodeBlock"; import { WebLink } from "@/components/WebLink"; @@ -689,6 +690,13 @@ export default function MarkdownTextRenderer({ ); }, + span({ children: spanChildren, className: spanClassName, node: _node, ...props }) { + void _node; + if (spanClassName?.split(" ").includes("katex-display")) { + return {spanChildren}; + } + return {spanChildren}; + }, // Streamdown decorates emphasis with spans by default. Preserve native // semantics for accessibility and predictable typography. strong({ children: markdownChildren, node: _node, ...props }) { diff --git a/webui/src/tests/display-math.test.tsx b/webui/src/tests/display-math.test.tsx new file mode 100644 index 000000000..46221e043 --- /dev/null +++ b/webui/src/tests/display-math.test.tsx @@ -0,0 +1,66 @@ +import { act, render } from "@testing-library/react"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { DisplayMath } from "@/components/DisplayMath"; + +let availableWidth: number; +let formulaWidth: number; +let formulaHeight: number; +let resize: () => void; + +beforeEach(() => { + availableWidth = 1000; + formulaWidth = 800; + formulaHeight = 80; + vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockImplementation(function (this: HTMLElement) { + return this.classList.contains("math-fit") ? availableWidth : 0; + }); + vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(function (this: HTMLElement) { + return this.classList.contains("math-fit-content") ? formulaWidth : 0; + }); + vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockImplementation(function (this: HTMLElement) { + return this.classList.contains("math-fit-content") ? formulaHeight : 0; + }); + vi.stubGlobal("ResizeObserver", class { + constructor(callback: () => void) { resize = callback; } + observe() {} + disconnect() {} + }); +}); + +afterEach(() => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); +}); + +it("fits a wide formula on resize and restores its size without enlarging it", () => { + const { container } = render(long equation); + const frame = container.querySelector(".math-fit")!; + const formula = container.querySelector(".math-fit-content")!; + expect(frame.style.height).toBe("80px"); + expect(formula.style.transform).toContain("scale(1)"); + + act(() => { availableWidth = 400; resize(); }); + expect(frame.style.height).toBe("40px"); + expect(formula.style.transform).toContain("scale(0.5)"); + expect(frame.textContent).toBe("long equation"); + + act(() => { availableWidth = 1200; resize(); }); + expect(frame.style.height).toBe("80px"); + expect(formula.style.transform).toContain("scale(1)"); +}); + +it("recalculates width and occupied height when a streaming formula grows", () => { + availableWidth = 400; + formulaWidth = 200; + formulaHeight = 40; + const { container, rerender } = render(initial equation); + const frame = container.querySelector(".math-fit")!; + expect(frame.style.height).toBe("40px"); + + formulaWidth = 800; + formulaHeight = 120; + rerender(completed equation); + expect(frame.style.height).toBe("60px"); + expect(container.querySelector(".math-fit-content")!.style.transform).toContain("scale(0.5)"); + expect(frame.textContent).toBe("completed equation"); +});