Skip to content

How to map the editor state to a different shape? #1710

Answered by zurfyx
tom-sherman asked this question in Q&A
Discussion options

You must be logged in to vote

Oh, I presume you're referring to server-side content. At Meta, where we store old Draft.js based documents in a particular JSON-like format, we just run a $dfs to perform the conversion

const nodes = $dfs();
const res = [];
for (const node of nodes) {
  if ($isParagraphNode(node)) {
    res.push({ type: 'paragraph', text: node.getTextContent());
  }
}

The example above is oversimplified, it will get hairer than that, that's why @trueadm recommended JSON.stringify(editor.getEditorState()) when possible. Also, you don't necessarily have to use $dfs, you can do your own tree traversal starting from $getRoot().

For real-time side-to-side rendering see #1825

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@tom-sherman
Comment options

@zurfyx
Comment options

zurfyx Apr 18, 2022
Collaborator

@tom-sherman
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by tom-sherman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants