vue-dnd-sortable 0.1.3 → 0.1.5

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
@@ -1,15 +1,81 @@
1
- # core
1
+ # vue-dnd-sortable
2
2
 
3
- To install dependencies:
3
+ A minimal drag-and-drop sortable list component library for Vue 3.
4
+ **⚠️ Warning: This package is experimental and poorly tested. Use at your own risk.**
5
+
6
+ ## Features
7
+
8
+ - Lightweight and flexible
9
+ - Built with Vue 3 Composition API
10
+ - Supports drag handles and custom spacing
11
+ - Simple API using `<SortableContext>`, `<SortableItem>`, and `<SortableHandle>`
12
+
13
+ ---
14
+
15
+ ## Installation
4
16
 
5
17
  ```bash
6
- bun install
7
- ```
18
+ bun add vue-dnd-sortable
19
+ ````
8
20
 
9
- To run:
21
+ or
10
22
 
11
23
  ```bash
12
- bun run index.ts
24
+ npm install vue-dnd-sortable
13
25
  ```
14
26
 
15
- This project was created using `bun init` in bun v1.2.5. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
27
+ ---
28
+
29
+ ## Usage Example
30
+
31
+ ```vue
32
+ <script setup lang="ts">
33
+ import { ref } from 'vue'
34
+ import {
35
+ arrayMove,
36
+ SortableContext,
37
+ SortableItem,
38
+ SortableHandle
39
+ } from 'vue-dnd-sortable'
40
+
41
+ const items = ref([1, 2, 3, 4, 5, 6])
42
+ </script>
43
+
44
+ <template>
45
+ <SortableContext axis="y" :items @end="items = arrayMove(items, $event)">
46
+ <div
47
+ style="display: flex; flex-direction: column; gap: 16px; width: 400px; margin: 0 auto;"
48
+ >
49
+ <SortableItem
50
+ v-for="(item, index) in items"
51
+ :id="item"
52
+ :key="item"
53
+ >
54
+ <SortableHandle>
55
+ handle
56
+ </SortableHandle>
57
+
58
+ {{ item }}
59
+ </SortableItem>
60
+ </div>
61
+ </SortableContext>
62
+ </template>
63
+ ```
64
+
65
+ ---
66
+
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
+ ## License
80
+
81
+ MIT
@@ -1,6 +1,6 @@
1
1
  import type { Coords } from './types';
