json / src /lib /utils /core /addNodeToGraph.ts
xinnni's picture
Upload 146 files
f909d7c verified
raw
history blame contribute delete
751 Bytes
import { NodeType } from "jsonc-parser";
import { calculateNodeSize } from "src/lib/utils/graph/calculateNodeSize";
import { Graph } from "src/lib/utils/json/jsonParser";
type Props = {
graph: Graph;
text: string | [string, string][];
isEmpty?: boolean;
type?: NodeType;
};
export const addNodeToGraph = ({ graph, text, type = "null", isEmpty = false }: Props) => {
const id = String(graph.nodes.length + 1);
const isParent = type === "array" || type === "object";
const { width, height } = calculateNodeSize(text, isParent);
const node = {
id,
text,
width,
height,
data: {
type,
isParent,
isEmpty,
childrenCount: isParent ? 1 : 0,
},
};
graph.nodes.push(node);
return id;
};