How to set new doc content

How are you supposed to change the doc-content? Probably a simple question but I didn´t quiet get, how you work using view.dispatch or view.setState…
I tried this

const state = EditorState.create({
        ...editorRef.state,
        doc: 'my new editorcontent'
      });
      editorRef.setState(state);

which ended up in changing nothing except that it even deleted all my extensions…
I also tried using

view.dispatch({
   newDoc: toText("my new content")
})

But that also didn´t work out (couldn´t even access the toText function because I couldn´t find out what it is derived from).
So what did I do wrong here?

Thanks in advance!

You can create a change spec that covers the whole existing document:

view.dispatch({changes: {
  from: 0,
  to: view.state.doc.length,
  insert: 'my new content'
}})

See also CodeMirror 6: Setting the contents of the editor

Or you can call view.setState(...) to complete replace the current state:

view.setState(EditorState.create({doc: 'my new content'}))

(but you also need to pass any extensions you are using)

1 Like