@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/Sortable.tsx
CHANGED
|
@@ -9,36 +9,127 @@ import {
|
|
|
9
9
|
useEffect,
|
|
10
10
|
useMainThreadRef,
|
|
11
11
|
useMemo,
|
|
12
|
-
useRef,
|
|
13
12
|
useState,
|
|
14
13
|
} from '@lynx-js/react'
|
|
15
14
|
import type { RefObject } from '@lynx-js/react'
|
|
16
15
|
|
|
17
16
|
import type { Point } 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,
|
|
21
21
|
DraggableRoot,
|
|
22
22
|
} from '@lynx-js/lynx-ui-draggable'
|
|
23
23
|
import type { DraggableRef } from '@lynx-js/lynx-ui-draggable'
|
|
24
|
-
import type { MainThread,
|
|
24
|
+
import type { MainThread, ScrollEvent } from '@lynx-js/types'
|
|
25
25
|
|
|
26
26
|
import { SortableContext } from './SortableContext'
|
|
27
|
-
import type {
|
|
27
|
+
import type {
|
|
28
|
+
SortableData,
|
|
29
|
+
SortableItemProps,
|
|
30
|
+
SortableRootProps,
|
|
31
|
+
} from './types'
|
|
28
32
|
import { useSortable } from './useSortable'
|
|
29
33
|
|
|
30
34
|
export { DraggableArea as SortableItemArea }
|
|
31
35
|
|
|
36
|
+
type RectTarget = 'item' | 'boundary' | 'scrollableBoundary'
|
|
37
|
+
interface ScrollByResult {
|
|
38
|
+
scrollTop?: number
|
|
39
|
+
detail?: { scrollTop?: number }
|
|
40
|
+
}
|
|
41
|
+
interface ScrollTranslateDelta {
|
|
42
|
+
dragDeltaY: number
|
|
43
|
+
measuredRectDeltaY: number
|
|
44
|
+
}
|
|
45
|
+
interface VerticalBounds {
|
|
46
|
+
top: number
|
|
47
|
+
bottom: number
|
|
48
|
+
}
|
|
49
|
+
interface EdgeDistance {
|
|
50
|
+
distanceToTop: number
|
|
51
|
+
distanceToBottom: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getEdgeDistance(
|
|
55
|
+
itemBounds: VerticalBounds,
|
|
56
|
+
boundaryRect: VerticalBounds,
|
|
57
|
+
): EdgeDistance {
|
|
58
|
+
'main thread'
|
|
59
|
+
return {
|
|
60
|
+
distanceToTop: itemBounds.top - boundaryRect.top,
|
|
61
|
+
distanceToBottom: boundaryRect.bottom - itemBounds.bottom,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getAutoScrollTriggerOverflow(
|
|
66
|
+
edgeDistance: EdgeDistance,
|
|
67
|
+
upperTriggerZone: number,
|
|
68
|
+
lowerTriggerZone: number,
|
|
69
|
+
) {
|
|
70
|
+
'main thread'
|
|
71
|
+
if (edgeDistance.distanceToTop < upperTriggerZone) {
|
|
72
|
+
return edgeDistance.distanceToTop - upperTriggerZone
|
|
73
|
+
}
|
|
74
|
+
if (edgeDistance.distanceToBottom < lowerTriggerZone) {
|
|
75
|
+
return lowerTriggerZone - edgeDistance.distanceToBottom
|
|
76
|
+
}
|
|
77
|
+
return 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const AUTO_SCROLL_MAX_STEP = 18
|
|
81
|
+
const AUTO_SCROLL_VELOCITY_FACTOR = 0.35
|
|
82
|
+
|
|
83
|
+
function getNormalizedElementKey(sortingKey: string) {
|
|
84
|
+
return String(sortingKey).replace(/[^\w-]/g, '-')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getSortableItemElementId(sortingKey: string) {
|
|
88
|
+
return `sortable-item-${getNormalizedElementKey(sortingKey)}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getSortableDragOverlayElementId(sortingKey: string) {
|
|
92
|
+
return `sortable-drag-overlay-${getNormalizedElementKey(sortingKey)}`
|
|
93
|
+
}
|
|
94
|
+
|
|
32
95
|
export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
33
96
|
const {
|
|
34
97
|
children,
|
|
35
98
|
data,
|
|
36
99
|
debugLog = false,
|
|
37
100
|
boundaryId,
|
|
101
|
+
scrollableBoundaryId,
|
|
102
|
+
as,
|
|
103
|
+
scrollableClassName,
|
|
104
|
+
scrollableContentClassName,
|
|
105
|
+
scrollableEnableScroll = true,
|
|
106
|
+
scrollableStickyUpperOffset = 0,
|
|
107
|
+
scrollableStickyLowerOffset = 0,
|
|
38
108
|
onSortEnd,
|
|
39
109
|
onSortStart,
|
|
40
110
|
enableSorting = true,
|
|
41
111
|
} = props
|
|
112
|
+
const scrollable = as === 'ScrollView'
|
|
113
|
+
const [isDragging, setIsDragging] = useState(false)
|
|
114
|
+
const handleInternalSortStart = useCallback(() => {
|
|
115
|
+
setIsDragging(true)
|
|
116
|
+
onSortStart?.()
|
|
117
|
+
}, [onSortStart])
|
|
118
|
+
const handleInternalSortEnd = useCallback(
|
|
119
|
+
(sortedData: SortableData<T>[]) => {
|
|
120
|
+
setIsDragging(false)
|
|
121
|
+
onSortEnd(sortedData)
|
|
122
|
+
},
|
|
123
|
+
[onSortEnd],
|
|
124
|
+
)
|
|
125
|
+
const scrollableElementId = useMemo(
|
|
126
|
+
() => scrollableBoundaryId ?? 'sortable-scrollable-boundary',
|
|
127
|
+
[scrollableBoundaryId],
|
|
128
|
+
)
|
|
129
|
+
const scrollableContentId = useMemo(
|
|
130
|
+
() => boundaryId ?? 'sortable-scrollable-content',
|
|
131
|
+
[boundaryId],
|
|
132
|
+
)
|
|
42
133
|
const sizeMap = useMainThreadRef<Record<string, number>>({})
|
|
43
134
|
const childrenRefMap = useMainThreadRef<
|
|
44
135
|
Record<string, DraggableRef | null>
|
|
@@ -50,11 +141,46 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
50
141
|
>(
|
|
51
142
|
{},
|
|
52
143
|
)
|
|
144
|
+
const dragOverlayRefMap = useMainThreadRef<
|
|
145
|
+
Record<string, MainThread.Element | null>
|
|
146
|
+
>(
|
|
147
|
+
{},
|
|
148
|
+
)
|
|
149
|
+
const dirtyKeysRef = useMainThreadRef<Record<string, boolean>>({})
|
|
150
|
+
const disabledKeysRef = useMainThreadRef<Record<string, boolean>>({})
|
|
151
|
+
const scrollableBoundaryUpperEdgeRef = useMainThreadRef(false)
|
|
152
|
+
const scrollableBoundaryLowerEdgeRef = useMainThreadRef(false)
|
|
153
|
+
const scrollableScrollTopRef = useMainThreadRef(0)
|
|
53
154
|
const updateItemSize = useCallback((sortingKey: string, size: number) => {
|
|
54
155
|
'main thread'
|
|
55
156
|
sizeMap.current[sortingKey] = size
|
|
56
157
|
}, [])
|
|
57
158
|
|
|
159
|
+
const handleScrollableBoundaryScroll = useCallback((event: ScrollEvent) => {
|
|
160
|
+
'main thread'
|
|
161
|
+
scrollableScrollTopRef.current = event.detail.scrollTop
|
|
162
|
+
}, [scrollableScrollTopRef])
|
|
163
|
+
|
|
164
|
+
const handleScrollableBoundaryUpperExposure = useCallback(() => {
|
|
165
|
+
'main thread'
|
|
166
|
+
scrollableBoundaryUpperEdgeRef.current = true
|
|
167
|
+
}, [scrollableBoundaryUpperEdgeRef])
|
|
168
|
+
|
|
169
|
+
const handleScrollableBoundaryUpperDisexposure = useCallback(() => {
|
|
170
|
+
'main thread'
|
|
171
|
+
scrollableBoundaryUpperEdgeRef.current = false
|
|
172
|
+
}, [scrollableBoundaryUpperEdgeRef])
|
|
173
|
+
|
|
174
|
+
const handleScrollableBoundaryLowerExposure = useCallback(() => {
|
|
175
|
+
'main thread'
|
|
176
|
+
scrollableBoundaryLowerEdgeRef.current = true
|
|
177
|
+
}, [scrollableBoundaryLowerEdgeRef])
|
|
178
|
+
|
|
179
|
+
const handleScrollableBoundaryLowerDisexposure = useCallback(() => {
|
|
180
|
+
'main thread'
|
|
181
|
+
scrollableBoundaryLowerEdgeRef.current = false
|
|
182
|
+
}, [scrollableBoundaryLowerEdgeRef])
|
|
183
|
+
|
|
58
184
|
const setChildrenRef = useCallback(
|
|
59
185
|
(refI: RefObject<DraggableRef>, key: string) => {
|
|
60
186
|
'main thread'
|
|
@@ -71,42 +197,143 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
71
197
|
childrenMTSRefMap.current[key] = refI.current
|
|
72
198
|
}, [])
|
|
73
199
|
|
|
74
|
-
const
|
|
75
|
-
|
|
200
|
+
const setDragOverlayRef = useCallback((
|
|
201
|
+
refI: RefObject<MainThread.Element | null>,
|
|
202
|
+
key: string,
|
|
203
|
+
) => {
|
|
204
|
+
'main thread'
|
|
205
|
+
dragOverlayRefMap.current[key] = refI.current
|
|
206
|
+
}, [dragOverlayRefMap])
|
|
207
|
+
|
|
208
|
+
const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
|
|
209
|
+
data: data,
|
|
76
210
|
sizeMap: sizeMap,
|
|
77
211
|
itemRefMap: childrenRefMap,
|
|
78
212
|
itemMTSRefMap: childrenMTSRefMap,
|
|
79
|
-
|
|
80
|
-
|
|
213
|
+
dirtyKeysRef,
|
|
214
|
+
disabledKeysRef,
|
|
215
|
+
onDragEnd: handleInternalSortEnd,
|
|
216
|
+
onDragStart: handleInternalSortStart,
|
|
81
217
|
debugLog,
|
|
82
218
|
})
|
|
83
219
|
const sortableContextValue = useMemo(() => ({
|
|
84
220
|
data,
|
|
221
|
+
isDragOverlay: false,
|
|
222
|
+
debugLog,
|
|
85
223
|
enableSorting,
|
|
86
|
-
boundaryId,
|
|
224
|
+
boundaryId: scrollable ? scrollableContentId : boundaryId,
|
|
225
|
+
scrollableBoundaryId: scrollable
|
|
226
|
+
? scrollableElementId
|
|
227
|
+
: scrollableBoundaryId,
|
|
228
|
+
scrollableBoundaryUpperEdgeRef: scrollable
|
|
229
|
+
? scrollableBoundaryUpperEdgeRef
|
|
230
|
+
: undefined,
|
|
231
|
+
scrollableBoundaryLowerEdgeRef: scrollable
|
|
232
|
+
? scrollableBoundaryLowerEdgeRef
|
|
233
|
+
: undefined,
|
|
234
|
+
scrollableScrollTopRef: scrollable
|
|
235
|
+
? scrollableScrollTopRef
|
|
236
|
+
: undefined,
|
|
237
|
+
dragOverlayRefMap: scrollable ? dragOverlayRefMap : undefined,
|
|
238
|
+
dirtyKeysRef,
|
|
239
|
+
disabledKeysRef,
|
|
240
|
+
scrollableStickyUpperOffset,
|
|
241
|
+
scrollableStickyLowerOffset,
|
|
87
242
|
updateItemSize,
|
|
88
243
|
setChildrenRef,
|
|
89
244
|
setChildrenMTSRef,
|
|
245
|
+
setDragOverlayRef,
|
|
90
246
|
handleDragEnd,
|
|
91
247
|
handleDragMove,
|
|
92
248
|
handleDragStart,
|
|
93
249
|
}), [
|
|
94
250
|
data,
|
|
251
|
+
debugLog,
|
|
95
252
|
enableSorting,
|
|
96
253
|
boundaryId,
|
|
254
|
+
scrollable,
|
|
255
|
+
scrollableContentId,
|
|
256
|
+
scrollableElementId,
|
|
257
|
+
scrollableBoundaryId,
|
|
258
|
+
scrollableBoundaryLowerEdgeRef,
|
|
259
|
+
scrollableBoundaryUpperEdgeRef,
|
|
260
|
+
scrollableScrollTopRef,
|
|
261
|
+
dragOverlayRefMap,
|
|
262
|
+
dirtyKeysRef,
|
|
263
|
+
disabledKeysRef,
|
|
264
|
+
scrollableStickyLowerOffset,
|
|
265
|
+
scrollableStickyUpperOffset,
|
|
97
266
|
updateItemSize,
|
|
98
267
|
setChildrenRef,
|
|
99
268
|
setChildrenMTSRef,
|
|
269
|
+
setDragOverlayRef,
|
|
100
270
|
handleDragEnd,
|
|
101
271
|
handleDragMove,
|
|
102
272
|
handleDragStart,
|
|
103
273
|
])
|
|
104
274
|
|
|
275
|
+
const sortableDragOverlayContextValue = useMemo(() => ({
|
|
276
|
+
...sortableContextValue,
|
|
277
|
+
isDragOverlay: true,
|
|
278
|
+
}), [sortableContextValue])
|
|
279
|
+
|
|
105
280
|
const renderedChildren = useMemo(
|
|
106
281
|
() => data?.map(item => children(item)),
|
|
107
282
|
[data, children],
|
|
108
283
|
)
|
|
109
284
|
|
|
285
|
+
const renderedDragOverlayChildren = useMemo(
|
|
286
|
+
() => data?.map(item => children(item)),
|
|
287
|
+
[data, children],
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
if (scrollable) {
|
|
291
|
+
return (
|
|
292
|
+
<>
|
|
293
|
+
<scroll-view
|
|
294
|
+
id={scrollableElementId}
|
|
295
|
+
className={scrollableClassName}
|
|
296
|
+
enable-scroll={scrollableEnableScroll && !isDragging}
|
|
297
|
+
scroll-orientation='vertical'
|
|
298
|
+
main-thread:bindscroll={handleScrollableBoundaryScroll}
|
|
299
|
+
>
|
|
300
|
+
<view
|
|
301
|
+
id={scrollableContentId}
|
|
302
|
+
className={scrollableContentClassName}
|
|
303
|
+
style={{ zIndex: '0' }}
|
|
304
|
+
>
|
|
305
|
+
<view
|
|
306
|
+
style='display: flex; flex-direction: column; overflow:hidden; height: 1ppx; width: 100%;'
|
|
307
|
+
exposure-scene={scrollableElementId}
|
|
308
|
+
exposure-id='upperExposureView'
|
|
309
|
+
id={`${scrollableElementId}-upperExposureView`}
|
|
310
|
+
main-thread:binduiappear={handleScrollableBoundaryUpperExposure}
|
|
311
|
+
main-thread:binduidisappear={handleScrollableBoundaryUpperDisexposure}
|
|
312
|
+
/>
|
|
313
|
+
<SortableContext.Provider
|
|
314
|
+
value={sortableContextValue}
|
|
315
|
+
>
|
|
316
|
+
{renderedChildren}
|
|
317
|
+
</SortableContext.Provider>
|
|
318
|
+
<view
|
|
319
|
+
style='display: flex; flex-direction: column; overflow:hidden; height: 1ppx; width: 100%;'
|
|
320
|
+
exposure-scene={scrollableElementId}
|
|
321
|
+
exposure-id='lowerExposureView'
|
|
322
|
+
id={`${scrollableElementId}-lowerExposureView`}
|
|
323
|
+
main-thread:binduiappear={handleScrollableBoundaryLowerExposure}
|
|
324
|
+
main-thread:binduidisappear={handleScrollableBoundaryLowerDisexposure}
|
|
325
|
+
/>
|
|
326
|
+
</view>
|
|
327
|
+
</scroll-view>
|
|
328
|
+
<SortableContext.Provider
|
|
329
|
+
value={sortableDragOverlayContextValue}
|
|
330
|
+
>
|
|
331
|
+
{renderedDragOverlayChildren}
|
|
332
|
+
</SortableContext.Provider>
|
|
333
|
+
</>
|
|
334
|
+
)
|
|
335
|
+
}
|
|
336
|
+
|
|
110
337
|
return (
|
|
111
338
|
<SortableContext.Provider
|
|
112
339
|
value={sortableContextValue}
|
|
@@ -126,11 +353,72 @@ interface boundingClientRectRes {
|
|
|
126
353
|
}
|
|
127
354
|
|
|
128
355
|
export function SortableItem(props: SortableItemProps) {
|
|
129
|
-
const {
|
|
356
|
+
const { isDragOverlay } = useContext(SortableContext)
|
|
357
|
+
if (isDragOverlay) {
|
|
358
|
+
return <SortableDragOverlayItem {...props} />
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return <SortableInteractiveItem {...props} />
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function SortableDragOverlayItem(props: SortableItemProps) {
|
|
365
|
+
const { className, children, sortingKey } = props
|
|
366
|
+
const { setDragOverlayRef } = useContext(SortableContext)
|
|
367
|
+
const overlayRef = useMainThreadRef<MainThread.Element | null>(null)
|
|
368
|
+
const overlayElementId = useMemo(
|
|
369
|
+
() => getSortableDragOverlayElementId(sortingKey),
|
|
370
|
+
[sortingKey],
|
|
371
|
+
)
|
|
372
|
+
const overlayStyle = useMemo(() => ({
|
|
373
|
+
position: 'fixed',
|
|
374
|
+
top: '0px',
|
|
375
|
+
left: '0px',
|
|
376
|
+
width: '0px',
|
|
377
|
+
height: '0px',
|
|
378
|
+
opacity: 0,
|
|
379
|
+
visibility: 'hidden',
|
|
380
|
+
zIndex: '10000',
|
|
381
|
+
transform: 'translate(0px, 0px)',
|
|
382
|
+
} as const), [])
|
|
383
|
+
|
|
384
|
+
useEffect(() => {
|
|
385
|
+
runOnMainThread(setDragOverlayRef)(overlayRef, sortingKey)
|
|
386
|
+
}, [overlayRef, setDragOverlayRef, sortingKey])
|
|
387
|
+
|
|
388
|
+
return (
|
|
389
|
+
<view
|
|
390
|
+
id={overlayElementId}
|
|
391
|
+
className={className}
|
|
392
|
+
main-thread:ref={overlayRef}
|
|
393
|
+
style={overlayStyle}
|
|
394
|
+
user-interaction-enabled={false}
|
|
395
|
+
>
|
|
396
|
+
{children}
|
|
397
|
+
</view>
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function SortableInteractiveItem(props: SortableItemProps) {
|
|
402
|
+
const {
|
|
403
|
+
className,
|
|
404
|
+
children,
|
|
405
|
+
sortingKey,
|
|
406
|
+
as = 'Draggable',
|
|
407
|
+
disabled = false,
|
|
408
|
+
} = props
|
|
130
409
|
const {
|
|
131
410
|
data,
|
|
132
411
|
enableSorting,
|
|
133
412
|
boundaryId,
|
|
413
|
+
scrollableBoundaryId,
|
|
414
|
+
scrollableBoundaryUpperEdgeRef,
|
|
415
|
+
scrollableBoundaryLowerEdgeRef,
|
|
416
|
+
scrollableScrollTopRef,
|
|
417
|
+
dragOverlayRefMap,
|
|
418
|
+
dirtyKeysRef,
|
|
419
|
+
disabledKeysRef,
|
|
420
|
+
scrollableStickyUpperOffset,
|
|
421
|
+
scrollableStickyLowerOffset,
|
|
134
422
|
updateItemSize,
|
|
135
423
|
setChildrenRef,
|
|
136
424
|
setChildrenMTSRef,
|
|
@@ -140,7 +428,6 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
140
428
|
} = useContext(SortableContext)
|
|
141
429
|
|
|
142
430
|
const MTSRef = useMainThreadRef<DraggableRef>(null)
|
|
143
|
-
const ref = useRef<NodesRef>(null)
|
|
144
431
|
const [itemRect, setItemRect] = useState<boundingClientRectRes>({
|
|
145
432
|
height: 0,
|
|
146
433
|
width: 0,
|
|
@@ -157,40 +444,561 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
157
444
|
bottom: 0,
|
|
158
445
|
right: 0,
|
|
159
446
|
})
|
|
447
|
+
const [scrollableBoundaryRect, setScrollableBoundaryRect] = useState<
|
|
448
|
+
boundingClientRectRes
|
|
449
|
+
>({
|
|
450
|
+
height: 0,
|
|
451
|
+
width: 0,
|
|
452
|
+
top: 0,
|
|
453
|
+
left: 0,
|
|
454
|
+
bottom: 0,
|
|
455
|
+
right: 0,
|
|
456
|
+
})
|
|
457
|
+
const autoScrollingRef = useMainThreadRef(false)
|
|
458
|
+
const lastAutoScrollTranslateY = useMainThreadRef(0)
|
|
459
|
+
const autoScrollDistanceSum = useMainThreadRef(0)
|
|
460
|
+
const autoScrollCompensationY = useMainThreadRef(0)
|
|
461
|
+
const autoScrollStartScrollTop = useMainThreadRef(0)
|
|
462
|
+
const itemMeasuredScrollTop = useMainThreadRef(0)
|
|
463
|
+
const dragStartScrollDeltaFromMeasuredRect = useMainThreadRef(0)
|
|
464
|
+
const autoScrollOverflow = useMainThreadRef(0)
|
|
465
|
+
const autoScrollDirection = useMainThreadRef(0)
|
|
466
|
+
const autoScrollStickyDirection = useMainThreadRef(0)
|
|
467
|
+
const dragSourceHidden = useMainThreadRef(false)
|
|
468
|
+
const latestDragTranslate = useMainThreadRef<Point>({ x: 0, y: 0 })
|
|
469
|
+
const latestDragEvent = useMainThreadRef<
|
|
470
|
+
MainThread.MouseEvent | MainThread.TouchEvent | null
|
|
471
|
+
>(null)
|
|
472
|
+
const autoScrollFrameRef = useMainThreadRef<(() => void) | null>(null)
|
|
473
|
+
const syncScrollDeltaForItemTranslateY = useCallback((
|
|
474
|
+
scrollByResult?: ScrollByResult,
|
|
475
|
+
fallbackScrollByOffset = 0,
|
|
476
|
+
): ScrollTranslateDelta => {
|
|
477
|
+
'main thread'
|
|
478
|
+
if (scrollableScrollTopRef) {
|
|
479
|
+
const previousScrollTop = scrollableScrollTopRef.current
|
|
480
|
+
const scrollTop = scrollByResult?.scrollTop
|
|
481
|
+
?? scrollByResult?.detail?.scrollTop
|
|
482
|
+
if (typeof scrollTop === 'number') {
|
|
483
|
+
scrollableScrollTopRef.current = scrollTop
|
|
484
|
+
} else if (scrollByResult || fallbackScrollByOffset !== 0) {
|
|
485
|
+
scrollableScrollTopRef.current += fallbackScrollByOffset
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (scrollByResult || fallbackScrollByOffset !== 0) {
|
|
489
|
+
autoScrollDistanceSum.current += scrollableScrollTopRef.current
|
|
490
|
+
- previousScrollTop
|
|
491
|
+
}
|
|
492
|
+
autoScrollCompensationY.current = scrollableScrollTopRef.current
|
|
493
|
+
- autoScrollStartScrollTop.current
|
|
494
|
+
return {
|
|
495
|
+
dragDeltaY: autoScrollCompensationY.current,
|
|
496
|
+
measuredRectDeltaY: scrollableScrollTopRef.current
|
|
497
|
+
- itemMeasuredScrollTop.current,
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (fallbackScrollByOffset !== 0) {
|
|
502
|
+
autoScrollDistanceSum.current += fallbackScrollByOffset
|
|
503
|
+
autoScrollCompensationY.current += fallbackScrollByOffset
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
dragDeltaY: autoScrollCompensationY.current,
|
|
507
|
+
measuredRectDeltaY: dragStartScrollDeltaFromMeasuredRect.current
|
|
508
|
+
+ autoScrollDistanceSum.current,
|
|
509
|
+
}
|
|
510
|
+
}, [
|
|
511
|
+
autoScrollDistanceSum,
|
|
512
|
+
autoScrollCompensationY,
|
|
513
|
+
autoScrollStartScrollTop,
|
|
514
|
+
dragStartScrollDeltaFromMeasuredRect,
|
|
515
|
+
itemMeasuredScrollTop,
|
|
516
|
+
scrollableScrollTopRef,
|
|
517
|
+
])
|
|
518
|
+
|
|
519
|
+
const itemElementId = useMemo(
|
|
520
|
+
() => getSortableItemElementId(sortingKey),
|
|
521
|
+
[sortingKey],
|
|
522
|
+
)
|
|
523
|
+
const setAutoScrolling = useCallback((isAutoScrolling: boolean) => {
|
|
524
|
+
'main thread'
|
|
525
|
+
autoScrollingRef.current = isAutoScrolling
|
|
526
|
+
}, [autoScrollingRef])
|
|
527
|
+
const captureItemMeasuredScrollTop = useCallback(() => {
|
|
528
|
+
'main thread'
|
|
529
|
+
itemMeasuredScrollTop.current = scrollableScrollTopRef?.current ?? 0
|
|
530
|
+
}, [itemMeasuredScrollTop, scrollableScrollTopRef])
|
|
531
|
+
|
|
532
|
+
const measureRectById = useCallback((
|
|
533
|
+
id: string | undefined,
|
|
534
|
+
target: RectTarget,
|
|
535
|
+
) => {
|
|
536
|
+
if (!id) {
|
|
537
|
+
return
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
lynx.createSelectorQuery().select(`#${id}`)?.invoke({
|
|
541
|
+
method: 'boundingClientRect',
|
|
542
|
+
params: {},
|
|
543
|
+
success: (res: unknown) => {
|
|
544
|
+
const rect = res as boundingClientRectRes
|
|
545
|
+
if (target === 'item') {
|
|
546
|
+
setItemRect(rect)
|
|
547
|
+
runOnMainThread(captureItemMeasuredScrollTop)()
|
|
548
|
+
return
|
|
549
|
+
}
|
|
550
|
+
if (target === 'boundary') {
|
|
551
|
+
setBoundaryRect(rect)
|
|
552
|
+
return
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
setScrollableBoundaryRect(rect)
|
|
556
|
+
},
|
|
557
|
+
}).exec()
|
|
558
|
+
}, [captureItemMeasuredScrollTop])
|
|
559
|
+
|
|
560
|
+
const refreshDraggingRects = useCallback(() => {
|
|
561
|
+
measureRectById(itemElementId, 'item')
|
|
562
|
+
measureRectById(boundaryId, 'boundary')
|
|
563
|
+
measureRectById(scrollableBoundaryId, 'scrollableBoundary')
|
|
564
|
+
}, [
|
|
565
|
+
boundaryId,
|
|
566
|
+
itemElementId,
|
|
567
|
+
measureRectById,
|
|
568
|
+
scrollableBoundaryId,
|
|
569
|
+
])
|
|
570
|
+
|
|
571
|
+
const scrollScrollableBoundaryBy = useCallback((
|
|
572
|
+
offset: number,
|
|
573
|
+
onScrolled?: () => void,
|
|
574
|
+
) => {
|
|
575
|
+
'main thread'
|
|
576
|
+
if (
|
|
577
|
+
!scrollableBoundaryId
|
|
578
|
+
|| offset === 0
|
|
579
|
+
) {
|
|
580
|
+
onScrolled?.()
|
|
581
|
+
return
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const element = lynx.querySelector(`#${scrollableBoundaryId}`)
|
|
585
|
+
if (!element) {
|
|
586
|
+
onScrolled?.()
|
|
587
|
+
return
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
element.invoke('scrollBy', {
|
|
591
|
+
offset,
|
|
592
|
+
}).then((res) => {
|
|
593
|
+
'main thread'
|
|
594
|
+
syncScrollDeltaForItemTranslateY(res as ScrollByResult, offset)
|
|
595
|
+
onScrolled?.()
|
|
596
|
+
})
|
|
597
|
+
}, [
|
|
598
|
+
autoScrollingRef,
|
|
599
|
+
scrollableBoundaryId,
|
|
600
|
+
sortingKey,
|
|
601
|
+
syncScrollDeltaForItemTranslateY,
|
|
602
|
+
])
|
|
603
|
+
|
|
604
|
+
const isAutoScrollBlocked = useCallback((offset: number) => {
|
|
605
|
+
'main thread'
|
|
606
|
+
if (offset > 0) { // block autoScroll down when at lower edge
|
|
607
|
+
return scrollableBoundaryLowerEdgeRef?.current === true
|
|
608
|
+
}
|
|
609
|
+
if (offset < 0) { // block autoScroll up when at upper edge
|
|
610
|
+
return scrollableBoundaryUpperEdgeRef?.current === true
|
|
611
|
+
}
|
|
612
|
+
return false
|
|
613
|
+
}, [scrollableBoundaryLowerEdgeRef, scrollableBoundaryUpperEdgeRef])
|
|
614
|
+
|
|
615
|
+
const getDragStartItemBounds = useCallback((translateY: number) => {
|
|
616
|
+
'main thread'
|
|
617
|
+
const measuredTopAtDragStart = itemRect.top
|
|
618
|
+
- dragStartScrollDeltaFromMeasuredRect.current
|
|
619
|
+
const measuredBottomAtDragStart = itemRect.bottom
|
|
620
|
+
- dragStartScrollDeltaFromMeasuredRect.current
|
|
621
|
+
|
|
622
|
+
return {
|
|
623
|
+
top: measuredTopAtDragStart + translateY,
|
|
624
|
+
bottom: measuredBottomAtDragStart + translateY,
|
|
625
|
+
}
|
|
626
|
+
}, [dragStartScrollDeltaFromMeasuredRect, itemRect])
|
|
627
|
+
|
|
628
|
+
const getAutoScrollOverflow = useCallback((
|
|
629
|
+
distanceToTop: number,
|
|
630
|
+
distanceToBottom: number,
|
|
631
|
+
) => {
|
|
632
|
+
'main thread'
|
|
633
|
+
return getAutoScrollTriggerOverflow(
|
|
634
|
+
{ distanceToTop, distanceToBottom },
|
|
635
|
+
scrollableStickyUpperOffset,
|
|
636
|
+
scrollableStickyLowerOffset,
|
|
637
|
+
)
|
|
638
|
+
}, [scrollableStickyLowerOffset, scrollableStickyUpperOffset])
|
|
639
|
+
|
|
640
|
+
const getAutoScrollDirection = useCallback((
|
|
641
|
+
overflowDirection: number,
|
|
642
|
+
translateDeltaY: number,
|
|
643
|
+
) => {
|
|
644
|
+
'main thread'
|
|
645
|
+
if (translateDeltaY === 0) {
|
|
646
|
+
return autoScrollDirection.current || overflowDirection
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
const dragDirection = translateDeltaY > 0 ? 1 : -1
|
|
650
|
+
return dragDirection === overflowDirection ? overflowDirection : 0
|
|
651
|
+
}, [autoScrollDirection])
|
|
652
|
+
|
|
653
|
+
const getVisualTranslateY = useCallback(() => {
|
|
654
|
+
'main thread'
|
|
655
|
+
const scrollDelta = syncScrollDeltaForItemTranslateY()
|
|
656
|
+
if (autoScrollStickyDirection.current > 0) {
|
|
657
|
+
return scrollableBoundaryRect.bottom
|
|
658
|
+
- itemRect.bottom
|
|
659
|
+
- scrollableStickyLowerOffset
|
|
660
|
+
+ scrollDelta.measuredRectDeltaY
|
|
661
|
+
}
|
|
662
|
+
if (autoScrollStickyDirection.current < 0) {
|
|
663
|
+
return scrollableBoundaryRect.top
|
|
664
|
+
- itemRect.top
|
|
665
|
+
+ scrollableStickyUpperOffset
|
|
666
|
+
+ scrollDelta.measuredRectDeltaY
|
|
667
|
+
}
|
|
668
|
+
return latestDragTranslate.current.y + scrollDelta.dragDeltaY
|
|
669
|
+
}, [
|
|
670
|
+
autoScrollStickyDirection,
|
|
671
|
+
itemRect,
|
|
672
|
+
latestDragTranslate,
|
|
673
|
+
scrollableBoundaryRect,
|
|
674
|
+
scrollableStickyLowerOffset,
|
|
675
|
+
scrollableStickyUpperOffset,
|
|
676
|
+
syncScrollDeltaForItemTranslateY,
|
|
677
|
+
])
|
|
678
|
+
|
|
679
|
+
const getVisualBounds = useCallback((visualTranslateY: number) => {
|
|
680
|
+
'main thread'
|
|
681
|
+
const scrollDelta = syncScrollDeltaForItemTranslateY()
|
|
682
|
+
return {
|
|
683
|
+
top: itemRect.top - scrollDelta.measuredRectDeltaY
|
|
684
|
+
+ visualTranslateY,
|
|
685
|
+
left: itemRect.left + latestDragTranslate.current.x,
|
|
686
|
+
width: itemRect.width,
|
|
687
|
+
height: itemRect.height,
|
|
688
|
+
}
|
|
689
|
+
}, [
|
|
690
|
+
itemRect,
|
|
691
|
+
latestDragTranslate,
|
|
692
|
+
syncScrollDeltaForItemTranslateY,
|
|
693
|
+
])
|
|
694
|
+
|
|
695
|
+
const getDragOverlayElement = useCallback(() => {
|
|
696
|
+
'main thread'
|
|
697
|
+
return dragOverlayRefMap?.current?.[sortingKey] ?? null
|
|
698
|
+
}, [dragOverlayRefMap, sortingKey])
|
|
699
|
+
|
|
700
|
+
const hideDragSourceItem = useCallback(() => {
|
|
701
|
+
'main thread'
|
|
702
|
+
if (!scrollableBoundaryId || dragSourceHidden.current) {
|
|
703
|
+
return
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
MTSRef.current?.MTSSetOtherStyles({
|
|
707
|
+
visibility: 'hidden',
|
|
708
|
+
opacity: '0',
|
|
709
|
+
})
|
|
710
|
+
dragSourceHidden.current = true
|
|
711
|
+
dirtyKeysRef.current[sortingKey] = true
|
|
712
|
+
}, [
|
|
713
|
+
MTSRef,
|
|
714
|
+
dragSourceHidden,
|
|
715
|
+
scrollableBoundaryId,
|
|
716
|
+
dirtyKeysRef,
|
|
717
|
+
sortingKey,
|
|
718
|
+
])
|
|
719
|
+
|
|
720
|
+
const showDragSourceItem = useCallback(() => {
|
|
721
|
+
'main thread'
|
|
722
|
+
if (!dragSourceHidden.current) {
|
|
723
|
+
return
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
MTSRef.current?.MTSSetOtherStyles({
|
|
727
|
+
visibility: 'visible',
|
|
728
|
+
opacity: '1',
|
|
729
|
+
})
|
|
730
|
+
dragSourceHidden.current = false
|
|
731
|
+
}, [
|
|
732
|
+
MTSRef,
|
|
733
|
+
dragSourceHidden,
|
|
734
|
+
])
|
|
735
|
+
|
|
736
|
+
const updateDragOverlayTransform = useCallback((visualTranslateY: number) => {
|
|
737
|
+
'main thread'
|
|
738
|
+
const overlay = getDragOverlayElement()
|
|
739
|
+
if (!overlay) {
|
|
740
|
+
return
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
const bounds = getVisualBounds(visualTranslateY)
|
|
744
|
+
overlay.setStyleProperty(
|
|
745
|
+
'transform',
|
|
746
|
+
`translate(${bounds.left}px, ${bounds.top}px)`,
|
|
747
|
+
)
|
|
748
|
+
}, [
|
|
749
|
+
getDragOverlayElement,
|
|
750
|
+
getVisualBounds,
|
|
751
|
+
])
|
|
752
|
+
|
|
753
|
+
const activateDragOverlay = useCallback(() => {
|
|
754
|
+
'main thread'
|
|
755
|
+
if (!scrollableBoundaryId) {
|
|
756
|
+
return
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
hideDragSourceItem()
|
|
760
|
+
|
|
761
|
+
const overlay = getDragOverlayElement()
|
|
762
|
+
if (!overlay) {
|
|
763
|
+
return
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
const visualTranslateY = getVisualTranslateY()
|
|
767
|
+
const bounds = getVisualBounds(visualTranslateY)
|
|
768
|
+
overlay.setStyleProperties({
|
|
769
|
+
position: 'fixed',
|
|
770
|
+
top: '0px',
|
|
771
|
+
left: '0px',
|
|
772
|
+
width: `${bounds.width}px`,
|
|
773
|
+
height: `${bounds.height}px`,
|
|
774
|
+
opacity: '1',
|
|
775
|
+
visibility: 'visible',
|
|
776
|
+
'z-index': '10000',
|
|
777
|
+
transform: `translate(${bounds.left}px, ${bounds.top}px)`,
|
|
778
|
+
})
|
|
779
|
+
}, [
|
|
780
|
+
getDragOverlayElement,
|
|
781
|
+
getVisualBounds,
|
|
782
|
+
getVisualTranslateY,
|
|
783
|
+
hideDragSourceItem,
|
|
784
|
+
scrollableBoundaryId,
|
|
785
|
+
])
|
|
786
|
+
|
|
787
|
+
const applyVisualTranslate = useCallback(() => {
|
|
788
|
+
'main thread'
|
|
789
|
+
const visualTranslateY = getVisualTranslateY()
|
|
790
|
+
if (scrollableBoundaryId) {
|
|
791
|
+
MTSRef.current?.MTSSetTransform(0, 0)
|
|
792
|
+
console.info('reset local drag visual')
|
|
793
|
+
updateDragOverlayTransform(visualTranslateY)
|
|
794
|
+
} else {
|
|
795
|
+
MTSRef.current?.MTSSetTransform(
|
|
796
|
+
latestDragTranslate.current.x,
|
|
797
|
+
visualTranslateY,
|
|
798
|
+
)
|
|
799
|
+
}
|
|
800
|
+
dirtyKeysRef.current[sortingKey] = true
|
|
801
|
+
return visualTranslateY
|
|
802
|
+
}, [
|
|
803
|
+
MTSRef,
|
|
804
|
+
getVisualTranslateY,
|
|
805
|
+
latestDragTranslate,
|
|
806
|
+
scrollableBoundaryId,
|
|
807
|
+
updateDragOverlayTransform,
|
|
808
|
+
dirtyKeysRef,
|
|
809
|
+
sortingKey,
|
|
810
|
+
])
|
|
811
|
+
|
|
812
|
+
const emitSortableDragMove = useCallback((
|
|
813
|
+
visualTranslateY: number,
|
|
814
|
+
event: MainThread.MouseEvent | MainThread.TouchEvent | null,
|
|
815
|
+
) => {
|
|
816
|
+
'main thread'
|
|
817
|
+
if (!event) {
|
|
818
|
+
return
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
handleDragMove?.(
|
|
822
|
+
{ x: latestDragTranslate.current.x, y: visualTranslateY },
|
|
823
|
+
sortingKey,
|
|
824
|
+
event,
|
|
825
|
+
)
|
|
826
|
+
}, [handleDragMove, latestDragTranslate, sortingKey])
|
|
827
|
+
|
|
828
|
+
const scheduleNextAutoScrollFrame = useCallback(() => {
|
|
829
|
+
'main thread'
|
|
830
|
+
const nextFrame = autoScrollFrameRef.current
|
|
831
|
+
if (nextFrame) {
|
|
832
|
+
setTimeout(nextFrame, 8)
|
|
833
|
+
}
|
|
834
|
+
}, [autoScrollFrameRef])
|
|
835
|
+
|
|
836
|
+
const finishAutoScrollFrame = useCallback(() => {
|
|
837
|
+
'main thread'
|
|
838
|
+
const visualTranslateY = applyVisualTranslate()
|
|
839
|
+
|
|
840
|
+
emitSortableDragMove(visualTranslateY, latestDragEvent.current)
|
|
841
|
+
scheduleNextAutoScrollFrame()
|
|
842
|
+
}, [
|
|
843
|
+
applyVisualTranslate,
|
|
844
|
+
emitSortableDragMove,
|
|
845
|
+
latestDragEvent,
|
|
846
|
+
scheduleNextAutoScrollFrame,
|
|
847
|
+
])
|
|
848
|
+
|
|
849
|
+
const runAutoScrollFrame = useCallback(() => {
|
|
850
|
+
'main thread'
|
|
851
|
+
if (!autoScrollingRef.current) {
|
|
852
|
+
return
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const overflow = autoScrollOverflow.current
|
|
856
|
+
if (overflow === 0) {
|
|
857
|
+
setAutoScrolling(false)
|
|
858
|
+
return
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
const direction = autoScrollDirection.current || (overflow > 0 ? 1 : -1)
|
|
862
|
+
if (isAutoScrollBlocked(direction)) {
|
|
863
|
+
const visualTranslateY = applyVisualTranslate()
|
|
864
|
+
emitSortableDragMove(visualTranslateY, latestDragEvent.current)
|
|
865
|
+
setAutoScrolling(false)
|
|
866
|
+
return
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
const step = direction * Math.min(
|
|
870
|
+
AUTO_SCROLL_MAX_STEP,
|
|
871
|
+
Math.abs(overflow) * AUTO_SCROLL_VELOCITY_FACTOR,
|
|
872
|
+
)
|
|
873
|
+
const finishScrolledFrame = () => {
|
|
874
|
+
'main thread'
|
|
875
|
+
finishAutoScrollFrame()
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
scrollScrollableBoundaryBy(step, finishScrolledFrame)
|
|
879
|
+
}, [
|
|
880
|
+
applyVisualTranslate,
|
|
881
|
+
autoScrollDirection,
|
|
882
|
+
autoScrollOverflow,
|
|
883
|
+
autoScrollingRef,
|
|
884
|
+
emitSortableDragMove,
|
|
885
|
+
finishAutoScrollFrame,
|
|
886
|
+
isAutoScrollBlocked,
|
|
887
|
+
latestDragEvent,
|
|
888
|
+
scrollScrollableBoundaryBy,
|
|
889
|
+
setAutoScrolling,
|
|
890
|
+
])
|
|
891
|
+
|
|
892
|
+
const startAutoScrollLoop = useCallback(() => {
|
|
893
|
+
'main thread'
|
|
894
|
+
if (autoScrollingRef.current) {
|
|
895
|
+
return
|
|
896
|
+
}
|
|
897
|
+
autoScrollFrameRef.current = runAutoScrollFrame
|
|
898
|
+
setAutoScrolling(true)
|
|
899
|
+
scheduleNextAutoScrollFrame()
|
|
900
|
+
}, [
|
|
901
|
+
autoScrollingRef,
|
|
902
|
+
runAutoScrollFrame,
|
|
903
|
+
scheduleNextAutoScrollFrame,
|
|
904
|
+
setAutoScrolling,
|
|
905
|
+
])
|
|
906
|
+
|
|
907
|
+
const stopAutoScrollLoop = useCallback(() => {
|
|
908
|
+
'main thread'
|
|
909
|
+
autoScrollOverflow.current = 0
|
|
910
|
+
autoScrollDirection.current = 0
|
|
911
|
+
autoScrollStickyDirection.current = 0
|
|
912
|
+
setAutoScrolling(false)
|
|
913
|
+
}, [
|
|
914
|
+
autoScrollDirection,
|
|
915
|
+
autoScrollOverflow,
|
|
916
|
+
autoScrollStickyDirection,
|
|
917
|
+
setAutoScrolling,
|
|
918
|
+
])
|
|
919
|
+
|
|
160
920
|
useEffect(() => {
|
|
161
921
|
runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
922
|
+
delayFrames(1, refreshDraggingRects)
|
|
923
|
+
}, [
|
|
924
|
+
data,
|
|
925
|
+
refreshDraggingRects,
|
|
926
|
+
setChildrenRef,
|
|
927
|
+
setChildrenMTSRef,
|
|
928
|
+
sortingKey,
|
|
929
|
+
])
|
|
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])
|
|
182
949
|
|
|
183
950
|
const handleMTSLayoutChange = (e: MainThread.LayoutChangeEvent) => {
|
|
184
951
|
'main thread'
|
|
185
952
|
updateItemSize(sortingKey, e.detail.height)
|
|
186
953
|
}
|
|
187
954
|
|
|
955
|
+
const resetAutoScrollDragState = useCallback(() => {
|
|
956
|
+
'main thread'
|
|
957
|
+
lastAutoScrollTranslateY.current = 0
|
|
958
|
+
autoScrollDistanceSum.current = 0
|
|
959
|
+
autoScrollCompensationY.current = 0
|
|
960
|
+
autoScrollOverflow.current = 0
|
|
961
|
+
autoScrollDirection.current = 0
|
|
962
|
+
autoScrollStickyDirection.current = 0
|
|
963
|
+
latestDragTranslate.current = { x: 0, y: 0 }
|
|
964
|
+
}, [
|
|
965
|
+
autoScrollCompensationY,
|
|
966
|
+
autoScrollDirection,
|
|
967
|
+
autoScrollDistanceSum,
|
|
968
|
+
autoScrollOverflow,
|
|
969
|
+
autoScrollStickyDirection,
|
|
970
|
+
lastAutoScrollTranslateY,
|
|
971
|
+
latestDragTranslate,
|
|
972
|
+
])
|
|
973
|
+
|
|
188
974
|
const itemDragStart = (
|
|
189
975
|
pagePoint: Point,
|
|
190
976
|
event: MainThread.MouseEvent | MainThread.TouchEvent,
|
|
191
977
|
) => {
|
|
192
978
|
'main thread'
|
|
979
|
+
resetAutoScrollDragState()
|
|
980
|
+
const scrollTop = scrollableScrollTopRef?.current ?? 0
|
|
981
|
+
autoScrollStartScrollTop.current = scrollTop
|
|
982
|
+
dragStartScrollDeltaFromMeasuredRect.current = scrollTop
|
|
983
|
+
- itemMeasuredScrollTop.current
|
|
984
|
+
latestDragEvent.current = event
|
|
985
|
+
if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
|
|
986
|
+
const draggedItem = getDragStartItemBounds(0)
|
|
987
|
+
const { distanceToTop, distanceToBottom } = getEdgeDistance(
|
|
988
|
+
draggedItem,
|
|
989
|
+
scrollableBoundaryRect,
|
|
990
|
+
)
|
|
991
|
+
const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom)
|
|
992
|
+
if (overflow !== 0) {
|
|
993
|
+
autoScrollStickyDirection.current = overflow > 0 ? 1 : -1
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
activateDragOverlay()
|
|
193
997
|
handleDragStart?.(pagePoint, sortingKey, event)
|
|
998
|
+
if (autoScrollStickyDirection.current !== 0) {
|
|
999
|
+
const visualTranslateY = getVisualTranslateY()
|
|
1000
|
+
emitSortableDragMove(visualTranslateY, event)
|
|
1001
|
+
}
|
|
194
1002
|
}
|
|
195
1003
|
|
|
196
1004
|
const itemDragging = (
|
|
@@ -198,31 +1006,110 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
198
1006
|
event: MainThread.MouseEvent | MainThread.TouchEvent,
|
|
199
1007
|
) => {
|
|
200
1008
|
'main thread'
|
|
1009
|
+
latestDragTranslate.current = translate
|
|
1010
|
+
latestDragEvent.current = event
|
|
1011
|
+
if (scrollableBoundaryId && scrollableBoundaryRect.height > 0) {
|
|
1012
|
+
const draggedItem = getDragStartItemBounds(translate.y)
|
|
1013
|
+
const { distanceToTop, distanceToBottom } = getEdgeDistance(
|
|
1014
|
+
draggedItem,
|
|
1015
|
+
scrollableBoundaryRect,
|
|
1016
|
+
)
|
|
1017
|
+
const translateDeltaY = translate.y - lastAutoScrollTranslateY.current
|
|
1018
|
+
const overflow = getAutoScrollOverflow(distanceToTop, distanceToBottom)
|
|
1019
|
+
|
|
1020
|
+
lastAutoScrollTranslateY.current = translate.y
|
|
1021
|
+
|
|
1022
|
+
if (overflow === 0) {
|
|
1023
|
+
stopAutoScrollLoop()
|
|
1024
|
+
const visualTranslateY = applyVisualTranslate()
|
|
1025
|
+
emitSortableDragMove(visualTranslateY, event)
|
|
1026
|
+
} else {
|
|
1027
|
+
const overflowDirection = overflow > 0 ? 1 : -1
|
|
1028
|
+
const scrollDirection = getAutoScrollDirection(
|
|
1029
|
+
overflowDirection,
|
|
1030
|
+
translateDeltaY,
|
|
1031
|
+
)
|
|
1032
|
+
autoScrollOverflow.current = overflow
|
|
1033
|
+
autoScrollStickyDirection.current = overflowDirection
|
|
1034
|
+
autoScrollDirection.current = scrollDirection
|
|
1035
|
+
const visualTranslateY = applyVisualTranslate()
|
|
1036
|
+
const blockedByScrollableEdge = scrollDirection !== 0
|
|
1037
|
+
&& isAutoScrollBlocked(scrollDirection)
|
|
1038
|
+
if (scrollDirection === 0) {
|
|
1039
|
+
setAutoScrolling(false)
|
|
1040
|
+
} else if (blockedByScrollableEdge) {
|
|
1041
|
+
setAutoScrolling(false)
|
|
1042
|
+
} else {
|
|
1043
|
+
startAutoScrollLoop()
|
|
1044
|
+
}
|
|
1045
|
+
emitSortableDragMove(visualTranslateY, event)
|
|
1046
|
+
}
|
|
1047
|
+
return
|
|
1048
|
+
}
|
|
1049
|
+
|
|
201
1050
|
handleDragMove?.(translate, sortingKey, event)
|
|
202
1051
|
}
|
|
203
1052
|
|
|
1053
|
+
const resetLocalDragVisuals = () => {
|
|
1054
|
+
'main thread'
|
|
1055
|
+
if (!dirtyKeysRef.current[sortingKey]) {
|
|
1056
|
+
return
|
|
1057
|
+
}
|
|
1058
|
+
console.info('reset local drag visual2')
|
|
1059
|
+
MTSRef.current?.MTSSetTransform(0, 0)
|
|
1060
|
+
const overlayWasActive = dragSourceHidden.current
|
|
1061
|
+
showDragSourceItem()
|
|
1062
|
+
if (overlayWasActive) {
|
|
1063
|
+
const overlay = getDragOverlayElement()
|
|
1064
|
+
if (overlay) {
|
|
1065
|
+
overlay.setStyleProperties({
|
|
1066
|
+
opacity: '0',
|
|
1067
|
+
visibility: 'hidden',
|
|
1068
|
+
transform: 'translate(0px, 0px)',
|
|
1069
|
+
})
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
dirtyKeysRef.current[sortingKey] = false
|
|
1073
|
+
}
|
|
1074
|
+
|
|
204
1075
|
const itemDragEnd = (
|
|
205
1076
|
_pagePoint: Point,
|
|
206
1077
|
event: MainThread.MouseEvent | MainThread.TouchEvent,
|
|
207
1078
|
) => {
|
|
208
1079
|
'main thread'
|
|
209
|
-
|
|
1080
|
+
resetAutoScrollDragState()
|
|
1081
|
+
autoScrollStartScrollTop.current = 0
|
|
1082
|
+
dragStartScrollDeltaFromMeasuredRect.current = 0
|
|
1083
|
+
latestDragEvent.current = null
|
|
1084
|
+
stopAutoScrollLoop()
|
|
1085
|
+
const orderChanged = handleDragEnd?.(sortingKey, event) ?? false
|
|
1086
|
+
if (!orderChanged) {
|
|
1087
|
+
// no data update -> no precommit will run -> reset locally now
|
|
1088
|
+
resetLocalDragVisuals()
|
|
1089
|
+
}
|
|
1090
|
+
// if orderChanged: precommit will run resetLocalDragVisuals in the same frame as setData
|
|
210
1091
|
}
|
|
211
1092
|
|
|
212
|
-
const
|
|
213
|
-
() => (
|
|
1093
|
+
const dataKeyOrderSignature = useMemo(
|
|
1094
|
+
() => (data ?? []).map(item => item.getSortingKey()).join('|'),
|
|
214
1095
|
[data],
|
|
215
1096
|
)
|
|
216
1097
|
|
|
1098
|
+
usePreCommit(() => {
|
|
1099
|
+
'main thread'
|
|
1100
|
+
resetLocalDragVisuals()
|
|
1101
|
+
}, [dataKeyOrderSignature])
|
|
1102
|
+
|
|
1103
|
+
const itemDraggable = enableSorting && !disabled
|
|
1104
|
+
|
|
217
1105
|
if (as === 'Draggable') {
|
|
218
1106
|
return (
|
|
219
1107
|
<Draggable
|
|
1108
|
+
id={itemElementId}
|
|
220
1109
|
MTSRef={MTSRef}
|
|
221
|
-
ref={ref}
|
|
222
1110
|
trigger='immediate'
|
|
223
|
-
style={resetStyle}
|
|
224
1111
|
className={className}
|
|
225
|
-
enableDragging={
|
|
1112
|
+
enableDragging={itemDraggable}
|
|
226
1113
|
draggableProps={{
|
|
227
1114
|
'main-thread:bindlayoutchange': handleMTSLayoutChange,
|
|
228
1115
|
}}
|
|
@@ -230,10 +1117,11 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
230
1117
|
onMTSDragEnd={itemDragEnd}
|
|
231
1118
|
onMTSDragging={itemDragging}
|
|
232
1119
|
allowedDirection={['up', 'down']}
|
|
233
|
-
{...(boundaryId
|
|
1120
|
+
{...(!scrollableBoundaryId && boundaryId
|
|
234
1121
|
&& {
|
|
235
|
-
minTranslateY: -(itemRect
|
|
236
|
-
maxTranslateY: boundaryRect
|
|
1122
|
+
minTranslateY: -(itemRect.top - boundaryRect.top),
|
|
1123
|
+
maxTranslateY: boundaryRect.bottom
|
|
1124
|
+
- itemRect.bottom,
|
|
237
1125
|
})}
|
|
238
1126
|
>
|
|
239
1127
|
{children}
|
|
@@ -242,10 +1130,9 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
242
1130
|
} else {
|
|
243
1131
|
return (
|
|
244
1132
|
<DraggableRoot
|
|
1133
|
+
id={itemElementId}
|
|
245
1134
|
MTSRef={MTSRef}
|
|
246
|
-
ref={ref}
|
|
247
1135
|
trigger='immediate'
|
|
248
|
-
style={resetStyle}
|
|
249
1136
|
className={className}
|
|
250
1137
|
draggableProps={{
|
|
251
1138
|
'main-thread:bindlayoutchange': handleMTSLayoutChange,
|
|
@@ -253,11 +1140,12 @@ export function SortableItem(props: SortableItemProps) {
|
|
|
253
1140
|
onMTSDragStart={itemDragStart}
|
|
254
1141
|
onMTSDragEnd={itemDragEnd}
|
|
255
1142
|
onMTSDragging={itemDragging}
|
|
256
|
-
enableDragging={
|
|
257
|
-
{...(boundaryId
|
|
1143
|
+
enableDragging={itemDraggable}
|
|
1144
|
+
{...(!scrollableBoundaryId && boundaryId
|
|
258
1145
|
&& {
|
|
259
|
-
minTranslateY: -(itemRect
|
|
260
|
-
maxTranslateY: boundaryRect
|
|
1146
|
+
minTranslateY: -(itemRect.top - boundaryRect.top),
|
|
1147
|
+
maxTranslateY: boundaryRect.bottom
|
|
1148
|
+
- itemRect.bottom,
|
|
261
1149
|
})}
|
|
262
1150
|
allowedDirection={['up', 'down']}
|
|
263
1151
|
>
|