@@ -9,15 +9,15 @@ export * from './commands';
99
1010export interface IMarkdownEditor extends ICodeMirror {
1111 className ?: string ;
12- prefixCls ?: string ,
13- value ?: string ,
14- height ?: number ,
15- visible ?: boolean ,
16- visibleEditor ?: boolean ,
17- toolbars ?: IToolBarProps [ 'toolbars' ] ,
18- toolbarsMode ?: IToolBarProps [ 'toolbars' ] ,
12+ prefixCls ?: string ;
13+ value ?: string ;
14+ height ?: number ;
15+ visible ?: boolean ;
16+ visibleEditor ?: boolean ;
17+ toolbars ?: IToolBarProps [ 'toolbars' ] ;
18+ toolbarsMode ?: IToolBarProps [ 'toolbars' ] ;
1919 previewProps ?: MarkdownPreviewProps ;
20- options ?: CodeMirror . EditorConfiguration ,
20+ options ?: CodeMirror . EditorConfiguration ;
2121}
2222
2323export interface MarkdownEditorRef {
@@ -29,25 +29,25 @@ export default React.forwardRef<MarkdownEditorRef, IMarkdownEditor>(MarkdownEdit
2929
3030function MarkdownEditor (
3131 props : IMarkdownEditor ,
32- ref ?:
33- | ( ( instance : MarkdownEditorRef ) => void )
34- | React . RefObject < MarkdownEditorRef >
35- | null
32+ ref ?: ( ( instance : MarkdownEditorRef ) => void ) | React . RefObject < MarkdownEditorRef > | null ,
3633) {
3734 const {
38- prefixCls = 'md-editor' , className,
35+ prefixCls = 'md-editor' ,
36+ className,
3937 onChange,
4038 toolbars = getCommands ( ) ,
4139 toolbarsMode = getModeCommands ( ) ,
4240 visible = true ,
4341 visibleEditor = true ,
44- previewProps = { } , ...codemirrorProps } = props ;
42+ previewProps = { } ,
43+ ...codemirrorProps
44+ } = props ;
4545 const [ value , setValue ] = useState ( props . value || '' ) ;
4646 const codeMirror = createRef < CodeMirror > ( ) ;
47- const previewContainer = useRef < HTMLDivElement | null > ( )
47+ const previewContainer = useRef < HTMLDivElement | null > ( ) ;
4848 const [ editor , setEditor ] = useState < CodeMirror . Editor > ( ) ;
49- const container = useRef < HTMLDivElement > ( null ) ;
50- const containerEditor = useRef < HTMLDivElement > ( null ) ;
49+ const container = useRef < HTMLDivElement > ( null ) ;
50+ const containerEditor = useRef < HTMLDivElement > ( null ) ;
5151
5252 useImperativeHandle ( ref , ( ) => ( {
5353 editor : editor ,
@@ -58,47 +58,44 @@ function MarkdownEditor(
5858 if ( codeMirror . current ) {
5959 setEditor ( codeMirror . current . editor ) ;
6060 }
61- } , [ codeMirror . current ] ) ;
61+ } , [ codeMirror ] ) ;
6262
6363 const toolBarProps = {
6464 editor,
6565 preview : previewContainer . current ,
6666 container : container . current ,
6767 containerEditor : containerEditor . current ,
68- editorProps : props
69- }
70- const codeEditor = useMemo ( ( ) => (
71- < CodeMirror
72- visibleEditor = { visibleEditor }
73- { ...codemirrorProps }
74- ref = { codeMirror }
75- onChange = { ( editor , data ) => {
76- setValue ( editor . getValue ( ) ) ;
77- onChange && onChange ( editor , data , editor . getValue ( ) )
78- } }
79- />
80- ) , [ visibleEditor , codemirrorProps ] )
68+ editorProps : props ,
69+ } ;
8170 return (
82- < div ref = { container } >
83- < div className = { `${ prefixCls || '' } ${ className || '' } ` } >
84- < ToolBar { ...toolBarProps } toolbars = { toolbarsMode } mode />
85- < ToolBar { ...toolBarProps } toolbars = { toolbars } />
86- < div className = { `${ prefixCls } -content` } >
87- < div className = { `${ prefixCls } -content-editor` } ref = { containerEditor } >
88- { visibleEditor && codeEditor }
89- </ div >
90- < MarkdownPreview
91- { ...previewProps }
92- data-visible = { ! ! visible }
93- className = { `${ prefixCls } -preview` }
94- ref = { ( instance ) => {
95- if ( instance && instance . mdp ) {
96- previewContainer . current = instance . mdp . current
97- }
98- } }
99- source = { value }
100- />
71+ < div className = { `${ prefixCls || '' } ${ className || '' } ` } ref = { container } >
72+ < ToolBar { ...toolBarProps } toolbars = { toolbarsMode } mode />
73+ < ToolBar { ...toolBarProps } toolbars = { toolbars } />
74+ < div className = { `${ prefixCls } -content` } style = { { height : codemirrorProps . height } } >
75+ < div className = { `${ prefixCls } -content-editor` } ref = { containerEditor } >
76+ { visibleEditor && (
77+ < CodeMirror
78+ visibleEditor = { visibleEditor }
79+ { ...codemirrorProps }
80+ ref = { codeMirror }
81+ onChange = { ( editor , data ) => {
82+ setValue ( editor . getValue ( ) ) ;
83+ onChange && onChange ( editor , data , editor . getValue ( ) ) ;
84+ } }
85+ />
86+ ) }
10187 </ div >
88+ < MarkdownPreview
89+ { ...previewProps }
90+ data-visible = { ! ! visible }
91+ className = { `${ prefixCls } -preview` }
92+ ref = { ( instance ) => {
93+ if ( instance && instance . mdp ) {
94+ previewContainer . current = instance . mdp . current ;
95+ }
96+ } }
97+ source = { value }
98+ />
10299 </ div >
103100 </ div >
104101 ) ;
0 commit comments