@lynx-js/lynx-ui-sortable 3.134.0 → 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 +21 -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 +1 -1
- 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/dist/useSortable.jsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { runOnBackground, useCallback, useMainThreadRef, useMemo } from "@lynx-js/react";
|
|
2
2
|
import { mtsLog } from "@lynx-js/lynx-ui-common";
|
|
3
3
|
function useSortable(useSortableOptions) {
|
|
4
|
-
const { data, sizeMap, itemMTSRefMap, dirtyKeysRef, onDragEnd, onDragStart, debugLog = false } = useSortableOptions;
|
|
4
|
+
const { data, sizeMap, itemMTSRefMap, dirtyKeysRef, disabledKeysRef, onDragEnd, onDragStart, debugLog = false } = useSortableOptions;
|
|
5
5
|
const safeData = Array.isArray(data) ? data : [];
|
|
6
6
|
const keyArray = useMemo(()=>safeData.map((item)=>item.getSortingKey()), [
|
|
7
7
|
safeData
|
|
@@ -13,6 +13,7 @@ function useSortable(useSortableOptions) {
|
|
|
13
13
|
});
|
|
14
14
|
const lastSwappingKey = useMainThreadRef('');
|
|
15
15
|
const swappingItemTranslation = useMainThreadRef(0);
|
|
16
|
+
const lastCrossedDisabledSize = useMainThreadRef(0);
|
|
16
17
|
const changedKey = useMainThreadRef([]);
|
|
17
18
|
const lastSwappedKey = useMainThreadRef('');
|
|
18
19
|
const changeItemZIndex = useCallback((zIndex, sortingKey)=>{
|
|
@@ -51,36 +52,68 @@ function useSortable(useSortableOptions) {
|
|
|
51
52
|
mtsLog(debugLog, '[swappingIndexAndDistance] invalid index', index);
|
|
52
53
|
return {
|
|
53
54
|
index: -1,
|
|
54
|
-
distance: 0
|
|
55
|
+
distance: 0,
|
|
56
|
+
crossedDisabledSize: 0
|
|
55
57
|
};
|
|
56
58
|
}
|
|
57
59
|
const absDistance = Math.abs(movingDistance);
|
|
58
60
|
const direction = movingDistance > 0 ? 1 : -1;
|
|
59
61
|
let currentIndex = index + direction;
|
|
60
62
|
let accumulatedDistance = 0;
|
|
63
|
+
let disabledGap = 0;
|
|
61
64
|
while(true){
|
|
62
65
|
if (currentIndex < 0 || currentIndex >= keyArray.length) return {
|
|
63
66
|
index: -1,
|
|
64
|
-
distance: (movingDistance > 0 ? 1 : -1) * (absDistance - accumulatedDistance)
|
|
67
|
+
distance: (movingDistance > 0 ? 1 : -1) * (absDistance - accumulatedDistance),
|
|
68
|
+
crossedDisabledSize: disabledGap
|
|
65
69
|
};
|
|
66
|
-
const
|
|
70
|
+
const currentKey = keyArray[currentIndex];
|
|
71
|
+
if (disabledKeysRef.current[currentKey]) {
|
|
72
|
+
const disabledSize = sizeMap.current[currentKey];
|
|
73
|
+
if ('number' != typeof disabledSize) {
|
|
74
|
+
mtsLog(debugLog, `[swappingIndexAndDistance] disabled item size not found for key ${currentKey}, stopping at index ${currentIndex}.`);
|
|
75
|
+
return {
|
|
76
|
+
index: -1,
|
|
77
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
78
|
+
crossedDisabledSize: disabledGap
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (accumulatedDistance + disabledSize >= absDistance) {
|
|
82
|
+
mtsLog(debugLog, `[swappingIndexAndDistance] still over disabled item ${currentKey} at index ${currentIndex}.`);
|
|
83
|
+
return {
|
|
84
|
+
index: -1,
|
|
85
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
86
|
+
crossedDisabledSize: disabledGap
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
mtsLog(debugLog, `[swappingIndexAndDistance] cross disabled item ${currentKey} at index ${currentIndex}.`);
|
|
90
|
+
accumulatedDistance += disabledSize;
|
|
91
|
+
disabledGap += disabledSize;
|
|
92
|
+
currentIndex += direction;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const size = sizeMap.current[currentKey];
|
|
67
96
|
if ('number' != typeof size) {
|
|
68
97
|
mtsLog(debugLog, `[swappingIndexAndDistance] item size not found for key ${sortingKey}, stopping at index ${currentIndex}.`);
|
|
69
98
|
return {
|
|
70
99
|
index: -1,
|
|
71
|
-
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance
|
|
100
|
+
distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,
|
|
101
|
+
crossedDisabledSize: disabledGap
|
|
72
102
|
};
|
|
73
103
|
}
|
|
74
104
|
accumulatedDistance += size;
|
|
75
105
|
if (accumulatedDistance >= absDistance) return {
|
|
76
106
|
index: currentIndex,
|
|
77
|
-
distance: (movingDistance > 0 ? 1 : -1) * (accumulatedDistance - size)
|
|
107
|
+
distance: (movingDistance > 0 ? 1 : -1) * (accumulatedDistance - size),
|
|
108
|
+
crossedDisabledSize: disabledGap
|
|
78
109
|
};
|
|
110
|
+
disabledGap = 0;
|
|
79
111
|
currentIndex += direction;
|
|
80
112
|
}
|
|
81
113
|
}, [
|
|
82
114
|
keyArray,
|
|
83
|
-
sizeMap
|
|
115
|
+
sizeMap,
|
|
116
|
+
disabledKeysRef
|
|
84
117
|
]);
|
|
85
118
|
const setTransform = useCallback((key, translate)=>{
|
|
86
119
|
'main thread';
|
|
@@ -97,7 +130,7 @@ function useSortable(useSortableOptions) {
|
|
|
97
130
|
const draggingItemSize = sizeMap.current[sortingKey];
|
|
98
131
|
if (null !== lastSwappingKey.current) {
|
|
99
132
|
const lastSwappingItemKey = lastSwappingKey.current;
|
|
100
|
-
Math.abs(swappingItemTranslation.current) > draggingItemSize * swapConfirmedPercentage.current ? setTransform(lastSwappingItemKey, (movingDistance < 0 ? 1 : -1) * draggingItemSize) : setTransform(lastSwappingItemKey, 0);
|
|
133
|
+
Math.abs(swappingItemTranslation.current) > draggingItemSize * swapConfirmedPercentage.current ? setTransform(lastSwappingItemKey, (movingDistance < 0 ? 1 : -1) * (draggingItemSize + lastCrossedDisabledSize.current)) : setTransform(lastSwappingItemKey, 0);
|
|
101
134
|
}
|
|
102
135
|
};
|
|
103
136
|
const updateLastSwappingItem = (movingDistance, sortingKey, swappingKey)=>{
|
|
@@ -129,8 +162,8 @@ function useSortable(useSortableOptions) {
|
|
|
129
162
|
};
|
|
130
163
|
const switchHandler = useCallback((movingDistance, sortingKey)=>{
|
|
131
164
|
'main thread';
|
|
132
|
-
const { index: swappingIndex, distance: consumedDistance } = swappingIndexAndDistance(movingDistance, sortingKey);
|
|
133
|
-
mtsLog(debugLog, 'swappingIndexAndDistance', swappingIndex, consumedDistance);
|
|
165
|
+
const { index: swappingIndex, distance: consumedDistance, crossedDisabledSize = 0 } = swappingIndexAndDistance(movingDistance, sortingKey);
|
|
166
|
+
mtsLog(debugLog, 'swappingIndexAndDistance', swappingIndex, consumedDistance, crossedDisabledSize);
|
|
134
167
|
if (swappingIndex < 0) return void clampPreviousItem(consumedDistance, sortingKey);
|
|
135
168
|
const swappingKey = keyArray[swappingIndex];
|
|
136
169
|
if (swappingKey !== lastSwappingKey.current) {
|
|
@@ -139,7 +172,10 @@ function useSortable(useSortableOptions) {
|
|
|
139
172
|
}
|
|
140
173
|
const unconsumedDistance = movingDistance - consumedDistance;
|
|
141
174
|
updateConfirmedInteractedID(unconsumedDistance, sortingKey);
|
|
142
|
-
|
|
175
|
+
const swappingItemSize = sizeMap.current[swappingKey] ?? 0;
|
|
176
|
+
const speedMultiplier = swappingItemSize > 0 ? (swappingItemSize + crossedDisabledSize) / swappingItemSize : 1;
|
|
177
|
+
setTransform(swappingKey, -unconsumedDistance * speedMultiplier);
|
|
178
|
+
lastCrossedDisabledSize.current = crossedDisabledSize;
|
|
143
179
|
}, [
|
|
144
180
|
changedKey,
|
|
145
181
|
itemMTSRefMap,
|
|
@@ -163,29 +199,49 @@ function useSortable(useSortableOptions) {
|
|
|
163
199
|
'main thread';
|
|
164
200
|
const draggingIndex = keyArray.indexOf(sortingKey);
|
|
165
201
|
const swappedIndex = keyArray.indexOf(lastSwappedKey.current);
|
|
166
|
-
|
|
202
|
+
mtsLog(debugLog, 'sortArray with lastSwappedKey ', lastSwappedKey.current);
|
|
203
|
+
if (-1 === draggingIndex || -1 === swappedIndex || draggingIndex === swappedIndex) return [
|
|
167
204
|
...keyArray
|
|
168
205
|
];
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
206
|
+
const disabledIndices = {};
|
|
207
|
+
const movableKeys = [];
|
|
208
|
+
for(let i = 0; i < keyArray.length; i++){
|
|
209
|
+
const k = keyArray[i];
|
|
210
|
+
if (disabledKeysRef.current[k]) disabledIndices[i] = k;
|
|
211
|
+
else movableKeys.push(k);
|
|
212
|
+
}
|
|
213
|
+
const movableDraggingIdx = movableKeys.indexOf(sortingKey);
|
|
214
|
+
const movableSwappedIdx = movableKeys.indexOf(lastSwappedKey.current);
|
|
215
|
+
if (-1 === movableDraggingIdx || -1 === movableSwappedIdx) return [
|
|
216
|
+
...keyArray
|
|
217
|
+
];
|
|
218
|
+
const [draggedItem] = movableKeys.splice(movableDraggingIdx, 1);
|
|
219
|
+
movableKeys.splice(movableSwappedIdx, 0, draggedItem);
|
|
220
|
+
const result = [];
|
|
221
|
+
let cursor = 0;
|
|
222
|
+
for(let i = 0; i < keyArray.length; i++)if (void 0 === disabledIndices[i]) {
|
|
223
|
+
result.push(movableKeys[cursor]);
|
|
224
|
+
cursor++;
|
|
225
|
+
} else result.push(disabledIndices[i]);
|
|
226
|
+
return result;
|
|
174
227
|
}, [
|
|
175
228
|
keyArray,
|
|
176
|
-
lastSwappedKey
|
|
229
|
+
lastSwappedKey,
|
|
230
|
+
disabledKeysRef
|
|
177
231
|
]);
|
|
178
232
|
const resetSwapTrackingRefs = useCallback(()=>{
|
|
179
233
|
'main thread';
|
|
180
234
|
lastSwappedKey.current = '';
|
|
181
235
|
lastSwappingKey.current = '';
|
|
182
236
|
swappingItemTranslation.current = 0;
|
|
237
|
+
lastCrossedDisabledSize.current = 0;
|
|
183
238
|
changedKey.current = [];
|
|
184
239
|
}, [
|
|
185
240
|
changedKey,
|
|
186
241
|
lastSwappedKey,
|
|
187
242
|
lastSwappingKey,
|
|
188
|
-
swappingItemTranslation
|
|
243
|
+
swappingItemTranslation,
|
|
244
|
+
lastCrossedDisabledSize
|
|
189
245
|
]);
|
|
190
246
|
const resetStatus = useCallback((draggingKey)=>{
|
|
191
247
|
'main thread';
|
package/dist/useSortable.jsx.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSortable.jsx","sources":["webpack://@lynx-js/lynx-ui-sortable/./src/useSortable.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport {\n runOnBackground,\n useCallback,\n useMainThreadRef,\n useMemo,\n} from '@lynx-js/react'\nimport type { MainThreadRef, RefObject } from '@lynx-js/react'\n\nimport { mtsLog } from '@lynx-js/lynx-ui-common'\nimport type { Point } from '@lynx-js/lynx-ui-common'\nimport type { DraggableRef } from '@lynx-js/lynx-ui-draggable'\nimport type { MainThread } from '@lynx-js/types'\n\nimport type { SortableData } from './types'\n\ninterface SortableOptionsType<T> {\n data: SortableData<T>[] // sorting key array\n sizeMap: MainThreadRef<Record<string, number>> // sorting key -> size\n itemRefMap: RefObject<Record<string, DraggableRef | null>>\n itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>> // sortingKey -> element Ref\n dirtyKeysRef: MainThreadRef<Record<string, boolean>>\n onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void\n onDragStart?: () => void\n debugLog?: boolean\n}\n\nexport function useSortable<T>(\n useSortableOptions: SortableOptionsType<T>,\n) {\n const {\n data,\n sizeMap,\n itemMTSRefMap,\n dirtyKeysRef,\n onDragEnd,\n onDragStart,\n debugLog = false,\n } = useSortableOptions\n\n const safeData = Array.isArray(data) ? data : []\n const keyArray = useMemo(() => safeData.map(item => item.getSortingKey()), [\n safeData,\n ])\n const swapConfirmedPercentage = useMainThreadRef<number>(0.5)\n const touchStartPoint = useMainThreadRef<Point>({ x: 0, y: 0 })\n const lastSwappingKey = useMainThreadRef<string>('')\n const swappingItemTranslation = useMainThreadRef<number>(0)\n const changedKey = useMainThreadRef<string[]>([]) // Store all the moved item. Remember to reset them\n const lastSwappedKey = useMainThreadRef<string>('') // Store the last swapped item. Use it to do the sorting.\n\n const changeItemZIndex = useCallback((zIndex: number, sortingKey: string) => {\n 'main thread'\n const item = itemMTSRefMap.current[sortingKey]\n\n item?.MTSSetOtherStyles({\n 'z-index': `${zIndex}`,\n })\n dirtyKeysRef.current[sortingKey] = true\n }, [itemMTSRefMap, dirtyKeysRef])\n\n const handleDragStartJS = useCallback(() => {\n onDragStart?.()\n }, [onDragStart])\n\n const handleDragStart = useCallback(\n (\n pagePoint: Point,\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n runOnBackground(handleDragStartJS)()\n touchStartPoint.current = pagePoint\n changedKey.current.push(sortingKey)\n changeItemZIndex(10000, sortingKey)\n mtsLog(debugLog, '[event drag start]', event)\n },\n [handleDragStartJS, touchStartPoint, changedKey, changeItemZIndex],\n )\n\n const swappingIndexAndDistance: (\n movingDistance: number,\n sortingKey: string,\n ) => { index: number, distance: number } = useCallback(\n (movingDistance, sortingKey) => {\n 'main thread'\n const index = keyArray.indexOf(sortingKey)\n if (keyArray.length === 0 || index < 0 || index >= keyArray.length) {\n mtsLog(debugLog, '[swappingIndexAndDistance] invalid index', index)\n return { index: -1, distance: 0 }\n }\n\n const absDistance = Math.abs(movingDistance)\n const direction = movingDistance > 0 ? 1 : -1\n let currentIndex = index + direction\n let accumulatedDistance = 0\n while (true) {\n if (currentIndex < 0 || currentIndex >= keyArray.length) {\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1)\n * (absDistance - accumulatedDistance),\n }\n }\n const size = sizeMap.current[keyArray[currentIndex]]\n if (typeof size !== 'number') {\n mtsLog(\n debugLog,\n `[swappingIndexAndDistance] item size not found for key ${sortingKey}, stopping at index ${currentIndex}.`,\n )\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,\n }\n }\n accumulatedDistance += size\n\n if (accumulatedDistance >= absDistance) {\n return {\n index: currentIndex,\n // This distance is the remained dragging distance needs to be consumed by current interacting item.\n // 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.\n distance: (movingDistance > 0 ? 1 : -1)\n * (accumulatedDistance - size),\n }\n }\n currentIndex += direction\n }\n },\n [keyArray, sizeMap],\n )\n\n const setTransform = useCallback(\n (key: string, translate: number) => {\n 'main thread'\n\n const item = key ? itemMTSRefMap.current[key] : null\n item?.MTSSetTransform(0, translate)\n if (key) {\n dirtyKeysRef.current[key] = true\n }\n },\n [itemMTSRefMap, dirtyKeysRef],\n )\n\n // Clamp the previous interacting item to the grid\n const clampPreviousItem = (movingDistance: number, sortingKey: string) => {\n 'main thread'\n mtsLog(\n debugLog,\n `clamp ${lastSwappingKey.current} to grid`,\n )\n const draggingItemSize = sizeMap.current[sortingKey]\n if (lastSwappingKey.current !== null) {\n const lastSwappingItemKey = lastSwappingKey.current\n if (\n Math.abs(swappingItemTranslation.current)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n setTransform(\n lastSwappingItemKey,\n (movingDistance < 0 ? 1 : -1) * draggingItemSize,\n )\n } else {\n setTransform(lastSwappingItemKey, 0)\n }\n }\n }\n\n const updateLastSwappingItem = (\n movingDistance: number,\n sortingKey: string,\n swappingKey: string,\n ) => {\n 'main thread'\n mtsLog(\n debugLog,\n `interact item from ${lastSwappingKey.current} to ${swappingKey}`,\n )\n clampPreviousItem(movingDistance, sortingKey)\n lastSwappingKey.current = swappingKey\n }\n\n // Update the confirmed interacting ID if the condition is met\n const updateConfirmedInteractedID = (\n unconsumedDistance: number,\n sortingKey: string,\n ) => {\n 'main thread'\n mtsLog(\n debugLog,\n `swapping with ${lastSwappingKey.current} at ${unconsumedDistance}`,\n )\n const draggingItemSize = sizeMap.current[sortingKey]\n if (\n Math.abs(unconsumedDistance)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n lastSwappedKey.current = lastSwappingKey.current\n mtsLog(\n debugLog,\n `confirmed swapping with ${lastSwappedKey.current} at cross ${unconsumedDistance}`,\n )\n } else if (lastSwappedKey.current === lastSwappingKey.current) {\n // Handle case when dragging back over a previously swapped item\n const swappingIndex = keyArray.indexOf(lastSwappingKey.current)\n const draggedItemIndex = keyArray.indexOf(sortingKey)\n\n // Get new swap candidate index based on drag direction reversal\n // Drag down → reverse up: `swappingIndex - 1`\n // Drag up → reverse down: `swappingIndex + 1`\n const indexModifier = swappingIndex > draggedItemIndex ? -1 : 1\n const newSwappedKeyIndex = swappingIndex + indexModifier\n\n // The logic should prevent out-of-bounds access, but as a safeguard:\n if (newSwappedKeyIndex >= 0 && newSwappedKeyIndex < keyArray.length) {\n const newSwappedKey = keyArray[newSwappedKeyIndex]\n\n // If new candidate's index matches dragged item's index, item returned to original position; no swap confirmed.\n if (newSwappedKeyIndex === draggedItemIndex) {\n lastSwappedKey.current = ''\n } else {\n lastSwappedKey.current = newSwappedKey\n }\n } else {\n // Item is returning to original position when reverting swap at list start/end.\n lastSwappedKey.current = ''\n }\n\n mtsLog(\n debugLog,\n `Reverted swap with ${lastSwappingKey.current}. New confirmed swap is with ${\n lastSwappedKey.current || 'none'\n }.`,\n )\n }\n swappingItemTranslation.current = unconsumedDistance\n }\n\n const switchHandler = useCallback(\n (movingDistance: number, sortingKey: string) => {\n 'main thread'\n const { index: swappingIndex, distance: consumedDistance } =\n swappingIndexAndDistance(movingDistance, sortingKey)\n mtsLog(\n debugLog,\n 'swappingIndexAndDistance',\n swappingIndex,\n consumedDistance,\n )\n if (swappingIndex < 0) {\n clampPreviousItem(consumedDistance, sortingKey)\n return\n }\n const swappingKey = keyArray[swappingIndex]\n\n if (swappingKey !== lastSwappingKey.current) {\n updateLastSwappingItem(movingDistance, sortingKey, swappingKey)\n changedKey.current.push(swappingKey)\n }\n\n const unconsumedDistance = movingDistance - consumedDistance\n updateConfirmedInteractedID(unconsumedDistance, sortingKey)\n setTransform(swappingKey, -unconsumedDistance)\n },\n [\n changedKey,\n itemMTSRefMap,\n keyArray,\n lastSwappingKey,\n setTransform,\n swappingIndexAndDistance,\n updateConfirmedInteractedID,\n updateLastSwappingItem,\n ],\n )\n\n const handleDragMove = useCallback(\n (\n delta: Point,\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n switchHandler(delta.y, sortingKey)\n changeItemZIndex(10000, sortingKey)\n mtsLog(debugLog, '[event drag move]', event)\n },\n [switchHandler, changeItemZIndex],\n )\n\n const sortArray = useCallback((sortingKey: string) => {\n 'main thread'\n const draggingIndex = keyArray.indexOf(sortingKey)\n const swappedIndex = keyArray.indexOf(lastSwappedKey.current)\n const swappedKeyArray = [...keyArray]\n\n mtsLog(debugLog, 'sortArray with lastSwappedKey ', lastSwappedKey.current)\n\n if (\n draggingIndex === -1 || swappedIndex === -1\n || draggingIndex === swappedIndex\n ) {\n return swappedKeyArray\n }\n\n const [draggedItem] = swappedKeyArray.splice(draggingIndex, 1)\n swappedKeyArray.splice(swappedIndex, 0, draggedItem)\n return swappedKeyArray\n }, [keyArray, lastSwappedKey])\n\n const resetSwapTrackingRefs = useCallback(() => {\n 'main thread'\n lastSwappedKey.current = ''\n lastSwappingKey.current = ''\n swappingItemTranslation.current = 0\n changedKey.current = []\n }, [changedKey, lastSwappedKey, lastSwappingKey, swappingItemTranslation])\n\n const resetStatus = useCallback((draggingKey?: string) => {\n 'main thread'\n changedKey.current.map((key: string) => {\n if (key === draggingKey) {\n return\n }\n const item = itemMTSRefMap?.current?.[key]\n if (!item) {\n return\n }\n if (typeof item.MTSSetTransform === 'function') {\n item.MTSSetTransform(0, 0)\n }\n if (typeof item.MTSResetInternalTranslateValues === 'function') {\n item.MTSResetInternalTranslateValues()\n }\n })\n resetSwapTrackingRefs()\n }, [changedKey, itemMTSRefMap, resetSwapTrackingRefs])\n\n const rootDragEnd = useCallback((sortedKey: string[]) => {\n const keyToItemMap = new Map(data.map((item, index) => {\n return [keyArray[index], item]\n }))\n const sortedData = sortedKey\n .map(key => keyToItemMap.get(key))\n .filter((item): item is NonNullable<typeof item> => !!item)\n onDragEnd?.(sortedData)\n }, [data, keyArray, onDragEnd])\n\n const handleDragEnd = useCallback(\n (\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ): boolean => {\n 'main thread'\n mtsLog(debugLog, '[event drag end]', event)\n const sortedKey = sortArray(sortingKey)\n let orderChanged = sortedKey.length !== keyArray.length\n if (!orderChanged) {\n for (let i = 0; i < sortedKey.length; i++) {\n if (sortedKey[i] !== keyArray[i]) {\n orderChanged = true\n break\n }\n }\n }\n if (orderChanged) {\n // data will be updated by consumer -> usePreCommit will reset dirty items\n runOnBackground(rootDragEnd)(sortedKey)\n // dom transforms are reset by usePreCommit, but swap tracking refs\n // must be cleared synchronously to avoid stale state on next drag\n resetSwapTrackingRefs()\n } else {\n // no data update -> no precommit -> reset dirty items synchronously here\n resetStatus(sortingKey)\n runOnBackground(rootDragEnd)(sortedKey)\n }\n return orderChanged\n },\n [keyArray, resetStatus, resetSwapTrackingRefs, rootDragEnd, sortArray],\n )\n\n return {\n handleDragStart,\n handleDragMove,\n handleDragEnd,\n }\n}\n"],"names":["useSortable","useSortableOptions","data","sizeMap","itemMTSRefMap","dirtyKeysRef","onDragEnd","onDragStart","debugLog","safeData","Array","keyArray","useMemo","item","swapConfirmedPercentage","useMainThreadRef","touchStartPoint","lastSwappingKey","swappingItemTranslation","changedKey","lastSwappedKey","changeItemZIndex","useCallback","zIndex","sortingKey","handleDragStartJS","handleDragStart","pagePoint","event","runOnBackground","mtsLog","swappingIndexAndDistance","movingDistance","index","absDistance","Math","direction","currentIndex","accumulatedDistance","size","setTransform","key","translate","clampPreviousItem","draggingItemSize","lastSwappingItemKey","updateLastSwappingItem","swappingKey","updateConfirmedInteractedID","unconsumedDistance","swappingIndex","draggedItemIndex","indexModifier","newSwappedKeyIndex","newSwappedKey","switchHandler","consumedDistance","handleDragMove","delta","sortArray","draggingIndex","swappedIndex","swappedKeyArray","draggedItem","resetSwapTrackingRefs","resetStatus","draggingKey","rootDragEnd","sortedKey","keyToItemMap","Map","sortedData","handleDragEnd","orderChanged","i"],"mappings":";;AA8BO,SAASA,YACdC,kBAA0C;IAE1C,MAAM,EACJC,IAAI,EACJC,OAAO,EACPC,aAAa,EACbC,YAAY,EACZC,SAAS,EACTC,WAAW,EACXC,WAAW,KAAK,EACjB,GAAGP;IAEJ,MAAMQ,WAAWC,MAAM,OAAO,CAACR,QAAQA,OAAO,EAAE;IAChD,MAAMS,WAAWC,QAAQ,IAAMH,SAAS,GAAG,CAACI,CAAAA,OAAQA,KAAK,aAAa,KAAK;QACzEJ;KACD;IACD,MAAMK,0BAA0BC,iBAAyB;IACzD,MAAMC,kBAAkBD,iBAAwB;QAAE,GAAG;QAAG,GAAG;IAAE;IAC7D,MAAME,kBAAkBF,iBAAyB;IACjD,MAAMG,0BAA0BH,iBAAyB;IACzD,MAAMI,aAAaJ,iBAA2B,EAAE;IAChD,MAAMK,iBAAiBL,iBAAyB;IAEhD,MAAMM,mBAAmBC,YAAY,C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":"useSortable.jsx","sources":["webpack://@lynx-js/lynx-ui-sortable/./src/useSortable.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport {\n runOnBackground,\n useCallback,\n useMainThreadRef,\n useMemo,\n} from '@lynx-js/react'\nimport type { MainThreadRef, RefObject } from '@lynx-js/react'\n\nimport { mtsLog } from '@lynx-js/lynx-ui-common'\nimport type { Point } from '@lynx-js/lynx-ui-common'\nimport type { DraggableRef } from '@lynx-js/lynx-ui-draggable'\nimport type { MainThread } from '@lynx-js/types'\n\nimport type { SortableData } from './types'\n\ninterface SortableOptionsType<T> {\n data: SortableData<T>[] // sorting key array\n sizeMap: MainThreadRef<Record<string, number>> // sorting key -> size\n itemRefMap: RefObject<Record<string, DraggableRef | null>>\n itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>> // sortingKey -> element Ref\n dirtyKeysRef: MainThreadRef<Record<string, boolean>>\n disabledKeysRef: MainThreadRef<Record<string, boolean>>\n onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void\n onDragStart?: () => void\n debugLog?: boolean\n}\n\nexport function useSortable<T>(\n useSortableOptions: SortableOptionsType<T>,\n) {\n const {\n data,\n sizeMap,\n itemMTSRefMap,\n dirtyKeysRef,\n disabledKeysRef,\n onDragEnd,\n onDragStart,\n debugLog = false,\n } = useSortableOptions\n\n const safeData = Array.isArray(data) ? data : []\n const keyArray = useMemo(() => safeData.map(item => item.getSortingKey()), [\n safeData,\n ])\n const swapConfirmedPercentage = useMainThreadRef<number>(0.5)\n const touchStartPoint = useMainThreadRef<Point>({ x: 0, y: 0 })\n const lastSwappingKey = useMainThreadRef<string>('')\n const swappingItemTranslation = useMainThreadRef<number>(0)\n const lastCrossedDisabledSize = useMainThreadRef<number>(0)\n const changedKey = useMainThreadRef<string[]>([]) // Store all the moved item. Remember to reset them\n const lastSwappedKey = useMainThreadRef<string>('') // Store the last swapped item. Use it to do the sorting.\n\n const changeItemZIndex = useCallback((zIndex: number, sortingKey: string) => {\n 'main thread'\n const item = itemMTSRefMap.current[sortingKey]\n\n item?.MTSSetOtherStyles({\n 'z-index': `${zIndex}`,\n })\n dirtyKeysRef.current[sortingKey] = true\n }, [itemMTSRefMap, dirtyKeysRef])\n\n const handleDragStartJS = useCallback(() => {\n onDragStart?.()\n }, [onDragStart])\n\n const handleDragStart = useCallback(\n (\n pagePoint: Point,\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n runOnBackground(handleDragStartJS)()\n touchStartPoint.current = pagePoint\n changedKey.current.push(sortingKey)\n changeItemZIndex(10000, sortingKey)\n mtsLog(debugLog, '[event drag start]', event)\n },\n [handleDragStartJS, touchStartPoint, changedKey, changeItemZIndex],\n )\n\n const swappingIndexAndDistance: (\n movingDistance: number,\n sortingKey: string,\n ) => { index: number, distance: number, crossedDisabledSize: number } =\n useCallback(\n (movingDistance, sortingKey) => {\n 'main thread'\n const index = keyArray.indexOf(sortingKey)\n if (keyArray.length === 0 || index < 0 || index >= keyArray.length) {\n mtsLog(debugLog, '[swappingIndexAndDistance] invalid index', index)\n return { index: -1, distance: 0, crossedDisabledSize: 0 }\n }\n\n const absDistance = Math.abs(movingDistance)\n const direction = movingDistance > 0 ? 1 : -1\n let currentIndex = index + direction\n let accumulatedDistance = 0\n // The total size of the contiguous disabled (locked) items immediately\n // preceding the current candidate movable, i.e. the disabled gap\n // between the previous movable neighbor (or the drag start) and the\n // movable we are about to evaluate. Every time we pass a movable\n // without picking it as the swap target, this gap resets to 0, so\n // each potential swap target only sees the locked items strictly\n // between it and its previous movable neighbor — never a cumulative\n // total from the drag origin.\n let disabledGap = 0\n while (true) {\n if (currentIndex < 0 || currentIndex >= keyArray.length) {\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1)\n * (absDistance - accumulatedDistance),\n crossedDisabledSize: disabledGap,\n }\n }\n const currentKey = keyArray[currentIndex]\n // Disabled (locked) items are never picked as a swap candidate and\n // never displaced. The dragged item is allowed to cross over them by\n // skipping past while still consuming their height, so the dragged\n // item's physical displacement keeps in sync with `accumulatedDistance`\n // and the disabled item's absolute position is preserved.\n if (disabledKeysRef.current[currentKey]) {\n const disabledSize = sizeMap.current[currentKey]\n if (typeof disabledSize !== 'number') {\n mtsLog(\n debugLog,\n `[swappingIndexAndDistance] disabled item size not found for key ${currentKey}, stopping at index ${currentIndex}.`,\n )\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,\n crossedDisabledSize: disabledGap,\n }\n }\n // The dragged item is still physically over this disabled item,\n // so no swap target should be produced yet.\n if (accumulatedDistance + disabledSize >= absDistance) {\n mtsLog(\n debugLog,\n `[swappingIndexAndDistance] still over disabled item ${currentKey} at index ${currentIndex}.`,\n )\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,\n crossedDisabledSize: disabledGap,\n }\n }\n mtsLog(\n debugLog,\n `[swappingIndexAndDistance] cross disabled item ${currentKey} at index ${currentIndex}.`,\n )\n accumulatedDistance += disabledSize\n disabledGap += disabledSize\n currentIndex += direction\n continue\n }\n const size = sizeMap.current[currentKey]\n if (typeof size !== 'number') {\n mtsLog(\n debugLog,\n `[swappingIndexAndDistance] item size not found for key ${sortingKey}, stopping at index ${currentIndex}.`,\n )\n return {\n index: -1,\n distance: (movingDistance > 0 ? 1 : -1) * accumulatedDistance,\n crossedDisabledSize: disabledGap,\n }\n }\n accumulatedDistance += size\n\n if (accumulatedDistance >= absDistance) {\n return {\n index: currentIndex,\n // This distance is the remained dragging distance needs to be consumed by current interacting item.\n // 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.\n distance: (movingDistance > 0 ? 1 : -1)\n * (accumulatedDistance - size),\n // Only the disabled items strictly between the previous movable\n // neighbor and this swap target. Locked items further back have\n // already been accounted for when their own following movable\n // became (and was clamped as) a swap target.\n crossedDisabledSize: disabledGap,\n }\n }\n // We just passed this movable without choosing it as the swap target,\n // so the disabled gap \"in front of\" the next candidate restarts here.\n disabledGap = 0\n currentIndex += direction\n }\n },\n [keyArray, sizeMap, disabledKeysRef],\n )\n\n const setTransform = useCallback(\n (key: string, translate: number) => {\n 'main thread'\n\n const item = key ? itemMTSRefMap.current[key] : null\n item?.MTSSetTransform(0, translate)\n if (key) {\n dirtyKeysRef.current[key] = true\n }\n },\n [itemMTSRefMap, dirtyKeysRef],\n )\n\n // Clamp the previous interacting item to the grid\n const clampPreviousItem = (movingDistance: number, sortingKey: string) => {\n 'main thread'\n mtsLog(\n debugLog,\n `clamp ${lastSwappingKey.current} to grid`,\n )\n const draggingItemSize = sizeMap.current[sortingKey]\n if (lastSwappingKey.current !== null) {\n const lastSwappingItemKey = lastSwappingKey.current\n if (\n Math.abs(swappingItemTranslation.current)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n // The previous swap target's \"swapped\" position must include the\n // height of any disabled items the dragged item had crossed over to\n // reach it; otherwise it would visually overlap with the (still\n // in-place) disabled items after we leave it behind.\n setTransform(\n lastSwappingItemKey,\n (movingDistance < 0 ? 1 : -1)\n * (draggingItemSize + lastCrossedDisabledSize.current),\n )\n } else {\n setTransform(lastSwappingItemKey, 0)\n }\n }\n }\n\n const updateLastSwappingItem = (\n movingDistance: number,\n sortingKey: string,\n swappingKey: string,\n ) => {\n 'main thread'\n mtsLog(\n debugLog,\n `interact item from ${lastSwappingKey.current} to ${swappingKey}`,\n )\n clampPreviousItem(movingDistance, sortingKey)\n lastSwappingKey.current = swappingKey\n }\n\n // Update the confirmed interacting ID if the condition is met\n const updateConfirmedInteractedID = (\n unconsumedDistance: number,\n sortingKey: string,\n ) => {\n 'main thread'\n mtsLog(\n debugLog,\n `swapping with ${lastSwappingKey.current} at ${unconsumedDistance}`,\n )\n const draggingItemSize = sizeMap.current[sortingKey]\n if (\n Math.abs(unconsumedDistance)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n lastSwappedKey.current = lastSwappingKey.current\n mtsLog(\n debugLog,\n `confirmed swapping with ${lastSwappedKey.current} at cross ${unconsumedDistance}`,\n )\n } else if (lastSwappedKey.current === lastSwappingKey.current) {\n // Handle case when dragging back over a previously swapped item\n const swappingIndex = keyArray.indexOf(lastSwappingKey.current)\n const draggedItemIndex = keyArray.indexOf(sortingKey)\n\n // Get new swap candidate index based on drag direction reversal\n // Drag down → reverse up: `swappingIndex - 1`\n // Drag up → reverse down: `swappingIndex + 1`\n const indexModifier = swappingIndex > draggedItemIndex ? -1 : 1\n const newSwappedKeyIndex = swappingIndex + indexModifier\n\n // The logic should prevent out-of-bounds access, but as a safeguard:\n if (newSwappedKeyIndex >= 0 && newSwappedKeyIndex < keyArray.length) {\n const newSwappedKey = keyArray[newSwappedKeyIndex]\n\n // If new candidate's index matches dragged item's index, item returned to original position; no swap confirmed.\n if (newSwappedKeyIndex === draggedItemIndex) {\n lastSwappedKey.current = ''\n } else {\n lastSwappedKey.current = newSwappedKey\n }\n } else {\n // Item is returning to original position when reverting swap at list start/end.\n lastSwappedKey.current = ''\n }\n\n mtsLog(\n debugLog,\n `Reverted swap with ${lastSwappingKey.current}. New confirmed swap is with ${\n lastSwappedKey.current || 'none'\n }.`,\n )\n }\n swappingItemTranslation.current = unconsumedDistance\n }\n\n const switchHandler = useCallback(\n (movingDistance: number, sortingKey: string) => {\n 'main thread'\n const {\n index: swappingIndex,\n distance: consumedDistance,\n crossedDisabledSize = 0,\n } = swappingIndexAndDistance(movingDistance, sortingKey)\n mtsLog(\n debugLog,\n 'swappingIndexAndDistance',\n swappingIndex,\n consumedDistance,\n crossedDisabledSize,\n )\n if (swappingIndex < 0) {\n clampPreviousItem(consumedDistance, sortingKey)\n return\n }\n const swappingKey = keyArray[swappingIndex]\n\n if (swappingKey !== lastSwappingKey.current) {\n updateLastSwappingItem(movingDistance, sortingKey, swappingKey)\n changedKey.current.push(swappingKey)\n }\n\n const unconsumedDistance = movingDistance - consumedDistance\n updateConfirmedInteractedID(unconsumedDistance, sortingKey)\n // When the dragged item has crossed over disabled items, those items do\n // not translate. To avoid the swap target jumping, scale its translate\n // proportionally as the dragged item progresses over it: while the\n // dragged item moves `swappingItemSize` over the swap target, the swap\n // target translates `swappingItemSize + crossedDisabledSize`, ending\n // exactly at the dragged item's original slot without any jump.\n const swappingItemSize = sizeMap.current[swappingKey] ?? 0\n const speedMultiplier = swappingItemSize > 0\n ? (swappingItemSize + crossedDisabledSize) / swappingItemSize\n : 1\n setTransform(swappingKey, -unconsumedDistance * speedMultiplier)\n lastCrossedDisabledSize.current = crossedDisabledSize\n },\n [\n changedKey,\n itemMTSRefMap,\n keyArray,\n lastSwappingKey,\n setTransform,\n swappingIndexAndDistance,\n updateConfirmedInteractedID,\n updateLastSwappingItem,\n ],\n )\n\n const handleDragMove = useCallback(\n (\n delta: Point,\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n switchHandler(delta.y, sortingKey)\n changeItemZIndex(10000, sortingKey)\n mtsLog(debugLog, '[event drag move]', event)\n },\n [switchHandler, changeItemZIndex],\n )\n\n const sortArray = useCallback((sortingKey: string) => {\n 'main thread'\n const draggingIndex = keyArray.indexOf(sortingKey)\n const swappedIndex = keyArray.indexOf(lastSwappedKey.current)\n\n mtsLog(debugLog, 'sortArray with lastSwappedKey ', lastSwappedKey.current)\n\n if (\n draggingIndex === -1 || swappedIndex === -1\n || draggingIndex === swappedIndex\n ) {\n return [...keyArray]\n }\n\n // Disabled items must keep their absolute positions in the result. We\n // splice only the movable (non-disabled) sequence and then reinsert\n // disabled items at their original indices.\n const disabledIndices: Record<number, string> = {}\n const movableKeys: string[] = []\n for (let i = 0; i < keyArray.length; i++) {\n const k = keyArray[i]\n if (disabledKeysRef.current[k]) {\n disabledIndices[i] = k\n } else {\n movableKeys.push(k)\n }\n }\n const movableDraggingIdx = movableKeys.indexOf(sortingKey)\n const movableSwappedIdx = movableKeys.indexOf(lastSwappedKey.current)\n if (movableDraggingIdx === -1 || movableSwappedIdx === -1) {\n return [...keyArray]\n }\n const [draggedItem] = movableKeys.splice(movableDraggingIdx, 1)\n movableKeys.splice(movableSwappedIdx, 0, draggedItem)\n const result: string[] = []\n let cursor = 0\n for (let i = 0; i < keyArray.length; i++) {\n if (disabledIndices[i] === undefined) {\n result.push(movableKeys[cursor])\n cursor++\n } else {\n result.push(disabledIndices[i])\n }\n }\n return result\n }, [keyArray, lastSwappedKey, disabledKeysRef])\n\n const resetSwapTrackingRefs = useCallback(() => {\n 'main thread'\n lastSwappedKey.current = ''\n lastSwappingKey.current = ''\n swappingItemTranslation.current = 0\n lastCrossedDisabledSize.current = 0\n changedKey.current = []\n }, [\n changedKey,\n lastSwappedKey,\n lastSwappingKey,\n swappingItemTranslation,\n lastCrossedDisabledSize,\n ])\n\n const resetStatus = useCallback((draggingKey?: string) => {\n 'main thread'\n changedKey.current.map((key: string) => {\n if (key === draggingKey) {\n return\n }\n const item = itemMTSRefMap?.current?.[key]\n if (!item) {\n return\n }\n if (typeof item.MTSSetTransform === 'function') {\n item.MTSSetTransform(0, 0)\n }\n if (typeof item.MTSResetInternalTranslateValues === 'function') {\n item.MTSResetInternalTranslateValues()\n }\n })\n resetSwapTrackingRefs()\n }, [changedKey, itemMTSRefMap, resetSwapTrackingRefs])\n\n const rootDragEnd = useCallback((sortedKey: string[]) => {\n const keyToItemMap = new Map(data.map((item, index) => {\n return [keyArray[index], item]\n }))\n const sortedData = sortedKey\n .map(key => keyToItemMap.get(key))\n .filter((item): item is NonNullable<typeof item> => !!item)\n onDragEnd?.(sortedData)\n }, [data, keyArray, onDragEnd])\n\n const handleDragEnd = useCallback(\n (\n sortingKey: string,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ): boolean => {\n 'main thread'\n mtsLog(debugLog, '[event drag end]', event)\n const sortedKey = sortArray(sortingKey)\n let orderChanged = sortedKey.length !== keyArray.length\n if (!orderChanged) {\n for (let i = 0; i < sortedKey.length; i++) {\n if (sortedKey[i] !== keyArray[i]) {\n orderChanged = true\n break\n }\n }\n }\n if (orderChanged) {\n // data will be updated by consumer -> usePreCommit will reset dirty items\n runOnBackground(rootDragEnd)(sortedKey)\n // dom transforms are reset by usePreCommit, but swap tracking refs\n // must be cleared synchronously to avoid stale state on next drag\n resetSwapTrackingRefs()\n } else {\n // no data update -> no precommit -> reset dirty items synchronously here\n resetStatus(sortingKey)\n runOnBackground(rootDragEnd)(sortedKey)\n }\n return orderChanged\n },\n [keyArray, resetStatus, resetSwapTrackingRefs, rootDragEnd, sortArray],\n )\n\n return {\n handleDragStart,\n handleDragMove,\n handleDragEnd,\n }\n}\n"],"names":["useSortable","useSortableOptions","data","sizeMap","itemMTSRefMap","dirtyKeysRef","disabledKeysRef","onDragEnd","onDragStart","debugLog","safeData","Array","keyArray","useMemo","item","swapConfirmedPercentage","useMainThreadRef","touchStartPoint","lastSwappingKey","swappingItemTranslation","lastCrossedDisabledSize","changedKey","lastSwappedKey","changeItemZIndex","useCallback","zIndex","sortingKey","handleDragStartJS","handleDragStart","pagePoint","event","runOnBackground","mtsLog","swappingIndexAndDistance","movingDistance","index","absDistance","Math","direction","currentIndex","accumulatedDistance","disabledGap","currentKey","disabledSize","size","setTransform","key","translate","clampPreviousItem","draggingItemSize","lastSwappingItemKey","updateLastSwappingItem","swappingKey","updateConfirmedInteractedID","unconsumedDistance","swappingIndex","draggedItemIndex","indexModifier","newSwappedKeyIndex","newSwappedKey","switchHandler","consumedDistance","crossedDisabledSize","swappingItemSize","speedMultiplier","handleDragMove","delta","sortArray","draggingIndex","swappedIndex","disabledIndices","movableKeys","i","k","movableDraggingIdx","movableSwappedIdx","draggedItem","result","cursor","undefined","resetSwapTrackingRefs","resetStatus","draggingKey","rootDragEnd","sortedKey","keyToItemMap","Map","sortedData","handleDragEnd","orderChanged"],"mappings":";;AA+BO,SAASA,YACdC,kBAA0C;IAE1C,MAAM,EACJC,IAAI,EACJC,OAAO,EACPC,aAAa,EACbC,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,AAAgC,cAAhC,OAAOA,KAAK,eAAe,EAC7BA,KAAK,eAAe,CAAC,GAAG;YAE1B,IAAI,AAAgD,cAAhD,OAAOA,KAAK,+BAA+B,EAC7CA,KAAK,+BAA+B;QAExC;QACAkE;IACF,GAAG;QAAC3D;QAAYjB;QAAe4E;KAAsB;IAErD,MAAMG,cAAc3D,YAAY,CAAC4D;QAC/B,MAAMC,eAAe,IAAIC,IAAIpF,KAAK,GAAG,CAAC,CAACY,MAAMqB,QACpC;gBAACvB,QAAQ,CAACuB,MAAM;gBAAErB;aAAK;QAEhC,MAAMyE,aAAaH,UAChB,GAAG,CAACtC,CAAAA,MAAOuC,aAAa,GAAG,CAACvC,MAC5B,MAAM,CAAC,CAAChC,OAA2C,CAAC,CAACA;QACxDP,YAAYgF;IACd,GAAG;QAACrF;QAAMU;QAAUL;KAAU;IAE9B,MAAMiF,gBAAgBhE,YACpB,CACEE,YACAI;QAEA;QACAE,OAAOvB,UAAU,oBAAoBqB;QACrC,MAAMsD,YAAYjB,UAAUzC;QAC5B,IAAI+D,eAAeL,UAAU,MAAM,KAAKxE,SAAS,MAAM;QACvD,IAAI,CAAC6E,cACH;YAAA,IAAK,IAAIjB,IAAI,GAAGA,IAAIY,UAAU,MAAM,EAAEZ,IACpC,IAAIY,SAAS,CAACZ,EAAE,KAAK5D,QAAQ,CAAC4D,EAAE,EAAE;gBAChCiB,eAAe;gBACf;YACF;QACF;QAEF,IAAIA,cAAc;YAEhB1D,gBAAgBoD,aAAaC;YAG7BJ;QACF,OAAO;YAELC,YAAYvD;YACZK,gBAAgBoD,aAAaC;QAC/B;QACA,OAAOK;IACT,GACA;QAAC7E;QAAUqE;QAAaD;QAAuBG;QAAahB;KAAU;IAGxE,OAAO;QACLvC;QACAqC;QACAuB;IACF;AACF"}
|
package/package.json
CHANGED
package/src/Sortable.tsx
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import type { RefObject } from '@lynx-js/react'
|
|
15
15
|
|
|
16
16
|
import type { Point } from '@lynx-js/lynx-ui-common'
|
|
17
|
-
import { usePreCommit } from '@lynx-js/lynx-ui-common'
|
|
17
|
+
import { delayFrames, usePreCommit } from '@lynx-js/lynx-ui-common'
|
|
18
18
|
import {
|
|
19
19
|
Draggable,
|
|
20
20
|
DraggableArea,
|
|
@@ -147,6 +147,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
147
147
|
{},
|
|
148
148
|
)
|
|
149
149
|
const dirtyKeysRef = useMainThreadRef<Record<string, boolean>>({})
|
|
150
|
+
const disabledKeysRef = useMainThreadRef<Record<string, boolean>>({})
|
|
150
151
|
const scrollableBoundaryUpperEdgeRef = useMainThreadRef(false)
|
|
151
152
|
const scrollableBoundaryLowerEdgeRef = useMainThreadRef(false)
|
|
152
153
|
const scrollableScrollTopRef = useMainThreadRef(0)
|
|
@@ -210,6 +211,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
210
211
|
itemRefMap: childrenRefMap,
|
|
211
212
|
itemMTSRefMap: childrenMTSRefMap,
|
|
212
213
|
dirtyKeysRef,
|
|
214
|
+
disabledKeysRef,
|
|
213
215
|
onDragEnd: handleInternalSortEnd,
|
|
214
216
|
onDragStart: handleInternalSortStart,
|
|
215
217
|
debugLog,
|
|
@@ -234,6 +236,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
234
236
|
: undefined,
|
|
235
237
|
dragOverlayRefMap: scrollable ? dragOverlayRefMap : undefined,
|
|
236
238
|
dirtyKeysRef,
|
|
239
|
+
disabledKeysRef,
|
|
237
240
|
scrollableStickyUpperOffset,
|
|
238
241
|
scrollableStickyLowerOffset,
|
|
239
242
|
updateItemSize,
|
|
@@ -257,6 +260,7 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
257
260
|
scrollableScrollTopRef,
|
|
258
261
|
dragOverlayRefMap,
|
|
259
262
|
dirtyKeysRef,
|
|
263
|
+
disabledKeysRef,
|
|
260
264
|
scrollableStickyLowerOffset,
|
|
261
265
|
scrollableStickyUpperOffset,
|
|
262
266
|
updateItemSize,
|
|
@@ -395,7 +399,13 @@ function SortableDragOverlayItem(props: SortableItemProps) {
|
|
|
395
399
|
}
|
|
396
400
|
|
|
397
401
|
function SortableInteractiveItem(props: SortableItemProps) {
|
|
398
|
-
const {
|
|
402
|
+
const {
|
|
403
|
+
className,
|
|
404
|
+
children,
|
|
405
|
+
sortingKey,
|
|
406
|
+
as = 'Draggable',
|
|
407
|
+
disabled = false,
|
|
408
|
+
} = props
|
|
399
409
|
const {
|
|
400
410
|
data,
|
|
401
411
|
enableSorting,
|
|
@@ -406,6 +416,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
406
416
|
scrollableScrollTopRef,
|
|
407
417
|
dragOverlayRefMap,
|
|
408
418
|
dirtyKeysRef,
|
|
419
|
+
disabledKeysRef,
|
|
409
420
|
scrollableStickyUpperOffset,
|
|
410
421
|
scrollableStickyLowerOffset,
|
|
411
422
|
updateItemSize,
|
|
@@ -908,7 +919,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
908
919
|
|
|
909
920
|
useEffect(() => {
|
|
910
921
|
runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey)
|
|
911
|
-
refreshDraggingRects
|
|
922
|
+
delayFrames(1, refreshDraggingRects)
|
|
912
923
|
}, [
|
|
913
924
|
data,
|
|
914
925
|
refreshDraggingRects,
|
|
@@ -917,6 +928,25 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
917
928
|
sortingKey,
|
|
918
929
|
])
|
|
919
930
|
|
|
931
|
+
const syncDisabledKey = useCallback(
|
|
932
|
+
(key: string, isDisabled: boolean) => {
|
|
933
|
+
'main thread'
|
|
934
|
+
if (isDisabled) {
|
|
935
|
+
disabledKeysRef.current[key] = true
|
|
936
|
+
} else {
|
|
937
|
+
delete disabledKeysRef.current[key]
|
|
938
|
+
}
|
|
939
|
+
},
|
|
940
|
+
[disabledKeysRef],
|
|
941
|
+
)
|
|
942
|
+
|
|
943
|
+
useEffect(() => {
|
|
944
|
+
runOnMainThread(syncDisabledKey)(sortingKey, disabled)
|
|
945
|
+
return () => {
|
|
946
|
+
runOnMainThread(syncDisabledKey)(sortingKey, false)
|
|
947
|
+
}
|
|
948
|
+
}, [disabled, sortingKey, syncDisabledKey])
|
|
949
|
+
|
|
920
950
|
const handleMTSLayoutChange = (e: MainThread.LayoutChangeEvent) => {
|
|
921
951
|
'main thread'
|
|
922
952
|
updateItemSize(sortingKey, e.detail.height)
|
|
@@ -1070,6 +1100,8 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
1070
1100
|
resetLocalDragVisuals()
|
|
1071
1101
|
}, [dataKeyOrderSignature])
|
|
1072
1102
|
|
|
1103
|
+
const itemDraggable = enableSorting && !disabled
|
|
1104
|
+
|
|
1073
1105
|
if (as === 'Draggable') {
|
|
1074
1106
|
return (
|
|
1075
1107
|
<Draggable
|
|
@@ -1077,7 +1109,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
1077
1109
|
MTSRef={MTSRef}
|
|
1078
1110
|
trigger='immediate'
|
|
1079
1111
|
className={className}
|
|
1080
|
-
enableDragging={
|
|
1112
|
+
enableDragging={itemDraggable}
|
|
1081
1113
|
draggableProps={{
|
|
1082
1114
|
'main-thread:bindlayoutchange': handleMTSLayoutChange,
|
|
1083
1115
|
}}
|
|
@@ -1108,7 +1140,7 @@ function SortableInteractiveItem(props: SortableItemProps) {
|
|
|
1108
1140
|
onMTSDragStart={itemDragStart}
|
|
1109
1141
|
onMTSDragEnd={itemDragEnd}
|
|
1110
1142
|
onMTSDragging={itemDragging}
|
|
1111
|
-
enableDragging={
|
|
1143
|
+
enableDragging={itemDraggable}
|
|
1112
1144
|
{...(!scrollableBoundaryId && boundaryId
|
|
1113
1145
|
&& {
|
|
1114
1146
|
minTranslateY: -(itemRect.top - boundaryRect.top),
|
package/src/SortableContext.ts
CHANGED
|
@@ -24,6 +24,7 @@ interface SortableContextType<T = unknown> {
|
|
|
24
24
|
Record<string, MainThread.Element | null>
|
|
25
25
|
>
|
|
26
26
|
dirtyKeysRef: MutableRefObject<Record<string, boolean>>
|
|
27
|
+
disabledKeysRef: MutableRefObject<Record<string, boolean>>
|
|
27
28
|
scrollableStickyUpperOffset: number
|
|
28
29
|
scrollableStickyLowerOffset: number
|
|
29
30
|
debugLog: boolean
|
|
@@ -62,6 +63,7 @@ export const SortableContext = createContext<SortableContextType>({
|
|
|
62
63
|
scrollableStickyUpperOffset: 0,
|
|
63
64
|
scrollableStickyLowerOffset: 0,
|
|
64
65
|
dirtyKeysRef: { current: {} },
|
|
66
|
+
disabledKeysRef: { current: {} },
|
|
65
67
|
updateItemSize: noop,
|
|
66
68
|
setChildrenRef: noop,
|
|
67
69
|
setChildrenMTSRef: noop,
|
package/src/types/index.docs.ts
CHANGED
|
@@ -189,4 +189,34 @@ export interface SortableItemProps {
|
|
|
189
189
|
* @zh 指定底层组件。若整个子节点区域均可拖动,使用默认值 'Draggable';否则使用 'DraggableRoot','DraggableRoot' 需与子节点 'DraggableArea' 一起使用。仅触摸其子节点 'DraggableArea' 时可拖动。
|
|
190
190
|
*/
|
|
191
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
|
|
192
222
|
}
|