@lynx-js/lynx-ui-sortable 3.133.0 → 3.134.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/dist/Sortable.jsx CHANGED
@@ -1,16 +1,96 @@
1
- import { runOnMainThread, useCallback, useContext, useEffect, useMainThreadRef, useMemo, useRef, useState } from "@lynx-js/react";
1
+ import { runOnMainThread, useCallback, useContext, useEffect, useMainThreadRef, useMemo, useState } from "@lynx-js/react";
2
+ import { usePreCommit } from "@lynx-js/lynx-ui-common";
2
3
  import { Draggable, DraggableArea, DraggableRoot } from "@lynx-js/lynx-ui-draggable";
3
4
  import { SortableContext } from "./SortableContext.jsx";
4
5
  import { useSortable } from "./useSortable.jsx";
6
+ function getEdgeDistance(itemBounds, boundaryRect) {
7
+ 'main thread';
8
+ return {
9
+ distanceToTop: itemBounds.top - boundaryRect.top,
10
+ distanceToBottom: boundaryRect.bottom - itemBounds.bottom
11
+ };
12
+ }
13
+ function getAutoScrollTriggerOverflow(edgeDistance, upperTriggerZone, lowerTriggerZone) {
14
+ 'main thread';
15
+ if (edgeDistance.distanceToTop < upperTriggerZone) return edgeDistance.distanceToTop - upperTriggerZone;
16
+ if (edgeDistance.distanceToBottom < lowerTriggerZone) return lowerTriggerZone - edgeDistance.distanceToBottom;
17
+ return 0;
18
+ }
19
+ const AUTO_SCROLL_MAX_STEP = 18;
20
+ const AUTO_SCROLL_VELOCITY_FACTOR = 0.35;
21
+ function getNormalizedElementKey(sortingKey) {
22
+ return String(sortingKey).replace(/[^\w-]/g, '-');
23
+ }
24
+ function getSortableItemElementId(sortingKey) {
25
+ return `sortable-item-${getNormalizedElementKey(sortingKey)}`;
26
+ }
27
+ function getSortableDragOverlayElementId(sortingKey) {
28
+ return `sortable-drag-overlay-${getNormalizedElementKey(sortingKey)}`;
29
+ }
5
30
  function SortableRoot(props) {
6
- const { children, data, debugLog = false, boundaryId, onSortEnd, onSortStart, enableSorting = true } = props;
31
+ const { children, data, debugLog = false, boundaryId, scrollableBoundaryId, as, scrollableClassName, scrollableContentClassName, scrollableEnableScroll = true, scrollableStickyUpperOffset = 0, scrollableStickyLowerOffset = 0, onSortEnd, onSortStart, enableSorting = true } = props;
32
+ const scrollable = 'ScrollView' === as;
33
+ const [isDragging, setIsDragging] = useState(false);
34
+ const handleInternalSortStart = useCallback(()=>{
35
+ setIsDragging(true);
36
+ onSortStart?.();
37
+ }, [
38
+ onSortStart
39
+ ]);
40
+ const handleInternalSortEnd = useCallback((sortedData)=>{
41
+ setIsDragging(false);
42
+ onSortEnd(sortedData);
43
+ }, [
44
+ onSortEnd
45
+ ]);
46
+ const scrollableElementId = useMemo(()=>scrollableBoundaryId ?? 'sortable-scrollable-boundary', [
47
+ scrollableBoundaryId
48
+ ]);
49
+ const scrollableContentId = useMemo(()=>boundaryId ?? 'sortable-scrollable-content', [
50
+ boundaryId
51
+ ]);
7
52
  const sizeMap = useMainThreadRef({});
8
53
  const childrenRefMap = useMainThreadRef({});
9
54
  const childrenMTSRefMap = useMainThreadRef({});
55
+ const dragOverlayRefMap = useMainThreadRef({});
56
+ const dirtyKeysRef = useMainThreadRef({});
57
+ const scrollableBoundaryUpperEdgeRef = useMainThreadRef(false);
58
+ const scrollableBoundaryLowerEdgeRef = useMainThreadRef(false);
59
+ const scrollableScrollTopRef = useMainThreadRef(0);
10
60
  const updateItemSize = useCallback((sortingKey, size)=>{
11
61
  'main thread';
12
62
  sizeMap.current[sortingKey] = size;
13
63
  }, []);
64
+ const handleScrollableBoundaryScroll = useCallback((event)=>{
65
+ 'main thread';
66
+ scrollableScrollTopRef.current = event.detail.scrollTop;
67
+ }, [
68
+ scrollableScrollTopRef
69
+ ]);
70
+ const handleScrollableBoundaryUpperExposure = useCallback(()=>{
71
+ 'main thread';
72
+ scrollableBoundaryUpperEdgeRef.current = true;
73
+ }, [
74
+ scrollableBoundaryUpperEdgeRef
75
+ ]);
76
+ const handleScrollableBoundaryUpperDisexposure = useCallback(()=>{
77
+ 'main thread';
78
+ scrollableBoundaryUpperEdgeRef.current = false;
79
+ }, [
80
+ scrollableBoundaryUpperEdgeRef
81
+ ]);
82
+ const handleScrollableBoundaryLowerExposure = useCallback(()=>{
83
+ 'main thread';
84
+ scrollableBoundaryLowerEdgeRef.current = true;
85
+ }, [
86
+ scrollableBoundaryLowerEdgeRef
87
+ ]);
88
+ const handleScrollableBoundaryLowerDisexposure = useCallback(()=>{
89
+ 'main thread';
90
+ scrollableBoundaryLowerEdgeRef.current = false;
91
+ }, [
92
+ scrollableBoundaryLowerEdgeRef
93
+ ]);
14
94
  const setChildrenRef = useCallback((refI, key)=>{
15
95
  'main thread';
16
96
  childrenRefMap.current[key] = refI.current;
@@ -19,49 +99,139 @@ function SortableRoot(props) {
19
99
  'main thread';
20
100
  childrenMTSRefMap.current[key] = refI.current;
21
101
  }, []);
102
+ const setDragOverlayRef = useCallback((refI, key)=>{
103
+ 'main thread';
104
+ dragOverlayRefMap.current[key] = refI.current;
105
+ }, [
106
+ dragOverlayRefMap
107
+ ]);
22
108
  const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
23
109
  data: data,
24
110
  sizeMap: sizeMap,
25
111
  itemRefMap: childrenRefMap,
26
112
  itemMTSRefMap: childrenMTSRefMap,
27
- onDragEnd: onSortEnd,
28
- onDragStart: onSortStart,
113
+ dirtyKeysRef,
114
+ onDragEnd: handleInternalSortEnd,
115
+ onDragStart: handleInternalSortStart,
29
116
  debugLog
30
117
  });
