How to shrink the width of the EditorView to match its contents

Anyone have any suggestions for how reduce the width of the EditorView if the length of the longest line gets smaller? I can make the width increase when the line gets longer because the editorView.contentDOM.offsetWidth will increase in that case and I can use that to change the width of the editorView’s parent. Unfortunately, editorView.contentDOM.offsetWidth doesn’t seem to decrease in the case that the line(s) get shorter. Is some other way that I could get the width of the longest line? Or is there some other way to approach the problem?

Thanks in advance.


This isn’t something the editor provides, so you’d have to use something like coordsAtPos to compute it yourself, possibly optimizing this by tracking transactions and only recomputing it when a change looks like it might affect the max width.

@marijn, Thanks. Are you suggesting something like iterating through all the lines of the editor, getting the max of their end positions, converting that to coordinates, and then subtracting the X coordinate of that from the X coordinate of the start position?


Yes, something like that.