Skip to content

Commit 2e36c9b

Browse files
committed
adding codemirror api events
1 parent aaf610d commit 2e36c9b

File tree

5 files changed

+209
-14
lines changed

5 files changed

+209
-14
lines changed

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,35 @@
66
import CodeMirror from 'react-codemirror2'
77
88
<CodeMirror
9-
value='foo'
10-
options={{theme: 'material'}}
11-
onChange={(value) => {
12-
console.log(value);
13-
}} />
9+
defaultValue='react-codemirror2'
10+
options={{theme: 'material', lineNumbers: true}}
11+
editorWillMount={(cm) => {
12+
}}
13+
editorDidMount={(cm) => {
14+
}}
15+
editorDidConfigure={(cm) => {
16+
}}
17+
editorWillUnmount={(cm) => {
18+
}}
19+
onSetDefaultValue={(defaultValue) => {
20+
}}
21+
onChange={(internalValue) => {
22+
}}
23+
onCursorActivity={() => {
24+
}}
25+
onViewportChange={(cm, viewportStart, viewportEnd) => {
26+
}}
27+
onGutterClick={(cm, lineNumber, event) => {
28+
}}
29+
onFocus={() => {
30+
}}
31+
onBlur={() => {
32+
}}
33+
onScroll={() => {
34+
}}
35+
onUpdate={() => {
36+
}}
37+
/>
1438
```
1539

16-
// TODO
40+
// better docs coming soon. all props are optional...

demo/index.jsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,32 @@ require('./index.scss');
66

77
render(
88
<CodeMirror
9-
value='foo'
10-
options={{theme: 'material', viewportMargin: Infinity}}
11-
onChange={(value) => console.log(value)}/>
9+
defaultValue='react-codemirror2'
10+
options={{theme: 'material', lineNumbers: true}}
11+
editorWillMount={(cm) => {
12+
}}
13+
editorDidMount={(cm) => {
14+
}}
15+
editorDidConfigure={(cm) => {
16+
}}
17+
editorWillUnmount={(cm) => {
18+
}}
19+
onSetDefaultValue={(defaultValue) => {
20+
}}
21+
onChange={(internalValue) => {
22+
}}
23+
onCursorActivity={() => {
24+
}}
25+
onViewportChange={(cm, viewportStart, viewportEnd) => {
26+
}}
27+
onGutterClick={(cm, lineNumber, event) => {
28+
}}
29+
onFocus={() => {
30+
}}
31+
onBlur={() => {
32+
}}
33+
onScroll={() => {
34+
}}
35+
onUpdate={() => {
36+
}}/>
1237
, document.getElementById('app'));

index.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,96 @@ let codemirror = require('codemirror');
33

44
export 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() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-codemirror2",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "a tiny react codemirror component wrapper",
55
"main": "index.js",
66
"scripts": {

src/react-codemirror2.jsx

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,96 @@ let codemirror = require('codemirror');
33

44
export 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

Comments
 (0)