Skip to content

Commit

Permalink
fix: graph collision detection for drag/drop (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson authored Sep 25, 2024
1 parent 383e227 commit 9e2e8b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions v3/src/components/graph/components/graph-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import {useGraphController} from "../hooks/use-graph-controller"
import {GraphLayoutContext} from '../hooks/use-graph-layout-context'
import {useInitGraphLayout} from '../hooks/use-init-graph-layout'
import {InstanceIdContext, useNextInstanceId} from "../../../hooks/use-instance-id-context"
import { registerTileCollisionDetection } from "../../../lib/dnd-kit/dnd-detect-collision"
import {AxisProviderContext} from '../../axis/hooks/use-axis-provider-context'
import {AxisLayoutContext} from "../../axis/models/axis-layout-context"
import {usePixiPointsArray} from '../../data-display/hooks/use-pixi-points-array'
import {GraphController} from "../models/graph-controller"
import {isGraphContentModel} from "../models/graph-content-model"
import {Graph} from "./graph"
import { graphCollisionDetection, kGraphIdBase } from './graph-drag-drop'
import { kTitleBarHeight } from "../../constants"
import {AttributeDragOverlay} from "../../drag-drop/attribute-drag-overlay"
import "../register-adornment-types"

registerTileCollisionDetection(kGraphIdBase, graphCollisionDetection)

export const GraphComponent = observer(function GraphComponent({tile}: ITileBaseProps) {
const graphModel = isGraphContentModel(tile?.content) ? tile?.content : undefined

Expand Down
21 changes: 21 additions & 0 deletions v3/src/components/graph/components/graph-drag-drop.ts
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 })
}
6 changes: 0 additions & 6 deletions v3/src/lib/dnd-kit/dnd-detect-collision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,5 @@ export const dndDetectCollision: CollisionDetection = (args) => {
}
}

// if we're in the graph component, use rectangle intersection on the drop targets (e.g. axes, plot)
if (collisions.find(collision => /graph.+component-drop-overlay/.test(`${collision.id}`))) {
const containers = args.droppableContainers.filter(({id}) => /graph.+-drop/.test(`${id}`))
return rectIntersection({ ...args, droppableContainers: containers })
}

return collisions
}

0 comments on commit 9e2e8b9

Please sign in to comment.