vue-dnd-sortable 0.1.6 → 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 CHANGED
@@ -48,7 +48,8 @@ npm install vue-dnd-sortable
48
48
  <script setup lang="ts">
49
49
  import { ref } from 'vue'
50
50
  import {
51
- arrayMove,
51
+ arrayMove,
52
+ horizontalListSortingStrategy,
52
53
  SortableContext,
53
54
  SortableItem,
54
55
  SortableHandle
@@ -58,17 +59,18 @@ const items = ref([1, 2, 3, 4, 5, 6])
58
59
  </script>
59
60
 
60
61
  <template>
61
- <SortableContext axis="y" :items @end="items = arrayMove(items, $event)">
62
+ <SortableContext
63
+ axis="x"
64
+ :items
65
+ :strategy="horizontalListSortingStrategy"
66
+ @end="items = arrayMove(items, $event)">
62
67
  <div
63
- style="display: flex; flex-direction: column; gap: 16px; width: 400px; margin: 0 auto;"
68
+ style="display: flex; gap: 16px; width: 400px; margin: 0 auto;"
64
69
  >
65
70
  <SortableItem
66
71
  v-for="(item, index) in items"
67
72
  :id="item"
68
73
  :key="item"
69
- :style="{
70
- marginBottom: index % 2 === 0 ? '20px' : '50px',
71
- }"
72
74
  >
73
75
  <SortableHandle>
74
76
  handle
@@ -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
- axis?: 'x' | 'y';
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';
@@ -1,7 +1,9 @@
1
1
  import type { Coords } from './types';
