@@ -3,6 +3,7 @@ import * as React from "react";
33import { IInstance } from './common/codemirror' ;
44import { IProps } from './common/props' ;
55import CodeMirror , { ICodeMirror } from './components/CodeMirror' ;
6+ import PreviewMarkdown from './components/PreviewMarkdown' ;
67import ToolBar from './components/ToolBar' ;
78import './index.less' ;
89
@@ -15,7 +16,7 @@ export interface IMarkdownEditor extends IProps, ICodeMirror {
1516}
1617
1718interface IMarkdownEditorState {
18- editor ?: CodeMirror ;
19+ preview : boolean ;
1920}
2021
2122export default class MarkdownEditor extends React . PureComponent < IMarkdownEditor , IMarkdownEditorState , { } > {
@@ -25,20 +26,33 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
2526 prefixCls : 'md-editor' ,
2627 value : '' ,
2728 } ;
28- // public state: IMarkdownEditorState = {
29- // editor: undefined,
30- // };
29+ public preview ! : PreviewMarkdown ;
3130 public CodeMirror ! : CodeMirror ;
31+ constructor ( props : IMarkdownEditor ) {
32+ super ( props ) ;
33+ this . state = {
34+ preview : true ,
35+ }
36+ }
3237 public render ( ) {
3338 const { prefixCls, className, toolbars, onChange, ...codemirrorProps } = this . props ;
3439 return (
3540 < div className = { classnames ( prefixCls , className ) } >
3641 < ToolBar toolbars = { toolbars } onClick = { this . onClick } />
37- < CodeMirror
38- ref = { this . getInstance }
39- { ...codemirrorProps }
40- onChange = { this . onChange }
41- />
42+ < div className = { classnames ( `${ prefixCls } -content` ) } >
43+ < CodeMirror
44+ style = { { width : this . state . preview ? '50%' : '100%' } }
45+ width = { this . state . preview ? '50%' : '100%' }
46+ ref = { this . getInstance }
47+ { ...codemirrorProps }
48+ onChange = { this . onChange }
49+ />
50+ < PreviewMarkdown
51+ ref = { ( pmd : PreviewMarkdown ) => this . preview = pmd }
52+ value = { this . props . value }
53+ visble = { this . state . preview }
54+ />
55+ </ div >
4256 </ div >
4357 ) ;
4458 }
@@ -50,10 +64,20 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
5064 private onChange = ( editor : IInstance , data : CodeMirror . EditorChange , value : string ) => {
5165 const { onChange } = this . props as IMarkdownEditor ;
5266 if ( onChange ) {
67+ if ( this . preview ) {
68+ this . preview . updateSource ( editor . getValue ( ) ) ;
69+ }
5370 onChange ( editor , data , value ) ;
5471 }
5572 }
73+ private previewMarkdown ( ) {
74+ this . setState ( { preview : ! this . state . preview } ) ;
75+ }
5676 private onClick = ( type : string ) => {
77+ if ( type === 'preview' ) {
78+ this . previewMarkdown ( ) ;
79+ return ;
80+ }
5781 const selection = this . CodeMirror . editor . getSelection ( ) ;
5882 const pos = this . CodeMirror . editor . getCursor ( ) ;
5983 let value = '' ;
0 commit comments