import React, { useEffect, useState } from 'react'; import { ICommand } from '.'; import { IMarkdownEditor, ToolBarProps } from '..'; const Preview: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & ToolBarProps }> = (props) => { const { editorProps } = props; const { containerEditor, preview, previewWidth = '50%', enablePreview = true } = editorProps; const [visible, setVisible] = useState(props.editorProps.visible); useEffect(() => setVisible(props.editorProps.visible), [props.editorProps.visible]); useEffect(() => { if (preview.current) { const $preview = preview.current; if (preview) { $preview.style.borderBottomRightRadius = '3px'; } if ($preview && visible) { $preview.style.width = previewWidth; $preview.style.overflow = 'auto'; if (previewWidth !== '100%') { $preview.style.borderLeft = '1px solid var(--color-border-muted)'; } $preview.style.padding = '20px'; if (containerEditor.current) { containerEditor.current.style.width = `calc(100% - ${previewWidth})`; } } else if ($preview) { $preview.style.width = '0%'; $preview.style.overflow = 'hidden'; $preview.style.borderLeft = '0px'; $preview.style.padding = '0'; if (containerEditor.current) { containerEditor.current.style.width = '100%'; } } } }, [visible, containerEditor, preview, previewWidth]); if (!enablePreview) return; const handle = () => { editorProps.onPreviewMode && editorProps.onPreviewMode(!visible); setVisible(!visible); }; return ( ); }; export const preview: ICommand = { name: 'preview', keyCommand: 'preview', button: (command, props, opts) => , icon: ( ), };