"use client"; 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 { useEffect } from 'react'; interface TipTapEditorProps { content: string; onChange: (content: string) => void; placeholder?: string; } export default function TipTapEditor({ content, onChange, placeholder = "Write something..." }: TipTapEditorProps) { const editor = useEditor({ extensions: [ StarterKit, Link.configure({ openOnClick: false, }), Placeholder.configure({ placeholder, }), ], content, immediatelyRender: false, editorProps: { attributes: { class: 'prose prose-sm sm:prose-base dark:prose-invert focus:outline-none min-h-[150px]', }, }, onUpdate: ({ editor }) => { // For caption, we generally want plain text or simple markdown, but TipTap gives HTML. // For Instagram captions, we usually just need text with line breaks. // For now, let's pass text. But if we want to keep bold/italic for internal preview, we might keep HTML? // Instagram API takes plain text. // Let's pass simple text for now, but returning getText() limits emojis? No, emojis are text. // Wait, if users want hashtags highlighting etc, TipTap is great. // Let's return text for now as the `caption` state string. onChange(editor.getText({ blockSeparator: "\n" })); }, }); // Update content if external change (e.g. emoji picker) useEffect(() => { if (editor && content !== editor.getText({ blockSeparator: "\n" })) { // Only update if significantly different to avoid cursor jumps // This is tricky with plain text syncing. // Better: only set content if editor is empty or on mount? // Or use commands.setContent if strictly needed. // For simple use case: if (editor.getText() === "") { editor.commands.setContent(content); } // Valid simple sync for emoji insertion at end: const current = editor.getText({ blockSeparator: "\n" }); if (content.startsWith(current) && content.length > current.length) { editor.commands.insertContent(content.slice(current.length)); } } }, [content, editor]); if (!editor) { return null; } return (
); }