Skip to content

Commit dec10e9

Browse files
committed
chore(core): cleanup
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
1 parent ee74bee commit dec10e9

File tree

6 files changed

+32
-48
lines changed

6 files changed

+32
-48
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const isConnectable = computed(() => {
8989
}
9090
9191
if (typeof connectable === 'function') {
92-
return connectable(node, connectedEdges.value)
92+
return connectable(nodeId, handleId)
9393
}
9494
9595
return isDef(connectable) ? connectable : nodesConnectable.value
@@ -98,15 +98,17 @@ const isConnectable = computed(() => {
9898
// todo: remove this and have users handle this themselves using `updateNodeInternals`
9999
// 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)
100100
onMounted(() => {
101+
const nodeHandleBounds = node.internals.handleBounds?.[type.value] ?? []
102+
101103
// if the node isn't initialized yet, we can't set up the handle bounds
102104
// the handle bounds will be automatically set up when the node is initialized (`updateNodeDimensions`)
103-
if (!node.dimensions.width || !node.dimensions.height) {
105+
if (!vueFlowRef.value || !node.measured.width || !node.measured.height) {
104106
return
105107
}
106108
107-
const existingBounds = node.handleBounds[type.value]?.find((b) => b.id === handleId)
109+
const existingBounds = nodeHandleBounds.find((b) => b.id === handleId)
108110
109-
if (!vueFlowRef.value || existingBounds) {
111+
if (existingBounds) {
110112
return
111113
}
112114
@@ -131,14 +133,24 @@ onMounted(() => {
131133
...getDimensions(handle.value),
132134
}
133135
134-
node.handleBounds[type.value] = [...(node.handleBounds[type.value] ?? []), nextBounds]
136+
node.internals.handleBounds = {
137+
source: [],
138+
target: [],
139+
...node.internals.handleBounds,
140+
[type.value]: [...nodeHandleBounds, nextBounds],
141+
}
135142
})
136143
137144
onUnmounted(() => {
138145
// clean up node internals
139-
const handleBounds = node.handleBounds[type.value]
140-
if (handleBounds) {
141-
node.handleBounds[type.value] = handleBounds.filter((b) => b.id !== handleId)
146+
const nodeHandleBounds = node.internals.handleBounds?.[type.value]
147+
if (nodeHandleBounds) {
148+
node.internals.handleBounds = {
149+
source: [],
150+
target: [],
151+
...node.internals.handleBounds,
152+
[type.value]: nodeHandleBounds.filter((b) => b.id !== handleId),
153+
}
142154
}
143155
})
144156

packages/core/src/composables/useNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, inject, ref } from 'vue'
2-
import type { CustomEvent, ElementData } from '../types'
2+
import type { NodeBase } from '@xyflow/system'
33
import { ErrorCode, VueFlowError, getConnectedEdges } from '../utils'
44
import { NodeRef } from '../context'
55
import { useVueFlow } from './useVueFlow'
@@ -16,13 +16,13 @@ import { useNodeId } from './useNodeId'
1616
* @param id - The id of the node to access
1717
* @returns the node id, the node, the node dom element, it's parent and connected edges
1818
*/
19-
export function useNode<Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(id?: string) {
19+
export function useNode<NodeType extends NodeBase = NodeBase>(id?: string) {
2020
const nodeId = id ?? useNodeId() ?? ''
2121
const nodeEl = inject(NodeRef, ref(null))
2222

2323
const { findNode, edges, emits } = useVueFlow()
2424

25-
const node = findNode<Data, CustomEvents>(nodeId)!
25+
const node = findNode<NodeType>(nodeId)!
2626

2727
if (!node) {
2828
emits.error(new VueFlowError(ErrorCode.NODE_NOT_FOUND, nodeId))
@@ -32,7 +32,7 @@ export function useNode<Data = ElementData, CustomEvents extends Record<string,
3232
id: nodeId,
3333
nodeEl,
3434
node,
35-
parentNode: computed(() => findNode(node.parentNode)),
35+
parentNode: computed(() => findNode(node.parentId)),
3636
connectedEdges: computed(() => getConnectedEdges([node], edges.value)),
3737
}
3838
}

packages/core/src/types/flow.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import type { VueFlowStore } from './store'
2222

2323
export type MaybeElement = Node | Edge | Connection | Element
2424

25+
export type ElementData = Record<string, unknown>
26+
2527
export interface CustomThemeVars {
2628
[key: string]: string | number | undefined
2729
}

packages/core/src/types/handle.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { Dimensions, Position, XYPosition } from './flow'
22
import type { Connection } from './connection'
3-
import type { GraphEdge } from './edge'
4-
import type { GraphNode } from './node'
53

64
export type HandleType = 'source' | 'target'
75

@@ -31,12 +29,9 @@ export interface ConnectingHandle {
3129
}
3230

3331
/** A valid connection function can determine if an attempted connection is valid or not, i.e. abort creating a new edge */
34-
export type ValidConnectionFunc = (
35-
connection: Connection,
36-
elements: { edges: GraphEdge[]; nodes: GraphNode[]; sourceNode: GraphNode; targetNode: GraphNode },
37-
) => boolean
32+
export type ValidConnectionFunc = (connection: Connection) => boolean
3833

39-
export type HandleConnectableFunc = (node: GraphNode, connectedEdges: GraphEdge[]) => boolean
34+
export type HandleConnectableFunc = (nodeId: string, handleId: string | null) => boolean
4035

4136
/**
4237
* set to true to allow unlimited connections,

packages/core/src/types/node.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { NodeBase, NodeProps as NodePropsBase } from '@xyflow/system'
2-
import type { Styles } from './flow'
2+
import type { ElementData, Styles } from './flow'
33
import type { HandleConnectable, HandleElement } from './handle'
44

5-
export type ElementData = Record<string, unknown>
6-
75
/** Defined as [[x-from, y-from], [x-to, y-to]] */
86
export type CoordinateExtent = [extentFrom: [fromX: number, fromY: number], extentTo: [toX: number, toY: number]]
97

packages/core/src/types/store.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { CSSProperties, ComputedRef, ToRefs } from 'vue'
22
import type { KeyFilter } from '@vueuse/core'
3-
import type { PanZoomInstance, Viewport } from '@xyflow/system'
3+
import type { InternalNodeBase, NodeBase, PanZoomInstance, Viewport } from '@xyflow/system'
44
import type { ViewportHelper } from '../composables'
55
import type {
66
Dimensions,
77
ElementData,
8-
Elements,
9-
FlowElements,
108
FlowExportObject,
119
FlowOptions,
1210
FlowProps,
@@ -161,8 +159,6 @@ export interface State extends Omit<FlowProps, 'id' | 'modelValue'> {
161159
ariaLiveMessage: string
162160
}
163161

164-
export type SetElements = (elements: Elements | ((elements: FlowElements) => Elements)) => void
165-
166162
export type SetNodes = (nodes: Node[] | ((nodes: GraphNode[]) => Node[])) => void
167163

168164
export type SetEdges = (edges: Edge[] | ((edges: GraphEdge[]) => Edge[])) => void
@@ -206,9 +202,9 @@ export type UpdateNodeDimensions = (updates: UpdateNodeDimensionsParams[]) => vo
206202

207203
export type UpdateNodeInternals = (nodeIds?: string[]) => void
208204

209-
export type FindNode = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
205+
export type FindNode = <NodeType extends NodeBase = NodeBase>(
210206
id: string | undefined | null,
211-
) => GraphNode<Data, CustomEvents> | undefined
207+
) => InternalNodeBase<NodeType> | undefined
212208

213209
export type FindEdge = <Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any>(
214210
id: string | undefined | null,
@@ -235,8 +231,6 @@ export type UpdateNodeData = <Data = ElementData, CustomEvents extends Record<st
235231
export type IsNodeIntersecting = (node: (Partial<Node> & { id: Node['id'] }) | Rect, area: Rect, partially?: boolean) => boolean
236232

237233
export interface Actions extends Omit<ViewportHelper, 'viewportInitialized'> {
238-
/** parses elements (nodes + edges) and re-sets the state */
239-
setElements: SetElements
240234
/** parses nodes and re-sets the state */
241235
setNodes: SetNodes
242236
/** parses edges and re-sets the state */
@@ -265,11 +259,6 @@ export interface Actions extends Omit<ViewportHelper, 'viewportInitialized'> {
265259
applyEdgeChanges: (changes: EdgeChange[]) => GraphEdge[]
266260
/** applies default node change handler */
267261
applyNodeChanges: (changes: NodeChange[]) => GraphNode[]
268-
/**
269-
* manually select elements and add to state
270-
* @deprecated will be removed in the next major, use {@link Actions.addSelectedNodes} or {@link Actions.addSelectedEdges} instead
271-
*/
272-
addSelectedElements: (elements: FlowElements) => void
273262
/** manually select edges and add to state */
274263
addSelectedEdges: (edges: GraphEdge[]) => void
275264
/** manually select nodes and add to state */
@@ -278,11 +267,6 @@ export interface Actions extends Omit<ViewportHelper, 'viewportInitialized'> {
278267
removeSelectedEdges: (edges: GraphEdge[]) => void
279268
/** manually unselect nodes and remove from state */
280269
removeSelectedNodes: (nodes: GraphNode[]) => void
281-
/**
282-
* @deprecated will be replaced in the next major
283-
* unselect selected elements (if none are passed, all elements are unselected)
284-
*/
285-
removeSelectedElements: (elements?: Elements) => void
286270
/** apply min zoom value to panzoom */
287271
setMinZoom: (zoom: number) => void
288272
/** apply max zoom value to panzoom */
@@ -341,11 +325,6 @@ export interface Getters {
341325
getEdgeTypes: Record<keyof DefaultEdgeTypes | string, EdgeComponent>
342326
/** returns object containing current node types */
343327
getNodeTypes: Record<keyof DefaultNodeTypes | string, NodeComponent>
344-
/**
345-
* get all elements
346-
* @deprecated - will be removed in next major version
347-
*/
348-
getElements: FlowElements
349328
/** all visible node */
350329
getNodes: GraphNode[]
351330
/** all visible edges */
@@ -360,8 +339,6 @@ export interface Getters {
360339
* @deprecated use {@link Actions.findEdge} instead
361340
*/
362341
getEdge: (id: string) => GraphEdge | undefined
363-
/** returns all currently selected elements */
364-
getSelectedElements: FlowElements
365342
/** returns all currently selected nodes */
366343
getSelectedNodes: GraphNode[]
367344
/** returns all currently selected edges */

0 commit comments

Comments
 (0)