'use client'; import { useState, useEffect } from 'react'; import { Box, Typography, Button, TextField, IconButton, Switch, FormControlLabel, CircularProgress, Chip } from '@mui/material'; import EditIcon from '@mui/icons-material/Edit'; import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh'; import ReactMarkdown from 'react-markdown'; import { useTranslation } from 'react-i18next'; import { useTheme } from '@mui/material/styles'; import 'github-markdown-css/github-markdown-light.css'; function getValue(value, answerType, useMarkdown, t, onOptimize) { if (value) { if (answerType === 'custom_format' && onOptimize) { try { const data = JSON.parse(value); value = JSON.stringify(data, null, 2); return ( {JSON.stringify(data, null, 2)} ); } catch {} } if (answerType === 'label' && onOptimize) { try { const labels = JSON.parse(value); if (Array.isArray(labels)) { return ( {labels.map((label, idx) => ( ))} ); } } catch { return {value}; } } return useMarkdown ? (
{value}
) : ( {value} ); } else { return ( {t('common.noData')} ); } } /** * 可编辑字段组件,支持 Markdown 和原始文本两种展示方式 */ export default function EditableField({ label, value, multiline = true, editing, onEdit, onChange, onSave, onCancel, onOptimize, tokenCount, optimizing = false, dataset }) { const { t } = useTranslation(); const theme = useTheme(); const { answerType } = dataset; const custom = answerType === 'custom_format' || answerType === 'label'; // 从 localStorage 读取 Markdown 展示设置,默认为 false const [useMarkdown, setUseMarkdown] = useState(() => { if (typeof window !== 'undefined') { const saved = localStorage.getItem('dataset-use-markdown'); return saved ? JSON.parse(saved) : false; } return false; }); // 当 useMarkdown 状态改变时,保存到 localStorage useEffect(() => { if (typeof window !== 'undefined') { localStorage.setItem('dataset-use-markdown', JSON.stringify(useMarkdown)); } }, [useMarkdown]); const toggleMarkdown = () => { setUseMarkdown(!useMarkdown); }; const getAnswerTypeLabel = type => { switch (type) { case 'label': return t('imageDatasets.typeLabel', '标签'); case 'custom_format': return t('imageDatasets.typeCustom', '自定义'); default: return t('imageDatasets.typeText', '文本'); } }; return ( {label} {!editing && value && ( <> {onOptimize && ( {getAnswerTypeLabel(answerType)} )} {/* 字符数标签 */} {value.length} Characters {/* Token 标签 */} {tokenCount > 0 && ( {tokenCount} Tokens )} )} {!editing && ( <> {onOptimize && !custom && ( {optimizing ? : } )} {!custom && ( } label={{useMarkdown ? 'Markdown' : 'Text'}} sx={{ ml: 1 }} /> )} )} {editing ? ( <> ) : ( {getValue(value, answerType, useMarkdown, t, onOptimize)} )} ); }