import { useCallback, useEffect, useRef, useState } from 'react'; import LexicalEditor from '@/lib/editor/lexical-editor'; import RawMarkdownEditor from '@/lib/editor/raw-markdown-editor'; interface MarkdownEditorProps { content: string; onChange?: (content: string) => void; readOnly?: boolean; onWikiLinkClick?: (pageType: 'concept' | 'entity', slug: string) => void; } export default function MarkdownEditor({ content, onChange, readOnly = false, onWikiLinkClick, }: MarkdownEditorProps) { const [showSource, setShowSource] = useState(false); const [rawContent, setRawContent] = useState(content); const contentRef = useRef(content); useEffect(() => { if (content === contentRef.current) { return; } if (showSource) setRawContent(content); }, [showSource, content]); const handleWysiwygChange = useCallback( (md: string) => { if (readOnly) return; contentRef.current = md; onChange?.(md); }, [onChange, readOnly], ); const handleRawChange = useCallback( (value: string) => { if (readOnly) return; setRawContent(value); contentRef.current = value; onChange?.(value); }, [onChange, readOnly], ); const toggleSource = useCallback(() => { if (!showSource) setRawContent(contentRef.current); setShowSource((prev) => !prev); }, [showSource]); return (
{rawContent}