Skip to content

Commit

Permalink
Fix issues with collision priority
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Jul 14, 2024
1 parent 5488c2d commit a00e16b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .changeset/fix-collision-priority.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@dnd-kit/abstract': patch
'@dnd-kit/collisions': patch
---

Fix issues with `collisionPriority` not being respected.
7 changes: 7 additions & 0 deletions packages/abstract/src/core/collision/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ export enum CollisionPriority {
Highest,
}

export enum CollisionType {
Collision,
ShapeIntersection,
PointerIntersection,
}

export interface Collision {
id: UniqueIdentifier;
priority: CollisionPriority | number;
type: CollisionType;
value: number;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/abstract/src/core/collision/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {Collision} from './types.ts';
*/
export function sortCollisions(a: Collision, b: Collision) {
if (a.priority === b.priority) {
return b.value - a.value;
if (a.type === b.type) {
return b.value - a.value;
}

return b.type - a.type;
}

return b.priority - a.priority;
Expand Down
10 changes: 2 additions & 8 deletions packages/abstract/src/core/entities/droppable/droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ export class Droppable<
U extends DragDropManager<any, any> = DragDropManager<any, any>,
> extends Entity<T> {
constructor(
{
accept,
collisionDetector,
collisionPriority = CollisionPriority.Normal,
type,
...input
}: Input<T>,
{accept, collisionDetector, collisionPriority, type, ...input}: Input<T>,
manager: U | undefined
) {
super(input, manager);
Expand Down Expand Up @@ -87,7 +81,7 @@ export class Droppable<
public accessor collisionDetector: CollisionDetector;

@reactive
public accessor collisionPriority: number;
public accessor collisionPriority: CollisionPriority | number | undefined;

@reactive
public accessor shape: Shape | undefined;
Expand Down
6 changes: 5 additions & 1 deletion packages/abstract/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export type {
Renderer,
} from './manager/index.ts';

export {CollisionPriority, sortCollisions} from './collision/index.ts';
export {
CollisionPriority,
CollisionType,
sortCollisions,
} from './collision/index.ts';
export type {Collision, CollisionDetector} from './collision/index.ts';

export {Modifier} from './modifiers/index.ts';
Expand Down
13 changes: 7 additions & 6 deletions packages/collision/src/algorithms/closestCenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CollisionPriority} from '@dnd-kit/abstract';
import {CollisionPriority, CollisionType} from '@dnd-kit/abstract';
import type {CollisionDetector} from '@dnd-kit/abstract';
import {Point} from '@dnd-kit/geometry';

Expand All @@ -15,11 +15,11 @@ export const closestCenter: CollisionDetector = (input) => {
return null;
}

// const collision = defaultCollisionDetection(input);
const collision = defaultCollisionDetection(input);

// if (collision) {
// return collision;
// }
if (collision) {
return collision;
}

const distance = Point.distance(
droppable.shape.center,
Expand All @@ -31,6 +31,7 @@ export const closestCenter: CollisionDetector = (input) => {
return {
id: droppable.id,
value,
priority: CollisionPriority.Low,
type: CollisionType.Collision,
priority: CollisionPriority.Normal,
};
};
3 changes: 2 additions & 1 deletion packages/collision/src/algorithms/directionBiased.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CollisionPriority} from '@dnd-kit/abstract';
import {CollisionPriority, CollisionType} from '@dnd-kit/abstract';
import type {CollisionDetector} from '@dnd-kit/abstract';
import {Point} from '@dnd-kit/geometry';

Expand Down Expand Up @@ -37,6 +37,7 @@ export const directionBiased: CollisionDetector = ({
return {
id: droppable.id,
value: 1 / Point.distance(droppable.shape.center, center),
type: CollisionType.Collision,
priority: CollisionPriority.Normal,
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/collision/src/algorithms/pointerDistance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CollisionPriority} from '@dnd-kit/abstract';
import {CollisionPriority, CollisionType} from '@dnd-kit/abstract';
import type {CollisionDetector} from '@dnd-kit/abstract';
import {Point} from '@dnd-kit/geometry';

Expand All @@ -19,6 +19,7 @@ export const pointerDistance: CollisionDetector = (input) => {
return {
id: droppable.id,
value,
priority: CollisionPriority.Low,
type: CollisionType.Collision,
priority: CollisionPriority.Normal,
};
};
39 changes: 7 additions & 32 deletions packages/collision/src/algorithms/pointerIntersection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CollisionPriority} from '@dnd-kit/abstract';
import {CollisionPriority, CollisionType} from '@dnd-kit/abstract';
import type {CollisionDetector} from '@dnd-kit/abstract';
import {Point, Rectangle} from '@dnd-kit/geometry';
import {Point} from '@dnd-kit/geometry';

/**
* A high precision collision detection algorithm that detects
Expand All @@ -11,9 +11,10 @@ import {Point, Rectangle} from '@dnd-kit/geometry';
*
* Returns null if the pointer is outside of the droppable element.
*/
export const pointerIntersection: CollisionDetector = (
{dragOperation, droppable}
) => {
export const pointerIntersection: CollisionDetector = ({
dragOperation,
droppable,
}) => {
const pointerCoordinates = dragOperation.position.current;

if (!pointerCoordinates) {
Expand All @@ -37,36 +38,10 @@ export const pointerIntersection: CollisionDetector = (
return {
id,
value: 1 / distance,
type: CollisionType.PointerIntersection,
priority: CollisionPriority.High,
};
}

// const {shape} = dragOperation;
// const {x, y} = pointerCoordinates;
// const size = shape
// ? {
// width: Math.min(
// Math.abs(shape.current.boundingRectangle.right - x),
// 30
// ),
// height: Math.min(
// Math.abs(shape.current.boundingRectangle.bottom - y),
// 30
// ),
// }
// : {width: 30, height: 30};
// const pointerShape = new Rectangle(x, y, size.width, size.height);
// const intersectionArea = droppable.shape.intersectionArea(pointerShape);

// if (intersectionArea) {
// const distance = Point.distance(droppable.shape.center, pointerCoordinates);

// return {
// id,
// value: 1 / distance,
// priority: CollisionPriority.Medium,
// };
// }

return null;
};
3 changes: 2 additions & 1 deletion packages/collision/src/algorithms/shapeIntersection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CollisionPriority} from '@dnd-kit/abstract';
import {CollisionPriority, CollisionType} from '@dnd-kit/abstract';
import type {CollisionDetector} from '@dnd-kit/abstract';
import {Point} from '@dnd-kit/geometry';

Expand Down Expand Up @@ -32,6 +32,7 @@ export const shapeIntersection: CollisionDetector = ({
return {
id: droppable.id,
value,
type: CollisionType.ShapeIntersection,
priority: CollisionPriority.Normal,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dom/src/sortable/sortable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {batch, reactive, untracked} from '@dnd-kit/state';
import {CollisionPriority, Entity} from '@dnd-kit/abstract';
import type {CollisionPriority} from '@dnd-kit/abstract';
import type {
Data,
PluginConstructor,
Expand Down Expand Up @@ -304,7 +304,7 @@ export class Sortable<T extends Data = Data> {
}

public set collisionPriority(value: CollisionPriority | number | undefined) {
this.droppable.collisionPriority = value ?? CollisionPriority.Normal;
this.droppable.collisionPriority = value;
}

public set collisionDetector(value: CollisionDetector | undefined) {
Expand Down

0 comments on commit a00e16b

Please sign in to comment.