'use client'; import React, { useEffect } from 'react'; import { useEditor, EditorContent } from '@tiptap/react'; import StarterKit from '@tiptap/starter-kit'; import Underline from '@tiptap/extension-underline'; import Link from '@tiptap/extension-link'; import Placeholder from '@tiptap/extension-placeholder'; interface RichTextEditorProps { value: string; onChange: (html: string) => void; placeholder?: string; minHeight?: string; } export default function RichTextEditor({ value, onChange, placeholder = 'Notes...', minHeight = '120px' }: RichTextEditorProps) { const editor = useEditor({ immediatelyRender: false, extensions: [ StarterKit.configure({ heading: { levels: [1, 2, 3] }, }), Underline, Link.configure({ openOnClick: false, HTMLAttributes: { rel: 'noopener noreferrer', target: '_blank' }, }), Placeholder.configure({ placeholder }), ], content: value, onUpdate: ({ editor }) => { // When content is just empty paragraph, return empty string const html = editor.isEmpty ? '' : editor.getHTML(); onChange(html); }, editorProps: { attributes: { class: 'rte-editor-content', style: `min-height: ${minHeight}; outline: none;`, }, }, }); // Sync external value changes (e.g. when editing an existing event) useEffect(() => { if (editor && editor.getHTML() !== value && !editor.isFocused) { editor.commands.setContent(value || ''); } }, [value, editor]); if (!editor) return null; const btn = (active: boolean) => ({ padding: '3px 7px', border: '1px solid var(--weekly-border, #ddd)', borderRadius: '4px', background: active ? 'var(--weekly-accent, #2563eb)' : 'var(--weekly-bg, white)', color: active ? 'white' : 'var(--weekly-text, #333)', cursor: 'pointer', fontSize: '0.78rem', fontWeight: active ? '700' : '500', lineHeight: 1, transition: 'all 0.15s', } as React.CSSProperties); const handleLink = () => { const prev = editor.getAttributes('link').href || ''; const url = window.prompt('Enter URL:', prev); if (url === null) return; if (url === '') { editor.chain().focus().unsetLink().run(); } else { editor.chain().focus().setLink({ href: url }).run(); } }; return (