File size: 7,785 Bytes
f909d7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
import { Node, NodeType } from "jsonc-parser";
import { calculateNodeSize } from "src/lib/utils/graph/calculateNodeSize";
import { Graph, States } from "src/lib/utils/json/jsonParser";
import { addEdgeToGraph } from "./addEdgeToGraph";
import { addNodeToGraph } from "./addNodeToGraph";
type PrimitiveOrNullType = "boolean" | "string" | "number" | "null";
type Traverse = {
states: States;
objectToTraverse: Node;
parentType?: string;
myParentId?: string;
nextType?: string;
};
const isPrimitiveOrNullType = (type: unknown): type is PrimitiveOrNullType => {
return ["boolean", "string", "number", "null"].includes(type as string);
};
const alignChildren = (nodeA: Node, nodeB: Node): number => {
const aChildType = nodeA?.children?.[1]?.type;
const bChildType = nodeB?.children?.[1]?.type;
if (isPrimitiveOrNullType(aChildType) && !isPrimitiveOrNullType(bChildType)) {
return -1;
}
return 0;
};
function handleNoChildren(
value: string | undefined,
states: States,
graph: Graph,
myParentId?: string,
parentType?: string,
nextType?: string
) {
if (value === undefined) return;
if (parentType === "property" && nextType !== "object" && nextType !== "array") {
states.brothersParentId = myParentId;
if (nextType === undefined && Array.isArray(states.brothersNode)) {
states.brothersNode.push([states.brotherKey, value]);
} else {
states.brotherKey = value;
}
} else if (parentType === "array") {
const nodeFromArrayId = addNodeToGraph({ graph, text: String(value) });
if (myParentId) {
addEdgeToGraph(graph, myParentId, nodeFromArrayId);
}
}
if (nextType && parentType !== "array" && (nextType === "object" || nextType === "array")) {
states.parentName = value;
}
}
function handleHasChildren(
type: NodeType,
states: States,
graph: Graph,
children: Node[],
myParentId?: string,
parentType?: string
) {
let parentId: string | undefined;
if (type !== "property" && states.parentName !== "") {
// add last brothers node and add parent node
if (states.brothersNode.length > 0) {
const findBrothersNode = states.brothersNodeProps.find(
e =>
e.parentId === states.brothersParentId &&
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]
);
if (findBrothersNode) {
const findNodeIndex = graph.nodes.findIndex(e => e.id === findBrothersNode?.id);
if (findNodeIndex !== -1) {
const modifyNodes = [...graph.nodes];
const foundNode = modifyNodes[findNodeIndex];
foundNode.text = foundNode.text.concat(states.brothersNode as any);
const { width, height } = calculateNodeSize(foundNode.text, false);
foundNode.width = width;
foundNode.height = height;
graph.nodes = modifyNodes;
states.brothersNode = [];
}
} else {
const brothersNodeId = addNodeToGraph({ graph, text: states.brothersNode });
states.brothersNode = [];
if (states.brothersParentId) {
addEdgeToGraph(graph, states.brothersParentId, brothersNodeId);
} else {
states.notHaveParent.push(brothersNodeId);
}
states.brothersNodeProps.push({
id: brothersNodeId,
parentId: states.brothersParentId,
objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],
});
}
}
// Add parent node
parentId = addNodeToGraph({ graph, type, text: states.parentName });
states.bracketOpen.push({ id: parentId, type });
states.parentName = "";
// Add edges from parent node
const brothersProps = states.brothersNodeProps.filter(
e =>
e.parentId === myParentId &&
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]
);
if (
(brothersProps.length > 0 &&
states.bracketOpen[states.bracketOpen.length - 2]?.type !== "object") ||
(brothersProps.length > 0 && states.bracketOpen.length === 1)
) {
addEdgeToGraph(graph, brothersProps[brothersProps.length - 1].id, parentId);
} else if (myParentId) {
addEdgeToGraph(graph, myParentId, parentId);
} else {
states.notHaveParent.push(parentId);
}
} else if (parentType === "array") {
states.objectsFromArray = [...states.objectsFromArray, states.objectsFromArrayId++];
}
const traverseObject = (objectToTraverse: Node, nextType: string) => {
traverse({
states,
objectToTraverse,
parentType: type,
myParentId: states.bracketOpen[states.bracketOpen.length - 1]?.id,
nextType,
});
};
const traverseArray = () => {
children.forEach((objectToTraverse, index, array) => {
const nextType = array[index + 1]?.type;
traverseObject(objectToTraverse, nextType);
});
};
if (type === "object") {
children.sort(alignChildren);
traverseArray();
} else {
traverseArray();
}
if (type !== "property") {
// Add or concatenate brothers node when it is the last parent node
if (states.brothersNode.length > 0) {
const findBrothersNode = states.brothersNodeProps.find(
e =>
e.parentId === states.brothersParentId &&
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]
);
if (findBrothersNode) {
const modifyNodes = [...graph.nodes];
const findNodeIndex = modifyNodes.findIndex(e => e.id === findBrothersNode?.id);
if (modifyNodes[findNodeIndex] && typeof states.brothersNode === "string") {
modifyNodes[findNodeIndex].text += states.brothersNode;
const { width, height } = calculateNodeSize(modifyNodes[findNodeIndex].text, false);
modifyNodes[findNodeIndex].width = width;
modifyNodes[findNodeIndex].height = height;
graph.nodes = modifyNodes;
states.brothersNode = [];
}
} else {
const brothersNodeId = addNodeToGraph({ graph, text: states.brothersNode });
states.brothersNode = [];
if (states.brothersParentId) {
addEdgeToGraph(graph, states.brothersParentId, brothersNodeId);
} else {
states.notHaveParent = [...states.notHaveParent, brothersNodeId];
}
const brothersNodeProps = {
id: brothersNodeId,
parentId: states.brothersParentId,
objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],
};
states.brothersNodeProps = [...states.brothersNodeProps, brothersNodeProps];
}
}
// Close brackets
if (parentType === "array") {
if (states.objectsFromArray.length > 0) {
states.objectsFromArray.pop();
}
} else {
if (states.bracketOpen.length > 0) {
states.bracketOpen.pop();
}
}
if (parentId) {
const myChildren = graph.edges.filter(edge => edge.from === parentId);
const parentIndex = graph.nodes.findIndex(node => node.id === parentId);
graph.nodes = graph.nodes.map((node, index) => {
if (index === parentIndex) {
const childrenCount = myChildren.length;
return { ...node, data: { ...node.data, childrenCount } };
}
return node;
});
}
}
}
export const traverse = ({
objectToTraverse,
states,
myParentId,
nextType,
parentType,
}: Traverse) => {
const graph = states.graph;
const { type, children, value } = objectToTraverse;
if (!children) {
handleNoChildren(value, states, graph, myParentId, parentType, nextType);
} else if (children) {
handleHasChildren(type, states, graph, children, myParentId, parentType);
}
};
|