feat(core): add onError hook
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import type { Actions, Connection, Edge, GraphEdge, GraphNode, Node } from '~/types'
|
||||
import type { Actions, Connection, Edge, GraphEdge, GraphNode, Node, State } from '~/types'
|
||||
|
||||
export const isDef = <T>(val: T): val is NonNullable<T> => typeof unref(val) !== 'undefined'
|
||||
|
||||
export function addEdgeToStore(edgeParams: Edge | Connection, edges: Edge[]) {
|
||||
export function addEdgeToStore(edgeParams: Edge | Connection, edges: Edge[], onError: State['hooks']['error']['trigger']) {
|
||||
if (!edgeParams.source || !edgeParams.target) {
|
||||
warn("Can't create edge. An edge needs a source and a target.")
|
||||
onError(new VueFlowError(ErrorCode.EDGE_INVALID, (edgeParams as Edge).id))
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -31,16 +31,17 @@ export function updateEdgeAction(
|
||||
edges: GraphEdge[],
|
||||
findEdge: Actions['findEdge'],
|
||||
shouldReplaceId: boolean,
|
||||
onError: State['hooks']['error']['trigger'],
|
||||
) {
|
||||
if (!newConnection.source || !newConnection.target) {
|
||||
warn("Can't create new edge. An edge needs a source and a target.")
|
||||
onError(new VueFlowError(ErrorCode.EDGE_INVALID, edge.id))
|
||||
return false
|
||||
}
|
||||
|
||||
const foundEdge = findEdge(edge.id)
|
||||
|
||||
if (!foundEdge) {
|
||||
warn(`The old edge with id=${edge.id} does not exist.`)
|
||||
onError(new VueFlowError(ErrorCode.EDGE_NOT_FOUND, edge.id))
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -60,7 +61,12 @@ export function updateEdgeAction(
|
||||
return newEdge
|
||||
}
|
||||
|
||||
export function createGraphNodes(nodes: Node[], findNode: Actions['findNode'], currGraphNodes: GraphNode[]) {
|
||||
export function createGraphNodes(
|
||||
nodes: Node[],
|
||||
currGraphNodes: GraphNode[],
|
||||
findNode: Actions['findNode'],
|
||||
onError: State['hooks']['error']['trigger'],
|
||||
) {
|
||||
const parentNodes: Record<string, true> = {}
|
||||
|
||||
const graphNodes = nodes.map((node) => {
|
||||
@@ -82,7 +88,7 @@ export function createGraphNodes(nodes: Node[], findNode: Actions['findNode'], c
|
||||
const parentNode = nextNodes.find((n) => n.id === node.parentNode)
|
||||
|
||||
if (node.parentNode && !parentNode) {
|
||||
warn(`Parent node ${node.parentNode} not found`)
|
||||
onError(new VueFlowError(ErrorCode.NODE_MISSING_PARENT, node.id, node.parentNode))
|
||||
}
|
||||
|
||||
if (node.parentNode || parentNodes[node.id]) {
|
||||
|
||||
Reference in New Issue
Block a user