Spaces:
Runtime error
Runtime error
File size: 2,806 Bytes
c7fbc2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | "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 (
<div className="w-full">
<EditorContent editor={editor} className="min-h-[100px]" />
</div>
);
}
|