@@ -11,6 +11,7 @@ export interface IMarkdownEditor extends IProps, ICodeMirror {
1111 prefixCls ?: string ,
1212 value ?: string ,
1313 height ?: number ,
14+ visble ?: boolean ,
1415 toolbars ?: string [ ] ,
1516 options ?: CodeMirror . EditorConfiguration ,
1617}
@@ -25,36 +26,45 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
2526 onChange : ( ) => null ,
2627 prefixCls : 'md-editor' ,
2728 value : '' ,
29+ visble : true ,
2830 } ;
2931 public preview ! : PreviewMarkdown ;
3032 public CodeMirror ! : CodeMirror ;
31- constructor ( props : IMarkdownEditor ) {
32- super ( props ) ;
33- this . state = {
34- preview : true ,
35- }
36- }
3733 public render ( ) {
38- const { prefixCls, className, toolbars, onChange, ...codemirrorProps } = this . props ;
34+ const { prefixCls, className, toolbars, onChange, visble , ...codemirrorProps } = this . props ;
3935 return (
4036 < div className = { classnames ( prefixCls , className ) } >
4137 < ToolBar toolbars = { toolbars } onClick = { this . onClick } />
4238 < div className = { classnames ( `${ prefixCls } -content` ) } >
4339 < CodeMirror
44- width = { this . state . preview ? '50%' : '100%' }
4540 ref = { this . getInstance }
4641 { ...codemirrorProps }
4742 onChange = { this . onChange }
4843 />
4944 < PreviewMarkdown
45+ visble = { visble }
5046 ref = { ( pmd : PreviewMarkdown ) => this . preview = pmd }
5147 value = { this . props . value }
52- visble = { this . state . preview }
5348 />
5449 </ div >
5550 </ div >
5651 ) ;
5752 }
53+
54+ public componentDidMount ( ) {
55+ if ( this . preview ) {
56+ this . props . visble ? this . preview . show ( ) : this . preview . hide ( ) ;
57+ this . CodeMirror . editor . setSize ( this . props . visble ? '50%' : '100%' ) ;
58+ }
59+ }
60+
61+ public componentWillReceiveProps ( nextProps : IMarkdownEditor ) {
62+ if ( nextProps . visble !== this . props . visble ) {
63+ nextProps . visble ? this . preview . show ( ) : this . preview . hide ( ) ;
64+ this . CodeMirror . editor . setSize ( nextProps . visble ? '50%' : '100%' ) ;
65+ }
66+ }
67+
5868 public getInstance = ( editor : CodeMirror ) => {
5969 if ( editor ) {
6070 this . CodeMirror = editor ;
@@ -70,7 +80,10 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
7080 }
7181 }
7282 private previewMarkdown ( ) {
73- this . setState ( { preview : ! this . state . preview } ) ;
83+ if ( this . preview ) {
84+ this . preview . state . visble ? this . preview . hide ( ) : this . preview . show ( ) ;
85+ this . CodeMirror . editor . setSize ( this . preview . state . visble ? '100%' : '50%' ) ;
86+ }
7487 }
7588 private onClick = ( type : string ) => {
7689 if ( type === 'preview' ) {
0 commit comments