31
118
  const sortableContextValue = useMemo(()=>({
32
119
  data,
120
+ isDragOverlay: false,
121
+ debugLog,
33
122
  enableSorting,
34
- boundaryId,
123
+ boundaryId: scrollable ? scrollableContentId : boundaryId,
124
+ scrollableBoundaryId: scrollable ? scrollableElementId : scrollableBoundaryId,
125
+ scrollableBoundaryUpperEdgeRef: scrollable ? scrollableBoundaryUpperEdgeRef : void 0,
126
+ scrollableBoundaryLowerEdgeRef: scrollable ? scrollableBoundaryLowerEdgeRef : void 0,
127
+ scrollableScrollTopRef: scrollable ? scrollableScrollTopRef : void 0,
128
+ dragOverlayRefMap: scrollable ? dragOverlayRefMap : void 0,
129
+ dirtyKeysRef,
130
+ scrollableStickyUpperOffset,
131
+ scrollableStickyLowerOffset,
35
132
  updateItemSize,
36
133
  setChildrenRef,
37
134
  setChildrenMTSRef,
135
+ setDragOverlayRef,
38
136
  handleDragEnd,
39
137
  handleDragMove,
40
138
  handleDragStart
41
139
  }), [
42
140
  data,
141
+ debugLog,
43
142
  enableSorting,
44
143
  boundaryId,
144
+ scrollable,
145
+ scrollableContentId,
146
+ scrollableElementId,
147
+ scrollableBoundaryId,
148
+ scrollableBoundaryLowerEdgeRef,
149
+ scrollableBoundaryUpperEdgeRef,
150
+ scrollableScrollTopRef,
151
+ dragOverlayRefMap,
152
+ dirtyKeysRef,
153
+ scrollableStickyLowerOffset,
154
+ scrollableStickyUpperOffset,
45
155
  updateItemSize,
46
156
  setChildrenRef,
47
157
  setChildrenMTSRef,
158
+ setDragOverlayRef,
48
159
  handleDragEnd,
49
160
  handleDragMove,
50
161
  handleDragStart
51
162
  ]);
163
+ const sortableDragOverlayContextValue = useMemo(()=>({
164
+ ...sortableContextValue,
165
+ isDragOverlay: true
166
+ }), [
167
+ sortableContextValue
168
+ ]);
52
169
  const renderedChildren = useMemo(()=>data?.map((item)=>children(item)), [
53
170
  data,
54
171
  children
55
172
  ]);
