@lynx-js/lynx-ui-sortable 3.134.0 → 3.135.3
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 +29 -0
- package/dist/Sortable.d.ts.map +1 -1
- package/dist/Sortable.jsx +28 -6
- package/dist/Sortable.jsx.map +1 -1
- package/dist/SortableContext.d.ts +1 -0
- package/dist/SortableContext.d.ts.map +1 -1
- package/dist/SortableContext.jsx +3 -0
- package/dist/SortableContext.jsx.map +1 -1
- package/dist/types/index.docs.d.ts +30 -0
- package/dist/types/index.docs.d.ts.map +1 -1
- package/dist/useSortable.d.ts +1 -0
- package/dist/useSortable.d.ts.map +1 -1
- package/dist/useSortable.jsx +75 -19
- package/dist/useSortable.jsx.map +1 -1
- package/package.json +3 -3
- package/src/Sortable.tsx +37 -5
- package/src/SortableContext.ts +2 -0
- package/src/types/index.docs.ts +30 -0
- package/src/useSortable.tsx +172 -54
package/src/useSortable.tsx
CHANGED
|
@@ -23,6 +23,7 @@ interface SortableOptionsType<T> {
|
|
|
23
23
|
itemRefMap: RefObject<Record<string, DraggableRef | null>>
|
|
24
24
|
itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>> // sortingKey -> element Ref
|
|
25
25
|
dirtyKeysRef: MainThreadRef<Record<string, boolean>>
|
|
26
|
+
disabledKeysRef: MainThreadRef<Record<string, boolean>>
|
|
26
27
|
onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void
|
|
27
28
|
onDragStart?: () => void
|
|
28
29
|
debugLog?: boolean
|
|
@@ -36,6 +37,7 @@ export function useSortable<T>(
|
|
|
36
37
|
sizeMap,
|
|
37
38
|
itemMTSRefMap,
|
|
38
39
|
dirtyKeysRef,
|
|
40
|
+
disabledKeysRef,
|
|
39
41
|
onDragEnd,
|
|
40
42
|
onDragStart,
|
|
41
43
|
debugLog = false,
|
|
@@ -49,6 +51,7 @@ export function useSortable<T>(
|
|
|
49
51
|
const touchStartPoint = useMainThreadRef<Point>({ x: 0, y: 0 })
|
|
50
52
|
const lastSwappingKey = useMainThreadRef<string>('')
|
|
51
53
|
const swappingItemTranslation = useMainThreadRef<number>(0)
|
|
54
|
+
const lastCrossedDisabledSize = useMainThreadRef<number>(0)
|
|
52
55
|
const changedKey = useMainThreadRef<string[]>([]) // Store all the moved item. Remember to reset them
|
|
53
56
|
const lastSwappedKey = useMainThreadRef<string>('') // Store the last swapped item. Use it to do the sorting.
|
|
54
57
|
|
|
@@ -85,54 +88,115 @@ export function useSortable<T>(
|
|
|
85
88
|
const swappingIndexAndDistance: (
|
|
86
89
|
movingDistance: number,
|
|
87
90
|
sortingKey: string,
|
|
88
|
-
) => { index: number, distance: number } =
|
|
89
|
-
(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
+
}
|
|
96
100
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
+
}
|
|
107
122
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
|
118
163
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
+
}
|
|
129
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
|
|
130
196
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
[keyArray, sizeMap],
|
|
135
|
-
)
|
|
197
|
+
},
|
|
198
|
+
[keyArray, sizeMap, disabledKeysRef],
|
|
199
|
+
)
|
|
136
200
|
|
|
137
201
|
const setTransform = useCallback(
|
|
138
202
|
(key: string, translate: number) => {
|
|
@@ -161,9 +225,14 @@ export function useSortable<T>(
|
|
|
161
225
|
Math.abs(swappingItemTranslation.current)
|
|
162
226
|
> draggingItemSize * swapConfirmedPercentage.current
|
|
163
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.
|
|
164
232
|
setTransform(
|
|
165
233
|
lastSwappingItemKey,
|
|
166
|
-
(movingDistance < 0 ? 1 : -1)
|
|
234
|
+
(movingDistance < 0 ? 1 : -1)
|
|
235
|
+
* (draggingItemSize + lastCrossedDisabledSize.current),
|
|
167
236
|
)
|
|
168
237
|
} else {
|
|
169
238
|
setTransform(lastSwappingItemKey, 0)
|
|
@@ -244,13 +313,17 @@ export function useSortable<T>(
|
|
|
244
313
|
const switchHandler = useCallback(
|
|
245
314
|
(movingDistance: number, sortingKey: string) => {
|
|
246
315
|
'main thread'
|
|
247
|
-
const {
|
|
248
|
-
|
|
316
|
+
const {
|
|
317
|
+
index: swappingIndex,
|
|
318
|
+
distance: consumedDistance,
|
|
319
|
+
crossedDisabledSize = 0,
|
|
320
|
+
} = swappingIndexAndDistance(movingDistance, sortingKey)
|
|
249
321
|
mtsLog(
|
|
250
322
|
debugLog,
|
|
251
323
|
'swappingIndexAndDistance',
|
|
252
324
|
swappingIndex,
|
|
253
325
|
consumedDistance,
|
|
326
|
+
crossedDisabledSize,
|
|
254
327
|
)
|
|
255
328
|
if (swappingIndex < 0) {
|
|
256
329
|
clampPreviousItem(consumedDistance, sortingKey)
|
|
@@ -265,7 +338,18 @@ export function useSortable<T>(
|
|
|
265
338
|
|
|
266
339
|
const unconsumedDistance = movingDistance - consumedDistance
|
|
267
340
|
updateConfirmedInteractedID(unconsumedDistance, sortingKey)
|
|
268
|
-
|
|
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
|
|
269
353
|
},
|
|
270
354
|
[
|
|
271
355
|
changedKey,
|
|
@@ -297,7 +381,6 @@ export function useSortable<T>(
|
|
|
297
381
|
'main thread'
|
|
298
382
|
const draggingIndex = keyArray.indexOf(sortingKey)
|
|
299
383
|
const swappedIndex = keyArray.indexOf(lastSwappedKey.current)
|
|
300
|
-
const swappedKeyArray = [...keyArray]
|
|
301
384
|
|
|
302
385
|
mtsLog(debugLog, 'sortArray with lastSwappedKey ', lastSwappedKey.current)
|
|
303
386
|
|
|
@@ -305,21 +388,56 @@ export function useSortable<T>(
|
|
|
305
388
|
draggingIndex === -1 || swappedIndex === -1
|
|
306
389
|
|| draggingIndex === swappedIndex
|
|
307
390
|
) {
|
|
308
|
-
return
|
|
391
|
+
return [...keyArray]
|
|
309
392
|
}
|
|
310
393
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return result
|
|
425
|
+
}, [keyArray, lastSwappedKey, disabledKeysRef])
|
|
315
426
|
|
|
316
427
|
const resetSwapTrackingRefs = useCallback(() => {
|
|
317
428
|
'main thread'
|
|
318
429
|
lastSwappedKey.current = ''
|
|
319
430
|
lastSwappingKey.current = ''
|
|
320
431
|
swappingItemTranslation.current = 0
|
|
432
|
+
lastCrossedDisabledSize.current = 0
|
|
321
433
|
changedKey.current = []
|
|
322
|
-
}, [
|
|
434
|
+
}, [
|
|
435
|
+
changedKey,
|
|
436
|
+
lastSwappedKey,
|
|
437
|
+
lastSwappingKey,
|
|
438
|
+
swappingItemTranslation,
|
|
439
|
+
lastCrossedDisabledSize,
|
|
440
|
+
])
|
|
323
441
|
|
|
324
442
|
const resetStatus = useCallback((draggingKey?: string) => {
|
|
325
443
|
'main thread'
|