@octanejs/dnd-kit-sortable 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/LICENSE.upstream +21 -0
- package/README.md +85 -2
- package/UPSTREAM.md +56 -0
- package/package.json +56 -3
- package/src/components/SortableContext.tsx +109 -0
- package/src/components/index.ts +2 -0
- package/src/hooks/defaults.ts +51 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/types.ts +28 -0
- package/src/hooks/useSortable.ts +243 -0
- package/src/hooks/utilities/index.ts +1 -0
- package/src/hooks/utilities/useDerivedTransform.ts +55 -0
- package/src/index.ts +14 -0
- package/src/sensors/index.ts +1 -0
- package/src/sensors/keyboard/index.ts +1 -0
- package/src/sensors/keyboard/sortableKeyboardCoordinates.ts +147 -0
- package/src/strategies/horizontalListSorting.ts +83 -0
- package/src/strategies/index.ts +4 -0
- package/src/strategies/rectSorting.ts +20 -0
- package/src/strategies/rectSwapping.ts +27 -0
- package/src/strategies/verticalListSorting.ts +87 -0
- package/src/types/data.ts +9 -0
- package/src/types/disabled.ts +4 -0
- package/src/types/index.ts +4 -0
- package/src/types/strategies.ts +10 -0
- package/src/types/type-guard.ts +26 -0
- package/src/utilities/arrayMove.ts +9 -0
- package/src/utilities/arraySwap.ts +11 -0
- package/src/utilities/getSortedRects.ts +20 -0
- package/src/utilities/index.ts +6 -0
- package/src/utilities/isValidIndex.ts +3 -0
- package/src/utilities/itemsEqual.ts +19 -0
- package/src/utilities/normalizeDisabled.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dominic Gannaway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/LICENSE.upstream
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021, Claudéric Demers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@octanejs/dnd-kit-sortable`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sortable presets for the legacy dnd kit API on Octane: `SortableContext`,
|
|
4
|
+
`useSortable`, sorting strategies, `arrayMove`, and keyboard sorting, ported
|
|
5
|
+
from `@dnd-kit/sortable` 10.0.0. It builds on `@octanejs/dnd-kit-core`.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @octanejs/dnd-kit-sortable @octanejs/dnd-kit-core octane
|
|
11
|
+
pnpm add @octanejs/dnd-kit-sortable @octanejs/dnd-kit-core octane
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Replace imports from `@dnd-kit/sortable` with `@octanejs/dnd-kit-sortable`;
|
|
15
|
+
[`@octanejs/codemod`](../codemod) rewrites them for you.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
The example also uses `CSS` from `@octanejs/dnd-kit-utilities`, which you can
|
|
20
|
+
install alongside it.
|
|
21
|
+
|
|
22
|
+
```tsrx
|
|
23
|
+
import { DndContext, KeyboardSensor, PointerSensor, closestCenter, useSensor, useSensors } from '@octanejs/dnd-kit-core';
|
|
24
|
+
import {
|
|
25
|
+
SortableContext,
|
|
26
|
+
arrayMove,
|
|
27
|
+
sortableKeyboardCoordinates,
|
|
28
|
+
useSortable,
|
|
29
|
+
verticalListSortingStrategy,
|
|
30
|
+
} from '@octanejs/dnd-kit-sortable';
|
|
31
|
+
import { CSS } from '@octanejs/dnd-kit-utilities';
|
|
32
|
+
import { useState } from 'octane';
|
|
33
|
+
|
|
34
|
+
function Row(props: { id: string }) @{
|
|
35
|
+
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id: props.id });
|
|
36
|
+
<li
|
|
37
|
+
ref={setNodeRef}
|
|
38
|
+
style={{ transform: CSS.Transform.toString(transform), transition }}
|
|
39
|
+
{...attributes}
|
|
40
|
+
{...listeners}
|
|
41
|
+
>
|
|
42
|
+
{props.id as string}
|
|
43
|
+
</li>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function List() @{
|
|
47
|
+
const [items, setItems] = useState(['a', 'b', 'c']);
|
|
48
|
+
const sensors = useSensors(
|
|
49
|
+
useSensor(PointerSensor),
|
|
50
|
+
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }),
|
|
51
|
+
);
|
|
52
|
+
<DndContext
|
|
53
|
+
sensors={sensors}
|
|
54
|
+
collisionDetection={closestCenter}
|
|
55
|
+
onDragEnd={({ active, over }) => {
|
|
56
|
+
if (over && active.id !== over.id) {
|
|
57
|
+
setItems((current) =>
|
|
58
|
+
arrayMove(current, current.indexOf(String(active.id)), current.indexOf(String(over.id))),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
<SortableContext items={items} strategy={verticalListSortingStrategy}>
|
|
64
|
+
<ul>
|
|
65
|
+
@for (const id of items; key id) {
|
|
66
|
+
<Row id={id} />
|
|
67
|
+
}
|
|
68
|
+
</ul>
|
|
69
|
+
</SortableContext>
|
|
70
|
+
</DndContext>
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## About this port
|
|
75
|
+
|
|
76
|
+
This package is part of Octane's port of the legacy dnd kit API, the
|
|
77
|
+
`@dnd-kit/core` 6.x family: `@octanejs/dnd-kit-core`, `@octanejs/dnd-kit-sortable`,
|
|
78
|
+
`@octanejs/dnd-kit-modifiers`, `@octanejs/dnd-kit-utilities`, and
|
|
79
|
+
`@octanejs/dnd-kit-accessibility`, one per upstream package. For the modern
|
|
80
|
+
`@dnd-kit/react` API, use [`@octanejs/dnd-kit`](../dnd-kit).
|
|
81
|
+
|
|
82
|
+
Parity is `recorded-unverified`. Upstream's own unit tests are placeholders, so
|
|
83
|
+
each package runs them pristine and adapted, and a repo-authored differential test
|
|
84
|
+
runs the same fixtures against the published React package and compares the DOM
|
|
85
|
+
after every drag step. See [`UPSTREAM.md`](./UPSTREAM.md) for the pin and the
|
|
86
|
+
evidence.
|
package/UPSTREAM.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# `@octanejs/dnd-kit-sortable` upstream ledger
|
|
2
|
+
|
|
3
|
+
`@octanejs/dnd-kit-sortable` ports `@dnd-kit/sortable@10.0.0` from
|
|
4
|
+
`https://github.com/clauderic/dnd-kit.git`, the legacy dnd kit API.
|
|
5
|
+
|
|
6
|
+
## Pin
|
|
7
|
+
|
|
8
|
+
- Package: `@dnd-kit/sortable@10.0.0`, the latest release.
|
|
9
|
+
- Source commit: `e9215e820798459ae036896fce7fd9a6fe855772`, the `@dnd-kit/core@6.3.1` release tag. It holds all
|
|
10
|
+
five legacy packages at their latest releases. Their trees there are
|
|
11
|
+
byte-identical to their own release tags; this package's tag is
|
|
12
|
+
`@dnd-kit/sortable@10.0.0` at `2d862922975e5fa73e1a11845f8c781296f21d4b`.
|
|
13
|
+
- npm integrity: `sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==`
|
|
14
|
+
- npm tarball SHA-256: `07d017cb1e84bfad0687c5415594cb3e4a8045cc7edfa9066cadda5e5323f604`
|
|
15
|
+
- License: MIT. `LICENSE.upstream` is the upstream package's license, byte for byte.
|
|
16
|
+
- React oracle: `react@19.2.7` and `react-dom@19.2.7` from the default catalog.
|
|
17
|
+
|
|
18
|
+
The legacy packages were published without a `gitHead` or npm provenance, so the
|
|
19
|
+
repository's preflight cannot tie the npm artifact to a commit on its own. The
|
|
20
|
+
source is pinned to the release tag instead, and the parity status stays
|
|
21
|
+
`recorded-unverified`.
|
|
22
|
+
|
|
23
|
+
`upstream/` holds the pinned package directory byte for byte. Each file
|
|
24
|
+
is checked against its git blob SHA in `audit/upstream.lock.json`:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pnpm react-port:materialize run --check --package-dir packages/dnd-kit-sortable
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Source boundary
|
|
31
|
+
|
|
32
|
+
`src/` mirrors `upstream/src/` file for file, formatted with the repository's
|
|
33
|
+
Prettier settings, with `react`/`react-dom` imports pointed at `octane` and
|
|
34
|
+
`@dnd-kit/*` imports pointed at the `@octanejs/dnd-kit-*` packages. Files that
|
|
35
|
+
change beyond that:
|
|
36
|
+
|
|
37
|
+
- `components/SortableContext.tsx`: `createContext` from `octane`, rendered as `<Context value>`.
|
|
38
|
+
- `hooks/utilities/useDerivedTransform.ts`: the `MutableRefObject` type comes from `octane`.
|
|
39
|
+
|
|
40
|
+
## Public surface
|
|
41
|
+
|
|
42
|
+
Every runtime export of `@dnd-kit/sortable@10.0.0` is ported under the same name: `SortableContext`, `useSortable`, `defaultAnimateLayoutChanges`, `defaultNewIndexGetter`, the four strategies, `sortableKeyboardCoordinates`, `arrayMove`, `arraySwap`, `hasSortableData`, and every public type.
|
|
43
|
+
`tests/exports.test.ts` compares the export names and value kinds with the published
|
|
44
|
+
package, and `typetests/public-api.test-d.ts` checks the public types.
|
|
45
|
+
|
|
46
|
+
## Tests
|
|
47
|
+
|
|
48
|
+
| Upstream | Disposition |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| `test/sortable.test.tsx (1 placeholder case)` | Runs unchanged against the pinned source and React (`dnd-kit-sortable-pristine`), and regenerated from the lock against the port (`dnd-kit-sortable-upstream`). |
|
|
51
|
+
| Repository Cypress specs (`cypress/integration/draggable_spec.ts`, `sortable_spec.ts`) | Not ported. They drive the Storybook stories outside the package boundary. The differential below covers the same pointer and keyboard drags against the published package. |
|
|
52
|
+
|
|
53
|
+
Because the upstream unit tests are placeholders, the behavioral contract is the
|
|
54
|
+
repo-authored differential: `tests/differential/parity.test.ts` (a keyboard reorder of a vertical list, a keyboard cancel, and a pointer reorder of horizontal tabs past a distance constraint). `audit/react-parity.json` registers all three
|
|
55
|
+
lanes; `node scripts/react-parity/dnd-kit-legacy-manifest.mjs` regenerates it and
|
|
56
|
+
the runtime inventories.
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/dnd-kit-sortable",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.22.2"
|
|
9
|
+
},
|
|
10
|
+
"description": "dnd kit sortable presets for the octane renderer — a port of the legacy @dnd-kit/sortable 10.0.0: SortableContext, useSortable, sorting strategies, arrayMove, and sortableKeyboardCoordinates.",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Dominic Gannaway",
|
|
13
|
+
"email": "dg@domgan.com"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
21
|
+
"directory": "packages/dnd-kit-sortable"
|
|
22
|
+
},
|
|
23
|
+
"main": "src/index.ts",
|
|
24
|
+
"module": "src/index.ts",
|
|
25
|
+
"types": "src/index.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"README.md",
|
|
29
|
+
"UPSTREAM.md",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"LICENSE.upstream"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./src/index.ts"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@octanejs/dnd-kit-utilities": "0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@octanejs/dnd-kit-core": "0.0.1",
|
|
41
|
+
"octane": "^0.16.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@dnd-kit/core": "6.3.1",
|
|
45
|
+
"@dnd-kit/sortable": "10.0.0",
|
|
46
|
+
"@dnd-kit/utilities": "3.2.2",
|
|
47
|
+
"@octanejs/dnd-kit-core": "0.0.1",
|
|
48
|
+
"@tsrx/react": "^0.4.5",
|
|
49
|
+
"esbuild": "^0.28.1",
|
|
50
|
+
"octane": "0.16.4",
|
|
51
|
+
"react": "19.2.7",
|
|
52
|
+
"react-dom": "19.2.7",
|
|
53
|
+
"vitest": "^4.1.10"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"test": "vitest run --root ../.. --project dnd-kit-sortable",
|
|
57
|
+
"typecheck": "octane-tsc -p tsconfig.json"
|
|
58
|
+
}
|
|
6
59
|
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** @jsxImportSource octane */
|
|
2
|
+
import { createContext, useEffect, useMemo, useRef } from 'octane';
|
|
3
|
+
import type { OctaneNode } from 'octane';
|
|
4
|
+
import { useDndContext, ClientRect, UniqueIdentifier } from '@octanejs/dnd-kit-core';
|
|
5
|
+
import { useIsomorphicLayoutEffect, useUniqueId } from '@octanejs/dnd-kit-utilities';
|
|
6
|
+
|
|
7
|
+
import type { Disabled, SortingStrategy } from '../types';
|
|
8
|
+
import { getSortedRects, itemsEqual, normalizeDisabled } from '../utilities';
|
|
9
|
+
import { rectSortingStrategy } from '../strategies';
|
|
10
|
+
|
|
11
|
+
export interface Props {
|
|
12
|
+
children: OctaneNode;
|
|
13
|
+
items: (UniqueIdentifier | { id: UniqueIdentifier })[];
|
|
14
|
+
strategy?: SortingStrategy;
|
|
15
|
+
id?: string;
|
|
16
|
+
disabled?: boolean | Disabled;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ID_PREFIX = 'Sortable';
|
|
20
|
+
|
|
21
|
+
interface ContextDescriptor {
|
|
22
|
+
activeIndex: number;
|
|
23
|
+
containerId: string;
|
|
24
|
+
disabled: Disabled;
|
|
25
|
+
disableTransforms: boolean;
|
|
26
|
+
items: UniqueIdentifier[];
|
|
27
|
+
overIndex: number;
|
|
28
|
+
useDragOverlay: boolean;
|
|
29
|
+
sortedRects: ClientRect[];
|
|
30
|
+
strategy: SortingStrategy;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const Context = createContext<ContextDescriptor>({
|
|
34
|
+
activeIndex: -1,
|
|
35
|
+
containerId: ID_PREFIX,
|
|
36
|
+
disableTransforms: false,
|
|
37
|
+
items: [],
|
|
38
|
+
overIndex: -1,
|
|
39
|
+
useDragOverlay: false,
|
|
40
|
+
sortedRects: [],
|
|
41
|
+
strategy: rectSortingStrategy,
|
|
42
|
+
disabled: {
|
|
43
|
+
draggable: false,
|
|
44
|
+
droppable: false,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export function SortableContext({
|
|
49
|
+
children,
|
|
50
|
+
id,
|
|
51
|
+
items: userDefinedItems,
|
|
52
|
+
strategy = rectSortingStrategy,
|
|
53
|
+
disabled: disabledProp = false,
|
|
54
|
+
}: Props) {
|
|
55
|
+
const { active, dragOverlay, droppableRects, over, measureDroppableContainers } = useDndContext();
|
|
56
|
+
const containerId = useUniqueId(ID_PREFIX, id);
|
|
57
|
+
const useDragOverlay = Boolean(dragOverlay.rect !== null);
|
|
58
|
+
const items = useMemo<UniqueIdentifier[]>(
|
|
59
|
+
() =>
|
|
60
|
+
userDefinedItems.map((item) => (typeof item === 'object' && 'id' in item ? item.id : item)),
|
|
61
|
+
[userDefinedItems],
|
|
62
|
+
);
|
|
63
|
+
const isDragging = active != null;
|
|
64
|
+
const activeIndex = active ? items.indexOf(active.id) : -1;
|
|
65
|
+
const overIndex = over ? items.indexOf(over.id) : -1;
|
|
66
|
+
const previousItemsRef = useRef(items);
|
|
67
|
+
const itemsHaveChanged = !itemsEqual(items, previousItemsRef.current);
|
|
68
|
+
const disableTransforms = (overIndex !== -1 && activeIndex === -1) || itemsHaveChanged;
|
|
69
|
+
const disabled = normalizeDisabled(disabledProp);
|
|
70
|
+
|
|
71
|
+
useIsomorphicLayoutEffect(() => {
|
|
72
|
+
if (itemsHaveChanged && isDragging) {
|
|
73
|
+
measureDroppableContainers(items);
|
|
74
|
+
}
|
|
75
|
+
}, [itemsHaveChanged, items, isDragging, measureDroppableContainers]);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
previousItemsRef.current = items;
|
|
79
|
+
}, [items]);
|
|
80
|
+
|
|
81
|
+
const contextValue = useMemo(
|
|
82
|
+
(): ContextDescriptor => ({
|
|
83
|
+
activeIndex,
|
|
84
|
+
containerId,
|
|
85
|
+
disabled,
|
|
86
|
+
disableTransforms,
|
|
87
|
+
items,
|
|
88
|
+
overIndex,
|
|
89
|
+
useDragOverlay,
|
|
90
|
+
sortedRects: getSortedRects(items, droppableRects),
|
|
91
|
+
strategy,
|
|
92
|
+
}),
|
|
93
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
94
|
+
[
|
|
95
|
+
activeIndex,
|
|
96
|
+
containerId,
|
|
97
|
+
disabled.draggable,
|
|
98
|
+
disabled.droppable,
|
|
99
|
+
disableTransforms,
|
|
100
|
+
items,
|
|
101
|
+
overIndex,
|
|
102
|
+
droppableRects,
|
|
103
|
+
useDragOverlay,
|
|
104
|
+
strategy,
|
|
105
|
+
],
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return <Context value={contextValue}>{children}</Context>;
|
|
109
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { CSS } from '@octanejs/dnd-kit-utilities';
|
|
2
|
+
|
|
3
|
+
import { arrayMove } from '../utilities';
|
|
4
|
+
|
|
5
|
+
import type { AnimateLayoutChanges, NewIndexGetter, SortableTransition } from './types';
|
|
6
|
+
|
|
7
|
+
export const defaultNewIndexGetter: NewIndexGetter = ({ id, items, activeIndex, overIndex }) =>
|
|
8
|
+
arrayMove(items, activeIndex, overIndex).indexOf(id);
|
|
9
|
+
|
|
10
|
+
export const defaultAnimateLayoutChanges: AnimateLayoutChanges = ({
|
|
11
|
+
containerId,
|
|
12
|
+
isSorting,
|
|
13
|
+
wasDragging,
|
|
14
|
+
index,
|
|
15
|
+
items,
|
|
16
|
+
newIndex,
|
|
17
|
+
previousItems,
|
|
18
|
+
previousContainerId,
|
|
19
|
+
transition,
|
|
20
|
+
}) => {
|
|
21
|
+
if (!transition || !wasDragging) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (previousItems !== items && index === newIndex) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (isSorting) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return newIndex !== index && containerId === previousContainerId;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const defaultTransition: SortableTransition = {
|
|
37
|
+
duration: 200,
|
|
38
|
+
easing: 'ease',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const transitionProperty = 'transform';
|
|
42
|
+
|
|
43
|
+
export const disabledTransition = CSS.Transition.toString({
|
|
44
|
+
property: transitionProperty,
|
|
45
|
+
duration: 0,
|
|
46
|
+
easing: 'linear',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export const defaultAttributes = {
|
|
50
|
+
roleDescription: 'sortable',
|
|
51
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { useSortable } from './useSortable';
|
|
2
|
+
export type { Arguments as UseSortableArguments } from './useSortable';
|
|
3
|
+
|
|
4
|
+
export { defaultAnimateLayoutChanges, defaultNewIndexGetter } from './defaults';
|
|
5
|
+
export type { AnimateLayoutChanges, NewIndexGetter } from './types';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Active, UniqueIdentifier } from '@octanejs/dnd-kit-core';
|
|
2
|
+
import type { Transition } from '@octanejs/dnd-kit-utilities';
|
|
3
|
+
|
|
4
|
+
export type SortableTransition = Pick<Transition, 'easing' | 'duration'>;
|
|
5
|
+
|
|
6
|
+
export type AnimateLayoutChanges = (args: {
|
|
7
|
+
active: Active | null;
|
|
8
|
+
containerId: UniqueIdentifier;
|
|
9
|
+
isDragging: boolean;
|
|
10
|
+
isSorting: boolean;
|
|
11
|
+
id: UniqueIdentifier;
|
|
12
|
+
index: number;
|
|
13
|
+
items: UniqueIdentifier[];
|
|
14
|
+
previousItems: UniqueIdentifier[];
|
|
15
|
+
previousContainerId: UniqueIdentifier;
|
|
16
|
+
newIndex: number;
|
|
17
|
+
transition: SortableTransition | null;
|
|
18
|
+
wasDragging: boolean;
|
|
19
|
+
}) => boolean;
|
|
20
|
+
|
|
21
|
+
export interface NewIndexGetterArguments {
|
|
22
|
+
id: UniqueIdentifier;
|
|
23
|
+
items: UniqueIdentifier[];
|
|
24
|
+
activeIndex: number;
|
|
25
|
+
overIndex: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type NewIndexGetter = (args: NewIndexGetterArguments) => number;
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { useContext, useEffect, useMemo, useRef } from 'octane';
|
|
2
|
+
import {
|
|
3
|
+
useDraggable,
|
|
4
|
+
useDroppable,
|
|
5
|
+
UseDraggableArguments,
|
|
6
|
+
UseDroppableArguments,
|
|
7
|
+
} from '@octanejs/dnd-kit-core';
|
|
8
|
+
import type { Data } from '@octanejs/dnd-kit-core';
|
|
9
|
+
import { CSS, isKeyboardEvent, useCombinedRefs } from '@octanejs/dnd-kit-utilities';
|
|
10
|
+
|
|
11
|
+
import { Context } from '../components';
|
|
12
|
+
import type { Disabled, SortableData, SortingStrategy } from '../types';
|
|
13
|
+
import { isValidIndex } from '../utilities';
|
|
14
|
+
import {
|
|
15
|
+
defaultAnimateLayoutChanges,
|
|
16
|
+
defaultAttributes,
|
|
17
|
+
defaultNewIndexGetter,
|
|
18
|
+
defaultTransition,
|
|
19
|
+
disabledTransition,
|
|
20
|
+
transitionProperty,
|
|
21
|
+
} from './defaults';
|
|
22
|
+
import type { AnimateLayoutChanges, NewIndexGetter, SortableTransition } from './types';
|
|
23
|
+
import { useDerivedTransform } from './utilities';
|
|
24
|
+
|
|
25
|
+
export interface Arguments
|
|
26
|
+
extends
|
|
27
|
+
Omit<UseDraggableArguments, 'disabled'>,
|
|
28
|
+
Pick<UseDroppableArguments, 'resizeObserverConfig'> {
|
|
29
|
+
animateLayoutChanges?: AnimateLayoutChanges;
|
|
30
|
+
disabled?: boolean | Disabled;
|
|
31
|
+
getNewIndex?: NewIndexGetter;
|
|
32
|
+
strategy?: SortingStrategy;
|
|
33
|
+
transition?: SortableTransition | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function useSortable({
|
|
37
|
+
animateLayoutChanges = defaultAnimateLayoutChanges,
|
|
38
|
+
attributes: userDefinedAttributes,
|
|
39
|
+
disabled: localDisabled,
|
|
40
|
+
data: customData,
|
|
41
|
+
getNewIndex = defaultNewIndexGetter,
|
|
42
|
+
id,
|
|
43
|
+
strategy: localStrategy,
|
|
44
|
+
resizeObserverConfig,
|
|
45
|
+
transition = defaultTransition,
|
|
46
|
+
}: Arguments) {
|
|
47
|
+
const {
|
|
48
|
+
items,
|
|
49
|
+
containerId,
|
|
50
|
+
activeIndex,
|
|
51
|
+
disabled: globalDisabled,
|
|
52
|
+
disableTransforms,
|
|
53
|
+
sortedRects,
|
|
54
|
+
overIndex,
|
|
55
|
+
useDragOverlay,
|
|
56
|
+
strategy: globalStrategy,
|
|
57
|
+
} = useContext(Context);
|
|
58
|
+
const disabled: Disabled = normalizeLocalDisabled(localDisabled, globalDisabled);
|
|
59
|
+
const index = items.indexOf(id);
|
|
60
|
+
const data = useMemo<SortableData & Data>(
|
|
61
|
+
() => ({ sortable: { containerId, index, items }, ...customData }),
|
|
62
|
+
[containerId, customData, index, items],
|
|
63
|
+
);
|
|
64
|
+
const itemsAfterCurrentSortable = useMemo(() => items.slice(items.indexOf(id)), [items, id]);
|
|
65
|
+
const {
|
|
66
|
+
rect,
|
|
67
|
+
node,
|
|
68
|
+
isOver,
|
|
69
|
+
setNodeRef: setDroppableNodeRef,
|
|
70
|
+
} = useDroppable({
|
|
71
|
+
id,
|
|
72
|
+
data,
|
|
73
|
+
disabled: disabled.droppable,
|
|
74
|
+
resizeObserverConfig: {
|
|
75
|
+
updateMeasurementsFor: itemsAfterCurrentSortable,
|
|
76
|
+
...resizeObserverConfig,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const {
|
|
80
|
+
active,
|
|
81
|
+
activatorEvent,
|
|
82
|
+
activeNodeRect,
|
|
83
|
+
attributes,
|
|
84
|
+
setNodeRef: setDraggableNodeRef,
|
|
85
|
+
listeners,
|
|
86
|
+
isDragging,
|
|
87
|
+
over,
|
|
88
|
+
setActivatorNodeRef,
|
|
89
|
+
transform,
|
|
90
|
+
} = useDraggable({
|
|
91
|
+
id,
|
|
92
|
+
data,
|
|
93
|
+
attributes: {
|
|
94
|
+
...defaultAttributes,
|
|
95
|
+
...userDefinedAttributes,
|
|
96
|
+
},
|
|
97
|
+
disabled: disabled.draggable,
|
|
98
|
+
});
|
|
99
|
+
const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
|
|
100
|
+
const isSorting = Boolean(active);
|
|
101
|
+
const displaceItem =
|
|
102
|
+
isSorting && !disableTransforms && isValidIndex(activeIndex) && isValidIndex(overIndex);
|
|
103
|
+
const shouldDisplaceDragSource = !useDragOverlay && isDragging;
|
|
104
|
+
const dragSourceDisplacement = shouldDisplaceDragSource && displaceItem ? transform : null;
|
|
105
|
+
const strategy = localStrategy ?? globalStrategy;
|
|
106
|
+
const finalTransform = displaceItem
|
|
107
|
+
? (dragSourceDisplacement ??
|
|
108
|
+
strategy({
|
|
109
|
+
rects: sortedRects,
|
|
110
|
+
activeNodeRect,
|
|
111
|
+
activeIndex,
|
|
112
|
+
overIndex,
|
|
113
|
+
index,
|
|
114
|
+
}))
|
|
115
|
+
: null;
|
|
116
|
+
const newIndex =
|
|
117
|
+
isValidIndex(activeIndex) && isValidIndex(overIndex)
|
|
118
|
+
? getNewIndex({ id, items, activeIndex, overIndex })
|
|
119
|
+
: index;
|
|
120
|
+
const activeId = active?.id;
|
|
121
|
+
const previous = useRef({
|
|
122
|
+
activeId,
|
|
123
|
+
items,
|
|
124
|
+
newIndex,
|
|
125
|
+
containerId,
|
|
126
|
+
});
|
|
127
|
+
const itemsHaveChanged = items !== previous.current.items;
|
|
128
|
+
const shouldAnimateLayoutChanges = animateLayoutChanges({
|
|
129
|
+
active,
|
|
130
|
+
containerId,
|
|
131
|
+
isDragging,
|
|
132
|
+
isSorting,
|
|
133
|
+
id,
|
|
134
|
+
index,
|
|
135
|
+
items,
|
|
136
|
+
newIndex: previous.current.newIndex,
|
|
137
|
+
previousItems: previous.current.items,
|
|
138
|
+
previousContainerId: previous.current.containerId,
|
|
139
|
+
transition,
|
|
140
|
+
wasDragging: previous.current.activeId != null,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const derivedTransform = useDerivedTransform({
|
|
144
|
+
disabled: !shouldAnimateLayoutChanges,
|
|
145
|
+
index,
|
|
146
|
+
node,
|
|
147
|
+
rect,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
if (isSorting && previous.current.newIndex !== newIndex) {
|
|
152
|
+
previous.current.newIndex = newIndex;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (containerId !== previous.current.containerId) {
|
|
156
|
+
previous.current.containerId = containerId;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (items !== previous.current.items) {
|
|
160
|
+
previous.current.items = items;
|
|
161
|
+
}
|
|
162
|
+
}, [isSorting, newIndex, containerId, items]);
|
|
163
|
+
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (activeId === previous.current.activeId) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (activeId != null && previous.current.activeId == null) {
|
|
170
|
+
previous.current.activeId = activeId;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const timeoutId = setTimeout(() => {
|
|
175
|
+
previous.current.activeId = activeId;
|
|
176
|
+
}, 50);
|
|
177
|
+
|
|
178
|
+
return () => clearTimeout(timeoutId);
|
|
179
|
+
}, [activeId]);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
active,
|
|
183
|
+
activeIndex,
|
|
184
|
+
attributes,
|
|
185
|
+
data,
|
|
186
|
+
rect,
|
|
187
|
+
index,
|
|
188
|
+
newIndex,
|
|
189
|
+
items,
|
|
190
|
+
isOver,
|
|
191
|
+
isSorting,
|
|
192
|
+
isDragging,
|
|
193
|
+
listeners,
|
|
194
|
+
node,
|
|
195
|
+
overIndex,
|
|
196
|
+
over,
|
|
197
|
+
setNodeRef,
|
|
198
|
+
setActivatorNodeRef,
|
|
199
|
+
setDroppableNodeRef,
|
|
200
|
+
setDraggableNodeRef,
|
|
201
|
+
transform: derivedTransform ?? finalTransform,
|
|
202
|
+
transition: getTransition(),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
function getTransition() {
|
|
206
|
+
if (
|
|
207
|
+
// Temporarily disable transitions for a single frame to set up derived transforms
|
|
208
|
+
derivedTransform ||
|
|
209
|
+
// Or to prevent items jumping to back to their "new" position when items change
|
|
210
|
+
(itemsHaveChanged && previous.current.newIndex === index)
|
|
211
|
+
) {
|
|
212
|
+
return disabledTransition;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if ((shouldDisplaceDragSource && !isKeyboardEvent(activatorEvent)) || !transition) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (isSorting || shouldAnimateLayoutChanges) {
|
|
220
|
+
return CSS.Transition.toString({
|
|
221
|
+
...transition,
|
|
222
|
+
property: transitionProperty,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function normalizeLocalDisabled(localDisabled: Arguments['disabled'], globalDisabled: Disabled) {
|
|
231
|
+
if (typeof localDisabled === 'boolean') {
|
|
232
|
+
return {
|
|
233
|
+
draggable: localDisabled,
|
|
234
|
+
// Backwards compatibility
|
|
235
|
+
droppable: false,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
draggable: localDisabled?.draggable ?? globalDisabled.draggable,
|
|
241
|
+
droppable: localDisabled?.droppable ?? globalDisabled.droppable,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useDerivedTransform } from './useDerivedTransform';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { MutableRefObject } from 'octane';
|
|
2
|
+
import { useEffect, useRef, useState } from 'octane';
|
|
3
|
+
import { getClientRect, ClientRect } from '@octanejs/dnd-kit-core';
|
|
4
|
+
import { Transform, useIsomorphicLayoutEffect } from '@octanejs/dnd-kit-utilities';
|
|
5
|
+
|
|
6
|
+
interface Arguments {
|
|
7
|
+
rect: MutableRefObject<ClientRect | null>;
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
index: number;
|
|
10
|
+
node: MutableRefObject<HTMLElement | null>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* When the index of an item changes while sorting,
|
|
15
|
+
* we need to temporarily disable the transforms
|
|
16
|
+
*/
|
|
17
|
+
export function useDerivedTransform({ disabled, index, node, rect }: Arguments) {
|
|
18
|
+
const [derivedTransform, setDerivedtransform] = useState<Transform | null>(null);
|
|
19
|
+
const previousIndex = useRef(index);
|
|
20
|
+
|
|
21
|
+
useIsomorphicLayoutEffect(() => {
|
|
22
|
+
if (!disabled && index !== previousIndex.current && node.current) {
|
|
23
|
+
const initial = rect.current;
|
|
24
|
+
|
|
25
|
+
if (initial) {
|
|
26
|
+
const current = getClientRect(node.current, {
|
|
27
|
+
ignoreTransform: true,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const delta = {
|
|
31
|
+
x: initial.left - current.left,
|
|
32
|
+
y: initial.top - current.top,
|
|
33
|
+
scaleX: initial.width / current.width,
|
|
34
|
+
scaleY: initial.height / current.height,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (delta.x || delta.y) {
|
|
38
|
+
setDerivedtransform(delta);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (index !== previousIndex.current) {
|
|
44
|
+
previousIndex.current = index;
|
|
45
|
+
}
|
|
46
|
+
}, [disabled, index, node, rect]);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (derivedTransform) {
|
|
50
|
+
setDerivedtransform(null);
|
|
51
|
+
}
|
|
52
|
+
}, [derivedTransform]);
|
|
53
|
+
|
|
54
|
+
return derivedTransform;
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { SortableContext } from './components';
|
|
2
|
+
export type { SortableContextProps } from './components';
|
|
3
|
+
export { useSortable, defaultAnimateLayoutChanges, defaultNewIndexGetter } from './hooks';
|
|
4
|
+
export type { UseSortableArguments, AnimateLayoutChanges, NewIndexGetter } from './hooks';
|
|
5
|
+
export {
|
|
6
|
+
horizontalListSortingStrategy,
|
|
7
|
+
rectSortingStrategy,
|
|
8
|
+
rectSwappingStrategy,
|
|
9
|
+
verticalListSortingStrategy,
|
|
10
|
+
} from './strategies';
|
|
11
|
+
export { sortableKeyboardCoordinates } from './sensors';
|
|
12
|
+
export { arrayMove, arraySwap } from './utilities';
|
|
13
|
+
export { hasSortableData } from './types';
|
|
14
|
+
export type { SortableData, SortingStrategy } from './types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './keyboard';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sortableKeyboardCoordinates } from './sortableKeyboardCoordinates';
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {
|
|
2
|
+
closestCorners,
|
|
3
|
+
getScrollableAncestors,
|
|
4
|
+
getFirstCollision,
|
|
5
|
+
KeyboardCode,
|
|
6
|
+
DroppableContainer,
|
|
7
|
+
KeyboardCoordinateGetter,
|
|
8
|
+
} from '@octanejs/dnd-kit-core';
|
|
9
|
+
import { subtract } from '@octanejs/dnd-kit-utilities';
|
|
10
|
+
|
|
11
|
+
import { hasSortableData } from '../../types';
|
|
12
|
+
|
|
13
|
+
const directions: string[] = [
|
|
14
|
+
KeyboardCode.Down,
|
|
15
|
+
KeyboardCode.Right,
|
|
16
|
+
KeyboardCode.Up,
|
|
17
|
+
KeyboardCode.Left,
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export const sortableKeyboardCoordinates: KeyboardCoordinateGetter = (
|
|
21
|
+
event,
|
|
22
|
+
{
|
|
23
|
+
context: {
|
|
24
|
+
active,
|
|
25
|
+
collisionRect,
|
|
26
|
+
droppableRects,
|
|
27
|
+
droppableContainers,
|
|
28
|
+
over,
|
|
29
|
+
scrollableAncestors,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
) => {
|
|
33
|
+
if (directions.includes(event.code)) {
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
|
|
36
|
+
if (!active || !collisionRect) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const filteredContainers: DroppableContainer[] = [];
|
|
41
|
+
|
|
42
|
+
droppableContainers.getEnabled().forEach((entry) => {
|
|
43
|
+
if (!entry || entry?.disabled) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const rect = droppableRects.get(entry.id);
|
|
48
|
+
|
|
49
|
+
if (!rect) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
switch (event.code) {
|
|
54
|
+
case KeyboardCode.Down:
|
|
55
|
+
if (collisionRect.top < rect.top) {
|
|
56
|
+
filteredContainers.push(entry);
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case KeyboardCode.Up:
|
|
60
|
+
if (collisionRect.top > rect.top) {
|
|
61
|
+
filteredContainers.push(entry);
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case KeyboardCode.Left:
|
|
65
|
+
if (collisionRect.left > rect.left) {
|
|
66
|
+
filteredContainers.push(entry);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case KeyboardCode.Right:
|
|
70
|
+
if (collisionRect.left < rect.left) {
|
|
71
|
+
filteredContainers.push(entry);
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const collisions = closestCorners({
|
|
78
|
+
active,
|
|
79
|
+
collisionRect: collisionRect,
|
|
80
|
+
droppableRects,
|
|
81
|
+
droppableContainers: filteredContainers,
|
|
82
|
+
pointerCoordinates: null,
|
|
83
|
+
});
|
|
84
|
+
let closestId = getFirstCollision(collisions, 'id');
|
|
85
|
+
|
|
86
|
+
if (closestId === over?.id && collisions.length > 1) {
|
|
87
|
+
closestId = collisions[1].id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (closestId != null) {
|
|
91
|
+
const activeDroppable = droppableContainers.get(active.id);
|
|
92
|
+
const newDroppable = droppableContainers.get(closestId);
|
|
93
|
+
const newRect = newDroppable ? droppableRects.get(newDroppable.id) : null;
|
|
94
|
+
const newNode = newDroppable?.node.current;
|
|
95
|
+
|
|
96
|
+
if (newNode && newRect && activeDroppable && newDroppable) {
|
|
97
|
+
const newScrollAncestors = getScrollableAncestors(newNode);
|
|
98
|
+
const hasDifferentScrollAncestors = newScrollAncestors.some(
|
|
99
|
+
(element, index) => scrollableAncestors[index] !== element,
|
|
100
|
+
);
|
|
101
|
+
const hasSameContainer = isSameContainer(activeDroppable, newDroppable);
|
|
102
|
+
const isAfterActive = isAfter(activeDroppable, newDroppable);
|
|
103
|
+
const offset =
|
|
104
|
+
hasDifferentScrollAncestors || !hasSameContainer
|
|
105
|
+
? {
|
|
106
|
+
x: 0,
|
|
107
|
+
y: 0,
|
|
108
|
+
}
|
|
109
|
+
: {
|
|
110
|
+
x: isAfterActive ? collisionRect.width - newRect.width : 0,
|
|
111
|
+
y: isAfterActive ? collisionRect.height - newRect.height : 0,
|
|
112
|
+
};
|
|
113
|
+
const rectCoordinates = {
|
|
114
|
+
x: newRect.left,
|
|
115
|
+
y: newRect.top,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const newCoordinates =
|
|
119
|
+
offset.x && offset.y ? rectCoordinates : subtract(rectCoordinates, offset);
|
|
120
|
+
|
|
121
|
+
return newCoordinates;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return undefined;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
function isSameContainer(a: DroppableContainer, b: DroppableContainer) {
|
|
130
|
+
if (!hasSortableData(a) || !hasSortableData(b)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return a.data.current.sortable.containerId === b.data.current.sortable.containerId;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isAfter(a: DroppableContainer, b: DroppableContainer) {
|
|
138
|
+
if (!hasSortableData(a) || !hasSortableData(b)) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!isSameContainer(a, b)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return a.data.current.sortable.index < b.data.current.sortable.index;
|
|
147
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ClientRect } from '@octanejs/dnd-kit-core';
|
|
2
|
+
import type { SortingStrategy } from '../types';
|
|
3
|
+
|
|
4
|
+
// To-do: We should be calculating scale transformation
|
|
5
|
+
const defaultScale = {
|
|
6
|
+
scaleX: 1,
|
|
7
|
+
scaleY: 1,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const horizontalListSortingStrategy: SortingStrategy = ({
|
|
11
|
+
rects,
|
|
12
|
+
activeNodeRect: fallbackActiveRect,
|
|
13
|
+
activeIndex,
|
|
14
|
+
overIndex,
|
|
15
|
+
index,
|
|
16
|
+
}) => {
|
|
17
|
+
const activeNodeRect = rects[activeIndex] ?? fallbackActiveRect;
|
|
18
|
+
|
|
19
|
+
if (!activeNodeRect) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const itemGap = getItemGap(rects, index, activeIndex);
|
|
24
|
+
|
|
25
|
+
if (index === activeIndex) {
|
|
26
|
+
const newIndexRect = rects[overIndex];
|
|
27
|
+
|
|
28
|
+
if (!newIndexRect) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
x:
|
|
34
|
+
activeIndex < overIndex
|
|
35
|
+
? newIndexRect.left + newIndexRect.width - (activeNodeRect.left + activeNodeRect.width)
|
|
36
|
+
: newIndexRect.left - activeNodeRect.left,
|
|
37
|
+
y: 0,
|
|
38
|
+
...defaultScale,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (index > activeIndex && index <= overIndex) {
|
|
43
|
+
return {
|
|
44
|
+
x: -activeNodeRect.width - itemGap,
|
|
45
|
+
y: 0,
|
|
46
|
+
...defaultScale,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (index < activeIndex && index >= overIndex) {
|
|
51
|
+
return {
|
|
52
|
+
x: activeNodeRect.width + itemGap,
|
|
53
|
+
y: 0,
|
|
54
|
+
...defaultScale,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
x: 0,
|
|
60
|
+
y: 0,
|
|
61
|
+
...defaultScale,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function getItemGap(rects: ClientRect[], index: number, activeIndex: number) {
|
|
66
|
+
const currentRect: ClientRect | undefined = rects[index];
|
|
67
|
+
const previousRect: ClientRect | undefined = rects[index - 1];
|
|
68
|
+
const nextRect: ClientRect | undefined = rects[index + 1];
|
|
69
|
+
|
|
70
|
+
if (!currentRect || (!previousRect && !nextRect)) {
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (activeIndex < index) {
|
|
75
|
+
return previousRect
|
|
76
|
+
? currentRect.left - (previousRect.left + previousRect.width)
|
|
77
|
+
: nextRect.left - (currentRect.left + currentRect.width);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return nextRect
|
|
81
|
+
? nextRect.left - (currentRect.left + currentRect.width)
|
|
82
|
+
: currentRect.left - (previousRect.left + previousRect.width);
|
|
83
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { arrayMove } from '../utilities';
|
|
2
|
+
import type { SortingStrategy } from '../types';
|
|
3
|
+
|
|
4
|
+
export const rectSortingStrategy: SortingStrategy = ({ rects, activeIndex, overIndex, index }) => {
|
|
5
|
+
const newRects = arrayMove(rects, overIndex, activeIndex);
|
|
6
|
+
|
|
7
|
+
const oldRect = rects[index];
|
|
8
|
+
const newRect = newRects[index];
|
|
9
|
+
|
|
10
|
+
if (!newRect || !oldRect) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
x: newRect.left - oldRect.left,
|
|
16
|
+
y: newRect.top - oldRect.top,
|
|
17
|
+
scaleX: newRect.width / oldRect.width,
|
|
18
|
+
scaleY: newRect.height / oldRect.height,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { SortingStrategy } from '../types';
|
|
2
|
+
|
|
3
|
+
export const rectSwappingStrategy: SortingStrategy = ({ activeIndex, index, rects, overIndex }) => {
|
|
4
|
+
let oldRect;
|
|
5
|
+
let newRect;
|
|
6
|
+
|
|
7
|
+
if (index === activeIndex) {
|
|
8
|
+
oldRect = rects[index];
|
|
9
|
+
newRect = rects[overIndex];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (index === overIndex) {
|
|
13
|
+
oldRect = rects[index];
|
|
14
|
+
newRect = rects[activeIndex];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!newRect || !oldRect) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
x: newRect.left - oldRect.left,
|
|
23
|
+
y: newRect.top - oldRect.top,
|
|
24
|
+
scaleX: newRect.width / oldRect.width,
|
|
25
|
+
scaleY: newRect.height / oldRect.height,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ClientRect } from '@octanejs/dnd-kit-core';
|
|
2
|
+
import type { SortingStrategy } from '../types';
|
|
3
|
+
|
|
4
|
+
// To-do: We should be calculating scale transformation
|
|
5
|
+
const defaultScale = {
|
|
6
|
+
scaleX: 1,
|
|
7
|
+
scaleY: 1,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const verticalListSortingStrategy: SortingStrategy = ({
|
|
11
|
+
activeIndex,
|
|
12
|
+
activeNodeRect: fallbackActiveRect,
|
|
13
|
+
index,
|
|
14
|
+
rects,
|
|
15
|
+
overIndex,
|
|
16
|
+
}) => {
|
|
17
|
+
const activeNodeRect = rects[activeIndex] ?? fallbackActiveRect;
|
|
18
|
+
|
|
19
|
+
if (!activeNodeRect) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (index === activeIndex) {
|
|
24
|
+
const overIndexRect = rects[overIndex];
|
|
25
|
+
|
|
26
|
+
if (!overIndexRect) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
x: 0,
|
|
32
|
+
y:
|
|
33
|
+
activeIndex < overIndex
|
|
34
|
+
? overIndexRect.top + overIndexRect.height - (activeNodeRect.top + activeNodeRect.height)
|
|
35
|
+
: overIndexRect.top - activeNodeRect.top,
|
|
36
|
+
...defaultScale,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const itemGap = getItemGap(rects, index, activeIndex);
|
|
41
|
+
|
|
42
|
+
if (index > activeIndex && index <= overIndex) {
|
|
43
|
+
return {
|
|
44
|
+
x: 0,
|
|
45
|
+
y: -activeNodeRect.height - itemGap,
|
|
46
|
+
...defaultScale,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (index < activeIndex && index >= overIndex) {
|
|
51
|
+
return {
|
|
52
|
+
x: 0,
|
|
53
|
+
y: activeNodeRect.height + itemGap,
|
|
54
|
+
...defaultScale,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
x: 0,
|
|
60
|
+
y: 0,
|
|
61
|
+
...defaultScale,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function getItemGap(clientRects: ClientRect[], index: number, activeIndex: number) {
|
|
66
|
+
const currentRect: ClientRect | undefined = clientRects[index];
|
|
67
|
+
const previousRect: ClientRect | undefined = clientRects[index - 1];
|
|
68
|
+
const nextRect: ClientRect | undefined = clientRects[index + 1];
|
|
69
|
+
|
|
70
|
+
if (!currentRect) {
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (activeIndex < index) {
|
|
75
|
+
return previousRect
|
|
76
|
+
? currentRect.top - (previousRect.top + previousRect.height)
|
|
77
|
+
: nextRect
|
|
78
|
+
? nextRect.top - (currentRect.top + currentRect.height)
|
|
79
|
+
: 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return nextRect
|
|
83
|
+
? nextRect.top - (currentRect.top + currentRect.height)
|
|
84
|
+
: previousRect
|
|
85
|
+
? currentRect.top - (previousRect.top + previousRect.height)
|
|
86
|
+
: 0;
|
|
87
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ClientRect } from '@octanejs/dnd-kit-core';
|
|
2
|
+
import type { Transform } from '@octanejs/dnd-kit-utilities';
|
|
3
|
+
|
|
4
|
+
export type SortingStrategy = (args: {
|
|
5
|
+
activeNodeRect: ClientRect | null;
|
|
6
|
+
activeIndex: number;
|
|
7
|
+
index: number;
|
|
8
|
+
rects: ClientRect[];
|
|
9
|
+
overIndex: number;
|
|
10
|
+
}) => Transform | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Active, Data, DroppableContainer, DraggableNode, Over } from '@octanejs/dnd-kit-core';
|
|
2
|
+
|
|
3
|
+
import type { SortableData } from './data';
|
|
4
|
+
|
|
5
|
+
export function hasSortableData<T extends Active | Over | DraggableNode | DroppableContainer>(
|
|
6
|
+
entry: T | null | undefined,
|
|
7
|
+
): entry is T & { data: { current: Data<SortableData> } } {
|
|
8
|
+
if (!entry) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const data = entry.data.current;
|
|
13
|
+
|
|
14
|
+
if (
|
|
15
|
+
data &&
|
|
16
|
+
'sortable' in data &&
|
|
17
|
+
typeof data.sortable === 'object' &&
|
|
18
|
+
'containerId' in data.sortable &&
|
|
19
|
+
'items' in data.sortable &&
|
|
20
|
+
'index' in data.sortable
|
|
21
|
+
) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Move an array item to a different position. Returns a new array with the item moved to the new position.
|
|
3
|
+
*/
|
|
4
|
+
export function arrayMove<T>(array: T[], from: number, to: number): T[] {
|
|
5
|
+
const newArray = array.slice();
|
|
6
|
+
newArray.splice(to < 0 ? newArray.length + to : to, 0, newArray.splice(from, 1)[0]);
|
|
7
|
+
|
|
8
|
+
return newArray;
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swap an array item to a different position. Returns a new array with the item swapped to the new position.
|
|
3
|
+
*/
|
|
4
|
+
export function arraySwap<T>(array: T[], from: number, to: number): T[] {
|
|
5
|
+
const newArray = array.slice();
|
|
6
|
+
|
|
7
|
+
newArray[from] = array[to];
|
|
8
|
+
newArray[to] = array[from];
|
|
9
|
+
|
|
10
|
+
return newArray;
|
|
11
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientRect,
|
|
3
|
+
UniqueIdentifier,
|
|
4
|
+
UseDndContextReturnValue,
|
|
5
|
+
} from '@octanejs/dnd-kit-core';
|
|
6
|
+
|
|
7
|
+
export function getSortedRects(
|
|
8
|
+
items: UniqueIdentifier[],
|
|
9
|
+
rects: UseDndContextReturnValue['droppableRects'],
|
|
10
|
+
) {
|
|
11
|
+
return items.reduce<ClientRect[]>((accumulator, id, index) => {
|
|
12
|
+
const rect = rects.get(id);
|
|
13
|
+
|
|
14
|
+
if (rect) {
|
|
15
|
+
accumulator[index] = rect;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return accumulator;
|
|
19
|
+
}, Array(items.length));
|
|
20
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { arrayMove } from './arrayMove';
|
|
2
|
+
export { arraySwap } from './arraySwap';
|
|
3
|
+
export { getSortedRects } from './getSortedRects';
|
|
4
|
+
export { isValidIndex } from './isValidIndex';
|
|
5
|
+
export { itemsEqual } from './itemsEqual';
|
|
6
|
+
export { normalizeDisabled } from './normalizeDisabled';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { UniqueIdentifier } from '@octanejs/dnd-kit-core';
|
|
2
|
+
|
|
3
|
+
export function itemsEqual(a: UniqueIdentifier[], b: UniqueIdentifier[]) {
|
|
4
|
+
if (a === b) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (a.length !== b.length) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < a.length; i++) {
|
|
13
|
+
if (a[i] !== b[i]) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return true;
|
|
19
|
+
}
|