173
+ const renderedDragOverlayChildren = useMemo(()=>data?.map((item)=>children(item)), [
174
+ data,
175
+ children
176
+ ]);
177
+ if (scrollable) return <>
178
+ <scroll-view id={scrollableElementId} className={scrollableClassName} enable-scroll={scrollableEnableScroll && !isDragging} scroll-orientation='vertical' main-thread:bindscroll={handleScrollableBoundaryScroll}>
179
+ <view id={scrollableContentId} className={scrollableContentClassName} style={{
180
+ zIndex: '0'
181
+ }}>
182
+ <view style='display: flex; flex-direction: column; overflow:hidden; height: 1ppx; width: 100%;' exposure-scene={scrollableElementId} exposure-id='upperExposureView' id={`${scrollableElementId}-upperExposureView`} main-thread:binduiappear={handleScrollableBoundaryUpperExposure} main-thread:binduidisappear={handleScrollableBoundaryUpperDisexposure}/>
183
+ <SortableContext.Provider value={sortableContextValue}>
184
+ {renderedChildren}
185
+ </SortableContext.Provider>
186
+ <view style='display: flex; flex-direction: column; overflow:hidden; height: 1ppx; width: 100%;' exposure-scene={scrollableElementId} exposure-id='lowerExposureView' id={`${scrollableElementId}-lowerExposureView`} main-thread:binduiappear={handleScrollableBoundaryLowerExposure} main-thread:binduidisappear={handleScrollableBoundaryLowerDisexposure}/>
187
+ </view>
188
+ </scroll-view>
189
+ <SortableContext.Provider value={sortableDragOverlayContextValue}>
190
+ {renderedDragOverlayChildren}
191
+ </SortableContext.Provider>
192
+ </>;
56
193
  return <SortableContext.Provider value={sortableContextValue}>
57
194
  {renderedChildren}
58
195
  </SortableContext.Provider>;
59
196
  }
