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