Skip to content

Commit a2c6cfe

Browse files
committed
key and drag events
1 parent 2e36c9b commit a2c6cfe

File tree

5 files changed

+133
-35
lines changed

5 files changed

+133
-35
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import CodeMirror from 'react-codemirror2'
1818
}}
1919
onSetDefaultValue={(defaultValue) => {
2020
}}
21-
onChange={(internalValue) => {
21+
onChange={(cm, metadata, internalValue) => {
2222
}}
2323
onCursorActivity={() => {
2424
}}
@@ -34,6 +34,18 @@ import CodeMirror from 'react-codemirror2'
3434
}}
3535
onUpdate={() => {
3636
}}
37+
onKeyDown={(cm, event) => {
38+
}}
39+
onKeyUp={(cm, event) => {
40+
}}
41+
onKeyPress={(cm, event) => {
42+
}}
43+
onDragEnter={(cm, event) => {
44+
}}
45+
onDragOver={(cm, event) => {
46+
}}
47+
onDrop={(cm, event) => {
48+
}
3749
/>
3850
```
3951

demo/index.jsx

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,47 @@ import CodeMirror from '../src/react-codemirror2.jsx';
55
require('./index.scss');
66

77
render(
8-
<CodeMirror
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-
}}/>
8+
<div>
9+
<CodeMirror
10+
defaultValue='react-codemirror2'
11+
options={{theme: 'material', lineNumbers: true}}
12+
editorWillMount={(cm) => {
13+
}}
14+
editorDidMount={(cm) => {
15+
}}
16+
editorDidConfigure={(cm) => {
17+
}}
18+
editorWillUnmount={(cm) => {
19+
}}
20+
onSetDefaultValue={(defaultValue) => {
21+
}}
22+
onChange={(cm, metadata, internalValue) => {
23+
}}
24+
onCursorActivity={() => {
25+
}}
26+
onViewportChange={(cm, viewportStart, viewportEnd) => {
27+
}}
28+
onGutterClick={(cm, lineNumber, event) => {
29+
}}
30+
onFocus={() => {
31+
}}
32+
onBlur={() => {
33+
}}
34+
onScroll={() => {
35+
}}
36+
onUpdate={() => {
37+
}}
38+
onKeyDown={(cm, event) => {
39+
}}
40+
onKeyUp={(cm, event) => {
41+
}}
42+
onKeyPress={(cm, event) => {
43+
}}
44+
onDragEnter={(cm, event) => {
45+
}}
46+
onDragOver={(cm, event) => {
47+
}}
48+
onDrop={(cm, event) => {
49+
}}/>
50+
</div>
3751
, document.getElementById('app'));

index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default class CodeMirror extends React.Component {
1919

2020
this.editor = codemirror(this.ref);
2121

22-
this.editor.on('change', () => {
22+
this.editor.on('change', (cm, metadata) => {
2323
if (this.props.onChange && this.initHydration) {
24-
this.props.onChange(this.editor.getValue());
24+
this.props.onChange(cm, metadata, this.editor.getValue());
2525
}
2626
});
2727

@@ -57,6 +57,42 @@ export default class CodeMirror extends React.Component {
5757
this.editor.on('update', this.props.onUpdate);
5858
}
5959

60+
if (this.props.onKeyDown) {
61+
this.editor.on('keydown', (cm, event) => {
62+
this.props.onKeyDown(cm, event);
63+
});
64+
}
65+
66+
if (this.props.onKeyUp) {
67+
this.editor.on('keyup', (cm, event) => {
68+
this.props.onKeyUp(cm, event);
69+
});
70+
}
71+
72+
if (this.props.onKeyPress) {
73+
this.editor.on('keypress', (cm, event) => {
74+
this.props.onKeyPress(cm, event);
75+
});
76+
}
77+
78+
if (this.props.onDragEnter) {
79+
this.editor.on('dragenter', (cm, event) => {
80+
this.props.onDragEnter(cm, event);
81+
});
82+
}
83+
84+
if (this.props.onDragOver) {
85+
this.editor.on('dragover', (cm, event) => {
86+
this.props.onDragOver(cm, event);
87+
});
88+
}
89+
90+
if (this.props.onDrop) {
91+
this.editor.on('drop', (cm, event) => {
92+
this.props.onDrop(cm, event);
93+
});
94+
}
95+
6096
this.hydrate(this.props);
6197

6298
if (this.props.editorDidMount) {

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.3",
3+
"version": "0.0.4",
44
"description": "a tiny react codemirror component wrapper",
55
"main": "index.js",
66
"scripts": {

src/react-codemirror2.jsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default class CodeMirror extends React.Component {
1919

2020
this.editor = codemirror(this.ref);
2121

22-
this.editor.on('change', () => {
22+
this.editor.on('change', (cm, metadata) => {
2323
if (this.props.onChange && this.initHydration) {
24-
this.props.onChange(this.editor.getValue());
24+
this.props.onChange(cm, metadata, this.editor.getValue());
2525
}
2626
});
2727

@@ -57,6 +57,42 @@ export default class CodeMirror extends React.Component {
5757
this.editor.on('update', this.props.onUpdate);
5858
}
5959

60+
if(this.props.onKeyDown) {
61+
this.editor.on('keydown', (cm, event) => {
62+
this.props.onKeyDown(cm, event);
63+
});
64+
}
65+
66+
if(this.props.onKeyUp) {
67+
this.editor.on('keyup', (cm, event) => {
68+
this.props.onKeyUp(cm, event);
69+
});
70+
}
71+
72+
if(this.props.onKeyPress) {
73+
this.editor.on('keypress', (cm, event) => {
74+
this.props.onKeyPress(cm, event);
75+
});
76+
}
77+
78+
if(this.props.onDragEnter) {
79+
this.editor.on('dragenter', (cm, event) => {
80+
this.props.onDragEnter(cm, event);
81+
});
82+
}
83+
84+
if(this.props.onDragOver) {
85+
this.editor.on('dragover', (cm, event) => {
86+
this.props.onDragOver(cm, event);
87+
});
88+
}
89+
90+
if(this.props.onDrop) {
91+
this.editor.on('drop', (cm, event) => {
92+
this.props.onDrop(cm, event);
93+
});
94+
}
95+
6096
this.hydrate(this.props);
6197

6298
if (this.props.editorDidMount) {

0 commit comments

Comments
 (0)