// 代码预览组件 import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { oneLight, vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { memo, useEffect, useState } from 'react'; import { useI18n } from '../i18n'; interface CodeViewProps { code: string; editable?: boolean; onChange?: (value: string) => void; disabled?: boolean; } export const CodeView = memo(function CodeView({ code, editable = false, onChange, disabled = false }: CodeViewProps) { const { t } = useI18n(); const [copied, setCopied] = useState(false); const [isEditing, setIsEditing] = useState(false); const [isDark, setIsDark] = useState( typeof document !== 'undefined' && document.documentElement.classList.contains('dark') ); useEffect(() => { if (typeof document === 'undefined') { return; } const updateThemeState = () => { setIsDark(document.documentElement.classList.contains('dark')); }; updateThemeState(); const observer = new MutationObserver(updateThemeState); observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }); return () => observer.disconnect(); }, []); const handleCopy = async () => { await navigator.clipboard.writeText(code); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const textareaClassName = [ 'w-full h-full resize-none bg-transparent p-4 text-[0.75rem] leading-relaxed overflow-auto', 'font-mono text-text-primary/90 focus:outline-none', disabled ? 'opacity-60 cursor-not-allowed' : '' ] .filter(Boolean) .join(' '); return (