prosekit / prosekit

Framework agnostic and headless rich text editor

Home Page:https://prosekit.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing the editor state

ManUtopiK opened this issue · comments

Hi!

In this example, we can load a new content in the editor.
Unfortunately, it doesn't preserve history (load a saved content, and do ctrl+z : nothing happens).
This is because it creates a new editor instance when loading saved content.

To preserve history, we can set a transaction that replace the root doc :

const doc = MyNewDoc

// Create a transaction to set the new document content
const tr = editor.state.tr.replaceWith(0, editor.state.doc.content.size, editor.schema.nodeFromJSON(doc))

// Apply the transaction to the current state
const newState = editor.state.apply(tr) as EditorState

// Update the editor view with the new state
editor.view.updateState(newState)

I use nodeFromJSON in this example, but we can use jsonFromHTML to do this.

So, I didn't find a function to do that simply in the prosekit code.
Maybe an helper to do that will be useful ?