@dnd-kit/helpers 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/README.md +17 -0
- package/dist/index.cjs +155 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +128 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @dnd-kit/helpers
|
|
2
|
+
|
|
3
|
+
[](https://npm.im/@dnd-kit/helpers)
|
|
4
|
+
|
|
5
|
+
Helpers for working with @dnd-kit.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
To get started, install the `@dnd-kit/helpers` package via npm or yarn:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @dnd-kit/helpers
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Visit [docs.dndkit.com](https://docs.dndkit.com) to learn how to get started with @dnd-kit.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
|
|
37
|
+
// src/index.ts
|
|
38
|
+
var src_exports = {};
|
|
39
|
+
__export(src_exports, {
|
|
40
|
+
arrayMove: () => arrayMove,
|
|
41
|
+
arraySwap: () => arraySwap,
|
|
42
|
+
move: () => move,
|
|
43
|
+
swap: () => swap
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// src/move.ts
|
|
48
|
+
function arrayMove(array, from, to) {
|
|
49
|
+
if (from === to) {
|
|
50
|
+
return array;
|
|
51
|
+
}
|
|
52
|
+
const newArray = array.slice();
|
|
53
|
+
newArray.splice(to, 0, newArray.splice(from, 1)[0]);
|
|
54
|
+
return newArray;
|
|
55
|
+
}
|
|
56
|
+
function arraySwap(array, from, to) {
|
|
57
|
+
if (from === to) {
|
|
58
|
+
return array;
|
|
59
|
+
}
|
|
60
|
+
const newArray = array.slice();
|
|
61
|
+
const item = newArray[from];
|
|
62
|
+
newArray[from] = newArray[to];
|
|
63
|
+
newArray[to] = item;
|
|
64
|
+
return newArray;
|
|
65
|
+
}
|
|
66
|
+
function mutate(items, source, target, mutation) {
|
|
67
|
+
if (!source || !target) {
|
|
68
|
+
return items;
|
|
69
|
+
}
|
|
70
|
+
const findIndex = (item, id) => item === id || typeof item === "object" && "id" in item && item.id === id;
|
|
71
|
+
if (Array.isArray(items)) {
|
|
72
|
+
const sourceIndex2 = items.findIndex((item) => findIndex(item, source.id));
|
|
73
|
+
const targetIndex2 = items.findIndex((item) => findIndex(item, target.id));
|
|
74
|
+
if (sourceIndex2 === -1 || targetIndex2 === -1) {
|
|
75
|
+
return items;
|
|
76
|
+
}
|
|
77
|
+
const { dragOperation: dragOperation2 } = source.manager;
|
|
78
|
+
if (!dragOperation2.canceled && "index" in source && typeof source.index === "number") {
|
|
79
|
+
const projectedSourceIndex = source.index;
|
|
80
|
+
if (projectedSourceIndex !== sourceIndex2) {
|
|
81
|
+
return mutation(items, sourceIndex2, projectedSourceIndex);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return mutation(items, sourceIndex2, targetIndex2);
|
|
85
|
+
}
|
|
86
|
+
if (source.id === target.id) {
|
|
87
|
+
return items;
|
|
88
|
+
}
|
|
89
|
+
const entries = Object.entries(items);
|
|
90
|
+
let sourceIndex = -1;
|
|
91
|
+
let sourceParent;
|
|
92
|
+
let targetIndex = -1;
|
|
93
|
+
let targetParent;
|
|
94
|
+
for (const [id, children] of entries) {
|
|
95
|
+
if (sourceIndex === -1) {
|
|
96
|
+
sourceIndex = children.findIndex((item) => findIndex(item, source.id));
|
|
97
|
+
if (sourceIndex !== -1) {
|
|
98
|
+
sourceParent = id;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (targetIndex === -1) {
|
|
102
|
+
targetIndex = children.findIndex((item) => findIndex(item, target.id));
|
|
103
|
+
if (targetIndex !== -1) {
|
|
104
|
+
targetParent = id;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (sourceIndex !== -1 && targetIndex !== -1) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const { dragOperation } = source.manager;
|
|
112
|
+
const position = dragOperation.position.current;
|
|
113
|
+
if (targetParent == null) {
|
|
114
|
+
if (target.id in items) {
|
|
115
|
+
const insertionIndex = target.shape && position.y > target.shape.center.y ? items[target.id].length : 0;
|
|
116
|
+
targetParent = target.id;
|
|
117
|
+
targetIndex = insertionIndex;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (sourceParent == null || targetParent == null) {
|
|
121
|
+
return items;
|
|
122
|
+
}
|
|
123
|
+
if (sourceParent === targetParent) {
|
|
124
|
+
return __spreadProps(__spreadValues({}, items), {
|
|
125
|
+
[sourceParent]: mutation(items[sourceParent], sourceIndex, targetIndex)
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const isBelowTarget = target.shape && position.y > target.shape.boundingRectangle.bottom;
|
|
129
|
+
const modifier = isBelowTarget ? 1 : 0;
|
|
130
|
+
const sourceItem = items[sourceParent][sourceIndex];
|
|
131
|
+
return __spreadProps(__spreadValues({}, items), {
|
|
132
|
+
[sourceParent]: [
|
|
133
|
+
...items[sourceParent].slice(0, sourceIndex),
|
|
134
|
+
...items[sourceParent].slice(sourceIndex + 1)
|
|
135
|
+
],
|
|
136
|
+
[targetParent]: [
|
|
137
|
+
...items[targetParent].slice(0, targetIndex + modifier),
|
|
138
|
+
sourceItem,
|
|
139
|
+
...items[targetParent].slice(targetIndex + modifier)
|
|
140
|
+
]
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function move(items, source, target) {
|
|
144
|
+
return mutate(items, source, target, arrayMove);
|
|
145
|
+
}
|
|
146
|
+
function swap(items, source, target) {
|
|
147
|
+
return mutate(items, source, target, arraySwap);
|
|
148
|
+
}
|
|
149
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
150
|
+
0 && (module.exports = {
|
|
151
|
+
arrayMove,
|
|
152
|
+
arraySwap,
|
|
153
|
+
move,
|
|
154
|
+
swap
|
|
155
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { UniqueIdentifier, Draggable, Droppable } from '@dnd-kit/abstract';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Move an array item to a different position. Returns a new array with the item moved to the new position.
|
|
5
|
+
*/
|
|
6
|
+
declare function arrayMove<T extends any[]>(array: T, from: number, to: number): T;
|
|
7
|
+
/**
|
|
8
|
+
* Move an array item to a different position. Returns a new array with the item moved to the new position.
|
|
9
|
+
*/
|
|
10
|
+
declare function arraySwap<T extends any[]>(array: T, from: number, to: number): T;
|
|
11
|
+
type Items = UniqueIdentifier[] | {
|
|
12
|
+
id: UniqueIdentifier;
|
|
13
|
+
}[];
|
|
14
|
+
declare function move<T extends Items | Record<UniqueIdentifier, Items>, U extends Draggable, V extends Droppable>(items: T, source: U | null, target: V | null): T;
|
|
15
|
+
declare function swap<T extends Items | Record<UniqueIdentifier, Items>, U extends Draggable, V extends Droppable>(items: T, source: U | null, target: V | null): T;
|
|
16
|
+
|
|
17
|
+
export { arrayMove, arraySwap, move, swap };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/move.ts
|
|
22
|
+
function arrayMove(array, from, to) {
|
|
23
|
+
if (from === to) {
|
|
24
|
+
return array;
|
|
25
|
+
}
|
|
26
|
+
const newArray = array.slice();
|
|
27
|
+
newArray.splice(to, 0, newArray.splice(from, 1)[0]);
|
|
28
|
+
return newArray;
|
|
29
|
+
}
|
|
30
|
+
function arraySwap(array, from, to) {
|
|
31
|
+
if (from === to) {
|
|
32
|
+
return array;
|
|
33
|
+
}
|
|
34
|
+
const newArray = array.slice();
|
|
35
|
+
const item = newArray[from];
|
|
36
|
+
newArray[from] = newArray[to];
|
|
37
|
+
newArray[to] = item;
|
|
38
|
+
return newArray;
|
|
39
|
+
}
|
|
40
|
+
function mutate(items, source, target, mutation) {
|
|
41
|
+
if (!source || !target) {
|
|
42
|
+
return items;
|
|
43
|
+
}
|
|
44
|
+
const findIndex = (item, id) => item === id || typeof item === "object" && "id" in item && item.id === id;
|
|
45
|
+
if (Array.isArray(items)) {
|
|
46
|
+
const sourceIndex2 = items.findIndex((item) => findIndex(item, source.id));
|
|
47
|
+
const targetIndex2 = items.findIndex((item) => findIndex(item, target.id));
|
|
48
|
+
if (sourceIndex2 === -1 || targetIndex2 === -1) {
|
|
49
|
+
return items;
|
|
50
|
+
}
|
|
51
|
+
const { dragOperation: dragOperation2 } = source.manager;
|
|
52
|
+
if (!dragOperation2.canceled && "index" in source && typeof source.index === "number") {
|
|
53
|
+
const projectedSourceIndex = source.index;
|
|
54
|
+
if (projectedSourceIndex !== sourceIndex2) {
|
|
55
|
+
return mutation(items, sourceIndex2, projectedSourceIndex);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return mutation(items, sourceIndex2, targetIndex2);
|
|
59
|
+
}
|
|
60
|
+
if (source.id === target.id) {
|
|
61
|
+
return items;
|
|
62
|
+
}
|
|
63
|
+
const entries = Object.entries(items);
|
|
64
|
+
let sourceIndex = -1;
|
|
65
|
+
let sourceParent;
|
|
66
|
+
let targetIndex = -1;
|
|
67
|
+
let targetParent;
|
|
68
|
+
for (const [id, children] of entries) {
|
|
69
|
+
if (sourceIndex === -1) {
|
|
70
|
+
sourceIndex = children.findIndex((item) => findIndex(item, source.id));
|
|
71
|
+
if (sourceIndex !== -1) {
|
|
72
|
+
sourceParent = id;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (targetIndex === -1) {
|
|
76
|
+
targetIndex = children.findIndex((item) => findIndex(item, target.id));
|
|
77
|
+
if (targetIndex !== -1) {
|
|
78
|
+
targetParent = id;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (sourceIndex !== -1 && targetIndex !== -1) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const { dragOperation } = source.manager;
|
|
86
|
+
const position = dragOperation.position.current;
|
|
87
|
+
if (targetParent == null) {
|
|
88
|
+
if (target.id in items) {
|
|
89
|
+
const insertionIndex = target.shape && position.y > target.shape.center.y ? items[target.id].length : 0;
|
|
90
|
+
targetParent = target.id;
|
|
91
|
+
targetIndex = insertionIndex;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (sourceParent == null || targetParent == null) {
|
|
95
|
+
return items;
|
|
96
|
+
}
|
|
97
|
+
if (sourceParent === targetParent) {
|
|
98
|
+
return __spreadProps(__spreadValues({}, items), {
|
|
99
|
+
[sourceParent]: mutation(items[sourceParent], sourceIndex, targetIndex)
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const isBelowTarget = target.shape && position.y > target.shape.boundingRectangle.bottom;
|
|
103
|
+
const modifier = isBelowTarget ? 1 : 0;
|
|
104
|
+
const sourceItem = items[sourceParent][sourceIndex];
|
|
105
|
+
return __spreadProps(__spreadValues({}, items), {
|
|
106
|
+
[sourceParent]: [
|
|
107
|
+
...items[sourceParent].slice(0, sourceIndex),
|
|
108
|
+
...items[sourceParent].slice(sourceIndex + 1)
|
|
109
|
+
],
|
|
110
|
+
[targetParent]: [
|
|
111
|
+
...items[targetParent].slice(0, targetIndex + modifier),
|
|
112
|
+
sourceItem,
|
|
113
|
+
...items[targetParent].slice(targetIndex + modifier)
|
|
114
|
+
]
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function move(items, source, target) {
|
|
118
|
+
return mutate(items, source, target, arrayMove);
|
|
119
|
+
}
|
|
120
|
+
function swap(items, source, target) {
|
|
121
|
+
return mutate(items, source, target, arraySwap);
|
|
122
|
+
}
|
|
123
|
+
export {
|
|
124
|
+
arrayMove,
|
|
125
|
+
arraySwap,
|
|
126
|
+
move,
|
|
127
|
+
swap
|
|
128
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dnd-kit/helpers",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/**"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --external react",
|
|
22
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts --external react",
|
|
23
|
+
"lint": "TIMING=1 eslint src/**/*.ts* --fix",
|
|
24
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@dnd-kit/abstract": "*",
|
|
28
|
+
"tslib": "^2.6.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@dnd-kit/eslint-config": "*",
|
|
32
|
+
"eslint": "^8.38.0",
|
|
33
|
+
"tsup": "^6.7.0",
|
|
34
|
+
"typescript": "^5.0.4"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|