|
| 1 | +/* istanbul ignore file */ |
| 2 | +/** |
| 3 | +This sorting strategy was copied over from https://github.com/clauderic/dnd-kit/pull/805 |
| 4 | +to resolve issues with variable sized draggables. |
| 5 | +*/ |
| 6 | +import { CollisionDetection, DroppableContainer } from '@dnd-kit/core'; |
| 7 | +import { sortBy } from 'lodash'; |
| 8 | + |
| 9 | +const collision = (dropppableContainer?: DroppableContainer) => ({ |
| 10 | + id: dropppableContainer?.id ?? '', |
| 11 | + value: dropppableContainer, |
| 12 | +}); |
| 13 | + |
| 14 | +// Look for the first (/ furthest up / highest) droppable container that is at least |
| 15 | +// 50% covered by the top edge of the dragging container. |
| 16 | +const highestDroppableContainerMajorityCovered: CollisionDetection = ({ |
| 17 | + droppableContainers, |
| 18 | + collisionRect, |
| 19 | +}) => { |
| 20 | + const ascendingDroppabaleContainers = sortBy( |
| 21 | + droppableContainers, |
| 22 | + (c) => c?.rect.current?.top, |
| 23 | + ); |
| 24 | + |
| 25 | + for (const droppableContainer of ascendingDroppabaleContainers) { |
| 26 | + const { |
| 27 | + rect: { current: droppableRect }, |
| 28 | + } = droppableContainer; |
| 29 | + |
| 30 | + if (droppableRect) { |
| 31 | + const coveredPercentage = (droppableRect.top + droppableRect.height - collisionRect.top) |
| 32 | + / droppableRect.height; |
| 33 | + |
| 34 | + if (coveredPercentage > 0.5) { |
| 35 | + return [collision(droppableContainer)]; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // if we haven't found anything then we are off the top, so return the first item |
| 41 | + return [collision(ascendingDroppabaleContainers[0])]; |
| 42 | +}; |
| 43 | + |
| 44 | +// Look for the last (/ furthest down / lowest) droppable container that is at least |
| 45 | +// 50% covered by the bottom edge of the dragging container. |
| 46 | +const lowestDroppableContainerMajorityCovered: CollisionDetection = ({ |
| 47 | + droppableContainers, |
| 48 | + collisionRect, |
| 49 | +}) => { |
| 50 | + const descendingDroppabaleContainers = sortBy( |
| 51 | + droppableContainers, |
| 52 | + (c) => c?.rect.current?.top, |
| 53 | + ).reverse(); |
| 54 | + |
| 55 | + for (const droppableContainer of descendingDroppabaleContainers) { |
| 56 | + const { |
| 57 | + rect: { current: droppableRect }, |
| 58 | + } = droppableContainer; |
| 59 | + |
| 60 | + if (droppableRect) { |
| 61 | + const coveredPercentage = (collisionRect.bottom - droppableRect.top) / droppableRect.height; |
| 62 | + |
| 63 | + if (coveredPercentage > 0.5) { |
| 64 | + return [collision(droppableContainer)]; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // if we haven't found anything then we are off the bottom, so return the last item |
| 70 | + return [collision(descendingDroppabaleContainers[0])]; |
| 71 | +}; |
| 72 | + |
| 73 | +export const verticalSortableListCollisionDetection: CollisionDetection = ( |
| 74 | + args, |
| 75 | +) => { |
| 76 | + if (args.collisionRect.top < (args.active.rect.current?.initial?.top ?? 0)) { |
| 77 | + return highestDroppableContainerMajorityCovered(args); |
| 78 | + } |
| 79 | + return lowestDroppableContainerMajorityCovered(args); |
| 80 | +}; |
0 commit comments