react-codemirror icon indicating copy to clipboard operation
react-codemirror copied to clipboard

differentiate onChange vs. manual input?

Open Glench opened this issue 9 years ago • 4 comments

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.

Glench avatar Feb 11 '16 00:02 Glench

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.

Glench avatar Feb 16 '16 23:02 Glench

See the discussion at #5

I created a pull request to help fix this: #35

alexdmiller avatar Mar 14 '16 19:03 alexdmiller

Possibly closed by #35.

rgbkrk avatar Apr 23 '16 04:04 rgbkrk

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

scniro avatar Jun 02 '17 11:06 scniro