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

+6 -1
View File
@@ -142,7 +142,12 @@ with **Escape**. Tabs and width follow each session while the WebUI remains open
They are kept only in memory and cleared when that session is deleted or the
connection ends. The pane overlays the conversation on narrow screens.
PNG, JPEG, GIF and WebP files can be previewed as images within the size limit.
Text previews include up to 384 KiB, with a notice when the file is truncated.
Large previews show readable text before adding syntax colors; very large or
minified source stays plain text. All previewed text remains available for
selection and browser search, and the code pane scrolls in both directions.
PNG, JPEG, GIF and WebP files up to 8 MiB can be previewed as images.
Hover or focus an image file reference for a compact quick look, or select it
to open the pane. Select the preview image for the full-size viewer, which
supports zoom controls, pinch-to-zoom and panning. HTML and SVG files remain
+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"