import React, { useState, useRef, useEffect, forwardRef } from "react"; import { motion } from "framer-motion"; import { Sparkles, X, Plus, RefreshCw, Loader2 } from 'lucide-react'; import MainEditor from "./main-editor/MainEditor"; import { cn } from "@/public/lib/utils"; import { TextToShotAdapter } from "@/app/service/adapter/textToShot"; import { TagValueObject } from "@/app/service/domain/valueObject"; interface CharacterEditorProps { className?: string; description: string; highlight: TagValueObject[]; onSmartPolish: (text: string) => void; onUpdateText: (text: string) => void; disabled?: boolean; } export const CharacterEditor = forwardRef(({ className, description, highlight, onSmartPolish, onUpdateText, disabled }, ref) => { const [isOptimizing, setIsOptimizing] = useState(false); const [content, setContent] = useState([]); const [isInit, setIsInit] = useState(true); const handleSmartPolish = async () => { setIsOptimizing(true); console.log('-==========handleSmartPolish===========-', content); const text = TextToShotAdapter.fromRoleToText(content); console.log('-==========getText===========-', text); onSmartPolish(text); }; const handleChangeContent = (content: any) => { console.log('-==========handleChangeContent===========-', content); onUpdateText(TextToShotAdapter.fromRoleToText(content)); setContent(content); }; useEffect(() => { setIsInit(true); console.log('-==========description===========-', description); console.log('-==========highlight===========-', highlight); const paragraphs = TextToShotAdapter.fromTextToRole(description, highlight); console.log('-==========paragraphs===========-', paragraphs); setContent(paragraphs); // 保存定时器ID const timerId = setTimeout(() => { setIsInit(false); setIsOptimizing(false); }, 100); // 清理函数:组件卸载时清理定时器 return () => { clearTimeout(timerId); }; }, [description]); // 暴露方法给父组件 React.useImperativeHandle(ref, () => ({ getRoleText: () => { return TextToShotAdapter.fromRoleToText(content); } })); return (
{/* 自由输入区域 */} { !isInit && } {/* 智能润色按钮 */} {isOptimizing ? : } {isOptimizing ? "Optimizing..." : "Optimization"}
); }); CharacterEditor.displayName = 'CharacterEditor';