From 09b2ae9a31066bf5c6a7c83fed84659892265482 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Thu, 24 Sep 2026 14:51:04 +0800 Subject: [PATCH] fix(webui): preserve preview paint and scroll ownership --- docs/webui.md | 7 ++++++- webui/src/components/CodeBlock.tsx | 18 +++++++++++++++--- webui/src/tests/code-block.test.tsx | 12 ++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/webui.md b/docs/webui.md index 42dcb05dc..1c2bb11f2 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -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 diff --git a/webui/src/components/CodeBlock.tsx b/webui/src/components/CodeBlock.tsx index 32e07fdf7..eae8074d9 100644 --- a/webui/src/components/CodeBlock.tsx +++ b/webui/src/components/CodeBlock.tsx @@ -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
+ // during progressive highlighting must not reset horizontal scroll.
+ ...(viewportHighlight ? { overflow: "visible" } : {}),
}}
codeTagProps={{
style: {
@@ -151,7 +154,8 @@ function CodeTextBlock({
return (
{
- 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({
({
children,
language,
style,
+ customStyle,
}: {
children: string;
language?: string;
style: Record;
+ customStyle?: React.CSSProperties;
}) => (
{children}
@@ -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(