import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { createEditor } from 'slate'; import { withReact, Slate, Editable, RenderElementProps, RenderLeafProps, } from 'slate-react'; type ExtendedRenderElementProps = RenderElementProps & { mode?: string }; export const renderElement = ({ attributes, children, element, mode, }: ExtendedRenderElementProps) => { switch (element.type) { case 'block-quote': return
{children}
; case 'heading-2': return (

{children}

); case 'heading-3': return (

{children}

); case 'code-block': return (
          {children}
        
); case 'code-line': return
{children}
; case 'link': return ( {children} ); default: return (

{children}

); } }; export const renderLeaf = ({ attributes, children, leaf }: RenderLeafProps) => { let el = children; if (leaf.bold) { el = {el}; } if (leaf.italic) { el = {el}; } if (leaf.underline) { el = {el}; } if (leaf.link) { el = ( {el} ); } return {el}; }; interface ReadOnlySlateProps { content: any; mode?: string; } const ReadOnlySlate: React.FC = ({ content, mode }) => { const [load, setLoad] = useState(false); const editor = useMemo(() => withReact(createEditor()), []); const value = useMemo(() => content, [content]); const performUpdate = useCallback(async () => { setLoad(true); await new Promise((res) => { setTimeout(() => { res(); }, 250); }); setLoad(false); }, []); useEffect(() => { performUpdate(); }, [value]); if (load) return null; return ( {}}> renderElement({ ...props, mode })} renderLeaf={renderLeaf} /> ); }; export default ReadOnlySlate;