Cursor out of Range when updating doc with a shorter version and cursor is at the end


Hi –

I’m new to prosemirror, thanks for the awesome editor framework.

I’m using prosemirror in an angular2 environment. In my case, the document can change from outside the editor. I’m pushing (a markdown doc) successfully into the editor:

this.props.state.doc = defaultMarkdownParser.parse(;

The problem arises, when the cursor is at the end of a document, and I push a shorter document into the editor. I’ll get a “Invalid Position XX” exception.

I’m a little bit lost how I could resolve the nearest valid position compared to the old (invalid) position - or at least reset the cursor to the end of the document.

Any help would be greatly appreciated!

— Hofer


There’s your issue. The state is supposed to be an immutable data structure and assigning to its properties is going to break things. Create a new state when you want to load a new document.


Dear marijn

Thanks for the quick reply. I adjusted the code, and indeed it runs stable.

The thing is, now the editor is completely reloaded on doc change. Before, the “cursor” stayed at the same position, just the contend changed. It felt slicker, but I’m totally aware of my problematic usage of this.props.state.doc. Is there a simple solution to keep the cursor at the same position after updating the content?

Best Hofer


The error likely came from exactly that place—the cursor was in a position that didn’t exist in the new document anymore. You can use Selection.findNear to try and find a selection in the new document near its old position, but in general if you load a new document, the selection position in the old document isn’t guaranteed to have anything to do with the corresponding position in the new document (for example if content was added/deleted above it).