Replacing a state's doc

How can I ‘load’ a new doc (ie a remote ‘checkpoint’/revision’) into an existing view/state ?

I want to replace the entire doc with a new one. I basically want to reinitialize the view with an identical state/config that just has it’s doc modified to a different one. How can this be achieved ?

I can’t create a new state because I can’t get the config of the old one. I can serialize/replace/deserialize but that seems awkward. Same for select all and replace.


Why can’t you get the config of the old one? If you have your EditorView, you can do the following:

    let newState = EditorState.create({schema: view.state.schema, doc, plugins: view.state.plugins});

Where I use DOMParser to parse some html into a doc node.

thanks ! the problem with this is that the new state’s plugins are not initialized, that is their init method is not called which I think cause all kinds of problems (for example they may still hold a reference to the old state)

I think maybe the solution is to call reconfigure using view.state.plugins but I’m not sure that’s correct.

Plugin instances don’t ever hold any references to a specific state (unless you’re doing something weird) – they are just descriptions of how the plugin behaves. If they keep state, they do it by declaring state fields, which hang off the EditorState object, not the Plugin.

I am thinking for example about collab. When resetting the doc I want collab to reset as well, that is for sendableSteps to return null even if there was something in it before. What is the best way to reset the doc in that situation ?

Exactly, which is why you’d create an entirely new state value.

@marijn @wes-r and others :slight_smile:

What if I need to change collab plugin version property when initializing a new doc? I need to filter out collab plugin from view.state.plugins and create it again with needed version property? something like

const plugins = view.state.plugins.filter(p => p.key !== 'collab');
plugins.push(collab({ version: 20 }));
let newState = EditorState.create({schema: view.state.schema, doc, plugins});


Right. Or even better, create a function that produces the array of plugins for your state, and parameterize it with the things you might want to change, so that you can call the same function when creating the original state and when creating a new one.


If I need to filter out collab plugin from plugins array, what is the correct way to do it?

key is not a property of Plugin, so I can’t really use it.

And is it ok to completely update document structure from inside the plugin?

Recreate the array of plugins, don’t filter it (unless you preserved the actual object collab() returned, in which case I guess you could look for that).

I do not know what you mean by that.