60
197
  function SortableItem(props) {
198
+ const { isDragOverlay } = useContext(SortableContext);
199
+ if (isDragOverlay) return <SortableDragOverlayItem {...props}/>;
200
+ return <SortableInteractiveItem {...props}/>;
201
+ }
202
+ function SortableDragOverlayItem(props) {
203
+ const { className, children, sortingKey } = props;
204
+ const { setDragOverlayRef } = useContext(SortableContext);
205
+ const overlayRef = useMainThreadRef(null);
206
+ const overlayElementId = useMemo(()=>getSortableDragOverlayElementId(sortingKey), [
207
+ sortingKey
208
+ ]);
209
+ const overlayStyle = useMemo(()=>({
210
+ position: 'fixed',
211
+ top: '0px',
212
+ left: '0px',
213
+ width: '0px',
214
+ height: '0px',
215
+ opacity: 0,
216
+ visibility: 'hidden',
217
+ zIndex: '10000',
218
+ transform: 'translate(0px, 0px)'
219
+ }), []);
220
+ useEffect(()=>{
221
+ runOnMainThread(setDragOverlayRef)(overlayRef, sortingKey);
222
+ }, [
223
+ overlayRef,
224
+ setDragOverlayRef,
225
+ sortingKey
226
+ ]);
227
+ return <view id={overlayElementId} className={className} main-thread:ref={overlayRef} style={overlayStyle} user-interaction-enabled={false}>
228
+ {children}
229
+ </view>;
230
+ }
231
+ function SortableInteractiveItem(props) {
61
232
  const { className, children, sortingKey, as = 'Draggable' } = props;
62
- const { data, enableSorting, boundaryId, updateItemSize, setChildrenRef, setChildrenMTSRef, handleDragStart, handleDragEnd, handleDragMove } = useContext(SortableContext);
233
+ const { data, enableSorting, boundaryId, scrollableBoundaryId, scrollableBoundaryUpperEdgeRef, scrollableBoundaryLowerEdgeRef, scrollableScrollTopRef, dragOverlayRefMap, dirtyKeysRef, scrollableStickyUpperOffset, scrollableStickyLowerOffset, updateItemSize, setChildrenRef, setChildrenMTSRef, handleDragStart, handleDragEnd, handleDragMove } = useContext(SortableContext);
63
234
  const MTSRef = useMainThreadRef(null);
64
- const ref = useRef(null);
65
235
  const [itemRect, setItemRect] = useState({
66
236
  height: 0,
67
237
  width: 0,
@@ -78,69 +248,504 @@ function SortableItem(props) {
78
248
  bottom: 0,
79
249
  right: 0
80
250
  });
81
- useEffect(()=>{
82
- runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey);
83
- if (boundaryId) {
84
- ref?.current?.invoke({
85
- method: 'boundingClientRect',
86
- params: {},
87
- success: (res)=>{
88
- setItemRect(res);
89
- }
90
- }).exec();
91
- lynx.createSelectorQuery().select(`#${boundaryId}`)?.invoke({
92
- method: 'boundingClientRect',
93
- params: {},
94
- success: (res)=>{
95
- setBoundaryRect(res);
251
+ const [scrollableBoundaryRect, setScrollableBoundaryRect] = useState({
252
+ height: 0,
253
+ width: 0,
254
+ top: 0,
255
+ left: 0,
256
+ bottom: 0,
257
+ right: 0
258
+ });
259
+ const autoScrollingRef = useMainThreadRef(false);
260
+ const lastAutoScrollTranslateY = useMainThreadRef(0);
261
+ const autoScrollDistanceSum = useMainThreadRef(0);
262
+ const autoScrollCompensationY = useMainThreadRef(0);
263
+ const autoScrollStartScrollTop = useMainThreadRef(0);
264
+ const itemMeasuredScrollTop = useMainThreadRef(0);
265
+ const dragStartScrollDeltaFromMeasuredRect = useMainThreadRef(0);
266
+ const autoScrollOverflow = useMainThreadRef(0);
267
+ const autoScrollDirection = useMainThreadRef(0);
268
+ const autoScrollStickyDirection = useMainThreadRef(0);
269
+ const dragSourceHidden = useMainThreadRef(false);
270
+ const latestDragTranslate = useMainThreadRef({
271
+ x: 0,
272
+ y: 0
273
+ });
274
+ const latestDragEvent = useMainThreadRef(null);
275
+ const autoScrollFrameRef = useMainThreadRef(null);
276
+ const syncScrollDeltaForItemTranslateY = useCallback((scrollByResult, fallbackScrollByOffset = 0)=>{
277
+ 'main thread';
278
+ if (scrollableScrollTopRef) {
279
+ const previousScrollTop = scrollableScrollTopRef.current;
280
+ const scrollTop = scrollByResult?.scrollTop ?? scrollByResult?.detail?.scrollTop;
281
+ if ('number' == typeof scrollTop) scrollableScrollTopRef.current = scrollTop;
282
+ else if (scrollByResult || 0 !== fallbackScrollByOffset) scrollableScrollTopRef.current += fallbackScrollByOffset;
283
+ if (scrollByResult || 0 !== fallbackScrollByOffset) autoScrollDistanceSum.current += scrollableScrollTopRef.current - previousScrollTop;
284
+ autoScrollCompensationY.current = scrollableScrollTopRef.current - autoScrollStartScrollTop.current;
285
+ return {
286
+ dragDeltaY: autoScrollCompensationY.current,
287
+ measuredRectDeltaY: scrollableScrollTopRef.current - itemMeasuredScrollTop.current
288
+ };
289
+ }
290
+ if (0 !== fallbackScrollByOffset) {
291
+ autoScrollDistanceSum.current += fallbackScrollByOffset;
292
+ autoScrollCompensationY.current += fallbackScrollByOffset;
293
+ }
294
+ return {
295
+ dragDeltaY: autoScrollCompensationY.current,
296
+ measuredRectDeltaY: dragStartScrollDeltaFromMeasuredRect.current + autoScrollDistanceSum.current
297
+ };
298
+ }, [
299
+ autoScrollDistanceSum,
300
+ autoScrollCompensationY,
301
+ autoScrollStartScrollTop,
302
+ dragStartScrollDeltaFromMeasuredRect,
303
+ itemMeasuredScrollTop,
304
+ scrollableScrollTopRef
305
+ ]);
306
+ const itemElementId = useMemo(()=>getSortableItemElementId(sortingKey), [
307
+ sortingKey
308
+ ]);
309
+ const setAutoScrolling = useCallback((isAutoScrolling)=>{
310
+ 'main thread';
311
+ autoScrollingRef.current = isAutoScrolling;
312
+ }, [
313
+ autoScrollingRef
314
+ ]);
315
+ const captureItemMeasuredScrollTop = useCallback(()=>{
316
+ 'main thread';
317
+ itemMeasuredScrollTop.current = scrollableScrollTopRef?.current ?? 0;
318
+ }, [
319
+ itemMeasuredScrollTop,
320
+ scrollableScrollTopRef
321
+ ]);
322
+ const measureRectById = useCallback((id, target)=>{
323
+ if (!id) return;
324
+ lynx.createSelectorQuery().select(`#${id}`)?.invoke({
325
+ method: 'boundingClientRect',
326
+ params: {},
327
+ success: (res)=>{
328
+ const rect = res;
329
+ if ('item' === target) {
330
+ setItemRect(rect);
331
+ runOnMainThread(captureItemMeasuredScrollTop)();
332
+ return;
96
333
  }
97
- }).exec();
334
+ if ('boundary' === target) return void setBoundaryRect(rect);
335
+ setScrollableBoundaryRect(rect);
336
+ }
337
+ }).exec();
338
+ }, [
339
+ captureItemMeasuredScrollTop
340
+ ]);
341
+ const refreshDraggingRects = useCallback(()=>{
342
+ measureRectById(itemElementId, 'item');
343
+ measureRectById(boundaryId, 'boundary');
344
+ measureRectById(scrollableBoundaryId, 'scrollableBoundary');
345
+ }, [
346
+ boundaryId,
347
+ itemElementId,
348
+ measureRectById,
349
+ scrollableBoundaryId
350
+ ]);
351
+ const scrollScrollableBoundaryBy = useCallback((offset, onScrolled)=>{
352
+ 'main thread';
353
+ if (!scrollableBoundaryId || 0 === offset) return void onScrolled?.();
354
+ const element = lynx.querySelector(`#${scrollableBoundaryId}`);
355
+ if (!element) return void onScrolled?.();
356
+ element.invoke('scrollBy', {
357
+ offset
358
+ }).then((res)=>{
359
+ 'main thread';
360
+ syncScrollDeltaForItemTranslateY(res, offset);
361
+ onScrolled?.();
362
+ });
363
+ }, [
364
+ autoScrollingRef,
365
+ scrollableBoundaryId,
366
+ sortingKey,
367
+ syncScrollDeltaForItemTranslateY
368
+ ]);
369
+ const isAutoScrollBlocked = useCallback((offset)=>{
370
+ 'main thread';
371
+ if (offset > 0) return scrollableBoundaryLowerEdgeRef?.current === true;
372
+ if (offset < 0) return scrollableBoundaryUpperEdgeRef?.current === true;
373
+ return false;
374
+ }, [
375
+ scrollableBoundaryLowerEdgeRef,
376
+ scrollableBoundaryUpperEdgeRef
377
+ ]);
378
+ const getDragStartItemBounds = useCallback((translateY)=>{
379
+ 'main thread';
380
+ const measuredTopAtDragStart = itemRect.top - dragStartScrollDeltaFromMeasuredRect.current;
381
+ const measuredBottomAtDragStart = itemRect.bottom - dragStartScrollDeltaFromMeasuredRect.current;
382
+ return {
383
+ top: measuredTopAtDragStart + translateY,
384
+ bottom: measuredBottomAtDragStart + translateY
385
+ };
386
+ }, [
387
+ dragStartScrollDeltaFromMeasuredRect,
388
+ itemRect
389
+ ]);
390
+ const getAutoScrollOverflow = useCallback((distanceToTop, distanceToBottom)=>{
391
+ 'main thread';
392
+ return getAutoScrollTriggerOverflow({
393
+ distanceToTop,
394
+ distanceToBottom
395
+ }, scrollableStickyUpperOffset, scrollableStickyLowerOffset);
396
+ }, [
397
+ scrollableStickyLowerOffset,
398
+ scrollableStickyUpperOffset
399
+ ]);
400
+ const getAutoScrollDirection = useCallback((overflowDirection, translateDeltaY)=>{
401
+ 'main thread';
402
+ if (0 === translateDeltaY) return autoScrollDirection.current || overflowDirection;
403
+ const dragDirection = translateDeltaY > 0 ? 1 : -1;
404
+ return dragDirection === overflowDirection ? overflowDirection : 0;
405
+ }, [
406
+ autoScrollDirection
407
+ ]);
408
+ const getVisualTranslateY = useCallback(()=>{
409
+ 'main thread';
410
+ const scrollDelta = syncScrollDeltaForItemTranslateY();
411
+ if (autoScrollStickyDirection.current > 0) return scrollableBoundaryRect.bottom - itemRect.bottom - scrollableStickyLowerOffset + scrollDelta.measuredRectDeltaY;
412
+ if (autoScrollStickyDirection.current < 0) return scrollableBoundaryRect.top - itemRect.top + scrollableStickyUpperOffset + scrollDelta.measuredRectDeltaY;
413
+ return latestDragTranslate.current.y + scrollDelta.dragDeltaY;
414
+ }, [
415
+ autoScrollStickyDirection,
416
+ itemRect,
417
+ latestDragTranslate,
418
+ scrollableBoundaryRect,
419
+ scrollableStickyLowerOffset,
420
+ scrollableStickyUpperOffset,
421
+ syncScrollDeltaForItemTranslateY
422
+ ]);
423
+ const getVisualBounds = useCallback((visualTranslateY)=>{
424
+ 'main thread';
425
+ const scrollDelta = syncScrollDeltaForItemTranslateY();
426
+ return {
427
+ top: itemRect.top - scrollDelta.measuredRectDeltaY + visualTranslateY,
428
+ left: itemRect.left + latestDragTranslate.current.x,
429
+ width: itemRect.width,
430
+ height: itemRect.height
431
+ };
432
+ }, [
433
+ itemRect,
434
+ latestDragTranslate,
435
+ syncScrollDeltaForItemTranslateY
436
+ ]);
437
+ const getDragOverlayElement = useCallback(()=>{
438
+ 'main thread';
439
+ return dragOverlayRefMap?.current?.[sortingKey] ?? null;
440
+ }, [
441
+ dragOverlayRefMap,
442
+ sortingKey
443
+ ]);
444
+ const hideDragSourceItem = useCallback(()=>{
445
+ 'main thread';
446
+ if (!scrollableBoundaryId || dragSourceHidden.current) return;
447
+ MTSRef.current?.MTSSetOtherStyles({
448
+ visibility: 'hidden',
449
+ opacity: '0'
450
+ });
451
+ dragSourceHidden.current = true;
452
+ dirtyKeysRef.current[sortingKey] = true;
453
+ }, [
454
+ MTSRef,
455
+ dragSourceHidden,
456
+ scrollableBoundaryId,
457
+ dirtyKeysRef,
458
+ sortingKey
459
+ ]);
460
+ const showDragSourceItem = useCallback(()=>{
461
+ 'main thread';
462
+ if (!dragSourceHidden.current) return;
463
+ MTSRef.current?.MTSSetOtherStyles({
464
+ visibility: 'visible',
465
+ opacity: '1'
466
+ });
467
+ dragSourceHidden.current = false;
468
+ }, [
469
+ MTSRef,
470
+ dragSourceHidden
471
+ ]);
472
+ const updateDragOverlayTransform = useCallback((visualTranslateY)=>{
473
+ 'main thread';
474
+ const overlay = getDragOverlayElement();
475
+ if (!overlay) return;
476
+ const bounds = getVisualBounds(visualTranslateY);
477
+ overlay.setStyleProperty('transform', `translate(${bounds.left}px, ${bounds.top}px)`);
478
+ }, [
479
+ getDragOverlayElement,
480
+ getVisualBounds
481
+ ]);
482
+ const activateDragOverlay = useCallback(()=>{
483
+ 'main thread';
484
+ if (!scrollableBoundaryId) return;
485
+ hideDragSourceItem();
486
+ const overlay = getDragOverlayElement();
487
+ if (!overlay) return;
488
+ const visualTranslateY = getVisualTranslateY();
489
+ const bounds = getVisualBounds(visualTranslateY);
490
+ overlay.setStyleProperties({
491
+ position: 'fixed',
492
+ top: '0px',
493
+ left: '0px',
494
+ width: `${bounds.width}px`,
495
+ height: `${bounds.height}px`,
496
+ opacity: '1',
497
+ visibility: 'visible',
498
+ 'z-index': '10000',
499
+ transform: `translate(${bounds.left}px, ${bounds.top}px)`
500
+ });
501
+ }, [
502
+ getDragOverlayElement,
503
+ getVisualBounds,
504
+ getVisualTranslateY,
505
+ hideDragSourceItem,
506
+ scrollableBoundaryId
507
+ ]);
508
+ const applyVisualTranslate = useCallback(()=>{
509
+ 'main thread';
510
+ const visualTranslateY = getVisualTranslateY();
511
+ if (scrollableBoundaryId) {
512
+ MTSRef.current?.MTSSetTransform(0, 0);
513
+ console.info('reset local drag visual');
514
+ updateDragOverlayTransform(visualTranslateY);
515
+ } else MTSRef.current?.MTSSetTransform(latestDragTranslate.current.x, visualTranslateY);
516
+ dirtyKeysRef.current[sortingKey] = true;
517
+ return visualTranslateY;
518
+ }, [
519
+ MTSRef,
520
+ getVisualTranslateY,
521
+ latestDragTranslate,
522
+ scrollableBoundaryId,
523
+ updateDragOverlayTransform,
524
+ dirtyKeysRef,
525
+ sortingKey
526
+ ]);
527
+ const emitSortableDragMove = useCallback((visualTranslateY, event)=>{
528
+ 'main thread';
529
+ if (!event) return;
530
+ handleDragMove?.({
531
+ x: latestDragTranslate.current.x,
532
+ y: visualTranslateY
533
+ }, sortingKey, event);
534
+ }, [
535
+ handleDragMove,
536
+ latestDragTranslate,
537
+ sortingKey
538
+ ]);
539
+ const scheduleNextAutoScrollFrame = useCallback(()=>{
540
+ 'main thread';
541
+ const nextFrame = autoScrollFrameRef.current;
542
+ if (nextFrame) setTimeout(nextFrame, 8);
543
+ }, [
544
+ autoScrollFrameRef
545
+ ]);
546
+ const finishAutoScrollFrame = useCallback(()=>{
547
+ 'main thread';
548
+ const visualTranslateY = applyVisualTranslate();
549
+ emitSortableDragMove(visualTranslateY, latestDragEvent.current);
550
+ scheduleNextAutoScrollFrame();
551
+ }, [
552
+ applyVisualTranslate,
553
+ emitSortableDragMove,
554
+ latestDragEvent,
555
+ scheduleNextAutoScrollFrame
556
+ ]);
557
+ const runAutoScrollFrame = useCallback(()=>{
558
+ 'main thread';
559
+ if (!autoScrollingRef.current) return;
560
+ const overflow = autoScrollOverflow.current;
561
+ if (0 === overflow) return void setAutoScrolling(false);
562
+ const direction = autoScrollDirection.current || (overflow > 0 ? 1 : -1);
563
+ if (isAutoScrollBlocked(direction)) {
564
+ const visualTranslateY = applyVisualTranslate();
565
+ emitSortableDragMove(visualTranslateY, latestDragEvent.current);
566
+ setAutoScrolling(false);
567
+ return;
98
568
  }
569
+ const step = direction * Math.min(AUTO_SCROLL_MAX_STEP, Math.abs(overflow) * AUTO_SCROLL_VELOCITY_FACTOR);
570
+ const finishScrolledFrame = ()=>{
571
+ 'main thread';
572
+ finishAutoScrollFrame();
573
+ };
574
+ scrollScrollableBoundaryBy(step, finishScrolledFrame);
99
575
  }, [
576
+ applyVisualTranslate,
577
+ autoScrollDirection,
578
+ autoScrollOverflow,
579
+ autoScrollingRef,
580
+ emitSortableDragMove,
581
+ finishAutoScrollFrame,
582
+ isAutoScrollBlocked,
583
+ latestDragEvent,
584
+ scrollScrollableBoundaryBy,
585
+ setAutoScrolling
586
+ ]);
587
+ const startAutoScrollLoop = useCallback(()=>{
588
+ 'main thread';
589
+ if (autoScrollingRef.current) return;
590
+ autoScrollFrameRef.current = runAutoScrollFrame;
591
+ setAutoScrolling(true);
592
+ scheduleNextAutoScrollFrame();
593
+ }, [
594
+ autoScrollingRef,
595
+ runAutoScrollFrame,
596
+ scheduleNextAutoScrollFrame,
597
+ setAutoScrolling
598
+ ]);
599
+ const stopAutoScrollLoop = useCallback(()=>{
600
+ 'main thread';
601
+ autoScrollOverflow.current = 0;
602
+ autoScrollDirection.current = 0;
603
+ autoScrollStickyDirection.current = 0;
604
+ setAutoScrolling(false);
605
+ }, [
606
+ autoScrollDirection,
607
+ autoScrollOverflow,
608
+ autoScrollStickyDirection,
609
+ setAutoScrolling
610
+ ]);
611
+ useEffect(()=>{
612
+ runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey);
613
+ refreshDraggingRects();
614
+ }, [
615
+ data,
616
+ refreshDraggingRects,
100
617
  setChildrenRef,
101
618
  setChildrenMTSRef,
102
- sortingKey,
103
- data,
104
- boundaryId
619
+ sortingKey
105
620
  ]);
106
621
  const handleMTSLayoutChange = (e)=>{
107
622
  'main thread';
108
623
  updateItemSize(sortingKey, e.detail.height);
109
624
  };
625
+ const resetAutoScrollDragState = useCallback(()=>{
626
+ 'main thread';
627
+ lastAutoScrollTranslateY.current = 0;
628
+ autoScrollDistanceSum.current = 0;
629
+ autoScrollCompensationY.current = 0;
630
+ autoScrollOverflow.current = 0;
631
+ autoScrollDirection.current = 0;
632
+ autoScrollStickyDirection.current = 0;
633
+ latestDragTranslate.current = {
634
+ x: 0,
635
+ y: 0
636
+ };
637
+ }, [
638
+ autoScrollCompensationY,
639
+ autoScrollDirection,
640
+ autoScrollDistanceSum,
641
+ autoScrollOverflow,
642
+ autoScrollStickyDirection,
643
+ lastAutoScrollTranslateY,
644
+ latestDragTranslate
645
+ ]);
110
646
  const itemDragStart = (pagePoint, event)=>{
111
647
  'main thread';
648
+ resetAutoScrollDragState();
649
+ const scrollTop = scrollableScrollTopRef?.current ?? 0;
650
+ autoScrollStartScrollTop.current = scrollTop;
651
+ dragStartScrollDeltaFromMeasuredRect.current = scrollTop - itemMeasuredScrollTop.current;
652
+ latestDragEvent.current = event;
653
+ if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
654
+ const draggedItem = getDragStartItemBounds(0);
655
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, scrollableBoundaryRect);
656
+ const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom);
657
+ if (0 !== overflow) autoScrollStickyDirection.current = overflow > 0 ? 1 : -1;
658
+ }
659
+ activateDragOverlay();
112
660
  handleDragStart?.(pagePoint, sortingKey, event);
661
+ if (0 !== autoScrollStickyDirection.current) {
662
+ const visualTranslateY = getVisualTranslateY();
663
+ emitSortableDragMove(visualTranslateY, event);
664
+ }
113
665
  };
114
666
  const itemDragging = (translate, event)=>{
115
667
  'main thread';
668
+ latestDragTranslate.current = translate;
669
+ latestDragEvent.current = event;
670
+ if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
671
+ const draggedItem = getDragStartItemBounds(translate.y);
672
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, scrollableBoundaryRect);
673
+ const translateDeltaY = translate.y - lastAutoScrollTranslateY.current;
674
+ const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom);
675
+ lastAutoScrollTranslateY.current = translate.y;
676
+ if (0 === overflow) {
677
+ stopAutoScrollLoop();
678
+ const visualTranslateY = applyVisualTranslate();
679
+ emitSortableDragMove(visualTranslateY, event);
680
+ } else {
681
+ const overflowDirection = overflow > 0 ? 1 : -1;
682
+ const scrollDirection = getAutoScrollDirection(overflowDirection, translateDeltaY);
683
+ autoScrollOverflow.current = overflow;
684
+ autoScrollStickyDirection.current = overflowDirection;
685
+ autoScrollDirection.current = scrollDirection;
686
+ const visualTranslateY = applyVisualTranslate();
687
+ const blockedByScrollableEdge = 0 !== scrollDirection && isAutoScrollBlocked(scrollDirection);
688
+ if (0 === scrollDirection) setAutoScrolling(false);
689
+ else if (blockedByScrollableEdge) setAutoScrolling(false);
690
+ else startAutoScrollLoop();
691
+ emitSortableDragMove(visualTranslateY, event);
692
+ }
693
+ return;
694
+ }
116
695
  handleDragMove?.(translate, sortingKey, event);
