import { useEditor, EditorContent, type Editor, Node, mergeAttributes } from '@tiptap/react' import StarterKit from '@tiptap/starter-kit' import Image from '@tiptap/extension-image' import Link from '@tiptap/extension-link' import Mention from '@tiptap/extension-mention' import Placeholder from '@tiptap/extension-placeholder' import { Table, TableRow, TableCell, TableHeader } from '@tiptap/extension-table' import { useCallback, useEffect, useRef, useState, forwardRef, useImperativeHandle } from 'react' import { Bold, Italic, Strikethrough, Code, List, ListOrdered, Quote, Heading1, Heading2, Heading3, Image as ImageIcon, Link as LinkIcon, Table as TableIcon, Undo, Redo, Minus, } from 'lucide-react' export interface PersonItem { id: string name: string label?: string ckbApiKey?: string // 存客宝密钥,留空则 fallback 全局 Key } export interface LinkTagItem { id: string label: string url: string type: 'url' | 'miniprogram' | 'ckb' appId?: string pagePath?: string } export interface RichEditorRef { getHTML: () => string getMarkdown: () => string } interface RichEditorProps { content: string onChange: (html: string) => void onImageUpload?: (file: File) => Promise persons?: PersonItem[] linkTags?: LinkTagItem[] placeholder?: string className?: string } function htmlToMarkdown(html: string): string { if (!html) return '' let md = html md = md.replace(/]*>(.*?)<\/h1>/gi, '# $1\n\n') md = md.replace(/]*>(.*?)<\/h2>/gi, '## $1\n\n') md = md.replace(/]*>(.*?)<\/h3>/gi, '### $1\n\n') md = md.replace(/]*>(.*?)<\/strong>/gi, '**$1**') md = md.replace(/]*>(.*?)<\/b>/gi, '**$1**') md = md.replace(/]*>(.*?)<\/em>/gi, '*$1*') md = md.replace(/]*>(.*?)<\/i>/gi, '*$1*') md = md.replace(/]*>(.*?)<\/s>/gi, '~~$1~~') md = md.replace(/]*>(.*?)<\/del>/gi, '~~$1~~') md = md.replace(/]*>(.*?)<\/code>/gi, '`$1`') md = md.replace(/]*>(.*?)<\/blockquote>/gi, '> $1\n\n') md = md.replace(/]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '![$2]($1)') md = md.replace(/]*src="([^"]*)"[^>]*>/gi, '![]($1)') md = md.replace(/]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)') md = md.replace(/]*>(.*?)<\/li>/gi, '- $1\n') md = md.replace(/<\/?[uo]l[^>]*>/gi, '\n') md = md.replace(//gi, '\n') md = md.replace(/]*>(.*?)<\/p>/gi, '$1\n\n') md = md.replace(//gi, '---\n\n') md = md.replace(/]*data-type="mention"[^>]*data-id="([^"]*)"[^>]*>@([^<]*)<\/span>/gi, '@$2') md = md.replace(/]*data-type="linkTag"[^>]*data-url="([^"]*)"[^>]*>#([^<]*)<\/span>/gi, '#[$2]($1)') md = md.replace(/<[^>]+>/g, '') md = md.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'") md = md.replace(/\n{3,}/g, '\n\n') return md.trim() } function markdownToHtml(md: string): string { if (!md) return '' if (md.startsWith('<') && md.includes('$1') html = html.replace(/^## (.+)$/gm, '

$1

') html = html.replace(/^# (.+)$/gm, '

$1

') html = html.replace(/\*\*(.+?)\*\*/g, '$1') html = html.replace(/\*(.+?)\*/g, '$1') html = html.replace(/~~(.+?)~~/g, '$1') html = html.replace(/`([^`]+)`/g, '$1') html = html.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '$1') html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1') html = html.replace(/^> (.+)$/gm, '

$1

') html = html.replace(/^---$/gm, '
') html = html.replace(/^- (.+)$/gm, '
  • $1
  • ') const lines = html.split('\n') const result: string[] = [] for (const line of lines) { const trimmed = line.trim() if (!trimmed) continue if (/^<(?:h[1-6]|blockquote|hr|li|ul|ol|table|img)/.test(trimmed)) { result.push(trimmed) } else { result.push(`

    ${trimmed}

    `) } } return result.join('') } /** * LinkTagExtension — 自定义 TipTap 内联节点,保留所有 data-* 属性 * 解决:insertContent(html) 会经过 TipTap schema 导致自定义属性被丢弃的问题 */ const LinkTagExtension = Node.create({ name: 'linkTag', group: 'inline', inline: true, selectable: true, atom: true, addAttributes() { return { label: { default: '' }, url: { default: '' }, tagType: { default: 'url', parseHTML: (el: HTMLElement) => el.getAttribute('data-tag-type') || 'url' }, tagId: { default: '', parseHTML: (el: HTMLElement) => el.getAttribute('data-tag-id') || '' }, pagePath: { default: '', parseHTML: (el: HTMLElement) => el.getAttribute('data-page-path') || '' }, appId: { default: '', parseHTML: (el: HTMLElement) => el.getAttribute('data-app-id') || '' }, mpKey: { default: '', parseHTML: (el: HTMLElement) => el.getAttribute('data-mp-key') || '' }, } }, parseHTML() { return [{ tag: 'span[data-type="linkTag"]', getAttrs: (el: HTMLElement) => ({ label: el.textContent?.replace(/^#/, '').trim() || '', url: el.getAttribute('data-url') || '', tagType: el.getAttribute('data-tag-type') || 'url', tagId: el.getAttribute('data-tag-id') || '', pagePath: el.getAttribute('data-page-path')|| '', appId: el.getAttribute('data-app-id') || '', mpKey: el.getAttribute('data-mp-key') || '', }) }] }, // eslint-disable-next-line @typescript-eslint/no-explicit-any renderHTML({ node, HTMLAttributes }: { node: any; HTMLAttributes: Record }) { return ['span', mergeAttributes(HTMLAttributes, { 'data-type': 'linkTag', 'data-url': node.attrs.url, 'data-tag-type': node.attrs.tagType, 'data-tag-id': node.attrs.tagId, 'data-page-path': node.attrs.pagePath, 'data-app-id': node.attrs.appId || '', 'data-mp-key': node.attrs.mpKey || node.attrs.appId || '', class: 'link-tag-node', }), `#${node.attrs.label}`] }, }) // eslint-disable-next-line @typescript-eslint/no-explicit-any const MentionSuggestion = (persons: PersonItem[]): any => ({ items: ({ query }: { query: string }) => persons.filter(p => p.name.toLowerCase().includes(query.toLowerCase()) || p.id.includes(query)).slice(0, 8), render: () => { let popup: HTMLDivElement | null = null let selectedIndex = 0 let items: PersonItem[] = [] // eslint-disable-next-line @typescript-eslint/no-explicit-any let command: ((p: { id: string; label: string }) => void) | null = null const update = () => { if (!popup) return popup.innerHTML = items.map((item, i) => `
    @${item.name} ${item.label || item.id}
    ` ).join('') popup.querySelectorAll('.mention-item').forEach(el => { el.addEventListener('click', () => { const idx = parseInt(el.getAttribute('data-index') || '0') if (command && items[idx]) command({ id: items[idx].id, label: items[idx].name }) }) }) } return { // eslint-disable-next-line @typescript-eslint/no-explicit-any onStart: (props: any) => { popup = document.createElement('div') popup.className = 'mention-popup' document.body.appendChild(popup) items = props.items command = props.command selectedIndex = 0 update() if (props.clientRect) { const rect = props.clientRect() if (rect) { popup.style.top = `${rect.bottom + 4}px` popup.style.left = `${rect.left}px` } } }, // eslint-disable-next-line @typescript-eslint/no-explicit-any onUpdate: (props: any) => { items = props.items command = props.command selectedIndex = 0 update() if (props.clientRect && popup) { const rect = props.clientRect() if (rect) { popup.style.top = `${rect.bottom + 4}px` popup.style.left = `${rect.left}px` } } }, onKeyDown: (props: { event: KeyboardEvent }) => { if (props.event.key === 'ArrowUp') { selectedIndex = Math.max(0, selectedIndex - 1); update(); return true } if (props.event.key === 'ArrowDown') { selectedIndex = Math.min(items.length - 1, selectedIndex + 1); update(); return true } if (props.event.key === 'Enter') { if (command && items[selectedIndex]) command({ id: items[selectedIndex].id, label: items[selectedIndex].name }); return true } if (props.event.key === 'Escape') { popup?.remove(); popup = null; return true } return false }, onExit: () => { popup?.remove(); popup = null }, } }, }) const RichEditor = forwardRef(({ content, onChange, onImageUpload, persons = [], linkTags = [], placeholder = '开始编辑内容...', className, }, ref) => { const fileInputRef = useRef(null) const [linkUrl, setLinkUrl] = useState('') const [showLinkInput, setShowLinkInput] = useState(false) const initialContent = useRef(markdownToHtml(content)) const editor = useEditor({ extensions: [ StarterKit, Image.configure({ inline: true, allowBase64: true }), Link.configure({ openOnClick: false, HTMLAttributes: { class: 'rich-link' } }), Mention.configure({ HTMLAttributes: { class: 'mention-tag' }, suggestion: MentionSuggestion(persons), }), LinkTagExtension, Placeholder.configure({ placeholder }), Table.configure({ resizable: true }), TableRow, TableCell, TableHeader, ], content: initialContent.current, onUpdate: ({ editor: ed }: { editor: Editor }) => { onChange(ed.getHTML()) }, editorProps: { attributes: { class: 'rich-editor-content' }, }, }) useImperativeHandle(ref, () => ({ getHTML: () => editor?.getHTML() || '', getMarkdown: () => htmlToMarkdown(editor?.getHTML() || ''), })) useEffect(() => { if (editor && content !== editor.getHTML()) { const html = markdownToHtml(content) if (html !== editor.getHTML()) { editor.commands.setContent(html) } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [content]) const handleImageUpload = useCallback(async (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (!file || !editor) return if (onImageUpload) { const url = await onImageUpload(file) if (url) editor.chain().focus().setImage({ src: url }).run() } else { const reader = new FileReader() reader.onload = () => { if (typeof reader.result === 'string') editor.chain().focus().setImage({ src: reader.result }).run() } reader.readAsDataURL(file) } e.target.value = '' }, [editor, onImageUpload]) const insertLinkTag = useCallback((tag: LinkTagItem) => { if (!editor) return // 通过自定义扩展节点插入,确保 data-* 属性不被 TipTap schema 丢弃 editor.chain().focus().insertContent({ type: 'linkTag', attrs: { label: tag.label, url: tag.url || '', tagType: tag.type || 'url', tagId: tag.id || '', pagePath: tag.pagePath || '', appId: tag.appId || '', mpKey: tag.type === 'miniprogram' ? (tag.appId || '') : '', }, }).run() }, [editor]) const addLink = useCallback(() => { if (!editor || !linkUrl) return editor.chain().focus().setLink({ href: linkUrl }).run() setLinkUrl('') setShowLinkInput(false) }, [editor, linkUrl]) if (!editor) return null return (
    {linkTags.length > 0 && ( <>
    )}
    {showLinkInput && (
    setLinkUrl(e.target.value)} onKeyDown={e => e.key === 'Enter' && addLink()} className="link-input" />
    )}
    ) }) RichEditor.displayName = 'RichEditor' export default RichEditor