@elliemae/ds-drag-and-drop 2.2.0-alpha.3 → 2.2.0-next.2

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 (82) hide show
  1. package/cjs/hierarchy/customCollisionDetection.js +50 -44
  2. package/cjs/hierarchy/getHierarchyKeyboardCoordinates.js +91 -78
  3. package/cjs/hierarchy/index.js +9 -28
  4. package/cjs/hierarchy/types.js +2 -27
  5. package/cjs/hierarchy/useHierarchyActionHandlers.js +35 -53
  6. package/cjs/hierarchy/useHierarchyAnnouncements.js +26 -45
  7. package/cjs/hierarchy/useHierarchyDndkitConfig.js +83 -77
  8. package/cjs/hierarchy/useHierarchyPreviewHandlers.js +27 -48
  9. package/cjs/index.js +11 -29
  10. package/cjs/tree/constants.js +12 -41
  11. package/cjs/tree/customCollisionDetection.js +128 -93
  12. package/cjs/tree/getTreeKeyboardCoordinates.js +100 -87
  13. package/cjs/tree/index.js +9 -28
  14. package/cjs/tree/types.js +2 -27
  15. package/cjs/tree/useTreeActionHandlers.js +56 -67
  16. package/cjs/tree/useTreeAnnouncements.js +40 -53
  17. package/cjs/tree/useTreeDndkitConfig.js +108 -102
  18. package/cjs/tree/useTreePreviewHandlers.js +37 -53
  19. package/cjs/tree/utilities.js +30 -44
  20. package/esm/hierarchy/customCollisionDetection.js +46 -15
  21. package/esm/hierarchy/getHierarchyKeyboardCoordinates.js +76 -42
  22. package/esm/hierarchy/index.js +1 -3
  23. package/esm/hierarchy/types.js +1 -2
  24. package/esm/hierarchy/useHierarchyActionHandlers.js +31 -24
  25. package/esm/hierarchy/useHierarchyAnnouncements.js +20 -14
  26. package/esm/hierarchy/useHierarchyDndkitConfig.js +66 -45
  27. package/esm/hierarchy/useHierarchyPreviewHandlers.js +19 -15
  28. package/esm/index.js +2 -4
  29. package/esm/tree/constants.js +10 -12
  30. package/esm/tree/customCollisionDetection.js +124 -64
  31. package/esm/tree/getTreeKeyboardCoordinates.js +83 -49
  32. package/esm/tree/index.js +1 -3
  33. package/esm/tree/types.js +1 -2
  34. package/esm/tree/useTreeActionHandlers.js +50 -36
  35. package/esm/tree/useTreeAnnouncements.js +30 -18
  36. package/esm/tree/useTreeDndkitConfig.js +89 -68
  37. package/esm/tree/useTreePreviewHandlers.js +28 -19
  38. package/esm/tree/utilities.js +23 -13
  39. package/package.json +1 -1
  40. package/types/hierarchy/types.d.ts +1 -1
  41. package/types/tree/types.d.ts +17 -13
  42. package/types/tree/useTreeActionHandlers.d.ts +2 -2
  43. package/types/tree/useTreeDndkitConfig.d.ts +2 -2
  44. package/types/tree/useTreePreviewHandlers.d.ts +2 -2
  45. package/cjs/hierarchy/customCollisionDetection.js.map +0 -7
  46. package/cjs/hierarchy/getHierarchyKeyboardCoordinates.js.map +0 -7
  47. package/cjs/hierarchy/index.js.map +0 -7
  48. package/cjs/hierarchy/types.js.map +0 -7
  49. package/cjs/hierarchy/useHierarchyActionHandlers.js.map +0 -7
  50. package/cjs/hierarchy/useHierarchyAnnouncements.js.map +0 -7
  51. package/cjs/hierarchy/useHierarchyDndkitConfig.js.map +0 -7
  52. package/cjs/hierarchy/useHierarchyPreviewHandlers.js.map +0 -7
  53. package/cjs/index.js.map +0 -7
  54. package/cjs/tree/constants.js.map +0 -7
  55. package/cjs/tree/customCollisionDetection.js.map +0 -7
  56. package/cjs/tree/getTreeKeyboardCoordinates.js.map +0 -7
  57. package/cjs/tree/index.js.map +0 -7
  58. package/cjs/tree/types.js.map +0 -7
  59. package/cjs/tree/useTreeActionHandlers.js.map +0 -7
  60. package/cjs/tree/useTreeAnnouncements.js.map +0 -7
  61. package/cjs/tree/useTreeDndkitConfig.js.map +0 -7
  62. package/cjs/tree/useTreePreviewHandlers.js.map +0 -7
  63. package/cjs/tree/utilities.js.map +0 -7
  64. package/esm/hierarchy/customCollisionDetection.js.map +0 -7
  65. package/esm/hierarchy/getHierarchyKeyboardCoordinates.js.map +0 -7
  66. package/esm/hierarchy/index.js.map +0 -7
  67. package/esm/hierarchy/types.js.map +0 -7
  68. package/esm/hierarchy/useHierarchyActionHandlers.js.map +0 -7
  69. package/esm/hierarchy/useHierarchyAnnouncements.js.map +0 -7
  70. package/esm/hierarchy/useHierarchyDndkitConfig.js.map +0 -7
  71. package/esm/hierarchy/useHierarchyPreviewHandlers.js.map +0 -7
  72. package/esm/index.js.map +0 -7
  73. package/esm/tree/constants.js.map +0 -7
  74. package/esm/tree/customCollisionDetection.js.map +0 -7
  75. package/esm/tree/getTreeKeyboardCoordinates.js.map +0 -7
  76. package/esm/tree/index.js.map +0 -7
  77. package/esm/tree/types.js.map +0 -7
  78. package/esm/tree/useTreeActionHandlers.js.map +0 -7
  79. package/esm/tree/useTreeAnnouncements.js.map +0 -7
  80. package/esm/tree/useTreeDndkitConfig.js.map +0 -7
  81. package/esm/tree/useTreePreviewHandlers.js.map +0 -7
  82. package/esm/tree/utilities.js.map +0 -7
