import { useRef, useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { Bold, Italic, Underline, List, ListOrdered, Heading1, Heading2, Heading3, AlignLeft, AlignCenter, AlignRight, Code, Quote, Link as LinkIcon } from 'lucide-react'; interface RichTextEditorProps { content: string; onChange: (content: string) => void; placeholder?: string; } export function RichTextEditor({ content, onChange, placeholder }: RichTextEditorProps) { const editorRef = useRef(null); const [selectedFormat, setSelectedFormat] = useState([]); // Set initial content only once on mount useEffect(() => { if (editorRef.current) { editorRef.current.innerHTML = content; } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Empty deps - only run once on mount const execCommand = (command: string, value?: string) => { document.execCommand(command, false, value); editorRef.current?.focus(); updateContent(); }; const updateContent = () => { if (editorRef.current) { onChange(editorRef.current.innerHTML); } }; const handleKeyDown = (e: React.KeyboardEvent) => { // Gestion des raccourcis clavier if (e.ctrlKey || e.metaKey) { switch (e.key.toLowerCase()) { case 'b': e.preventDefault(); execCommand('bold'); break; case 'i': e.preventDefault(); execCommand('italic'); break; case 'u': e.preventDefault(); execCommand('underline'); break; } } }; const insertLink = () => { const url = prompt('Entrez l\'URL:'); if (url) { execCommand('createLink', url); } }; const formatButton = ( icon: React.ReactNode, command: string, value?: string, tooltip?: string ) => ( ); return (
{/* Barre d'outils */}
{/* Titres */}
{formatButton(, 'formatBlock', '

', 'Titre 1')} {formatButton(, 'formatBlock', '

', 'Titre 2')} {formatButton(, 'formatBlock', '

', 'Titre 3')}

{/* Formatage de texte */}
{formatButton(, 'bold', undefined, 'Gras (Ctrl+B)')} {formatButton(, 'italic', undefined, 'Italique (Ctrl+I)')} {formatButton(, 'underline', undefined, 'Souligné (Ctrl+U)')}
{/* Listes */}
{formatButton(, 'insertUnorderedList', undefined, 'Liste à puces')} {formatButton(, 'insertOrderedList', undefined, 'Liste numérotée')}
{/* Alignement */}
{formatButton(, 'justifyLeft', undefined, 'Aligner à gauche')} {formatButton(, 'justifyCenter', undefined, 'Centrer')} {formatButton(, 'justifyRight', undefined, 'Aligner à droite')}
{/* Autres */}
{formatButton(, 'formatBlock', '
', 'Citation')} {formatButton(, 'formatBlock', '
', 'Code')}
              
            
{/* Zone d'édition */}
); }