feat(webui): preview Markdown files with a source toggle
This commit is contained in:
1 parent
742ea48df3
commit
0869c928a2
18 files changed
+182
-17
No files matched your search
@@ -9,6 +9,7 @@ interface FilePreviewPanelProps {
|
||||
token: string;
|
||||
loadPreview?: (path: string) => Promise<FilePreviewPayload>;
|
||||
initialPreview?: FilePreviewPayload;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
}
|
||||
|
||||
export function FilePreviewPanel({
|
||||
@@ -17,6 +18,7 @@ export function FilePreviewPanel({
|
||||
token,
|
||||
loadPreview,
|
||||
initialPreview,
|
||||
onOpenFilePreview,
|
||||
}: FilePreviewPanelProps) {
|
||||
const [state, setState] = useState<FilePreviewPanelState>(() => initialPreview
|
||||
? { status: "ready", payload: initialPreview } : { status: "loading" });
|
||||
@@ -55,6 +57,7 @@ export function FilePreviewPanel({
|
||||
}, [path, sessionKey, loadPreview]);
|
||||
|
||||
const reportImageError = useCallback(() => setState({ status: "error" }), []);
|
||||
return <FilePreviewPanelView path={path} state={state} imageOpen={imageOpen}
|
||||
return <FilePreviewPanelView key={`${sessionKey}:${path}`} path={path} state={state} imageOpen={imageOpen}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
onImageOpenChange={setImageOpen} onImageError={reportImageError} />;
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { AlertCircle } from "lucide-react";
|
||||
|
||||
import { CodeBlock } from "@/components/CodeBlock";
|
||||
import { MarkdownText } from "@/components/MarkdownText";
|
||||
import { SegmentedControl } from "@/components/ui/segmented-control";
|
||||
import { ImageLightbox } from "@/components/ImageLightbox";
|
||||
import { fileKindForPath, splitFilePath } from "@/components/FileReferenceChip";
|
||||
import type { FilePreviewPayload } from "@/lib/types";
|
||||
@@ -11,14 +14,18 @@ export type FilePreviewPanelState =
|
||||
| { status: "error"; message?: string; routeMissing?: boolean }
|
||||
| { status: "ready"; payload: FilePreviewPayload };
|
||||
|
||||
export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange, onImageError }: {
|
||||
export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange, onImageError, onOpenFilePreview }: {
|
||||
path: string;
|
||||
state: FilePreviewPanelState;
|
||||
imageOpen: boolean;
|
||||
onImageOpenChange: (open: boolean) => void;
|
||||
onImageError: () => void;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [mode, setMode] = useState<"preview" | "source">("preview");
|
||||
const markdown = state.status === "ready" && state.payload.kind !== "image"
|
||||
&& state.payload.language === "markdown";
|
||||
const displayPath = state.status === "ready" ? state.payload.display_path : path;
|
||||
const { name } = splitFilePath(displayPath);
|
||||
const fileName = name || displayPath;
|
||||
@@ -32,7 +39,14 @@ export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange
|
||||
|
||||
return (
|
||||
<section aria-label={t("filePreview.aria")} data-testid="file-preview-panel" className="flex min-h-0 flex-1 flex-col">
|
||||
<div data-file-preview-scroll tabIndex={0}
|
||||
{markdown ? (
|
||||
<div className="flex shrink-0 items-center justify-end px-4 py-2">
|
||||
<SegmentedControl value={mode} onChange={setMode} ariaLabel={t("filePreview.displayMode")}
|
||||
options={[{ value: "preview", label: t("filePreview.preview") }, { value: "source", label: t("filePreview.source") }]}
|
||||
itemClassName="px-3 [@media(pointer:coarse)]:min-h-9" />
|
||||
</div>
|
||||
) : null}
|
||||
<div key={mode} data-file-preview-scroll tabIndex={0}
|
||||
className="min-h-0 flex-1 overflow-auto focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/60">
|
||||
{state.status === "loading" ? (
|
||||
<div role="status" aria-label={t("filePreview.loading", { defaultValue: "Loading preview..." })}
|
||||
@@ -77,7 +91,12 @@ export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange
|
||||
})}
|
||||
</div>
|
||||
) : null}
|
||||
<CodeBlock
|
||||
{markdown && mode === "preview" ? (
|
||||
<MarkdownText documentPath={state.payload.path} onOpenFilePreview={onOpenFilePreview}
|
||||
className="px-5 pb-6 pt-1 text-sm [overflow-wrap:anywhere] sm:px-6 [&>:first-child]:mt-0">
|
||||
{state.payload.content}
|
||||
</MarkdownText>
|
||||
) : <CodeBlock
|
||||
language={state.payload.language}
|
||||
code={state.payload.content}
|
||||
chrome="none"
|
||||
@@ -86,7 +105,7 @@ export function FilePreviewPanelView({ path, state, imageOpen, onImageOpenChange
|
||||
wrapLongLines={false}
|
||||
viewportHighlight
|
||||
className="min-h-full"
|
||||
/>
|
||||
/>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -15,6 +15,7 @@ interface MarkdownTextProps {
|
||||
streaming?: boolean;
|
||||
preserveStreamingLayout?: boolean;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
documentPath?: string;
|
||||
}
|
||||
|
||||
const loadMarkdownRenderer = () => import("@/components/MarkdownTextRenderer");
|
||||
@@ -27,6 +28,7 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
|
||||
streaming,
|
||||
preserveStreamingLayout,
|
||||
onOpenFilePreview,
|
||||
documentPath,
|
||||
}: {
|
||||
source: string;
|
||||
className?: string;
|
||||
@@ -34,6 +36,7 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
|
||||
streaming: boolean;
|
||||
preserveStreamingLayout: boolean;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
documentPath?: string;
|
||||
}) {
|
||||
return (
|
||||
<LazyMarkdownRenderer
|
||||
@@ -42,6 +45,7 @@ const MemoizedMarkdownRenderer = memo(function MemoizedMarkdownRenderer({
|
||||
streaming={streaming}
|
||||
preserveStreamingLayout={preserveStreamingLayout}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
documentPath={documentPath}
|
||||
>
|
||||
{source}
|
||||
</LazyMarkdownRenderer>
|
||||
@@ -80,6 +84,7 @@ export function MarkdownText({
|
||||
streaming = false,
|
||||
preserveStreamingLayout = false,
|
||||
onOpenFilePreview,
|
||||
documentPath,
|
||||
}: MarkdownTextProps) {
|
||||
const renderedSource = children;
|
||||
const renderPhase = streaming ? "streaming" : "complete";
|
||||
@@ -111,6 +116,7 @@ export function MarkdownText({
|
||||
streaming={streaming}
|
||||
preserveStreamingLayout={preserveStreamingLayout}
|
||||
onOpenFilePreview={onOpenFilePreview}
|
||||
documentPath={documentPath}
|
||||
/>
|
||||
</Suspense>
|
||||
</MarkdownRendererBoundary>
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
FileReferenceChip,
|
||||
isFilePatternReference,
|
||||
isLikelyFilePath,
|
||||
splitFilePath,
|
||||
} from "@/components/FileReferenceChip";
|
||||
import { useLogoFallback } from "@/hooks/useLogoFallback";
|
||||
import { inferMediaKind } from "@/lib/media";
|
||||
@@ -50,6 +51,8 @@ interface MarkdownTextRendererProps {
|
||||
streaming?: boolean;
|
||||
preserveStreamingLayout?: boolean;
|
||||
onOpenFilePreview?: (path: string) => void;
|
||||
/** Read-only documents must not load embedded resources or use chat routing. */
|
||||
documentPath?: string;
|
||||
}
|
||||
|
||||
type MarkdownAstNode = {
|
||||
@@ -388,6 +391,29 @@ function fileReferenceFromLink(href: string | undefined): string | null {
|
||||
return isPreviewableFileTarget(target) ? target : null;
|
||||
}
|
||||
|
||||
function DocumentLink({ href, path, onOpen, children }: {
|
||||
href: string;
|
||||
path: string;
|
||||
onOpen?: (path: string) => void;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
// Fragment-only links must not replace the application's hash route.
|
||||
if (!href || href.startsWith("#")) return <>{children}</>;
|
||||
if (/^(https?:\/\/|mailto:)/i.test(href)) {
|
||||
return <WebLink href={href} className="text-blue-500 underline underline-offset-2 dark:text-blue-300">{children}</WebLink>;
|
||||
}
|
||||
if (/^[a-z][a-z\d+.-]*:/i.test(href) || href.startsWith("//")) return <>{children}</>;
|
||||
let target = href.split(/[?#]/, 1)[0];
|
||||
try { target = decodeURIComponent(target); } catch { return <>{children}</>; }
|
||||
if (!target) return <>{children}</>;
|
||||
// Resolve against the document, not the WebUI origin or the project root.
|
||||
// The gateway still resolves and authorizes the resulting path.
|
||||
const resolved = /^[\\/]|^[a-z]:[\\/]/i.test(target)
|
||||
? target : `${splitFilePath(path).directory}${target}`;
|
||||
return <FileReferenceChip path={nodeText(children) || target} previewPath={resolved}
|
||||
tooltipPath={resolved} onOpen={onOpen} />;
|
||||
}
|
||||
|
||||
function sessionReferenceHref(href: string): string | null {
|
||||
const prefix = href.startsWith("#session/")
|
||||
? "#session/"
|
||||
@@ -554,6 +580,7 @@ export default function MarkdownTextRenderer({
|
||||
streaming = false,
|
||||
preserveStreamingLayout = false,
|
||||
onOpenFilePreview,
|
||||
documentPath,
|
||||
}: MarkdownTextRendererProps) {
|
||||
const { t } = useTranslation();
|
||||
const [mathPlugin, setMathPlugin] = useState(() => loadedMathPlugin);
|
||||
@@ -588,7 +615,7 @@ export default function MarkdownTextRenderer({
|
||||
);
|
||||
}
|
||||
const raw = String(kids).replace(/\n$/, "");
|
||||
if (isLikelyFilePath(raw)) {
|
||||
if (!documentPath && isLikelyFilePath(raw)) {
|
||||
return (
|
||||
<InferredFileReferenceChip
|
||||
path={raw}
|
||||
@@ -663,6 +690,9 @@ export default function MarkdownTextRenderer({
|
||||
if (href === "streamdown:incomplete-link") {
|
||||
return <>{markdownChildren}</>;
|
||||
}
|
||||
if (documentPath) {
|
||||
return <DocumentLink href={href} path={documentPath} onOpen={onOpenFilePreview}>{markdownChildren}</DocumentLink>;
|
||||
}
|
||||
const sessionHref = sessionReferenceHref(href);
|
||||
if (sessionHref) {
|
||||
return (
|
||||
@@ -764,7 +794,7 @@ export default function MarkdownTextRenderer({
|
||||
);
|
||||
},
|
||||
li({ children: markdownChildren, className: itemClassName }) {
|
||||
const link = inlineLinkPreviewFromChildren(markdownChildren);
|
||||
const link = !documentPath && inlineLinkPreviewFromChildren(markdownChildren);
|
||||
if (link) {
|
||||
return (
|
||||
<li className={cn("list-none pl-0", itemClassName)}>
|
||||
@@ -837,6 +867,9 @@ export default function MarkdownTextRenderer({
|
||||
const source = typeof src === "string" ? src : "";
|
||||
if (!source) return null;
|
||||
const label = typeof alt === "string" ? alt : "";
|
||||
if (documentPath) {
|
||||
return <DocumentLink href={source} path={documentPath} onOpen={onOpenFilePreview}>{label || source}</DocumentLink>;
|
||||
}
|
||||
const kind = markdownAttachmentKind(source, label);
|
||||
return (
|
||||
<AttachmentTile
|
||||
@@ -850,7 +883,7 @@ export default function MarkdownTextRenderer({
|
||||
);
|
||||
},
|
||||
}),
|
||||
[highlightCode, onOpenFilePreview, t],
|
||||
[highlightCode, onOpenFilePreview, documentPath, t],
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
@@ -2134,6 +2134,7 @@ export function ThreadShell({
|
||||
token={token}
|
||||
loadPreview={filePreviews.load}
|
||||
initialPreview={filePreviews.peek(activePreview.value)}
|
||||
onOpenFilePreview={openFilePreview}
|
||||
/>
|
||||
) : <WebPreviewPanel key={activePreview.id} url={activePreview.value} />}
|
||||
</PreviewPane>
|
||||
|
||||
Reference in new issue
Block a user