import React, { useEffect } from 'react'; import { useEditor, EditorContent } from '@tiptap/react'; import StarterKit from '@tiptap/starter-kit'; import Placeholder from '@tiptap/extension-placeholder'; import Link from '@tiptap/extension-link'; import { Bold, Italic, List, ListOrdered, Link as LinkIcon, Undo, Redo, } from 'lucide-react'; import { Button } from './button'; import { __ } from '@/lib/i18n'; interface RichTextEditorProps { content: string; onChange: (content: string) => void; placeholder?: string; variables?: string[]; onVariableInsert?: (variable: string) => void; } export function RichTextEditor({ content, onChange, placeholder = __('Start typing...'), variables = [], onVariableInsert, }: RichTextEditorProps) { const editor = useEditor({ extensions: [ StarterKit, Placeholder.configure({ placeholder, }), Link.configure({ openOnClick: false, HTMLAttributes: { class: 'text-primary underline', }, }), ], content, onUpdate: ({ editor }) => { onChange(editor.getHTML()); }, editorProps: { attributes: { class: 'prose prose-sm max-w-none focus:outline-none min-h-[200px] px-4 py-3', }, }, }); // Update editor content when prop changes (fix for default value not showing) useEffect(() => { if (editor && content !== editor.getHTML()) { editor.commands.setContent(content); } }, [content, editor]); if (!editor) { return null; } const insertVariable = (variable: string) => { editor.chain().focus().insertContent(`{${variable}}`).run(); if (onVariableInsert) { onVariableInsert(variable); } }; const setLink = () => { const url = window.prompt(__('Enter URL:')); if (url) { editor.chain().focus().setLink({ href: url }).run(); } }; return (
{/* Toolbar */}
{/* Editor */} {/* Variables */} {variables.length > 0 && (
{__('Available Variables:')}
{variables.map((variable) => ( ))}
)}
); }