@os-design/drag-sort 1.0.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 (135) hide show
  1. package/LICENCE.md +1 -0
  2. package/README.md +15 -0
  3. package/dist/cjs/DragAndDrop.js +197 -0
  4. package/dist/cjs/DragAndDrop.js.map +1 -0
  5. package/dist/cjs/Draggable.js +97 -0
  6. package/dist/cjs/Draggable.js.map +1 -0
  7. package/dist/cjs/Droppable.js +112 -0
  8. package/dist/cjs/Droppable.js.map +1 -0
  9. package/dist/cjs/index.js +77 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/utils/ListStore.js +68 -0
  12. package/dist/cjs/utils/ListStore.js.map +1 -0
  13. package/dist/cjs/utils/NodeList.js +266 -0
  14. package/dist/cjs/utils/NodeList.js.map +1 -0
  15. package/dist/cjs/utils/getElementOffset.js +27 -0
  16. package/dist/cjs/utils/getElementOffset.js.map +1 -0
  17. package/dist/cjs/utils/getElementScroll.js +27 -0
  18. package/dist/cjs/utils/getElementScroll.js.map +1 -0
  19. package/dist/cjs/utils/getNodeRect.js +48 -0
  20. package/dist/cjs/utils/getNodeRect.js.map +1 -0
  21. package/dist/cjs/utils/useAppendClassName.js +27 -0
  22. package/dist/cjs/utils/useAppendClassName.js.map +1 -0
  23. package/dist/cjs/utils/useBlankNode.js +103 -0
  24. package/dist/cjs/utils/useBlankNode.js.map +1 -0
  25. package/dist/cjs/utils/useDragAndDrop.js +32 -0
  26. package/dist/cjs/utils/useDragAndDrop.js.map +1 -0
  27. package/dist/cjs/utils/useDragEffect.js +509 -0
  28. package/dist/cjs/utils/useDragEffect.js.map +1 -0
  29. package/dist/cjs/utils/useDroppable.js +35 -0
  30. package/dist/cjs/utils/useDroppable.js.map +1 -0
  31. package/dist/cjs/utils/useGeneratedId.js +22 -0
  32. package/dist/cjs/utils/useGeneratedId.js.map +1 -0
  33. package/dist/cjs/utils/useGetNodeStyle.js +28 -0
  34. package/dist/cjs/utils/useGetNodeStyle.js.map +1 -0
  35. package/dist/cjs/utils/useInitRect.js +25 -0
  36. package/dist/cjs/utils/useInitRect.js.map +1 -0
  37. package/dist/cjs/utils/useInitScrollOffset.js +27 -0
  38. package/dist/cjs/utils/useInitScrollOffset.js.map +1 -0
  39. package/dist/cjs/utils/useMoveNode.js +110 -0
  40. package/dist/cjs/utils/useMoveNode.js.map +1 -0
  41. package/dist/cjs/utils/useScrollEventByPoint.js +66 -0
  42. package/dist/cjs/utils/useScrollEventByPoint.js.map +1 -0
  43. package/dist/cjs/utils/useTargetList.js +58 -0
  44. package/dist/cjs/utils/useTargetList.js.map +1 -0
  45. package/dist/cjs/utils/useTransitionStyle.js +30 -0
  46. package/dist/cjs/utils/useTransitionStyle.js.map +1 -0
  47. package/dist/esm/DragAndDrop.js +139 -0
  48. package/dist/esm/DragAndDrop.js.map +1 -0
  49. package/dist/esm/Draggable.js +62 -0
  50. package/dist/esm/Draggable.js.map +1 -0
  51. package/dist/esm/Droppable.js +84 -0
  52. package/dist/esm/Droppable.js.map +1 -0
  53. package/dist/esm/index.js +7 -0
  54. package/dist/esm/index.js.map +1 -0
  55. package/dist/esm/utils/ListStore.js +46 -0
  56. package/dist/esm/utils/ListStore.js.map +1 -0
  57. package/dist/esm/utils/NodeList.js +207 -0
  58. package/dist/esm/utils/NodeList.js.map +1 -0
  59. package/dist/esm/utils/getElementOffset.js +19 -0
  60. package/dist/esm/utils/getElementOffset.js.map +1 -0
  61. package/dist/esm/utils/getElementScroll.js +19 -0
  62. package/dist/esm/utils/getElementScroll.js.map +1 -0
  63. package/dist/esm/utils/getNodeRect.js +36 -0
  64. package/dist/esm/utils/getNodeRect.js.map +1 -0
  65. package/dist/esm/utils/useAppendClassName.js +19 -0
  66. package/dist/esm/utils/useAppendClassName.js.map +1 -0
  67. package/dist/esm/utils/useBlankNode.js +81 -0
  68. package/dist/esm/utils/useBlankNode.js.map +1 -0
  69. package/dist/esm/utils/useDragAndDrop.js +13 -0
  70. package/dist/esm/utils/useDragAndDrop.js.map +1 -0
  71. package/dist/esm/utils/useDragEffect.js +417 -0
  72. package/dist/esm/utils/useDragEffect.js.map +1 -0
  73. package/dist/esm/utils/useDroppable.js +14 -0
  74. package/dist/esm/utils/useDroppable.js.map +1 -0
  75. package/dist/esm/utils/useGeneratedId.js +7 -0
  76. package/dist/esm/utils/useGeneratedId.js.map +1 -0
  77. package/dist/esm/utils/useGetNodeStyle.js +20 -0
  78. package/dist/esm/utils/useGetNodeStyle.js.map +1 -0
  79. package/dist/esm/utils/useInitRect.js +15 -0
  80. package/dist/esm/utils/useInitRect.js.map +1 -0
  81. package/dist/esm/utils/useInitScrollOffset.js +14 -0
  82. package/dist/esm/utils/useInitScrollOffset.js.map +1 -0
  83. package/dist/esm/utils/useMoveNode.js +62 -0
  84. package/dist/esm/utils/useMoveNode.js.map +1 -0
  85. package/dist/esm/utils/useScrollEventByPoint.js +53 -0
  86. package/dist/esm/utils/useScrollEventByPoint.js.map +1 -0
  87. package/dist/esm/utils/useTargetList.js +27 -0
  88. package/dist/esm/utils/useTargetList.js.map +1 -0
  89. package/dist/esm/utils/useTransitionStyle.js +24 -0
  90. package/dist/esm/utils/useTransitionStyle.js.map +1 -0
  91. package/dist/types/DragAndDrop.d.ts +16 -0
  92. package/dist/types/DragAndDrop.d.ts.map +1 -0
  93. package/dist/types/Draggable.d.ts +17 -0
  94. package/dist/types/Draggable.d.ts.map +1 -0
  95. package/dist/types/Droppable.d.ts +15 -0
  96. package/dist/types/Droppable.d.ts.map +1 -0
  97. package/dist/types/index.d.ts +7 -0
  98. package/dist/types/index.d.ts.map +1 -0
  99. package/dist/types/utils/ListStore.d.ts +10 -0
  100. package/dist/types/utils/ListStore.d.ts.map +1 -0
  101. package/dist/types/utils/NodeList.d.ts +42 -0
  102. package/dist/types/utils/NodeList.d.ts.map +1 -0
  103. package/dist/types/utils/getElementOffset.d.ts +6 -0
  104. package/dist/types/utils/getElementOffset.d.ts.map +1 -0
  105. package/dist/types/utils/getElementScroll.d.ts +6 -0
  106. package/dist/types/utils/getElementScroll.d.ts.map +1 -0
  107. package/dist/types/utils/getNodeRect.d.ts +9 -0
  108. package/dist/types/utils/getNodeRect.d.ts.map +1 -0
  109. package/dist/types/utils/useAppendClassName.d.ts +4 -0
  110. package/dist/types/utils/useAppendClassName.d.ts.map +1 -0
  111. package/dist/types/utils/useBlankNode.d.ts +14 -0
  112. package/dist/types/utils/useBlankNode.d.ts.map +1 -0
  113. package/dist/types/utils/useDragAndDrop.d.ts +20 -0
  114. package/dist/types/utils/useDragAndDrop.d.ts.map +1 -0
  115. package/dist/types/utils/useDragEffect.d.ts +23 -0
  116. package/dist/types/utils/useDragEffect.d.ts.map +1 -0
  117. package/dist/types/utils/useDroppable.d.ts +12 -0
  118. package/dist/types/utils/useDroppable.d.ts.map +1 -0
  119. package/dist/types/utils/useGeneratedId.d.ts +3 -0
  120. package/dist/types/utils/useGeneratedId.d.ts.map +1 -0
  121. package/dist/types/utils/useGetNodeStyle.d.ts +9 -0
  122. package/dist/types/utils/useGetNodeStyle.d.ts.map +1 -0
  123. package/dist/types/utils/useInitRect.d.ts +9 -0
  124. package/dist/types/utils/useInitRect.d.ts.map +1 -0
  125. package/dist/types/utils/useInitScrollOffset.d.ts +7 -0
  126. package/dist/types/utils/useInitScrollOffset.d.ts.map +1 -0
  127. package/dist/types/utils/useMoveNode.d.ts +15 -0
  128. package/dist/types/utils/useMoveNode.d.ts.map +1 -0
  129. package/dist/types/utils/useScrollEventByPoint.d.ts +7 -0
  130. package/dist/types/utils/useScrollEventByPoint.d.ts.map +1 -0
  131. package/dist/types/utils/useTargetList.d.ts +6 -0
  132. package/dist/types/utils/useTargetList.d.ts.map +1 -0
  133. package/dist/types/utils/useTransitionStyle.d.ts +8 -0
  134. package/dist/types/utils/useTransitionStyle.d.ts.map +1 -0
  135. package/package.json +49 -0
