Skip to content

Commit 0825101

Browse files
committed
refactor(core): remove node intersections arg from drag events (#1460)
* refactor(core): remove node intersections arg from drag events * chore(changeset): add * docs(examples): update intersection example
1 parent a9ccd61 commit 0825101

File tree

8 files changed

+70
-22
lines changed

8 files changed

+70
-22
lines changed

.changeset/fifty-cherries-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Remove node intersections from drag event args

docs/examples/intersection/App.vue

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script setup>
2-
import { ref } from 'vue'
3-
import { VueFlow, useVueFlow } from '@vue-flow/core'
2+
import { computed, ref } from 'vue'
3+
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
44
55
/**
66
* You can either use `getIntersectingNodes` to check if a given node intersects with others
77
* or `isNodeIntersecting` to check if a node is intersecting with a given area
88
*/
9-
const { onNodeDrag, getIntersectingNodes, isNodeIntersecting: __, updateNode } = useVueFlow()
9+
const { onNodeDrag, getIntersectingNodes, isNodeIntersecting, updateNode, screenToFlowCoordinate } = useVueFlow()
1010
1111
const nodes = ref([
1212
{
@@ -37,8 +37,34 @@ const nodes = ref([
3737
},
3838
])
3939
40-
onNodeDrag(({ node }) => {
41-
const intersectionIds = getIntersectingNodes(node).map((intersection) => intersection.id)
40+
const panelEl = ref()
41+
42+
const isIntersectingWithPanel = ref(false)
43+
44+
const panelPosition = computed(() => {
45+
if (!panelEl.value) {
46+
return {
47+
x: 0,
48+
y: 0,
49+
width: 0,
50+
height: 0,
51+
}
52+
}
53+
54+
const { left, top, width, height } = panelEl.value.$el.getBoundingClientRect()
55+
56+
return {
57+
...screenToFlowCoordinate({ x: left, y: top }),
58+
width,
59+
height,
60+
}
61+
})
62+
63+
onNodeDrag(({ node: draggedNode }) => {
64+
const intersections = getIntersectingNodes(draggedNode)
65+
const intersectionIds = intersections.map((intersection) => intersection.id)
66+
67+
isIntersectingWithPanel.value = isNodeIntersecting(draggedNode, panelPosition.value)
4268
4369
for (const node of nodes.value) {
4470
const isIntersecting = intersectionIds.includes(node.id)
@@ -49,5 +75,7 @@ onNodeDrag(({ node }) => {
4975
</script>
5076

5177
<template>
52-
<VueFlow :nodes="nodes" fit-view-on-init />
78+
<VueFlow :nodes="nodes" fit-view-on-init>
79+
<Panel ref="panelEl" position="bottom-right" :class="{ intersecting: isIntersectingWithPanel }"> </Panel>
80+
</VueFlow>
5381
</template>

docs/examples/intersection/style.css

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
.vue-flow__node.intersecting {
2-
background-color: yellow;
2+
background-color: #f15a16;
3+
}
4+
5+
.vue-flow__panel {
6+
height: 250px;
7+
width: 250px;
8+
border: 1px dashed #ccc;
9+
pointer-events: none !important;
10+
border-radius: 4px;
11+
display: flex;
12+
justify-content: center;
13+
align-items: center;
14+
}
15+
16+
.vue-flow__panel.intersecting {
17+
border-color: #f15a16;
18+
background-color: rgba(241, 90, 22, 0.03);
319
}

docs/src/.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
210210
{ text: 'Screenshot', link: '/examples/screenshot' },
211211
{ text: 'Confirm Delete', link: '/examples/confirm' },
212212
{ text: 'Hidden', link: '/examples/hidden' },
213-
{ text: 'Node Intersections', link: '/examples/intersection' },
214213
{ text: 'Multiple Flows', link: '/examples/multi' },
215214
{ text: 'Pinia Store', link: '/examples/pinia' },
216215
{ text: 'Viewport Transition', link: '/examples/transition' },
@@ -224,6 +223,7 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
224223
items: [
225224
{ text: 'Custom Node', link: '/examples/nodes/' },
226225
{ text: 'Update Node', link: '/examples/nodes/update-node' },
226+
{ text: 'Intersections', link: '/examples/nodes/intersection' },
227227
{ text: 'Nested Nodes', link: '/examples/nodes/nesting' },
228228
{ text: 'Node Resizer', link: '/examples/nodes/node-resizer' },
229229
{ text: 'Node Toolbar', link: '/examples/nodes/node-toolbar' },

docs/src/examples/intersection.md

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Node Intersections
2+
3+
Sometimes you need to know if two nodes intersect. `useVueFlow` provides you methods like `getIntersectingNodes`
4+
or `isNodeIntersection` to help you find out when two or more nodes intersect,
5+
or if a node intersects with a given area.
6+
7+
<div class="mt-6">
8+
<Repl example="intersection"></Repl>
9+
</div>

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ const NodeWrapper = defineComponent({
5050
addSelectedNodes,
5151
updateNodeDimensions,
5252
onUpdateNodeInternals,
53-
getIntersectingNodes,
5453
getNodeTypes,
5554
nodeExtent,
5655
elevateNodesOnSelect,
@@ -120,17 +119,16 @@ const NodeWrapper = defineComponent({
120119
id: props.id,
121120
el: nodeElement,
122121
disabled: () => !isDraggable.value,
123-
selectable: () => isSelectable.value,
122+
selectable: isSelectable,
124123
dragHandle: () => node.dragHandle,
125124
onStart(args) {
126-
// todo: remove intersections from here - they are not needed and only reduce performance
127-
emit.dragStart({ ...args, intersections: getIntersectingNodes(node) })
125+
emit.dragStart(args)
128126
},
129127
onDrag(args) {
130-
emit.drag({ ...args, intersections: getIntersectingNodes(node) })
128+
emit.drag(args)
131129
},
132130
onStop(args) {
133-
emit.dragStop({ ...args, intersections: getIntersectingNodes(node) })
131+
emit.dragStop(args)
134132
},
135133
})
136134

packages/core/src/types/hooks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export interface NodeDragEvent {
2121
event: MouseTouchEvent
2222
node: GraphNode
2323
nodes: GraphNode[]
24-
intersections?: GraphNode[]
2524
}
2625

2726
export interface EdgeMouseEvent {

0 commit comments

Comments
 (0)