2
2
  interface ActivatorOptions {
3
- element: HTMLElement;
3
+ element: Element;
4
4
  onStart?: (coords: Coords) => void;
5
5
  onMove?: (coords: Coords) => void;
6
6
  onEnd?: (e: PointerEvent) => void;
@@ -0,0 +1,15 @@
1
+ type __VLS_Props = {
2
+ as?: string;
3
+ };
4
+ declare var __VLS_6: {};
5
+ type __VLS_Slots = {} & {
6
+ default?: (props: typeof __VLS_6) => any;
7
+ };
8
+ declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
9
+ declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
10
+ export default _default;
11
+ type __VLS_WithSlots<T, S> = T & {
12
+ new (): {
13
+ $slots: S;
14
+ };
15
+ };
@@ -1 +1,3 @@
1
- export { default as DndProvider } from './provider.vue';
1
+ export { default as SortableContext } from './context.vue';
2
+ export { default as SortableHandle } from './handle.vue';
3
+ export { default as SortableItem } from './sortable.vue';
@@ -0,0 +1,16 @@
1
+ type __VLS_Props = {
2
+ as?: string;
3
+ id: number;
4
+ };
5
+ declare var __VLS_7: {};
6
+ type __VLS_Slots = {} & {
7
+ default?: (props: typeof __VLS_7) => any;
8
+ };
9
+ declare const __VLS_component: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
10
+ declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
11
+ export default _default;
12
+ type __VLS_WithSlots<T, S> = T & {
13
+ new (): {
14
+ $slots: S;
15
+ };
16
+ };
@@ -1,10 +1,18 @@
1
- import type { MaybeRefOrGetter } from 'vue';
2
1
  import type { UniqueIdentifier } from './types';
2
+ import type { MaybeComputedElementRef } from './utilities';
3
3
  export interface SortableOptions {
4
4
  id: UniqueIdentifier;
5
- element: MaybeRefOrGetter<HTMLElement | null>;
6
- handleElement?: MaybeRefOrGetter<HTMLElement | null>;
5
+ element: MaybeComputedElementRef;
6
+ handle?: MaybeComputedElementRef;
7
7
  }
8
+ interface SortableContext {
9
+ setHandleRef: (el: MaybeComputedElementRef) => void;
10
+ }
11
+ declare const injectSortableContext: <T extends SortableContext | null | undefined = SortableContext>(fallback?: T | undefined) => T extends null ? SortableContext | null : SortableContext;
12
+ export { injectSortableContext };
8
13
  export declare function useSortable(options: SortableOptions): {
9
14
  isDragging: import("vue").ComputedRef<boolean>;
15
+ elementRef: import("vue").Ref<Element | HTMLElement | SVGElement | null | undefined, Element | HTMLElement | SVGElement | null | undefined>;
16
+ setHandleRef: (el: MaybeComputedElementRef) => void;
10
17
  };
18
+ export declare function useSetHandleRef(): (el: MaybeComputedElementRef) => void;
package/dist/context.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { ComputedRef, Ref } from 'vue';
2
2
  import type { Coords, Nullable, UniqueIdentifier } from './types';
3
3
  import type { DomRectMap } from './utilities/rects';
4
4
  interface DndContext {
5
- elements: Ref<Map<UniqueIdentifier, HTMLElement>>;
5
+ elements: Ref<Map<UniqueIdentifier, Element>>;
6
6
  rects: Ref<DomRectMap>;
7
7
  items: ComputedRef<UniqueIdentifier[]>;
8
8
  translate: Ref<Coords | null>;
@@ -11,7 +11,7 @@ interface DndContext {
11
11
  sortedRects: Ref<DOMRect[]>;
12
12
  activeIndex: ComputedRef<number | null>;
13
13
  overIndex: ComputedRef<number | null>;
14
- activate: (id: UniqueIdentifier, handle: Nullable<HTMLElement>) => void;
14
+ activate: (id: UniqueIdentifier, handle: Nullable<Element>) => void;
15
15
  }
16
16
  export interface DndEndEvent {
17
17
  active: UniqueIdentifier;
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from './collision';
2
2
  export * from './components';
3
3
  export * from './composables';
4
4
  export * from './helpers';
5
+ export type { MaybeElement } from './utilities';
@@ -1,3 +1,4 @@
1
1
  export * from './context';
2
2
  export * from './rects';
3
+ export * from './vue';
3
4
  export declare function isValidIndex(index: number | null): index is number;
@@ -0,0 +1,7 @@
1
+ import type { ComponentPublicInstance, MaybeRef, MaybeRefOrGetter } from 'vue';
2
+ export type VueInstance = ComponentPublicInstance;
3
+ export type MaybeElementRef<T extends MaybeElement = MaybeElement> = MaybeRef<T>;
4
+ export type MaybeComputedElementRef<T extends MaybeElement = MaybeElement> = MaybeRefOrGetter<T>;
5
+ export type MaybeElement = Element | HTMLElement | SVGElement | VueInstance | undefined | null;
6
+ export type UnRefElementReturn<T extends MaybeElement = MaybeElement> = T extends VueInstance ? Exclude<MaybeElement, VueInstance> : T | undefined;
7
+ export declare function unrefElement<T extends MaybeElement>(elRef: MaybeComputedElementRef<T>): UnRefElementReturn<T>;
@@ -1,52 +1,52 @@
1
- import { inject as R, provide as A, defineComponent as C, ref as w, computed as f, watch as S, renderSlot as b, onMounted as D, toValue as E, onUnmounted as O } from "vue";
2
- function _(s, e) {
3
- return e.value - s.value;
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";
2
+ function Y(o, e) {
3
+ return e.value - o.value;
4
4
  }
5
- function $(s, e) {
6
- return s.value - e.value;
5
+ function q(o, e) {
6
+ return o.value - e.value;
7
7
  }
8
- const G = ({ rect: s, rects: e }) => {
9
- function i(t, n) {
10
- const o = Math.max(t.x, n.x), u = Math.min(t.x + t.width, n.x + n.width), c = Math.max(t.y, n.y), v = Math.min(t.y + t.height, n.y + n.height), m = u - o, x = v - c;
11
- if (o < u && c < v) {
12
- const p = m * x, g = t.width * t.height, y = n.width * n.height, L = p / (g + y - p);
13
- return Number(L.toFixed(4));
8
+ const se = ({ rect: o, rects: e }) => {
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);
13
+ return Number(b.toFixed(4));
14
14
  }
15
15
  return 0;
16
16
  }
17
- const r = [];
17
+ const i = [];
18
18
  for (const [t, n] of e)
19
- r.push({
19
+ i.push({
20
20
  id: t,
21
- value: i(s, n)
21
+ value: s(o, n)
22
22
  });
23
- return r.sort(_);
24
- }, j = ({ rect: s, rects: e }) => {
25
- function i(n, o) {
26
- return Math.sqrt((n.x - o.x) ** 2 + (n.y - o.y) ** 2);
23
+ return i.sort(Y);
24
+ }, G = ({ rect: o, rects: e }) => {
25
+ function s(n, l) {
26
+ return Math.sqrt((n.x - l.x) ** 2 + (n.y - l.y) ** 2);
27
27
  }
28
- function r(n) {
28
+ function i(n) {
29
29
  return {
30
30
  x: n.x + n.width / 2,
31
31
  y: n.y + n.height / 2
32
32
  };
33
33
  }
34
34
  const t = [];
35
- for (const [n, o] of e)
35
+ for (const [n, l] of e)
36
36
  t.push({
37
37
  id: n,
38
- value: i(r(o), r(s))
38
+ value: s(i(l), i(o))
39
39
  });
40
- return t.sort($);
40
+ return t.sort(q);
41
41
  };
42
- class M {
42
+ class _ {
43
43
  listeners = [];
44
44
  element;
45
45
  constructor(e) {
46
46
  this.element = e;
47
47
  }
48
- add(e, i) {
49
- this.element.addEventListener(e, i), this.listeners.push([e, i]);
48
+ add(e, s) {
49
+ this.element.addEventListener(e, s), this.listeners.push([e, s]);
50
50
  }
51
51
  removeAll() {
52
52
  this.listeners.forEach((e) => {
@@ -54,15 +54,15 @@ class M {
54
54
  });
55
55
  }
56
56
  }
57
- class z {
57
+ class K {
58
58
  document;
59
59
  listeners;
60
60
  documentListeners;
61
61
  options;
62
62
  windowListeners;
63
63
  constructor(e) {
64
- const { element: i } = e;
65
- this.options = e, this.document = i.ownerDocument, this.listeners = new M(i), this.documentListeners = new M(this.document), this.windowListeners = new M(this.document?.defaultView ?? window), this.handleStart = this.handleStart.bind(this), this.handleMove = this.handleMove.bind(this), this.handleEnd = this.handleEnd.bind(this), this.deactivate();
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();
66
66
  }
67
67
  deactivate() {
68
68
  this.listeners.removeAll(), this.documentListeners.removeAll(), this.windowListeners.removeAll(), this.listeners.add("pointerdown", this.handleStart);
@@ -89,180 +89,237 @@ class z {
89
89
  this.document?.getSelection()?.removeAllRanges();
90
90
  }
91
91
  }
92
- function B(s) {
92
+ function j(o) {
93
93
  const e = Symbol(
94
- `${s}Context`
94
+ `${o}Context`
95
95
  );
96
- return [i, r];
97
- function i(t) {
98
- const n = R(e, t);
99
- if (n || n === null) return n;
96
+ function s(t) {
97
+ const n = H(e, t);
98
+ if (n || n === null)
99
+ return n;
100
100
  throw new Error(
101
- `Injection \`${e.toString()}\` not found. Component must be used within \`${s}\``
101
+ `Injection \`${e.toString()}\` not found. Component must be used within \`${o}\``
102
102
  );
103
103
  }
104
- function r(t) {
105
- return A(e, t), t;
104
+ function i(t) {
105
+ return B(e, t), t;
106
106
  }
107
+ return [s, i];
107
108
  }
108
- function F(s, e) {
109
- return s.reduce((i, r) => {
110
- const t = e.get(r);
111
- return t && i.push(t), i;
109
+ function N(o, e) {
110
+ return o.reduce((s, i) => {
111
+ const t = e.get(i);
112
+ return t && s.push(t), s;
112
113
  }, []);
113
114
  }
114
- function I(s) {
115
- return s !== null && s >= 0;
115
+ function w(o) {
116
+ const e = z(o);
117
+ return e?.$el ?? e;
116
118
  }
117
- const [T, V] = B("DndContext");
118
- function X(s) {
119
+ function E(o) {
120
+ return o !== null && o >= 0;
121
+ }
122
+ const [U, J] = j("DndContext");
123
+ function P(o) {
119
124
  const {
120
125
  rects: e,
121
- activeIndex: i,
122
- overIndex: r,
126
+ activeIndex: s,
127
+ overIndex: i,
123
128
  index: t
124
- } = s, n = e[i];
129
+ } = o, n = e[s];
125
130
  if (!n)
126
131
  return null;
127
- if (t === i) {
128
- const u = e[r];
129
- return u ? {
132
+ if (t === s) {
133
+ const r = e[i];
134
+ return r ? {
130
135
  x: 0,
131
- y: i > r ? n.top - u.top : u.top - n.top
136
+ y: s > i ? n.top - r.top : r.top - n.top
132
137
  } : null;
133
138
  }
134
- const o = Y(e, t, i);
135
- return t > i && t <= r ? {
139
+ const l = Q(e, t, s);
140
+ return t > s && t <= i ? {
136
141
  x: 0,
137
- y: -n.height - o
138
- } : t < i && t >= r ? {
142
+ y: -n.height - l
143
+ } : t < s && t >= i ? {
139
144
  x: 0,
140
- y: n.height - o
145
+ y: n.height - l
141
146
  } : {
142
147
  x: 0,
143
148
  y: 0
144
149
  };
145
150
  }
146
- function Y(s, e, i) {
147
- const r = s[e], t = s[e - 1], n = s[e + 1];
148
- return !r || !t && !n ? 0 : i < 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;
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;
149
154
  }
150
- const K = /* @__PURE__ */ C({
151
- __name: "provider",
155
+ const oe = /* @__PURE__ */ L({
156
+ __name: "context",
152
157
  props: {
153
158
  items: {},
154
- collision: { type: Function, default: j },
159
+ collision: { type: Function, default: G },
155
160
  axis: {}
156
161
  },
157
162
  emits: ["end"],
158
- setup(s, { emit: e }) {
159
- const i = s, r = e, t = w(/* @__PURE__ */ new Map()), n = w(/* @__PURE__ */ new Map()), o = w(null), u = w(null), c = w({
163
+ 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({
160
165
  start: null,
161
166
  current: { x: 0, y: 0 }
162
- }), v = f(() => c.value.start ? {
163
- x: i.axis === "y" ? 0 : c.value.current.x - c.value.start.x,
164
- y: i.axis === "x" ? 0 : c.value.current.y - c.value.start.y
165
- } : null), m = f(() => o.value ? i.items.indexOf(o.value) : null), x = f(() => u.value ? i.items.indexOf(u.value) : null), p = f(() => F(i.items, n.value));
166
- function g() {
167
- for (const [l, a] of t.value)
168
- n.value.set(l, a.getBoundingClientRect());
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));
171
+ function S() {
172
+ for (const [a, d] of t.value)
173
+ n.value.set(a, d.getBoundingClientRect());
169
174
  }
170
- function y() {
171
- if (!o.value)
175
+ function b() {
176
+ if (!r.value)
172
177
  return null;
173
- const l = n.value.get(o.value), a = l && v.value ? {
174
- ...l,
175
- width: l.width,
176
- height: l.height,
177
- x: l.x + v.value?.x,
178
- y: l.y + v.value?.y
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
179
184
  } : null;
180
- return a ? i.collision({
181
- rect: a,
185
+ return d ? s.collision({
186
+ rect: d,
182
187
  rects: n.value
183
188
  })[0]?.id ?? null : null;
184
189
  }
185
- function L(l, a) {
186
- const d = t.value.get(l);
187
- if (d)
188
- return new z({
189
- element: a ?? d,
190
- onStart(h) {
191
- g(), c.value.start = h;
192
- },
193
- onMove(h) {
194
- c.value.start && (o.value || (o.value = l), c.value.current.x = h.x, c.value.current.y = h.y, u.value = y());
195
- },
196
- onEnd() {
197
- o.value && r("end", {
198
- active: o.value,
199
- over: u.value
200
- }), o.value = null, u.value = null, c.value.start = null;
201
- }
202
- });
190
+ function O(a, d) {
191
+ const v = t.value.get(a);
192
+ if (!v)
193
+ return;
194
+ const m = new K({
195
+ element: d ?? v,
196
+ onStart(R) {
197
+ S(), u.value.start = R;
198
+ },
199
+ 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());
201
+ },
202
+ 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;
207
+ }
208
+ });
209
+ l.value.set(a, m);
203
210
  }
204
- return S(t, g, { deep: !0 }), S([o, u], ([l]) => {
205
- if (!l) {
206
- for (const [d, h] of t.value)
207
- h.style.transition = "", h.style.position = "", h.style.zIndex = "", h.style.transform = "";
211
+ return C(t, S, { deep: !0 }), C([r, c], ([a]) => {
212
+ if (!a) {
213
+ for (const [v, m] of t.value)
214
+ m.style.transition = "", m.style.position = "", m.style.zIndex = "", m.style.transform = "";
208
215
  return;
209
216
  }
210
- const a = t.value.get(l);
211
- if (a) {
212
- a.style.position = "relative", a.style.zIndex = "1";
213
- for (const [d, h] of t.value)
214
- d !== l && (h.style.transition = "0.2s transform");
217
+ const d = t.value.get(a);
218
+ if (d) {
219
+ 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");
215
222
  }
216
- }), S(v, () => {
217
- if (!(!I(m.value) || !I(x.value)) && !(!o.value || !v.value))
218
- for (const [l, a] of t.value) {
219
- let d = null;
220
- l === o.value ? d = v.value : d = X({
221
- activeIndex: m.value,
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({
228
+ activeIndex: f.value,
222
229
  overIndex: x.value,
223
- rects: p.value,
224
- index: i.items.indexOf(l)
225
- }), d && (a.style.transform = `translate(${d.x}px, ${d.y}px)`);
230
+ rects: g.value,
231
+ index: s.items.indexOf(a)
232
+ }), v && (d.style.transform = `translate(${v.x}px, ${v.y}px)`);
226
233
  }
227
- }), V({
234
+ }), J({
228
235
  rects: n,
229
- activate: L,
236
+ activate: O,
230
237
  elements: t,
231
- translate: v,
232
- active: o,
233
- over: u,
234
- activeIndex: m,
238
+ translate: h,
239
+ active: r,
240
+ over: c,
241
+ activeIndex: f,
235
242
  overIndex: x,
236
- items: f(() => i.items),
237
- sortedRects: p
238
- }), (l, a) => b(l.$slots, "default");
243
+ items: y(() => s.items),
244
+ sortedRects: g
245
+ }), (a, d) => M(a.$slots, "default");
239
246
  }
240
- });
241
- function N(s) {
242
- const { active: e, elements: i, activate: r } = T(), { id: t, element: n, handleElement: o } = s;
243
- return D(() => {
244
- const c = E(n);
245
- c && (i.value.set(t, c), r(t, E(o)));
246
- }), O(() => {
247
- i.value.delete(t);
247
+ }), [
248
+ W,
249
+ Z
250
+ ] = j("SortableContext");
251
+ 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(() => {
254
+ const f = w(n);
255
+ f && s.value.set(t, f);
256
+ }), k(() => {
257
+ s.value.delete(t);
258
+ }), F(() => {
259
+ n && (c.value = w(n)), l && (r.value = w(l)), i(t, r.value);
260
+ });
261
+ const u = y(() => e.value === t), h = (f) => {
262
+ r.value = w(f);
263
+ };
264
+ return Z({
265
+ setHandleRef: h
248
266
  }), {
249
- isDragging: f(() => e.value === t)
267
+ isDragging: u,
268
+ elementRef: c,
269
+ setHandleRef: h
250
270
  };
251
271
  }
252
- function P(s, e) {
253
- const { active: i, over: r } = e;
254
- if (!r)
255
- return s;
256
- const t = s.indexOf(i), n = s.indexOf(r);
272
+ function te() {
273
+ return W().setHandleRef;
274
+ }
275
+ const ie = /* @__PURE__ */ L({
276
+ __name: "handle",
277
+ props: {
278
+ as: { default: "button" }
279
+ },
280
+ setup(o) {
281
+ const e = te();
282
+ return (s, i) => (A(), I($(s.as), { ref: V(e) }, {
283
+ default: D(() => [
284
+ M(s.$slots, "default")
285
+ ]),
286
+ _: 3
287
+ }, 512));
288
+ }
289
+ }), re = /* @__PURE__ */ L({
290
+ __name: "sortable",
291
+ props: {
292
+ as: { default: "div" },
293
+ id: {}
294
+ },
295
+ setup(o) {
296
+ const e = X("el");
297
+ return ee({ id: o.id, element: e }), (s, i) => (A(), I($(s.as), { ref: "el" }, {
298
+ default: D(() => [
299
+ M(s.$slots, "default")
300
+ ]),
301
+ _: 3
302
+ }, 512));
303
+ }
304
+ });
305
+ function le(o, e) {
306
+ const { active: s, over: i } = e;
307
+ if (!i)
308
+ return o;
309
+ const t = o.indexOf(s), n = o.indexOf(i);
257
310
  if (t === n)
258
- return s;
259
- const o = s.slice(), u = o.splice(t, 1)[0];
260
- return u && o.splice(n, 0, u), o;
311
+ return o;
312
+ const l = o.slice(), r = l.splice(t, 1)[0];
313
+ return r && l.splice(n, 0, r), l;
261
314
  }
262
315
  export {
263
- K as DndProvider,
264
- P as arrayMove,
265
- j as closestToCenter,
266
- G as rectangleIntersection,
267
- N as useSortable
316
+ oe as SortableContext,
317
+ ie as SortableHandle,
318
+ re as SortableItem,
319
+ le as arrayMove,
320
+ G as closestToCenter,
321
+ W as injectSortableContext,
322
+ se as rectangleIntersection,
323
+ te as useSetHandleRef,
324
+ ee as useSortable
268
325
  };
@@ -1 +1 @@
1
- (function(h,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],l):(h=typeof globalThis<"u"?globalThis:h||self,l(h.VueDnD={},h.Vue))})(this,function(h,l){"use strict";function I(o,e){return e.value-o.value}function b(o,e){return o.value-e.value}const C=({rect:o,rects:e})=>{function i(t,n){const s=Math.max(t.x,n.x),u=Math.min(t.x+t.width,n.x+n.width),a=Math.max(t.y,n.y),m=Math.min(t.y+t.height,n.y+n.height),p=u-s,x=m-a;if(s<u&&a<m){const w=p*x,y=t.width*t.height,S=n.width*n.height,L=w/(y+S-w);return Number(L.toFixed(4))}return 0}const r=[];for(const[t,n]of e)r.push({id:t,value:i(o,n)});return r.sort(I)},M=({rect:o,rects:e})=>{function i(n,s){return Math.sqrt((n.x-s.x)**2+(n.y-s.y)**2)}function r(n){return{x:n.x+n.width/2,y:n.y+n.height/2}}const t=[];for(const[n,s]of e)t.push({id:n,value:i(r(s),r(o))});return t.sort(b)};class g{listeners=[];element;constructor(e){this.element=e}add(e,i){this.element.addEventListener(e,i),this.listeners.push([e,i])}removeAll(){this.listeners.forEach(e=>{this.element.removeEventListener(...e)})}}class R{document;listeners;documentListeners;options;windowListeners;constructor(e){const{element:i}=e;this.options=e,this.document=i.ownerDocument,this.listeners=new g(i),this.documentListeners=new g(this.document),this.windowListeners=new g(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 A(o){const e=Symbol(`${o}Context`);return[i,r];function i(t){const n=l.inject(e,t);if(n||n===null)return n;throw new Error(`Injection \`${e.toString()}\` not found. Component must be used within \`${o}\``)}function r(t){return l.provide(e,t),t}}function D(o,e){return o.reduce((i,r)=>{const t=e.get(r);return t&&i.push(t),i},[])}function E(o){return o!==null&&o>=0}const[j,O]=A("DndContext");function T(o){const{rects:e,activeIndex:i,overIndex:r,index:t}=o,n=e[i];if(!n)return null;if(t===i){const u=e[r];return u?{x:0,y:i>r?n.top-u.top:u.top-n.top}:null}const s=_(e,t,i);return t>i&&t<=r?{x:0,y:-n.height-s}:t<i&&t>=r?{x:0,y:n.height-s}:{x:0,y:0}}function _(o,e,i){const r=o[e],t=o[e-1],n=o[e+1];return!r||!t&&!n?0:i<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 $=l.defineComponent({__name:"provider",props:{items:{},collision:{type:Function,default:M},axis:{}},emits:["end"],setup(o,{emit:e}){const i=o,r=e,t=l.ref(new Map),n=l.ref(new Map),s=l.ref(null),u=l.ref(null),a=l.ref({start:null,current:{x:0,y:0}}),m=l.computed(()=>a.value.start?{x:i.axis==="y"?0:a.value.current.x-a.value.start.x,y:i.axis==="x"?0:a.value.current.y-a.value.start.y}:null),p=l.computed(()=>s.value?i.items.indexOf(s.value):null),x=l.computed(()=>u.value?i.items.indexOf(u.value):null),w=l.computed(()=>D(i.items,n.value));function y(){for(const[c,d]of t.value)n.value.set(c,d.getBoundingClientRect())}function S(){if(!s.value)return null;const c=n.value.get(s.value),d=c&&m.value?{...c,width:c.width,height:c.height,x:c.x+m.value?.x,y:c.y+m.value?.y}:null;return d?i.collision({rect:d,rects:n.value})[0]?.id??null:null}function L(c,d){const f=t.value.get(c);if(f)return new R({element:d??f,onStart(v){y(),a.value.start=v},onMove(v){a.value.start&&(s.value||(s.value=c),a.value.current.x=v.x,a.value.current.y=v.y,u.value=S())},onEnd(){s.value&&r("end",{active:s.value,over:u.value}),s.value=null,u.value=null,a.value.start=null}})}return l.watch(t,y,{deep:!0}),l.watch([s,u],([c])=>{if(!c){for(const[f,v]of t.value)v.style.transition="",v.style.position="",v.style.zIndex="",v.style.transform="";return}const d=t.value.get(c);if(d){d.style.position="relative",d.style.zIndex="1";for(const[f,v]of t.value)f!==c&&(v.style.transition="0.2s transform")}}),l.watch(m,()=>{if(!(!E(p.value)||!E(x.value))&&!(!s.value||!m.value))for(const[c,d]of t.value){let f=null;c===s.value?f=m.value:f=T({activeIndex:p.value,overIndex:x.value,rects:w.value,index:i.items.indexOf(c)}),f&&(d.style.transform=`translate(${f.x}px, ${f.y}px)`)}}),O({rects:n,activate:L,elements:t,translate:m,active:s,over:u,activeIndex:p,overIndex:x,items:l.computed(()=>i.items),sortedRects:w}),(c,d)=>l.renderSlot(c.$slots,"default")}});function V(o){const{active:e,elements:i,activate:r}=j(),{id:t,element:n,handleElement:s}=o;return l.onMounted(()=>{const a=l.toValue(n);a&&(i.value.set(t,a),r(t,l.toValue(s)))}),l.onUnmounted(()=>{i.value.delete(t)}),{isDragging:l.computed(()=>e.value===t)}}function z(o,e){const{active:i,over:r}=e;if(!r)return o;const t=o.indexOf(i),n=o.indexOf(r);if(t===n)return o;const s=o.slice(),u=s.splice(t,1)[0];return u&&s.splice(n,0,u),s}h.DndProvider=$,h.arrayMove=z,h.closestToCenter=M,h.rectangleIntersection=C,h.useSortable=V,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-dnd-sortable",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Simple sortable for vue",
5
5
  "type": "module",
6
6
  "main": "dist/vue-dnd.umd.js",