-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: graph collision detection for drag/drop (#1517)
- Loading branch information
Showing
3 changed files
with
25 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { closestCenter, CollisionDetection, pointerWithin, rectIntersection } from "@dnd-kit/core" | ||
|
||
export const kGraphIdBase = "graph" | ||
|
||
const isGraphDropTarget = ({ id }: { id: string | number }) => /^graph.+-drop$/.test(`${id}`) | ||
|
||
export const graphCollisionDetection: CollisionDetection = (args) => { | ||
const graphTargets = args.droppableContainers.filter(isGraphDropTarget) | ||
|
||
// prioritize pointer within a target; need fallback for keyboard sensor | ||
const collisions = pointerWithin({ ...args, droppableContainers: graphTargets }) | ||
if (collisions.length) return collisions | ||
|
||
// determine the targets that are intersecting the drag rect | ||
const intersectingTargets = rectIntersection({ ...args, droppableContainers: graphTargets }) | ||
const intersectingTargetIds = new Set<string>(intersectingTargets.map(({ id }) => `${id}`)) | ||
|
||
// use closest center to choose among the intersecting drop targets | ||
const intersectingContainers = graphTargets.filter(({ id }) => intersectingTargetIds.has(`${id}`)) | ||
return closestCenter({ ...args, droppableContainers: intersectingContainers }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters