vue-dnd-sortable 0.1.5 → 0.1.7
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 +24 -16
- package/dist/components/context.vue.d.ts +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/sortable.d.ts +4 -2
- package/dist/vue-dnd.es.js +138 -109
- package/dist/vue-dnd.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -3,6 +3,22 @@
|
|
3
3
|
A minimal drag-and-drop sortable list component library for Vue 3.
|
4
4
|
**⚠️ Warning: This package is experimental and poorly tested. Use at your own risk.**
|
5
5
|
|
6
|
+
---
|
7
|
+
|
8
|
+
## 🚨 Disclaimer
|
9
|
+
|
10
|
+
This library is:
|
11
|
+
|
12
|
+
- 🧪 **Experimental**
|
13
|
+
- ❌ **Missing many essential features**
|
14
|
+
- ⚠️ **Poorly tested**
|
15
|
+
- 📦 May be **archived** or abandoned at any time
|
16
|
+
|
17
|
+
It’s intended for personal use or experimentation — **not production-ready**.
|
18
|
+
**Use cautiously.**
|
19
|
+
|
20
|
+
---
|
21
|
+
|
6
22
|
## Features
|
7
23
|
|
8
24
|
- Lightweight and flexible
|
@@ -32,7 +48,8 @@ npm install vue-dnd-sortable
|
|
32
48
|
<script setup lang="ts">
|
33
49
|
import { ref } from 'vue'
|
34
50
|
import {
|
35
|
-
arrayMove,
|
51
|
+
arrayMove,
|
52
|
+
horizontalListSortingStrategy,
|
36
53
|
SortableContext,
|
37
54
|
SortableItem,
|
38
55
|
SortableHandle
|
@@ -42,9 +59,13 @@ const items = ref([1, 2, 3, 4, 5, 6])
|
|
42
59
|
</script>
|
43
60
|
|
44
61
|
<template>
|
45
|
-
<SortableContext
|
62
|
+
<SortableContext
|
63
|
+
axis="x"
|
64
|
+
:items
|
65
|
+
:strategy="horizontalListSortingStrategy"
|
66
|
+
@end="items = arrayMove(items, $event)">
|
46
67
|
<div
|
47
|
-
style="display: flex;
|
68
|
+
style="display: flex; gap: 16px; width: 400px; margin: 0 auto;"
|
48
69
|
>
|
49
70
|
<SortableItem
|
50
71
|
v-for="(item, index) in items"
|
@@ -54,7 +75,6 @@ const items = ref([1, 2, 3, 4, 5, 6])
|
|
54
75
|
<SortableHandle>
|
55
76
|
handle
|
56
77
|
</SortableHandle>
|
57
|
-
|
58
78
|
{{ item }}
|
59
79
|
</SortableItem>
|
60
80
|
</div>
|
@@ -64,18 +84,6 @@ const items = ref([1, 2, 3, 4, 5, 6])
|
|
64
84
|
|
65
85
|
---
|
66
86
|
|
67
|
-
## Disclaimer
|
68
|
-
|
69
|
-
This library is still in an **experimental** state.
|
70
|
-
|
71
|
-
* ❌ No comprehensive test coverage
|
72
|
-
* ❌ May contain unexpected bugs or breaking changes
|
73
|
-
* ❌ API is unstable and subject to change
|
74
|
-
|
75
|
-
Please test thoroughly before using in production.
|
76
|
-
|
77
|
-
---
|
78
|
-
|
79
87
|
## License
|
80
88
|
|
81
89
|
MIT
|
@@ -1,10 +1,12 @@
|
|
1
1
|
import type { CollisionDetector } from '../collision';
|
2
2
|
import type { DndEndEvent } from '../context';
|
3
|
+
import type { AxisSortingStrategy } from '../sortable';
|
3
4
|
import type { UniqueIdentifier } from '../types';
|
4
5
|
interface Props {
|
5
6
|
items: UniqueIdentifier[];
|
7
|
+
strategy?: AxisSortingStrategy;
|
6
8
|
collision?: CollisionDetector;
|
7
|
-
|
9
|
+
lockAxis?: 'x' | 'y';
|
8
10
|
}
|
9
11
|
declare var __VLS_1: {};
|
10
12
|
type __VLS_Slots = {} & {
|
@@ -15,6 +17,7 @@ declare const __VLS_component: import("vue").DefineComponent<Props, {}, {}, {},
|
|
15
17
|
}, string, import("vue").PublicProps, Readonly<Props> & Readonly<{
|
16
18
|
onEnd?: ((event: DndEndEvent) => any) | undefined;
|
17
19
|
}>, {
|
20
|
+
strategy: AxisSortingStrategy;
|
18
21
|
collision: CollisionDetector;
|
19
22
|
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
20
23
|
declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
|
package/dist/index.d.ts
CHANGED
@@ -2,4 +2,5 @@ export * from './collision';
|
|
2
2
|
export * from './components';
|
3
3
|
export * from './composables';
|
4
4
|
export * from './helpers';
|
5
|
+
export { type AxisSortingStrategy, horizontalListSortingStrategy, verticalListSortingStrategy } from './sortable';
|
5
6
|
export type { MaybeElement } from './utilities';
|
package/dist/sortable.d.ts
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
import type { Coords } from './types';
|
2
|
-
export
|
2
|
+
export type AxisSortingStrategy = (args: {
|
3
3
|
rects: DOMRect[];
|
4
4
|
activeIndex: number;
|
5
5
|
overIndex: number;
|
6
6
|
index: number;
|
7
|
-
})
|
7
|
+
}) => Coords | null;
|
8
|
+
export declare const verticalListSortingStrategy: AxisSortingStrategy;
|
9
|
+
export declare const horizontalListSortingStrategy: AxisSortingStrategy;
|
package/dist/vue-dnd.es.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { inject as
|
1
|
+
import { inject as z, provide as H, toValue as B, defineComponent as _, ref as x, computed as y, watch as C, renderSlot as I, onMounted as F, onUnmounted as T, watchEffect as N, createBlock as A, openBlock as E, resolveDynamicComponent as $, unref as V, withCtx as D, useTemplateRef as X } from "vue";
|
2
2
|
function Y(o, e) {
|
3
3
|
return e.value - o.value;
|
4
4
|
}
|
@@ -7,25 +7,25 @@ function q(o, e) {
|
|
7
7
|
}
|
8
8
|
const se = ({ rect: o, rects: e }) => {
|
9
9
|
function s(t, n) {
|
10
|
-
const l = Math.max(t.x, n.x),
|
11
|
-
if (l <
|
12
|
-
const
|
10
|
+
const l = Math.max(t.x, n.x), i = Math.min(t.x + t.width, n.x + n.width), a = Math.max(t.y, n.y), c = Math.min(t.y + t.height, n.y + n.height), v = i - l, f = c - a;
|
11
|
+
if (l < i && a < c) {
|
12
|
+
const p = v * f, g = t.width * t.height, S = n.width * n.height, b = p / (g + S - p);
|
13
13
|
return Number(b.toFixed(4));
|
14
14
|
}
|
15
15
|
return 0;
|
16
16
|
}
|
17
|
-
const
|
17
|
+
const r = [];
|
18
18
|
for (const [t, n] of e)
|
19
|
-
|
19
|
+
r.push({
|
20
20
|
id: t,
|
21
21
|
value: s(o, n)
|
22
22
|
});
|
23
|
-
return
|
23
|
+
return r.sort(Y);
|
24
24
|
}, G = ({ rect: o, rects: e }) => {
|
25
25
|
function s(n, l) {
|
26
26
|
return Math.sqrt((n.x - l.x) ** 2 + (n.y - l.y) ** 2);
|
27
27
|
}
|
28
|
-
function
|
28
|
+
function r(n) {
|
29
29
|
return {
|
30
30
|
x: n.x + n.width / 2,
|
31
31
|
y: n.y + n.height / 2
|
@@ -35,11 +35,11 @@ const se = ({ rect: o, rects: e }) => {
|
|
35
35
|
for (const [n, l] of e)
|
36
36
|
t.push({
|
37
37
|
id: n,
|
38
|
-
value: s(
|
38
|
+
value: s(r(l), r(o))
|
39
39
|
});
|
40
40
|
return t.sort(q);
|
41
41
|
};
|
42
|
-
class
|
42
|
+
class L {
|
43
43
|
listeners = [];
|
44
44
|
element;
|
45
45
|
constructor(e) {
|
@@ -62,7 +62,7 @@ class K {
|
|
62
62
|
windowListeners;
|
63
63
|
constructor(e) {
|
64
64
|
const { element: s } = e;
|
65
|
-
this.options = e, this.document = s.ownerDocument, this.listeners = new
|
65
|
+
this.options = e, this.document = s.ownerDocument, this.listeners = new L(s), this.documentListeners = new L(this.document), this.windowListeners = new L(this.document?.defaultView ?? window), this.handleStart = this.handleStart.bind(this), this.handleMove = this.handleMove.bind(this), this.handleEnd = this.handleEnd.bind(this), this.deactivate();
|
66
66
|
}
|
67
67
|
deactivate() {
|
68
68
|
this.listeners.removeAll(), this.documentListeners.removeAll(), this.windowListeners.removeAll(), this.listeners.add("pointerdown", this.handleStart);
|
@@ -94,199 +94,226 @@ function j(o) {
|
|
94
94
|
`${o}Context`
|
95
95
|
);
|
96
96
|
function s(t) {
|
97
|
-
const n =
|
97
|
+
const n = z(e, t);
|
98
98
|
if (n || n === null)
|
99
99
|
return n;
|
100
100
|
throw new Error(
|
101
101
|
`Injection \`${e.toString()}\` not found. Component must be used within \`${o}\``
|
102
102
|
);
|
103
103
|
}
|
104
|
-
function
|
105
|
-
return
|
104
|
+
function r(t) {
|
105
|
+
return H(e, t), t;
|
106
106
|
}
|
107
|
-
return [s,
|
107
|
+
return [s, r];
|
108
108
|
}
|
109
|
-
function
|
110
|
-
return o.reduce((s,
|
111
|
-
const t = e.get(
|
109
|
+
function U(o, e) {
|
110
|
+
return o.reduce((s, r) => {
|
111
|
+
const t = e.get(r);
|
112
112
|
return t && s.push(t), s;
|
113
113
|
}, []);
|
114
114
|
}
|
115
115
|
function w(o) {
|
116
|
-
const e =
|
116
|
+
const e = B(o);
|
117
117
|
return e?.$el ?? e;
|
118
118
|
}
|
119
|
-
function
|
119
|
+
function M(o) {
|
120
120
|
return o !== null && o >= 0;
|
121
121
|
}
|
122
|
-
const [
|
123
|
-
function P(o) {
|
122
|
+
const [J, P] = j("DndContext"), Q = (o) => {
|
124
123
|
const {
|
125
124
|
rects: e,
|
126
125
|
activeIndex: s,
|
127
|
-
overIndex:
|
126
|
+
overIndex: r,
|
128
127
|
index: t
|
129
128
|
} = o, n = e[s];
|
130
129
|
if (!n)
|
131
130
|
return null;
|
132
131
|
if (t === s) {
|
133
|
-
const
|
134
|
-
return
|
132
|
+
const i = e[r];
|
133
|
+
return i ? {
|
135
134
|
x: 0,
|
136
|
-
y: s >
|
135
|
+
y: s > r ? n.top - i.top : i.top - n.top
|
137
136
|
} : null;
|
138
137
|
}
|
139
|
-
const l =
|
140
|
-
return t > s && t <=
|
138
|
+
const l = O(e, t, s, "y");
|
139
|
+
return t > s && t <= r ? {
|
141
140
|
x: 0,
|
142
141
|
y: -n.height - l
|
143
|
-
} : t < s && t >=
|
142
|
+
} : t < s && t >= r ? {
|
144
143
|
x: 0,
|
145
144
|
y: n.height - l
|
146
145
|
} : {
|
147
146
|
x: 0,
|
148
147
|
y: 0
|
149
148
|
};
|
149
|
+
};
|
150
|
+
function O(o, e, s, r) {
|
151
|
+
const t = o[e], n = o[e - 1], l = o[e + 1], i = r === "x" ? "width" : "height", a = r === "x" ? "left" : "top";
|
152
|
+
return !t || !n && !l ? 0 : s < e ? n ? t[a] - (n[a] + n[i]) : l ? l[a] - (t[a] + t[i]) : 0 : l ? t[a] - (l[a] - l[i]) : n ? n[a] - (t[a] + t[i]) : 0;
|
150
153
|
}
|
151
|
-
|
152
|
-
const
|
153
|
-
|
154
|
-
|
155
|
-
|
154
|
+
const oe = (o) => {
|
155
|
+
const {
|
156
|
+
rects: e,
|
157
|
+
activeIndex: s,
|
158
|
+
overIndex: r,
|
159
|
+
index: t
|
160
|
+
} = o, n = e[s];
|
161
|
+
if (!n)
|
162
|
+
return null;
|
163
|
+
if (t === s) {
|
164
|
+
const i = e[r];
|
165
|
+
return i ? {
|
166
|
+
x: s > r ? n.left - i.left : i.left - n.left,
|
167
|
+
y: 0
|
168
|
+
} : null;
|
169
|
+
}
|
170
|
+
const l = O(e, t, s, "x");
|
171
|
+
return t > s && t <= r ? {
|
172
|
+
x: -n.width - l,
|
173
|
+
y: 0
|
174
|
+
} : t < s && t >= r ? {
|
175
|
+
x: n.width - l,
|
176
|
+
y: 0
|
177
|
+
} : {
|
178
|
+
y: 0,
|
179
|
+
x: 0
|
180
|
+
};
|
181
|
+
}, ie = /* @__PURE__ */ _({
|
156
182
|
__name: "context",
|
157
183
|
props: {
|
158
184
|
items: {},
|
185
|
+
strategy: { type: Function, default: Q },
|
159
186
|
collision: { type: Function, default: G },
|
160
|
-
|
187
|
+
lockAxis: {}
|
161
188
|
},
|
162
189
|
emits: ["end"],
|
163
190
|
setup(o, { emit: e }) {
|
164
|
-
const s = o,
|
191
|
+
const s = o, r = e, t = x(/* @__PURE__ */ new Map()), n = x(/* @__PURE__ */ new Map()), l = x(/* @__PURE__ */ new Map()), i = x(null), a = x(null), c = x({
|
165
192
|
start: null,
|
166
193
|
current: { x: 0, y: 0 }
|
167
|
-
}),
|
168
|
-
x: s.
|
169
|
-
y: s.
|
170
|
-
} : null), f = y(() =>
|
194
|
+
}), v = y(() => c.value.start ? {
|
195
|
+
x: s.lockAxis === "y" ? 0 : c.value.current.x - c.value.start.x,
|
196
|
+
y: s.lockAxis === "x" ? 0 : c.value.current.y - c.value.start.y
|
197
|
+
} : null), f = y(() => i.value ? s.items.indexOf(i.value) : null), p = y(() => a.value ? s.items.indexOf(a.value) : null), g = y(() => U(s.items, n.value));
|
171
198
|
function S() {
|
172
|
-
for (const [
|
173
|
-
n.value.set(
|
199
|
+
for (const [u, d] of t.value)
|
200
|
+
n.value.set(u, d.getBoundingClientRect());
|
174
201
|
}
|
175
202
|
function b() {
|
176
|
-
if (!
|
203
|
+
if (!i.value)
|
177
204
|
return null;
|
178
|
-
const
|
179
|
-
...
|
180
|
-
width:
|
181
|
-
height:
|
182
|
-
x:
|
183
|
-
y:
|
205
|
+
const u = n.value.get(i.value), d = u && v.value ? {
|
206
|
+
...u,
|
207
|
+
width: u.width,
|
208
|
+
height: u.height,
|
209
|
+
x: u.x + v.value?.x,
|
210
|
+
y: u.y + v.value?.y
|
184
211
|
} : null;
|
185
212
|
return d ? s.collision({
|
186
213
|
rect: d,
|
187
214
|
rects: n.value
|
188
215
|
})[0]?.id ?? null : null;
|
189
216
|
}
|
190
|
-
function
|
191
|
-
const
|
192
|
-
if (!
|
217
|
+
function k(u, d) {
|
218
|
+
const h = t.value.get(u);
|
219
|
+
if (!h)
|
193
220
|
return;
|
194
221
|
const m = new K({
|
195
|
-
element: d ??
|
222
|
+
element: d ?? h,
|
196
223
|
onStart(R) {
|
197
|
-
S(),
|
224
|
+
S(), c.value.start = R;
|
198
225
|
},
|
199
226
|
onMove(R) {
|
200
|
-
|
227
|
+
c.value.start && (i.value || (i.value = u), c.value.current.x = R.x, c.value.current.y = R.y, a.value = b());
|
201
228
|
},
|
202
229
|
onEnd() {
|
203
|
-
|
204
|
-
active:
|
205
|
-
over:
|
206
|
-
}),
|
230
|
+
i.value && r("end", {
|
231
|
+
active: i.value,
|
232
|
+
over: a.value
|
233
|
+
}), i.value = null, a.value = null, c.value.start = null;
|
207
234
|
}
|
208
235
|
});
|
209
|
-
l.value.set(
|
236
|
+
l.value.set(u, m);
|
210
237
|
}
|
211
|
-
return C(t, S, { deep: !0 }), C([
|
212
|
-
if (!
|
213
|
-
for (const [
|
238
|
+
return C(t, S, { deep: !0 }), C([i, a], ([u]) => {
|
239
|
+
if (!u) {
|
240
|
+
for (const [h, m] of t.value)
|
214
241
|
m.style.transition = "", m.style.position = "", m.style.zIndex = "", m.style.transform = "";
|
215
242
|
return;
|
216
243
|
}
|
217
|
-
const d = t.value.get(
|
244
|
+
const d = t.value.get(u);
|
218
245
|
if (d) {
|
219
246
|
d.style.position = "relative", d.style.zIndex = "1";
|
220
|
-
for (const [
|
221
|
-
|
247
|
+
for (const [h, m] of t.value)
|
248
|
+
h !== u && (m.style.transition = "0.2s transform");
|
222
249
|
}
|
223
|
-
}), C(
|
224
|
-
if (!(!
|
225
|
-
for (const [
|
226
|
-
let
|
227
|
-
|
250
|
+
}), C(v, () => {
|
251
|
+
if (!(!M(f.value) || !M(p.value)) && !(!i.value || !v.value))
|
252
|
+
for (const [u, d] of t.value) {
|
253
|
+
let h = null;
|
254
|
+
u === i.value ? h = v.value : h = s.strategy({
|
228
255
|
activeIndex: f.value,
|
229
|
-
overIndex:
|
256
|
+
overIndex: p.value,
|
230
257
|
rects: g.value,
|
231
|
-
index: s.items.indexOf(
|
232
|
-
}),
|
258
|
+
index: s.items.indexOf(u)
|
259
|
+
}), h && (d.style.transform = `translate(${h.x}px, ${h.y}px)`);
|
233
260
|
}
|
234
|
-
}),
|
261
|
+
}), P({
|
235
262
|
rects: n,
|
236
|
-
activate:
|
263
|
+
activate: k,
|
237
264
|
elements: t,
|
238
|
-
translate:
|
239
|
-
active:
|
240
|
-
over:
|
265
|
+
translate: v,
|
266
|
+
active: i,
|
267
|
+
over: a,
|
241
268
|
activeIndex: f,
|
242
|
-
overIndex:
|
269
|
+
overIndex: p,
|
243
270
|
items: y(() => s.items),
|
244
271
|
sortedRects: g
|
245
|
-
}), (
|
272
|
+
}), (u, d) => I(u.$slots, "default");
|
246
273
|
}
|
247
274
|
}), [
|
248
275
|
W,
|
249
276
|
Z
|
250
277
|
] = j("SortableContext");
|
251
278
|
function ee(o) {
|
252
|
-
const { active: e, elements: s, activate:
|
253
|
-
|
279
|
+
const { active: e, elements: s, activate: r } = J(), { id: t, element: n, handle: l } = o, i = x(w(l)), a = x(w(n));
|
280
|
+
F(() => {
|
254
281
|
const f = w(n);
|
255
282
|
f && s.value.set(t, f);
|
256
|
-
}),
|
283
|
+
}), T(() => {
|
257
284
|
s.value.delete(t);
|
258
|
-
}),
|
259
|
-
n && (
|
285
|
+
}), N(() => {
|
286
|
+
n && (a.value = w(n)), l && (i.value = w(l)), r(t, i.value);
|
260
287
|
});
|
261
|
-
const
|
262
|
-
|
288
|
+
const c = y(() => e.value === t), v = (f) => {
|
289
|
+
i.value = w(f);
|
263
290
|
};
|
264
291
|
return Z({
|
265
|
-
setHandleRef:
|
292
|
+
setHandleRef: v
|
266
293
|
}), {
|
267
|
-
isDragging:
|
268
|
-
elementRef:
|
269
|
-
setHandleRef:
|
294
|
+
isDragging: c,
|
295
|
+
elementRef: a,
|
296
|
+
setHandleRef: v
|
270
297
|
};
|
271
298
|
}
|
272
299
|
function te() {
|
273
300
|
return W().setHandleRef;
|
274
301
|
}
|
275
|
-
const
|
302
|
+
const re = /* @__PURE__ */ _({
|
276
303
|
__name: "handle",
|
277
304
|
props: {
|
278
305
|
as: { default: "button" }
|
279
306
|
},
|
280
307
|
setup(o) {
|
281
308
|
const e = te();
|
282
|
-
return (s,
|
309
|
+
return (s, r) => (E(), A($(s.as), { ref: V(e) }, {
|
283
310
|
default: D(() => [
|
284
|
-
|
311
|
+
I(s.$slots, "default")
|
285
312
|
]),
|
286
313
|
_: 3
|
287
314
|
}, 512));
|
288
315
|
}
|
289
|
-
}),
|
316
|
+
}), le = /* @__PURE__ */ _({
|
290
317
|
__name: "sortable",
|
291
318
|
props: {
|
292
319
|
as: { default: "div" },
|
@@ -294,32 +321,34 @@ const ie = /* @__PURE__ */ L({
|
|
294
321
|
},
|
295
322
|
setup(o) {
|
296
323
|
const e = X("el");
|
297
|
-
return ee({ id: o.id, element: e }), (s,
|
324
|
+
return ee({ id: o.id, element: e }), (s, r) => (E(), A($(s.as), { ref: "el" }, {
|
298
325
|
default: D(() => [
|
299
|
-
|
326
|
+
I(s.$slots, "default")
|
300
327
|
]),
|
301
328
|
_: 3
|
302
329
|
}, 512));
|
303
330
|
}
|
304
331
|
});
|
305
|
-
function
|
306
|
-
const { active: s, over:
|
307
|
-
if (!
|
332
|
+
function ae(o, e) {
|
333
|
+
const { active: s, over: r } = e;
|
334
|
+
if (!r)
|
308
335
|
return o;
|
309
|
-
const t = o.indexOf(s), n = o.indexOf(
|
336
|
+
const t = o.indexOf(s), n = o.indexOf(r);
|
310
337
|
if (t === n)
|
311
338
|
return o;
|
312
|
-
const l = o.slice(),
|
313
|
-
return
|
339
|
+
const l = o.slice(), i = l.splice(t, 1)[0];
|
340
|
+
return i && l.splice(n, 0, i), l;
|
314
341
|
}
|
315
342
|
export {
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
343
|
+
ie as SortableContext,
|
344
|
+
re as SortableHandle,
|
345
|
+
le as SortableItem,
|
346
|
+
ae as arrayMove,
|
320
347
|
G as closestToCenter,
|
348
|
+
oe as horizontalListSortingStrategy,
|
321
349
|
W as injectSortableContext,
|
322
350
|
se as rectangleIntersection,
|
323
351
|
te as useSetHandleRef,
|
324
|
-
ee as useSortable
|
352
|
+
ee as useSortable,
|
353
|
+
Q as verticalListSortingStrategy
|
325
354
|
};
|
package/dist/vue-dnd.umd.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
(function(
|
1
|
+
(function(d,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(d=typeof globalThis<"u"?globalThis:d||self,i(d.VueDnD={},d.Vue))})(this,function(d,i){"use strict";function j(s,t){return t.value-s.value}function k(s,t){return s.value-t.value}const O=({rect:s,rects:t})=>{function o(e,n){const c=Math.max(e.x,n.x),r=Math.min(e.x+e.width,n.x+n.width),a=Math.max(e.y,n.y),f=Math.min(e.y+e.height,n.y+n.height),v=r-c,p=f-a;if(c<r&&a<f){const w=v*p,g=e.width*e.height,S=n.width*n.height,b=w/(g+S-w);return Number(b.toFixed(4))}return 0}const l=[];for(const[e,n]of t)l.push({id:e,value:o(s,n)});return l.sort(j)},L=({rect:s,rects:t})=>{function o(n,c){return Math.sqrt((n.x-c.x)**2+(n.y-c.y)**2)}function l(n){return{x:n.x+n.width/2,y:n.y+n.height/2}}const e=[];for(const[n,c]of t)e.push({id:n,value:o(l(c),l(s))});return e.sort(k)};class R{listeners=[];element;constructor(t){this.element=t}add(t,o){this.element.addEventListener(t,o),this.listeners.push([t,o])}removeAll(){this.listeners.forEach(t=>{this.element.removeEventListener(...t)})}}class T{document;listeners;documentListeners;options;windowListeners;constructor(t){const{element:o}=t;this.options=t,this.document=o.ownerDocument,this.listeners=new R(o),this.documentListeners=new R(this.document),this.windowListeners=new R(this.document?.defaultView??window),this.handleStart=this.handleStart.bind(this),this.handleMove=this.handleMove.bind(this),this.handleEnd=this.handleEnd.bind(this),this.deactivate()}deactivate(){this.listeners.removeAll(),this.documentListeners.removeAll(),this.windowListeners.removeAll(),this.listeners.add("pointerdown",this.handleStart)}activate(){this.windowListeners.add("selectionchange",this.clearSelection),this.windowListeners.add("resize",this.handleEnd),this.windowListeners.add("visibilitychange",this.handleEnd),this.windowListeners.add("dragstart",t=>t.preventDefault()),this.windowListeners.add("contextmenu",t=>t.preventDefault()),this.documentListeners.add("pointermove",this.handleMove),this.documentListeners.add("pointerup",this.handleEnd)}handleStart(t){t.button===0&&(this.options.onStart?.({x:t.clientX,y:t.clientY}),this.activate())}handleMove(t){this.options.onMove?.({x:t.clientX,y:t.clientY}),this.clearSelection()}handleEnd(t){this.options.onEnd?.(t),this.deactivate()}clearSelection(){this.document?.getSelection()?.removeAllRanges()}}function M(s){const t=Symbol(`${s}Context`);function o(e){const n=i.inject(t,e);if(n||n===null)return n;throw new Error(`Injection \`${t.toString()}\` not found. Component must be used within \`${s}\``)}function l(e){return i.provide(t,e),e}return[o,l]}function z(s,t){return s.reduce((o,l)=>{const e=t.get(l);return e&&o.push(e),o},[])}function y(s){const t=i.toValue(s);return t?.$el??t}function _(s){return s!==null&&s>=0}const[B,H]=M("DndContext"),I=s=>{const{rects:t,activeIndex:o,overIndex:l,index:e}=s,n=t[o];if(!n)return null;if(e===o){const r=t[l];return r?{x:0,y:o>l?n.top-r.top:r.top-n.top}:null}const c=A(t,e,o,"y");return e>o&&e<=l?{x:0,y:-n.height-c}:e<o&&e>=l?{x:0,y:n.height-c}:{x:0,y:0}};function A(s,t,o,l){const e=s[t],n=s[t-1],c=s[t+1],r=l==="x"?"width":"height",a=l==="x"?"left":"top";return!e||!n&&!c?0:o<t?n?e[a]-(n[a]+n[r]):c?c[a]-(e[a]+e[r]):0:c?e[a]-(c[a]-c[r]):n?n[a]-(e[a]+e[r]):0}const V=s=>{const{rects:t,activeIndex:o,overIndex:l,index:e}=s,n=t[o];if(!n)return null;if(e===o){const r=t[l];return r?{x:o>l?n.left-r.left:r.left-n.left,y:0}:null}const c=A(t,e,o,"x");return e>o&&e<=l?{x:-n.width-c,y:0}:e<o&&e>=l?{x:n.width-c,y:0}:{y:0,x:0}},F=i.defineComponent({__name:"context",props:{items:{},strategy:{type:Function,default:I},collision:{type:Function,default:L},lockAxis:{}},emits:["end"],setup(s,{emit:t}){const o=s,l=t,e=i.ref(new Map),n=i.ref(new Map),c=i.ref(new Map),r=i.ref(null),a=i.ref(null),f=i.ref({start:null,current:{x:0,y:0}}),v=i.computed(()=>f.value.start?{x:o.lockAxis==="y"?0:f.value.current.x-f.value.start.x,y:o.lockAxis==="x"?0:f.value.current.y-f.value.start.y}:null),p=i.computed(()=>r.value?o.items.indexOf(r.value):null),w=i.computed(()=>a.value?o.items.indexOf(a.value):null),g=i.computed(()=>z(o.items,n.value));function S(){for(const[u,h]of e.value)n.value.set(u,h.getBoundingClientRect())}function b(){if(!r.value)return null;const u=n.value.get(r.value),h=u&&v.value?{...u,width:u.width,height:u.height,x:u.x+v.value?.x,y:u.y+v.value?.y}:null;return h?o.collision({rect:h,rects:n.value})[0]?.id??null:null}function G(u,h){const m=e.value.get(u);if(!m)return;const x=new T({element:h??m,onStart(C){S(),f.value.start=C},onMove(C){f.value.start&&(r.value||(r.value=u),f.value.current.x=C.x,f.value.current.y=C.y,a.value=b())},onEnd(){r.value&&l("end",{active:r.value,over:a.value}),r.value=null,a.value=null,f.value.start=null}});c.value.set(u,x)}return i.watch(e,S,{deep:!0}),i.watch([r,a],([u])=>{if(!u){for(const[m,x]of e.value)x.style.transition="",x.style.position="",x.style.zIndex="",x.style.transform="";return}const h=e.value.get(u);if(h){h.style.position="relative",h.style.zIndex="1";for(const[m,x]of e.value)m!==u&&(x.style.transition="0.2s transform")}}),i.watch(v,()=>{if(!(!_(p.value)||!_(w.value))&&!(!r.value||!v.value))for(const[u,h]of e.value){let m=null;u===r.value?m=v.value:m=o.strategy({activeIndex:p.value,overIndex:w.value,rects:g.value,index:o.items.indexOf(u)}),m&&(h.style.transform=`translate(${m.x}px, ${m.y}px)`)}}),H({rects:n,activate:G,elements:e,translate:v,active:r,over:a,activeIndex:p,overIndex:w,items:i.computed(()=>o.items),sortedRects:g}),(u,h)=>i.renderSlot(u.$slots,"default")}}),[E,q]=M("SortableContext");function D(s){const{active:t,elements:o,activate:l}=B(),{id:e,element:n,handle:c}=s,r=i.ref(y(c)),a=i.ref(y(n));i.onMounted(()=>{const p=y(n);p&&o.value.set(e,p)}),i.onUnmounted(()=>{o.value.delete(e)}),i.watchEffect(()=>{n&&(a.value=y(n)),c&&(r.value=y(c)),l(e,r.value)});const f=i.computed(()=>t.value===e),v=p=>{r.value=y(p)};return q({setHandleRef:v}),{isDragging:f,elementRef:a,setHandleRef:v}}function $(){return E().setHandleRef}const N=i.defineComponent({__name:"handle",props:{as:{default:"button"}},setup(s){const t=$();return(o,l)=>(i.openBlock(),i.createBlock(i.resolveDynamicComponent(o.as),{ref:i.unref(t)},{default:i.withCtx(()=>[i.renderSlot(o.$slots,"default")]),_:3},512))}}),X=i.defineComponent({__name:"sortable",props:{as:{default:"div"},id:{}},setup(s){const t=i.useTemplateRef("el");return D({id:s.id,element:t}),(o,l)=>(i.openBlock(),i.createBlock(i.resolveDynamicComponent(o.as),{ref:"el"},{default:i.withCtx(()=>[i.renderSlot(o.$slots,"default")]),_:3},512))}});function Y(s,t){const{active:o,over:l}=t;if(!l)return s;const e=s.indexOf(o),n=s.indexOf(l);if(e===n)return s;const c=s.slice(),r=c.splice(e,1)[0];return r&&c.splice(n,0,r),c}d.SortableContext=F,d.SortableHandle=N,d.SortableItem=X,d.arrayMove=Y,d.closestToCenter=L,d.horizontalListSortingStrategy=V,d.injectSortableContext=E,d.rectangleIntersection=O,d.useSetHandleRef=$,d.useSortable=D,d.verticalListSortingStrategy=I,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|