@@ -3,23 +3,96 @@ let codemirror = require('codemirror');
33
44export default class CodeMirror extends React . Component {
55
6+ constructor ( props ) {
7+ super ( props ) ;
8+
9+ this . initHydration = false ;
10+ }
11+
12+ componentWillMount ( ) {
13+ if ( this . props . editorWillMount ) {
14+ this . props . editorWillMount ( codemirror ) ;
15+ }
16+ }
17+
618 componentDidMount ( ) {
719
820 this . editor = codemirror ( this . ref ) ;
9- this . editor . on ( 'change' , ( ) => this . props . onChange ( this . editor . getValue ( ) ) ) ;
21+
22+ this . editor . on ( 'change' , ( ) => {
23+ if ( this . props . onChange && this . initHydration ) {
24+ this . props . onChange ( this . editor . getValue ( ) ) ;
25+ }
26+ } ) ;
27+
28+ if ( this . props . onCursorActivity ) {
29+ this . editor . on ( 'cursorActivity' , this . props . onCursorActivity ) ;
30+ }
31+
32+ if ( this . props . onViewportChange ) {
33+ this . editor . on ( 'viewportChange' , ( cm , start , end ) => {
34+ this . props . onViewportChange ( cm , start , end ) ;
35+ } ) ;
36+ }
37+
38+ if ( this . props . onGutterClick ) {
39+ this . editor . on ( 'gutterClick' , ( cm , lineNumber , event ) => {
40+ this . props . onGutterClick ( cm , lineNumber , event ) ;
41+ } ) ;
42+ }
43+
44+ if ( this . props . onFocus ) {
45+ this . editor . on ( 'focus' , this . props . onFocus ) ;
46+ }
47+
48+ if ( this . props . onBlur ) {
49+ this . editor . on ( 'blur' , this . props . onBlur ) ;
50+ }
51+
52+ if ( this . props . onScroll ) {
53+ this . editor . on ( 'scroll' , this . props . onScroll ) ;
54+ }
55+
56+ if ( this . props . onUpdate ) {
57+ this . editor . on ( 'update' , this . props . onUpdate ) ;
58+ }
1059
1160 this . hydrate ( this . props ) ;
61+
62+ if ( this . props . editorDidMount ) {
63+ this . props . editorDidMount ( codemirror ) ;
64+ }
1265 }
1366
1467 componentWillReceiveProps ( nextProps ) {
1568
1669 this . hydrate ( nextProps ) ;
1770 }
1871
72+ componentWillUnmount ( ) {
73+
74+ if ( this . props . editorWillUnmount ) {
75+ this . props . editorWillUnmount ( codemirror ) ;
76+ }
77+ }
78+
1979 hydrate ( props ) {
2080
2181 Object . keys ( props . options || { } ) . forEach ( key => this . editor . setOption ( key , props . options [ key ] ) ) ;
22- this . editor . setValue ( props . value || '' ) ;
82+
83+ if ( this . props . editorDidConfigure ) {
84+ this . props . editorDidConfigure ( codemirror ) ;
85+ }
86+
87+ if ( this . props . defaultValue && ! this . initHydration ) {
88+ this . editor . setValue ( props . defaultValue ) ;
89+
90+ if ( this . props . onSetDefaultValue ) {
91+ this . props . onSetDefaultValue ( this . editor . getValue ( ) ) ;
92+ }
93+ }
94+
95+ this . initHydration = true ;
2396 }
2497
2598 render ( ) {
0 commit comments