react-codemirror
react-codemirror copied to clipboard
differentiate onChange vs. manual input?
Hi there! I would just like to start by saying thanks! React-Codemirror has been awesome. Sometimes I wish instead of bug reports there was a thing on github for "appreciation reports".
Anyway, a thing I just ran into is that it seems the onChange handler gets called both when a user is editing a codemirror and also when changing the value from React (which I believe uses cm.setValue()). I was wondering if it's worth differentiating those two cases in this library. Maybe it isn't, but it might at least be specified in the docs. Either way, I'm going to figure out a way around it for my use case so that I can tell when a user is doing something in the text field vs when my stuff is re-rendering a value to the codemirror instance.
After some digging, I made a monkeypatch in my version that surfaces CodeMirror's onChange handler's "origin" property:
codemirrorValueChanged: function codemirrorValueChanged(doc, change) {
var newValue = doc.getValue();
if (this._currentCodemirrorValue !== newValue) {
this._currentCodemirrorValue = newValue;
this.props.onChange && this.props.onChange(newValue, change.origin);
}
},
Here is a description of the Codemirror change object for reference. I don't know if it's actually a good idea to put this object in React-CodeMirror's onChange handler, but it was useful in my project.
See the discussion at #5
I created a pull request to help fix this: #35
Possibly closed by #35.
If this issue still persists please give react-codemirror2 a look. I'm planning on maintaining this moving forward. Better docs will be coming. Feel free to open up any issues/suggestions as I'll be trying to gather as much constructive feedback from the community moving forward