@@ -0,0 +1,417 @@
1
+ import { useCallback, useEffect, useMemo, useRef } from 'react';
2
+ import useThrottle from '@os-design/use-throttle';
3
+ import useEvent from '@os-design/use-event';
4
+ import getElementScroll from './getElementScroll';
5
+ import useTargetList from './useTargetList';
6
+ import useInitRect from './useInitRect';
7
+ import useInitScrollOffset from './useInitScrollOffset';
8
+ import useGetNodeStyle from './useGetNodeStyle'; // eslint-disable-next-line import/no-cycle
9
+
10
+ import useMoveNode from './useMoveNode';
11
+ import getNodeRect from './getNodeRect'; // eslint-disable-next-line import/no-cycle
12
+
13
+ import useBlankNode from './useBlankNode';
14
+ const HIDDEN_NODE_STYLE = {
15
+ opacity: 0,
16
+ pointerEvents: 'none'
17
+ };
18
+
19
+ const createEmptyNode = ({
20
+ prev = null,
21
+ next = null
22
+ }) => [prev, next, {
23
+ current: null
24
+ }, () => {}, -1];
25
+ /* eslint-disable @typescript-eslint/no-explicit-any,no-constant-condition */
26
+
27
+
28
+ const useDragEffect = props => {
29
+ const {
30
+ draggedNode,
31
+ cursorPosition,
32
+ listStoreRef,
33
+ onDragEnd
34
+ } = props;
35
+ const targetRef = useRef(null);
36
+ const targetNodeRef = useRef(draggedNode);
37
+ useEffect(() => {
38
+ targetNodeRef.current = draggedNode;
39
+ }, [draggedNode]);
40
+ const onDragEndRef = useRef(onDragEnd);
41
+ useEffect(() => {
42
+ onDragEndRef.current = onDragEnd;
43
+ }, [onDragEnd]); // The initial bounds of the dragged node.
44
+ // We can't read the bounds of the dragged node on the fly because the node can be unmounted in the virtual list.
45
+
46
+ const initDraggedNodeRect = useInitRect(draggedNode?.node[2]); // The initial scroll position of the list where the dragged node is located.
47
+ // Used to detect the actual position of the dragged node.
48
+ // The purpose is the same as with initDraggedNodeRect.
49
+
50
+ const initDraggedNodeListScrollOffset = useInitScrollOffset(draggedNode?.list.ref); // The central position of the dragged node
51
+
52
+ const position = useMemo(() => {
53
+ if (!draggedNode || !initDraggedNodeRect) return null;
54
+ const {
55
+ x,
56
+ y
57
+ } = cursorPosition;
58
+ const {
59
+ initWidth,
60
+ initHeight
61
+ } = initDraggedNodeRect;
62
+ return {
63
+ x: x - draggedNode.position.x + initWidth / 2,
64
+ y: y - draggedNode.position.y + initHeight / 2
65
+ };
66
+ }, [cursorPosition, draggedNode, initDraggedNodeRect]); // Returns the style for moving the node in the specified direction
67
+
68
+ const getNodeStyle = useGetNodeStyle(initDraggedNodeRect); // The list in which the cursor is located
69
+
70
+ const targetList = useTargetList(position, listStoreRef);
71
+ const moveNode = useMoveNode(position, draggedNode, getNodeStyle);
72
+ const setTargetNode = useCallback((list, node) => {
73
+ const [,,,, nodeIndex] = node;
74
+ targetNodeRef.current = {
75
+ list,
76
+ node
77
+ };
78
+ targetRef.current = {
79
+ id: list.id,
80
+ index: nodeIndex
81
+ };
82
+ }, []);
83
+ const clearTargetNode = useCallback(() => {
84
+ targetNodeRef.current = null;
85
+ targetRef.current = null;
86
+ }, []); // Returns the central position of the dragged node in the list
87
+
88
+ const getDraggedNodePos = useCallback(() => {
89
+ if (!targetList || !targetList.ref.current || !initDraggedNodeRect || !initDraggedNodeListScrollOffset) {
90
+ return null;
91
+ }
92
+
93
+ const {
94
+ initX,
95
+ initY,
96
+ initWidth,
97
+ initHeight
98
+ } = initDraggedNodeRect;
99
+ const {
100
+ initScrollLeft,
101
+ initScrollTop
102
+ } = initDraggedNodeListScrollOffset;
103
+ const {
104
+ scrollLeft,
105
+ scrollTop
106
+ } = getElementScroll(targetList.ref.current);
107
+ return {
108
+ x: initX + initWidth / 2 + initScrollLeft - scrollLeft,
109
+ y: initY + initHeight / 2 + initScrollTop - scrollTop
110
+ };
111
+ }, [initDraggedNodeListScrollOffset, initDraggedNodeRect, targetList]);
112
+ const getDraggedNodePosRef = useRef(getDraggedNodePos);
113
+ useEffect(() => {
114
+ getDraggedNodePosRef.current = getDraggedNodePos;
115
+ }, [getDraggedNodePos]); // Hide the dragged node
116
+
117
+ useEffect(() => {
118
+ if (!draggedNode) return () => {};
119
+ const [,,, draggedNodeSetStyle] = draggedNode.node;
120
+ draggedNodeSetStyle(HIDDEN_NODE_STYLE);
121
+ return () => draggedNodeSetStyle({});
122
+ }, [draggedNode]); // Append the blank node to the list to increase the height of it.
123
+ // Used when the dragged node is located inside another list.
124
+
125
+ const removeBlankNode = useBlankNode({
126
+ draggedNode,
127
+ targetList,
128
+ initDraggedNodeRect
129
+ });
130
+ const updateTargetNode = useCallback(() => {
131
+ if (!draggedNode) return;
132
+ const prevTargetNode = targetNodeRef.current; // The last updated node
133
+ // Dragging outside the list
134
+
135
+ if (prevTargetNode && prevTargetNode.list === draggedNode.list && targetList !== draggedNode.list) {
136
+ moveNode({
137
+ list: prevTargetNode.list,
138
+ startNode: prevTargetNode.node,
139
+ type: 'down',
140
+ untilTheEnd: true
141
+ });
142
+ clearTargetNode();
143
+ } // Dragging outside another list
144
+
145
+
146
+ if (prevTargetNode && prevTargetNode.list !== draggedNode.list && targetList !== prevTargetNode.list) {
147
+ moveNode({
148
+ list: prevTargetNode.list,
149
+ startNode: prevTargetNode.node,
150
+ type: 'down',
151
+ untilTheEnd: true,
152
+ insideAnotherList: true
153
+ });
154
+ removeBlankNode();
155
+ clearTargetNode();
156
+ } // Dragging inside the list
157
+
158
+
159
+ if (!prevTargetNode && targetList === draggedNode.list) {
160
+ const tail = targetList.getTail();
161
+ if (!tail) return;
162
+ const node = moveNode({
163
+ list: targetList,
164
+ startNode: tail,
165
+ type: 'up'
166
+ });
167
+ setTargetNode(targetList, node);
168
+ return;
169
+ } // Dragging inside another list
170
+
171
+
172
+ if (targetList && targetList !== draggedNode.list && prevTargetNode?.list !== targetList) {
173
+ const tail = targetList.getTail();
174
+ if (!tail) return;
175
+ const node = moveNode({
176
+ list: targetList,
177
+ startNode: tail,
178
+ type: 'up',
179
+ insideAnotherList: true
180
+ });
181
+ setTargetNode(targetList, node);
182
+ return;
183
+ } // Dragging in the same list
184
+
185
+
186
+ if (prevTargetNode && prevTargetNode.list === draggedNode.list && targetList === draggedNode.list) {
187
+ const axis = targetList.horizontal ? 'x' : 'y';
188
+ const [,, prevTargetNodeRef,,] = prevTargetNode.node;
189
+
190
+ if (!prevTargetNodeRef.current) {
191
+ // The target node was unmounted. It happens when the virtual list is used.
192
+ // If the cursor is above the dragged node, we need to move the nodes down from the tail to the node where
193
+ // the cursor is located. Otherwise, we need to move the nodes up from the head to the node where the cursor
194
+ // is located.
195
+ const draggedNodePos = getDraggedNodePosRef.current();
196
+ if (!position || !draggedNodePos) return;
197
+ const isDraggingUp = position[axis] < draggedNodePos[axis];
198
+
199
+ if (isDraggingUp) {
200
+ const tail = targetList.getTail();
201
+ if (!tail || !tail[2].current) return;
202
+ const node = moveNode({
203
+ list: targetList,
204
+ startNode: createEmptyNode({
205
+ prev: tail
206
+ }),
207
+ type: 'up'
208
+ });
209
+ setTargetNode(targetList, node);
210
+ } else {
211
+ const head = targetList.getHead();
212
+ if (!head || !head[2].current) return;
213
+ const node = moveNode({
214
+ list: targetList,
215
+ startNode: createEmptyNode({
216
+ next: head
217
+ }),
218
+ type: 'down'
219
+ });
220
+ setTargetNode(targetList, node);
221
+ }
222
+
223
+ return;
224
+ }
225
+
226
+ const prevTargetNodeRect = getNodeRect(prevTargetNode.node[2]);
227
+ const rectProp = targetList.horizontal ? 'left' : 'top';
228
+ if (!position || !prevTargetNodeRect) return;
229
+ const isMoveUp = position[axis] < prevTargetNodeRect[rectProp];
230
+ const node = isMoveUp ? moveNode({
231
+ list: targetList,
232
+ startNode: prevTargetNode.node,
233
+ type: 'up'
234
+ }) : moveNode({
235
+ list: targetList,
236
+ startNode: prevTargetNode.node,
237
+ type: 'down'
238
+ });
239
+ setTargetNode(targetList, node);
240
+ return;
241
+ } // Dragging in another list
242
+
243
+
244
+ if (targetList && targetList !== draggedNode.list && prevTargetNode?.list === targetList) {
245
+ const axis = targetList.horizontal ? 'x' : 'y';
246
+ const prevTargetNodeRect = getNodeRect(prevTargetNode.node[2]);
247
+ const rectProp = targetList.horizontal ? 'left' : 'top';
248
+ if (!position || !prevTargetNodeRect) return;
249
+ const isMoveUp = position[axis] < prevTargetNodeRect[rectProp];
250
+ const node = isMoveUp ? moveNode({
251
+ list: targetList,
252
+ startNode: prevTargetNode.node,
253
+ type: 'up',
254
+ insideAnotherList: true
255
+ }) : moveNode({
256
+ list: targetList,
257
+ startNode: prevTargetNode.node,
258
+ type: 'down',
259
+ insideAnotherList: true
260
+ });
261
+ setTargetNode(targetList, node);
262
+ }
263
+ }, [draggedNode, targetList, moveNode, clearTargetNode, removeBlankNode, setTargetNode, position]); // Update the target node if either the position or the target list has been changed
264
+
265
+ useEffect(() => {
266
+ updateTargetNode();
267
+ }, [updateTargetNode]); // Update the target node if the target list has been scrolled
268
+
269
+ const [throttledUpdateTargetNode] = useThrottle(updateTargetNode, 100);
270
+ useEvent(targetList?.ref, 'scroll', throttledUpdateTargetNode);
271
+ useEvent(window, 'scroll', throttledUpdateTargetNode); // Reset styles of the nodes when the dragged node was dropped
272
+
273
+ useEffect(() => {
274
+ if (!draggedNode) return () => {};
275
+ const [,,,, draggedNodeIndex] = draggedNode.node;
276
+ return () => {
277
+ const targetNode = targetNodeRef.current; // If the dragged node was inside another list, reset the styles for the nodes,
278
+ // starting at the tail and ending with the dragged node.
279
+
280
+ if (targetNode && targetNode.list !== draggedNode.list) {
281
+ const tail = targetNode.list.getTail();
282
+ if (!tail) return;
283
+ let node = tail;
284
+
285
+ while (true) {
286
+ const [prev,,, nodeSetStyle, nodeIndex] = node;
287
+ nodeSetStyle({});
288
+ if (!prev || nodeIndex <= draggedNodeIndex) break;
289
+ node = prev;
290
+ }
291
+ } // If the dragged node was outside the origin list, reset the styles for the nodes,
292
+ // starting at the tail and ending with the dragged node.
293
+
294
+
295
+ const tail = draggedNode.list.getTail();
296
+
297
+ if (targetNode?.list !== draggedNode.list && tail) {
298
+ let node = tail;
299
+
300
+ while (true) {
301
+ const [prev,,, nodeSetStyle, nodeIndex] = node;
302
+ nodeSetStyle({});
303
+ if (!prev || nodeIndex <= draggedNodeIndex) break;
304
+ node = prev;
305
+ }
306
+ }
307
+
308
+ if (!targetNode) return;
309
+ const [,,, targetNodeSetStyle, targetNodeIndex] = targetNode.node;
310
+
311
+ if (targetNodeIndex > draggedNodeIndex) {
312
+ let {
313
+ node
314
+ } = targetNode;
315
+
316
+ while (true) {
317
+ const [prev,,, nodeSetStyle, nodeIndex] = node;
318
+ nodeSetStyle({});
319
+ if (!prev || nodeIndex < draggedNodeIndex) return;
320
+ node = prev;
321
+ }
322
+ }
323
+
324
+ if (targetNodeIndex < draggedNodeIndex) {
325
+ let {
326
+ node
327
+ } = targetNode;
328
+
329
+ while (true) {
330
+ const [, next,, nodeSetStyle, nodeIndex] = node;
331
+ nodeSetStyle({});
332
+ if (!next || nodeIndex > draggedNodeIndex) return;
333
+ node = next;
334
+ }
335
+ }
336
+
337
+ if (targetNodeIndex === draggedNodeIndex) {
338
+ targetNodeSetStyle({});
339
+ }
340
+ };
341
+ }, [draggedNode]); // Update the position of the mounted nodes between the dragged and target nodes (used in virtual list)
342
+
343
+ useEffect(() => {
344
+ if (!draggedNode) return () => {};
345
+ const [,,,, draggedNodeIndex] = draggedNode.node;
346
+
347
+ const update = nodeProps => {
348
+ const {
349
+ setStyle,
350
+ index
351
+ } = nodeProps;
352
+ const targetNode = targetNodeRef.current; // If the dragged node outside the origin list
353
+
354
+ if (targetNodeRef.current?.list !== draggedNode.list && index > draggedNodeIndex) {
355
+ setStyle(getNodeStyle('up', draggedNode.list.horizontal));
356
+ return;
357
+ }
358
+
359
+ if (!targetNode || !targetNode.node) return;
360
+ const [,,,, targetNodeIndex] = targetNode.node; // If the dragged node was mounted
361
+
362
+ if (index === draggedNodeIndex) {
363
+ setStyle(HIDDEN_NODE_STYLE);
364
+ return;
365
+ } // If the node between the dragged and target nodes was mounted
366
+
367
+
368
+ if (index > draggedNodeIndex && index < targetNodeIndex) {
369
+ setStyle(getNodeStyle('up', targetNode.list.horizontal));
370
+ } else if (index < draggedNodeIndex && index > targetNodeIndex) {
371
+ setStyle(getNodeStyle('down', targetNode.list.horizontal));
372
+ }
373
+ };
374
+
375
+ draggedNode.list.addListener(update);
376
+ return () => draggedNode.list.removeListener(update);
377
+ }, [draggedNode, getNodeStyle]);
378
+ useEffect(() => {
379
+ if (!draggedNode || !targetList || targetList === draggedNode.list) {
380
+ return () => {};
381
+ }
382
+
383
+ const update = nodeProps => {
384
+ const {
385
+ setStyle,
386
+ index
387
+ } = nodeProps;
388
+ const targetNode = targetNodeRef.current;
389
+ if (!targetNode) return;
390
+ const [,,,, targetNodeIndex] = targetNode.node;
391
+ if (index < targetNodeIndex) return;
392
+ setStyle(getNodeStyle('down', targetList.horizontal));
393
+ };
394
+
395
+ targetList.addListener(update);
396
+ return () => targetList.removeListener(update);
397
+ }, [draggedNode, getNodeStyle, targetList]); // Call the onDragEnd callback if the draggedNode was changed to null
398
+
399
+ useEffect(() => {
400
+ if (!draggedNode) return () => {};
401
+ return () => {
402
+ // eslint-disable-next-line react-hooks/exhaustive-deps
403
+ const target = targetRef.current;
404
+ if (!target) return;
405
+ const [,,,, draggedNodeIndex] = draggedNode.node;
406
+ const dragged = {
407
+ id: draggedNode.list.id,
408
+ index: draggedNodeIndex
409
+ };
410
+ if (dragged.id === target.id && dragged.index === target.index) return;
411
+ onDragEndRef.current(dragged, target);
412
+ };
413
+ }, [draggedNode]);
414
+ };
415
+
416
+ export default useDragEffect;
417
+ //# sourceMappingURL=useDragEffect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useDragEffect.ts"],"names":["useCallback","useEffect","useMemo","useRef","useThrottle","useEvent","getElementScroll","useTargetList","useInitRect","useInitScrollOffset","useGetNodeStyle","useMoveNode","getNodeRect","useBlankNode","HIDDEN_NODE_STYLE","opacity","pointerEvents","createEmptyNode","prev","next","current","useDragEffect","props","draggedNode","cursorPosition","listStoreRef","onDragEnd","targetRef","targetNodeRef","onDragEndRef","initDraggedNodeRect","node","initDraggedNodeListScrollOffset","list","ref","position","x","y","initWidth","initHeight","getNodeStyle","targetList","moveNode","setTargetNode","nodeIndex","id","index","clearTargetNode","getDraggedNodePos","initX","initY","initScrollLeft","initScrollTop","scrollLeft","scrollTop","getDraggedNodePosRef","draggedNodeSetStyle","removeBlankNode","updateTargetNode","prevTargetNode","startNode","type","untilTheEnd","insideAnotherList","tail","getTail","axis","horizontal","prevTargetNodeRef","draggedNodePos","isDraggingUp","head","getHead","prevTargetNodeRect","rectProp","isMoveUp","throttledUpdateTargetNode","window","draggedNodeIndex","targetNode","nodeSetStyle","targetNodeSetStyle","targetNodeIndex","update","nodeProps","setStyle","addListener","removeListener","target","dragged"],"mappings":"AACA,SAGEA,WAHF,EAIEC,SAJF,EAKEC,OALF,EAMEC,MANF,QAOO,OAPP;AAQA,OAAOC,WAAP,MAAwB,yBAAxB;AACA,OAAOC,QAAP,MAAqB,sBAArB;AAGA,OAAOC,gBAAP,MAA6B,oBAA7B;AACA,OAAOC,aAAP,MAA0B,iBAA1B;AACA,OAAOC,WAAP,MAAwB,eAAxB;AACA,OAAOC,mBAAP,MAAgC,uBAAhC;AACA,OAAOC,eAAP,MAA4B,mBAA5B,C,CACA;;AACA,OAAOC,WAAP,MAAwB,eAAxB;AACA,OAAOC,WAAP,MAAwB,eAAxB,C,CACA;;AACA,OAAOC,YAAP,MAAyB,gBAAzB;AA8BA,MAAMC,iBAAgC,GAAG;AACvCC,EAAAA,OAAO,EAAE,CAD8B;AAEvCC,EAAAA,aAAa,EAAE;AAFwB,CAAzC;;AAKA,MAAMC,eAAe,GAAG,CAAC;AACvBC,EAAAA,IAAI,GAAG,IADgB;AAEvBC,EAAAA,IAAI,GAAG;AAFgB,CAAD,KAMJ,CAACD,IAAD,EAAOC,IAAP,EAAa;AAAEC,EAAAA,OAAO,EAAE;AAAX,CAAb,EAAgC,MAAM,CAAE,CAAxC,EAA0C,CAAC,CAA3C,CANpB;AAQA;;;AAEA,MAAMC,aAAa,GAAIC,KAAD,IAA+B;AACnD,QAAM;AAAEC,IAAAA,WAAF;AAAeC,IAAAA,cAAf;AAA+BC,IAAAA,YAA/B;AAA6CC,IAAAA;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T,OAAb,CAAqBqE,OAArB,EAA8BD,MAA9B;AACD,KARD;AASD,GAXQ,EAWN,CAACjE,WAAD,CAXM,CAAT;AAYD,CArYD;;AAuYA,eAAeF,aAAf","sourcesContent":["import { Position } from '@os-design/use-drag';\nimport {\n CSSProperties,\n RefObject,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n} from 'react';\nimport useThrottle from '@os-design/use-throttle';\nimport useEvent from '@os-design/use-event';\nimport NodeList, { ExistingNode, Node, NodeProps } from './NodeList';\nimport ListStore from './ListStore';\nimport getElementScroll from './getElementScroll';\nimport useTargetList from './useTargetList';\nimport useInitRect from './useInitRect';\nimport useInitScrollOffset from './useInitScrollOffset';\nimport useGetNodeStyle from './useGetNodeStyle';\n// eslint-disable-next-line import/no-cycle\nimport useMoveNode from './useMoveNode';\nimport getNodeRect from './getNodeRect';\n// eslint-disable-next-line import/no-cycle\nimport useBlankNode from './useBlankNode';\n\nexport interface DraggedNode {\n list: NodeList;\n node: ExistingNode;\n position: Position;\n}\n\ninterface TargetNode {\n list: NodeList;\n node: ExistingNode;\n}\n\nexport interface ItemLocation {\n id: string;\n index: number;\n}\n\nexport type DragEndHandler = (\n dragged: ItemLocation,\n target: ItemLocation\n) => void;\n\ninterface UseDragEffectProps {\n draggedNode: DraggedNode | null;\n cursorPosition: Position;\n listStoreRef: RefObject<ListStore>;\n onDragEnd: DragEndHandler;\n}\n\nconst HIDDEN_NODE_STYLE: CSSProperties = {\n opacity: 0,\n pointerEvents: 'none',\n};\n\nconst createEmptyNode = ({\n prev = null,\n next = null,\n}: {\n prev?: Node;\n next?: Node;\n}): ExistingNode => [prev, next, { current: null }, () => {}, -1];\n\n/* eslint-disable @typescript-eslint/no-explicit-any,no-constant-condition */\n\nconst useDragEffect = (props: UseDragEffectProps) => {\n const { draggedNode, cursorPosition, listStoreRef, onDragEnd } = props;\n\n const targetRef = useRef<ItemLocation | null>(null);\n const targetNodeRef = useRef<TargetNode | null>(draggedNode);\n\n useEffect(() => {\n targetNodeRef.current = draggedNode;\n }, [draggedNode]);\n\n const onDragEndRef = useRef(onDragEnd);\n useEffect(() => {\n onDragEndRef.current = onDragEnd;\n }, [onDragEnd]);\n\n // The initial bounds of the dragged node.\n // We can't read the bounds of the dragged node on the fly because the node can be unmounted in the virtual list.\n const initDraggedNodeRect = useInitRect(draggedNode?.node[2]);\n\n // The initial scroll position of the list where the dragged node is located.\n // Used to detect the actual position of the dragged node.\n // The purpose is the same as with initDraggedNodeRect.\n const initDraggedNodeListScrollOffset = useInitScrollOffset(\n draggedNode?.list.ref\n );\n\n // The central position of the dragged node\n const position = useMemo(() => {\n if (!draggedNode || !initDraggedNodeRect) return null;\n const { x, y } = cursorPosition;\n const { initWidth, initHeight } = initDraggedNodeRect;\n return {\n x: x - draggedNode.position.x + initWidth / 2,\n y: y - draggedNode.position.y + initHeight / 2,\n };\n }, [cursorPosition, draggedNode, initDraggedNodeRect]);\n\n // Returns the style for moving the node in the specified direction\n const getNodeStyle = useGetNodeStyle(initDraggedNodeRect);\n\n // The list in which the cursor is located\n const targetList = useTargetList(position, listStoreRef);\n\n const moveNode = useMoveNode(position, draggedNode, getNodeStyle);\n\n const setTargetNode = useCallback((list: NodeList, node: ExistingNode) => {\n const [, , , , nodeIndex] = node;\n targetNodeRef.current = { list, node };\n targetRef.current = { id: list.id, index: nodeIndex };\n }, []);\n\n const clearTargetNode = useCallback(() => {\n targetNodeRef.current = null;\n targetRef.current = null;\n }, []);\n\n // Returns the central position of the dragged node in the list\n const getDraggedNodePos = useCallback(() => {\n if (\n !targetList ||\n !targetList.ref.current ||\n !initDraggedNodeRect ||\n !initDraggedNodeListScrollOffset\n ) {\n return null;\n }\n const { initX, initY, initWidth, initHeight } = initDraggedNodeRect;\n const { initScrollLeft, initScrollTop } = initDraggedNodeListScrollOffset;\n const { scrollLeft, scrollTop } = getElementScroll(targetList.ref.current);\n return {\n x: initX + initWidth / 2 + initScrollLeft - scrollLeft,\n y: initY + initHeight / 2 + initScrollTop - scrollTop,\n };\n }, [initDraggedNodeListScrollOffset, initDraggedNodeRect, targetList]);\n\n const getDraggedNodePosRef = useRef(getDraggedNodePos);\n useEffect(() => {\n getDraggedNodePosRef.current = getDraggedNodePos;\n }, [getDraggedNodePos]);\n\n // Hide the dragged node\n useEffect(() => {\n if (!draggedNode) return () => {};\n const [, , , draggedNodeSetStyle] = draggedNode.node;\n draggedNodeSetStyle(HIDDEN_NODE_STYLE);\n return () => draggedNodeSetStyle({});\n }, [draggedNode]);\n\n // Append the blank node to the list to increase the height of it.\n // Used when the dragged node is located inside another list.\n const removeBlankNode = useBlankNode({\n draggedNode,\n targetList,\n initDraggedNodeRect,\n });\n\n const updateTargetNode = useCallback(() => {\n if (!draggedNode) return;\n const prevTargetNode = targetNodeRef.current; // The last updated node\n\n // Dragging outside the list\n if (\n prevTargetNode &&\n prevTargetNode.list === draggedNode.list &&\n targetList !== draggedNode.list\n ) {\n moveNode({\n list: prevTargetNode.list,\n startNode: prevTargetNode.node,\n type: 'down',\n untilTheEnd: true,\n });\n clearTargetNode();\n }\n\n // Dragging outside another list\n if (\n prevTargetNode &&\n prevTargetNode.list !== draggedNode.list &&\n targetList !== prevTargetNode.list\n ) {\n moveNode({\n list: prevTargetNode.list,\n startNode: prevTargetNode.node,\n type: 'down',\n untilTheEnd: true,\n insideAnotherList: true,\n });\n removeBlankNode();\n clearTargetNode();\n }\n\n // Dragging inside the list\n if (!prevTargetNode && targetList === draggedNode.list) {\n const tail = targetList.getTail();\n if (!tail) return;\n const node = moveNode({\n list: targetList,\n startNode: tail,\n type: 'up',\n });\n setTargetNode(targetList, node);\n return;\n }\n\n // Dragging inside another list\n if (\n targetList &&\n targetList !== draggedNode.list &&\n prevTargetNode?.list !== targetList\n ) {\n const tail = targetList.getTail();\n if (!tail) return;\n const node = moveNode({\n list: targetList,\n startNode: tail,\n type: 'up',\n insideAnotherList: true,\n });\n setTargetNode(targetList, node);\n return;\n }\n\n // Dragging in the same list\n if (\n prevTargetNode &&\n prevTargetNode.list === draggedNode.list &&\n targetList === draggedNode.list\n ) {\n const axis = targetList.horizontal ? 'x' : 'y';\n const [, , prevTargetNodeRef, ,] = prevTargetNode.node;\n if (!prevTargetNodeRef.current) {\n // The target node was unmounted. It happens when the virtual list is used.\n // If the cursor is above the dragged node, we need to move the nodes down from the tail to the node where\n // the cursor is located. Otherwise, we need to move the nodes up from the head to the node where the cursor\n // is located.\n const draggedNodePos = getDraggedNodePosRef.current();\n if (!position || !draggedNodePos) return;\n const isDraggingUp = position[axis] < draggedNodePos[axis];\n if (isDraggingUp) {\n const tail = targetList.getTail();\n if (!tail || !tail[2].current) return;\n const node = moveNode({\n list: targetList,\n startNode: createEmptyNode({ prev: tail }),\n type: 'up',\n });\n setTargetNode(targetList, node);\n } else {\n const head = targetList.getHead();\n if (!head || !head[2].current) return;\n const node = moveNode({\n list: targetList,\n startNode: createEmptyNode({ next: head }),\n type: 'down',\n });\n setTargetNode(targetList, node);\n }\n return;\n }\n const prevTargetNodeRect = getNodeRect(prevTargetNode.node[2]);\n const rectProp = targetList.horizontal ? 'left' : 'top';\n if (!position || !prevTargetNodeRect) return;\n const isMoveUp = position[axis] < prevTargetNodeRect[rectProp];\n const node = isMoveUp\n ? moveNode({\n list: targetList,\n startNode: prevTargetNode.node,\n type: 'up',\n })\n : moveNode({\n list: targetList,\n startNode: prevTargetNode.node,\n type: 'down',\n });\n setTargetNode(targetList, node);\n return;\n }\n\n // Dragging in another list\n if (\n targetList &&\n targetList !== draggedNode.list &&\n prevTargetNode?.list === targetList\n ) {\n const axis = targetList.horizontal ? 'x' : 'y';\n const prevTargetNodeRect = getNodeRect(prevTargetNode.node[2]);\n const rectProp = targetList.horizontal ? 'left' : 'top';\n if (!position || !prevTargetNodeRect) return;\n const isMoveUp = position[axis] < prevTargetNodeRect[rectProp];\n const node = isMoveUp\n ? moveNode({\n list: targetList,\n startNode: prevTargetNode.node,\n type: 'up',\n insideAnotherList: true,\n })\n : moveNode({\n list: targetList,\n startNode: prevTargetNode.node,\n type: 'down',\n insideAnotherList: true,\n });\n setTargetNode(targetList, node);\n }\n }, [\n draggedNode,\n targetList,\n moveNode,\n clearTargetNode,\n removeBlankNode,\n setTargetNode,\n position,\n ]);\n\n // Update the target node if either the position or the target list has been changed\n useEffect(() => {\n updateTargetNode();\n }, [updateTargetNode]);\n\n // Update the target node if the target list has been scrolled\n const [throttledUpdateTargetNode] = useThrottle(updateTargetNode, 100);\n useEvent(targetList?.ref as any, 'scroll', throttledUpdateTargetNode);\n useEvent(window, 'scroll', throttledUpdateTargetNode);\n\n // Reset styles of the nodes when the dragged node was dropped\n useEffect(() => {\n if (!draggedNode) return () => {};\n const [, , , , draggedNodeIndex] = draggedNode.node;\n return () => {\n const targetNode = targetNodeRef.current;\n\n // If the dragged node was inside another list, reset the styles for the nodes,\n // starting at the tail and ending with the dragged node.\n if (targetNode && targetNode.list !== draggedNode.list) {\n const tail = targetNode.list.getTail();\n if (!tail) return;\n let node = tail;\n while (true) {\n const [prev, , , nodeSetStyle, nodeIndex] = node;\n nodeSetStyle({});\n if (!prev || nodeIndex <= draggedNodeIndex) break;\n node = prev;\n }\n }\n\n // If the dragged node was outside the origin list, reset the styles for the nodes,\n // starting at the tail and ending with the dragged node.\n const tail = draggedNode.list.getTail();\n if (targetNode?.list !== draggedNode.list && tail) {\n let node = tail;\n while (true) {\n const [prev, , , nodeSetStyle, nodeIndex] = node;\n nodeSetStyle({});\n if (!prev || nodeIndex <= draggedNodeIndex) break;\n node = prev;\n }\n }\n\n if (!targetNode) return;\n const [, , , targetNodeSetStyle, targetNodeIndex] = targetNode.node;\n if (targetNodeIndex > draggedNodeIndex) {\n let { node } = targetNode;\n while (true) {\n const [prev, , , nodeSetStyle, nodeIndex] = node;\n nodeSetStyle({});\n if (!prev || nodeIndex < draggedNodeIndex) return;\n node = prev;\n }\n }\n if (targetNodeIndex < draggedNodeIndex) {\n let { node } = targetNode;\n while (true) {\n const [, next, , nodeSetStyle, nodeIndex] = node;\n nodeSetStyle({});\n if (!next || nodeIndex > draggedNodeIndex) return;\n node = next;\n }\n }\n if (targetNodeIndex === draggedNodeIndex) {\n targetNodeSetStyle({});\n }\n };\n }, [draggedNode]);\n\n // Update the position of the mounted nodes between the dragged and target nodes (used in virtual list)\n useEffect(() => {\n if (!draggedNode) return () => {};\n const [, , , , draggedNodeIndex] = draggedNode.node;\n const update = (nodeProps: NodeProps) => {\n const { setStyle, index } = nodeProps;\n const targetNode = targetNodeRef.current;\n\n // If the dragged node outside the origin list\n if (\n targetNodeRef.current?.list !== draggedNode.list &&\n index > draggedNodeIndex\n ) {\n setStyle(getNodeStyle('up', draggedNode.list.horizontal));\n return;\n }\n\n if (!targetNode || !targetNode.node) return;\n const [, , , , targetNodeIndex] = targetNode.node;\n // If the dragged node was mounted\n if (index === draggedNodeIndex) {\n setStyle(HIDDEN_NODE_STYLE);\n return;\n }\n // If the node between the dragged and target nodes was mounted\n if (index > draggedNodeIndex && index < targetNodeIndex) {\n setStyle(getNodeStyle('up', targetNode.list.horizontal));\n } else if (index < draggedNodeIndex && index > targetNodeIndex) {\n setStyle(getNodeStyle('down', targetNode.list.horizontal));\n }\n };\n draggedNode.list.addListener(update);\n return () => draggedNode.list.removeListener(update);\n }, [draggedNode, getNodeStyle]);\n\n useEffect(() => {\n if (!draggedNode || !targetList || targetList === draggedNode.list) {\n return () => {};\n }\n const update = (nodeProps: NodeProps) => {\n const { setStyle, index } = nodeProps;\n const targetNode = targetNodeRef.current;\n if (!targetNode) return;\n const [, , , , targetNodeIndex] = targetNode.node;\n if (index < targetNodeIndex) return;\n setStyle(getNodeStyle('down', targetList.horizontal));\n };\n targetList.addListener(update);\n return () => targetList.removeListener(update);\n }, [draggedNode, getNodeStyle, targetList]);\n\n // Call the onDragEnd callback if the draggedNode was changed to null\n useEffect(() => {\n if (!draggedNode) return () => {};\n return () => {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const target = targetRef.current;\n if (!target) return;\n const [, , , , draggedNodeIndex] = draggedNode.node;\n const dragged = { id: draggedNode.list.id, index: draggedNodeIndex };\n if (dragged.id === target.id && dragged.index === target.index) return;\n onDragEndRef.current(dragged, target);\n };\n }, [draggedNode]);\n};\n\nexport default useDragEffect;\n"],"file":"useDragEffect.js"}
@@ -0,0 +1,14 @@
1
+ import React, { useContext } from 'react';
2
+ export const DroppableContext = /*#__PURE__*/React.createContext({
3
+ registerNode: () => [null, null, {
4
+ current: null
5
+ }, () => {}, -1],
6
+ deregisterNode: () => {},
7
+ onMouseDown: () => {},
8
+ onTouchStart: () => {}
9
+ });
10
+
11
+ const useDroppable = () => useContext(DroppableContext);
12
+
13
+ export default useDroppable;
14
+ //# sourceMappingURL=useDroppable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useDroppable.ts"],"names":["React","useContext","DroppableContext","createContext","registerNode","current","deregisterNode","onMouseDown","onTouchStart","useDroppable"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,UAAhB,QAA0D,OAA1D;AAUA,OAAO,MAAMC,gBAAgB,gBAAGF,KAAK,CAACG,aAAN,CAA2C;AACzEC,EAAAA,YAAY,EAAE,MACZ,CAAC,IAAD,EAAO,IAAP,EAAa;AAAEC,IAAAA,OAAO,EAAE;AAAX,GAAb,EAAgC,MAAM,CAAE,CAAxC,EAA0C,CAAC,CAA3C,CAFuE;AAGzEC,EAAAA,cAAc,EAAE,MAAM,CAAE,CAHiD;AAIzEC,EAAAA,WAAW,EAAE,MAAM,CAAE,CAJoD;AAKzEC,EAAAA,YAAY,EAAE,MAAM,CAAE;AALmD,CAA3C,CAAzB;;AAQP,MAAMC,YAAY,GAAG,MAA6BR,UAAU,CAACC,gBAAD,CAA5D;;AAEA,eAAeO,YAAf","sourcesContent":["import React, { useContext, MouseEvent, TouchEvent } from 'react';\nimport { ExistingNode, NodeProps } from './NodeList';\n\ninterface DroppableContextProps {\n registerNode: (props: NodeProps) => ExistingNode;\n deregisterNode: (node: ExistingNode) => void;\n onMouseDown: (e: MouseEvent, node: ExistingNode) => void;\n onTouchStart: (e: TouchEvent, node: ExistingNode) => void;\n}\n\nexport const DroppableContext = React.createContext<DroppableContextProps>({\n registerNode: () =>\n [null, null, { current: null }, () => {}, -1] as ExistingNode,\n deregisterNode: () => {},\n onMouseDown: () => {},\n onTouchStart: () => {},\n});\n\nconst useDroppable = (): DroppableContextProps => useContext(DroppableContext);\n\nexport default useDroppable;\n"],"file":"useDroppable.js"}
@@ -0,0 +1,7 @@
1
+ import { useMemo } from 'react';
2
+ import crypto from 'crypto';
3
+
4
+ const useGeneratedId = size => useMemo(() => crypto.randomBytes(size).toString('hex'), [size]);
5
+
6
+ export default useGeneratedId;
7
+ //# sourceMappingURL=useGeneratedId.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useGeneratedId.ts"],"names":["useMemo","crypto","useGeneratedId","size","randomBytes","toString"],"mappings":"AAAA,SAASA,OAAT,QAAwB,OAAxB;AACA,OAAOC,MAAP,MAAmB,QAAnB;;AAEA,MAAMC,cAAc,GAAIC,IAAD,IACrBH,OAAO,CAAC,MAAMC,MAAM,CAACG,WAAP,CAAmBD,IAAnB,EAAyBE,QAAzB,CAAkC,KAAlC,CAAP,EAAiD,CAACF,IAAD,CAAjD,CADT;;AAGA,eAAeD,cAAf","sourcesContent":["import { useMemo } from 'react';\nimport crypto from 'crypto';\n\nconst useGeneratedId = (size: number) =>\n useMemo(() => crypto.randomBytes(size).toString('hex'), [size]);\n\nexport default useGeneratedId;\n"],"file":"useGeneratedId.js"}
@@ -0,0 +1,20 @@
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+
3
+ const useGetNodeStyle = size => {
4
+ const sizeRef = useRef(size);
5
+ useEffect(() => {
6
+ sizeRef.current = size;
7
+ }, [size]);
8
+ return useCallback((type, horizontal) => {
9
+ if (type === 'init' || !sizeRef.current) return {};
10
+ const translateFn = horizontal ? 'translateX' : 'translateY';
11
+ const sizeProp = horizontal ? 'initWidth' : 'initHeight';
12
+ const sign = type === 'down' ? 1 : -1;
13
+ const style = {};
14
+ style.transform = `${translateFn}(${sign * sizeRef.current[sizeProp]}px)`;
15
+ return style;
16
+ }, []);
17
+ };
18
+
19
+ export default useGetNodeStyle;
20
+ //# sourceMappingURL=useGetNodeStyle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useGetNodeStyle.ts"],"names":["useCallback","useEffect","useRef","useGetNodeStyle","size","sizeRef","current","type","horizontal","translateFn","sizeProp","sign","style","transform"],"mappings":"AAAA,SAAwBA,WAAxB,EAAqCC,SAArC,EAAgDC,MAAhD,QAA8D,OAA9D;;AAYA,MAAMC,eAAe,GAAIC,IAAD,IAA4C;AAClE,QAAMC,OAAO,GAAGH,MAAM,CAACE,IAAD,CAAtB;AAEAH,EAAAA,SAAS,CAAC,MAAM;AACdI,IAAAA,OAAO,CAACC,OAAR,GAAkBF,IAAlB;AACD,GAFQ,EAEN,CAACA,IAAD,CAFM,CAAT;AAIA,SAAOJ,WAAW,CAAkB,CAACO,IAAD,EAAOC,UAAP,KAAsB;AACxD,QAAID,IAAI,KAAK,MAAT,IAAmB,CAACF,OAAO,CAACC,OAAhC,EAAyC,OAAO,EAAP;AAEzC,UAAMG,WAAW,GAAGD,UAAU,GAAG,YAAH,GAAkB,YAAhD;AACA,UAAME,QAAQ,GAAGF,UAAU,GAAG,WAAH,GAAiB,YAA5C;AACA,UAAMG,IAAI,GAAGJ,IAAI,KAAK,MAAT,GAAkB,CAAlB,GAAsB,CAAC,CAApC;AAEA,UAAMK,KAAoB,GAAG,EAA7B;AACAA,IAAAA,KAAK,CAACC,SAAN,GAAmB,GAAEJ,WAAY,IAAGE,IAAI,GAAGN,OAAO,CAACC,OAAR,CAAgBI,QAAhB,CAA0B,KAArE;AAEA,WAAOE,KAAP;AACD,GAXiB,EAWf,EAXe,CAAlB;AAYD,CAnBD;;AAqBA,eAAeT,eAAf","sourcesContent":["import { CSSProperties, useCallback, useEffect, useRef } from 'react';\n\ninterface InitSize {\n initWidth: number;\n initHeight: number;\n}\n\nexport type NodeStyleGetter = (\n type: 'up' | 'down' | 'init',\n horizontal: boolean\n) => CSSProperties;\n\nconst useGetNodeStyle = (size: InitSize | null): NodeStyleGetter => {\n const sizeRef = useRef(size);\n\n useEffect(() => {\n sizeRef.current = size;\n }, [size]);\n\n return useCallback<NodeStyleGetter>((type, horizontal) => {\n if (type === 'init' || !sizeRef.current) return {};\n\n const translateFn = horizontal ? 'translateX' : 'translateY';\n const sizeProp = horizontal ? 'initWidth' : 'initHeight';\n const sign = type === 'down' ? 1 : -1;\n\n const style: CSSProperties = {};\n style.transform = `${translateFn}(${sign * sizeRef.current[sizeProp]}px)`;\n\n return style;\n }, []);\n};\n\nexport default useGetNodeStyle;\n"],"file":"useGetNodeStyle.js"}
@@ -0,0 +1,15 @@
1
+ import { useMemo } from 'react';
2
+
3
+ const useInitRect = ref => useMemo(() => {
4
+ if (!ref || !ref.current) return null;
5
+ const rect = ref.current.getBoundingClientRect();
6
+ return {
7
+ initX: rect.x,
8
+ initY: rect.y,
9
+ initWidth: rect.width,
10
+ initHeight: rect.height
11
+ };
12
+ }, [ref]);
13
+
14
+ export default useInitRect;
15
+ //# sourceMappingURL=useInitRect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useInitRect.ts"],"names":["useMemo","useInitRect","ref","current","rect","getBoundingClientRect","initX","x","initY","y","initWidth","width","initHeight","height"],"mappings":"AAAA,SAAoBA,OAApB,QAAmC,OAAnC;;AAEA,MAAMC,WAAW,GAAIC,GAAD,IAClBF,OAAO,CAAC,MAAM;AACZ,MAAI,CAACE,GAAD,IAAQ,CAACA,GAAG,CAACC,OAAjB,EAA0B,OAAO,IAAP;AAC1B,QAAMC,IAAI,GAAGF,GAAG,CAACC,OAAJ,CAAYE,qBAAZ,EAAb;AACA,SAAO;AACLC,IAAAA,KAAK,EAAEF,IAAI,CAACG,CADP;AAELC,IAAAA,KAAK,EAAEJ,IAAI,CAACK,CAFP;AAGLC,IAAAA,SAAS,EAAEN,IAAI,CAACO,KAHX;AAILC,IAAAA,UAAU,EAAER,IAAI,CAACS;AAJZ,GAAP;AAMD,CATM,EASJ,CAACX,GAAD,CATI,CADT;;AAYA,eAAeD,WAAf","sourcesContent":["import { RefObject, useMemo } from 'react';\n\nconst useInitRect = (ref?: RefObject<HTMLElement>) =>\n useMemo(() => {\n if (!ref || !ref.current) return null;\n const rect = ref.current.getBoundingClientRect();\n return {\n initX: rect.x,\n initY: rect.y,\n initWidth: rect.width,\n initHeight: rect.height,\n };\n }, [ref]);\n\nexport default useInitRect;\n"],"file":"useInitRect.js"}
@@ -0,0 +1,14 @@
1
+ import { useMemo } from 'react';
2
+ import getElementScroll from './getElementScroll';
3
+
4
+ const useInitScrollOffset = ref => useMemo(() => {
5
+ if (!ref || !ref.current) return null;
6
+ const scrollOffset = getElementScroll(ref.current);
7
+ return {
8
+ initScrollLeft: scrollOffset.scrollLeft,
9
+ initScrollTop: scrollOffset.scrollTop
10
+ };
11
+ }, [ref]);
12
+
13
+ export default useInitScrollOffset;
14
+ //# sourceMappingURL=useInitScrollOffset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/useInitScrollOffset.ts"],"names":["useMemo","getElementScroll","useInitScrollOffset","ref","current","scrollOffset","initScrollLeft","scrollLeft","initScrollTop","scrollTop"],"mappings":"AAAA,SAAoBA,OAApB,QAAmC,OAAnC;AACA,OAAOC,gBAAP,MAA6B,oBAA7B;;AAEA,MAAMC,mBAAmB,GAAIC,GAAD,IAC1BH,OAAO,CAAC,MAAM;AACZ,MAAI,CAACG,GAAD,IAAQ,CAACA,GAAG,CAACC,OAAjB,EAA0B,OAAO,IAAP;AAC1B,QAAMC,YAAY,GAAGJ,gBAAgB,CAACE,GAAG,CAACC,OAAL,CAArC;AACA,SAAO;AACLE,IAAAA,cAAc,EAAED,YAAY,CAACE,UADxB;AAELC,IAAAA,aAAa,EAAEH,YAAY,CAACI;AAFvB,GAAP;AAID,CAPM,EAOJ,CAACN,GAAD,CAPI,CADT;;AAUA,eAAeD,mBAAf","sourcesContent":["import { RefObject, useMemo } from 'react';\nimport getElementScroll from './getElementScroll';\n\nconst useInitScrollOffset = (ref?: RefObject<HTMLElement>) =>\n useMemo(() => {\n if (!ref || !ref.current) return null;\n const scrollOffset = getElementScroll(ref.current);\n return {\n initScrollLeft: scrollOffset.scrollLeft,\n initScrollTop: scrollOffset.scrollTop,\n };\n }, [ref]);\n\nexport default useInitScrollOffset;\n"],"file":"useInitScrollOffset.js"}
@@ -0,0 +1,62 @@
1
+ import { useCallback } from 'react';
2
+ import getNodeRect from './getNodeRect';
3
+
4
+ /* eslint-disable no-constant-condition */
5
+ const useMoveNode = (position, draggedNode, getNodeStyle) => useCallback(props => {
6
+ const {
7
+ list,
8
+ startNode,
9
+ type,
10
+ untilTheEnd = false,
11
+ insideAnotherList = false
12
+ } = props;
13
+ if (!draggedNode || !position) return startNode;
14
+ const [,,,, draggedNodeIndex] = draggedNode.node;
15
+ const axis = list.horizontal ? 'x' : 'y';
16
+ let node = startNode;
17
+
18
+ if (type === 'up') {
19
+ while (true) {
20
+ const [prev,,, nodeSetStyle, nodeIndex] = node;
21
+ if (!prev) return node;
22
+ const [,, prevNodeRef, prevNodeSetStyle, prevNodeIndex] = prev;
23
+
24
+ if (!untilTheEnd) {
25
+ const prevNodeRect = getNodeRect(prevNodeRef);
26
+ const rectProp = list.horizontal ? 'right' : 'bottom';
27
+ if (prevNodeRect && position[axis] > prevNodeRect[rectProp]) return node;
28
+ }
29
+
30
+ if (insideAnotherList || prevNodeIndex < draggedNodeIndex) {
31
+ prevNodeSetStyle(getNodeStyle('down', list.horizontal));
32
+ } else if (nodeIndex > draggedNodeIndex) {
33
+ nodeSetStyle(getNodeStyle('init', list.horizontal));
34
+ }
35
+
36
+ node = prev;
37
+ }
38
+ } else {
39
+ while (true) {
40
+ const [, next,, nodeSetStyle, nodeIndex] = node;
41
+ if (!next) return node;
42
+ const [,, nextNodeRef, nextNodeSetStyle, nextNodeIndex] = next;
43
+
44
+ if (!untilTheEnd) {
45
+ const nextNodeRect = getNodeRect(nextNodeRef);
46
+ const rectProp = list.horizontal ? 'left' : 'top';
47
+ if (nextNodeRect && position[axis] < nextNodeRect[rectProp]) return node;
48
+ }
49
+
50
+ if (insideAnotherList || nodeIndex < draggedNodeIndex) {
51
+ nodeSetStyle(getNodeStyle('init', list.horizontal));
52
+ } else if (nextNodeIndex > draggedNodeIndex) {
53
+ nextNodeSetStyle(getNodeStyle('up', list.horizontal));
54
+ }
55
+
56
+ node = next;
57
+ }
58
+ }
59
+ }, [draggedNode, getNodeStyle, position]);
60
+
61
+ export default useMoveNode;
62
+ //# sourceMappingURL=useMoveNode.js.map