@@ -1,77 +1,137 @@
1
- import * as React from "react";
2
- import { DropIndicatorPosition } from "./constants";
3
- const DUMMY_ID = "DUMMY_ID_INTERNAL_USE_ONLY";
1
+ import 'core-js/modules/esnext.async-iterator.find.js';
2
+ import 'core-js/modules/esnext.iterator.constructor.js';
3
+ import 'core-js/modules/esnext.iterator.find.js';
4
+ import 'core-js/modules/esnext.async-iterator.reduce.js';
5
+ import 'core-js/modules/esnext.iterator.reduce.js';
6
+ import 'core-js/modules/web.dom-collections.iterator.js';
7
+ import { DropIndicatorPosition } from './constants.js';
8
+
9
+ const DUMMY_ID = 'DUMMY_ID_INTERNAL_USE_ONLY';
4
10
  const RECT_UP = {
5
11
  offsetTop: -Infinity,
6
12
  offsetLeft: 0,
13
+ // Doesn't matter,
7
14
  width: 0,
8
- height: 0
15
+ // Doesn't matter
16
+ height: 0 // Doesn't matter
17
+
9
18
  };
10
19
  const RECT_DOWN = {
11
20
  offsetTop: Infinity,
12
21
  offsetLeft: 0,
22
+ // Doesn't matter,
13
23
  width: 0,
14
- height: 0
15
- };
16
- const thresholdRatio = 0.2;
24
+ // Doesn't matter
25
+ height: 0 // Doesn't matter
26
+
27
+ }; // Percentage of height to take into consideration when looking for colliding rects
28
+
29
+ const thresholdRatio = 0.2; // Percentage to be inside
30
+
17
31
  const insideThreshold = 0.7;
