fix(webui): preserve preview paint and scroll ownership

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent 8b02d56e6c
commit 09b2ae9a31
3 files changed
+33 -4

No files matched your search

+15 -3
View File
@@ -91,6 +91,9 @@ const LazyHighlightedCode = lazy(async () => {
fontSize: "13px",
lineHeight: chrome === "none" ? 1.55 : 1.6,
tabSize: 2,
// FilePreviewPanel owns both scroll axes. Replacing this <pre>
// during progressive highlighting must not reset horizontal scroll.
...(viewportHighlight ? { overflow: "visible" } : {}),
}}
codeTagProps={{
style: {
@@ -151,7 +154,8 @@ function CodeTextBlock({
return (
<pre
className={cn(
"m-0 overflow-x-auto bg-transparent font-mono text-[13px] text-foreground/90",
"m-0 bg-transparent font-mono text-[13px] text-foreground/90",
compact ? "overflow-visible" : "overflow-x-auto",
showLineNumbers ? "whitespace-pre" : "whitespace-pre-wrap",
chrome === "default"
? "py-4 pl-5 pr-14 leading-[1.6]"
@@ -223,10 +227,17 @@ export const CodeBlock = memo(function CodeBlock({
if (!deferHighlight) return;
// Paint the tab and full plain source first. Color is a progressive enhancement,
// not a reason to hold up switching tabs (including an already cached file).
let readyToEnhance = false;
let frame = requestAnimationFrame(() => {
frame = requestAnimationFrame(enhance);
frame = requestAnimationFrame(() => {
readyToEnhance = true;
enhance();
});
});
function enhance() {
// A tab click may clear a selection before the first frame. Only resume
// selection-blocked work here after the initial paint opportunity.
if (!readyToEnhance) return;
const selection = document.getSelection();
if (rootRef.current && selection && !selection.isCollapsed && selection.rangeCount
&& selection.getRangeAt(0).intersectsNode(rootRef.current)) return;
@@ -253,7 +264,8 @@ export const CodeBlock = memo(function CodeBlock({
<div
ref={rootRef}
className={cn(
"not-prose relative overflow-hidden",
"not-prose relative",
useViewportHighlight ? "overflow-visible" : "overflow-hidden",
hasChrome && "rounded-floating bg-secondary/70",
className,
)}
+12
View File
@@ -15,15 +15,18 @@ vi.mock("react-syntax-highlighter/dist/esm/prism-async-light", () => ({
children,
language,
style,
customStyle,
}: {
children: string;
language?: string;
style: Record<string, unknown>;
customStyle?: React.CSSProperties;
}) => (
<pre
data-testid="highlighted-code"
data-language={language}
data-theme={style === mockedStyles.dark ? "dark" : "light"}
style={customStyle}
>
<code>{children}</code>
</pre>
@@ -92,8 +95,16 @@ describe("CodeBlock", () => {
showLineNumbers wrapLongLines={false} viewportHighlight />);
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
expect(screen.queryByTestId("highlighted-code")).not.toBeInTheDocument();
expect(screen.getByTestId("plain-code-fallback")).toHaveClass("overflow-visible");
expect(screen.getByTestId("plain-code-fallback").parentElement).toHaveClass("overflow-visible");
// Clicking a tab can clear a previous selection. That browser event must
// not bypass the initial paint opportunity reserved for the new tab.
await act(async () => { document.dispatchEvent(new Event("selectionchange")); });
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
await act(async () => { callbacks.get(1)?.(0); });
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
await act(async () => { document.dispatchEvent(new Event("selectionchange")); });
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
const selection = document.getSelection()!;
const range = document.createRange();
range.selectNodeContents(screen.getByTestId("plain-code-fallback"));
@@ -102,6 +113,7 @@ describe("CodeBlock", () => {
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
await act(async () => { selection.removeAllRanges(); document.dispatchEvent(new Event("selectionchange")); });
expect(await screen.findByTestId("highlighted-code")).toBeInTheDocument();
expect(screen.getByTestId("highlighted-code")).toHaveStyle({ overflow: "visible" });
unmount();
expect(cancel).toHaveBeenCalledWith(2);
const second = render(<CodeBlock code={code} language="ts" chrome="none"