import React, { useRef, useEffect, useState, useCallback } from "react"; import { Editor } from "@tinymce/tinymce-react"; function CustomEditor(props) { const editorRef = useRef(null); const [isEditorReady, setIsEditorReady] = useState(false); const [currentContent, setCurrentContent] = useState(props.initialData || ""); // Handle editor initialization const handleInit = useCallback((evt, editor) => { editorRef.current = editor; setIsEditorReady(true); // Set initial content immediately when editor is ready if (currentContent) { editor.setContent(currentContent); } // Simple onChange handler editor.on('change', () => { const content = editor.getContent(); setCurrentContent(content); if (props.onChange) { props.onChange(content); } }); }, [currentContent, props.onChange]); // Watch for changes in initialData prop useEffect(() => { if (props.initialData !== currentContent) { setCurrentContent(props.initialData || ""); // Update editor content if editor is ready if (editorRef.current && isEditorReady) { editorRef.current.setContent(props.initialData || ""); } } }, [props.initialData, currentContent, isEditorReady]); // Handle initial data when editor becomes ready useEffect(() => { if (isEditorReady && currentContent && editorRef.current) { editorRef.current.setContent(currentContent); } }, [isEditorReady, currentContent]); return ( ); } export default CustomEditor;