vue-dnd-sortable 0.1.7 → 0.1.9

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
@@ -60,7 +60,7 @@ const items = ref([1, 2, 3, 4, 5, 6])
60
60
 
61
61
  <template>
62
62
  <SortableContext
63
- axis="x"
63
+ lock-axis="x"
64
64
  :items
65
65
  :strategy="horizontalListSortingStrategy"
66
66
  @end="items = arrayMove(items, $event)">
@@ -82,6 +82,41 @@ const items = ref([1, 2, 3, 4, 5, 6])
82
82
  </template>
83
83
  ```
84
84
 
85
+ ```vue
86
+ <script setup lang="ts">
87
+ import { useTemplateRef } from 'vue'
88
+ import { useSortable } from 'vue-dnd-sortable'
89
+
90
+ const { id } = defineProps<{
91
+ id: number
92
+ }>()
93
+
94
+ const element = useTemplateRef('el')
95
+
96
+ useSortable({ id, element })
97
+ </script>
98
+
99
+ <template>
100
+ <div ref="el">
101
+ <slot />
102
+ </div>
103
+ </template>
104
+ ```
105
+
106
+ ```vue
107
+ <script setup lang="ts">
108
+ import { useSetHandleRef } from 'vue-dnd-sortable'
109
+
110
+ const setHandleRef = useSetHandleRef()
111
+ </script>
112
+
113
+ <template>
114
+ <button :ref="setHandleRef">
115
+ handle
116
+ </button>
117
+ </template>
118
+ ```
119
+
85
120
  ---
86
121
 
87
122
  ## License
@@ -12,7 +12,7 @@ declare const injectSortableContext: <T extends SortableContext | null | undefin
12
12
  export { injectSortableContext };
13
13
  export declare function useSortable(options: SortableOptions): {
14
14
  isDragging: import("vue").ComputedRef<boolean>;
15
- elementRef: import("vue").Ref<Element | HTMLElement | SVGElement | null | undefined, Element | HTMLElement | SVGElement | null | undefined>;
15
+ setElementRef: (el: MaybeComputedElementRef) => void;
16
16
  setHandleRef: (el: MaybeComputedElementRef) => void;
17
17
  };
18
18
  export declare function useSetHandleRef(): (el: MaybeComputedElementRef) => void;
@@ -1,4 +1,4 @@
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";
1
+ import { inject as z, provide as H, toValue as B, defineComponent as _, ref as x, computed as y, watch as C, renderSlot as E, onMounted as F, onUnmounted as T, watchEffect as N, createBlock as M, openBlock as A, 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,9 +7,9 @@ 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), 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;
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), h = i - l, p = c - a;
11
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);
12
+ const d = h * p, g = t.width * t.height, S = n.width * n.height, b = d / (g + S - d);
13
13
  return Number(b.toFixed(4));
14
14
  }
15
15
  return 0;
@@ -116,7 +116,7 @@ function w(o) {
116
116
  const e = B(o);
117
117
  return e?.$el ?? e;
118
118
  }
119
- function M(o) {
119
+ function I(o) {
120
120
  return o !== null && o >= 0;
121
121
  }
122
122
  const [J, P] = j("DndContext"), Q = (o) => {
@@ -191,35 +191,35 @@ const oe = (o) => {
191
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({
192
192
  start: null,
193
193
  current: { x: 0, y: 0 }
194
- }), v = y(() => c.value.start ? {
194
+ }), h = y(() => c.value.start ? {
195
195
  x: s.lockAxis === "y" ? 0 : c.value.current.x - c.value.start.x,
196
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));
197
+ } : null), p = y(() => i.value ? s.items.indexOf(i.value) : null), d = y(() => a.value ? s.items.indexOf(a.value) : null), g = y(() => U(s.items, n.value));
198
198
  function S() {
199
- for (const [u, d] of t.value)
200
- n.value.set(u, d.getBoundingClientRect());
199
+ for (const [u, v] of t.value)
200
+ n.value.set(u, v.getBoundingClientRect());
201
201
  }
202
202
  function b() {
203
203
  if (!i.value)
204
204
  return null;
205
- const u = n.value.get(i.value), d = u && v.value ? {
205
+ const u = n.value.get(i.value), v = u && h.value ? {
206
206
  ...u,
207
207
  width: u.width,
208
208
  height: u.height,
209
- x: u.x + v.value?.x,
210
- y: u.y + v.value?.y
209
+ x: u.x + h.value?.x,
210
+ y: u.y + h.value?.y
211
211
  } : null;
212
- return d ? s.collision({
213
- rect: d,
212
+ return v ? s.collision({
213
+ rect: v,
214
214
  rects: n.value
215
215
  })[0]?.id ?? null : null;
216
216
  }
217
- function k(u, d) {
218
- const h = t.value.get(u);
219
- if (!h)
217
+ function k(u, v) {
218
+ const f = t.value.get(u);
219
+ if (!f)
220
220
  return;
221
221
  const m = new K({
222
- element: d ?? h,
222
+ element: v ?? f,
223
223
  onStart(R) {
224
224
  S(), c.value.start = R;
225
225
  },
@@ -237,39 +237,39 @@ const oe = (o) => {
237
237
  }
238
238
  return C(t, S, { deep: !0 }), C([i, a], ([u]) => {
239
239
  if (!u) {
240
- for (const [h, m] of t.value)
240
+ for (const [f, m] of t.value)
241
241
  m.style.transition = "", m.style.position = "", m.style.zIndex = "", m.style.transform = "";
242
242
  return;
243
243
  }
244
- const d = t.value.get(u);
245
- if (d) {
246
- d.style.position = "relative", d.style.zIndex = "1";
247
- for (const [h, m] of t.value)
248
- h !== u && (m.style.transition = "0.2s transform");
244
+ const v = t.value.get(u);
245
+ if (v) {
246
+ v.style.position = "relative", v.style.zIndex = "1";
247
+ for (const [f, m] of t.value)
248
+ f !== u && (m.style.transition = "0.2s transform");
249
249
  }
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({
255
- activeIndex: f.value,
256
- overIndex: p.value,
250
+ }), C(h, () => {
251
+ if (!(!I(p.value) || !I(d.value)) && !(!i.value || !h.value))
252
+ for (const [u, v] of t.value) {
253
+ let f = null;
254
+ u === i.value ? f = h.value : f = s.strategy({
255
+ activeIndex: p.value,
256
+ overIndex: d.value,
257
257
  rects: g.value,
258
258
  index: s.items.indexOf(u)
259
- }), h && (d.style.transform = `translate(${h.x}px, ${h.y}px)`);
259
+ }), f && (v.style.transform = `translate(${f.x}px, ${f.y}px)`);
260
260
  }
261
261
  }), P({
262
262
  rects: n,
263
263
  activate: k,
264
264
  elements: t,
265
- translate: v,
265
+ translate: h,
266
266
  active: i,
267
267
  over: a,
268
- activeIndex: f,
269
- overIndex: p,
268
+ activeIndex: p,
269
+ overIndex: d,
270
270
  items: y(() => s.items),
271
271
  sortedRects: g
272
- }), (u, d) => I(u.$slots, "default");
272
+ }), (u, v) => E(u.$slots, "default");
273
273
  }
274
274
  }), [
275
275
  W,
@@ -278,22 +278,24 @@ const oe = (o) => {
278
278
  function ee(o) {
279
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
280
  F(() => {
281
- const f = w(n);
282
- f && s.value.set(t, f);
281
+ const d = w(n);
282
+ d && s.value.set(t, d);
283
283
  }), T(() => {
284
284
  s.value.delete(t);
285
285
  }), N(() => {
286
286
  n && (a.value = w(n)), l && (i.value = w(l)), r(t, i.value);
287
287
  });
288
- const c = y(() => e.value === t), v = (f) => {
289
- i.value = w(f);
288
+ const c = y(() => e.value === t), h = (d) => {
289
+ i.value = w(d);
290
+ }, p = (d) => {
291
+ a.value = w(d);
290
292
  };
291
293
  return Z({
292
- setHandleRef: v
294
+ setHandleRef: h
293
295
  }), {
294
296
  isDragging: c,
295
- elementRef: a,
296
- setHandleRef: v
297
+ setElementRef: p,
298
+ setHandleRef: h
297
299
  };
298
300
  }
299
301
  function te() {
@@ -306,9 +308,9 @@ const re = /* @__PURE__ */ _({
306
308
  },
307
309
  setup(o) {
308
310
  const e = te();
309
- return (s, r) => (E(), A($(s.as), { ref: V(e) }, {
311
+ return (s, r) => (A(), M($(s.as), { ref: V(e) }, {
310
312
  default: D(() => [
311
- I(s.$slots, "default")
313
+ E(s.$slots, "default")
312
314
  ]),
313
315
  _: 3
314
316
  }, 512));
@@ -321,9 +323,9 @@ const re = /* @__PURE__ */ _({
321
323
  },
322
324
  setup(o) {
323
325
  const e = X("el");
324
- return ee({ id: o.id, element: e }), (s, r) => (E(), A($(s.as), { ref: "el" }, {
326
+ return ee({ id: o.id, element: e }), (s, r) => (A(), M($(s.as), { ref: "el" }, {
325
327
  default: D(() => [
326
- I(s.$slots, "default")
328
+ E(s.$slots, "default")
327
329
  ]),
328
330
  _: 3
329
331
  }, 512));
@@ -1 +1 @@
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"})});
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),m=r-c,w=f-a;if(c<r&&a<f){const h=m*w,g=e.width*e.height,S=n.width*n.height,b=h/(g+S-h);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=E(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 E(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=E(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}}),m=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),w=i.computed(()=>r.value?o.items.indexOf(r.value):null),h=i.computed(()=>a.value?o.items.indexOf(a.value):null),g=i.computed(()=>z(o.items,n.value));function S(){for(const[u,v]of e.value)n.value.set(u,v.getBoundingClientRect())}function b(){if(!r.value)return null;const u=n.value.get(r.value),v=u&&m.value?{...u,width:u.width,height:u.height,x:u.x+m.value?.x,y:u.y+m.value?.y}:null;return v?o.collision({rect:v,rects:n.value})[0]?.id??null:null}function G(u,v){const p=e.value.get(u);if(!p)return;const x=new T({element:v??p,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[p,x]of e.value)x.style.transition="",x.style.position="",x.style.zIndex="",x.style.transform="";return}const v=e.value.get(u);if(v){v.style.position="relative",v.style.zIndex="1";for(const[p,x]of e.value)p!==u&&(x.style.transition="0.2s transform")}}),i.watch(m,()=>{if(!(!_(w.value)||!_(h.value))&&!(!r.value||!m.value))for(const[u,v]of e.value){let p=null;u===r.value?p=m.value:p=o.strategy({activeIndex:w.value,overIndex:h.value,rects:g.value,index:o.items.indexOf(u)}),p&&(v.style.transform=`translate(${p.x}px, ${p.y}px)`)}}),H({rects:n,activate:G,elements:e,translate:m,active:r,over:a,activeIndex:w,overIndex:h,items:i.computed(()=>o.items),sortedRects:g}),(u,v)=>i.renderSlot(u.$slots,"default")}}),[A,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 h=y(n);h&&o.value.set(e,h)}),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),m=h=>{r.value=y(h)},w=h=>{a.value=y(h)};return q({setHandleRef:m}),{isDragging:f,setElementRef:w,setHandleRef:m}}function $(){return A().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=A,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.7",
3
+ "version": "0.1.9",
4
4
  "description": "Simple sortable for vue",
5
5
  "type": "module",
6
6
  "main": "dist/vue-dnd.umd.js",