@elliemae/ds-drag-and-drop 2.0.0-alpha.12
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 +70 -0
- package/cjs/hierarchy/getHierarchyKeyboardCoordinates.js +121 -0
- package/cjs/hierarchy/index.js +9 -0
- package/cjs/hierarchy/types.js +2 -0
- package/cjs/hierarchy/useHierarchyActionHandlers.js +50 -0
- package/cjs/hierarchy/useHierarchyAnnouncements.js +36 -0
- package/cjs/hierarchy/useHierarchyDndkitConfig.js +119 -0
- package/cjs/hierarchy/useHierarchyPreviewHandlers.js +43 -0
- package/cjs/index.js +11 -0
- package/cjs/tree/constants.js +12 -0
- package/cjs/tree/customCollisionDetection.js +137 -0
- package/cjs/tree/getTreeKeyboardCoordinates.js +139 -0
- package/cjs/tree/index.js +9 -0
- package/cjs/tree/types.js +2 -0
- package/cjs/tree/useTreeActionHandlers.js +75 -0
- package/cjs/tree/useTreeAnnouncements.js +55 -0
- package/cjs/tree/useTreeDndkitConfig.js +142 -0
- package/cjs/tree/useTreePreviewHandlers.js +54 -0
- package/cjs/tree/utilities.js +52 -0
- package/esm/hierarchy/customCollisionDetection.js +66 -0
- package/esm/hierarchy/getHierarchyKeyboardCoordinates.js +113 -0
- package/esm/hierarchy/index.js +1 -0
- package/esm/hierarchy/types.js +1 -0
- package/esm/hierarchy/useHierarchyActionHandlers.js +46 -0
- package/esm/hierarchy/useHierarchyAnnouncements.js +32 -0
- package/esm/hierarchy/useHierarchyDndkitConfig.js +111 -0
- package/esm/hierarchy/useHierarchyPreviewHandlers.js +39 -0
- package/esm/index.js +2 -0
- package/esm/tree/constants.js +10 -0
- package/esm/tree/customCollisionDetection.js +133 -0
- package/esm/tree/getTreeKeyboardCoordinates.js +131 -0
- package/esm/tree/index.js +1 -0
- package/esm/tree/types.js +1 -0
- package/esm/tree/useTreeActionHandlers.js +71 -0
- package/esm/tree/useTreeAnnouncements.js +51 -0
- package/esm/tree/useTreeDndkitConfig.js +134 -0
- package/esm/tree/useTreePreviewHandlers.js +50 -0
- package/esm/tree/utilities.js +47 -0
- package/package.json +125 -0
- package/types/hierarchy/customCollisionDetection.d.ts +3 -0
- package/types/hierarchy/getHierarchyKeyboardCoordinates.d.ts +3 -0
- package/types/hierarchy/index.d.ts +1 -0
- package/types/hierarchy/types.d.ts +80 -0
- package/types/hierarchy/useHierarchyActionHandlers.d.ts +1 -0
- package/types/hierarchy/useHierarchyAnnouncements.d.ts +3 -0
- package/types/hierarchy/useHierarchyDndkitConfig.d.ts +2 -0
- package/types/hierarchy/useHierarchyPreviewHandlers.d.ts +2 -0
- package/types/index.d.ts +2 -0
- package/types/tree/constants.d.ts +6 -0
- package/types/tree/customCollisionDetection.d.ts +4 -0
- package/types/tree/getTreeKeyboardCoordinates.d.ts +3 -0
- package/types/tree/index.d.ts +1 -0
- package/types/tree/types.d.ts +98 -0
- package/types/tree/useTreeActionHandlers.d.ts +2 -0
- package/types/tree/useTreeAnnouncements.d.ts +4 -0
- package/types/tree/useTreeDndkitConfig.d.ts +2 -0
- package/types/tree/useTreePreviewHandlers.d.ts +2 -0
- package/types/tree/utilities.d.ts +7 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
8
|
+
require('core-js/modules/esnext.async-iterator.find.js');
|
|
9
|
+
require('core-js/modules/esnext.iterator.find.js');
|
|
10
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
11
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
12
|
+
|
|
13
|
+
const customCollisionDetection = (activeId, activeParent, dndItems) => _ref => {
|
|
14
|
+
var _originalContainer$re;
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
droppableContainers,
|
|
18
|
+
collisionRect
|
|
19
|
+
} = _ref;
|
|
20
|
+
const entriesWithSameParent = droppableContainers.filter(_ref2 => {
|
|
21
|
+
var _dndItems$find;
|
|
22
|
+
|
|
23
|
+
let {
|
|
24
|
+
id
|
|
25
|
+
} = _ref2;
|
|
26
|
+
return ((_dndItems$find = dndItems.find(item => item.id === id)) === null || _dndItems$find === void 0 ? void 0 : _dndItems$find.parentId) === activeParent;
|
|
27
|
+
});
|
|
28
|
+
const originalContainer = entriesWithSameParent.find(_ref3 => {
|
|
29
|
+
let {
|
|
30
|
+
id
|
|
31
|
+
} = _ref3;
|
|
32
|
+
return id === activeId;
|
|
33
|
+
});
|
|
34
|
+
const originalRect = originalContainer === null || originalContainer === void 0 ? void 0 : (_originalContainer$re = originalContainer.rect) === null || _originalContainer$re === void 0 ? void 0 : _originalContainer$re.current;
|
|
35
|
+
if (!originalRect) return null;
|
|
36
|
+
|
|
37
|
+
if (originalRect.offsetLeft <= collisionRect.offsetLeft && collisionRect.offsetLeft <= originalRect.offsetLeft + originalRect.width) {
|
|
38
|
+
return originalContainer.id;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isLeft = collisionRect.offsetLeft <= originalRect.offsetLeft + collisionRect.width / 2 + 5;
|
|
42
|
+
let match = null; // If going to the left, search the closest offset left
|
|
43
|
+
// Otherwise, get the closest offset right
|
|
44
|
+
|
|
45
|
+
const collisionPoint = collisionRect.offsetLeft + collisionRect.width / 2;
|
|
46
|
+
entriesWithSameParent.forEach(entry => {
|
|
47
|
+
const {
|
|
48
|
+
rect: {
|
|
49
|
+
current: rect
|
|
50
|
+
}
|
|
51
|
+
} = entry;
|
|
52
|
+
|
|
53
|
+
if (rect !== null) {
|
|
54
|
+
const offsetRight = rect.offsetLeft + rect.width;
|
|
55
|
+
|
|
56
|
+
if (!match) {
|
|
57
|
+
match = entry;
|
|
58
|
+
} else if (match && match.rect.current && isLeft && Math.abs(collisionPoint - rect.offsetLeft) <= Math.abs(collisionPoint - match.rect.current.offsetLeft)) {
|
|
59
|
+
match = entry;
|
|
60
|
+
} else if (match && match.rect.current && !isLeft && Math.abs(collisionPoint - offsetRight) <= Math.abs(collisionPoint - match.rect.current.offsetLeft - match.rect.current.width)) {
|
|
61
|
+
match = entry;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}); // Typescript believes match is always null for some reason
|
|
65
|
+
|
|
66
|
+
const matchCastedForSomeReason = match;
|
|
67
|
+
return matchCastedForSomeReason === null ? null : matchCastedForSomeReason.id;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
exports.customCollisionDetection = customCollisionDetection;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
6
|
+
require('core-js/modules/esnext.async-iterator.find.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
8
|
+
require('core-js/modules/esnext.iterator.find.js');
|
|
9
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
10
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
11
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
12
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
13
|
+
var core = require('@dnd-kit/core');
|
|
14
|
+
|
|
15
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
|
+
|
|
17
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
18
|
+
|
|
19
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
20
|
+
|
|
21
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
22
|
+
const directions = [core.KeyboardCode.Down, core.KeyboardCode.Right, core.KeyboardCode.Up, core.KeyboardCode.Left];
|
|
23
|
+
const vertical = [core.KeyboardCode.Up, core.KeyboardCode.Down];
|
|
24
|
+
|
|
25
|
+
const getHorizontalKeyboardCoordinates = _ref => {
|
|
26
|
+
var _droppableContainers$, _droppableContainers$2, _droppableContainers$3, _droppableContainers$4;
|
|
27
|
+
|
|
28
|
+
let {
|
|
29
|
+
items,
|
|
30
|
+
active,
|
|
31
|
+
over,
|
|
32
|
+
event,
|
|
33
|
+
currentCoordinates,
|
|
34
|
+
droppableContainers,
|
|
35
|
+
collisionRect
|
|
36
|
+
} = _ref;
|
|
37
|
+
if (vertical.includes(event.code) || !active || !over) return undefined;
|
|
38
|
+
const activeItem = items.find(item => item.id === active.id);
|
|
39
|
+
const overRect = over.rect;
|
|
40
|
+
const layoutRects = []; // Get the reacheable rects depending on the arrow key pressed
|
|
41
|
+
|
|
42
|
+
droppableContainers.forEach(container => {
|
|
43
|
+
var _items$find;
|
|
44
|
+
|
|
45
|
+
if (container !== null && container !== void 0 && container.disabled || !overRect) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const node = container === null || container === void 0 ? void 0 : container.node.current;
|
|
50
|
+
|
|
51
|
+
if (!node) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const itemParent = (_items$find = items.find(item => item.id === container.id)) === null || _items$find === void 0 ? void 0 : _items$find.parentId;
|
|
56
|
+
if (itemParent !== (activeItem === null || activeItem === void 0 ? void 0 : activeItem.parentId)) return;
|
|
57
|
+
const rect = core.getViewRect(node);
|
|
58
|
+
|
|
59
|
+
if (event.code === core.KeyboardCode.Left && collisionRect.left > rect.right - 10) {
|
|
60
|
+
layoutRects.push(container);
|
|
61
|
+
} else if (event.code === core.KeyboardCode.Right && collisionRect.left < rect.left) {
|
|
62
|
+
layoutRects.push(container);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
const closestId = core.closestCorners({
|
|
66
|
+
droppableContainers: layoutRects,
|
|
67
|
+
collisionRect,
|
|
68
|
+
active
|
|
69
|
+
});
|
|
70
|
+
if (!closestId) return undefined;
|
|
71
|
+
const closestDroppableContainer = (_droppableContainers$ = droppableContainers.get(closestId)) === null || _droppableContainers$ === void 0 ? void 0 : (_droppableContainers$2 = _droppableContainers$.node) === null || _droppableContainers$2 === void 0 ? void 0 : _droppableContainers$2.current;
|
|
72
|
+
const activeDroppableContainer = (_droppableContainers$3 = droppableContainers.get(active === null || active === void 0 ? void 0 : active.id)) === null || _droppableContainers$3 === void 0 ? void 0 : (_droppableContainers$4 = _droppableContainers$3.node) === null || _droppableContainers$4 === void 0 ? void 0 : _droppableContainers$4.current;
|
|
73
|
+
if (!closestDroppableContainer || !activeDroppableContainer) return undefined;
|
|
74
|
+
const closestRect = core.getViewRect(closestDroppableContainer);
|
|
75
|
+
const activeRect = core.getViewRect(activeDroppableContainer);
|
|
76
|
+
return _objectSpread(_objectSpread({}, currentCoordinates), {}, {
|
|
77
|
+
x: closestRect.left + (closestRect.width - collisionRect.width) / 2 + (closestRect.offsetLeft < activeRect.left ? -1 : 1)
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const getHierarchyKeyboardCoordinates = (context, dragOverlayDataTestid, isHorizontalDnD) => (event, _ref2) => {
|
|
82
|
+
let {
|
|
83
|
+
currentCoordinates,
|
|
84
|
+
context: {
|
|
85
|
+
active,
|
|
86
|
+
over,
|
|
87
|
+
collisionRect,
|
|
88
|
+
droppableContainers
|
|
89
|
+
}
|
|
90
|
+
} = _ref2;
|
|
91
|
+
|
|
92
|
+
if (directions.includes(event.code)) {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
|
|
95
|
+
if (!collisionRect) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const {
|
|
100
|
+
current: {
|
|
101
|
+
items
|
|
102
|
+
}
|
|
103
|
+
} = context;
|
|
104
|
+
const args = {
|
|
105
|
+
dragOverlayDataTestid,
|
|
106
|
+
items,
|
|
107
|
+
active,
|
|
108
|
+
over,
|
|
109
|
+
event,
|
|
110
|
+
currentCoordinates,
|
|
111
|
+
droppableContainers,
|
|
112
|
+
collisionRect
|
|
113
|
+
};
|
|
114
|
+
if (isHorizontalDnD) return getHorizontalKeyboardCoordinates(args);
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return undefined;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
exports.getHierarchyKeyboardCoordinates = getHierarchyKeyboardCoordinates;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
const useHierarchyActionHandlers = _ref => {
|
|
8
|
+
let {
|
|
9
|
+
handlePreviewDragStart,
|
|
10
|
+
handlePreviewDragMove,
|
|
11
|
+
handlePreviewDragEnd,
|
|
12
|
+
handlePreviewDragCancel,
|
|
13
|
+
onReorder,
|
|
14
|
+
dndItems
|
|
15
|
+
} = _ref;
|
|
16
|
+
const onDragStart = react.useCallback(e => {
|
|
17
|
+
handlePreviewDragStart(e);
|
|
18
|
+
}, [handlePreviewDragStart]);
|
|
19
|
+
const onDragMove = react.useCallback(e => {
|
|
20
|
+
handlePreviewDragMove(e);
|
|
21
|
+
}, [handlePreviewDragMove]);
|
|
22
|
+
const onDragEnd = react.useCallback(e => {
|
|
23
|
+
handlePreviewDragEnd(e);
|
|
24
|
+
const {
|
|
25
|
+
active,
|
|
26
|
+
over
|
|
27
|
+
} = e;
|
|
28
|
+
if (!over) return;
|
|
29
|
+
|
|
30
|
+
if (active.id !== over.id) {
|
|
31
|
+
const activeIndex = dndItems.findIndex(item => item.id === active.id);
|
|
32
|
+
const overIndex = dndItems.findIndex(item => item.id === over.id);
|
|
33
|
+
onReorder(dndItems[activeIndex], {
|
|
34
|
+
targetIndex: overIndex,
|
|
35
|
+
fromIndex: activeIndex
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}, [handlePreviewDragEnd, dndItems, onReorder]);
|
|
39
|
+
const onDragCancel = react.useCallback(e => {
|
|
40
|
+
handlePreviewDragCancel(e);
|
|
41
|
+
}, [handlePreviewDragCancel]);
|
|
42
|
+
return {
|
|
43
|
+
onDragStart,
|
|
44
|
+
onDragMove,
|
|
45
|
+
onDragEnd,
|
|
46
|
+
onDragCancel
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
exports.useHierarchyActionHandlers = useHierarchyActionHandlers;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
const useHierarchyAnnouncements = visibleItemsDictionary => {
|
|
8
|
+
const onDragStart = react.useCallback(id => "Picked up draggable item from position ".concat(visibleItemsDictionary[id].index + 1, "."), [visibleItemsDictionary]);
|
|
9
|
+
const onDragMove = react.useCallback((id, overId) => {
|
|
10
|
+
if (overId) {
|
|
11
|
+
const overIndex = visibleItemsDictionary[overId].index + 1;
|
|
12
|
+
return "Draggable item was moved to position ".concat(overIndex, ".");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return "Draggable item is no longer over a droppable area.";
|
|
16
|
+
}, [visibleItemsDictionary]);
|
|
17
|
+
const onDragOver = onDragMove;
|
|
18
|
+
const onDragEnd = react.useCallback((id, overId) => {
|
|
19
|
+
if (overId) {
|
|
20
|
+
const overIndex = visibleItemsDictionary[overId].index + 1;
|
|
21
|
+
return "Draggable item was dropped over position ".concat(overIndex, ".");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return "Draggable item was dropped at it's original position.";
|
|
25
|
+
}, [visibleItemsDictionary]);
|
|
26
|
+
const onDragCancel = react.useCallback(id => "Dragging was cancelled. Draggable item from position ".concat(visibleItemsDictionary[id].index + 1, " was dropped at it's initial position."), [visibleItemsDictionary]);
|
|
27
|
+
return {
|
|
28
|
+
onDragStart,
|
|
29
|
+
onDragOver,
|
|
30
|
+
onDragMove,
|
|
31
|
+
onDragEnd,
|
|
32
|
+
onDragCancel
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.useHierarchyAnnouncements = useHierarchyAnnouncements;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
6
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
7
|
+
require('core-js/modules/esnext.async-iterator.map.js');
|
|
8
|
+
require('core-js/modules/esnext.iterator.map.js');
|
|
9
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
10
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
11
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
12
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
13
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
14
|
+
var react = require('react');
|
|
15
|
+
var core = require('@dnd-kit/core');
|
|
16
|
+
var useHierarchyActionHandlers = require('./useHierarchyActionHandlers.js');
|
|
17
|
+
var useHierarchyPreviewHandlers = require('./useHierarchyPreviewHandlers.js');
|
|
18
|
+
var getHierarchyKeyboardCoordinates = require('./getHierarchyKeyboardCoordinates.js');
|
|
19
|
+
var customCollisionDetection = require('./customCollisionDetection.js');
|
|
20
|
+
var useHierarchyAnnouncements = require('./useHierarchyAnnouncements.js');
|
|
21
|
+
|
|
22
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
23
|
+
|
|
24
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
25
|
+
|
|
26
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
27
|
+
|
|
28
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
29
|
+
// if second parameter is true, the space will be done on the horizontal axis
|
|
30
|
+
|
|
31
|
+
const adjustTranslate = isHorizontalDnD => _ref => {
|
|
32
|
+
let {
|
|
33
|
+
transform
|
|
34
|
+
} = _ref;
|
|
35
|
+
|
|
36
|
+
const newTransform = _objectSpread({}, transform);
|
|
37
|
+
|
|
38
|
+
if (isHorizontalDnD) {
|
|
39
|
+
newTransform.y = transform.y + 15;
|
|
40
|
+
} else {
|
|
41
|
+
newTransform.y = transform.y - 35;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return newTransform;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const measuring = {
|
|
48
|
+
droppable: {
|
|
49
|
+
strategy: core.MeasuringStrategy.Always
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const noop = () => null;
|
|
54
|
+
|
|
55
|
+
const useHierarchyDndkitConfig = _ref2 => {
|
|
56
|
+
var _itemsDictionary$acti;
|
|
57
|
+
|
|
58
|
+
let {
|
|
59
|
+
flattenedItems,
|
|
60
|
+
dragOverlayDataTestid,
|
|
61
|
+
isHorizontalDnD = false,
|
|
62
|
+
onReorder,
|
|
63
|
+
onPreviewResetState = noop,
|
|
64
|
+
onPreviewDragStart = noop
|
|
65
|
+
} = _ref2;
|
|
66
|
+
const [activeId, setActiveId] = react.useState('');
|
|
67
|
+
const dndItems = react.useMemo(() => flattenedItems.map(_ref3 => {
|
|
68
|
+
let [item] = _ref3;
|
|
69
|
+
return item;
|
|
70
|
+
}), [flattenedItems]);
|
|
71
|
+
const itemsDictionary = react.useMemo(() => {
|
|
72
|
+
const dictionary = {};
|
|
73
|
+
dndItems.forEach(item => {
|
|
74
|
+
dictionary[item.id] = item;
|
|
75
|
+
});
|
|
76
|
+
return dictionary;
|
|
77
|
+
}, [dndItems]);
|
|
78
|
+
const modifiers = react.useMemo(() => [adjustTranslate(isHorizontalDnD)], [isHorizontalDnD]);
|
|
79
|
+
const sensorContext = react.useRef({
|
|
80
|
+
items: dndItems
|
|
81
|
+
});
|
|
82
|
+
react.useEffect(() => {
|
|
83
|
+
sensorContext.current = {
|
|
84
|
+
items: dndItems
|
|
85
|
+
};
|
|
86
|
+
}, [dndItems]);
|
|
87
|
+
const coordinateGetter = react.useMemo(() => getHierarchyKeyboardCoordinates.getHierarchyKeyboardCoordinates(sensorContext, dragOverlayDataTestid, isHorizontalDnD), [isHorizontalDnD]);
|
|
88
|
+
const sensors = core.useSensors(core.useSensor(core.PointerSensor), core.useSensor(core.KeyboardSensor, {
|
|
89
|
+
coordinateGetter
|
|
90
|
+
}));
|
|
91
|
+
const dragPreviewHandlers = useHierarchyPreviewHandlers.useHierarchyPreviewHandlers({
|
|
92
|
+
setActiveId,
|
|
93
|
+
onPreviewResetState,
|
|
94
|
+
onPreviewDragStart
|
|
95
|
+
});
|
|
96
|
+
const dragActionHandlers = useHierarchyActionHandlers.useHierarchyActionHandlers(_objectSpread(_objectSpread({}, dragPreviewHandlers), {}, {
|
|
97
|
+
dndItems,
|
|
98
|
+
onReorder
|
|
99
|
+
}));
|
|
100
|
+
const announcements = useHierarchyAnnouncements.useHierarchyAnnouncements(itemsDictionary);
|
|
101
|
+
const dndContextProps = react.useMemo(() => {
|
|
102
|
+
var _itemsDictionary;
|
|
103
|
+
|
|
104
|
+
return _objectSpread({
|
|
105
|
+
announcements,
|
|
106
|
+
modifiers,
|
|
107
|
+
sensors,
|
|
108
|
+
measuring,
|
|
109
|
+
collisionDetection: customCollisionDetection.customCollisionDetection(activeId, (_itemsDictionary = itemsDictionary[activeId !== null && activeId !== void 0 ? activeId : '']) === null || _itemsDictionary === void 0 ? void 0 : _itemsDictionary.parentId, dndItems)
|
|
110
|
+
}, dragActionHandlers);
|
|
111
|
+
}, [announcements, modifiers, sensors, activeId, itemsDictionary, dndItems, dragActionHandlers]);
|
|
112
|
+
return {
|
|
113
|
+
dndContextProps,
|
|
114
|
+
activeId,
|
|
115
|
+
activeIndex: activeId ? (_itemsDictionary$acti = itemsDictionary[activeId]) === null || _itemsDictionary$acti === void 0 ? void 0 : _itemsDictionary$acti.index : -1
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
exports.useHierarchyDndkitConfig = useHierarchyDndkitConfig;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
const useHierarchyPreviewHandlers = _ref => {
|
|
8
|
+
let {
|
|
9
|
+
setActiveId,
|
|
10
|
+
onPreviewResetState,
|
|
11
|
+
onPreviewDragStart
|
|
12
|
+
} = _ref;
|
|
13
|
+
const resetState = react.useCallback(() => {
|
|
14
|
+
setActiveId('');
|
|
15
|
+
onPreviewResetState();
|
|
16
|
+
document.body.style.setProperty('cursor', '');
|
|
17
|
+
}, [setActiveId, onPreviewResetState]);
|
|
18
|
+
const handlePreviewDragStart = react.useCallback(_ref2 => {
|
|
19
|
+
let {
|
|
20
|
+
active: {
|
|
21
|
+
id
|
|
22
|
+
}
|
|
23
|
+
} = _ref2;
|
|
24
|
+
setActiveId(id);
|
|
25
|
+
document.body.style.setProperty('cursor', 'grabbing');
|
|
26
|
+
onPreviewDragStart();
|
|
27
|
+
}, [setActiveId, onPreviewDragStart]);
|
|
28
|
+
const handlePreviewDragMove = react.useCallback(() => null, []);
|
|
29
|
+
const handlePreviewDragEnd = react.useCallback(() => {
|
|
30
|
+
resetState();
|
|
31
|
+
}, [resetState]);
|
|
32
|
+
const handlePreviewDragCancel = react.useCallback(() => {
|
|
33
|
+
resetState();
|
|
34
|
+
}, [resetState]);
|
|
35
|
+
return {
|
|
36
|
+
handlePreviewDragStart,
|
|
37
|
+
handlePreviewDragMove,
|
|
38
|
+
handlePreviewDragEnd,
|
|
39
|
+
handlePreviewDragCancel
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
exports.useHierarchyPreviewHandlers = useHierarchyPreviewHandlers;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var useHierarchyDndkitConfig = require('./hierarchy/useHierarchyDndkitConfig.js');
|
|
6
|
+
var useTreeDndkitConfig = require('./tree/useTreeDndkitConfig.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
exports.useHierarchyDndkitConfig = useHierarchyDndkitConfig.useHierarchyDndkitConfig;
|
|
11
|
+
exports.useTreeDndkitConfig = useTreeDndkitConfig.useTreeDndkitConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
exports.DropIndicatorPosition = void 0;
|
|
6
|
+
|
|
7
|
+
(function (DropIndicatorPosition) {
|
|
8
|
+
DropIndicatorPosition[DropIndicatorPosition["None"] = 0] = "None";
|
|
9
|
+
DropIndicatorPosition[DropIndicatorPosition["Before"] = 1] = "Before";
|
|
10
|
+
DropIndicatorPosition[DropIndicatorPosition["After"] = 2] = "After";
|
|
11
|
+
DropIndicatorPosition[DropIndicatorPosition["Inside"] = 3] = "Inside";
|
|
12
|
+
})(exports.DropIndicatorPosition || (exports.DropIndicatorPosition = {}));
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('core-js/modules/esnext.async-iterator.find.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.find.js');
|
|
8
|
+
require('core-js/modules/esnext.async-iterator.reduce.js');
|
|
9
|
+
require('core-js/modules/esnext.iterator.reduce.js');
|
|
10
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
11
|
+
var constants = require('./constants.js');
|
|
12
|
+
|
|
13
|
+
const DUMMY_ID = 'DUMMY_ID_INTERNAL_USE_ONLY';
|
|
14
|
+
const RECT_UP = {
|
|
15
|
+
offsetTop: -Infinity,
|
|
16
|
+
offsetLeft: 0,
|
|
17
|
+
// Doesn't matter,
|
|
18
|
+
width: 0,
|
|
19
|
+
// Doesn't matter
|
|
20
|
+
height: 0 // Doesn't matter
|
|
21
|
+
|
|
22
|
+
};
|
|
23
|
+
const RECT_DOWN = {
|
|
24
|
+
offsetTop: Infinity,
|
|
25
|
+
offsetLeft: 0,
|
|
26
|
+
// Doesn't matter,
|
|
27
|
+
width: 0,
|
|
28
|
+
// Doesn't matter
|
|
29
|
+
height: 0 // Doesn't matter
|
|
30
|
+
|
|
31
|
+
}; // Percentage of height to take into consideration when looking for colliding rects
|
|
32
|
+
|
|
33
|
+
const thresholdRatio = 0.2; // Percentage to be inside
|
|
34
|
+
|
|
35
|
+
const insideThreshold = 0.7;
|
|
36
|
+
const customCollisionDetection = (activeId, visibleItemsDictionary, setDropIndicatorPosition, maxDragAndDropLevel, lastPosition, setLastPosition) => _ref => {
|
|
37
|
+
var _originalContainer$re;
|
|
38
|
+
|
|
39
|
+
let {
|
|
40
|
+
droppableContainers,
|
|
41
|
+
collisionRect
|
|
42
|
+
} = _ref;
|
|
43
|
+
const originalContainer = droppableContainers.find(_ref2 => {
|
|
44
|
+
let {
|
|
45
|
+
id
|
|
46
|
+
} = _ref2;
|
|
47
|
+
return id === activeId;
|
|
48
|
+
});
|
|
49
|
+
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
|
|
50
|
+
// This modifies how to search the matching colliding rect
|
|
51
|
+
|
|
52
|
+
let isUp = lastPosition === 'up';
|
|
53
|
+
|
|
54
|
+
if (originalRect) {
|
|
55
|
+
isUp = originalRect.offsetTop > collisionRect.top;
|
|
56
|
+
} // Threshold
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const threshold = collisionRect.height * thresholdRatio;
|
|
60
|
+
let collidingContainer = null;
|
|
61
|
+
|
|
62
|
+
if (isUp) {
|
|
63
|
+
// Up -- We need to find the first rectangle downwards
|
|
64
|
+
collidingContainer = droppableContainers.reduce((firstRectDown, container) => {
|
|
65
|
+
const rect = container.rect.current;
|
|
66
|
+
|
|
67
|
+
if (rect && firstRectDown.rect.current) {
|
|
68
|
+
const {
|
|
69
|
+
offsetTop: rectOffsetTop
|
|
70
|
+
} = rect;
|
|
71
|
+
const {
|
|
72
|
+
offsetTop: firstRectDownOffsetTop
|
|
73
|
+
} = firstRectDown.rect.current;
|
|
74
|
+
|
|
75
|
+
if (rectOffsetTop + threshold > collisionRect.top && rectOffsetTop < firstRectDownOffsetTop) {
|
|
76
|
+
return container;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return firstRectDown;
|
|
81
|
+
}, {
|
|
82
|
+
id: DUMMY_ID,
|
|
83
|
+
rect: {
|
|
84
|
+
current: RECT_DOWN
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
// Down -- We need to find the first rectangle upwards
|
|
89
|
+
collidingContainer = droppableContainers.reduce((firstRectUp, container) => {
|
|
90
|
+
const rect = container.rect.current;
|
|
91
|
+
|
|
92
|
+
if (rect && firstRectUp.rect.current) {
|
|
93
|
+
const {
|
|
94
|
+
offsetTop: rectOffsetTop
|
|
95
|
+
} = rect;
|
|
96
|
+
const {
|
|
97
|
+
offsetTop: firstRectUpOffsetTop
|
|
98
|
+
} = firstRectUp.rect.current;
|
|
99
|
+
|
|
100
|
+
if (rectOffsetTop - threshold < collisionRect.top && rectOffsetTop > firstRectUpOffsetTop) {
|
|
101
|
+
return container;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return firstRectUp;
|
|
106
|
+
}, {
|
|
107
|
+
id: DUMMY_ID,
|
|
108
|
+
rect: {
|
|
109
|
+
current: RECT_UP
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
} // If we didn't find a match, return null
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if (collidingContainer.id === DUMMY_ID) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const collidingRect = collidingContainer.rect.current;
|
|
120
|
+
if (!collidingRect) return null; // Calculate the intersection interval
|
|
121
|
+
|
|
122
|
+
const [top, bottom] = [Math.max(collisionRect.top, collidingRect.offsetTop), Math.min(collisionRect.bottom, collidingRect.offsetTop + collidingRect.height)]; // Calculate the percentage of intersection
|
|
123
|
+
|
|
124
|
+
const intersectionPercentage = Math.abs(bottom - top) / collidingRect.height;
|
|
125
|
+
|
|
126
|
+
if (intersectionPercentage > insideThreshold && visibleItemsDictionary[collidingContainer.id].depth + 1 <= maxDragAndDropLevel && collidingContainer.id !== activeId) {
|
|
127
|
+
setDropIndicatorPosition(constants.DropIndicatorPosition.Inside);
|
|
128
|
+
} else {
|
|
129
|
+
setDropIndicatorPosition(isUp ? constants.DropIndicatorPosition.Before : constants.DropIndicatorPosition.After);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isUp && lastPosition !== 'up') setLastPosition('up');else if (!isUp && lastPosition !== 'down') setLastPosition('down'); // Return the id of the match rectangle
|
|
133
|
+
|
|
134
|
+
return collidingContainer.id;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
exports.customCollisionDetection = customCollisionDetection;
|