2
- export declare function verticalListSortingStrategy(args: {
2
+ export type AxisSortingStrategy = (args: {
3
3
  rects: DOMRect[];
4
4
  activeIndex: number;
5
5
  overIndex: number;
6
6
  index: number;
7
- }): Coords | null;
7
+ }) => Coords | null;
8
+ export declare const verticalListSortingStrategy: AxisSortingStrategy;
9
+ export declare const horizontalListSortingStrategy: AxisSortingStrategy;
@@ -1,4 +1,4 @@
1
- import { inject as H, provide as B, toValue as z, defineComponent as L, ref as p, computed as y, watch as C, renderSlot as M, onMounted as T, onUnmounted as k, watchEffect as F, createBlock as I, openBlock as A, resolveDynamicComponent as $, unref as V, withCtx as D, useTemplateRef as X } from "vue";
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), r = Math.min(t.x + t.width, n.x + n.width), c = Math.max(t.y, n.y), u = Math.min(t.y + t.height, n.y + n.height), h = r - l, f = u - c;
11
- if (l < r && c < u) {
12
- const x = h * f, g = t.width * t.height, S = n.width * n.height, b = x / (g + S - x);
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 i = [];
17
+ const r = [];
18
18
  for (const [t, n] of e)
19
- i.push({
19
+ r.push({
20
20
  id: t,
21
21
  value: s(o, n)
22
22
  });
23
- return i.sort(Y);
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 i(n) {
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(i(l), i(o))
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 _(s), this.documentListeners = new _(this.document), this.windowListeners = new _(this.document?.defaultView ?? window), this.handleStart = this.handleStart.bind(this), this.handleMove = this.handleMove.bind(this), this.handleEnd = this.handleEnd.bind(this), this.deactivate();
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 = H(e, t);
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 i(t) {
105
- return B(e, t), t;
104
+ function r(t) {
105
+ return H(e, t), t;
106
106
  }
107
- return [s, i];
107
+ return [s, r];
108
108
  }
109
- function N(o, e) {
110
- return o.reduce((s, i) => {
111
- const t = e.get(i);
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 = z(o);
116
+ const e = B(o);
117
117
  return e?.$el ?? e;
118
118
  }
119
- function E(o) {
119
+ function M(o) {
120
120
  return o !== null && o >= 0;
121
121
  }
122
- const [U, J] = j("DndContext");
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: i,
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 r = e[i];
134
- return r ? {
132
+ const i = e[r];
133
+ return i ? {
135
134
  x: 0,
136
- y: s > i ? n.top - r.top : r.top - n.top
135
+ y: s > r ? n.top - i.top : i.top - n.top
137
136
  } : null;
138
137
  }
139
- const l = Q(e, t, s);
140
- return t > s && t <= i ? {
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 >= i ? {
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
- function Q(o, e, s) {
152
- const i = o[e], t = o[e - 1], n = o[e + 1];
153
- return !i || !t && !n ? 0 : s < e ? t ? i.top - (t.top + t.height) : n ? n.top - (i.top + i.height) : 0 : n ? i.top - (n.top - n.height) : t ? t.top - (i.top + i.height) : 0;
154
- }
155
- const oe = /* @__PURE__ */ L({
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
- axis: {}
187
+ lockAxis: {}
161
188
  },
162
189
  emits: ["end"],
163
190
  setup(o, { emit: e }) {
164
- const s = o, i = e, t = p(/* @__PURE__ */ new Map()), n = p(/* @__PURE__ */ new Map()), l = p(/* @__PURE__ */ new Map()), r = p(null), c = p(null), u = p({
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
- }), h = y(() => u.value.start ? {
168
- x: s.axis === "y" ? 0 : u.value.current.x - u.value.start.x,
169
- y: s.axis === "x" ? 0 : u.value.current.y - u.value.start.y
170
- } : null), f = y(() => r.value ? s.items.indexOf(r.value) : null), x = y(() => c.value ? s.items.indexOf(c.value) : null), g = y(() => N(s.items, n.value));
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 [a, d] of t.value)
173
- n.value.set(a, d.getBoundingClientRect());
199
+ for (const [u, d] of t.value)
200
+ n.value.set(u, d.getBoundingClientRect());
174
201
  }
175
202
  function b() {
176
- if (!r.value)
203
+ if (!i.value)
177
204
  return null;
178
- const a = n.value.get(r.value), d = a && h.value ? {
179
- ...a,
180
- width: a.width,
181
- height: a.height,
182
- x: a.x + h.value?.x,
183
- y: a.y + h.value?.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 O(a, d) {
191
- const v = t.value.get(a);
192
- if (!v)
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 ?? v,
222
+ element: d ?? h,
196
223
  onStart(R) {
197
- S(), u.value.start = R;
224
+ S(), c.value.start = R;
198
225
  },
199
226
  onMove(R) {
200
- u.value.start && (r.value || (r.value = a), u.value.current.x = R.x, u.value.current.y = R.y, c.value = b());
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
- r.value && i("end", {
204
- active: r.value,
205
- over: c.value
206
- }), r.value = null, c.value = null, u.value.start = null;
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(a, m);
236
+ l.value.set(u, m);
210
237
  }
211
- return C(t, S, { deep: !0 }), C([r, c], ([a]) => {
212
- if (!a) {
213
- for (const [v, m] of t.value)
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(a);
244
+ const d = t.value.get(u);
218
245
  if (d) {
219
246
  d.style.position = "relative", d.style.zIndex = "1";
220
- for (const [v, m] of t.value)
221
- v !== a && (m.style.transition = "0.2s transform");
247
+ for (const [h, m] of t.value)
248
+ h !== u && (m.style.transition = "0.2s transform");
222
249
  }
223
- }), C(h, () => {
224
- if (!(!E(f.value) || !E(x.value)) && !(!r.value || !h.value))
225
- for (const [a, d] of t.value) {
226
- let v = null;
227
- a === r.value ? v = h.value : v = P({
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: x.value,
256
+ overIndex: p.value,
230
257
  rects: g.value,
231
- index: s.items.indexOf(a)
232
- }), v && (d.style.transform = `translate(${v.x}px, ${v.y}px)`);
258
+ index: s.items.indexOf(u)
259
+ }), h && (d.style.transform = `translate(${h.x}px, ${h.y}px)`);
233
260
  }
234
- }), J({
261
+ }), P({
235
262
  rects: n,
236
- activate: O,
263
+ activate: k,
237
264
  elements: t,
238
- translate: h,
239
- active: r,
240
- over: c,
265
+ translate: v,
266
+ active: i,
267
+ over: a,
241
268
  activeIndex: f,
242
- overIndex: x,
269
+ overIndex: p,
243
270
  items: y(() => s.items),
244
271
  sortedRects: g
245
- }), (a, d) => M(a.$slots, "default");
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: i } = U(), { id: t, element: n, handle: l } = o, r = p(w(l)), c = p(w(n));
253
- T(() => {
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
- }), k(() => {
283
+ }), T(() => {
257
284
  s.value.delete(t);
258
- }), F(() => {
259
- n && (c.value = w(n)), l && (r.value = w(l)), i(t, r.value);
285
+ }), N(() => {
286
+ n && (a.value = w(n)), l && (i.value = w(l)), r(t, i.value);
260
287
  });
261
- const u = y(() => e.value === t), h = (f) => {
262
- r.value = w(f);
288
+ const c = y(() => e.value === t), v = (f) => {
289
+ i.value = w(f);
263
290
  };
264
291
  return Z({
265
- setHandleRef: h
292
+ setHandleRef: v
266
293
  }), {
267
- isDragging: u,
268
- elementRef: c,
269
- setHandleRef: h
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 ie = /* @__PURE__ */ L({
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, i) => (A(), I($(s.as), { ref: V(e) }, {
309
+ return (s, r) => (E(), A($(s.as), { ref: V(e) }, {
283
310
  default: D(() => [
284
- M(s.$slots, "default")
311
+ I(s.$slots, "default")
285
312
  ]),
286
313
  _: 3
287
314
  }, 512));
288
315
  }
289
- }), re = /* @__PURE__ */ L({
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, i) => (A(), I($(s.as), { ref: "el" }, {
324
+ return ee({ id: o.id, element: e }), (s, r) => (E(), A($(s.as), { ref: "el" }, {
298
325
  default: D(() => [
299
- M(s.$slots, "default")
326
+ I(s.$slots, "default")
300
327
  ]),
301
328
  _: 3
302
329
  }, 512));
303
330
  }
304
331
  });
305
- function le(o, e) {
306
- const { active: s, over: i } = e;
307
- if (!i)
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(i);
336
+ const t = o.indexOf(s), n = o.indexOf(r);
310
337
  if (t === n)
311
338
  return o;
312
- const l = o.slice(), r = l.splice(t, 1)[0];
313
- return r && l.splice(n, 0, r), l;
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
- oe as SortableContext,
317
- ie as SortableHandle,
318
- re as SortableItem,
319
- le as arrayMove,
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
  };
@@ -1 +1 @@
1
- (function(u,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(u=typeof globalThis<"u"?globalThis:u||self,i(u.VueDnD={},u.Vue))})(this,function(u,i){"use strict";function D(s,e){return e.value-s.value}function $(s,e){return s.value-e.value}const j=({rect:s,rects:e})=>{function o(t,n){const a=Math.max(t.x,n.x),l=Math.min(t.x+t.width,n.x+n.width),f=Math.max(t.y,n.y),d=Math.min(t.y+t.height,n.y+n.height),v=l-a,p=d-f;if(a<l&&f<d){const y=v*p,g=t.width*t.height,S=n.width*n.height,R=y/(g+S-y);return Number(R.toFixed(4))}return 0}const r=[];for(const[t,n]of e)r.push({id:t,value:o(s,n)});return r.sort(D)},M=({rect:s,rects:e})=>{function o(n,a){return Math.sqrt((n.x-a.x)**2+(n.y-a.y)**2)}function r(n){return{x:n.x+n.width/2,y:n.y+n.height/2}}const t=[];for(const[n,a]of e)t.push({id:n,value:o(r(a),r(s))});return t.sort($)};class b{listeners=[];element;constructor(e){this.element=e}add(e,o){this.element.addEventListener(e,o),this.listeners.push([e,o])}removeAll(){this.listeners.forEach(e=>{this.element.removeEventListener(...e)})}}class O{document;listeners;documentListeners;options;windowListeners;constructor(e){const{element:o}=e;this.options=e,this.document=o.ownerDocument,this.listeners=new b(o),this.documentListeners=new b(this.document),this.windowListeners=new b(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",e=>e.preventDefault()),this.windowListeners.add("contextmenu",e=>e.preventDefault()),this.documentListeners.add("pointermove",this.handleMove),this.documentListeners.add("pointerup",this.handleEnd)}handleStart(e){e.button===0&&(this.options.onStart?.({x:e.clientX,y:e.clientY}),this.activate())}handleMove(e){this.options.onMove?.({x:e.clientX,y:e.clientY}),this.clearSelection()}handleEnd(e){this.options.onEnd?.(e),this.deactivate()}clearSelection(){this.document?.getSelection()?.removeAllRanges()}}function _(s){const e=Symbol(`${s}Context`);function o(t){const n=i.inject(e,t);if(n||n===null)return n;throw new Error(`Injection \`${e.toString()}\` not found. Component must be used within \`${s}\``)}function r(t){return i.provide(e,t),t}return[o,r]}function T(s,e){return s.reduce((o,r)=>{const t=e.get(r);return t&&o.push(t),o},[])}function w(s){const e=i.toValue(s);return e?.$el??e}function L(s){return s!==null&&s>=0}const[B,H]=_("DndContext");function k(s){const{rects:e,activeIndex:o,overIndex:r,index:t}=s,n=e[o];if(!n)return null;if(t===o){const l=e[r];return l?{x:0,y:o>r?n.top-l.top:l.top-n.top}:null}const a=V(e,t,o);return t>o&&t<=r?{x:0,y:-n.height-a}:t<o&&t>=r?{x:0,y:n.height-a}:{x:0,y:0}}function V(s,e,o){const r=s[e],t=s[e-1],n=s[e+1];return!r||!t&&!n?0:o<e?t?r.top-(t.top+t.height):n?n.top-(r.top+r.height):0:n?r.top-(n.top-n.height):t?t.top-(r.top+r.height):0}const z=i.defineComponent({__name:"context",props:{items:{},collision:{type:Function,default:M},axis:{}},emits:["end"],setup(s,{emit:e}){const o=s,r=e,t=i.ref(new Map),n=i.ref(new Map),a=i.ref(new Map),l=i.ref(null),f=i.ref(null),d=i.ref({start:null,current:{x:0,y:0}}),v=i.computed(()=>d.value.start?{x:o.axis==="y"?0:d.value.current.x-d.value.start.x,y:o.axis==="x"?0:d.value.current.y-d.value.start.y}:null),p=i.computed(()=>l.value?o.items.indexOf(l.value):null),y=i.computed(()=>f.value?o.items.indexOf(f.value):null),g=i.computed(()=>T(o.items,n.value));function S(){for(const[c,h]of t.value)n.value.set(c,h.getBoundingClientRect())}function R(){if(!l.value)return null;const c=n.value.get(l.value),h=c&&v.value?{...c,width:c.width,height:c.height,x:c.x+v.value?.x,y:c.y+v.value?.y}:null;return h?o.collision({rect:h,rects:n.value})[0]?.id??null:null}function G(c,h){const m=t.value.get(c);if(!m)return;const x=new O({element:h??m,onStart(C){S(),d.value.start=C},onMove(C){d.value.start&&(l.value||(l.value=c),d.value.current.x=C.x,d.value.current.y=C.y,f.value=R())},onEnd(){l.value&&r("end",{active:l.value,over:f.value}),l.value=null,f.value=null,d.value.start=null}});a.value.set(c,x)}return i.watch(t,S,{deep:!0}),i.watch([l,f],([c])=>{if(!c){for(const[m,x]of t.value)x.style.transition="",x.style.position="",x.style.zIndex="",x.style.transform="";return}const h=t.value.get(c);if(h){h.style.position="relative",h.style.zIndex="1";for(const[m,x]of t.value)m!==c&&(x.style.transition="0.2s transform")}}),i.watch(v,()=>{if(!(!L(p.value)||!L(y.value))&&!(!l.value||!v.value))for(const[c,h]of t.value){let m=null;c===l.value?m=v.value:m=k({activeIndex:p.value,overIndex:y.value,rects:g.value,index:o.items.indexOf(c)}),m&&(h.style.transform=`translate(${m.x}px, ${m.y}px)`)}}),H({rects:n,activate:G,elements:t,translate:v,active:l,over:f,activeIndex:p,overIndex:y,items:i.computed(()=>o.items),sortedRects:g}),(c,h)=>i.renderSlot(c.$slots,"default")}}),[E,q]=_("SortableContext");function I(s){const{active:e,elements:o,activate:r}=B(),{id:t,element:n,handle:a}=s,l=i.ref(w(a)),f=i.ref(w(n));i.onMounted(()=>{const p=w(n);p&&o.value.set(t,p)}),i.onUnmounted(()=>{o.value.delete(t)}),i.watchEffect(()=>{n&&(f.value=w(n)),a&&(l.value=w(a)),r(t,l.value)});const d=i.computed(()=>e.value===t),v=p=>{l.value=w(p)};return q({setHandleRef:v}),{isDragging:d,elementRef:f,setHandleRef:v}}function A(){return E().setHandleRef}const F=i.defineComponent({__name:"handle",props:{as:{default:"button"}},setup(s){const e=A();return(o,r)=>(i.openBlock(),i.createBlock(i.resolveDynamicComponent(o.as),{ref:i.unref(e)},{default:i.withCtx(()=>[i.renderSlot(o.$slots,"default")]),_:3},512))}}),X=i.defineComponent({__name:"sortable",props:{as:{default:"div"},id:{}},setup(s){const e=i.useTemplateRef("el");return I({id:s.id,element:e}),(o,r)=>(i.openBlock(),i.createBlock(i.resolveDynamicComponent(o.as),{ref:"el"},{default:i.withCtx(()=>[i.renderSlot(o.$slots,"default")]),_:3},512))}});function Y(s,e){const{active:o,over:r}=e;if(!r)return s;const t=s.indexOf(o),n=s.indexOf(r);if(t===n)return s;const a=s.slice(),l=a.splice(t,1)[0];return l&&a.splice(n,0,l),a}u.SortableContext=z,u.SortableHandle=F,u.SortableItem=X,u.arrayMove=Y,u.closestToCenter=M,u.injectSortableContext=E,u.rectangleIntersection=j,u.useSetHandleRef=A,u.useSortable=I,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-dnd-sortable",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Simple sortable for vue",
5
5
  "type": "module",
6
6
  "main": "dist/vue-dnd.umd.js",