'use client'; import { useRef, useEffect, useState } from 'react'; import { Box, Toolbar, IconButton, Divider, Select, MenuItem, FormControl } from '@mui/material'; import { FormatBold, FormatItalic, FormatUnderlined, FormatListBulleted, FormatListNumbered, Link, Image, Undo, Redo } from '@mui/icons-material'; interface SimpleRichEditorProps { value: string; onChange: (value: string) => void; height?: number; } export function SimpleRichEditor({ value, onChange, height = 400 }: SimpleRichEditorProps) { const editorRef = useRef(null); const [isUpdating, setIsUpdating] = useState(false); // Update editor content when value prop changes (but not when we're updating it ourselves) useEffect(() => { if (!isUpdating && editorRef.current && editorRef.current.innerHTML !== value) { editorRef.current.innerHTML = value; } }, [value, isUpdating]); const handleInput = () => { if (editorRef.current) { setIsUpdating(true); onChange(editorRef.current.innerHTML); setTimeout(() => setIsUpdating(false), 0); } }; const execCommand = (command: string, value?: string) => { document.execCommand(command, false, value); editorRef.current?.focus(); handleInput(); }; const insertHeading = (tag: string) => { if (tag === 'paragraph') { execCommand('formatBlock', 'div'); } else { execCommand('formatBlock', tag); } }; return ( execCommand('bold')} size="small"> execCommand('italic')} size="small"> execCommand('underline')} size="small"> execCommand('insertUnorderedList')} size="small"> execCommand('insertOrderedList')} size="small"> { const url = prompt('Enter link URL:'); if (url) execCommand('createLink', url); }} size="small" > execCommand('undo')} size="small"> execCommand('redo')} size="small">
); }