import React, { useRef, useEffect } from 'react'; import { Bold, Italic, Underline, Heading1, Heading2, Heading3, List, ListOrdered, Link, AlignRight, AlignCenter, AlignLeft, Palette, Undo, Redo, } from 'lucide-react'; interface RichTextEditorProps { value: string; onChange: (html: string) => void; placeholder?: string; className?: string; } const COLORS = [ { label: 'سیاه / پیش‌فرض', value: '#111827' }, { label: 'خاکستری', value: '#4b5563' }, { label: 'آبی کنینا', value: '#1d4ed8' }, { label: 'بنفش اختصاصی', value: '#7c3aed' }, { label: 'سبز درمانی', value: '#059669' }, { label: 'قرمز هشدار', value: '#dc2626' }, { label: 'کهربایی / زرد', value: '#d97706' }, ]; export default function RichTextEditor({ value, onChange, placeholder, className = '', }: RichTextEditorProps) { const editorRef = useRef(null); const isTypingRef = useRef(false); const isMountedRef = useRef(false); // Sync value from props on initial mount or when external change occurs (not while active) useEffect(() => { if (!editorRef.current) return; if (!isMountedRef.current) { editorRef.current.innerHTML = value || ''; isMountedRef.current = true; return; } // Only update innerHTML if it's different and not currently focused/typing if (editorRef.current.innerHTML !== (value || '')) { if (!isTypingRef.current && document.activeElement !== editorRef.current) { editorRef.current.innerHTML = value || ''; } } }, [value]); const exec = (command: string, val: string | undefined = undefined) => { document.execCommand(command, false, val); if (editorRef.current) { onChange(editorRef.current.innerHTML); } }; const handleLink = () => { const url = prompt('آدرس لینک (URL) را وارد کنید:', 'https://'); if (url) { exec('createLink', url); } }; const handleInput = () => { if (editorRef.current) { isTypingRef.current = true; onChange(editorRef.current.innerHTML); setTimeout(() => { isTypingRef.current = false; }, 50); } }; return (
{/* Toolbar */}
{/* Headings */}
{/* Basic Formats */}
{/* Alignment */}
{/* Lists & Link */}
{/* Color Palette */}
{COLORS.map((c) => (
{/* Undo / Redo */}
{/* Editable Area */}
); }