How to get the full size of the EditorView

As I understand it, the general way that one uses CodeMirror is that the EditorView sizes itself according to its parent element. Suppose, however, that I want the reverse. I would like to size its parent element to the EditorView such that I can always display its entire contents without scrolling or cropping. How could I do that? Further note that in my case, I can’t directly change any CSS styling on the parent element. I need to get a width and height that I can use to set the parent element’s size via an API that it supports.

I should note that I have some code which seems to work, but I am worried that it might be depending on implementation details that are not guaranteed to remain true.



The best way to do this would just be to have the parent not have its own height, so that it uses the height of its content. Failing that, if you’ve styled the editor to have a fixed height and the styling is stable, view.dom.getBoundingClientRect() can give you the size of the editor.

Thanks, @marijn! So, view.dom will contain the entire contents, not just the part exposed in the viewport? Also, what’s the difference between view.dom and view.contentDOM?

Just pinging to see if anyone knows the answer to my last couple of questions:

  1. Will view.dom contain the entire contents, and not just the part exposed in the viewport?
  2. What’s the difference between view.dom and view.contentDOM ?