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