@lucasols/dnd-kit-sortable 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/index.d.ts +137 -9
  2. package/dist/index.js +520 -7
  3. package/dist/index.js.map +1 -0
  4. package/package.json +21 -17
  5. package/dist/components/SortableContext.d.ts +0 -26
  6. package/dist/components/index.d.ts +0 -2
  7. package/dist/dnd-kit-sortable.cjs.development.js +0 -801
  8. package/dist/dnd-kit-sortable.cjs.development.js.map +0 -1
  9. package/dist/dnd-kit-sortable.cjs.production.min.js +0 -2
  10. package/dist/dnd-kit-sortable.cjs.production.min.js.map +0 -1
  11. package/dist/dnd-kit-sortable.esm.js +0 -783
  12. package/dist/dnd-kit-sortable.esm.js.map +0 -1
  13. package/dist/hooks/defaults.d.ts +0 -9
  14. package/dist/hooks/index.d.ts +0 -4
  15. package/dist/hooks/types.d.ts +0 -24
  16. package/dist/hooks/useSortable.d.ts +0 -36
  17. package/dist/hooks/utilities/index.d.ts +0 -1
  18. package/dist/hooks/utilities/useDerivedTransform.d.ts +0 -11
  19. package/dist/sensors/index.d.ts +0 -1
  20. package/dist/sensors/keyboard/index.d.ts +0 -1
  21. package/dist/sensors/keyboard/sortableKeyboardCoordinates.d.ts +0 -2
  22. package/dist/strategies/horizontalListSorting.d.ts +0 -2
  23. package/dist/strategies/index.d.ts +0 -4
  24. package/dist/strategies/rectSorting.d.ts +0 -2
  25. package/dist/strategies/rectSwapping.d.ts +0 -2
  26. package/dist/strategies/verticalListSorting.d.ts +0 -2
  27. package/dist/types/data.d.ts +0 -8
  28. package/dist/types/disabled.d.ts +0 -4
  29. package/dist/types/index.d.ts +0 -4
  30. package/dist/types/strategies.d.ts +0 -9
  31. package/dist/types/type-guard.d.ts +0 -7
  32. package/dist/utilities/arrayMove.d.ts +0 -4
  33. package/dist/utilities/arraySwap.d.ts +0 -4
  34. package/dist/utilities/getSortedRects.d.ts +0 -2
  35. package/dist/utilities/index.d.ts +0 -6
  36. package/dist/utilities/isValidIndex.d.ts +0 -1
  37. package/dist/utilities/itemsEqual.d.ts +0 -2
  38. package/dist/utilities/normalizeDisabled.d.ts +0 -2