117
696
  };
697
+ const resetLocalDragVisuals = ()=>{
698
+ 'main thread';
699
+ if (!dirtyKeysRef.current[sortingKey]) return;
700
+ console.info('reset local drag visual2');
701
+ MTSRef.current?.MTSSetTransform(0, 0);
702
+ const overlayWasActive = dragSourceHidden.current;
703
+ showDragSourceItem();
704
+ if (overlayWasActive) {
705
+ const overlay = getDragOverlayElement();
706
+ if (overlay) overlay.setStyleProperties({
707
+ opacity: '0',
708
+ visibility: 'hidden',
709
+ transform: 'translate(0px, 0px)'
710
+ });
711
+ }
712
+ dirtyKeysRef.current[sortingKey] = false;
713
+ };
118
714
  const itemDragEnd = (_pagePoint, event)=>{
119
715
  'main thread';
120
- handleDragEnd?.(sortingKey, event);
716
+ resetAutoScrollDragState();
717
+ autoScrollStartScrollTop.current = 0;
718
+ dragStartScrollDeltaFromMeasuredRect.current = 0;
719
+ latestDragEvent.current = null;
720
+ stopAutoScrollLoop();
721
+ const orderChanged = handleDragEnd?.(sortingKey, event) ?? false;
722
+ if (!orderChanged) resetLocalDragVisuals();
121
723
  };
