Export node as JSON
Summary: There has been multiple requests to incorporate an export to plain text functionality for a while. This diff adds it. It will export a node and optionally its chidren as JSON. Reviewed By: antonk52 Differential Revision: D49596476 fbshipit-source-id: 3681bc0c2d02e1ea64ff589e0e272f6d54ad0524
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5accf039c9
commit
c1b0d9d753
@@ -8,6 +8,8 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
ClientNode,
|
||||
Id,
|
||||
Inspectable,
|
||||
InspectableObject,
|
||||
Metadata,
|
||||
@@ -73,3 +75,34 @@ export function transform(
|
||||
});
|
||||
return object;
|
||||
}
|
||||
|
||||
export function exportNode(
|
||||
node: ClientNode,
|
||||
metadata: Map<MetadataId, Metadata>,
|
||||
nodes: Map<Id, ClientNode>,
|
||||
recursive: boolean = false,
|
||||
): any {
|
||||
const rawExport: any = (
|
||||
node: ClientNode,
|
||||
metadata: Map<MetadataId, Metadata>,
|
||||
nodes: Map<Id, ClientNode>,
|
||||
recursive: boolean = false,
|
||||
) => {
|
||||
return {
|
||||
...node,
|
||||
attributes: transform(node.attributes, metadata),
|
||||
children: recursive
|
||||
? node.children.map((child) => {
|
||||
const childNode = nodes.get(child);
|
||||
if (childNode == null) {
|
||||
throw new Error(`Node ${child} not found`);
|
||||
}
|
||||
|
||||
return rawExport(childNode, metadata, nodes, recursive);
|
||||
})
|
||||
: [],
|
||||
};
|
||||
};
|
||||
|
||||
return JSON.stringify(rawExport(node, metadata, nodes, recursive), null, 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user