Hello Everybody
In my use case, I need to have more than one editor on a page, so to map these EditorStates
objects to their dom EditorViews
I added an id
prop to each EditorState
and the same id
to its view.dom
div container.
Iād like to save these EditorStates in the Chrome localStorage, and from reading the docs, I guessed toJSON
is the right method to get a serializable object, so I did:
const initialState = EditorState.create({
doc: "editor"
});
const initialStateData = initialState.toJSON();
initialStateData.id = 1;
I actually wanted to add the id
to initialState
, because from the docs it seemed possible to add an id
custom field and then serialize it. The docs:
When custom fields should be serialized, you can pass them in as an object mapping property names to fields
I couldnāt make it work, I tried different syntax, but never got a resulting object like:
{doc: 'editor', selection: {ā¦}, id:1}
So, I got it to work by adding the id
after calling initialState.toJSON()
without passing to it an argument.
When I retrieve the serialized object from localStorage Iād like to transform it into a proper EditorState
object, I mean with all its methods, to pass it to EditorView
:
const view = new EditorView({state});
And to do it, I thought to use fromJSON
, but again, as with toJSON
, I think I donāt get the syntax right when passing arguments, and if I call it without arguments, the EditorState
I get as a result has lost its id
prop. I think I donāt understand
static fromJSON(
json: any,
configā ?: EditorStateConfig = {},
fieldsā ?: Object<StateField<any>>
) ā EditorState
as well as
toJSON(fieldsā ?: Object<StateField<any>>) ā any
Probably an example on how to use it would help. I couldnāt find any example in the āExamplesā part of the docs.
Also Iād like to know if you think I am doing things the right way, or if thereās a better way (for example, I was wondering if I should use StateFields
ā¦).
Thanks!