Last active
February 25, 2023 22:33
-
-
Save fantactuka/26dcc03c2b2a2cb43f894282c101d674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INTERNAL_PROPS = new Set([ | |
'__first', | |
'__last', | |
'__size', | |
'__parent', | |
'__next', | |
'__prev', | |
'__cachedText', | |
'__key', | |
]); | |
exportJSON() { | |
const serializedNode: {[key: string]: unknown} = {type: this.getType()}; | |
const node = this.getLatest(); | |
const isTextNode = $isTextNode(this); | |
const isElementNode = $isElementNode(this); | |
for (const key in Object.keys(node)) { | |
if ( | |
key[0] !== '_' || | |
key[1] !== '_' || | |
INTERNAL_PROPS.has(key) || | |
(isElementNode && (key === 'children' || key === '__format')) || | |
(isTextNode && key === '__mode') | |
) { | |
continue; | |
} | |
let value = node[key as keyof LexicalNode]; | |
if (value instanceof LexicalEditor) { | |
// @ts-ignore better typing? | |
value = value.toJSON(); | |
} | |
serializedNode[key.slice(2)] = value; | |
} | |
if (isElementNode) { | |
serializedNode.children = []; | |
// @ts-ignore wish type predicates worked here | |
serializedNode.format = this.getFormatType(); | |
} else if (isTextNode) { | |
// @ts-ignore wish type predicates worked here | |
serializedNode.mode = this.getMode(); | |
} | |
return serializedNode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment