@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.
@@ -3,7 +3,7 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import { createContext } from '@lynx-js/react'
6
- import type { RefObject } from '@lynx-js/react'
6
+ import type { MutableRefObject, RefObject } from '@lynx-js/react'
7
7
 
8
8
  import { noop } from '@lynx-js/lynx-ui-common'
9
9
  import type { Point } from '@lynx-js/lynx-ui-common'
@@ -14,7 +14,19 @@ import type { SortableData } from './types'
14
14
 
15
15
  interface SortableContextType<T = unknown> {
16
16
  data: SortableData<T>[]
17
+ isDragOverlay: boolean
17
18
  boundaryId?: string
19
+ scrollableBoundaryId?: string
20
+ scrollableBoundaryUpperEdgeRef?: MutableRefObject<boolean>
21
+ scrollableBoundaryLowerEdgeRef?: MutableRefObject<boolean>
22
+ scrollableScrollTopRef?: MutableRefObject<number>
23
+ dragOverlayRefMap?: MutableRefObject<
24
+ Record<string, MainThread.Element | null>
25
+ >
26
+ dirtyKeysRef: MutableRefObject<Record<string, boolean>>
27
+ scrollableStickyUpperOffset: number
28
+ scrollableStickyLowerOffset: number
29
+ debugLog: boolean
18
30
  enableSorting: boolean
19
31
  updateItemSize: (sortingKey: string, size: number) => void
20
32
  setChildrenRef: (refI: RefObject<DraggableRef>, key: string) => void
@@ -22,6 +34,10 @@ interface SortableContextType<T = unknown> {
22
34
  refI: RefObject<DraggableRef>,
23
35
  key: string,
24
36
  ) => void
37
+ setDragOverlayRef: (
38
+ refI: RefObject<MainThread.Element | null>,
39
+ key: string,
40
+ ) => void
25
41
  handleDragStart: (
26
42
  pagePoint: Point,
27
43
  sortingKey: string,
@@ -40,10 +56,16 @@ interface SortableContextType<T = unknown> {
40
56
 
41
57
  export const SortableContext = createContext<SortableContextType>({
42
58
  data: [],
59
+ isDragOverlay: false,
60
+ debugLog: false,
43
61
  enableSorting: true,
62
+ scrollableStickyUpperOffset: 0,
63
+ scrollableStickyLowerOffset: 0,
64
+ dirtyKeysRef: { current: {} },
44
65
  updateItemSize: noop,
45
66
  setChildrenRef: noop,
46
67
  setChildrenMTSRef: noop,
68
+ setDragOverlayRef: noop,
47
69
  handleDragStart: noop,
48
70
  handleDragMove: noop,
49
71
  handleDragEnd: noop,
@@ -28,6 +28,61 @@ export interface SortableData<T> {
28
28
  }
29
29
 
30
30
  export interface SortableRootProps<T> {
31
+ /**
32
+ * Specifies which container element SortableRoot should render and own as the scrollable boundary.
33
+ * - `'ScrollView'`: render an internal vertical `scroll-view`.
34
+ * - Omit (default): render a plain `view` without scrolling capability.
35
+ * @Android
36
+ * @iOS
37
+ * @Harmony
38
+ * @zh 指定 SortableRoot 渲染并持有的滚动容器类型。
39
+ * - `'ScrollView'`:渲染一个内部的纵向 `scroll-view`。
40
+ * - 不传(默认):渲染普通 `view`,不带滚动能力。
41
+ */
42
+ as?: 'ScrollView'
43
+ /**
44
+ * Class name applied to the owned `scroll-view` when `as` is `'ScrollView'`.
45
+ * @Android
46
+ * @iOS
47
+ * @Harmony
48
+ * @zh `as` 为 `'ScrollView'` 时应用到内部 `scroll-view` 的类名。
49
+ */
50
+ scrollableClassName?: string
51
+ /**
52
+ * Class name applied to the owned content boundary view when `as` is `'ScrollView'`.
53
+ * @Android
54
+ * @iOS
55
+ * @Harmony
56
+ * @zh `as` 为 `'ScrollView'` 时应用到内部内容边界 view 的类名。
57
+ */
58
+ scrollableContentClassName?: string
59
+ /**
60
+ * Whether the owned `scroll-view` can be scrolled by user gestures.
61
+ * @defaultValue true
62
+ * @Android
63
+ * @iOS
64
+ * @Harmony
65
+ * @zh 内部 `scroll-view` 是否允许用户手势滚动。
66
+ */
67
+ scrollableEnableScroll?: boolean
68
+ /**
69
+ * Distance between the dragged item and the upper edge of the owned or configured scrollable boundary while auto-scrolling upward.
70
+ * @defaultValue 0
71
+ * @Android
72
+ * @iOS
73
+ * @Harmony
74
+ * @zh 向上自动滚动时,拖拽项与可滚动边界上边缘之间保持的距离。
75
+ */
76
+ scrollableStickyUpperOffset?: number
77
+ /**
78
+ * Distance between the dragged item and the lower edge of the owned or configured scrollable boundary while auto-scrolling downward.
79
+ * @defaultValue 0
80
+ * @Android
81
+ * @iOS
82
+ * @Harmony
83
+ * @zh 向下自动滚动时,拖拽项与可滚动边界下边缘之间保持的距离。
84
+ */
85
+ scrollableStickyLowerOffset?: number
31
86
  /**
32
87
  * Whether to enable sorting.
33
88
  * @defaultValue true
@@ -61,6 +116,14 @@ export interface SortableRootProps<T> {
61
116
  * @zh 作为边界限制的项的唯一键。如果项被拖出边界,排序将被取消。
62
117
  */
63
118
  boundaryId?: string
119
+ /**
120
+ * The unique id of a scrollable boundary container. When the dragged item reaches the edge of this container, Sortable will call `scrollBy` on it so the list can continue moving with the drag direction.
121
+ * @Android
122
+ * @iOS
123
+ * @Harmony
124
+ * @zh 可滚动边界容器的唯一 id。当拖拽项触达该容器边缘时,Sortable 会对其调用 `scrollBy`,使列表随拖拽方向继续滚动。
125
+ */
126
+ scrollableBoundaryId?: string
64
127
  /**
65
128
  * Callback function that is triggered when sorting ends. The parameter is the sorted data.
66
129
  * @Android
@@ -17,23 +17,25 @@ import type { MainThread } from '@lynx-js/types'
17
17
 
18
18
  import type { SortableData } from './types'
19
19
 
20
- interface SortableOptionsType {
21
- data: SortableData<unknown>[] // sorting key array
20
+ interface SortableOptionsType<T> {
21
+ data: SortableData<T>[] // sorting key array
22
22
  sizeMap: MainThreadRef<Record<string, number>> // sorting key -> size
23
23
  itemRefMap: RefObject<Record<string, DraggableRef | null>>
24
24
  itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>> // sortingKey -> element Ref
25
- onDragEnd?: (sortedKeyArray: SortableData<unknown>[]) => void
25
+ dirtyKeysRef: MainThreadRef<Record<string, boolean>>
26
+ onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void
26
27
  onDragStart?: () => void
27
28
  debugLog?: boolean
28
29
  }
29
30
 
30
- export function useSortable(
31
- useSortableOptions: SortableOptionsType,
31
+ export function useSortable<T>(
32
+ useSortableOptions: SortableOptionsType<T>,
32
33
  ) {
33
34
  const {
34
35
  data,
35
36
  sizeMap,
36
37
  itemMTSRefMap,
38
+ dirtyKeysRef,
37
39
  onDragEnd,
38
40
  onDragStart,
39
41
  debugLog = false,
@@ -57,7 +59,8 @@ export function useSortable(
57
59
  item?.MTSSetOtherStyles({
58
60
  'z-index': `${zIndex}`,
59
61
  })
60
- }, [itemMTSRefMap])
62
+ dirtyKeysRef.current[sortingKey] = true
63
+ }, [itemMTSRefMap, dirtyKeysRef])
61
64
 
62
65
  const handleDragStartJS = useCallback(() => {
63
66
  onDragStart?.()
@@ -132,12 +135,16 @@ export function useSortable(
132
135
  )
133
136
 
134
137
  const setTransform = useCallback(
135
- (item: DraggableRef | null, translate: number) => {
138
+ (key: string, translate: number) => {
136
139
  'main thread'
137
140
 
138
- item?.MTSSetOtherStyles({ 'transform': `translateY(${translate}px)` })
141
+ const item = key ? itemMTSRefMap.current[key] : null
142
+ item?.MTSSetTransform(0, translate)
143
+ if (key) {
144
+ dirtyKeysRef.current[key] = true
145
+ }
139
146
  },
140
- [],
147
+ [itemMTSRefMap, dirtyKeysRef],
141
148
  )
142
149
 
143
150
  // Clamp the previous interacting item to the grid
@@ -149,19 +156,17 @@ export function useSortable(
149
156
  )
150
157
  const draggingItemSize = sizeMap.current[sortingKey]
151
158
  if (lastSwappingKey.current !== null) {
152
- const lastSwappingItem = lastSwappingKey.current
153
- ? itemMTSRefMap.current[lastSwappingKey.current]
154
- : null
159
+ const lastSwappingItemKey = lastSwappingKey.current
155
160
  if (
156
161
  Math.abs(swappingItemTranslation.current)
157
162
  > draggingItemSize * swapConfirmedPercentage.current
158
163
  ) {
159
164
  setTransform(
160
- lastSwappingItem,
165
+ lastSwappingItemKey,
161
166
  (movingDistance < 0 ? 1 : -1) * draggingItemSize,
162
167
  )
163
168
  } else {
164
- setTransform(lastSwappingItem, 0)
169
+ setTransform(lastSwappingItemKey, 0)
165
170
  }
166
171
  }
167
172
  }
@@ -252,7 +257,6 @@ export function useSortable(
252
257
  return
253
258
  }
254
259
  const swappingKey = keyArray[swappingIndex]
255
- const swappingItem = itemMTSRefMap.current[swappingKey]
256
260
 
257
261
  if (swappingKey !== lastSwappingKey.current) {
258
262
  updateLastSwappingItem(movingDistance, sortingKey, swappingKey)
@@ -261,7 +265,7 @@ export function useSortable(
261
265
 
262
266
  const unconsumedDistance = movingDistance - consumedDistance
263
267
  updateConfirmedInteractedID(unconsumedDistance, sortingKey)
264
- setTransform(swappingItem, -unconsumedDistance)
268
+ setTransform(swappingKey, -unconsumedDistance)
265
269
  },
266
270
  [
267
271
  changedKey,
@@ -309,18 +313,33 @@ export function useSortable(
309
313
  return swappedKeyArray
310
314
  }, [keyArray, lastSwappedKey])
311
315
 
312
- const resetStatus = useCallback(() => {
316
+ const resetSwapTrackingRefs = useCallback(() => {
317
+ 'main thread'
318
+ lastSwappedKey.current = ''
319
+ lastSwappingKey.current = ''
320
+ swappingItemTranslation.current = 0
321
+ changedKey.current = []
322
+ }, [changedKey, lastSwappedKey, lastSwappingKey, swappingItemTranslation])
323
+
324
+ const resetStatus = useCallback((draggingKey?: string) => {
313
325
  'main thread'
314
326
  changedKey.current.map((key: string) => {
327
+ if (key === draggingKey) {
328
+ return
329
+ }
315
330
  const item = itemMTSRefMap?.current?.[key]
316
- if (item && typeof item.MTSResetInternalTranslateValues === 'function') {
317
- item?.MTSResetInternalTranslateValues()
331
+ if (!item) {
332
+ return
333
+ }
334
+ if (typeof item.MTSSetTransform === 'function') {
335
+ item.MTSSetTransform(0, 0)
336
+ }
337
+ if (typeof item.MTSResetInternalTranslateValues === 'function') {
338
+ item.MTSResetInternalTranslateValues()
318
339
  }
319
340
  })
320
- lastSwappedKey.current = ''
321
- lastSwappingKey.current = ''
322
- changedKey.current = []
323
- }, [changedKey, itemMTSRefMap, lastSwappedKey, lastSwappingKey])
341
+ resetSwapTrackingRefs()
342
+ }, [changedKey, itemMTSRefMap, resetSwapTrackingRefs])
324
343
 
325
344
  const rootDragEnd = useCallback((sortedKey: string[]) => {
326
345
  const keyToItemMap = new Map(data.map((item, index) => {
@@ -336,14 +355,33 @@ export function useSortable(
336
355
  (
337
356
  sortingKey: string,
338
357
  event: MainThread.MouseEvent | MainThread.TouchEvent,
339
- ) => {
358
+ ): boolean => {
340
359
  'main thread'
341
360
  mtsLog(debugLog, '[event drag end]', event)
342
361
  const sortedKey = sortArray(sortingKey)
343
- runOnBackground(rootDragEnd)(sortedKey)
344
- resetStatus()
362
+ let orderChanged = sortedKey.length !== keyArray.length
363
+ if (!orderChanged) {
364
+ for (let i = 0; i < sortedKey.length; i++) {
365
+ if (sortedKey[i] !== keyArray[i]) {
366
+ orderChanged = true
367
+ break
368
+ }
369
+ }
370
+ }
371
+ if (orderChanged) {
372
+ // data will be updated by consumer -> usePreCommit will reset dirty items
373
+ runOnBackground(rootDragEnd)(sortedKey)
374
+ // dom transforms are reset by usePreCommit, but swap tracking refs
375
+ // must be cleared synchronously to avoid stale state on next drag
376
+ resetSwapTrackingRefs()
377
+ } else {
378
+ // no data update -> no precommit -> reset dirty items synchronously here
379
+ resetStatus(sortingKey)
380
+ runOnBackground(rootDragEnd)(sortedKey)
381
+ }
382
+ return orderChanged
345
383
  },
346
- [resetStatus, rootDragEnd, sortArray],
384
+ [keyArray, resetStatus, resetSwapTrackingRefs, rootDragEnd, sortArray],
347
385
  )
348
386
 
349
387
  return {