import React, { useState } from 'react'; import { useEditor, EditorContent } from '@tiptap/react'; import StarterKit from '@tiptap/starter-kit'; import Link from '@tiptap/extension-link'; import { Bold, Italic, List, ListOrdered, Link as LinkIcon, Heading1, Heading2, Undo, Redo } from 'lucide-react'; import { Button } from '../common'; interface CMSEditorProps { content: string; onChange: (content: string) => void; } export const CMSEditor: React.FC = ({ content, onChange }) => { const [linkUrl, setLinkUrl] = useState(''); const [showLinkDialog, setShowLinkDialog] = useState(false); const editor = useEditor({ extensions: [ StarterKit, Link.configure({ openOnClick: false, }), ], content, onUpdate: ({ editor }) => { onChange(editor.getHTML()); }, }); // Update editor content when prop changes React.useEffect(() => { if (editor && content !== editor.getHTML()) { editor.commands.setContent(content); } }, [content, editor]); if (!editor) { return null; } const addLink = () => { if (linkUrl) { editor.chain().focus().setLink({ href: linkUrl }).run(); setLinkUrl(''); setShowLinkDialog(false); } }; const MenuButton: React.FC<{ onClick: () => void; active?: boolean; children: React.ReactNode; title: string; }> = ({ onClick, active, children, title }) => ( ); return (
{/* Toolbar */}
editor.chain().focus().toggleHeading({ level: 1 }).run()} active={editor.isActive('heading', { level: 1 })} title="Heading 1" > editor.chain().focus().toggleHeading({ level: 2 }).run()} active={editor.isActive('heading', { level: 2 })} title="Heading 2" >
editor.chain().focus().toggleBold().run()} active={editor.isActive('bold')} title="Bold" > editor.chain().focus().toggleItalic().run()} active={editor.isActive('italic')} title="Italic" >
editor.chain().focus().toggleBulletList().run()} active={editor.isActive('bulletList')} title="Bullet List" > editor.chain().focus().toggleOrderedList().run()} active={editor.isActive('orderedList')} title="Ordered List" >
setShowLinkDialog(true)} active={editor.isActive('link')} title="Add Link" >
editor.chain().focus().undo().run()} title="Undo" > editor.chain().focus().redo().run()} title="Redo" >
{/* Link Dialog */} {showLinkDialog && (
setLinkUrl(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && addLink()} placeholder="Enter URL..." className="flex-1 px-3 py-1 border border-primary-300 rounded-md focus:ring-2 focus:ring-primary-500" autoFocus />
)} {/* Editor */}
); }; CMSEditor.displayName = 'CMSEditor';