diff --git a/src/blog/BlogEditor.tsx b/src/blog/BlogEditor.tsx index 228e7bf..5e1aadb 100644 --- a/src/blog/BlogEditor.tsx +++ b/src/blog/BlogEditor.tsx @@ -15,6 +15,8 @@ import { LinkNode, AutoLinkNode } from '@lexical/link'; import { LinkPlugin } from '@lexical/react/LexicalLinkPlugin'; import { AutoLinkPlugin } from '@lexical/react/LexicalAutoLinkPlugin'; import { HashtagNode } from '@lexical/hashtag'; +import { forwardRef, useImperativeHandle } from 'react'; +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { ImageNode } from './nodes/ImageNode'; import { MentionNode } from './nodes/MentionNode'; @@ -87,7 +89,24 @@ const editorConfig = { ], }; -export default function BlogEditor() { +export interface BlogEditorRef { + getEditorState: () => string; +} + +// Plugin to expose editor instance via ref +function EditorRefPlugin({ editorRef }: { editorRef: React.Ref }) { + const [editor] = useLexicalComposerContext(); + + useImperativeHandle(editorRef, () => ({ + getEditorState: () => { + return JSON.stringify(editor.getEditorState().toJSON()); + }, + })); + + return null; +} + +const BlogEditor = forwardRef((_, ref) => { return (
@@ -113,8 +132,13 @@ export default function BlogEditor() { +
); -} +}); + +BlogEditor.displayName = 'BlogEditor'; + +export default BlogEditor; diff --git a/src/pages/CreatePost.tsx b/src/pages/CreatePost.tsx index 35c6def..057889a 100644 --- a/src/pages/CreatePost.tsx +++ b/src/pages/CreatePost.tsx @@ -1,12 +1,13 @@ import { useState, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import Layout from '../components/Layout'; -import BlogEditor from '../blog/BlogEditor'; +import BlogEditor, { type BlogEditorRef } from '../blog/BlogEditor'; import { uploadImage, createBlogPost } from '../blog/api'; import '../App.css'; function CreatePost() { const navigate = useNavigate(); + const editorRef = useRef(null); const [title, setTitle] = useState(''); const [coverImage, setCoverImage] = useState(null); const [coverImageKey, setCoverImageKey] = useState(''); @@ -66,13 +67,17 @@ function CreatePost() { return; } + // Get editor content + if (!editorRef.current) { + alert('Editor not initialized'); + return; + } + setIsSubmitting(true); try { - // Get editor content - // Note: You'll need to export the editor content from the BlogEditor component - // For now, we'll use a placeholder - const content = JSON.stringify({ editorState: 'placeholder' }); + // Get the actual editor state as JSON + const content = editorRef.current.getEditorState(); const response = await createBlogPost(title, content, coverImageKey); @@ -286,7 +291,7 @@ function CreatePost() { overflow: 'hidden', background: 'var(--bg-card)' }}> - +