import { CodeHighlightNode, CodeNode } from '@lexical/code' import { AutoLinkNode, LinkNode } from '@lexical/link' import { ListItemNode, ListNode } from '@lexical/list' import { $convertFromMarkdownString, $convertToMarkdownString, TRANSFORMERS, } from '@lexical/markdown' import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' import { LexicalComposer } from '@lexical/react/LexicalComposer' import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' import { ContentEditable } from '@lexical/react/LexicalContentEditable' import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin' import { MarkdownShortcutPlugin } from '@lexical/react/LexicalMarkdownShortcutPlugin' import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin' import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin' import { HeadingNode, QuoteNode } from '@lexical/rich-text' import { TableCellNode, TableNode, TableRowNode } from '@lexical/table' import { useEffect, useRef } from 'react' import { ToolbarPlugin } from './lexical/plugins/ToolbarPlugin' import './markdown-editor.css' const theme = { ltr: 'ltr', rtl: 'rtl', placeholder: 'editor-placeholder', paragraph: 'editor-paragraph', quote: 'editor-quote', heading: { h1: 'editor-heading-h1', h2: 'editor-heading-h2', h3: 'editor-heading-h3', h4: 'editor-heading-h4', h5: 'editor-heading-h5', h6: 'editor-heading-h6', }, list: { nested: { listitem: 'editor-nested-listitem', }, ol: 'editor-list-ol', ul: 'editor-list-ul', listitem: 'editor-listitem', }, image: 'editor-image', link: 'editor-link', text: { bold: 'editor-text-bold', italic: 'editor-text-italic', overflowed: 'editor-text-overflowed', hashtag: 'editor-text-hashtag', underline: 'editor-text-underline', strikethrough: 'editor-text-strikethrough', underlineStrikethrough: 'editor-text-underlineStrikethrough', code: 'editor-text-code', }, code: 'editor-code', codeHighlight: { atrule: 'editor-tokenAttr', attr: 'editor-tokenAttr', boolean: 'editor-tokenProperty', builtin: 'editor-tokenSelector', cdata: 'editor-tokenComment', char: 'editor-tokenSelector', class: 'editor-tokenFunction', 'class-name': 'editor-tokenFunction', comment: 'editor-tokenComment', constant: 'editor-tokenProperty', deleted: 'editor-tokenProperty', doctype: 'editor-tokenComment', entity: 'editor-tokenOperator', function: 'editor-tokenFunction', important: 'editor-tokenVariable', inserted: 'editor-tokenSelector', keyword: 'editor-tokenAttr', namespace: 'editor-tokenVariable', number: 'editor-tokenProperty', operator: 'editor-tokenOperator', prolog: 'editor-tokenComment', property: 'editor-tokenProperty', punctuation: 'editor-tokenPunctuation', regex: 'editor-tokenVariable', selector: 'editor-tokenSelector', string: 'editor-tokenSelector', symbol: 'editor-tokenProperty', tag: 'editor-tokenProperty', url: 'editor-tokenOperator', variable: 'editor-tokenVariable', }, } function OnChangeMarkdownPlugin({ onChange }: { onChange: (markdown: string) => void }) { const [editor] = useLexicalComposerContext() return ( { editor.getEditorState().read(() => { const markdown = $convertToMarkdownString(TRANSFORMERS) onChange(markdown) }) }} /> ) } function InitialContentPlugin({ content }: { content: string }) { const [editor] = useLexicalComposerContext() const isFirstRender = useRef(true) useEffect(() => { if (isFirstRender.current) { isFirstRender.current = false editor.update(() => { $convertFromMarkdownString(content, TRANSFORMERS) }) } }, [editor, content]) return null } export interface MarkdownEditorProps { initialContent?: string onChange?: (content: string) => void readOnly?: boolean } export function MarkdownEditor({ initialContent = '', onChange, readOnly = false, }: MarkdownEditorProps) { const initialConfig = { namespace: 'MarkdownEditor', theme, editable: !readOnly, onError: (error: Error) => { console.error(error) }, nodes: [ HeadingNode, ListNode, ListItemNode, QuoteNode, CodeNode, CodeHighlightNode, TableNode, TableCellNode, TableRowNode, AutoLinkNode, LinkNode, ], } return (
{!readOnly && }
} placeholder={
{readOnly ? 'No content.' : 'Enter some text...'}
} ErrorBoundary={LexicalErrorBoundary} /> {onChange && }
) }