@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/CHANGELOG.md +27 -0
- package/dist/Sortable.d.ts.map +1 -1
- package/dist/Sortable.jsx +667 -40
- package/dist/Sortable.jsx.map +1 -1
- package/dist/SortableContext.d.ts +13 -1
- package/dist/SortableContext.d.ts.map +1 -1
- package/dist/SortableContext.jsx +11 -0
- package/dist/SortableContext.jsx.map +1 -1
- package/dist/types/index.docs.d.ts +93 -0
- package/dist/types/index.docs.d.ts.map +1 -1
- package/dist/useSortable.d.ts +3 -1
- package/dist/useSortable.d.ts.map +1 -1
- package/dist/useSortable.jsx +122 -35
- package/dist/useSortable.jsx.map +1 -1
- package/package.json +1 -1
- package/src/Sortable.tsx +933 -45
- package/src/SortableContext.ts +25 -1
- package/src/types/index.docs.ts +93 -0
- package/src/useSortable.tsx +230 -74
package/src/SortableContext.ts
CHANGED
|
@@ -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,20 @@ 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
|
+
disabledKeysRef: MutableRefObject<Record<string, boolean>>
|
|
28
|
+
scrollableStickyUpperOffset: number
|
|
29
|
+
scrollableStickyLowerOffset: number
|
|
30
|
+
debugLog: boolean
|
|
18
31
|
enableSorting: boolean
|
|
19
32
|
updateItemSize: (sortingKey: string, size: number) => void
|
|
20
33
|
setChildrenRef: (refI: RefObject<DraggableRef>, key: string) => void
|
|
@@ -22,6 +35,10 @@ interface SortableContextType<T = unknown> {
|
|
|
22
35
|
refI: RefObject<DraggableRef>,
|
|
23
36
|
key: string,
|
|
24
37
|
) => void
|
|
38
|
+
setDragOverlayRef: (
|
|
39
|
+
refI: RefObject<MainThread.Element | null>,
|
|
40
|
+
key: string,
|
|
41
|
+
) => void
|
|
25
42
|
handleDragStart: (
|
|
26
43
|
pagePoint: Point,
|
|
27
44
|
sortingKey: string,
|
|
@@ -40,10 +57,17 @@ interface SortableContextType<T = unknown> {
|
|
|
40
57
|
|
|
41
58
|
export const SortableContext = createContext<SortableContextType>({
|
|
42
59
|
data: [],
|
|
60
|
+
isDragOverlay: false,
|
|
61
|
+
debugLog: false,
|
|
43
62
|
enableSorting: true,
|
|
63
|
+
scrollableStickyUpperOffset: 0,
|
|
64
|
+
scrollableStickyLowerOffset: 0,
|
|
65
|
+
dirtyKeysRef: { current: {} },
|
|
66
|
+
disabledKeysRef: { current: {} },
|
|
44
67
|
updateItemSize: noop,
|
|
45
68
|
setChildrenRef: noop,
|
|
46
69
|
setChildrenMTSRef: noop,
|
|
70
|
+
setDragOverlayRef: noop,
|
|
47
71
|
handleDragStart: noop,
|
|
48
72
|
handleDragMove: noop,
|
|
49
73
|
handleDragEnd: noop,
|
package/src/types/index.docs.ts
CHANGED
|
@@ -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
|
|
@@ -126,4 +189,34 @@ export interface SortableItemProps {
|
|
|
126
189
|
* @zh 指定底层组件。若整个子节点区域均可拖动,使用默认值 'Draggable';否则使用 'DraggableRoot','DraggableRoot' 需与子节点 'DraggableArea' 一起使用。仅触摸其子节点 'DraggableArea' 时可拖动。
|
|
127
190
|
*/
|
|
128
191
|
as?: 'Draggable' | 'DraggableRoot'
|
|
192
|
+
/**
|
|
193
|
+
* Whether this item is locked from sorting.
|
|
194
|
+
*
|
|
195
|
+
* A disabled item:
|
|
196
|
+
* - Cannot be dragged itself. Touch / long-press gestures are not bound on
|
|
197
|
+
* it, so interacting with it never starts a sort operation.
|
|
198
|
+
* - Cannot be displaced by other items being dragged. It always stays at
|
|
199
|
+
* its current position.
|
|
200
|
+
* - Always keeps its absolute position in the final sorted order. Other
|
|
201
|
+
* items can still freely cross over it; only the relative order of
|
|
202
|
+
* non-disabled items can change.
|
|
203
|
+
*
|
|
204
|
+
* Note that this only affects the disabled item itself. Other (non-disabled)
|
|
205
|
+
* items keep their full interactivity.
|
|
206
|
+
*
|
|
207
|
+
* @defaultValue false
|
|
208
|
+
* @Android
|
|
209
|
+
* @iOS
|
|
210
|
+
* @Harmony
|
|
211
|
+
* @zh 是否禁用该项的拖拽排序。
|
|
212
|
+
*
|
|
213
|
+
* 被禁用的项:
|
|
214
|
+
* - 自身无法被拖动。其上不会绑定 touch / longpress 等手势,触摸该项不会触发任何排序操作。
|
|
215
|
+
* - 不会因为其他项的拖动而发生位移,始终保持在原位置。
|
|
216
|
+
* - 在最终排序结果中始终保持其绝对位置不变;其他项仍可自由地越过它进行排序,
|
|
217
|
+
* 仅未被禁用项之间的相对顺序会发生变化。
|
|
218
|
+
*
|
|
219
|
+
* 该属性只影响被禁用的项自身;其它(未被禁用的)项的交互能力不受影响。
|
|
220
|
+
*/
|
|
221
|
+
disabled?: boolean
|
|
129
222
|
}
|
package/src/useSortable.tsx
CHANGED
|
@@ -22,6 +22,8 @@ interface SortableOptionsType<T> {
|
|
|
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
|
+
dirtyKeysRef: MainThreadRef<Record<string, boolean>>
|
|
26
|
+
disabledKeysRef: MainThreadRef<Record<string, boolean>>
|
|
25
27
|
onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void
|
|
26
28
|
onDragStart?: () => void
|
|
27
29
|
debugLog?: boolean
|
|
@@ -34,6 +36,8 @@ export function useSortable<T>(
|
|
|
34
36
|
data,
|
|
35
37
|
sizeMap,
|
|
36
38
|
itemMTSRefMap,
|
|
39
|
+
dirtyKeysRef,
|
|
40
|
+
disabledKeysRef,
|
|
37
41
|
onDragEnd,
|
|
38
42
|
onDragStart,
|
|
39
43
|
debugLog = false,
|
|
@@ -47,6 +51,7 @@ export function useSortable<T>(
|
|
|
47
51
|
const touchStartPoint = useMainThreadRef<Point>({ x: 0, y: 0 })
|
|
48
52
|
const lastSwappingKey = useMainThreadRef<string>('')
|
|
49
53
|
const swappingItemTranslation = useMainThreadRef<number>(0)
|
|
54
|
+
const lastCrossedDisabledSize = useMainThreadRef<number>(0)
|
|
50
55
|
const changedKey = useMainThreadRef<string[]>([]) // Store all the moved item. Remember to reset them
|
|
51
56
|
const lastSwappedKey = useMainThreadRef<string>('') // Store the last swapped item. Use it to do the sorting.
|
|
52
57
|
|
|
@@ -57,7 +62,8 @@ export function useSortable<T>(
|
|
|
57
62
|
item?.MTSSetOtherStyles({
|
|
58
63
|
'z-index': `${zIndex}`,
|
|
59
64
|
})
|
|
60
|
-
|
|
65
|
+
dirtyKeysRef.current[sortingKey] = true
|
|
66
|
+
}, [itemMTSRefMap, dirtyKeysRef])
|
|
61
67
|
|
|
62
68
|
const handleDragStartJS = useCallback(() => {
|
|
63
69
|
onDragStart?.()
|
|
@@ -82,62 +88,127 @@ export function useSortable<T>(
|
|
|
82
88
|
const swappingIndexAndDistance: (
|
|
83
89
|
movingDistance: number,
|
|
84
90
|
sortingKey: string,
|
|
85
|
-
) => { index: number, distance: number } =
|
|
86
|
-
(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
) => { index: number, distance: number, crossedDisabledSize: number } =
|
|
92
|
+
useCallback(
|
|
93
|
+
(movingDistance, sortingKey) => {
|
|
94
|
+
'main thread'
|
|
95
|
+
const index = keyArray.indexOf(sortingKey)
|
|
96
|
+
if (keyArray.length === 0 || index < 0 || index >= keyArray.length) {
|
|
97
|
+
mtsLog(debugLog, '[swappingIndexAndDistance] invalid index', index)
|
|
98
|
+
return { index: -1, distance: 0, crossedDisabledSize: 0 }
|
|
99
|
+
}
|
|
93
100
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
const absDistance = Math.abs(movingDistance)
|
|
102
|
+
const direction = movingDistance > 0 ? 1 : -1
|
|
103
|
+
let currentIndex = index + direction
|
|
104
|
+
let accumulatedDistance = 0
|
|
105
|
+
// The total size of the contiguous disabled (locked) items immediately
|
|
106
|
+
// preceding the current candidate movable, i.e. the disabled gap
|
|
107
|
+
// between the previous movable neighbor (or the drag start) and the
|
|
108
|
+
// movable we are about to evaluate. Every time we pass a movable
|
|
109
|
+
// without picking it as the swap target, this gap resets to 0, so
|
|
110
|
+
// each potential swap target only sees the locked items strictly
|
|
111
|
+
// between it and its previous movable neighbor — never a cumulative
|
|
112
|
+
// total from the drag origin.
|
|
113
|
+
let disabledGap = 0
|
|
114
|
+
while (true) {
|
|
115
|
+
if (currentIndex < 0 || currentIndex >= keyArray.length) {
|
|
116
|
+
return {
|
|
117
|
+
index: -1,
|
|
118
|
+
distance: (movingDistance > 0 ? 1 : -1)
|
|
119
|
+
* (absDistance - accumulatedDistance),
|
|
120
|
+
crossedDisabledSize: disabledGap,
|
|
121
|
+
}
|
|
104
122
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
123
|
+
const currentKey = keyArray[currentIndex]
|
|
124
|
+
// Disabled (locked) items are never picked as a swap candidate and
|
|
125
|
+
// never displaced. The dragged item is allowed to cross over them by
|
|
126
|
+
// skipping past while still consuming their height, so the dragged
|
|
127
|
+
// item's physical displacement keeps in sync with `accumulatedDistance`
|
|
128
|
+
// and the disabled item's absolute position is preserved.
|
|
129
|
+
if (disabledKeysRef.current[currentKey]) {
|
|
130
|
+
const disabledSize = sizeMap.current[currentKey]
|
|
131
|
+
if (typeof disabledSize !== 'number') {
|
|
132
|
+
mtsLog(
|
|
133
|
+
debugLog,
|
|
134
|
+
`[swappingIndexAndDistance] disabled item size not found for key ${currentKey}, stopping at index ${currentIndex}.`,
|
|
135
|
+
)
|
|
136
|
+
return {
|
|
137
|
+
index: -1,
|
|
138
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
139
|
+
crossedDisabledSize: disabledGap,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// The dragged item is still physically over this disabled item,
|
|
143
|
+
// so no swap target should be produced yet.
|
|
144
|
+
if (accumulatedDistance + disabledSize >= absDistance) {
|
|
145
|
+
mtsLog(
|
|
146
|
+
debugLog,
|
|
147
|
+
`[swappingIndexAndDistance] still over disabled item ${currentKey} at index ${currentIndex}.`,
|
|
148
|
+
)
|
|
149
|
+
return {
|
|
150
|
+
index: -1,
|
|
151
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
152
|
+
crossedDisabledSize: disabledGap,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
mtsLog(
|
|
156
|
+
debugLog,
|
|
157
|
+
`[swappingIndexAndDistance] cross disabled item ${currentKey} at index ${currentIndex}.`,
|
|
158
|
+
)
|
|
159
|
+
accumulatedDistance += disabledSize
|
|
160
|
+
disabledGap += disabledSize
|
|
161
|
+
currentIndex += direction
|
|
162
|
+
continue
|
|
115
163
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
164
|
+
const size = sizeMap.current[currentKey]
|
|
165
|
+
if (typeof size !== 'number') {
|
|
166
|
+
mtsLog(
|
|
167
|
+
debugLog,
|
|
168
|
+
`[swappingIndexAndDistance] item size not found for key ${sortingKey}, stopping at index ${currentIndex}.`,
|
|
169
|
+
)
|
|
170
|
+
return {
|
|
171
|
+
index: -1,
|
|
172
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
173
|
+
crossedDisabledSize: disabledGap,
|
|
174
|
+
}
|
|
126
175
|
}
|
|
176
|
+
accumulatedDistance += size
|
|
177
|
+
|
|
178
|
+
if (accumulatedDistance >= absDistance) {
|
|
179
|
+
return {
|
|
180
|
+
index: currentIndex,
|
|
181
|
+
// This distance is the remained dragging distance needs to be consumed by current interacting item.
|
|
182
|
+
// E.g.: the total delta is 458 and all item is all 100px tall. Then the first 400 will be consumed the previous clamped items. And the 58 is current interacting item.
|
|
183
|
+
distance: (movingDistance > 0 ? 1 : -1)
|
|
184
|
+
* (accumulatedDistance - size),
|
|
185
|
+
// Only the disabled items strictly between the previous movable
|
|
186
|
+
// neighbor and this swap target. Locked items further back have
|
|
187
|
+
// already been accounted for when their own following movable
|
|
188
|
+
// became (and was clamped as) a swap target.
|
|
189
|
+
crossedDisabledSize: disabledGap,
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// We just passed this movable without choosing it as the swap target,
|
|
193
|
+
// so the disabled gap "in front of" the next candidate restarts here.
|
|
194
|
+
disabledGap = 0
|
|
195
|
+
currentIndex += direction
|
|
127
196
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
[keyArray, sizeMap],
|
|
132
|
-
)
|
|
197
|
+
},
|
|
198
|
+
[keyArray, sizeMap, disabledKeysRef],
|
|
199
|
+
)
|
|
133
200
|
|
|
134
201
|
const setTransform = useCallback(
|
|
135
|
-
(
|
|
202
|
+
(key: string, translate: number) => {
|
|
136
203
|
'main thread'
|
|
137
204
|
|
|
138
|
-
item
|
|
205
|
+
const item = key ? itemMTSRefMap.current[key] : null
|
|
206
|
+
item?.MTSSetTransform(0, translate)
|
|
207
|
+
if (key) {
|
|
208
|
+
dirtyKeysRef.current[key] = true
|
|
209
|
+
}
|
|
139
210
|
},
|
|
140
|
-
[],
|
|
211
|
+
[itemMTSRefMap, dirtyKeysRef],
|
|
141
212
|
)
|
|
142
213
|
|
|
143
214
|
// Clamp the previous interacting item to the grid
|
|
@@ -149,19 +220,22 @@ export function useSortable<T>(
|
|
|
149
220
|
)
|
|
150
221
|
const draggingItemSize = sizeMap.current[sortingKey]
|
|
151
222
|
if (lastSwappingKey.current !== null) {
|
|
152
|
-
const
|
|
153
|
-
? itemMTSRefMap.current[lastSwappingKey.current]
|
|
154
|
-
: null
|
|
223
|
+
const lastSwappingItemKey = lastSwappingKey.current
|
|
155
224
|
if (
|
|
156
225
|
Math.abs(swappingItemTranslation.current)
|
|
157
226
|
> draggingItemSize * swapConfirmedPercentage.current
|
|
158
227
|
) {
|
|
228
|
+
// The previous swap target's "swapped" position must include the
|
|
229
|
+
// height of any disabled items the dragged item had crossed over to
|
|
230
|
+
// reach it; otherwise it would visually overlap with the (still
|
|
231
|
+
// in-place) disabled items after we leave it behind.
|
|
159
232
|
setTransform(
|
|
160
|
-
|
|
161
|
-
(movingDistance < 0 ? 1 : -1)
|
|
233
|
+
lastSwappingItemKey,
|
|
234
|
+
(movingDistance < 0 ? 1 : -1)
|
|
235
|
+
* (draggingItemSize + lastCrossedDisabledSize.current),
|
|
162
236
|
)
|
|
163
237
|
} else {
|
|
164
|
-
setTransform(
|
|
238
|
+
setTransform(lastSwappingItemKey, 0)
|
|
165
239
|
}
|
|
166
240
|
}
|
|
167
241
|
}
|
|
@@ -239,20 +313,23 @@ export function useSortable<T>(
|
|
|
239
313
|
const switchHandler = useCallback(
|
|
240
314
|
(movingDistance: number, sortingKey: string) => {
|
|
241
315
|
'main thread'
|
|
242
|
-
const {
|
|
243
|
-
|
|
316
|
+
const {
|
|
317
|
+
index: swappingIndex,
|
|
318
|
+
distance: consumedDistance,
|
|
319
|
+
crossedDisabledSize = 0,
|
|
320
|
+
} = swappingIndexAndDistance(movingDistance, sortingKey)
|
|
244
321
|
mtsLog(
|
|
245
322
|
debugLog,
|
|
246
323
|
'swappingIndexAndDistance',
|
|
247
324
|
swappingIndex,
|
|
248
325
|
consumedDistance,
|
|
326
|
+
crossedDisabledSize,
|
|
249
327
|
)
|
|
250
328
|
if (swappingIndex < 0) {
|
|
251
329
|
clampPreviousItem(consumedDistance, sortingKey)
|
|
252
330
|
return
|
|
253
331
|
}
|
|
254
332
|
const swappingKey = keyArray[swappingIndex]
|
|
255
|
-
const swappingItem = itemMTSRefMap.current[swappingKey]
|
|
256
333
|
|
|
257
334
|
if (swappingKey !== lastSwappingKey.current) {
|
|
258
335
|
updateLastSwappingItem(movingDistance, sortingKey, swappingKey)
|
|
@@ -261,7 +338,18 @@ export function useSortable<T>(
|
|
|
261
338
|
|
|
262
339
|
const unconsumedDistance = movingDistance - consumedDistance
|
|
263
340
|
updateConfirmedInteractedID(unconsumedDistance, sortingKey)
|
|
264
|
-
|
|
341
|
+
// When the dragged item has crossed over disabled items, those items do
|
|
342
|
+
// not translate. To avoid the swap target jumping, scale its translate
|
|
343
|
+
// proportionally as the dragged item progresses over it: while the
|
|
344
|
+
// dragged item moves `swappingItemSize` over the swap target, the swap
|
|
345
|
+
// target translates `swappingItemSize + crossedDisabledSize`, ending
|
|
346
|
+
// exactly at the dragged item's original slot without any jump.
|
|
347
|
+
const swappingItemSize = sizeMap.current[swappingKey] ?? 0
|
|
348
|
+
const speedMultiplier = swappingItemSize > 0
|
|
349
|
+
? (swappingItemSize + crossedDisabledSize) / swappingItemSize
|
|
350
|
+
: 1
|
|
351
|
+
setTransform(swappingKey, -unconsumedDistance * speedMultiplier)
|
|
352
|
+
lastCrossedDisabledSize.current = crossedDisabledSize
|
|
265
353
|
},
|
|
266
354
|
[
|
|
267
355
|
changedKey,
|
|
@@ -293,7 +381,6 @@ export function useSortable<T>(
|
|
|
293
381
|
'main thread'
|
|
294
382
|
const draggingIndex = keyArray.indexOf(sortingKey)
|
|
295
383
|
const swappedIndex = keyArray.indexOf(lastSwappedKey.current)
|
|
296
|
-
const swappedKeyArray = [...keyArray]
|
|
297
384
|
|
|
298
385
|
mtsLog(debugLog, 'sortArray with lastSwappedKey ', lastSwappedKey.current)
|
|
299
386
|
|
|
@@ -301,26 +388,76 @@ export function useSortable<T>(
|
|
|
301
388
|
draggingIndex === -1 || swappedIndex === -1
|
|
302
389
|
|| draggingIndex === swappedIndex
|
|
303
390
|
) {
|
|
304
|
-
return
|
|
391
|
+
return [...keyArray]
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Disabled items must keep their absolute positions in the result. We
|
|
395
|
+
// splice only the movable (non-disabled) sequence and then reinsert
|
|
396
|
+
// disabled items at their original indices.
|
|
397
|
+
const disabledIndices: Record<number, string> = {}
|
|
398
|
+
const movableKeys: string[] = []
|
|
399
|
+
for (let i = 0; i < keyArray.length; i++) {
|
|
400
|
+
const k = keyArray[i]
|
|
401
|
+
if (disabledKeysRef.current[k]) {
|
|
402
|
+
disabledIndices[i] = k
|
|
403
|
+
} else {
|
|
404
|
+
movableKeys.push(k)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const movableDraggingIdx = movableKeys.indexOf(sortingKey)
|
|
408
|
+
const movableSwappedIdx = movableKeys.indexOf(lastSwappedKey.current)
|
|
409
|
+
if (movableDraggingIdx === -1 || movableSwappedIdx === -1) {
|
|
410
|
+
return [...keyArray]
|
|
411
|
+
}
|
|
412
|
+
const [draggedItem] = movableKeys.splice(movableDraggingIdx, 1)
|
|
413
|
+
movableKeys.splice(movableSwappedIdx, 0, draggedItem)
|
|
414
|
+
const result: string[] = []
|
|
415
|
+
let cursor = 0
|
|
416
|
+
for (let i = 0; i < keyArray.length; i++) {
|
|
417
|
+
if (disabledIndices[i] === undefined) {
|
|
418
|
+
result.push(movableKeys[cursor])
|
|
419
|
+
cursor++
|
|
420
|
+
} else {
|
|
421
|
+
result.push(disabledIndices[i])
|
|
422
|
+
}
|
|
305
423
|
}
|
|
424
|
+
return result
|
|
425
|
+
}, [keyArray, lastSwappedKey, disabledKeysRef])
|
|
306
426
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
427
|
+
const resetSwapTrackingRefs = useCallback(() => {
|
|
428
|
+
'main thread'
|
|
429
|
+
lastSwappedKey.current = ''
|
|
430
|
+
lastSwappingKey.current = ''
|
|
431
|
+
swappingItemTranslation.current = 0
|
|
432
|
+
lastCrossedDisabledSize.current = 0
|
|
433
|
+
changedKey.current = []
|
|
434
|
+
}, [
|
|
435
|
+
changedKey,
|
|
436
|
+
lastSwappedKey,
|
|
437
|
+
lastSwappingKey,
|
|
438
|
+
swappingItemTranslation,
|
|
439
|
+
lastCrossedDisabledSize,
|
|
440
|
+
])
|
|
311
441
|
|
|
312
|
-
const resetStatus = useCallback(() => {
|
|
442
|
+
const resetStatus = useCallback((draggingKey?: string) => {
|
|
313
443
|
'main thread'
|
|
314
444
|
changedKey.current.map((key: string) => {
|
|
445
|
+
if (key === draggingKey) {
|
|
446
|
+
return
|
|
447
|
+
}
|
|
315
448
|
const item = itemMTSRefMap?.current?.[key]
|
|
316
|
-
if (item
|
|
317
|
-
|
|
449
|
+
if (!item) {
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
if (typeof item.MTSSetTransform === 'function') {
|
|
453
|
+
item.MTSSetTransform(0, 0)
|
|
454
|
+
}
|
|
455
|
+
if (typeof item.MTSResetInternalTranslateValues === 'function') {
|
|
456
|
+
item.MTSResetInternalTranslateValues()
|
|
318
457
|
}
|
|
319
458
|
})
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
changedKey.current = []
|
|
323
|
-
}, [changedKey, itemMTSRefMap, lastSwappedKey, lastSwappingKey])
|
|
459
|
+
resetSwapTrackingRefs()
|
|
460
|
+
}, [changedKey, itemMTSRefMap, resetSwapTrackingRefs])
|
|
324
461
|
|
|
325
462
|
const rootDragEnd = useCallback((sortedKey: string[]) => {
|
|
326
463
|
const keyToItemMap = new Map(data.map((item, index) => {
|
|
@@ -336,14 +473,33 @@ export function useSortable<T>(
|
|
|
336
473
|
(
|
|
337
474
|
sortingKey: string,
|
|
338
475
|
event: MainThread.MouseEvent | MainThread.TouchEvent,
|
|
339
|
-
) => {
|
|
476
|
+
): boolean => {
|
|
340
477
|
'main thread'
|
|
341
478
|
mtsLog(debugLog, '[event drag end]', event)
|
|
342
479
|
const sortedKey = sortArray(sortingKey)
|
|
343
|
-
|
|
344
|
-
|
|
480
|
+
let orderChanged = sortedKey.length !== keyArray.length
|
|
481
|
+
if (!orderChanged) {
|
|
482
|
+
for (let i = 0; i < sortedKey.length; i++) {
|
|
483
|
+
if (sortedKey[i] !== keyArray[i]) {
|
|
484
|
+
orderChanged = true
|
|
485
|
+
break
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (orderChanged) {
|
|
490
|
+
// data will be updated by consumer -> usePreCommit will reset dirty items
|
|
491
|
+
runOnBackground(rootDragEnd)(sortedKey)
|
|
492
|
+
// dom transforms are reset by usePreCommit, but swap tracking refs
|
|
493
|
+
// must be cleared synchronously to avoid stale state on next drag
|
|
494
|
+
resetSwapTrackingRefs()
|
|
495
|
+
} else {
|
|
496
|
+
// no data update -> no precommit -> reset dirty items synchronously here
|
|
497
|
+
resetStatus(sortingKey)
|
|
498
|
+
runOnBackground(rootDragEnd)(sortedKey)
|
|
499
|
+
}
|
|
500
|
+
return orderChanged
|
|
345
501
|
},
|
|
346
|
-
[resetStatus, rootDragEnd, sortArray],
|
|
502
|
+
[keyArray, resetStatus, resetSwapTrackingRefs, rootDragEnd, sortArray],
|
|
347
503
|
)
|
|
348
504
|
|
|
349
505
|
return {
|