refactor(core): avoid inserting invalid nodes
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
export enum ErrorCode {
|
export enum ErrorCode {
|
||||||
MISSING_VIEWPORT_DIMENSIONS = 'MISSING_VIEWPORT_DIMENSIONS',
|
MISSING_VIEWPORT_DIMENSIONS = 'MISSING_VIEWPORT_DIMENSIONS',
|
||||||
|
NODE_INVALID = 'NODE_INVALID',
|
||||||
NODE_NOT_FOUND = 'NODE_NOT_FOUND',
|
NODE_NOT_FOUND = 'NODE_NOT_FOUND',
|
||||||
NODE_MISSING_PARENT = 'NODE_MISSING_PARENT',
|
NODE_MISSING_PARENT = 'NODE_MISSING_PARENT',
|
||||||
NODE_TYPE_MISSING = 'NODE_TYPE_MISSING',
|
NODE_TYPE_MISSING = 'NODE_TYPE_MISSING',
|
||||||
@@ -16,6 +17,7 @@ export enum ErrorCode {
|
|||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
[ErrorCode.MISSING_VIEWPORT_DIMENSIONS]: () => 'The Vue Flow parent container needs a width and a height to render the graph',
|
[ErrorCode.MISSING_VIEWPORT_DIMENSIONS]: () => 'The Vue Flow parent container needs a width and a height to render the graph',
|
||||||
|
[ErrorCode.NODE_INVALID]: (id?: string) => `Node is invalid\nNode: ${id}`,
|
||||||
[ErrorCode.NODE_NOT_FOUND]: (id: string) => `Node not found\nNode: ${id}`,
|
[ErrorCode.NODE_NOT_FOUND]: (id: string) => `Node not found\nNode: ${id}`,
|
||||||
[ErrorCode.NODE_MISSING_PARENT]: (id: string, parentId: string) => `Node is missing a parent\nNode: ${id}\nParent: ${parentId}`,
|
[ErrorCode.NODE_MISSING_PARENT]: (id: string, parentId: string) => `Node is missing a parent\nNode: ${id}\nParent: ${parentId}`,
|
||||||
[ErrorCode.NODE_TYPE_MISSING]: (type: string) => `Node type is missing\nType: ${type}`,
|
[ErrorCode.NODE_TYPE_MISSING]: (type: string) => `Node type is missing\nType: ${type}`,
|
||||||
|
|||||||
@@ -79,7 +79,13 @@ export function createGraphNodes(
|
|||||||
) {
|
) {
|
||||||
const parentNodes: Record<string, true> = {}
|
const parentNodes: Record<string, true> = {}
|
||||||
|
|
||||||
const graphNodes = nodes.map((node) => {
|
const graphNodes = nodes.reduce((nextNodes, node) => {
|
||||||
|
// make sure we don't try to add invalid nodes
|
||||||
|
if (!node || typeof node !== 'object' || !isNode(node)) {
|
||||||
|
onError(new VueFlowError(ErrorCode.NODE_INVALID))
|
||||||
|
return nextNodes
|
||||||
|
}
|
||||||
|
|
||||||
const parsed = parseNode(node, {
|
const parsed = parseNode(node, {
|
||||||
...findNode(node.id),
|
...findNode(node.id),
|
||||||
parentNode: node.parentNode,
|
parentNode: node.parentNode,
|
||||||
@@ -89,12 +95,12 @@ export function createGraphNodes(
|
|||||||
parentNodes[node.parentNode] = true
|
parentNodes[node.parentNode] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return parsed
|
return nextNodes.concat(parsed)
|
||||||
})
|
}, [] as GraphNode[])
|
||||||
|
|
||||||
const nextNodes = [...graphNodes, ...currGraphNodes]
|
const nextNodes = [...graphNodes, ...currGraphNodes]
|
||||||
|
|
||||||
graphNodes.forEach((node) => {
|
for (const node of graphNodes) {
|
||||||
const parentNode = nextNodes.find((n) => n.id === node.parentNode)
|
const parentNode = nextNodes.find((n) => n.id === node.parentNode)
|
||||||
|
|
||||||
if (node.parentNode && !parentNode) {
|
if (node.parentNode && !parentNode) {
|
||||||
@@ -110,7 +116,7 @@ export function createGraphNodes(
|
|||||||
parentNode.isParent = true
|
parentNode.isParent = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
return graphNodes
|
return graphNodes
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user