"use client"; import { useState, useRef, useCallback } from "react"; import { Bold, List, ListOrdered, AlignLeft, AlignRight } from "lucide-react"; interface RichTextEditorProps { value: string; onChange: (value: string) => void; placeholder?: string; minHeight?: string; } export function RichTextEditor({ value, onChange, placeholder, minHeight = "200px" }: RichTextEditorProps) { const editorRef = useRef(null); const [isFocused, setIsFocused] = useState(false); const execCommand = useCallback((command: string, value?: string) => { document.execCommand(command, false, value); if (editorRef.current) { onChange(editorRef.current.innerHTML); } }, [onChange]); const handleInput = () => { if (editorRef.current) { onChange(editorRef.current.innerHTML); } }; const handlePaste = (e: React.ClipboardEvent) => { e.preventDefault(); const text = e.clipboardData.getData('text/plain'); document.execCommand('insertText', false, text); }; const ToolbarButton = ({ onClick, active, children, title }: { onClick: () => void; active?: boolean; children: React.ReactNode; title: string; }) => ( ); return (
{/* Toolbar */}
execCommand('bold')} title="Negrito">
execCommand('insertUnorderedList')} title="Lista com marcadores"> execCommand('insertOrderedList')} title="Lista numerada">
execCommand('justifyLeft')} title="Alinhar à esquerda"> execCommand('justifyRight')} title="Alinhar à direita">
{/* Editor Area */}
setIsFocused(true)} onBlur={() => setIsFocused(false)} className="p-3 outline-none bg-background" style={{ minHeight }} data-placeholder={placeholder} dangerouslySetInnerHTML={{ __html: value }} suppressContentEditableWarning />
); }