fix(webui): keep file preview tab switching responsive

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent ebb29759c0
commit 8b02d56e6c
6 files changed
+308 -12

No files matched your search

+62 -7
View File
@@ -1,4 +1,4 @@
import { Suspense, lazy, useCallback, useMemo, useState, type ReactNode } from "react";
import { Suspense, lazy, memo, startTransition, useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { Check, Copy } from "lucide-react";
import { useTranslation } from "react-i18next";
@@ -16,6 +16,7 @@ interface CodeBlockProps {
highlight?: boolean;
showLineNumbers?: boolean;
wrapLongLines?: boolean;
viewportHighlight?: boolean;
}
interface HighlightedCodeProps {
@@ -25,6 +26,7 @@ interface HighlightedCodeProps {
chrome: "default" | "none";
showLineNumbers: boolean;
wrapLongLines: boolean;
viewportHighlight: boolean;
}
const CODE_FONT_STACK = [
@@ -46,10 +48,12 @@ const LazyHighlightedCode = lazy(async () => {
{ default: SyntaxHighlighter },
{ default: oneDark },
{ default: oneLight },
{ ViewportCodeRows },
] = await Promise.all([
import("react-syntax-highlighter/dist/esm/prism-async-light"),
import("react-syntax-highlighter/dist/esm/styles/prism/one-dark"),
import("react-syntax-highlighter/dist/esm/styles/prism/one-light"),
import("@/components/ViewportCodeRows"),
]);
return {
@@ -60,6 +64,7 @@ const LazyHighlightedCode = lazy(async () => {
chrome,
showLineNumbers,
wrapLongLines,
viewportHighlight,
}: HighlightedCodeProps) {
const theme = isDark ? oneDark : oneLight;
const transparentTheme = chrome === "none" ? {
@@ -91,6 +96,7 @@ const LazyHighlightedCode = lazy(async () => {
style: {
background: "transparent",
fontFamily: CODE_FONT_STACK,
...(viewportHighlight ? { lineHeight: "inherit" } : {}),
},
}}
lineNumberStyle={{
@@ -101,8 +107,9 @@ const LazyHighlightedCode = lazy(async () => {
userSelect: "none",
}}
PreTag="pre"
showLineNumbers={showLineNumbers}
showLineNumbers={showLineNumbers && !viewportHighlight}
wrapLongLines={wrapLongLines}
renderer={viewportHighlight ? props => <ViewportCodeRows {...props} /> : undefined}
>
{code}
</SyntaxHighlighter>
@@ -130,6 +137,7 @@ function CodeTextBlock({
testId,
className,
renderText = renderPlainText,
compact = false,
}: {
code: string;
chrome: "default" | "none";
@@ -137,6 +145,7 @@ function CodeTextBlock({
testId: string;
className?: string;
renderText?: (value: string) => ReactNode;
compact?: boolean;
}) {
const lines = showLineNumbers ? code.split("\n") : [];
return (
@@ -146,13 +155,20 @@ function CodeTextBlock({
showLineNumbers ? "whitespace-pre" : "whitespace-pre-wrap",
chrome === "default"
? "py-4 pl-5 pr-14 leading-[1.6]"
: "p-3 leading-[1.55]",
: compact ? "px-4 py-3 leading-[1.55]" : "p-3 leading-[1.55]",
className,
)}
data-testid={testId}
style={compact ? { fontFamily: CODE_FONT_STACK } : undefined}
>
<code className="text-inherit">
{showLineNumbers ? (
{showLineNumbers && compact ? (
<span className="flex min-w-max">
<span aria-hidden className="shrink-0 select-none pr-[1.15rem] text-right text-muted-foreground/60"
style={{ minWidth: "4.25em" }}>{lines.map((_, index) => index + 1).join("\n")}</span>
<span>{renderText(code)}</span>
</span>
) : showLineNumbers ? (
lines.map((line, index) => (
<span key={index} className="flex min-w-max">
<span className="w-10 shrink-0 select-none pr-4 text-right text-muted-foreground/60">
@@ -173,7 +189,7 @@ function shouldRenderAnsi(language: string | undefined, code: string): boolean {
return Boolean((normalized && ANSI_LANGUAGES.has(normalized)) || hasAnsi(code));
}
export function CodeBlock({
export const CodeBlock = memo(function CodeBlock({
language,
code,
className,
@@ -181,14 +197,48 @@ export function CodeBlock({
highlight = true,
showLineNumbers = false,
wrapLongLines = true,
viewportHighlight = false,
}: CodeBlockProps) {
const { t } = useTranslation();
const rootRef = useRef<HTMLDivElement>(null);
const [copied, setCopied] = useState(false);
const [highlightedSource, setHighlightedSource] = useState<string | null>(null);
const renderAnsi = useMemo(() => shouldRenderAnsi(language, code), [language, code]);
const plainCode = useMemo(() => renderAnsi ? stripAnsi(code) : code, [renderAnsi, code]);
const isDark = useThemeValue() === "dark";
const hasChrome = chrome === "default";
const syntaxLanguage = normalizeCodeLanguage(language);
const useViewportHighlight = viewportHighlight && showLineNumbers && !wrapLongLines;
// Minified/very large previews must not monopolize the main thread in Prism.
// Plain text still contains the entire preview, with normal selection and find.
const withinHighlightBudget = useMemo(() => {
if (!useViewportHighlight) return true;
if (code.length > 100_000) return false;
const lines = code.split("\n");
return lines.length <= 2_000 && !lines.some(line => line.length > 2_000);
}, [code, useViewportHighlight]);
const deferHighlight = highlight && !renderAnsi && useViewportHighlight
&& withinHighlightBudget && code.length > 8_000;
useEffect(() => {
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 frame = requestAnimationFrame(() => {
frame = requestAnimationFrame(enhance);
});
function enhance() {
const selection = document.getSelection();
if (rootRef.current && selection && !selection.isCollapsed && selection.rangeCount
&& selection.getRangeAt(0).intersectsNode(rootRef.current)) return;
document.removeEventListener("selectionchange", enhance);
startTransition(() => setHighlightedSource(code));
}
document.addEventListener("selectionchange", enhance);
return () => {
cancelAnimationFrame(frame);
document.removeEventListener("selectionchange", enhance);
};
}, [code, deferHighlight]);
const copyLabel = copied ? t("code.copied") : t("code.copyAria");
const onCopy = useCallback(() => {
@@ -201,6 +251,7 @@ export function CodeBlock({
return (
<div
ref={rootRef}
className={cn(
"not-prose relative overflow-hidden",
hasChrome && "rounded-floating bg-secondary/70",
@@ -215,8 +266,9 @@ export function CodeBlock({
showLineNumbers={showLineNumbers}
testId="ansi-code"
renderText={renderAnsiText}
compact={useViewportHighlight}
/>
) : highlight ? (
) : highlight && withinHighlightBudget && (!deferHighlight || highlightedSource === code) ? (
<Suspense
fallback={
<CodeTextBlock
@@ -224,6 +276,7 @@ export function CodeBlock({
chrome={chrome}
showLineNumbers={showLineNumbers}
testId="plain-code-fallback"
compact={useViewportHighlight}
/>
}
>
@@ -234,6 +287,7 @@ export function CodeBlock({
chrome={chrome}
showLineNumbers={showLineNumbers}
wrapLongLines={wrapLongLines}
viewportHighlight={useViewportHighlight}
/>
</Suspense>
) : (
@@ -242,6 +296,7 @@ export function CodeBlock({
chrome={chrome}
showLineNumbers={showLineNumbers}
testId="plain-code-fallback"
compact={useViewportHighlight}
/>
)}
{hasChrome ? (
@@ -265,4 +320,4 @@ export function CodeBlock({
) : null}
</div>
);
}
});