Load document from JSON

Thanks to the info in this thread, I could create a plugin to save / retrieve doc from localStorage:

const saveRetrieveDocPlugin = new Plugin({
  state: {
    init(config, state) {
      const strContent = localStorage.getItem('spellcheck-content');
      if (strContent) {
        state.doc = state.schema.nodeFromJSON(JSON.parse(strContent));
      }
    },
    apply(tr) {
      if (tr.docChanged) {
        localStorage.setItem('spellcheck-content', JSON.stringify(tr.doc.toJSON()));
      }
    },
  },
});
5 Likes