Retrieve scroll position when appending/removing view dom element.


I am building a project where several CodeMirror instances can be opened at the same time in different tab (CM instances are stored globally). To display the selected editor, I append/remove the coresponding view.dom to a “content” div. When I switch to another tab and come back, the editor scroll position is lost and set to {top:0, left:0}.

Any idea how I could retrieve the correct scroll position when switch back to an editor?
Should I store the scroll position on my own and apply it to the editor when displaying it.


The reliable way to do this is to figure out which character is in the top-left corner of the editor, and which pixel offset it has from the corner (using coordsAtPos/posAtCoords), and scroll back to it by dispatching the scrollIntoView effect with the appropriate configuration.

Here is a simplified example of what I’m trying to do.

Press button to display one editor or the other. You can see that if you scroll into an editor, then display the other one and then the first one again, the scroll position of the first editor has been reset.

In this case, scroll top position could be between two line image
Do you think using coordsAtPos/posAtCoords could work in that case?