Skip to content

Instantly share code, notes, and snippets.

@fantactuka
Last active February 25, 2023 22:33
Show Gist options
  • Save fantactuka/26dcc03c2b2a2cb43f894282c101d674 to your computer and use it in GitHub Desktop.
Save fantactuka/26dcc03c2b2a2cb43f894282c101d674 to your computer and use it in GitHub Desktop.
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