122
- const resetStyle = useMemo(()=>({
123
- transform: 'translate(0, 0)',
124
- zIndex: '0'
125
- }), [
724
+ const dataKeyOrderSignature = useMemo(()=>(data ?? []).map((item)=>item.getSortingKey()).join('|'), [
126
725
  data
127
726
  ]);
128
- if ('Draggable' === as) return <Draggable MTSRef={MTSRef} ref={ref} trigger='immediate' style={resetStyle} className={className} enableDragging={enableSorting} draggableProps={{
727
+ usePreCommit(()=>{
728
+ 'main thread';
729
+ resetLocalDragVisuals();
730
+ }, [
731
+ dataKeyOrderSignature
732
+ ]);
733
+ if ('Draggable' === as) return <Draggable id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} enableDragging={enableSorting} draggableProps={{
129
734
  'main-thread:bindlayoutchange': handleMTSLayoutChange
130
735
  }} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} allowedDirection={[
131
736
  'up',
132
737
  'down'
133
- ]} {...boundaryId && {
134
- minTranslateY: -(itemRect?.top - boundaryRect?.top),
135
- maxTranslateY: boundaryRect?.bottom - itemRect?.bottom
738
+ ]} {...!scrollableBoundaryId && boundaryId && {
739
+ minTranslateY: -(itemRect.top - boundaryRect.top),
740
+ maxTranslateY: boundaryRect.bottom - itemRect.bottom
136
741
  }}>
137
742
  {children}
138
743
  </Draggable>;
139
- return <DraggableRoot MTSRef={MTSRef} ref={ref} trigger='immediate' style={resetStyle} className={className} draggableProps={{
744
+ return <DraggableRoot id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} draggableProps={{
140
745
  'main-thread:bindlayoutchange': handleMTSLayoutChange
141
- }} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} enableDragging={enableSorting} {...boundaryId && {
142
- minTranslateY: -(itemRect?.top - boundaryRect?.top),
143
- maxTranslateY: boundaryRect?.bottom - itemRect?.bottom
746
+ }} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} enableDragging={enableSorting} {...!scrollableBoundaryId && boundaryId && {
747
+ minTranslateY: -(itemRect.top - boundaryRect.top),
748
+ maxTranslateY: boundaryRect.bottom - itemRect.bottom
144
749
  }} allowedDirection={[
145
750
  'up',
146
751
  'down'