Skip to content

Commit 0b2659c

Browse files
committed
chore(core): cleanup
Signed-off-by: braks <[email protected]>
1 parent f85947a commit 0b2659c

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

packages/core/src/components/Handle/Handle.vue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const { id: nodeId, node, nodeEl, connectedEdges } = useNode()
3333
3434
const handle = ref<HTMLDivElement>()
3535
36-
const handleId = computed(() => id ?? `${nodeId}__handle-${position}`)
36+
const handleId = computed(() => id ?? `${nodeId.value}__handle-${position}`)
3737
3838
const isConnectableStart = computed(() => (typeof connectableStart !== 'undefined' ? connectableStart : true))
3939
@@ -51,7 +51,7 @@ const isConnectable = computed(() => {
5151
return !connectedEdges.value.some((edge) => {
5252
const id = edge[`${type.value}Handle`]
5353
54-
if (edge[type.value] !== nodeId) {
54+
if (edge[type.value] !== nodeId.value) {
5555
return false
5656
}
5757
@@ -60,42 +60,42 @@ const isConnectable = computed(() => {
6060
}
6161
6262
if (isFunction(connectable)) {
63-
return connectable(node, connectedEdges.value)
63+
return connectable(node.value, connectedEdges.value)
6464
}
6565
6666
return isDef(connectable) ? connectable : nodesConnectable.value
6767
})
6868
6969
const isConnecting = computed(
7070
() =>
71-
(connectionStartHandle.value?.nodeId === nodeId &&
71+
(connectionStartHandle.value?.nodeId === nodeId.value &&
7272
connectionStartHandle.value?.handleId === handleId.value &&
7373
connectionStartHandle.value?.type === type.value) ||
74-
(connectionEndHandle.value?.nodeId === nodeId &&
74+
(connectionEndHandle.value?.nodeId === nodeId.value &&
7575
connectionEndHandle.value?.handleId === handleId.value &&
7676
connectionEndHandle.value?.type === type.value),
7777
)
7878
7979
const isClickConnecting = computed(
8080
() =>
81-
connectionClickStartHandle.value?.nodeId === nodeId &&
81+
connectionClickStartHandle.value?.nodeId === nodeId.value &&
8282
connectionClickStartHandle.value?.handleId === handleId.value &&
8383
connectionClickStartHandle.value?.type === type.value,
8484
)
8585
8686
// set up handle bounds if they don't exist yet and the node has been initialized (i.e. the handle was added after the node has already been mounted)
87-
until(() => node?.initialized)
87+
until(() => node.value.initialized)
8888
.toBe(true, { flush: 'post' })
8989
.then(() => {
90-
const existingBounds = node.handleBounds[type.value]?.find((b) => b.id === handleId.value)
90+
const existingBounds = node.value.handleBounds[type.value]?.find((b) => b.id === handleId.value)
9191
9292
if (!vueFlowRef.value || existingBounds) {
9393
return
9494
}
9595
9696
const viewportNode = vueFlowRef.value.querySelector('.vue-flow__transformationpane')
9797
98-
if (!nodeEl || !handle.value || !viewportNode || !handleId.value) {
98+
if (!nodeEl.value || !handle.value || !viewportNode || !handleId.value) {
9999
return
100100
}
101101
@@ -114,7 +114,7 @@ until(() => node?.initialized)
114114
...getDimensions(handle.value),
115115
}
116116
117-
node.handleBounds[type.value] = [...(node.handleBounds[type.value] ?? []), nextBounds]
117+
node.value.handleBounds[type.value] = [...(node.value.handleBounds[type.value] ?? []), nextBounds]
118118
})
119119
120120
function onPointerDown(event: MouseEvent | TouchEvent) {

packages/core/src/composables/useEdge.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { MaybeRef } from '@vueuse/core'
1+
import type { MaybeRefOrGetter } from '@vueuse/core'
22
import { computed, inject } from 'vue'
3+
import { toValue } from '@vueuse/core'
34
import { useVueFlow } from './useVueFlow'
45
import type { GraphEdge } from '~/types'
56
import { ErrorCode, VueFlowError } from '~/utils'
@@ -13,34 +14,26 @@ import { EdgeId, EdgeRef } from '~/context'
1314
*
1415
* @param id The id of the edge to get
1516
*/
16-
export function useEdge<T extends GraphEdge = GraphEdge>(id?: MaybeRef<string>) {
17+
export function useEdge<T extends GraphEdge = GraphEdge>(id?: MaybeRefOrGetter<string>) {
1718
const { findEdge, emits } = useVueFlow()
1819

1920
const edgeRef = inject(EdgeRef, null)
2021

2122
const edgeIdInjection = inject(EdgeId, '')
2223

2324
const edgeId = computed(() => {
24-
const nextId = unref(id) ?? edgeIdInjection
25+
const nextId = toValue(id) ?? edgeIdInjection
2526

26-
if (!nextId || nextId === '') {
27-
throw new VueFlowError(`No edge id provided and no injection could be found!`, 'useEdge')
27+
if (!nextId) {
28+
emits.error(new VueFlowError(ErrorCode.EDGE_NOT_FOUND, nextId))
2829
}
2930

3031
return nextId
3132
})
3233

33-
const edgeEl = computed(() => unref(edgeRef) ?? document.querySelector(`[data-id="${edgeId.value}"]`))
34+
const edgeEl = computed(() => toValue(edgeRef) ?? document.querySelector(`[data-id="${edgeId.value}"]`))
3435

35-
const edge = computed(() => {
36-
const nextEdge = findEdge<T>(edgeId.value)
37-
38-
if (!nextEdge) {
39-
throw new VueFlowError(`Edge with id ${edgeId.value} not found!`, 'useEdge')
40-
}
41-
42-
return nextEdge
43-
})
36+
const edge = computed(() => findEdge<T>(edgeId.value)!)
4437

4538
return {
4639
id: edgeId,

packages/core/src/composables/useNode.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { computed, inject } from 'vue'
2+
import type { MaybeRefOrGetter } from '@vueuse/core'
3+
import { toValue } from '@vueuse/core'
24
import { useVueFlow } from './useVueFlow'
3-
import type { MaybeRef } from '@vueuse/core'
45
import type { GraphNode } from '~/types'
56
import { NodeId, NodeRef } from '~/context'
67
import { ErrorCode, VueFlowError, getConnectedEdges } from '~/utils'
@@ -11,34 +12,26 @@ import { ErrorCode, VueFlowError, getConnectedEdges } from '~/utils'
1112
* If no node id is provided, the node id is injected from context,
1213
* meaning if you do not provide an id, this composable has to be called in a child of your custom node component, or it will throw!
1314
*/
14-
export function useNode<T extends GraphNode = GraphNode>(id?: MaybeRef<string>) {
15+
export function useNode<T extends GraphNode = GraphNode>(id?: MaybeRefOrGetter<string>) {
1516
const { findNode, edges, emits } = useVueFlow()
1617

1718
const nodeIdInjection = inject(NodeId, '')
1819

1920
const nodeId = computed(() => {
20-
const nextId = unref(id) ?? nodeIdInjection
21+
const nextId = toValue(id) ?? nodeIdInjection
2122

22-
if (!nextId || nextId === '') {
23-
throw new VueFlowError(`No node id provided and no injection could be found!`, 'useNode')
23+
if (!nextId) {
24+
emits.error(new VueFlowError(ErrorCode.NODE_NOT_FOUND, nextId))
2425
}
2526

2627
return nextId
2728
})
2829

2930
const nodeRef = inject(NodeRef, null)
3031

31-
const nodeEl = computed(() => unref(nodeRef) ?? document.querySelector(`[data-id="${nodeId.value}"]`))
32+
const nodeEl = computed(() => toValue(nodeRef) ?? document.querySelector(`[data-id="${nodeId.value}"]`))
3233

33-
const node = computed(() => {
34-
const nextNode = findNode<T>(nodeId.value)
35-
36-
if (!nextNode) {
37-
throw new VueFlowError(`Node with id ${nodeId.value} not found!`, 'useNode')
38-
}
39-
40-
return nextNode
41-
})
34+
const node = computed(() => findNode<T>(nodeId.value)!)
4235

4336
const parentNode = computed(() => findNode(node.value.parentNode))
4437

0 commit comments

Comments
 (0)