@@ -1,783 +0,0 @@
1
- import React, { useMemo, useRef, useEffect, useState, useContext } from 'react';
2
- import { useDndContext, getClientRect, useDroppable, useDraggable, closestCorners, getFirstCollision, getScrollableAncestors, KeyboardCode } from '@lucasols/dnd-kit-core';
3
- import { useUniqueId, useIsomorphicLayoutEffect, CSS, useCombinedRefs, isKeyboardEvent, subtract } from '@lucasols/dnd-kit-utilities';
4
-
5
- /**
6
- * Move an array item to a different position. Returns a new array with the item moved to the new position.
7
- */
8
- function arrayMove(array, from, to) {
9
- const newArray = array.slice();
10
- newArray.splice(to < 0 ? newArray.length + to : to, 0, newArray.splice(from, 1)[0]);
11
- return newArray;
12
- }
13
-
14
- /**
15
- * Swap an array item to a different position. Returns a new array with the item swapped to the new position.
16
- */
17
- function arraySwap(array, from, to) {
18
- const newArray = array.slice();
19
- newArray[from] = array[to];
20
- newArray[to] = array[from];
21
- return newArray;
22
- }
23
-
24
- function getSortedRects(items, rects) {
25
- return items.reduce((accumulator, id, index) => {
26
- const rect = rects.get(id);
27
-
28
- if (rect) {
29
- accumulator[index] = rect;
30
- }
31
-
32
- return accumulator;
33
- }, Array(items.length));
34
- }
35
-
36
- function isValidIndex(index) {
37
- return index !== null && index >= 0;
38
- }
39
-
40
- function itemsEqual(a, b) {
41
- if (a === b) {
42
- return true;
43
- }
44
-
45
- if (a.length !== b.length) {
46
- return false;
47
- }
48
-
49
- for (let i = 0; i < a.length; i++) {
50
- if (a[i] !== b[i]) {
51
- return false;
52
- }
53
- }
54
-
55
- return true;
56
- }
57
-
58
- function normalizeDisabled(disabled) {
59
- if (typeof disabled === 'boolean') {
60
- return {
61
- draggable: disabled,
62
- droppable: disabled
63
- };
64
- }
65
-
66
- return disabled;
67
- }
68
-
69
- // To-do: We should be calculating scale transformation
70
- const defaultScale = {
71
- scaleX: 1,
72
- scaleY: 1
73
- };
74
- const horizontalListSortingStrategy = _ref => {
75
- var _rects$activeIndex;
76
-
77
- let {
78
- rects,
79
- activeNodeRect: fallbackActiveRect,
80
- activeIndex,
81
- overIndex,
82
- index
83
- } = _ref;
84
- const activeNodeRect = (_rects$activeIndex = rects[activeIndex]) != null ? _rects$activeIndex : fallbackActiveRect;
85
-
86
- if (!activeNodeRect) {
87
- return null;
88
- }
89
-
90
- const itemGap = getItemGap(rects, index, activeIndex);
91
-
92
- if (index === activeIndex) {
93
- const newIndexRect = rects[overIndex];
94
-
95
- if (!newIndexRect) {
96
- return null;
97
- }
98
-
99
- return {
100
- x: activeIndex < overIndex ? newIndexRect.left + newIndexRect.width - (activeNodeRect.left + activeNodeRect.width) : newIndexRect.left - activeNodeRect.left,
101
- y: 0,
102
- ...defaultScale
103
- };
104
- }
105
-
106
- if (index > activeIndex && index <= overIndex) {
107
- return {
108
- x: -activeNodeRect.width - itemGap,
109
- y: 0,
110
- ...defaultScale
111
- };
112
- }
113
-
114
- if (index < activeIndex && index >= overIndex) {
115
- return {
116
- x: activeNodeRect.width + itemGap,
117
- y: 0,
118
- ...defaultScale
119
- };
120
- }
121
-
122
- return {
123
- x: 0,
124
- y: 0,
125
- ...defaultScale
126
- };
127
- };
128
-
129
- function getItemGap(rects, index, activeIndex) {
130
- const currentRect = rects[index];
131
- const previousRect = rects[index - 1];
132
- const nextRect = rects[index + 1];
133
-
134
- if (!currentRect || !previousRect && !nextRect) {
135
- return 0;
136
- }
137
-
138
- if (activeIndex < index) {
139
- return previousRect ? currentRect.left - (previousRect.left + previousRect.width) : nextRect.left - (currentRect.left + currentRect.width);
140
- }
141
-
142
- return nextRect ? nextRect.left - (currentRect.left + currentRect.width) : currentRect.left - (previousRect.left + previousRect.width);
143
- }
144
-
145
- const rectSortingStrategy = _ref => {
146
- let {
147
- rects,
148
- activeIndex,
149
- overIndex,
150
- index
151
- } = _ref;
152
- const newRects = arrayMove(rects, overIndex, activeIndex);
153
- const oldRect = rects[index];
154
- const newRect = newRects[index];
155
-
156
- if (!newRect || !oldRect) {
157
- return null;
158
- }
159
-
160
- return {
161
- x: newRect.left - oldRect.left,
162
- y: newRect.top - oldRect.top,
163
- scaleX: newRect.width / oldRect.width,
164
- scaleY: newRect.height / oldRect.height
165
- };
166
- };
167
-
168
- const rectSwappingStrategy = _ref => {
169
- let {
170
- activeIndex,
171
- index,
172
- rects,
173
- overIndex
174
- } = _ref;
175
- let oldRect;
176
- let newRect;
177
-
178
- if (index === activeIndex) {
179
- oldRect = rects[index];
180
- newRect = rects[overIndex];
181
- }
182
-
183
- if (index === overIndex) {
184
- oldRect = rects[index];
185
- newRect = rects[activeIndex];
186
- }
187
-
188
- if (!newRect || !oldRect) {
189
- return null;
190
- }
191
-
192
- return {
193
- x: newRect.left - oldRect.left,
194
- y: newRect.top - oldRect.top,
195
- scaleX: newRect.width / oldRect.width,
196
- scaleY: newRect.height / oldRect.height
197
- };
198
- };
199
-
200
- // To-do: We should be calculating scale transformation
201
- const defaultScale$1 = {
202
- scaleX: 1,
203
- scaleY: 1
204
- };
205
- const verticalListSortingStrategy = _ref => {
206
- var _rects$activeIndex;
207
-
208
- let {
209
- activeIndex,
210
- activeNodeRect: fallbackActiveRect,
211
- index,
212
- rects,
213
- overIndex
214
- } = _ref;
215
- const activeNodeRect = (_rects$activeIndex = rects[activeIndex]) != null ? _rects$activeIndex : fallbackActiveRect;
216
-
217
- if (!activeNodeRect) {
218
- return null;
219
- }
220
-
221
- if (index === activeIndex) {
222
- const overIndexRect = rects[overIndex];
223
-
224
- if (!overIndexRect) {
225
- return null;
226
- }
227
-
228
- return {
229
- x: 0,
230
- y: activeIndex < overIndex ? overIndexRect.top + overIndexRect.height - (activeNodeRect.top + activeNodeRect.height) : overIndexRect.top - activeNodeRect.top,
231
- ...defaultScale$1
232
- };
233
- }
234
-
235
- const itemGap = getItemGap$1(rects, index, activeIndex);
236
-
237
- if (index > activeIndex && index <= overIndex) {
238
- return {
239
- x: 0,
240
- y: -activeNodeRect.height - itemGap,
241
- ...defaultScale$1
242
- };
243
- }
244
-
245
- if (index < activeIndex && index >= overIndex) {
246
- return {
247
- x: 0,
248
- y: activeNodeRect.height + itemGap,
249
- ...defaultScale$1
250
- };
251
- }
252
-
253
- return {
254
- x: 0,
255
- y: 0,
256
- ...defaultScale$1
257
- };
258
- };
259
-
260
- function getItemGap$1(clientRects, index, activeIndex) {
261
- const currentRect = clientRects[index];
262
- const previousRect = clientRects[index - 1];
263
- const nextRect = clientRects[index + 1];
264
-
265
- if (!currentRect) {
266
- return 0;
267
- }
268
-
269
- if (activeIndex < index) {
270
- return previousRect ? currentRect.top - (previousRect.top + previousRect.height) : nextRect ? nextRect.top - (currentRect.top + currentRect.height) : 0;
271
- }
272
-
273
- return nextRect ? nextRect.top - (currentRect.top + currentRect.height) : previousRect ? currentRect.top - (previousRect.top + previousRect.height) : 0;
274
- }
275
-
276
- const ID_PREFIX = 'Sortable';
277
- const Context = /*#__PURE__*/React.createContext({
278
- activeIndex: -1,
279
- containerId: ID_PREFIX,
280
- disableTransforms: false,
281
- items: [],
282
- overIndex: -1,
283
- useDragOverlay: false,
284
- sortedRects: [],
285
- strategy: rectSortingStrategy,
286
- disabled: {
287
- draggable: false,
288
- droppable: false
289
- }
290
- });
291
- function SortableContext(_ref) {
292
- let {
293
- children,
294
- id,
295
- items: userDefinedItems,
296
- strategy = rectSortingStrategy,
297
- disabled: disabledProp = false
298
- } = _ref;
299
- const {
300
- active,
301
- dragOverlay,
302
- droppableRects,
303
- over,
304
- measureDroppableContainers
305
- } = useDndContext();
306
- const containerId = useUniqueId(ID_PREFIX, id);
307
- const useDragOverlay = Boolean(dragOverlay.rect !== null);
308
- const items = useMemo(() => userDefinedItems.map(item => typeof item === 'object' && 'id' in item ? item.id : item), [userDefinedItems]);
309
- const isDragging = active != null;
310
- const activeIndex = active ? items.indexOf(active.id) : -1;
311
- const overIndex = over ? items.indexOf(over.id) : -1;
312
- const previousItemsRef = useRef(items);
313
- const itemsHaveChanged = !itemsEqual(items, previousItemsRef.current);
314
- const disableTransforms = overIndex !== -1 && activeIndex === -1 || itemsHaveChanged;
315
- const disabled = normalizeDisabled(disabledProp);
316
- useIsomorphicLayoutEffect(() => {
317
- if (itemsHaveChanged && isDragging) {
318
- measureDroppableContainers(items);
319
- }
320
- }, [itemsHaveChanged, items, isDragging, measureDroppableContainers]);
321
- useEffect(() => {
322
- previousItemsRef.current = items;
323
- }, [items]);
324
- const contextValue = useMemo(() => ({
325
- activeIndex,
326
- containerId,
327
- disabled,
328
- disableTransforms,
329
- items,
330
- overIndex,
331
- useDragOverlay,
332
- sortedRects: getSortedRects(items, droppableRects),
333
- strategy
334
- }), // eslint-disable-next-line react-hooks/exhaustive-deps
335
- [activeIndex, containerId, disabled.draggable, disabled.droppable, disableTransforms, items, overIndex, droppableRects, useDragOverlay, strategy]);
336
- return React.createElement(Context.Provider, {
337
- value: contextValue
338
- }, children);
339
- }
340
-
341
- const defaultNewIndexGetter = _ref => {
342
- let {
343
- id,
344
- items,
345
- activeIndex,
346
- overIndex
347
- } = _ref;
348
- return arrayMove(items, activeIndex, overIndex).indexOf(id);
349
- };
350
- const defaultAnimateLayoutChanges = _ref2 => {
351
- let {
352
- containerId,
353
- isSorting,
354
- wasDragging,
355
- index,
356
- items,
357
- newIndex,
358
- previousItems,
359
- previousContainerId,
360
- transition
361
- } = _ref2;
362
-
363
- if (!transition || !wasDragging) {
364
- return false;
365
- }
366
-
367
- if (previousItems !== items && index === newIndex) {
368
- return false;
369
- }
370
-
371
- if (isSorting) {
372
- return true;
373
- }
374
-
375
- return newIndex !== index && containerId === previousContainerId;
376
- };
377
- const defaultTransition = {
378
- duration: 200,
379
- easing: 'ease'
380
- };
381
- const transitionProperty = 'transform';
382
- const disabledTransition = /*#__PURE__*/CSS.Transition.toString({
383
- property: transitionProperty,
384
- duration: 0,
385
- easing: 'linear'
386
- });
387
- const defaultAttributes = {
388
- roleDescription: 'sortable'
389
- };
390
-
391
- /*
392
- * When the index of an item changes while sorting,
393
- * we need to temporarily disable the transforms
394
- */
395
-
396
- function useDerivedTransform(_ref) {
397
- let {
398
- disabled,
399
- index,
400
- node,
401
- rect
402
- } = _ref;
403
- const [derivedTransform, setDerivedtransform] = useState(null);
404
- const previousIndex = useRef(index);
405
- useIsomorphicLayoutEffect(() => {
406
- if (!disabled && index !== previousIndex.current && node.current) {
407
- const initial = rect.current;
408
-
409
- if (initial) {
410
- const current = getClientRect(node.current, {
411
- ignoreTransform: true
412
- });
413
- const delta = {
414
- x: initial.left - current.left,
415
- y: initial.top - current.top,
416
- scaleX: initial.width / current.width,
417
- scaleY: initial.height / current.height
418
- };
419
-
420
- if (delta.x || delta.y) {
421
- setDerivedtransform(delta);
422
- }
423
- }
424
- }
425
-
426
- if (index !== previousIndex.current) {
427
- previousIndex.current = index;
428
- }
429
- }, [disabled, index, node, rect]);
430
- useEffect(() => {
431
- if (derivedTransform) {
432
- setDerivedtransform(null);
433
- }
434
- }, [derivedTransform]);
435
- return derivedTransform;
436
- }
437
-
438
- function useSortable(_ref) {
439
- let {
440
- animateLayoutChanges = defaultAnimateLayoutChanges,
441
- attributes: userDefinedAttributes,
442
- disabled: localDisabled,
443
- data: customData,
444
- getNewIndex = defaultNewIndexGetter,
445
- id,
446
- strategy: localStrategy,
447
- resizeObserverConfig,
448
- transition = defaultTransition
449
- } = _ref;
450
- const {
451
- items,
452
- containerId,
453
- activeIndex,
454
- disabled: globalDisabled,
455
- disableTransforms,
456
- sortedRects,
457
- overIndex,
458
- useDragOverlay,
459
- strategy: globalStrategy
460
- } = useContext(Context);
461
- const disabled = normalizeLocalDisabled(localDisabled, globalDisabled);
462
- const index = items.indexOf(id);
463
- const data = useMemo(() => ({
464
- sortable: {
465
- containerId,
466
- index,
467
- items
468
- },
469
- ...customData
470
- }), [containerId, customData, index, items]);
471
- const itemsAfterCurrentSortable = useMemo(() => items.slice(items.indexOf(id)), [items, id]);
472
- const {
473
- rect,
474
- node,
475
- isOver,
476
- setNodeRef: setDroppableNodeRef
477
- } = useDroppable({
478
- id,
479
- data,
480
- disabled: disabled.droppable,
481
- resizeObserverConfig: {
482
- updateMeasurementsFor: itemsAfterCurrentSortable,
483
- ...resizeObserverConfig
484
- }
485
- });
486
- const {
487
- active,
488
- activatorEvent,
489
- activeNodeRect,
490
- attributes,
491
- setNodeRef: setDraggableNodeRef,
492
- listeners,
493
- isDragging,
494
- over,
495
- setActivatorNodeRef,
496
- transform
497
- } = useDraggable({
498
- id,
499
- data,
500
- attributes: { ...defaultAttributes,
501
- ...userDefinedAttributes
502
- },
503
- disabled: disabled.draggable
504
- });
505
- const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
506
- const isSorting = Boolean(active);
507
- const displaceItem = isSorting && !disableTransforms && isValidIndex(activeIndex) && isValidIndex(overIndex);
508
- const shouldDisplaceDragSource = !useDragOverlay && isDragging;
509
- const dragSourceDisplacement = shouldDisplaceDragSource && displaceItem ? transform : null;
510
- const strategy = localStrategy != null ? localStrategy : globalStrategy;
511
- const finalTransform = displaceItem ? dragSourceDisplacement != null ? dragSourceDisplacement : strategy({
512
- rects: sortedRects,
513
- activeNodeRect,
514
- activeIndex,
515
- overIndex,
516
- index
517
- }) : null;
518
- const newIndex = isValidIndex(activeIndex) && isValidIndex(overIndex) ? getNewIndex({
519
- id,
520
- items,
521
- activeIndex,
522
- overIndex
523
- }) : index;
524
- const activeId = active == null ? void 0 : active.id;
525
- const previous = useRef({
526
- activeId,
527
- items,
528
- newIndex,
529
- containerId
530
- });
531
- const itemsHaveChanged = items !== previous.current.items;
532
- const shouldAnimateLayoutChanges = animateLayoutChanges({
533
- active,
534
- containerId,
535
- isDragging,
536
- isSorting,
537
- id,
538
- index,
539
- items,
540
- newIndex: previous.current.newIndex,
541
- previousItems: previous.current.items,
542
- previousContainerId: previous.current.containerId,
543
- transition,
544
- wasDragging: previous.current.activeId != null
545
- });
546
- const derivedTransform = useDerivedTransform({
547
- disabled: !shouldAnimateLayoutChanges,
548
- index,
549
- node,
550
- rect
551
- });
552
- useEffect(() => {
553
- if (isSorting && previous.current.newIndex !== newIndex) {
554
- previous.current.newIndex = newIndex;
555
- }
556
-
557
- if (containerId !== previous.current.containerId) {
558
- previous.current.containerId = containerId;
559
- }
560
-
561
- if (items !== previous.current.items) {
562
- previous.current.items = items;
563
- }
564
- }, [isSorting, newIndex, containerId, items]);
565
- useEffect(() => {
566
- if (activeId === previous.current.activeId) {
567
- return;
568
- }
569
-
570
- if (activeId && !previous.current.activeId) {
571
- previous.current.activeId = activeId;
572
- return;
573
- }
574
-
575
- const timeoutId = setTimeout(() => {
576
- previous.current.activeId = activeId;
577
- }, 50);
578
- return () => clearTimeout(timeoutId);
579
- }, [activeId]);
580
- return {
581
- active,
582
- activeIndex,
583
- attributes,
584
- data,
585
- rect,
586
- index,
587
- newIndex,
588
- items,
589
- isOver,
590
- isSorting,
591
- isDragging,
592
- listeners,
593
- node,
594
- overIndex,
595
- over,
596
- setNodeRef,
597
- setActivatorNodeRef,
598
- setDroppableNodeRef,
599
- setDraggableNodeRef,
600
- transform: derivedTransform != null ? derivedTransform : finalTransform,
601
- transition: getTransition()
602
- };
603
-
604
- function getTransition() {
605
- if ( // Temporarily disable transitions for a single frame to set up derived transforms
606
- derivedTransform || // Or to prevent items jumping to back to their "new" position when items change
607
- itemsHaveChanged && previous.current.newIndex === index) {
608
- return disabledTransition;
609
- }
610
-
611
- if (shouldDisplaceDragSource && !isKeyboardEvent(activatorEvent) || !transition) {
612
- return undefined;
613
- }
614
-
615
- if (isSorting || shouldAnimateLayoutChanges) {
616
- return CSS.Transition.toString({ ...transition,
617
- property: transitionProperty
618
- });
619
- }
620
-
621
- return undefined;
622
- }
623
- }
624
-
625
- function normalizeLocalDisabled(localDisabled, globalDisabled) {
626
- var _localDisabled$dragga, _localDisabled$droppa;
627
-
628
- if (typeof localDisabled === 'boolean') {
629
- return {
630
- draggable: localDisabled,
631
- // Backwards compatibility
632
- droppable: false
633
- };
634
- }
635
-
636
- return {
637
- draggable: (_localDisabled$dragga = localDisabled == null ? void 0 : localDisabled.draggable) != null ? _localDisabled$dragga : globalDisabled.draggable,
638
- droppable: (_localDisabled$droppa = localDisabled == null ? void 0 : localDisabled.droppable) != null ? _localDisabled$droppa : globalDisabled.droppable
639
- };
640
- }
641
-
642
- function hasSortableData(entry) {
643
- if (!entry) {
644
- return false;
645
- }
646
-
647
- const data = entry.data.current;
648
-
649
- if (data && 'sortable' in data && typeof data.sortable === 'object' && 'containerId' in data.sortable && 'items' in data.sortable && 'index' in data.sortable) {
650
- return true;
651
- }
652
-
653
- return false;
654
- }
655
-
656
- const directions = [KeyboardCode.Down, KeyboardCode.Right, KeyboardCode.Up, KeyboardCode.Left];
657
- const sortableKeyboardCoordinates = (event, _ref) => {
658
- let {
659
- context: {
660
- active,
661
- collisionRect,
662
- droppableRects,
663
- droppableContainers,
664
- over,
665
- scrollableAncestors
666
- }
667
- } = _ref;
668
-
669
- if (directions.includes(event.code)) {
670
- event.preventDefault();
671
-
672
- if (!active || !collisionRect) {
673
- return;
674
- }
675
-
676
- const filteredContainers = [];
677
- droppableContainers.getEnabled().forEach(entry => {
678
- if (!entry || entry != null && entry.disabled) {
679
- return;
680
- }
681
-
682
- const rect = droppableRects.get(entry.id);
683
-
684
- if (!rect) {
685
- return;
686
- }
687
-
688
- switch (event.code) {
689
- case KeyboardCode.Down:
690
- if (collisionRect.top < rect.top) {
691
- filteredContainers.push(entry);
692
- }
693
-
694
- break;
695
-
696
- case KeyboardCode.Up:
697
- if (collisionRect.top > rect.top) {
698
- filteredContainers.push(entry);
699
- }
700
-
701
- break;
702
-
703
- case KeyboardCode.Left:
704
- if (collisionRect.left > rect.left) {
705
- filteredContainers.push(entry);
706
- }
707
-
708
- break;
709
-
710
- case KeyboardCode.Right:
711
- if (collisionRect.left < rect.left) {
712
- filteredContainers.push(entry);
713
- }
714
-
715
- break;
716
- }
717
- });
718
- const collisions = closestCorners({
719
- active,
720
- collisionRect: collisionRect,
721
- droppableRects,
722
- droppableContainers: filteredContainers,
723
- pointerCoordinates: null
724
- });
725
- let closestId = getFirstCollision(collisions, 'id');
726
-
727
- if (closestId === (over == null ? void 0 : over.id) && collisions.length > 1) {
728
- closestId = collisions[1].id;
729
- }
730
-
731
- if (closestId != null) {
732
- const activeDroppable = droppableContainers.get(active.id);
733
- const newDroppable = droppableContainers.get(closestId);
734
- const newRect = newDroppable ? droppableRects.get(newDroppable.id) : null;
735
- const newNode = newDroppable == null ? void 0 : newDroppable.node.current;
736
-
737
- if (newNode && newRect && activeDroppable && newDroppable) {
738
- const newScrollAncestors = getScrollableAncestors(newNode);
739
- const hasDifferentScrollAncestors = newScrollAncestors.some((element, index) => scrollableAncestors[index] !== element);
740
- const hasSameContainer = isSameContainer(activeDroppable, newDroppable);
741
- const isAfterActive = isAfter(activeDroppable, newDroppable);
742
- const offset = hasDifferentScrollAncestors || !hasSameContainer ? {
743
- x: 0,
744
- y: 0
745
- } : {
746
- x: isAfterActive ? collisionRect.width - newRect.width : 0,
747
- y: isAfterActive ? collisionRect.height - newRect.height : 0
748
- };
749
- const rectCoordinates = {
750
- x: newRect.left,
751
- y: newRect.top
752
- };
753
- const newCoordinates = offset.x && offset.y ? rectCoordinates : subtract(rectCoordinates, offset);
754
- return newCoordinates;
755
- }
756
- }
757
- }
758
-
759
- return undefined;
760
- };
761
-
762
- function isSameContainer(a, b) {
763
- if (!hasSortableData(a) || !hasSortableData(b)) {
764
- return false;
765
- }
766
-
767
- return a.data.current.sortable.containerId === b.data.current.sortable.containerId;
768
- }
769
-
770
- function isAfter(a, b) {
771
- if (!hasSortableData(a) || !hasSortableData(b)) {
772
- return false;
773
- }
774
-
775
- if (!isSameContainer(a, b)) {
776
- return false;
777
- }
778
-
779
- return a.data.current.sortable.index < b.data.current.sortable.index;
780
- }
781
-
782
- export { SortableContext, arrayMove, arraySwap, defaultAnimateLayoutChanges, defaultNewIndexGetter, hasSortableData, horizontalListSortingStrategy, rectSortingStrategy, rectSwappingStrategy, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy };
783
- //# sourceMappingURL=dnd-kit-sortable.esm.js.map