Skip to content

Commit

Permalink
refactor(core): remove node intersections arg from drag events (#1460)
Browse files Browse the repository at this point in the history
* refactor(core): remove node intersections arg from drag events

* chore(changeset): add

* docs(examples): update intersection example
  • Loading branch information
bcakmakoglu committed Jun 10, 2024
1 parent ba6e250 commit c94e175
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-cherries-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---

Remove node intersections from drag event args
40 changes: 34 additions & 6 deletions docs/examples/intersection/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup>
import { ref } from 'vue'
import { VueFlow, useVueFlow } from '@vue-flow/core'
import { computed, ref } from 'vue'
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
/**
* You can either use `getIntersectingNodes` to check if a given node intersects with others
* or `isNodeIntersecting` to check if a node is intersecting with a given area
*/
const { onNodeDrag, getIntersectingNodes, isNodeIntersecting: __, updateNode } = useVueFlow()
const { onNodeDrag, getIntersectingNodes, isNodeIntersecting, updateNode, screenToFlowCoordinate } = useVueFlow()
const nodes = ref([
{
Expand Down Expand Up @@ -37,8 +37,34 @@ const nodes = ref([
},
])
onNodeDrag(({ node }) => {
const intersectionIds = getIntersectingNodes(node).map((intersection) => intersection.id)
const panelEl = ref()
const isIntersectingWithPanel = ref(false)
const panelPosition = computed(() => {
if (!panelEl.value) {
return {
x: 0,
y: 0,
width: 0,
height: 0,
}
}
const { left, top, width, height } = panelEl.value.$el.getBoundingClientRect()
return {
...screenToFlowCoordinate({ x: left, y: top }),
width,
height,
}
})
onNodeDrag(({ node: draggedNode }) => {
const intersections = getIntersectingNodes(draggedNode)
const intersectionIds = intersections.map((intersection) => intersection.id)
isIntersectingWithPanel.value = isNodeIntersecting(draggedNode, panelPosition.value)
for (const node of nodes.value) {
const isIntersecting = intersectionIds.includes(node.id)
Expand All @@ -49,5 +75,7 @@ onNodeDrag(({ node }) => {
</script>

<template>
<VueFlow :nodes="nodes" fit-view-on-init />
<VueFlow :nodes="nodes" fit-view-on-init>
<Panel ref="panelEl" position="bottom-right" :class="{ intersecting: isIntersectingWithPanel }"> </Panel>
</VueFlow>
</template>
18 changes: 17 additions & 1 deletion docs/examples/intersection/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
.vue-flow__node.intersecting {
background-color: yellow;
background-color: #f15a16;
}

.vue-flow__panel {
height: 250px;
width: 250px;
border: 1px dashed #ccc;
pointer-events: none !important;
border-radius: 4px;
display: flex;
justify-content: center;
align-items: center;
}

.vue-flow__panel.intersecting {
border-color: #f15a16;
background-color: rgba(241, 90, 22, 0.03);
}
2 changes: 1 addition & 1 deletion docs/src/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
{ text: 'Screenshot', link: '/examples/screenshot' },
{ text: 'Confirm Delete', link: '/examples/confirm' },
{ text: 'Hidden', link: '/examples/hidden' },
{ text: 'Node Intersections', link: '/examples/intersection' },
{ text: 'Multiple Flows', link: '/examples/multi' },
{ text: 'Pinia Store', link: '/examples/pinia' },
{ text: 'Viewport Transition', link: '/examples/transition' },
Expand All @@ -224,6 +223,7 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
items: [
{ text: 'Custom Node', link: '/examples/nodes/' },
{ text: 'Update Node', link: '/examples/nodes/update-node' },
{ text: 'Intersections', link: '/examples/nodes/intersection' },
{ text: 'Nested Nodes', link: '/examples/nodes/nesting' },
{ text: 'Node Resizer', link: '/examples/nodes/node-resizer' },
{ text: 'Node Toolbar', link: '/examples/nodes/node-toolbar' },
Expand Down
7 changes: 0 additions & 7 deletions docs/src/examples/intersection.md

This file was deleted.

9 changes: 9 additions & 0 deletions docs/src/examples/nodes/intersection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Node Intersections

Sometimes you need to know if two nodes intersect. `useVueFlow` provides you methods like `getIntersectingNodes`
or `isNodeIntersection` to help you find out when two or more nodes intersect,
or if a node intersects with a given area.

<div class="mt-6">
<Repl example="intersection"></Repl>
</div>
10 changes: 4 additions & 6 deletions packages/core/src/components/Nodes/NodeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const NodeWrapper = defineComponent({
addSelectedNodes,
updateNodeDimensions,
onUpdateNodeInternals,
getIntersectingNodes,
getNodeTypes,
nodeExtent,
elevateNodesOnSelect,
Expand Down Expand Up @@ -120,17 +119,16 @@ const NodeWrapper = defineComponent({
id: props.id,
el: nodeElement,
disabled: () => !isDraggable.value,
selectable: () => isSelectable.value,
selectable: isSelectable,
dragHandle: () => node.dragHandle,
onStart(args) {
// todo: remove intersections from here - they are not needed and only reduce performance
emit.dragStart({ ...args, intersections: getIntersectingNodes(node) })
emit.dragStart(args)
},
onDrag(args) {
emit.drag({ ...args, intersections: getIntersectingNodes(node) })
emit.drag(args)
},
onStop(args) {
emit.dragStop({ ...args, intersections: getIntersectingNodes(node) })
emit.dragStop(args)
},
})

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface NodeDragEvent {
event: MouseTouchEvent
node: GraphNode
nodes: GraphNode[]
intersections?: GraphNode[]
}

export interface EdgeMouseEvent {
Expand Down

0 comments on commit c94e175

Please sign in to comment.