"use client"; import { Button } from "@mdxui/primitives/button"; import { Card } from "@mdxui/primitives/card"; import { cn } from "@mdxui/primitives/lib/utils"; import { Edit3, Eye, Split } from "lucide-react"; import { useCallback, useState } from "react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; export interface MarkdownEditorProps { /** Initial markdown content */ initialValue?: string; /** Callback when content changes */ onChange?: (value: string) => void; /** Placeholder text */ placeholder?: string; /** Default view mode */ defaultMode?: "edit" | "preview" | "split"; /** Height of the editor */ height?: string | number; /** Additional CSS class */ className?: string; /** Whether the editor is read-only */ readOnly?: boolean; /** Enable GitHub Flavored Markdown */ enableGfm?: boolean; /** Custom toolbar actions */ toolbar?: React.ReactNode; } export const MarkdownEditor: React.FC = ({ initialValue = "", onChange, placeholder = "Start writing...", defaultMode = "split", height = 400, className, readOnly = false, enableGfm = true, toolbar, }) => { const [content, setContent] = useState(initialValue); const [mode, setMode] = useState<"edit" | "preview" | "split">(defaultMode); const handleChange = useCallback( (value: string) => { setContent(value); onChange?.(value); }, [onChange], ); const insertMarkdown = useCallback( (before: string, after = "") => { const textarea = document.querySelector( ".markdown-textarea", ) as HTMLTextAreaElement; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const selectedText = content.substring(start, end); const newText = content.substring(0, start) + before + selectedText + after + content.substring(end); handleChange(newText); setTimeout(() => { textarea.focus(); textarea.setSelectionRange(start + before.length, end + before.length); }, 0); }, [content, handleChange], ); const defaultToolbar = (
); const editorArea = (