@lynx-js/lynx-ui-sortable 3.139.0 → 3.140.1

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,7 +1,8 @@
1
- import { runOnMainThread, useCallback, useContext, useEffect, useMainThreadRef, useMemo, useState } from "@lynx-js/react";
1
+ import { memo, runOnMainThread, useCallback, useContext, useEffect, useMainThreadRef, useMemo, useState } from "@lynx-js/react";
2
2
  import { delayFrames, usePreCommit } from "@lynx-js/lynx-ui-common";
3
3
  import { Draggable, DraggableArea, DraggableRoot } from "@lynx-js/lynx-ui-draggable";
4
- import { SortableContext } from "./SortableContext.jsx";
4
+ import { commitSortableOrder } from "./commitSortableOrder.jsx";
5
+ import { SortableContext, SortableOrderContext } from "./SortableContext.jsx";
5
6
  import { useSortable } from "./useSortable.jsx";
6
7
  function getEdgeDistance(itemBounds, boundaryRect) {
7
8
  'main thread';
@@ -18,6 +19,25 @@ function getAutoScrollTriggerOverflow(edgeDistance, upperTriggerZone, lowerTrigg
18
19
  }
19
20
  const AUTO_SCROLL_MAX_STEP = 18;
20
21
  const AUTO_SCROLL_VELOCITY_FACTOR = 0.35;
22
+ const Sortable_SortableItemsProvider = memo(function(props) {
23
+ return <SortableContext.Provider value={props.value}>
24
+ {props.children}
25
+ </SortableContext.Provider>;
26
+ });
27
+ function RenderSortableItem(props) {
28
+ return props.renderItem(props.item);
29
+ }
30
+ const SortableRenderedItem = memo(RenderSortableItem);
31
+ function SortableItemGeometryObserver(props) {
32
+ const orderSignature = useContext(SortableOrderContext);
33
+ useEffect(()=>{
34
+ delayFrames(1, props.refresh);
35
+ }, [
36
+ orderSignature,
37
+ props.refresh
38
+ ]);
39
+ return null;
40
+ }
21
41
  function getNormalizedElementKey(sortingKey) {
22
42
  return String(sortingKey).replace(/[^\w-]/g, '-');
23
43
  }
@@ -30,15 +50,16 @@ function getSortableDragOverlayElementId(sortingKey) {
30
50
  function SortableRoot(props) {
31
51
  const { children, data, debugLog = false, boundaryId, scrollableBoundaryId, as, scrollableClassName, scrollableContentClassName, scrollableEnableScroll = true, scrollableStickyUpperOffset = 0, scrollableStickyLowerOffset = 0, onSortEnd, onSortStart, enableSorting = true } = props;
32
52
  const scrollable = 'ScrollView' === as;
33
- const [isDragging, setIsDragging] = useState(false);
34
- const handleInternalSortStart = useCallback(()=>{
35
- setIsDragging(true);
53
+ const [activeDragKey, setActiveDragKey] = useState(null);
54
+ const isDragging = null !== activeDragKey;
55
+ const handleInternalSortStart = useCallback((sortingKey)=>{
56
+ setActiveDragKey(sortingKey);
36
57
  onSortStart?.();
37
58
  }, [
38
59
  onSortStart
39
60
  ]);
40
61
  const handleInternalSortEnd = useCallback((sortedData)=>{
41
- setIsDragging(false);
62
+ setActiveDragKey(null);
42
63
  onSortEnd(sortedData);
43
64
  }, [
44
65
  onSortEnd
@@ -53,10 +74,26 @@ function SortableRoot(props) {
53
74
  const childrenRefMap = useMainThreadRef({});
54
75
  const childrenMTSRefMap = useMainThreadRef({});
55
76
  const dragOverlayRefMap = useMainThreadRef({});
77
+ const dragOverlayActivatorRefMap = useMainThreadRef({});
56
78
  const dirtyKeysRef = useMainThreadRef({});
79
+ const resetItemVisualsRef = useMainThreadRef({});
80
+ const keyArray = useMemo(()=>data?.map((item)=>item.getSortingKey()) ?? [], [
81
+ data
82
+ ]);
83
+ const dataKeySignature = JSON.stringify(keyArray);
84
+ const orderRef = useMainThreadRef(keyArray);
85
+ usePreCommit(()=>{
86
+ 'main thread';
87
+ commitSortableOrder(orderRef, keyArray, dirtyKeysRef, resetItemVisualsRef, null === activeDragKey);
88
+ }, [
89
+ dataKeySignature,
90
+ activeDragKey
91
+ ]);
57
92
  const disabledKeysRef = useMainThreadRef({});
58
93
  const scrollableBoundaryUpperEdgeRef = useMainThreadRef(false);
59
94
  const scrollableBoundaryLowerEdgeRef = useMainThreadRef(false);
95
+ const scrollableBoundaryRectRef = useMainThreadRef(null);
96
+ const scrollableBoundaryMeasurementVersion = useMainThreadRef(0);
60
97
  const scrollableScrollTopRef = useMainThreadRef(0);
61
98
  const updateItemSize = useCallback((sortingKey, size)=>{
62
99
  'main thread';
@@ -92,6 +129,47 @@ function SortableRoot(props) {
92
129
  }, [
93
130
  scrollableBoundaryLowerEdgeRef
94
131
  ]);
132
+ const measureScrollableBoundary = useCallback(()=>{
133
+ 'main thread';
134
+ if (!scrollable) return;
135
+ const measurementVersion = scrollableBoundaryMeasurementVersion.current + 1;
136
+ scrollableBoundaryMeasurementVersion.current = measurementVersion;
137
+ const measurement = lynx.querySelector(`#${scrollableElementId}`)?.invoke('boundingClientRect', {});
138
+ measurement?.then((value)=>{
139
+ 'main thread';
140
+ if (scrollableBoundaryMeasurementVersion.current !== measurementVersion) return;
141
+ scrollableBoundaryRectRef.current = value;
142
+ });
143
+ }, [
144
+ scrollable,
145
+ scrollableBoundaryMeasurementVersion,
146
+ scrollableBoundaryRectRef,
147
+ scrollableElementId
148
+ ]);
149
+ const handleScrollableBoundaryLayoutChange = useCallback(()=>{
150
+ 'main thread';
151
+ measureScrollableBoundary();
152
+ }, [
153
+ measureScrollableBoundary
154
+ ]);
155
+ const invalidateScrollableBoundaryMeasurement = useCallback(()=>{
156
+ 'main thread';
157
+ scrollableBoundaryMeasurementVersion.current += 1;
158
+ scrollableBoundaryRectRef.current = null;
159
+ }, [
160
+ scrollableBoundaryMeasurementVersion,
161
+ scrollableBoundaryRectRef
162
+ ]);
163
+ useEffect(()=>{
164
+ if (scrollable) runOnMainThread(measureScrollableBoundary)();
165
+ return ()=>{
166
+ runOnMainThread(invalidateScrollableBoundaryMeasurement)();
167
+ };
168
+ }, [
169
+ invalidateScrollableBoundaryMeasurement,
170
+ measureScrollableBoundary,
171
+ scrollable
172
+ ]);
95
173
  const setChildrenRef = useCallback((refI, key)=>{
96
174
  'main thread';
97
175
  childrenRefMap.current[key] = refI.current;
@@ -102,12 +180,22 @@ function SortableRoot(props) {
102
180
  }, []);
103
181
  const setDragOverlayRef = useCallback((refI, key)=>{
104
182
  'main thread';
105
- dragOverlayRefMap.current[key] = refI.current;
183
+ const overlay = refI.current;
184
+ dragOverlayRefMap.current[key] = overlay;
185
+ if (overlay) dragOverlayActivatorRefMap.current[key]?.();
186
+ }, [
187
+ dragOverlayActivatorRefMap,
188
+ dragOverlayRefMap
189
+ ]);
190
+ const clearDragOverlayRef = useCallback((key)=>{
191
+ 'main thread';
192
+ delete dragOverlayRefMap.current[key];
106
193
  }, [
107
194
  dragOverlayRefMap
108
195
  ]);
109
196
  const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
110
197
  data: data,
198
+ orderRef,
111
199
  sizeMap: sizeMap,
112
200
  itemRefMap: childrenRefMap,
113
201
  itemMTSRefMap: childrenMTSRefMap,
@@ -118,16 +206,18 @@ function SortableRoot(props) {
118
206
  debugLog
119
207
  });
120
208
  const sortableContextValue = useMemo(()=>({
121
- data,
122
209
  isDragOverlay: false,
123
210
  debugLog,
211
+ resetItemVisualsRef,
124
212
  enableSorting,
125
213
  boundaryId: scrollable ? scrollableContentId : boundaryId,
126
214
  scrollableBoundaryId: scrollable ? scrollableElementId : scrollableBoundaryId,
127
215
  scrollableBoundaryUpperEdgeRef: scrollable ? scrollableBoundaryUpperEdgeRef : void 0,
128
216
  scrollableBoundaryLowerEdgeRef: scrollable ? scrollableBoundaryLowerEdgeRef : void 0,
217
+ scrollableBoundaryRectRef: scrollable ? scrollableBoundaryRectRef : void 0,
129
218
  scrollableScrollTopRef: scrollable ? scrollableScrollTopRef : void 0,
130
219
  dragOverlayRefMap: scrollable ? dragOverlayRefMap : void 0,
220
+ dragOverlayActivatorRefMap: scrollable ? dragOverlayActivatorRefMap : void 0,
131
221
  dirtyKeysRef,
132
222
  disabledKeysRef,
133
223
  scrollableStickyUpperOffset,
@@ -136,12 +226,13 @@ function SortableRoot(props) {
136
226
  setChildrenRef,
137
227
  setChildrenMTSRef,
138
228
  setDragOverlayRef,
229
+ clearDragOverlayRef,
139
230
  handleDragEnd,
140
231
  handleDragMove,
141
232
  handleDragStart
142
233
  }), [
143
- data,
144
234
  debugLog,
235
+ resetItemVisualsRef,
145
236
  enableSorting,
146
237
  boundaryId,
147
238
  scrollable,
@@ -149,9 +240,11 @@ function SortableRoot(props) {
149
240
  scrollableElementId,
150
241
  scrollableBoundaryId,
151
242
  scrollableBoundaryLowerEdgeRef,
243
+ scrollableBoundaryRectRef,
152
244
  scrollableBoundaryUpperEdgeRef,
153
245
  scrollableScrollTopRef,
154
246
  dragOverlayRefMap,
247
+ dragOverlayActivatorRefMap,
155
248
  dirtyKeysRef,
156
249
  disabledKeysRef,
157
250
  scrollableStickyLowerOffset,
@@ -160,6 +253,7 @@ function SortableRoot(props) {
160
253
  setChildrenRef,
161
254
  setChildrenMTSRef,
162
255
  setDragOverlayRef,
256
+ clearDragOverlayRef,
163
257
  handleDragEnd,
164
258
  handleDragMove,
165
259
  handleDragStart
@@ -170,33 +264,40 @@ function SortableRoot(props) {
170
264
  }), [
171
265
  sortableContextValue
172
266
  ]);
173
- const renderedChildren = useMemo(()=>data?.map((item)=>children(item)), [
267
+ const renderedChildren = useMemo(()=>data?.map((item)=><SortableRenderedItem key={item.getSortingKey()} item={item} renderItem={children}/>), [
174
268
  data,
175
269
  children
176
270
  ]);
177
- const renderedDragOverlayChildren = useMemo(()=>data?.map((item)=>children(item)), [
178
- data,
179
- children
271
+ const renderedDragOverlayChild = useMemo(()=>{
272
+ if (null === activeDragKey) return null;
273
+ const activeItem = data?.find((item)=>item.getSortingKey() === activeDragKey);
274
+ return activeItem ? children(activeItem) : null;
275
+ }, [
276
+ activeDragKey,
277
+ children,
278
+ data
180
279
  ]);
181
280
  if (scrollable) return <>
182
- <scroll-view id={scrollableElementId} className={scrollableClassName} enable-scroll={scrollableEnableScroll && !isDragging} scroll-orientation='vertical' main-thread:bindscroll={handleScrollableBoundaryScroll}>
281
+ <scroll-view id={scrollableElementId} className={scrollableClassName} enable-scroll={scrollableEnableScroll && !isDragging} scroll-orientation='vertical' main-thread:bindlayoutchange={handleScrollableBoundaryLayoutChange} main-thread:bindscroll={handleScrollableBoundaryScroll}>
183
282
  <view id={scrollableContentId} className={scrollableContentClassName} style={{
184
283
  zIndex: '0'
185
284
  }}>
186
285
  <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}>
286
+ <Sortable_SortableItemsProvider value={sortableContextValue}>
188
287
  {renderedChildren}
189
- </SortableContext.Provider>
288
+ </Sortable_SortableItemsProvider>
190
289
  <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
290
  </view>
192
291
  </scroll-view>
193
292
  <SortableContext.Provider value={sortableDragOverlayContextValue}>
194
- {renderedDragOverlayChildren}
293
+ {renderedDragOverlayChild}
195
294
  </SortableContext.Provider>
196
295
  </>;
197
- return <SortableContext.Provider value={sortableContextValue}>
198
- {renderedChildren}
199
- </SortableContext.Provider>;
296
+ return <SortableOrderContext.Provider value={dataKeySignature}>
297
+ <Sortable_SortableItemsProvider value={sortableContextValue}>
298
+ {renderedChildren}
299
+ </Sortable_SortableItemsProvider>
300
+ </SortableOrderContext.Provider>;
200
301
  }
201
302
  function SortableItem(props) {
202
303
  const { isDragOverlay } = useContext(SortableContext);
@@ -205,7 +306,7 @@ function SortableItem(props) {
205
306
  }
206
307
  function SortableDragOverlayItem(props) {
207
308
  const { className, children, sortingKey } = props;
208
- const { setDragOverlayRef } = useContext(SortableContext);
309
+ const { clearDragOverlayRef, setDragOverlayRef } = useContext(SortableContext);
209
310
  const overlayRef = useMainThreadRef(null);
210
311
  const overlayElementId = useMemo(()=>getSortableDragOverlayElementId(sortingKey), [
211
312
  sortingKey
@@ -223,7 +324,11 @@ function SortableDragOverlayItem(props) {
223
324
  }), []);
224
325
  useEffect(()=>{
225
326
  runOnMainThread(setDragOverlayRef)(overlayRef, sortingKey);
327
+ return ()=>{
328
+ runOnMainThread(clearDragOverlayRef)(sortingKey);
329
+ };
226
330
  }, [
331
+ clearDragOverlayRef,
227
332
  overlayRef,
228
333
  setDragOverlayRef,
229
334
  sortingKey
@@ -234,7 +339,7 @@ function SortableDragOverlayItem(props) {
234
339
  }
235
340
  function SortableInteractiveItem(props) {
236
341
  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);
342
+ const { resetItemVisualsRef, enableSorting, boundaryId, scrollableBoundaryId, scrollableBoundaryUpperEdgeRef, scrollableBoundaryLowerEdgeRef, scrollableBoundaryRectRef, scrollableScrollTopRef, dragOverlayRefMap, dragOverlayActivatorRefMap, dirtyKeysRef, disabledKeysRef, scrollableStickyUpperOffset, scrollableStickyLowerOffset, updateItemSize, setChildrenMTSRef, handleDragStart, handleDragEnd, handleDragMove } = useContext(SortableContext);
238
343
  const MTSRef = useMainThreadRef(null);
239
344
  const [itemRect, setItemRect] = useState({
240
345
  height: 0,
@@ -271,6 +376,8 @@ function SortableInteractiveItem(props) {
271
376
  const autoScrollDirection = useMainThreadRef(0);
272
377
  const autoScrollStickyDirection = useMainThreadRef(0);
273
378
  const dragSourceHidden = useMainThreadRef(false);
379
+ const activeItemRectRef = useMainThreadRef(null);
380
+ const activeRectMeasurementVersion = useMainThreadRef(0);
274
381
  const latestDragTranslate = useMainThreadRef({
275
382
  x: 0,
276
383
  y: 0
@@ -343,6 +450,7 @@ function SortableInteractiveItem(props) {
343
450
  captureItemMeasuredScrollTop
344
451
  ]);
345
452
  const refreshDraggingRects = useCallback(()=>{
453
+ if (scrollableBoundaryRectRef) return;
346
454
  measureRectById(itemElementId, 'item');
347
455
  measureRectById(boundaryId, 'boundary');
348
456
  measureRectById(scrollableBoundaryId, 'scrollableBoundary');
@@ -350,7 +458,8 @@ function SortableInteractiveItem(props) {
350
458
  boundaryId,
351
459
  itemElementId,
352
460
  measureRectById,
353
- scrollableBoundaryId
461
+ scrollableBoundaryId,
462
+ scrollableBoundaryRectRef
354
463
  ]);
355
464
  const scrollScrollableBoundaryBy = useCallback((offset, onScrolled)=>{
356
465
  'main thread';
@@ -379,17 +488,34 @@ function SortableInteractiveItem(props) {
379
488
  scrollableBoundaryLowerEdgeRef,
380
489
  scrollableBoundaryUpperEdgeRef
381
490
  ]);
491
+ const getCurrentItemRect = useCallback(()=>{
492
+ 'main thread';
493
+ return scrollableBoundaryRectRef ? activeItemRectRef.current : itemRect;
494
+ }, [
495
+ activeItemRectRef,
496
+ itemRect,
497
+ scrollableBoundaryRectRef
498
+ ]);
499
+ const getCurrentScrollableBoundaryRect = useCallback(()=>{
500
+ 'main thread';
501
+ return scrollableBoundaryRectRef ? scrollableBoundaryRectRef.current : scrollableBoundaryRect;
502
+ }, [
503
+ scrollableBoundaryRect,
504
+ scrollableBoundaryRectRef
505
+ ]);
382
506
  const getDragStartItemBounds = useCallback((translateY)=>{
383
507
  'main thread';
384
- const measuredTopAtDragStart = itemRect.top - dragStartScrollDeltaFromMeasuredRect.current;
385
- const measuredBottomAtDragStart = itemRect.bottom - dragStartScrollDeltaFromMeasuredRect.current;
508
+ const currentItemRect = getCurrentItemRect();
509
+ if (!currentItemRect) return null;
510
+ const measuredTopAtDragStart = currentItemRect.top - dragStartScrollDeltaFromMeasuredRect.current;
511
+ const measuredBottomAtDragStart = currentItemRect.bottom - dragStartScrollDeltaFromMeasuredRect.current;
386
512
  return {
387
513
  top: measuredTopAtDragStart + translateY,
388
514
  bottom: measuredBottomAtDragStart + translateY
389
515
  };
390
516
  }, [
391
517
  dragStartScrollDeltaFromMeasuredRect,
392
- itemRect
518
+ getCurrentItemRect
393
519
  ]);
394
520
  const getAutoScrollOverflow = useCallback((distanceToTop, distanceToBottom)=>{
395
521
  'main thread';
@@ -412,29 +538,33 @@ function SortableInteractiveItem(props) {
412
538
  const getVisualTranslateY = useCallback(()=>{
413
539
  'main thread';
414
540
  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;
541
+ const currentItemRect = getCurrentItemRect();
542
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect();
543
+ if (autoScrollStickyDirection.current > 0 && currentItemRect && currentScrollableBoundaryRect) return currentScrollableBoundaryRect.bottom - currentItemRect.bottom - scrollableStickyLowerOffset + scrollDelta.measuredRectDeltaY;
544
+ if (autoScrollStickyDirection.current < 0 && currentItemRect && currentScrollableBoundaryRect) return currentScrollableBoundaryRect.top - currentItemRect.top + scrollableStickyUpperOffset + scrollDelta.measuredRectDeltaY;
417
545
  return latestDragTranslate.current.y + scrollDelta.dragDeltaY;
418
546
  }, [
419
547
  autoScrollStickyDirection,
420
- itemRect,
548
+ getCurrentItemRect,
549
+ getCurrentScrollableBoundaryRect,
421
550
  latestDragTranslate,
422
- scrollableBoundaryRect,
423
551
  scrollableStickyLowerOffset,
424
552
  scrollableStickyUpperOffset,
425
553
  syncScrollDeltaForItemTranslateY
426
554
  ]);
427
555
  const getVisualBounds = useCallback((visualTranslateY)=>{
428
556
  'main thread';
557
+ const currentItemRect = getCurrentItemRect();
558
+ if (!currentItemRect) return null;
429
559
  const scrollDelta = syncScrollDeltaForItemTranslateY();
430
560
  return {
431
- top: itemRect.top - scrollDelta.measuredRectDeltaY + visualTranslateY,
432
- left: itemRect.left + latestDragTranslate.current.x,
433
- width: itemRect.width,
434
- height: itemRect.height
561
+ top: currentItemRect.top - scrollDelta.measuredRectDeltaY + visualTranslateY,
562
+ left: currentItemRect.left + latestDragTranslate.current.x,
563
+ width: currentItemRect.width,
564
+ height: currentItemRect.height
435
565
  };
436
566
  }, [
437
- itemRect,
567
+ getCurrentItemRect,
438
568
  latestDragTranslate,
439
569
  syncScrollDeltaForItemTranslateY
440
570
  ]);
@@ -478,19 +608,22 @@ function SortableInteractiveItem(props) {
478
608
  const overlay = getDragOverlayElement();
479
609
  if (!overlay) return;
480
610
  const bounds = getVisualBounds(visualTranslateY);
611
+ if (!bounds) return;
481
612
  overlay.setStyleProperty('transform', `translate(${bounds.left}px, ${bounds.top}px)`);
482
613
  }, [
483
614
  getDragOverlayElement,
484
615
  getVisualBounds
485
616
  ]);
486
- const activateDragOverlay = useCallback(()=>{
617
+ const tryActivateDragOverlay = useCallback(()=>{
487
618
  'main thread';
488
- if (!scrollableBoundaryId) return;
489
- hideDragSourceItem();
619
+ if (!scrollableBoundaryId || null === latestDragEvent.current) return;
490
620
  const overlay = getDragOverlayElement();
491
621
  if (!overlay) return;
492
622
  const visualTranslateY = getVisualTranslateY();
493
623
  const bounds = getVisualBounds(visualTranslateY);
624
+ if (!bounds) return;
625
+ MTSRef.current?.MTSSetTransform(0, 0);
626
+ hideDragSourceItem();
494
627
  overlay.setStyleProperties({
495
628
  position: 'fixed',
496
629
  top: '0px',
@@ -503,24 +636,63 @@ function SortableInteractiveItem(props) {
503
636
  transform: `translate(${bounds.left}px, ${bounds.top}px)`
504
637
  });
505
638
  }, [
639
+ MTSRef,
506
640
  getDragOverlayElement,
507
641
  getVisualBounds,
508
642
  getVisualTranslateY,
509
643
  hideDragSourceItem,
644
+ latestDragEvent,
510
645
  scrollableBoundaryId
511
646
  ]);
647
+ const measureActiveItemAndTryOverlay = useCallback(()=>{
648
+ 'main thread';
649
+ if (!scrollableBoundaryRectRef || null === latestDragEvent.current) return;
650
+ const measurementVersion = activeRectMeasurementVersion.current + 1;
651
+ activeRectMeasurementVersion.current = measurementVersion;
652
+ activeItemRectRef.current = null;
653
+ const measurement = lynx.querySelector(`#${itemElementId}`)?.invoke('boundingClientRect', {});
654
+ measurement?.then((value)=>{
655
+ 'main thread';
656
+ if (activeRectMeasurementVersion.current !== measurementVersion || null === latestDragEvent.current) return;
657
+ activeItemRectRef.current = value;
658
+ itemMeasuredScrollTop.current = scrollableScrollTopRef?.current ?? 0;
659
+ const currentScrollableBoundaryRect = scrollableBoundaryRectRef.current;
660
+ const draggedItem = getDragStartItemBounds(latestDragTranslate.current.y);
661
+ if (currentScrollableBoundaryRect && currentScrollableBoundaryRect.height > 0 && draggedItem) {
662
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, currentScrollableBoundaryRect);
663
+ const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom);
664
+ if (0 !== overflow) autoScrollStickyDirection.current = overflow > 0 ? 1 : -1;
665
+ }
666
+ tryActivateDragOverlay();
667
+ });
668
+ }, [
669
+ activeItemRectRef,
670
+ activeRectMeasurementVersion,
671
+ autoScrollStickyDirection,
672
+ getAutoScrollOverflow,
673
+ getDragStartItemBounds,
674
+ itemElementId,
675
+ itemMeasuredScrollTop,
676
+ latestDragEvent,
677
+ latestDragTranslate,
678
+ scrollableBoundaryRectRef,
679
+ scrollableScrollTopRef,
680
+ tryActivateDragOverlay
681
+ ]);
512
682
  const applyVisualTranslate = useCallback(()=>{
513
683
  'main thread';
514
684
  const visualTranslateY = getVisualTranslateY();
515
- if (scrollableBoundaryId) {
685
+ if (scrollableBoundaryId) if (dragSourceHidden.current && getDragOverlayElement()) {
516
686
  MTSRef.current?.MTSSetTransform(0, 0);
517
- console.info('reset local drag visual');
518
687
  updateDragOverlayTransform(visualTranslateY);
519
688
  } else MTSRef.current?.MTSSetTransform(latestDragTranslate.current.x, visualTranslateY);
689
+ else MTSRef.current?.MTSSetTransform(latestDragTranslate.current.x, visualTranslateY);
520
690
  dirtyKeysRef.current[sortingKey] = true;
521
691
  return visualTranslateY;
522
692
  }, [
523
693
  MTSRef,
694
+ dragSourceHidden,
695
+ getDragOverlayElement,
524
696
  getVisualTranslateY,
525
697
  latestDragTranslate,
526
698
  scrollableBoundaryId,
@@ -549,11 +721,13 @@ function SortableInteractiveItem(props) {
549
721
  ]);
550
722
  const finishAutoScrollFrame = useCallback(()=>{
551
723
  'main thread';
724
+ if (!autoScrollingRef.current || null === latestDragEvent.current) return;
552
725
  const visualTranslateY = applyVisualTranslate();
553
726
  emitSortableDragMove(visualTranslateY, latestDragEvent.current);
554
727
  scheduleNextAutoScrollFrame();
555
728
  }, [
556
729
  applyVisualTranslate,
730
+ autoScrollingRef,
557
731
  emitSortableDragMove,
558
732
  latestDragEvent,
559
733
  scheduleNextAutoScrollFrame
@@ -612,13 +786,28 @@ function SortableInteractiveItem(props) {
612
786
  autoScrollStickyDirection,
613
787
  setAutoScrolling
614
788
  ]);
789
+ const invalidateActiveItemMeasurement = useCallback(()=>{
790
+ 'main thread';
791
+ activeRectMeasurementVersion.current += 1;
792
+ activeItemRectRef.current = null;
793
+ latestDragEvent.current = null;
794
+ if (dragOverlayActivatorRefMap) dragOverlayActivatorRefMap.current[sortingKey] = null;
795
+ }, [
796
+ activeItemRectRef,
797
+ activeRectMeasurementVersion,
798
+ dragOverlayActivatorRefMap,
799
+ latestDragEvent,
800
+ sortingKey
801
+ ]);
615
802
  useEffect(()=>{
616
803
  runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey);
617
- delayFrames(1, refreshDraggingRects);
804
+ return ()=>{
805
+ runOnMainThread(invalidateActiveItemMeasurement)();
806
+ };
618
807
  }, [
619
- data,
808
+ invalidateActiveItemMeasurement,
620
809
  refreshDraggingRects,
621
- setChildrenRef,
810
+ scrollableBoundaryRectRef,
622
811
  setChildrenMTSRef,
623
812
  sortingKey
624
813
  ]);
@@ -629,20 +818,13 @@ function SortableInteractiveItem(props) {
629
818
  }, [
630
819
  disabledKeysRef
631
820
  ]);
632
- useEffect(()=>{
633
- runOnMainThread(syncDisabledKey)(sortingKey, disabled);
634
- return ()=>{
635
- runOnMainThread(syncDisabledKey)(sortingKey, false);
636
- };
821
+ const handleMTSLayoutChange = useCallback((e)=>{
822
+ 'main thread';
823
+ updateItemSize(sortingKey, e.detail.height);
637
824
  }, [
638
- disabled,
639
825
  sortingKey,
640
- syncDisabledKey
826
+ updateItemSize
641
827
  ]);
642
- const handleMTSLayoutChange = (e)=>{
643
- 'main thread';
644
- updateItemSize(sortingKey, e.detail.height);
645
- };
646
828
  const resetAutoScrollDragState = useCallback(()=>{
647
829
  'main thread';
648
830
  lastAutoScrollTranslateY.current = 0;
@@ -664,33 +846,59 @@ function SortableInteractiveItem(props) {
664
846
  lastAutoScrollTranslateY,
665
847
  latestDragTranslate
666
848
  ]);
667
- const itemDragStart = (pagePoint, event)=>{
849
+ const itemDragStart = useCallback((pagePoint, event)=>{
668
850
  'main thread';
669
851
  resetAutoScrollDragState();
670
852
  const scrollTop = scrollableScrollTopRef?.current ?? 0;
671
853
  autoScrollStartScrollTop.current = scrollTop;
672
- dragStartScrollDeltaFromMeasuredRect.current = scrollTop - itemMeasuredScrollTop.current;
854
+ dragStartScrollDeltaFromMeasuredRect.current = scrollableBoundaryRectRef ? 0 : scrollTop - itemMeasuredScrollTop.current;
673
855
  latestDragEvent.current = event;
674
- if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
856
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect();
857
+ if (scrollableBoundaryId && currentScrollableBoundaryRect && currentScrollableBoundaryRect.height > 0) {
675
858
  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;
859
+ if (draggedItem) {
860
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, currentScrollableBoundaryRect);
861
+ const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom);
862
+ if (0 !== overflow) autoScrollStickyDirection.current = overflow > 0 ? 1 : -1;
863
+ }
679
864
  }
680
- activateDragOverlay();
865
+ if (dragOverlayActivatorRefMap) dragOverlayActivatorRefMap.current[sortingKey] = tryActivateDragOverlay;
866
+ if (scrollableBoundaryRectRef) measureActiveItemAndTryOverlay();
681
867
  handleDragStart?.(pagePoint, sortingKey, event);
682
868
  if (0 !== autoScrollStickyDirection.current) {
683
869
  const visualTranslateY = getVisualTranslateY();
684
870
  emitSortableDragMove(visualTranslateY, event);
685
871
  }
686
- };
687
- const itemDragging = (translate, event)=>{
872
+ }, [
873
+ resetAutoScrollDragState,
874
+ scrollableScrollTopRef,
875
+ autoScrollStartScrollTop,
876
+ dragStartScrollDeltaFromMeasuredRect,
877
+ scrollableBoundaryRectRef,
878
+ itemMeasuredScrollTop,
879
+ latestDragEvent,
880
+ getCurrentScrollableBoundaryRect,
881
+ scrollableBoundaryId,
882
+ getDragStartItemBounds,
883
+ getAutoScrollOverflow,
884
+ autoScrollStickyDirection,
885
+ dragOverlayActivatorRefMap,
886
+ sortingKey,
887
+ tryActivateDragOverlay,
888
+ measureActiveItemAndTryOverlay,
889
+ handleDragStart,
890
+ getVisualTranslateY,
891
+ emitSortableDragMove
892
+ ]);
893
+ const itemDragging = useCallback((translate, event)=>{
688
894
  'main thread';
689
895
  latestDragTranslate.current = translate;
690
896
  latestDragEvent.current = event;
691
- if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
897
+ const currentScrollableBoundaryRect = getCurrentScrollableBoundaryRect();
898
+ if (scrollableBoundaryId && currentScrollableBoundaryRect && currentScrollableBoundaryRect.height > 0) {
692
899
  const draggedItem = getDragStartItemBounds(translate.y);
693
- const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, scrollableBoundaryRect);
900
+ if (!draggedItem) return void handleDragMove?.(translate, sortingKey, event);
901
+ const { distanceToTop, distanceToBottom } = getEdgeDistance(draggedItem, currentScrollableBoundaryRect);
694
902
  const translateDeltaY = translate.y - lastAutoScrollTranslateY.current;
695
903
  const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom);
696
904
  lastAutoScrollTranslateY.current = translate.y;
@@ -714,12 +922,34 @@ function SortableInteractiveItem(props) {
714
922
  return;
715
923
  }
716
924
  handleDragMove?.(translate, sortingKey, event);
717
- };
718
- const resetLocalDragVisuals = ()=>{
925
+ }, [
926
+ latestDragTranslate,
927
+ latestDragEvent,
928
+ getCurrentScrollableBoundaryRect,
929
+ scrollableBoundaryId,
930
+ getDragStartItemBounds,
931
+ handleDragMove,
932
+ sortingKey,
933
+ lastAutoScrollTranslateY,
934
+ getAutoScrollOverflow,
935
+ stopAutoScrollLoop,
936
+ applyVisualTranslate,
937
+ emitSortableDragMove,
938
+ getAutoScrollDirection,
939
+ autoScrollOverflow,
940
+ autoScrollStickyDirection,
941
+ autoScrollDirection,
942
+ isAutoScrollBlocked,
943
+ setAutoScrolling,
944
+ startAutoScrollLoop
945
+ ]);
946
+ const resetLocalDragVisuals = useCallback(()=>{
719
947
  'main thread';
720
948
  if (!dirtyKeysRef.current[sortingKey]) return;
721
- console.info('reset local drag visual2');
949
+ invalidateActiveItemMeasurement();
950
+ stopAutoScrollLoop();
722
951
  MTSRef.current?.MTSSetTransform(0, 0);
952
+ MTSRef.current?.MTSResetInternalTranslateValues();
723
953
  const overlayWasActive = dragSourceHidden.current;
724
954
  showDragSourceItem();
725
955
  if (overlayWasActive) {
@@ -730,49 +960,93 @@ function SortableInteractiveItem(props) {
730
960
  transform: 'translate(0px, 0px)'
731
961
  });
732
962
  }
733
- dirtyKeysRef.current[sortingKey] = false;
734
- };
735
- const itemDragEnd = (_pagePoint, event)=>{
963
+ delete dirtyKeysRef.current[sortingKey];
964
+ }, [
965
+ dirtyKeysRef,
966
+ sortingKey,
967
+ MTSRef,
968
+ dragSourceHidden,
969
+ showDragSourceItem,
970
+ getDragOverlayElement,
971
+ invalidateActiveItemMeasurement,
972
+ stopAutoScrollLoop
973
+ ]);
974
+ const itemDragEnd = useCallback((_pagePoint, event)=>{
736
975
  'main thread';
737
976
  resetAutoScrollDragState();
738
977
  autoScrollStartScrollTop.current = 0;
739
978
  dragStartScrollDeltaFromMeasuredRect.current = 0;
740
- latestDragEvent.current = null;
979
+ invalidateActiveItemMeasurement();
741
980
  stopAutoScrollLoop();
742
981
  const orderChanged = handleDragEnd?.(sortingKey, event) ?? false;
743
982
  if (!orderChanged) resetLocalDragVisuals();
744
- };
745
- const dataKeyOrderSignature = useMemo(()=>(data ?? []).map((item)=>item.getSortingKey()).join('|'), [
746
- data
983
+ }, [
984
+ resetAutoScrollDragState,
985
+ autoScrollStartScrollTop,
986
+ dragStartScrollDeltaFromMeasuredRect,
987
+ invalidateActiveItemMeasurement,
988
+ stopAutoScrollLoop,
989
+ handleDragEnd,
990
+ sortingKey,
991
+ resetLocalDragVisuals
747
992
  ]);
748
993
  usePreCommit(()=>{
749
994
  'main thread';
750
- resetLocalDragVisuals();
995
+ resetItemVisualsRef.current[sortingKey] = resetLocalDragVisuals;
996
+ syncDisabledKey(sortingKey, disabled);
997
+ }, [
998
+ resetItemVisualsRef,
999
+ sortingKey,
1000
+ resetLocalDragVisuals,
1001
+ syncDisabledKey,
1002
+ disabled
1003
+ ]);
1004
+ const unregisterItem = useCallback(()=>{
1005
+ 'main thread';
1006
+ resetItemVisualsRef.current[sortingKey]?.();
1007
+ delete resetItemVisualsRef.current[sortingKey];
1008
+ delete dirtyKeysRef.current[sortingKey];
1009
+ delete disabledKeysRef.current[sortingKey];
1010
+ setChildrenMTSRef({
1011
+ current: null
1012
+ }, sortingKey);
751
1013
  }, [
752
- dataKeyOrderSignature
1014
+ resetItemVisualsRef,
1015
+ dirtyKeysRef,
1016
+ disabledKeysRef,
1017
+ setChildrenMTSRef,
1018
+ sortingKey
753
1019
  ]);
1020
+ useEffect(()=>()=>{
1021
+ runOnMainThread(unregisterItem)();
1022
+ }, [
1023
+ unregisterItem
1024
+ ]);
1025
+ const draggableProps = useMemo(()=>({
1026
+ 'main-thread:bindlayoutchange': handleMTSLayoutChange
1027
+ }), [
1028
+ handleMTSLayoutChange
1029
+ ]);
1030
+ const allowedDirection = useMemo(()=>[
1031
+ 'up',
1032
+ 'down'
1033
+ ], []);
1034
+ const itemChildren = <>
1035
+ {!scrollableBoundaryRectRef && <SortableItemGeometryObserver refresh={refreshDraggingRects}/>}
1036
+ {children}
1037
+ </>;
754
1038
  const itemDraggable = enableSorting && !disabled;
755
- if ('Draggable' === as) return <Draggable id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} enableDragging={itemDraggable} draggableProps={{
756
- 'main-thread:bindlayoutchange': handleMTSLayoutChange
757
- }} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} allowedDirection={[
758
- 'up',
759
- 'down'
760
- ]} {...!scrollableBoundaryId && boundaryId && {
1039
+ if ('Draggable' === as) return <Draggable id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} enableDragging={itemDraggable} draggableProps={draggableProps} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} allowedDirection={allowedDirection} {...!scrollableBoundaryId && boundaryId && {
761
1040
  minTranslateY: -(itemRect.top - boundaryRect.top),
762
1041
  maxTranslateY: boundaryRect.bottom - itemRect.bottom
763
1042
  }}>
764
- {children}
1043
+ {itemChildren}
765
1044
  </Draggable>;
766
- return <DraggableRoot id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} draggableProps={{
767
- 'main-thread:bindlayoutchange': handleMTSLayoutChange
768
- }} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} enableDragging={itemDraggable} {...!scrollableBoundaryId && boundaryId && {
1045
+ return <DraggableRoot id={itemElementId} MTSRef={MTSRef} trigger='immediate' className={className} draggableProps={draggableProps} onMTSDragStart={itemDragStart} onMTSDragEnd={itemDragEnd} onMTSDragging={itemDragging} enableDragging={itemDraggable} {...!scrollableBoundaryId && boundaryId && {
769
1046
  minTranslateY: -(itemRect.top - boundaryRect.top),
770
1047
  maxTranslateY: boundaryRect.bottom - itemRect.bottom
771
- }} allowedDirection={[
772
- 'up',
773
- 'down'
774
- ]}>
775
- {children}
1048
+ }} allowedDirection={allowedDirection}>
1049
+ {itemChildren}
776
1050
  </DraggableRoot>;
777
1051
  }
778
1052
  export { DraggableArea as SortableItemArea, SortableItem, SortableRoot };