save the folding state and reapply after reloading

Hi, what is the best way to save the folding state to disk and reapply it in case we have to reload the page.

Thank for great work.

@codemirror/language 6.1.0 makes this easier by exporting foldState, which you can use with EditorState.toJSON/fromJSON to serialize the fold state along with the rest of your state.

1 Like

Thanks. This is great! what about in version earlier such as 0.19.2. I have some dependencies which limits us from moving to the new version right a way.

There this doesn’t exist, so you’d have to used foldedRanges (if that is available there yet) to set up a solution if your custom code.

Is there an example of getting foldState. How exactly should I use EditorState.toJSON to jsontify foldState.

let stateJSON = state.toJSON({fold: foldState})
let deserializedState = EditorState.fromJSON(stateJSON, {extensions}, {fold: foldState})

This is great. Btw, is there a way to know when a folding event happen? Basically, I would like to save the foldState if there is any thing change. what is the best practice to implement this?

A transaction will have this effect in it when it folds, something, and this one when it unfolds something.