Skip to content

Commit 65a3d18

Browse files
committed
possible fix for #2
1 parent 74925fb commit 65a3d18

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ require('codemirror/mode/javascript/javascript');
5050
- `options` - see codemirror [configuration](https://codemirror.net/doc/manual.html#config)
5151
- `value` - set component value through props
5252
> triggers `onValueSet`
53+
- `resetCursorOnSet`
54+
> `boolean` reset the internal codemirror cursor position should a new `value` prob be set. Default: `false`
5355
5456
## component specific events
5557

docs/app.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,17 @@ var CodeMirror = function (_React$Component) {
148148

149149
if (this.props.value !== nextProps.value) {
150150
this.hydrated = false;
151+
152+
if (!this.props.resetCursorOnSet) {
153+
this.cursorPos = this.editor.getCursor();
154+
}
151155
}
152156

153157
this.hydrate(nextProps);
158+
159+
if (!this.props.resetCursorOnSet) {
160+
this.editor.setCursor(this.cursorPos);
161+
}
154162
}
155163
}, {
156164
key: 'componentWillUnmount',
@@ -174,6 +182,7 @@ var CodeMirror = function (_React$Component) {
174182
}
175183

176184
if (this.props.value && !this.hydrated) {
185+
177186
this.editor.setValue(props.value);
178187

179188
if (this.props.onValueSet) {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-codemirror2",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "a tiny react codemirror component wrapper",
55
"main": "index.js",
66
"scripts": {
@@ -54,9 +54,9 @@
5454
"react": "15.6.1",
5555
"react-dom": "15.6.1",
5656
"react-redux": "5.0.5",
57-
"redux": "3.7.1",
57+
"redux": "3.7.2",
5858
"sass-loader": "6.0.6",
5959
"style-loader": "0.18.2",
60-
"webpack": "3.1.0"
60+
"webpack": "3.2.0"
6161
}
6262
}

src/react-codemirror2.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,17 @@ export default class CodeMirror extends React.Component {
120120

121121
if (this.props.value !== nextProps.value) {
122122
this.hydrated = false;
123+
124+
if (!this.props.resetCursorOnSet) {
125+
this.cursorPos = this.editor.getCursor();
126+
}
123127
}
124128

125129
this.hydrate(nextProps);
130+
131+
if (!this.props.resetCursorOnSet) {
132+
this.editor.setCursor(this.cursorPos);
133+
}
126134
}
127135

128136
componentWillUnmount() {
@@ -141,6 +149,7 @@ export default class CodeMirror extends React.Component {
141149
}
142150

143151
if (this.props.value && !this.hydrated) {
152+
144153
this.editor.setValue(props.value);
145154

146155
if (this.props.onValueSet) {

0 commit comments

Comments
 (0)