Created
July 26, 2024 19:23
-
-
Save JosephScript/3abac1facaa71ae27b90597a34b4e31f to your computer and use it in GitHub Desktop.
CraftJS generateEditorJson with only JSON
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
import { ROOT_NODE, UserComponent } from '@craftjs/core' | |
import { getRandomId } from '@craftjs/utils' | |
// We can infer the NodeConfig type from the UserComponent's props | |
type NodeConfig<T extends UserComponent> = { | |
node: T | |
props: React.ComponentProps<T> | |
} | |
export const generateEditorJson = <T extends UserComponent>( | |
nodes: NodeConfig<T>[], | |
) => { | |
// Initial nodes, directly add to root | |
const initialNodes: Record<string, any> = { | |
[ROOT_NODE]: { | |
type: { resolvedName: 'Container' }, // swap Container with whatever you like as your root node | |
displayName: 'Container', // this also assumes you have a component registered named "Container" | |
isCanvas: true, | |
hidden: false, | |
custom: {}, | |
props: {}, | |
nodes: [], | |
linkedNodes: {}, | |
}, | |
} | |
nodes.forEach((nodeConfig) => { | |
const nodeId = getRandomId() | |
const { node, props } = nodeConfig | |
const resolvedName = node.name | |
const defaultProps = node.craft?.props | |
initialNodes[ROOT_NODE].nodes.push(nodeId) | |
initialNodes[nodeId] = { | |
type: { resolvedName }, | |
displayName: node.craft?.displayName || resolvedName, // Fallback to resolvedName if displayName is not provided | |
parent: ROOT_NODE, | |
isCanvas: false, | |
hidden: false, | |
custom: {}, | |
props: { ...defaultProps, ...props }, // Merge default and provided props | |
nodes: [], // Add child nodes if necessary, | |
linkedNodes: {}, | |
} | |
}) | |
return JSON.stringify(initialNodes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: