@lynx-js/lynx-ui-sortable 3.133.0 → 3.133.1
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 +11 -0
- package/dist/Sortable.d.ts.map +1 -1
- package/dist/Sortable.jsx +1 -1
- package/dist/Sortable.jsx.map +1 -1
- package/dist/useSortable.d.ts +4 -4
- package/dist/useSortable.d.ts.map +1 -1
- package/dist/useSortable.jsx.map +1 -1
- package/package.json +3 -3
- package/src/Sortable.tsx +4 -8
- package/src/useSortable.tsx +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @lynx-js/lynx-ui-sortable
|
|
2
2
|
|
|
3
|
+
## 3.133.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Improve Web and desktop touch compatibility by routing `onTouchStart`, ([#200](https://github.com/lynx-family/lynx-ui/pull/200))
|
|
8
|
+
`onTouchMove`, `onTouchEnd`, and `onTouchCancel` through shared touch
|
|
9
|
+
emulation.
|
|
10
|
+
- Updated dependencies [[`3932070`](https://github.com/lynx-family/lynx-ui/commit/393207073522e221a79e65c643df2afb329ec931)]:
|
|
11
|
+
- @lynx-js/lynx-ui-common@3.133.1
|
|
12
|
+
- @lynx-js/lynx-ui-draggable@3.133.1
|
|
13
|
+
|
|
3
14
|
## 3.133.0
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/dist/Sortable.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sortable.d.ts","sourceRoot":"","sources":["../src/Sortable.tsx"],"names":[],"mappings":"AAiBA,OAAO,EAEL,aAAa,EAEd,MAAM,4BAA4B,CAAA;AAKnC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Sortable.d.ts","sourceRoot":"","sources":["../src/Sortable.tsx"],"names":[],"mappings":"AAiBA,OAAO,EAEL,aAAa,EAEd,MAAM,4BAA4B,CAAA;AAKnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAGnE,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,CAAA;AAE5C,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,+BAqF1D;AAWD,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,+BA4IpD"}
|
package/dist/Sortable.jsx
CHANGED
|
@@ -20,7 +20,7 @@ function SortableRoot(props) {
|
|
|
20
20
|
childrenMTSRefMap.current[key] = refI.current;
|
|
21
21
|
}, []);
|
|
22
22
|
const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
|
|
23
|
-
data
|
|
23
|
+
data,
|
|
24
24
|
sizeMap: sizeMap,
|
|
25
25
|
itemRefMap: childrenRefMap,
|
|
26
26
|
itemMTSRefMap: childrenMTSRefMap,
|
package/dist/Sortable.jsx.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sortable.jsx","sources":["webpack://@lynx-js/lynx-ui-sortable/./src/Sortable.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 runOnMainThread,\n useCallback,\n useContext,\n useEffect,\n useMainThreadRef,\n useMemo,\n useRef,\n useState,\n} from '@lynx-js/react'\nimport type { RefObject } from '@lynx-js/react'\n\nimport type { Point } from '@lynx-js/lynx-ui-common'\nimport {\n Draggable,\n DraggableArea,\n DraggableRoot,\n} from '@lynx-js/lynx-ui-draggable'\nimport type { DraggableRef } from '@lynx-js/lynx-ui-draggable'\nimport type { MainThread, NodesRef } from '@lynx-js/types'\n\nimport { SortableContext } from './SortableContext'\nimport type {\n SortableData,\n SortableItemProps,\n SortableRootProps,\n} from './types'\nimport { useSortable } from './useSortable'\n\nexport { DraggableArea as SortableItemArea }\n\nexport function SortableRoot<T>(props: SortableRootProps<T>) {\n const {\n children,\n data,\n debugLog = false,\n boundaryId,\n onSortEnd,\n onSortStart,\n enableSorting = true,\n } = props\n const sizeMap = useMainThreadRef<Record<string, number>>({})\n const childrenRefMap = useMainThreadRef<\n Record<string, DraggableRef | null>\n >(\n {},\n )\n const childrenMTSRefMap = useMainThreadRef<\n Record<string, DraggableRef | null>\n >(\n {},\n )\n const updateItemSize = useCallback((sortingKey: string, size: number) => {\n 'main thread'\n sizeMap.current[sortingKey] = size\n }, [])\n\n const setChildrenRef = useCallback(\n (refI: RefObject<DraggableRef>, key: string) => {\n 'main thread'\n childrenRefMap.current[key] = refI.current\n },\n [],\n )\n\n const setChildrenMTSRef = useCallback((\n refI: RefObject<DraggableRef>,\n key: string,\n ) => {\n 'main thread'\n childrenMTSRefMap.current[key] = refI.current\n }, [])\n\n const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({\n data: data as SortableData<unknown>[],\n sizeMap: sizeMap,\n itemRefMap: childrenRefMap,\n itemMTSRefMap: childrenMTSRefMap,\n onDragEnd: onSortEnd as (sortedData: SortableData<unknown>[]) => void,\n onDragStart: onSortStart,\n debugLog,\n })\n const sortableContextValue = useMemo(() => ({\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragEnd,\n handleDragMove,\n handleDragStart,\n }), [\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragEnd,\n handleDragMove,\n handleDragStart,\n ])\n\n const renderedChildren = useMemo(\n () => data?.map(item => children(item)),\n [data, children],\n )\n\n return (\n <SortableContext.Provider\n value={sortableContextValue}\n >\n {renderedChildren}\n </SortableContext.Provider>\n )\n}\n\ninterface boundingClientRectRes {\n height: number\n width: number\n top: number\n left: number\n bottom: number\n right: number\n}\n\nexport function SortableItem(props: SortableItemProps) {\n const { className, children, sortingKey, as = 'Draggable' } = props\n const {\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragStart,\n handleDragEnd,\n handleDragMove,\n } = useContext(SortableContext)\n\n const MTSRef = useMainThreadRef<DraggableRef>(null)\n const ref = useRef<NodesRef>(null)\n const [itemRect, setItemRect] = useState<boundingClientRectRes>({\n height: 0,\n width: 0,\n top: 0,\n left: 0,\n bottom: 0,\n right: 0,\n })\n const [boundaryRect, setBoundaryRect] = useState<boundingClientRectRes>({\n height: 0,\n width: 0,\n top: 0,\n left: 0,\n bottom: 0,\n right: 0,\n })\n useEffect(() => {\n runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey)\n if (boundaryId) {\n ref?.current\n ?.invoke({\n method: 'boundingClientRect',\n params: {},\n success: (res) => {\n setItemRect(res as boundingClientRectRes)\n },\n })\n .exec()\n lynx.createSelectorQuery().select(`#${boundaryId}`)?.invoke({\n method: 'boundingClientRect',\n params: {},\n success: (res) => {\n setBoundaryRect(res as boundingClientRectRes)\n },\n })\n .exec()\n }\n }, [setChildrenRef, setChildrenMTSRef, sortingKey, data, boundaryId])\n\n const handleMTSLayoutChange = (e: MainThread.LayoutChangeEvent) => {\n 'main thread'\n updateItemSize(sortingKey, e.detail.height)\n }\n\n const itemDragStart = (\n pagePoint: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragStart?.(pagePoint, sortingKey, event)\n }\n\n const itemDragging = (\n translate: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragMove?.(translate, sortingKey, event)\n }\n\n const itemDragEnd = (\n _pagePoint: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragEnd?.(sortingKey, event)\n }\n\n const resetStyle = useMemo(\n () => ({ transform: 'translate(0, 0)', zIndex: '0' }),\n [data],\n )\n\n if (as === 'Draggable') {\n return (\n <Draggable\n MTSRef={MTSRef}\n ref={ref}\n trigger='immediate'\n style={resetStyle}\n className={className}\n enableDragging={enableSorting}\n draggableProps={{\n 'main-thread:bindlayoutchange': handleMTSLayoutChange,\n }}\n onMTSDragStart={itemDragStart}\n onMTSDragEnd={itemDragEnd}\n onMTSDragging={itemDragging}\n allowedDirection={['up', 'down']}\n {...(boundaryId\n && {\n minTranslateY: -(itemRect?.top - boundaryRect?.top),\n maxTranslateY: boundaryRect?.bottom - itemRect?.bottom,\n })}\n >\n {children}\n </Draggable>\n )\n } else {\n return (\n <DraggableRoot\n MTSRef={MTSRef}\n ref={ref}\n trigger='immediate'\n style={resetStyle}\n className={className}\n draggableProps={{\n 'main-thread:bindlayoutchange': handleMTSLayoutChange,\n }}\n onMTSDragStart={itemDragStart}\n onMTSDragEnd={itemDragEnd}\n onMTSDragging={itemDragging}\n enableDragging={enableSorting}\n {...(boundaryId\n && {\n minTranslateY: -(itemRect?.top - boundaryRect?.top),\n maxTranslateY: boundaryRect?.bottom - itemRect?.bottom,\n })}\n allowedDirection={['up', 'down']}\n >\n {children}\n </DraggableRoot>\n )\n }\n}\n"],"names":["SortableRoot","props","children","data","debugLog","boundaryId","onSortEnd","onSortStart","enableSorting","sizeMap","useMainThreadRef","childrenRefMap","childrenMTSRefMap","updateItemSize","useCallback","sortingKey","size","setChildrenRef","refI","key","setChildrenMTSRef","handleDragEnd","handleDragMove","handleDragStart","useSortable","sortableContextValue","useMemo","renderedChildren","item","SortableContext","SortableItem","className","as","useContext","MTSRef","ref","useRef","itemRect","setItemRect","useState","boundaryRect","setBoundaryRect","useEffect","runOnMainThread","res","lynx","handleMTSLayoutChange","e","itemDragStart","pagePoint","event","itemDragging","translate","itemDragEnd","_pagePoint","resetStyle","Draggable","DraggableRoot"],"mappings":";;;;AAmCO,SAASA,aAAgBC,KAA2B;IACzD,MAAM,EACJC,QAAQ,EACRC,IAAI,EACJC,WAAW,KAAK,EAChBC,UAAU,EACVC,SAAS,EACTC,WAAW,EACXC,gBAAgB,IAAI,EACrB,GAAGP;IACJ,MAAMQ,UAAUC,iBAA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gCsC;IAClC,GACA,gBAAgBE,eAChB,cAAcK,aACd,eAAeF,cACf,kBAAkB;QAAC;QAAM;KAAO,GAC/B,GAAI9C,cACA;QACD,eAAe,CAAEgC,CAAAA,UAAU,MAAMG,cAAc,GAAE;QACjD,eAAeA,cAAc,SAASH,UAAU;IAClD,CAAC,EACJ;QACC,CAACnC,SAAS;MACZ,EAAEsD;IAGJ,QACGC,cACC,QAAQvB,QACR,KAAKC,KACL,QAAQ,YACR,OAAOoB,YACP,WAAWxB,WACX,gBAAgB;QACd,gCAAgCe;IAClC,GACA,gBAAgBE,eAChB,cAAcK,aACd,eAAeF,cACf,gBAAgB3C,gBACf,GAAIH,cACA;QACD,eAAe,CAAEgC,CAAAA,UAAU,MAAMG,cAAc,GAAE;QACjD,eAAeA,cAAc,SAASH,UAAU;IAClD,CAAC,EACH,kBAAkB;QAAC;QAAM;KAAO,EACjC;QACC,CAACnC,SAAS;MACZ,EAAEuD;AAGR"}
|
|
1
|
+
{"version":3,"file":"Sortable.jsx","sources":["webpack://@lynx-js/lynx-ui-sortable/./src/Sortable.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 runOnMainThread,\n useCallback,\n useContext,\n useEffect,\n useMainThreadRef,\n useMemo,\n useRef,\n useState,\n} from '@lynx-js/react'\nimport type { RefObject } from '@lynx-js/react'\n\nimport type { Point } from '@lynx-js/lynx-ui-common'\nimport {\n Draggable,\n DraggableArea,\n DraggableRoot,\n} from '@lynx-js/lynx-ui-draggable'\nimport type { DraggableRef } from '@lynx-js/lynx-ui-draggable'\nimport type { MainThread, NodesRef } from '@lynx-js/types'\n\nimport { SortableContext } from './SortableContext'\nimport type { SortableItemProps, SortableRootProps } from './types'\nimport { useSortable } from './useSortable'\n\nexport { DraggableArea as SortableItemArea }\n\nexport function SortableRoot<T>(props: SortableRootProps<T>) {\n const {\n children,\n data,\n debugLog = false,\n boundaryId,\n onSortEnd,\n onSortStart,\n enableSorting = true,\n } = props\n const sizeMap = useMainThreadRef<Record<string, number>>({})\n const childrenRefMap = useMainThreadRef<\n Record<string, DraggableRef | null>\n >(\n {},\n )\n const childrenMTSRefMap = useMainThreadRef<\n Record<string, DraggableRef | null>\n >(\n {},\n )\n const updateItemSize = useCallback((sortingKey: string, size: number) => {\n 'main thread'\n sizeMap.current[sortingKey] = size\n }, [])\n\n const setChildrenRef = useCallback(\n (refI: RefObject<DraggableRef>, key: string) => {\n 'main thread'\n childrenRefMap.current[key] = refI.current\n },\n [],\n )\n\n const setChildrenMTSRef = useCallback((\n refI: RefObject<DraggableRef>,\n key: string,\n ) => {\n 'main thread'\n childrenMTSRefMap.current[key] = refI.current\n }, [])\n\n const { handleDragEnd, handleDragMove, handleDragStart } = useSortable<T>({\n data,\n sizeMap: sizeMap,\n itemRefMap: childrenRefMap,\n itemMTSRefMap: childrenMTSRefMap,\n onDragEnd: onSortEnd,\n onDragStart: onSortStart,\n debugLog,\n })\n const sortableContextValue = useMemo(() => ({\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragEnd,\n handleDragMove,\n handleDragStart,\n }), [\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragEnd,\n handleDragMove,\n handleDragStart,\n ])\n\n const renderedChildren = useMemo(\n () => data?.map(item => children(item)),\n [data, children],\n )\n\n return (\n <SortableContext.Provider\n value={sortableContextValue}\n >\n {renderedChildren}\n </SortableContext.Provider>\n )\n}\n\ninterface boundingClientRectRes {\n height: number\n width: number\n top: number\n left: number\n bottom: number\n right: number\n}\n\nexport function SortableItem(props: SortableItemProps) {\n const { className, children, sortingKey, as = 'Draggable' } = props\n const {\n data,\n enableSorting,\n boundaryId,\n updateItemSize,\n setChildrenRef,\n setChildrenMTSRef,\n handleDragStart,\n handleDragEnd,\n handleDragMove,\n } = useContext(SortableContext)\n\n const MTSRef = useMainThreadRef<DraggableRef>(null)\n const ref = useRef<NodesRef>(null)\n const [itemRect, setItemRect] = useState<boundingClientRectRes>({\n height: 0,\n width: 0,\n top: 0,\n left: 0,\n bottom: 0,\n right: 0,\n })\n const [boundaryRect, setBoundaryRect] = useState<boundingClientRectRes>({\n height: 0,\n width: 0,\n top: 0,\n left: 0,\n bottom: 0,\n right: 0,\n })\n useEffect(() => {\n runOnMainThread(setChildrenMTSRef)(MTSRef, sortingKey)\n if (boundaryId) {\n ref?.current\n ?.invoke({\n method: 'boundingClientRect',\n params: {},\n success: (res) => {\n setItemRect(res as boundingClientRectRes)\n },\n })\n .exec()\n lynx.createSelectorQuery().select(`#${boundaryId}`)?.invoke({\n method: 'boundingClientRect',\n params: {},\n success: (res) => {\n setBoundaryRect(res as boundingClientRectRes)\n },\n })\n .exec()\n }\n }, [setChildrenRef, setChildrenMTSRef, sortingKey, data, boundaryId])\n\n const handleMTSLayoutChange = (e: MainThread.LayoutChangeEvent) => {\n 'main thread'\n updateItemSize(sortingKey, e.detail.height)\n }\n\n const itemDragStart = (\n pagePoint: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragStart?.(pagePoint, sortingKey, event)\n }\n\n const itemDragging = (\n translate: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragMove?.(translate, sortingKey, event)\n }\n\n const itemDragEnd = (\n _pagePoint: Point,\n event: MainThread.MouseEvent | MainThread.TouchEvent,\n ) => {\n 'main thread'\n handleDragEnd?.(sortingKey, event)\n }\n\n const resetStyle = useMemo(\n () => ({ transform: 'translate(0, 0)', zIndex: '0' }),\n [data],\n )\n\n if (as === 'Draggable') {\n return (\n <Draggable\n MTSRef={MTSRef}\n ref={ref}\n trigger='immediate'\n style={resetStyle}\n className={className}\n enableDragging={enableSorting}\n draggableProps={{\n 'main-thread:bindlayoutchange': handleMTSLayoutChange,\n }}\n onMTSDragStart={itemDragStart}\n onMTSDragEnd={itemDragEnd}\n onMTSDragging={itemDragging}\n allowedDirection={['up', 'down']}\n {...(boundaryId\n && {\n minTranslateY: -(itemRect?.top - boundaryRect?.top),\n maxTranslateY: boundaryRect?.bottom - itemRect?.bottom,\n })}\n >\n {children}\n </Draggable>\n )\n } else {\n return (\n <DraggableRoot\n MTSRef={MTSRef}\n ref={ref}\n trigger='immediate'\n style={resetStyle}\n className={className}\n draggableProps={{\n 'main-thread:bindlayoutchange': handleMTSLayoutChange,\n }}\n onMTSDragStart={itemDragStart}\n onMTSDragEnd={itemDragEnd}\n onMTSDragging={itemDragging}\n enableDragging={enableSorting}\n {...(boundaryId\n && {\n minTranslateY: -(itemRect?.top - boundaryRect?.top),\n maxTranslateY: boundaryRect?.bottom - itemRect?.bottom,\n })}\n allowedDirection={['up', 'down']}\n >\n {children}\n </DraggableRoot>\n )\n }\n}\n"],"names":["SortableRoot","props","children","data","debugLog","boundaryId","onSortEnd","onSortStart","enableSorting","sizeMap","useMainThreadRef","childrenRefMap","childrenMTSRefMap","updateItemSize","useCallback","sortingKey","size","setChildrenRef","refI","key","setChildrenMTSRef","handleDragEnd","handleDragMove","handleDragStart","useSortable","sortableContextValue","useMemo","renderedChildren","item","SortableContext","SortableItem","className","as","useContext","MTSRef","ref","useRef","itemRect","setItemRect","useState","boundaryRect","setBoundaryRect","useEffect","runOnMainThread","res","lynx","handleMTSLayoutChange","e","itemDragStart","pagePoint","event","itemDragging","translate","itemDragEnd","_pagePoint","resetStyle","Draggable","DraggableRoot"],"mappings":";;;;AA+BO,SAASA,aAAgBC,KAA2B;IACzD,MAAM,EACJC,QAAQ,EACRC,IAAI,EACJC,WAAW,KAAK,EAChBC,UAAU,EACVC,SAAS,EACTC,WAAW,EACXC,gBAAgB,IAAI,EACrB,GAAGP;IACJ,MAAMQ,UAAUC,iBAAyC,CAAC;IAC1D,MAAMC,iBAAiBD,iBAGrB,CAAC;IAEH,MAAME,oBAAoBF,iBAGxB,CAAC;IAEH,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,cACA;QACD,eAAe,CAAEgC,CAAAA,UAAU,MAAMG,cAAc,GAAE;QACjD,eAAeA,cAAc,SAASH,UAAU;IAClD,CAAC,EACJ;QACC,CAACnC,SAAS;MACZ,EAAEsD;IAGJ,QACGC,cACC,QAAQvB,QACR,KAAKC,KACL,QAAQ,YACR,OAAOoB,YACP,WAAWxB,WACX,gBAAgB;QACd,gCAAgCe;IAClC,GACA,gBAAgBE,eAChB,cAAcK,aACd,eAAeF,cACf,gBAAgB3C,gBACf,GAAIH,cACA;QACD,eAAe,CAAEgC,CAAAA,UAAU,MAAMG,cAAc,GAAE;QACjD,eAAeA,cAAc,SAASH,UAAU;IAClD,CAAC,EACH,kBAAkB;QAAC;QAAM;KAAO,EACjC;QACC,CAACnC,SAAS;MACZ,EAAEuD;AAGR"}
|
package/dist/useSortable.d.ts
CHANGED
|
@@ -3,16 +3,16 @@ import type { Point } from '@lynx-js/lynx-ui-common';
|
|
|
3
3
|
import type { DraggableRef } from '@lynx-js/lynx-ui-draggable';
|
|
4
4
|
import type { MainThread } from '@lynx-js/types';
|
|
5
5
|
import type { SortableData } from './types';
|
|
6
|
-
interface SortableOptionsType {
|
|
7
|
-
data: SortableData<
|
|
6
|
+
interface SortableOptionsType<T> {
|
|
7
|
+
data: SortableData<T>[];
|
|
8
8
|
sizeMap: MainThreadRef<Record<string, number>>;
|
|
9
9
|
itemRefMap: RefObject<Record<string, DraggableRef | null>>;
|
|
10
10
|
itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>>;
|
|
11
|
-
onDragEnd?: (sortedKeyArray: SortableData<
|
|
11
|
+
onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void;
|
|
12
12
|
onDragStart?: () => void;
|
|
13
13
|
debugLog?: boolean;
|
|
14
14
|
}
|
|
15
|
-
export declare function useSortable(useSortableOptions: SortableOptionsType): {
|
|
15
|
+
export declare function useSortable<T>(useSortableOptions: SortableOptionsType<T>): {
|
|
16
16
|
handleDragStart: (pagePoint: Point, sortingKey: string, event: MainThread.MouseEvent | MainThread.TouchEvent) => void;
|
|
17
17
|
handleDragMove: (delta: Point, sortingKey: string, event: MainThread.MouseEvent | MainThread.TouchEvent) => void;
|
|
18
18
|
handleDragEnd: (sortingKey: string, event: MainThread.MouseEvent | MainThread.TouchEvent) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSortable.d.ts","sourceRoot":"","sources":["../src/useSortable.tsx"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,UAAU,mBAAmB;
|
|
1
|
+
{"version":3,"file":"useSortable.d.ts","sourceRoot":"","sources":["../src/useSortable.tsx"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3C,UAAU,mBAAmB,CAAC,CAAC;IAC7B,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,CAAA;IACvB,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IAC9C,UAAU,EAAE,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC,CAAC,CAAA;IAC1D,aAAa,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC,CAAC,CAAA;IACjE,SAAS,CAAC,EAAE,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAA;IACvD,WAAW,CAAC,EAAE,MAAM,IAAI,CAAA;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,wBAAgB,WAAW,CAAC,CAAC,EAC3B,kBAAkB,EAAE,mBAAmB,CAAC,CAAC,CAAC;iCAqC3B,KAAK,cACJ,MAAM,SACX,UAAU,CAAC,UAAU,GAAG,UAAU,CAAC,UAAU;4BAkN7C,KAAK,cACA,MAAM,SACX,UAAU,CAAC,UAAU,GAAG,UAAU,CAAC,UAAU;gCAuDxC,MAAM,SACX,UAAU,CAAC,UAAU,GAAG,UAAU,CAAC,UAAU;EAgBzD"}
|
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 {\n data: SortableData<unknown>[] // 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 onDragEnd?: (sortedKeyArray: SortableData<unknown>[]) => void\n onDragStart?: () => void\n debugLog?: boolean\n}\n\nexport function useSortable(\n useSortableOptions: SortableOptionsType,\n) {\n const {\n data,\n sizeMap,\n itemMTSRefMap,\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 }, [itemMTSRefMap])\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 (item: DraggableRef | null, translate: number) => {\n 'main thread'\n\n item?.MTSSetOtherStyles({ 'transform': `translateY(${translate}px)` })\n },\n [],\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 lastSwappingItem = lastSwappingKey.current\n ? itemMTSRefMap.current[lastSwappingKey.current]\n : null\n if (\n Math.abs(swappingItemTranslation.current)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n setTransform(\n lastSwappingItem,\n (movingDistance < 0 ? 1 : -1) * draggingItemSize,\n )\n } else {\n setTransform(lastSwappingItem, 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 const swappingItem = itemMTSRefMap.current[swappingKey]\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(swappingItem, -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 resetStatus = useCallback(() => {\n 'main thread'\n changedKey.current.map((key: string) => {\n const item = itemMTSRefMap?.current?.[key]\n if (item && typeof item.MTSResetInternalTranslateValues === 'function') {\n item?.MTSResetInternalTranslateValues()\n }\n })\n lastSwappedKey.current = ''\n lastSwappingKey.current = ''\n changedKey.current = []\n }, [changedKey, itemMTSRefMap, lastSwappedKey, lastSwappingKey])\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 ) => {\n 'main thread'\n mtsLog(debugLog, '[event drag end]', event)\n const sortedKey = sortArray(sortingKey)\n runOnBackground(rootDragEnd)(sortedKey)\n resetStatus()\n },\n [resetStatus, rootDragEnd, sortArray],\n )\n\n return {\n handleDragStart,\n handleDragMove,\n handleDragEnd,\n }\n}\n"],"names":["useSortable","useSortableOptions","data","sizeMap","itemMTSRefMap","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","translate","clampPreviousItem","draggingItemSize","lastSwappingItem","updateLastSwappingItem","swappingKey","updateConfirmedInteractedID","unconsumedDistance","swappingIndex","draggedItemIndex","indexModifier","newSwappedKeyIndex","newSwappedKey","switchHandler","consumedDistance","swappingItem","handleDragMove","delta","sortArray","draggingIndex","swappedIndex","swappedKeyArray","draggedItem","resetStatus","key","rootDragEnd","sortedKey","keyToItemMap","Map","sortedData","handleDragEnd"],"mappings":";;AA6BO,SAASA,YACdC,kBAAuC;IAEvC,MAAM,EACJC,IAAI,EACJC,OAAO,EACPC,aAAa,EACbC,SAAS,EACTC,WAAW,E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AGD,gBAAgB,MAAM,CAACF,eAAe;QAC5DE,gBAAgB,MAAM,CAACD,cAAc,GAAGE;QACxC,OAAOD;IACT,GAAG;QAACnD;QAAUS;KAAe;IAE7B,MAAM4C,cAAc1C,YAAY;QAC9B;QACAH,WAAW,OAAO,CAAC,GAAG,CAAC,CAAC8C;YACtB,MAAMpD,OAAOR,eAAe,SAAS,CAAC4D,IAAI;YAC1C,IAAIpD,QAAQ,AAAgD,cAAhD,OAAOA,KAAK,+BAA+B,EACrDA,MAAM;QAEV;QACAO,eAAe,OAAO,GAAG;QACzBH,gBAAgB,OAAO,GAAG;QAC1BE,WAAW,OAAO,GAAG,EAAE;IACzB,GAAG;QAACA;QAAYd;QAAee;QAAgBH;KAAgB;IAE/D,MAAMiD,cAAc5C,YAAY,CAAC6C;QAC/B,MAAMC,eAAe,IAAIC,IAAIlE,KAAK,GAAG,CAAC,CAACU,MAAMoB,QACpC;gBAACtB,QAAQ,CAACsB,MAAM;gBAAEpB;aAAK;QAEhC,MAAMyD,aAAaH,UAChB,GAAG,CAACF,CAAAA,MAAOG,aAAa,GAAG,CAACH,MAC5B,MAAM,CAAC,CAACpD,OAA2C,CAAC,CAACA;QACxDP,YAAYgE;IACd,GAAG;QAACnE;QAAMQ;QAAUL;KAAU;IAE9B,MAAMiE,gBAAgBjD,YACpB,CACEE,YACAI;QAEA;QACAE,OAAOtB,UAAU,oBAAoBoB;QACrC,MAAMuC,YAAYR,UAAUnC;QAC5BK,gBAAgBqC,aAAaC;QAC7BH;IACF,GACA;QAACA;QAAaE;QAAaP;KAAU;IAGvC,OAAO;QACLjC;QACA+B;QACAc;IACF;AACF"}
|
|
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 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 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 }, [itemMTSRefMap])\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 (item: DraggableRef | null, translate: number) => {\n 'main thread'\n\n item?.MTSSetOtherStyles({ 'transform': `translateY(${translate}px)` })\n },\n [],\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 lastSwappingItem = lastSwappingKey.current\n ? itemMTSRefMap.current[lastSwappingKey.current]\n : null\n if (\n Math.abs(swappingItemTranslation.current)\n > draggingItemSize * swapConfirmedPercentage.current\n ) {\n setTransform(\n lastSwappingItem,\n (movingDistance < 0 ? 1 : -1) * draggingItemSize,\n )\n } else {\n setTransform(lastSwappingItem, 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 const swappingItem = itemMTSRefMap.current[swappingKey]\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(swappingItem, -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 resetStatus = useCallback(() => {\n 'main thread'\n changedKey.current.map((key: string) => {\n const item = itemMTSRefMap?.current?.[key]\n if (item && typeof item.MTSResetInternalTranslateValues === 'function') {\n item?.MTSResetInternalTranslateValues()\n }\n })\n lastSwappedKey.current = ''\n lastSwappingKey.current = ''\n changedKey.current = []\n }, [changedKey, itemMTSRefMap, lastSwappedKey, lastSwappingKey])\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 ) => {\n 'main thread'\n mtsLog(debugLog, '[event drag end]', event)\n const sortedKey = sortArray(sortingKey)\n runOnBackground(rootDragEnd)(sortedKey)\n resetStatus()\n },\n [resetStatus, rootDragEnd, sortArray],\n )\n\n return {\n handleDragStart,\n handleDragMove,\n handleDragEnd,\n }\n}\n"],"names":["useSortable","useSortableOptions","data","sizeMap","itemMTSRefMap","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","translate","clampPreviousItem","draggingItemSize","lastSwappingItem","updateLastSwappingItem","swappingKey","updateConfirmedInteractedID","unconsumedDistance","swappingIndex","draggedItemIndex","indexModifier","newSwappedKeyIndex","newSwappedKey","switchHandler","consumedDistance","swappingItem","handleDragMove","delta","sortArray","draggingIndex","swappedIndex","swappedKeyArray","draggedItem","resetStatus","key","rootDragEnd","sortedKey","keyToItemMap","Map","sortedData","handleDragEnd"],"mappings":";;AA6BO,SAASA,YACdC,kBAA0C;IAE1C,MAAM,EACJC,IAAI,EACJC,OAAO,EACPC,aAAa,EACbC,SAAS,EACTC,WAAW,EACXC,WAAW,KAAK,EACjB,GAAGN;IAEJ,MAAMO,WAAWC,MAAM,OAAO,CAACP,QAAQA,OAAO,EAAE;IAChD,MAAMQ,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,CAACC,QAAgBC;QACpD;QACA,MAAMX,OAAOR,cAAc,OAAO,CAACmB,WAAW;QAE9CX,MAAM,kBAAkB;YACtB,WAAW,GAAGU,QAAQ;QACxB;IACF,GAAG;QAAClB;KAAc;IAElB,MAAMoB,oBAAoBH,YAAY;QACpCf;IACF,GAAG;QAACA;KAAY;IAEhB,MAAMmB,kBAAkBJ,YACtB,CACEK,WACAH,YACAI;QAEA;QACAC,gBAAgBJ;QAChBT,gBAAgB,OAAO,GAAGW;QAC1BR,WAAW,OAAO,CAAC,IAAI,CAACK;QACxBH,iBAAiB,OAAOG;QACxBM,OAAOtB,UAAU,sB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;QAEA;QACAE,OAAOtB,UAAU,oBAAoBoB;QACrC,MAAMuC,YAAYR,UAAUnC;QAC5BK,gBAAgBqC,aAAaC;QAC7BH;IACF,GACA;QAACA;QAAaE;QAAaP;KAAU;IAGvC,OAAO;QACLjC;QACA+B;QACAc;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/lynx-ui-sortable",
|
|
3
|
-
"version": "3.133.
|
|
3
|
+
"version": "3.133.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/lynx-family/lynx-ui.git",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"dist/**"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@lynx-js/lynx-ui-common": "3.133.
|
|
29
|
-
"@lynx-js/lynx-ui-draggable": "3.133.
|
|
28
|
+
"@lynx-js/lynx-ui-common": "3.133.1",
|
|
29
|
+
"@lynx-js/lynx-ui-draggable": "3.133.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@lynx-js/react": "0.114.0",
|
package/src/Sortable.tsx
CHANGED
|
@@ -24,11 +24,7 @@ import type { DraggableRef } from '@lynx-js/lynx-ui-draggable'
|
|
|
24
24
|
import type { MainThread, NodesRef } from '@lynx-js/types'
|
|
25
25
|
|
|
26
26
|
import { SortableContext } from './SortableContext'
|
|
27
|
-
import type {
|
|
28
|
-
SortableData,
|
|
29
|
-
SortableItemProps,
|
|
30
|
-
SortableRootProps,
|
|
31
|
-
} from './types'
|
|
27
|
+
import type { SortableItemProps, SortableRootProps } from './types'
|
|
32
28
|
import { useSortable } from './useSortable'
|
|
33
29
|
|
|
34
30
|
export { DraggableArea as SortableItemArea }
|
|
@@ -75,12 +71,12 @@ export function SortableRoot<T>(props: SortableRootProps<T>) {
|
|
|
75
71
|
childrenMTSRefMap.current[key] = refI.current
|
|
76
72
|
}, [])
|
|
77
73
|
|
|
78
|
-
const { handleDragEnd, handleDragMove, handleDragStart } = useSortable({
|
|
79
|
-
data
|
|
74
|
+
const { handleDragEnd, handleDragMove, handleDragStart } = useSortable<T>({
|
|
75
|
+
data,
|
|
80
76
|
sizeMap: sizeMap,
|
|
81
77
|
itemRefMap: childrenRefMap,
|
|
82
78
|
itemMTSRefMap: childrenMTSRefMap,
|
|
83
|
-
onDragEnd: onSortEnd
|
|
79
|
+
onDragEnd: onSortEnd,
|
|
84
80
|
onDragStart: onSortStart,
|
|
85
81
|
debugLog,
|
|
86
82
|
})
|
package/src/useSortable.tsx
CHANGED
|
@@ -17,18 +17,18 @@ import type { MainThread } from '@lynx-js/types'
|
|
|
17
17
|
|
|
18
18
|
import type { SortableData } from './types'
|
|
19
19
|
|
|
20
|
-
interface SortableOptionsType {
|
|
21
|
-
data: SortableData<
|
|
20
|
+
interface SortableOptionsType<T> {
|
|
21
|
+
data: SortableData<T>[] // sorting key array
|
|
22
22
|
sizeMap: MainThreadRef<Record<string, number>> // sorting key -> size
|
|
23
23
|
itemRefMap: RefObject<Record<string, DraggableRef | null>>
|
|
24
24
|
itemMTSRefMap: MainThreadRef<Record<string, DraggableRef | null>> // sortingKey -> element Ref
|
|
25
|
-
onDragEnd?: (sortedKeyArray: SortableData<
|
|
25
|
+
onDragEnd?: (sortedKeyArray: SortableData<T>[]) => void
|
|
26
26
|
onDragStart?: () => void
|
|
27
27
|
debugLog?: boolean
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export function useSortable(
|
|
31
|
-
useSortableOptions: SortableOptionsType
|
|
30
|
+
export function useSortable<T>(
|
|
31
|
+
useSortableOptions: SortableOptionsType<T>,
|
|
32
32
|
) {
|
|
33
33
|
const {
|
|
34
34
|
data,
|