Starting at codemirror 5.25.0, calling cm.setValue() updates codemirror and additionally causes the codemirror instance to scroll into view. This is a behavior change from codemirror 5.24 and earlier, where calling cm.setValue() would update a codemirror instance that is off screen without causing any scrolling. This causes a problem for my project which has a number of codemirror instances on the page, as it causes unexpected scrolling behaviors when off-screen instances are updated. It would be good to be able to update the contents of a codemirror instance without forcing it to scroll into view.
I have seen this with Chrome Version 56.0.2924.87 (64-bit) on MacOS Sierra 10.12.3 (16D32).
Here is a demo of the previous behavior with 5.24.0 (note that pressing the button updates codemirror and does not cause scroll): https://jsfiddle.net/tjkwuuwk/1/
Here is a demo of the new behavior with 5.25.0 (note the pressing the button updates codemirror and causes it to scroll into view): https://jsfiddle.net/Lj6k396f/
I'm pretty sure that the new behavior was introduced by this commit: https://github.com/codemirror/CodeMirror/commit/c88b02c5badc415abb25ef83ed28e22a8f94f940
Specifically in operations.js:
- let coords = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from),
- clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin)
- if (op.scrollToPos.isCursor && cm.state.focused) maybeScrollWindow(cm, coords)
+ let rect = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from),
+ clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin)
+ maybeScrollWindow(cm, rect)
The op.scrollToPos.isCursor && cm.state.focused check has been removed, which I think is the relevant change.
I'm happy to submit a PR to restore that check, if that is the correct fix. If so, I would appreciate advice on a unit test for this, as scrolling behavior in maybeScrollWindow() seems to be turned off for PhantomJS automated testing.
I'm facing the same issue. Searching for a way to disable scrolling on setValue.
I noticed that if I click off the CodeMirror instance first, then call setValue, the scrolling behavior is not quite the same. One alternative that might work (in my case at least) is to programmatically simulate the blur (un-focus) of the editor.
Attached patch should help
Thanks @marijnh !
Thanks @marijnh. I've created a fiddle with the fix: https://jsfiddle.net/xgh9dwoo/1/, and can confirm that the expected behavior now occurs.
Most helpful comment
Attached patch should help