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
This commit is contained in:
1 parent
d9ceae15be
commit
07c4e3f0d1
4 files changed
+181
No files matched your search
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
|
||||
|
||||
import "./DisplayMath.css";
|
||||
|
||||
export function DisplayMath({ children }: { children: ReactNode }) {
|
||||
const frame = useRef<HTMLSpanElement>(null);
|
||||
const content = useRef<HTMLSpanElement>(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 <span ref={frame} className="math-fit" style={{ height: layout.height || undefined }}
|
||||
data-math-scale={layout.scale}>
|
||||
<span ref={content} className="math-fit-content" style={{ transform: `translateX(-50%) scale(${layout.scale})` }}>
|
||||
<span className="katex-display">{children}</span>
|
||||
</span>
|
||||
</span>;
|
||||
}
|
||||
@@ -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({
|
||||
</WebLink>
|
||||
);
|
||||
},
|
||||
span({ children: spanChildren, className: spanClassName, node: _node, ...props }) {
|
||||
void _node;
|
||||
if (spanClassName?.split(" ").includes("katex-display")) {
|
||||
return <DisplayMath>{spanChildren}</DisplayMath>;
|
||||
}
|
||||
return <span className={spanClassName} {...props}>{spanChildren}</span>;
|
||||
},
|
||||
// Streamdown decorates emphasis with spans by default. Preserve native
|
||||
// semantics for accessibility and predictable typography.
|
||||
strong({ children: markdownChildren, node: _node, ...props }) {
|
||||
|
||||
Reference in new issue
Block a user