Files

279 lines
9.2 KiB
TypeScript

"use client";
import { useEffect, useRef, useState, type ReactNode } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vs } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { vscDarkPlus } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { useTheme } from "@/hooks/useTheme";
import { useI18n } from "@/hooks/useI18n";
import { copyText } from "@/lib/clipboard";
interface MermaidBlockProps {
code: string;
isStreaming?: boolean;
defaultPreview?: boolean;
}
const ZOOM_STEP = 0.25;
const ZOOM_MIN = 0.5;
const ZOOM_MAX = 3;
type RenderState =
| { key: string; status: "loading" }
| { key: string; status: "error" }
| { key: string; status: "ready"; svg: string };
export function MermaidBlock({ code, isStreaming, defaultPreview = false }: MermaidBlockProps) {
const { isDark } = useTheme();
const { t } = useI18n();
const [showPreview, setShowPreview] = useState(defaultPreview);
const [renderState, setRenderState] = useState<RenderState | null>(null);
const [zoomOpen, setZoomOpen] = useState(false);
const currentKey = `${isDark ? "dark" : "light"}\n${code}`;
const previewVisible = showPreview && !isStreaming;
useEffect(() => {
if (!previewVisible) return;
let cancelled = false;
setRenderState({ key: currentKey, status: "loading" });
const render = async () => {
const { default: mermaid } = await import("mermaid");
mermaid.initialize({
startOnLoad: false,
securityLevel: "strict",
suppressErrorRendering: true,
theme: isDark ? "dark" : "default",
});
const parsed = await mermaid.parse(code, { suppressErrors: true });
if (!parsed) throw new Error("Invalid Mermaid diagram");
const id =
typeof crypto !== "undefined" && "randomUUID" in crypto
? `mermaid-${crypto.randomUUID()}`
: `mermaid-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const result = await mermaid.render(id, code);
if (!cancelled) {
setRenderState({ key: currentKey, status: "ready", svg: result.svg });
}
};
render().catch(() => {
if (!cancelled) setRenderState({ key: currentKey, status: "error" });
});
return () => {
cancelled = true;
};
}, [code, currentKey, isDark, previewVisible]);
const previewButton = (
<button
type="button"
onClick={() => setShowPreview((v) => !v)}
disabled={isStreaming}
title={isStreaming ? t("i18n.previewAfterStreaming") : (previewVisible ? t("i18n.showMermaidSource") : t("i18n.previewMermaid"))}
className={["markdown-code-action", previewVisible ? "is-active" : ""].filter(Boolean).join(" ")}
>
{previewVisible ? t("i18n.source") : t("i18n.preview")}
</button>
);
if (!previewVisible) {
return <CodeBlock code={code} lang="mermaid" headerAction={previewButton} />;
}
const body = renderState?.key === currentKey && renderState.status === "error" ? (
<div className="mermaid-block mermaid-block-error">{t("i18n.invalidMermaid")}</div>
) : renderState?.key !== currentKey || renderState.status !== "ready" ? (
<div className="mermaid-block mermaid-block-loading" aria-label={t("i18n.renderingMermaid")} />
) : (
<>
{!zoomOpen && (
<button
type="button"
className="mermaid-block mermaid-preview-button"
title={t("i18n.openMermaidViewer")}
aria-label={t("i18n.openMermaidViewer")}
onClick={() => setZoomOpen(true)}
dangerouslySetInnerHTML={{ __html: renderState.svg }}
/>
)}
{zoomOpen && <MermaidZoomDialog svg={renderState.svg} onClose={() => setZoomOpen(false)} />}
</>
);
return (
<div className="markdown-code-block">
<div className="markdown-code-header">
<span className="markdown-code-lang">mermaid</span>
{previewButton}
</div>
{body}
</div>
);
}
function MermaidZoomDialog({ svg, onClose }: { svg: string; onClose: () => void }) {
const { t } = useI18n();
const dialogRef = useRef<HTMLDialogElement>(null);
const [zoom, setZoom] = useState(1);
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
const previousOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
dialog.showModal();
return () => {
document.body.style.overflow = previousOverflow;
if (dialog.open) dialog.close();
};
}, []);
return (
<dialog
ref={dialogRef}
className="mermaid-zoom-dialog"
aria-label={t("i18n.mermaidViewer")}
onCancel={(event) => {
event.preventDefault();
onClose();
}}
onKeyDown={(event) => {
if (event.key !== "Escape") return;
event.preventDefault();
onClose();
}}
>
<div className="mermaid-zoom-layout">
<div className="mermaid-zoom-toolbar">
<span className="mermaid-zoom-title">{t("i18n.mermaidDiagram")}</span>
<div className="mermaid-zoom-actions">
<div className="mermaid-zoom-stepper">
<button
type="button"
onClick={() => setZoom((value) => Math.max(ZOOM_MIN, value - ZOOM_STEP))}
disabled={zoom <= ZOOM_MIN}
title={t("i18n.zoomOut")}
aria-label={t("i18n.zoomOut")}
>
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M5 12h14" />
</svg>
</button>
<span className="mermaid-zoom-value">{Math.round(zoom * 100)}%</span>
<button
type="button"
onClick={() => setZoom((value) => Math.min(ZOOM_MAX, value + ZOOM_STEP))}
disabled={zoom >= ZOOM_MAX}
title={t("i18n.zoomIn")}
aria-label={t("i18n.zoomIn")}
>
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M12 5v14M5 12h14" />
</svg>
</button>
</div>
<button
type="button"
className="mermaid-zoom-icon-button"
onClick={() => setZoom(1)}
title={t("i18n.fitToWidth")}
aria-label={t("i18n.fitToWidth")}
>
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M8 3H3v5M16 3h5v5M8 21H3v-5M16 21h5v-5" />
</svg>
</button>
<button
type="button"
className="mermaid-zoom-icon-button"
onClick={onClose}
title={t("i18n.close")}
aria-label={t("i18n.close")}
>
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M6 6l12 12M18 6 6 18" />
</svg>
</button>
</div>
</div>
<div
className="mermaid-zoom-viewport"
onClick={(event) => {
if (event.target === event.currentTarget) onClose();
}}
>
<div
className="mermaid-zoom-canvas"
style={{ width: `${zoom * 100}%` }}
dangerouslySetInnerHTML={{ __html: svg }}
/>
</div>
</div>
</dialog>
);
}
interface CodeBlockProps {
code: string;
lang: string;
headerAction?: ReactNode;
}
/**
* Syntax-highlighted code block with copy button.
* Used as the "source" view for mermaid blocks and for all non-mermaid code fences.
*/
export function CodeBlock({ code, lang, headerAction }: CodeBlockProps) {
const { isDark } = useTheme();
const { t } = useI18n();
const [copied, setCopied] = useState(false);
const copy = () => {
copyText(code).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 1500);
});
};
return (
<div className="markdown-code-block">
<div className="markdown-code-header">
<span className="markdown-code-lang">{lang || "text"}</span>
<div className="markdown-code-actions">
{headerAction}
<button
onClick={copy}
className="markdown-code-action"
>
{copied ? t("i18n.copied") : t("i18n.copy")}
</button>
</div>
</div>
<SyntaxHighlighter
language={lang || "text"}
style={isDark ? vscDarkPlus : vs}
showLineNumbers
lineNumberStyle={{ color: "var(--text-dim)", fontStyle: "normal" }}
customStyle={{
margin: 0,
padding: "11px 13px",
fontSize: 12.5,
lineHeight: 1.62,
borderRadius: 0,
background: "color-mix(in srgb, var(--bg) 92%, var(--bg-panel))",
}}
codeTagProps={{ style: { fontFamily: "var(--font-mono)" } }}
>
{code}
</SyntaxHighlighter>
</div>
);
}