18
- const customCollisionDetection = (activeId, visibleItemsDictionary, setDropIndicatorPosition, maxDragAndDropLevel, lastPosition, setLastPosition) => ({ droppableContainers, collisionRect }) => {
19
- const originalContainer = droppableContainers.find(({ id }) => id === activeId);
20
- const originalRect = originalContainer?.rect?.current;
21
- let isUp = lastPosition === "up";
22
- if (originalRect) {
23
- isUp = originalRect.offsetTop > collisionRect.top;
24
- }
25
- const threshold = collisionRect.height * thresholdRatio;
26
- let collidingContainer = null;
27
- if (isUp) {
28
- collidingContainer = droppableContainers.reduce((firstRectDown, container) => {
29
- const rect = container.rect.current;
30
- if (rect && firstRectDown.rect.current) {
31
- const { offsetTop: rectOffsetTop } = rect;
32
- const { offsetTop: firstRectDownOffsetTop } = firstRectDown.rect.current;
33
- if (rectOffsetTop + threshold > collisionRect.top && rectOffsetTop < firstRectDownOffsetTop) {
34
- return container;
32
+ const customCollisionDetection = (activeId, visibleItemsDictionary, setDropIndicatorPosition, maxDragAndDropLevel, lastPosition, setLastPosition) => {
33
+ const func = _ref => {
34
+ var _originalContainer$re;
35
+
36
+ let {
37
+ droppableContainers,
38
+ collisionRect
39
+ } = _ref;
40
+ const originalContainer = droppableContainers.find(_ref2 => {
41
+ let {
42
+ id
43
+ } = _ref2;
44
+ return id === activeId;
45
+ });
46
+ const originalRect = originalContainer === null || originalContainer === void 0 ? void 0 : (_originalContainer$re = originalContainer.rect) === null || _originalContainer$re === void 0 ? void 0 : _originalContainer$re.current; // We first check if the item was moved up or down
47
+ // This modifies how to search the matching colliding rect
48
+
49
+ let isUp = lastPosition === 'up';
50
+
51
+ if (originalRect) {
52
+ isUp = originalRect.offsetTop > collisionRect.top;
53
+ } // Threshold
54
+
55
+
56
+ const threshold = collisionRect.height * thresholdRatio;
57
+ let collidingContainer = null;
58
+
59
+ if (isUp) {
60
+ // Up -- We need to find the first rectangle downwards
61
+ collidingContainer = droppableContainers.reduce((firstRectDown, container) => {
62
+ const rect = container.rect.current;
63
+
64
+ if (rect && firstRectDown.rect.current) {
65
+ const {
66
+ offsetTop: rectOffsetTop
67
+ } = rect;
68
+ const {
69
+ offsetTop: firstRectDownOffsetTop
70
+ } = firstRectDown.rect.current;
71
+
72
+ if (rectOffsetTop + threshold > collisionRect.top && rectOffsetTop < firstRectDownOffsetTop) {
73
+ return container;
74
+ }
35
75
  }
36
- }
37
- return firstRectDown;
38
- }, { id: DUMMY_ID, rect: { current: RECT_DOWN } });
39
- } else {
40
- collidingContainer = droppableContainers.reduce((firstRectUp, container) => {
41
- const rect = container.rect.current;
42
- if (rect && firstRectUp.rect.current) {
43
- const { offsetTop: rectOffsetTop } = rect;
44
- const { offsetTop: firstRectUpOffsetTop } = firstRectUp.rect.current;
45
- if (rectOffsetTop - threshold < collisionRect.top && rectOffsetTop > firstRectUpOffsetTop) {
46
- return container;
76
+
77
+ return firstRectDown;
78
+ }, {
79
+ id: DUMMY_ID,
80
+ rect: {
81
+ current: RECT_DOWN
47
82
  }
48
- }
49
- return firstRectUp;
50
- }, { id: DUMMY_ID, rect: { current: RECT_UP } });
51
- }
52
- if (collidingContainer.id === DUMMY_ID) {
53
- return null;
54
- }
55
- const collidingRect = collidingContainer.rect.current;
56
- if (!collidingRect)
57
- return null;
58
- const [top, bottom] = [
59
- Math.max(collisionRect.top, collidingRect.offsetTop),
60
- Math.min(collisionRect.bottom, collidingRect.offsetTop + collidingRect.height)
61
- ];
62
- const intersectionPercentage = Math.abs(bottom - top) / collidingRect.height;
63
- if (intersectionPercentage > insideThreshold && visibleItemsDictionary[collidingContainer.id].depth + 1 <= maxDragAndDropLevel && collidingContainer.id !== activeId) {
64
- setDropIndicatorPosition(DropIndicatorPosition.Inside);
65
- } else {
66
- setDropIndicatorPosition(isUp ? DropIndicatorPosition.Before : DropIndicatorPosition.After);
67
- }
68
- if (isUp && lastPosition !== "up")
69
- setLastPosition("up");
70
- else if (!isUp && lastPosition !== "down")
71
- setLastPosition("down");
72
- return collidingContainer.id;
73
- };
74
- export {
75
- customCollisionDetection
83
+ });
84
+ } else {
85
+ // Down -- We need to find the first rectangle upwards
86
+ collidingContainer = droppableContainers.reduce((firstRectUp, container) => {
87
+ const rect = container.rect.current;
88
+
89
+ if (rect && firstRectUp.rect.current) {
90
+ const {
91
+ offsetTop: rectOffsetTop
92
+ } = rect;
93
+ const {
94
+ offsetTop: firstRectUpOffsetTop
95
+ } = firstRectUp.rect.current;
96
+
97
+ if (rectOffsetTop - threshold < collisionRect.top && rectOffsetTop > firstRectUpOffsetTop) {
98
+ return container;
99
+ }
100
+ }
101
+
102
+ return firstRectUp;
103
+ }, {
104
+ id: DUMMY_ID,
105
+ rect: {
106
+ current: RECT_UP
107
+ }
108
+ });
109
+ } // If we didn't find a match, return null
110
+
111
+
112
+ if (collidingContainer.id === DUMMY_ID) {
113
+ return null;
114
+ }
115
+
116
+ const collidingRect = collidingContainer.rect.current;
117
+ if (!collidingRect) return null; // Calculate the intersection interval
118
+
119
+ const [top, bottom] = [Math.max(collisionRect.top, collidingRect.offsetTop), Math.min(collisionRect.top + collisionRect.height, collidingRect.offsetTop + collidingRect.height)]; // Calculate the percentage of intersection
120
+
121
+ const intersectionPercentage = Math.abs(bottom - top) / collidingRect.height;
122
+
123
+ if (intersectionPercentage > insideThreshold && visibleItemsDictionary[collidingContainer.id].depth + 1 <= maxDragAndDropLevel && collidingContainer.id !== activeId) {
124
+ setDropIndicatorPosition(DropIndicatorPosition.Inside);
125
+ } else {
126
+ setDropIndicatorPosition(isUp ? DropIndicatorPosition.Before : DropIndicatorPosition.After);
127
+ }
128
+
129
+ if (isUp && lastPosition !== 'up') setLastPosition('up');else if (!isUp && lastPosition !== 'down') setLastPosition('down'); // Return the id of the match rectangle
130
+
131
+ return collidingContainer.id;
132
+ };
133
+
134
+ return func;
76
135
  };
77
- //# sourceMappingURL=customCollisionDetection.js.map
136
+
137
+ export { customCollisionDetection };
@@ -1,29 +1,45 @@
1
- import * as React from "react";
2
- import { closestCorners, getViewRect, KeyboardCode } from "@dnd-kit/core";
3
- import { DropIndicatorPosition } from "./constants";
1
+ import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
2
+ import 'core-js/modules/esnext.async-iterator.for-each.js';
3
+ import 'core-js/modules/esnext.iterator.constructor.js';
4
+ import 'core-js/modules/esnext.iterator.for-each.js';
5
+ import 'core-js/modules/esnext.async-iterator.find.js';
6
+ import 'core-js/modules/esnext.iterator.find.js';
7
+ import 'core-js/modules/esnext.async-iterator.filter.js';
8
+ import 'core-js/modules/esnext.iterator.filter.js';
9
+ import { KeyboardCode, closestCorners, getViewRect } from '@dnd-kit/core';
10
+ import { DropIndicatorPosition } from './constants.js';
11
+
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
15
  const directions = [KeyboardCode.Down, KeyboardCode.Right, KeyboardCode.Up, KeyboardCode.Left];
5
16
  const horizontal = [KeyboardCode.Left, KeyboardCode.Right];
6
- const vertical = [KeyboardCode.Up, KeyboardCode.Down];
7
- const getVerticalKeyboardCoordinates = ({
8
- items,
9
- active,
10
- over,
11
- event,
12
- currentCoordinates,
13
- droppableContainers,
14
- collisionRect,
15
- dropIndicatorPosition,
16
- maxDragAndDropLevel
17
- }) => {
18
- if (horizontal.includes(event.code))
19
- return void 0;
17
+
18
+ const getVerticalKeyboardCoordinates = _ref => {
19
+ var _droppableContainers$, _droppableContainers$2;
20
+
21
+ let {
22
+ items,
23
+ active,
24
+ over,
25
+ event,
26
+ currentCoordinates,
27
+ droppableContainers,
28
+ collisionRect,
29
+ dropIndicatorPosition,
30
+ maxDragAndDropLevel
31
+ } = _ref;
32
+ if (horizontal.includes(event.code)) return undefined;
20
33
  const overRect = over.rect;
21
- const layoutRects = [];
22
- droppableContainers.forEach((container) => {
23
- if (container?.disabled || !overRect) {
34
+ const layoutRects = []; // Get the reacheable rects depending on the arrow key pressed
35
+
36
+ droppableContainers.forEach(container => {
37
+ if (container !== null && container !== void 0 && container.disabled || !overRect) {
24
38
  return;
25
39
  }
40
+
26
41
  const rect = container.rect.current;
42
+
27
43
  if (rect && event.code === KeyboardCode.Down && collisionRect.top - 2 <= rect.offsetTop) {
28
44
  layoutRects.push(container);
29
45
  } else if (rect && event.code === KeyboardCode.Up && collisionRect.top >= rect.offsetTop) {
@@ -35,44 +51,64 @@ const getVerticalKeyboardCoordinates = ({
35
51
  droppableContainers: layoutRects,
36
52
  active
37
53
  });
38
- const closestItem = items.find((item) => item.uid === closestId);
39
- const closestElement = droppableContainers.get(closestId)?.node?.current;
40
- if (!closestId || !closestItem || !closestElement)
41
- return void 0;
54
+ const closestItem = items.find(item => item.uid === closestId);
55
+ const closestElement = (_droppableContainers$ = droppableContainers.get(closestId)) === null || _droppableContainers$ === void 0 ? void 0 : (_droppableContainers$2 = _droppableContainers$.node) === null || _droppableContainers$2 === void 0 ? void 0 : _droppableContainers$2.current;
56
+ if (!closestId || !closestItem || !closestElement) return undefined;
42
57
  const closestRect = getViewRect(closestElement);
58
+
43
59
  if (event.code === KeyboardCode.Up) {
60
+ // If the drop indicator is inside (or over ourselves)
61
+ // We are gonna go to the before position
62
+ // Else we are gonna go inside the over rect
44
63
  if (dropIndicatorPosition === DropIndicatorPosition.Inside || closestId === active.id || closestItem.depth + 1 > maxDragAndDropLevel) {
45
- return {
46
- ...currentCoordinates,
64
+ return _objectSpread(_objectSpread({}, currentCoordinates), {}, {
47
65
  y: closestRect.top - collisionRect.height / 2
48
- };
66
+ });
49
67
  }
50
- return {
51
- ...currentCoordinates,
68
+
69
+ return _objectSpread(_objectSpread({}, currentCoordinates), {}, {
52
70
  y: closestRect.top + Math.abs(closestRect.height - collisionRect.height) / 2
53
- };
54
- }
71
+ });
72
+ } // If the drop indicator is inside (or over ourselves)
73
+ // We are gonna go to the after position
74
+ // Else we are gonna go inside the over rect
75
+
76
+
55
77
  if (dropIndicatorPosition === DropIndicatorPosition.Inside || closestId === active.id || closestItem.depth + 1 > maxDragAndDropLevel) {
56
- return {
57
- ...currentCoordinates,
78
+ return _objectSpread(_objectSpread({}, currentCoordinates), {}, {
58
79
  y: closestRect.top + collisionRect.height / 2
59
- };
80
+ });
60
81
  }
61
- return {
62
- ...currentCoordinates,
82
+
83
+ return _objectSpread(_objectSpread({}, currentCoordinates), {}, {
63
84
  y: closestRect.top + Math.abs(closestRect.height - collisionRect.height) / 2
64
- };
85
+ });
65
86
  };
66
- const getTreeKeyboardCoordinates = (context, isHorizontalDnD, maxDragAndDropLevel) => (event, { currentCoordinates, context: { over, translatedRect, droppableContainers, active, collisionRect } }) => {
87
+
88
+ const getTreeKeyboardCoordinates = (context, isHorizontalDnD, maxDragAndDropLevel) => (event, _ref2) => {
89
+ let {
90
+ currentCoordinates,
91
+ context: {
92
+ over,
93
+ translatedRect,
94
+ droppableContainers,
95
+ active,
96
+ collisionRect
97
+ }
98
+ } = _ref2;
99
+
67
100
  if (directions.includes(event.code)) {
68
101
  if (!translatedRect) {
69
- return void 0;
102
+ return undefined;
70
103
  }
104
+
71
105
  const {
72
- current: { items, dropIndicatorPosition }
106
+ current: {
107
+ items,
108
+ dropIndicatorPosition
109
+ }
73
110
  } = context;
74
- if (!over || !active || !collisionRect)
75
- return void 0;
111
+ if (!over || !active || !collisionRect) return undefined;
76
112
  const args = {
77
113
  items,
78
114
  active,
@@ -84,13 +120,11 @@ const getTreeKeyboardCoordinates = (context, isHorizontalDnD, maxDragAndDropLeve
84
120
  dropIndicatorPosition,
85
121
  maxDragAndDropLevel
86
122
  };
87
- if (isHorizontalDnD)
88
- return void 0;
123
+ if (isHorizontalDnD) return undefined;
89
124
  return getVerticalKeyboardCoordinates(args);
90
125
  }
91
- return void 0;
92
- };
93
- export {
94
- getTreeKeyboardCoordinates
126
+
127
+ return undefined;
95
128
  };
96
- //# sourceMappingURL=getTreeKeyboardCoordinates.js.map
129
+
130
+ export { getTreeKeyboardCoordinates };
package/esm/tree/index.js CHANGED
@@ -1,3 +1 @@
1
- import * as React from "react";
2
- export * from "./useTreeDndkitConfig";
3
- //# sourceMappingURL=index.js.map
1
+ export { useTreeDndkitConfig } from './useTreeDndkitConfig.js';
package/esm/tree/types.js CHANGED
@@ -1,2 +1 @@
1
- import * as React from "react";
2
- //# sourceMappingURL=types.js.map
1
+
@@ -1,56 +1,70 @@
1
- import * as React from "react";
2
- import { arrayMove } from "@dnd-kit/sortable";
3
- import { cloneDeep } from "lodash";
4
- import { useCallback } from "react";
5
- import { DropIndicatorPosition } from "./constants";
6
- const useTreeActionHandlers = ({
7
- handlePreviewDragStart,
8
- handlePreviewDragMove,
9
- handlePreviewDragOver,
10
- handlePreviewDragEnd,
11
- handlePreviewDragCancel,
12
- onReorder,
13
- flattenedItems,
14
- projected,
15
- dropIndicatorPosition
16
- }) => {
17
- const onDragStart = useCallback((e) => {
1
+ import { arrayMove } from '@dnd-kit/sortable';
2
+ import { cloneDeep } from 'lodash';
3
+ import { useCallback } from 'react';
4
+ import { DropIndicatorPosition } from './constants.js';
5
+
6
+ const useTreeActionHandlers = _ref => {
7
+ let {
8
+ handlePreviewDragStart,
9
+ handlePreviewDragMove,
10
+ handlePreviewDragOver,
11
+ handlePreviewDragEnd,
12
+ handlePreviewDragCancel,
13
+ onReorder,
14
+ flattenedItems,
15
+ projected,
16
+ dropIndicatorPosition,
17
+ isDropValid
18
+ } = _ref;
19
+ const onDragStart = useCallback(e => {
18
20
  handlePreviewDragStart(e);
19
21
  }, [handlePreviewDragStart]);
20
- const onDragMove = useCallback((e) => {
22
+ const onDragMove = useCallback(e => {
21
23
  handlePreviewDragMove(e);
22
24
  }, [handlePreviewDragMove]);
23
- const onDragOver = useCallback((e) => {
25
+ const onDragOver = useCallback(e => {
24
26
  handlePreviewDragOver(e);
25
27
  }, [handlePreviewDragOver]);
26
- const onDragEnd = useCallback((e) => {
28
+ const onDragEnd = useCallback(e => {
27
29
  handlePreviewDragEnd(e);
28
- const { active, over } = e;
29
- if (over === null)
30
- return;
31
- const activeIndex = flattenedItems.findIndex((item) => item.uid === active.id);
30
+ const {
31
+ active,
32
+ over
33
+ } = e;
34
+ if (over === null || !isDropValid) return;
35
+ const activeIndex = flattenedItems.findIndex(item => item.uid === active.id);
32
36
  let considerExpanding = null;
33
- let overIndex = flattenedItems.findIndex((item) => item.uid === over.id);
37
+ let overIndex = flattenedItems.findIndex(item => item.uid === over.id); // If drop indicator is inside, then put it last,
38
+ // It will be reconstructed well later
39
+
34
40
  if (dropIndicatorPosition === DropIndicatorPosition.Inside) {
35
41
  considerExpanding = over.id;
36
- overIndex = flattenedItems[overIndex].realIndex + (flattenedItems[overIndex].original.subRows?.length ?? 0) + 1;
37
- }
42
+ overIndex = flattenedItems[overIndex].realIndex + flattenedItems[overIndex].childrenCount + 1;
43
+ } // If we are dropping the item in a new position, or new depth
44
+
45
+
38
46
  if (projected && (activeIndex !== overIndex || flattenedItems[activeIndex].depth !== projected.depth)) {
47
+ // Change parent and depth from projected data
39
48
  flattenedItems[activeIndex].parentId = projected.parentId;
40
- flattenedItems[activeIndex].depth = projected.depth;
49
+ flattenedItems[activeIndex].depth = projected.depth; // If same index, don't move the array, just copy it
50
+
41
51
  const newFlattenedData = activeIndex !== overIndex ? arrayMove(flattenedItems, activeIndex, overIndex) : cloneDeep(flattenedItems);
42
52
  onReorder(newFlattenedData, {
43
53
  targetIndex: overIndex,
44
54
  fromIndex: activeIndex
45
- }, considerExpanding || "");
55
+ }, considerExpanding || '');
46
56
  }
47
- }, [handlePreviewDragEnd, flattenedItems, projected, onReorder, dropIndicatorPosition]);
48
- const onDragCancel = useCallback((e) => {
57
+ }, [handlePreviewDragEnd, isDropValid, flattenedItems, dropIndicatorPosition, projected, onReorder]);
58
+ const onDragCancel = useCallback(e => {
49
59
  handlePreviewDragCancel(e);
50
60
  }, [handlePreviewDragCancel]);
51
- return { onDragStart, onDragMove, onDragOver, onDragEnd, onDragCancel };
52
- };
53
- export {
54
- useTreeActionHandlers
61
+ return {
62
+ onDragStart,
63
+ onDragMove,
64
+ onDragOver,
65
+ onDragEnd,
66
+ onDragCancel
67
+ };
55
68
  };
56
- //# sourceMappingURL=useTreeActionHandlers.js.map
69
+
70
+ export { useTreeActionHandlers };
@@ -1,39 +1,51 @@
1
- import * as React from "react";
2
- import { useCallback } from "react";
3
- import { DropIndicatorPosition } from "./constants";
1
+ import { useCallback } from 'react';
2
+ import { DropIndicatorPosition } from './constants.js';
3
+
4
4
  const useTreeAnnouncements = (visibleItemsDictionary, dropIndicatorPosition) => {
5
- const onDragStart = useCallback((id) => `Picked up draggable item from position ${visibleItemsDictionary[id].realIndex + 1}.`, [visibleItemsDictionary]);
5
+ const onDragStart = useCallback(id => "Picked up draggable item from position ".concat(visibleItemsDictionary[id].realIndex + 1, "."), [visibleItemsDictionary]);
6
6
  const onDragMove = useCallback((id, overId) => {
7
7
  if (overId) {
8
8
  const overIndex = visibleItemsDictionary[overId].realIndex + 1;
9
+
9
10
  if (dropIndicatorPosition === DropIndicatorPosition.Inside) {
10
- return `Draggable item was moved inside the item at position ${overIndex}.`;
11
+ return "Draggable item was moved inside the item at position ".concat(overIndex, ".");
11
12
  }
13
+
12
14
  if (dropIndicatorPosition === DropIndicatorPosition.Before) {
13
- return `Draggable item was moved to position ${overIndex - 1}.`;
15
+ return "Draggable item was moved to position ".concat(overIndex - 1, ".");
14
16
  }
15
- return `Draggable item was moved to position ${overIndex}.`;
17
+
18
+ return "Draggable item was moved to position ".concat(overIndex, ".");
16
19
  }
17
- return `Draggable item is no longer over a droppable area.`;
20
+
21
+ return "Draggable item is no longer over a droppable area.";
18
22
  }, [visibleItemsDictionary, dropIndicatorPosition]);
19
23
  const onDragOver = onDragMove;
20
24
  const onDragEnd = useCallback((id, overId) => {
21
25
  if (overId) {
22
26
  const overIndex = visibleItemsDictionary[overId].realIndex + 1;
27
+
23
28
  if (dropIndicatorPosition === DropIndicatorPosition.Inside) {
24
- return `Draggable item was dropped inside the item at position ${overIndex}.`;
29
+ return "Draggable item was dropped inside the item at position ".concat(overIndex, ".");
25
30
  }
31
+
26
32
  if (dropIndicatorPosition === DropIndicatorPosition.Before) {
27
- return `Draggable item was dropped over position ${overIndex - 1}.`;
33
+ return "Draggable item was dropped over position ".concat(overIndex - 1, ".");
28
34
  }
29
- return `Draggable item was dropped over position ${overIndex}.`;
35
+
36
+ return "Draggable item was dropped over position ".concat(overIndex, ".");
30
37
  }
31
- return `Draggable item was dropped at it's original position.`;
38
+
39
+ return "Draggable item was dropped at it's original position.";
32
40
  }, [dropIndicatorPosition, visibleItemsDictionary]);
33
- const onDragCancel = useCallback((id) => `Dragging was cancelled. Draggable item from position ${visibleItemsDictionary[id].realIndex + 1} was dropped at it's initial position.`, [visibleItemsDictionary]);
34
- return { onDragStart, onDragOver, onDragMove, onDragEnd, onDragCancel };
41
+ const onDragCancel = useCallback(id => "Dragging was cancelled. Draggable item from position ".concat(visibleItemsDictionary[id].realIndex + 1, " was dropped at it's initial position."), [visibleItemsDictionary]);
42
+ return {
43
+ onDragStart,
44
+ onDragOver,
45
+ onDragMove,
46
+ onDragEnd,
47
+ onDragCancel
48
+ };
35
49
  };
36
- export {
37
- useTreeAnnouncements
38
- };
39
- //# sourceMappingURL=useTreeAnnouncements.js.map
50
+
51
+ export { useTreeAnnouncements };