import { EditorContent, useEditor } from '@tiptap/react' import Link from '@tiptap/extension-link' import Placeholder from '@tiptap/extension-placeholder' import Underline from '@tiptap/extension-underline' import { Markdown } from '@tiptap/markdown' import StarterKit from '@tiptap/starter-kit' import { Card, Splitter, theme } from 'antd' import PropTypes from 'prop-types' import { useEffect, useRef, useState } from 'react' import { useThemeContext } from '../context/ThemeContext' import MarkdownToolbar from './MarkdownToolbar' import CodeBlockEditor from './CodeBlockEditor' const MarkdownInput = ({ value, onChange, minHeight = '120px', size = 'small', showCard = true }) => { const markdownValue = typeof value === 'string' ? value : '' const lastMarkdownRef = useRef(markdownValue) const { isDarkMode } = useThemeContext() const { token } = theme.useToken() const [viewState, setViewState] = useState({ editor: true, code: false }) const [focusedPanel, setFocusedPanel] = useState(null) const editorPanelRef = useRef(null) const codePanelRef = useRef(null) const handlePanelBlur = () => { requestAnimationFrame(() => { const active = document.activeElement if (editorPanelRef.current?.contains(active)) { setFocusedPanel('editor') } else if (codePanelRef.current?.contains(active)) { setFocusedPanel('code') } else { setFocusedPanel(null) } }) } const editor = useEditor({ extensions: [ StarterKit, Link.configure({ autolink: true, defaultProtocol: 'https', openOnClick: false }), Placeholder.configure({ placeholder: 'Enter text here...' }), Underline, Markdown ], content: markdownValue, contentType: 'markdown', editorProps: { attributes: { class: 'md-editor__content' } }, immediatelyRender: false, onUpdate: ({ editor }) => { const nextMarkdown = editor.getMarkdown() lastMarkdownRef.current = nextMarkdown onChange?.(nextMarkdown) } }) useEffect(() => { if (!editor) { return } if (markdownValue !== lastMarkdownRef.current) { editor.commands.setContent(markdownValue, { contentType: 'markdown', emitUpdate: false }) lastMarkdownRef.current = markdownValue } }, [editor, markdownValue]) const editorPanel = (