Skip to content

Commit 8c3f54e

Browse files
committed
feat(core): add onError hook
Signed-off-by: braks <[email protected]>
1 parent 87249e5 commit 8c3f54e

File tree

22 files changed

+250
-180
lines changed

22 files changed

+250
-180
lines changed

packages/core/src/auto-imports.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ declare global {
1414
const EdgeId: typeof import('./context/index')['EdgeId']
1515
const EdgeRef: typeof import('./context/index')['EdgeRef']
1616
const EffectScope: typeof import('vue')['EffectScope']
17+
const ErrorCode: typeof import('./utils/errors')['ErrorCode']
1718
const NodeId: typeof import('./context/index')['NodeId']
1819
const NodeRef: typeof import('./context/index')['NodeRef']
1920
const Slots: typeof import('./context/index')['Slots']
2021
const Storage: typeof import('./composables/useVueFlow')['Storage']
2122
const VueFlow: typeof import('./context/index')['VueFlow']
22-
const VueFlowError: typeof import('./utils/log')['VueFlowError']
23+
const VueFlowError: typeof import('./utils/errors')['VueFlowError']
2324
const addEdge: typeof import('./utils/graph')['addEdge']
2425
const addEdgeToStore: typeof import('./utils/store')['addEdgeToStore']
2526
const applyChanges: typeof import('./utils/changes')['applyChanges']
@@ -44,6 +45,7 @@ declare global {
4445
const createAdditionChange: typeof import('./utils/changes')['createAdditionChange']
4546
const createApp: typeof import('vue')['createApp']
4647
const createEventHook: typeof import('@vueuse/core')['createEventHook']
48+
const createExtendedEventHook: typeof import('./utils/createExtendedEventHook')['createExtendedEventHook']
4749
const createGlobalState: typeof import('@vueuse/core')['createGlobalState']
4850
const createGraphNodes: typeof import('./utils/store')['createGraphNodes']
4951
const createHooks: typeof import('./store/hooks')['createHooks']

packages/core/src/components/Nodes/NodeWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ const NodeWrapper = defineComponent({
235235
nextPos.y = snapGrid[1] * Math.round(nextPos.y / snapGrid[1])
236236
}
237237

238-
const { computedPosition, position } = calcNextPosition(node, nextPos, nodeExtent, parentNode)
238+
const { computedPosition, position } = calcNextPosition(node, nextPos, emits.error, nodeExtent, parentNode)
239239

240240
// only overwrite positions if there are changes when clamping
241241
if (node.computedPosition.x !== computedPosition.x || node.computedPosition.y !== computedPosition.y) {

packages/core/src/composables/useDrag.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function useDrag(params: UseDragParams) {
4040
removeSelectedElements,
4141
addSelectedNodes,
4242
updateNodePositions,
43+
emits,
4344
} = $(useVueFlow())
4445

4546
const { onStart, onDrag, onStop, el, disabled, id, selectable } = $(params)
@@ -83,6 +84,7 @@ function useDrag(params: UseDragParams) {
8384
const { computedPosition } = calcNextPosition(
8485
n,
8586
nextPosition,
87+
emits.error,
8688
nodeExtent,
8789
n.parentNode ? findNode(n.parentNode) : undefined,
8890
)

packages/core/src/composables/useEdge.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CustomEvent, ElementData } from '~/types'
2+
import { VueFlowError } from '~/utils/errors'
23

34
/**
45
* Access an edge
@@ -11,12 +12,12 @@ export default function useEdge<Data = ElementData, CustomEvents extends Record<
1112
const edgeId = id ?? inject(EdgeId, '')
1213
const edgeEl = inject(EdgeRef, null)
1314

14-
const { findEdge } = useVueFlow()
15+
const { findEdge, emits } = useVueFlow()
1516

1617
const edge = findEdge<Data, CustomEvents>(edgeId)
1718

1819
if (!edge) {
19-
throw new VueFlowError(`Edge with id ${edgeId} not found!`, 'useEdge')
20+
emits.error(new VueFlowError(ErrorCode.EDGE_NOT_FOUND, edgeId))
2021
}
2122

2223
return {

packages/core/src/composables/useEdgeHooks.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { EdgeEventsEmit, EdgeEventsOn, GraphEdge, VueFlowStore } from '~/types'
22

33
const createEdgeHooks = () => ({
4-
doubleClick: createEventHook(),
5-
click: createEventHook(),
6-
mouseEnter: createEventHook(),
7-
mouseMove: createEventHook(),
8-
mouseLeave: createEventHook(),
9-
contextMenu: createEventHook(),
10-
updateStart: createEventHook(),
11-
update: createEventHook(),
12-
updateEnd: createEventHook(),
4+
doubleClick: createExtendedEventHook(),
5+
click: createExtendedEventHook(),
6+
mouseEnter: createExtendedEventHook(),
7+
mouseMove: createExtendedEventHook(),
8+
mouseLeave: createExtendedEventHook(),
9+
contextMenu: createExtendedEventHook(),
10+
updateStart: createExtendedEventHook(),
11+
update: createExtendedEventHook(),
12+
updateEnd: createExtendedEventHook(),
1313
})
1414

1515
export default function useEdgeHooks(edge: GraphEdge, emits: VueFlowStore['emits']): { emit: EdgeEventsEmit; on: EdgeEventsOn } {

packages/core/src/composables/useNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ export default function useNode<Data = ElementData, CustomEvents extends Record<
1111
const nodeId = id ?? inject(NodeId, '')
1212
const nodeEl = inject(NodeRef, null)
1313

14-
const { findNode, getEdges } = useVueFlow()
14+
const { findNode, getEdges, emits } = useVueFlow()
1515

1616
const node = findNode<Data, CustomEvents>(nodeId)
1717

1818
if (!node) {
19-
throw new VueFlowError(`Node with id ${nodeId} not found!`, 'useNode')
19+
emits.error(new VueFlowError(ErrorCode.NODE_NOT_FOUND, nodeId))
2020
}
2121

2222
return {
2323
id: nodeId,
2424
nodeEl,
2525
node,
26-
parentNode: computed(() => (node.parentNode ? findNode(node.parentNode) : undefined)),
27-
connectedEdges: computed(() => getConnectedEdges([node], getEdges.value)),
26+
parentNode: computed(() => (node!.parentNode ? findNode(node!.parentNode) : undefined)),
27+
connectedEdges: computed(() => getConnectedEdges([node!], getEdges.value)),
2828
}
2929
}

packages/core/src/composables/useNodeHooks.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { GraphNode, NodeEventsEmit, NodeEventsOn, VueFlowStore } from '~/types'
22

33
const createNodeHooks = () => ({
4-
doubleClick: createEventHook(),
5-
click: createEventHook(),
6-
mouseEnter: createEventHook(),
7-
mouseMove: createEventHook(),
8-
mouseLeave: createEventHook(),
9-
contextMenu: createEventHook(),
10-
dragStart: createEventHook(),
11-
drag: createEventHook(),
12-
dragStop: createEventHook(),
4+
doubleClick: createExtendedEventHook(),
5+
click: createExtendedEventHook(),
6+
mouseEnter: createExtendedEventHook(),
7+
mouseMove: createExtendedEventHook(),
8+
mouseLeave: createExtendedEventHook(),
9+
contextMenu: createExtendedEventHook(),
10+
dragStart: createExtendedEventHook(),
11+
drag: createExtendedEventHook(),
12+
dragStop: createExtendedEventHook(),
1313
})
1414

1515
export default function useNodeHooks(node: GraphNode, emits: VueFlowStore['emits']): { emit: NodeEventsEmit; on: NodeEventsOn } {

packages/core/src/composables/useUpdateNodePositions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { NodeDragItem, XYPosition } from '~/types'
22

33
function useUpdateNodePositions() {
4-
const { getSelectedNodes, nodeExtent, updateNodePositions, findNode, snapGrid, snapToGrid, nodesDraggable } = useVueFlow()
4+
const { getSelectedNodes, nodeExtent, updateNodePositions, findNode, snapGrid, snapToGrid, nodesDraggable, emits } =
5+
useVueFlow()
56

67
return (positionDiff: XYPosition, isShiftPressed = false) => {
78
// by default a node moves 5px on each key press, or 20px if shift is pressed
@@ -21,6 +22,7 @@ function useUpdateNodePositions() {
2122
const { computedPosition } = calcNextPosition(
2223
n,
2324
nextPosition,
25+
emits.error,
2426
nodeExtent.value,
2527
n.parentNode ? findNode(n.parentNode) : undefined,
2628
)

packages/core/src/composables/useVueFlow.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ export default (options?: FlowProps): VueFlowStore => {
123123
if (options) vueFlow.setState(options)
124124
}
125125

126-
/**
127-
* Vue flow wasn't able to find any store instance - we can't proceed
128-
*/
129-
if (!vueFlow) throw new VueFlowError('Store instance not found.', 'useVueFlow')
130-
131126
// always provide a fresh instance into context on call
132127
if (scope) {
133128
provide(VueFlow, vueFlow)

packages/core/src/container/EdgeRenderer/EdgeRenderer.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
getEdgeTypes,
2020
elevateEdgesOnSelect,
2121
dimensions,
22+
emits,
2223
} = $(useVueFlow())
2324
2425
const sourceNode = controlledComputed(
@@ -75,7 +76,7 @@ function getType(type?: string, template?: GraphEdge['template']) {
7576
7677
const slot = slots?.[`edge-${name}`]
7778
if (!slot) {
78-
warn(`Edge type "${type}" not found and no edge-slot detected. Using fallback type "default".`)
79+
emits.error(new VueFlowError(ErrorCode.EDGE_TYPE_MISSING, edgeType))
7980
return false
8081
}
8182

0 commit comments

Comments
 (0)