getScrollInfo() producing inconsistent results?

I’m trying to save and restore the exact scroll position when the editor changes focus, but any method I’ve tried has inconsistent results.

For example, if I start from the top of my document and scroll all the way to the bottom, getScrollInfo()['top'] returns 5264.

If I switch to an editor running in a different element, and then switch back to the old one, it’s in the exact same position, but I get a value of 3784. Trying to restore the value I saved when the editor lost focus results in scrolling to a completely different place in the document.

Scrolling all the way up and back down produces 5264 again.

Clearly something is being left out of the calculation, but what? I’ve tried slathering refresh() both before and after saving and restoring the value, but nothing seems to help. I’ve also tried different methods of both getting and setting the scroll height.

This could be caused by the virtual viewport feature, and the way heights for lines outside of the viewport (which aren’t rendered to the DOM) are estimated, rather than precise pixel values.

To restore a scroll position, it’s usually better to use a ‘reference’ line near the top of the visible content, compute that line’s offset from the top of the scroll box, and then scroll back to that offset from that line’s vertical position, rather than to a specific offset.