@ptahjs/dnd 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/dnd.js +116 -72
  2. package/package.json +7 -3
package/dist/dnd.js CHANGED
@@ -2,18 +2,31 @@
2
2
  var e = class {
3
3
  #e = /* @__PURE__ */ new Map();
4
4
  emit(e, t) {
5
+ let n = this.#e.get(e);
6
+ if (!n || n.length === 0) return t;
7
+ let r = n.slice();
8
+ for (let n of r) try {
9
+ n(t);
10
+ } catch (t) {
11
+ console.error(`[EventDispatcher] handler error for "${e}":`, t);
12
+ }
13
+ return t;
14
+ }
15
+ pipe(e, t) {
5
16
  let n = this.#e.get(e);
6
17
  if (!n || n.length === 0) return t;
7
18
  let r = t;
8
- for (let e of n) {
9
- let t = e(r);
10
- t !== void 0 && (r = t);
19
+ for (let t of n.slice()) try {
20
+ let e = t(r);
21
+ e !== void 0 && (r = e);
22
+ } catch (t) {
23
+ console.error(`[EventDispatcher] pipe handler error for "${e}":`, t);
11
24
  }
12
25
  return r;
13
26
  }
14
27
  on(e, t) {
15
28
  let n = this.#e.get(e);
16
- return n ? n.push(t) : this.#e.set(e, [t]), this;
29
+ return n ? n.includes(t) || n.push(t) : this.#e.set(e, [t]), this;
17
30
  }
18
31
  off(e, t) {
19
32
  let n = this.#e.get(e);
@@ -66,29 +79,30 @@ var e = class {
66
79
  };
67
80
  //#endregion
68
81
  //#region src/utils/computeDropRegion.js
69
- function s(e) {
70
- if (e?.cache?.dropRegionFns) return e.cache.dropRegionFns;
71
- let t = () => {
82
+ function s(e, t) {
83
+ let n = `dropRegionFns_${(t instanceof Set ? Array.from(t).sort().join(",") : t) || "all"}`;
84
+ if (e?.cache?.[n]) return e.cache[n];
85
+ let a = () => {
72
86
  if (e.currentDrop) return e.currentDropRect || void 0;
73
- }, n = {
74
- isOverBottom: () => {
75
- let n = t();
76
- return n ? e.y > n.y + n.height * (1 - r) : !1;
77
- },
78
- isOverLeft: () => {
79
- let n = t();
80
- return n ? e.x < n.x + n.width * i : !1;
81
- },
82
- isOverRight: () => {
83
- let n = t();
84
- return n ? e.x > n.x + n.width * (1 - i) : !1;
85
- },
86
- isOverTop: () => {
87
- let n = t();
88
- return n ? e.y < n.y + n.height * r : !1;
89
- }
87
+ }, o = !t || t === "all" || t instanceof Set && (t.has("top") || t.has("bottom")), s = !t || t === "all" || t instanceof Set && (t.has("left") || t.has("right")), c = {
88
+ isOverTop: o ? () => {
89
+ let t = a();
90
+ return t ? e.y < t.y + t.height * r : !1;
91
+ } : () => !1,
92
+ isOverBottom: o ? () => {
93
+ let t = a();
94
+ return t ? e.y > t.y + t.height * (1 - r) : !1;
95
+ } : () => !1,
96
+ isOverLeft: s ? () => {
97
+ let t = a();
98
+ return t ? e.x < t.x + t.width * i : !1;
99
+ } : () => !1,
100
+ isOverRight: s ? () => {
101
+ let t = a();
102
+ return t ? e.x > t.x + t.width * (1 - i) : !1;
103
+ } : () => !1
90
104
  };
91
- return e?.cache && (e.cache.dropRegionFns = n), n;
105
+ return e?.cache && (e.cache[n] = c), c;
92
106
  }
93
107
  //#endregion
94
108
  //#region src/utils/parseData.js
@@ -354,8 +368,8 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
354
368
  let e = !1;
355
369
  try {
356
370
  e = !!this.#r(performance.now());
357
- } catch {
358
- e = !1;
371
+ } catch (t) {
372
+ e = !1, console.error("[RafScheduler] frame callback error:", t);
359
373
  }
360
374
  if (this.#e) {
361
375
  if (e || this.#t) {
@@ -426,11 +440,13 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
426
440
  }), e.preventDefault();
427
441
  };
428
442
  #p = (e) => {
429
- !this.#n || !this.#c || "pointerId" in e && e.pointerId !== this.#r || (this.#c({
443
+ if (!this.#n || !this.#c || "pointerId" in e && e.pointerId !== this.#r) return;
444
+ let t = this.#r, n = e.type;
445
+ this.#d(), this.#c({
430
446
  event: e,
431
- pointerId: this.#r,
432
- reason: e.type
433
- }), this.#d(), e.preventDefault?.());
447
+ pointerId: t,
448
+ reason: n
449
+ }), e.preventDefault?.();
434
450
  };
435
451
  }, ee = class {
436
452
  scrolled = !1;
@@ -507,6 +523,9 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
507
523
  onDestroy(e, t) {
508
524
  for (let n of this.#e) n.onDestroy?.(e, t);
509
525
  }
526
+ dispose() {
527
+ this.#e.length = 0;
528
+ }
510
529
  }, ne = class {
511
530
  #e;
512
531
  #t;
@@ -573,11 +592,15 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
573
592
  started: this.#t.started
574
593
  };
575
594
  }
576
- case "END": return !this.#t.active || e.pointerId !== void 0 && e.pointerId !== this.#t.pointerId ? { handled: !1 } : {
577
- handled: !0,
578
- ended: !!this.#t.started,
579
- reason: e.reason
580
- };
595
+ case "END": {
596
+ if (!this.#t.active || e.pointerId !== void 0 && e.pointerId !== this.#t.pointerId) return { handled: !1 };
597
+ let t = !!this.#t.started;
598
+ return this.#t.active = !1, {
599
+ handled: !0,
600
+ ended: t,
601
+ reason: e.reason
602
+ };
603
+ }
581
604
  default: return { handled: !1 };
582
605
  }
583
606
  }
@@ -596,13 +619,17 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
596
619
  #s = new te();
597
620
  #c;
598
621
  #l;
622
+ #u = !1;
599
623
  canDrop = () => !0;
600
624
  renderMirror = void 0;
601
625
  get root() {
602
626
  return this.#t;
603
627
  }
604
628
  get monitor() {
605
- return this.#o?.session;
629
+ if (!this.#u) return this.#o?.session;
630
+ }
631
+ get destroyed() {
632
+ return this.#u;
606
633
  }
607
634
  constructor(e = {}) {
608
635
  super(), this.#n = { threshold: e.threshold ?? a.DRAG_START };
@@ -610,10 +637,10 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
610
637
  t.threshold = this.#n.threshold, t.adapter = this.#r, this.#o = new ne({
611
638
  store: this.#a,
612
639
  session: t
613
- }), this.#c = new y(this.#m), this.#l = new b({
614
- onDown: this.#d,
615
- onMove: this.#f,
616
- onUp: this.#p
640
+ }), this.#c = new y(this.#h), this.#l = new b({
641
+ onDown: this.#f,
642
+ onMove: this.#p,
643
+ onUp: this.#m
617
644
  }), this.setRoot(e.root);
618
645
  }
619
646
  use(e) {
@@ -623,13 +650,14 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
623
650
  let t = this.#r.resolveRoot(e);
624
651
  if (t === this.#t) return;
625
652
  let n = this.#t;
626
- this.#e.abort(), this.#e = new AbortController(), this.#t = t, this.#l.setRoot(this.#t), this.#t && (this.#s.onRootChange(this.#t, n, this.#e.signal), this.#t.addEventListener("click", this.#u, {
653
+ this.#e.abort(), this.#e = new AbortController(), this.#t = t, this.#l.setRoot(this.#t), this.#t && (this.#s.onRootChange(this.#t, n, this.#e.signal), this.#t.addEventListener("click", this.#d, {
627
654
  capture: !0,
628
655
  passive: !1,
629
656
  signal: this.#e.signal
630
657
  }));
631
658
  }
632
659
  destroy() {
660
+ if (this.#u) return;
633
661
  this.#e.abort(), this.#l.destroy(), this.#c.cancel();
634
662
  let e = this.#o.session;
635
663
  e?.active && (this.#s.onEnd(d({
@@ -641,22 +669,20 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
641
669
  }), {
642
670
  ended: !1,
643
671
  reason: "destroy"
644
- }), e.dragAbort?.abort()), this.offAll(), this.#s.onDestroy(this, e);
645
- let t = this.#o.resetSession({ ended: !1 });
646
- t.adapter = this.#r;
672
+ }), e.dragAbort?.abort()), this.#s.onDestroy(this, e), this.#s.dispose?.(), this.offAll(), this.#u = !0;
647
673
  }
648
- #u = (e) => {
674
+ #d = (e) => {
649
675
  let t = this.#o?.session;
650
676
  if (!t?.suppressNextClick) return;
651
677
  let n = performance.now(), r = !t.lastDragTs || n - t.lastDragTs < 800;
652
678
  t.suppressNextClick = !1, r && (e.preventDefault?.(), e.stopPropagation?.(), e.stopImmediatePropagation?.());
653
679
  };
654
- #d = (e) => {
680
+ #f = (e) => {
655
681
  if (!this.#t || e.button !== 0) return { accepted: !1 };
656
682
  let n = p(e.target, this.#r);
657
683
  if (!n) return { accepted: !1 };
658
684
  let r = this.#r.closestDraggable(n);
659
- if (!r) return { accepted: !1 };
685
+ if (!r || e.target?.closest?.("input, textarea, select, button, a[href], [contenteditable]")) return { accepted: !1 };
660
686
  e.preventDefault(), e.target?.setPointerCapture?.(e.pointerId);
661
687
  let i = this.#r.getNamespace(r), a = this.#r.closestDrop(r), o = r.getBoundingClientRect(), s = c(this.#r.getAttr(r, t.dataAttr));
662
688
  if (!this.#o.dispatch({
@@ -689,7 +715,7 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
689
715
  signal: l.dragAbort.signal
690
716
  };
691
717
  };
692
- #f = ({ pointerId: e, x: t, y: n }) => {
718
+ #p = ({ pointerId: e, x: t, y: n }) => {
693
719
  let r = this.#o.dispatch({
694
720
  type: "MOVE",
695
721
  pointerId: e,
@@ -710,7 +736,7 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
710
736
  }
711
737
  r.started && this.#c.request();
712
738
  };
713
- #p = ({ event: e, pointerId: t, reason: n }) => {
739
+ #m = ({ event: e, pointerId: t, reason: n }) => {
714
740
  let r = this.#o.dispatch({
715
741
  type: "END",
716
742
  pointerId: t,
@@ -737,7 +763,7 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
737
763
  let c = this.#o.resetSession({ ended: a });
738
764
  c.adapter = this.#r, a ? this.emit("drop", s) : this.emit("cancel", s), e.preventDefault?.();
739
765
  };
740
- #m = (e) => {
766
+ #h = (e) => {
741
767
  let t = this.#o.session;
742
768
  if (!t.active || !t.started) return !1;
743
769
  this.#i.reset(e), this.#r.beginFrame(), t.cache.frameId = this.#r.frameId;
@@ -809,7 +835,7 @@ var g = t.dragScopeAttr, _ = `[${g}], [data-${g}]`, v = class {
809
835
  width: `${i.width}px`,
810
836
  height: `${i.height}px`,
811
837
  position: "fixed"
812
- }), document.body.appendChild(a), r.mirrorEl = a);
838
+ }), (e.dnd?.root || document.body).appendChild(a), r.mirrorEl = a);
813
839
  };
814
840
  onCommit = (e) => {
815
841
  let t = e?.session;
@@ -898,12 +924,13 @@ function A(e, t, n, r) {
898
924
  }
899
925
  function ae(e) {
900
926
  if (!m(e)) return;
901
- let n = t?.dropIndicatorAttr || "drop-indicator", r = `data-${n}`, i;
927
+ let n = t.dropIndicatorAttr || "drop-indicator", r = `data-${n}`, i;
902
928
  if (e.hasAttribute?.(n)) i = e.getAttribute?.(n);
903
929
  else if (e.hasAttribute?.(r)) i = e.getAttribute?.(r);
904
930
  else return;
905
931
  let a = (i ?? "").trim();
906
932
  if (!a) return "all";
933
+ console.log("raw", i);
907
934
  let o = /* @__PURE__ */ new Set();
908
935
  for (let e of a.split(/[,\s]+/)) {
909
936
  let t = (e || "").trim().toLowerCase();
@@ -917,13 +944,13 @@ function oe(e) {
917
944
  let a = t.currentDrop || e.dnd?.root || r.body;
918
945
  return m(a) && i.parentNode !== a && a.appendChild(i), i;
919
946
  }
920
- function se(e) {
947
+ function se(e, t) {
921
948
  if (!e.currentDrop || !e.currentDropRect) return;
922
- let t = s(e);
923
- if (t.isOverLeft()) return "left";
924
- if (t.isOverRight()) return "right";
925
- if (t.isOverTop()) return "top";
926
- if (t.isOverBottom()) return "bottom";
949
+ let n = s(e, t);
950
+ if (n.isOverLeft()) return "left";
951
+ if (n.isOverRight()) return "right";
952
+ if (n.isOverTop()) return "top";
953
+ if (n.isOverBottom()) return "bottom";
927
954
  }
928
955
  function ce(e) {
929
956
  let t = e.session;
@@ -944,7 +971,7 @@ var le = class {
944
971
  t.indicatorRegion = void 0;
945
972
  return;
946
973
  }
947
- let a = oe(e), o = ce(e), s = se(t), c = i === "all" || s && i.has(s) ? s : void 0, l = !!(r && t.currentDropRect && o && c);
974
+ let a = oe(e), o = ce(e), s = se(t, i), c = i === "all" || s && i.has(s) ? s : void 0, l = !!(r && t.currentDropRect && o && c);
948
975
  if (A(e, a, n.dropIndicatorActive, l), t.indicatorRegion = l ? c : void 0, l) for (let [t, n] of Object.entries(O)) A(e, a, n, t === c);
949
976
  else for (let t of k) A(e, a, t, !1);
950
977
  }
@@ -1085,20 +1112,21 @@ function H(e, t) {
1085
1112
  var U = class {
1086
1113
  order = -40;
1087
1114
  #e;
1115
+ #t;
1088
1116
  onAttach(e) {
1089
1117
  this.#e = e;
1090
1118
  }
1091
1119
  onRootChange(e, t, n) {
1092
- m(e) && e.addEventListener("pointerdown", (e) => {
1120
+ this.#t = n, m(e) && e.addEventListener("pointerdown", (e) => {
1093
1121
  if (e.button !== 0 || h(e.target)) return;
1094
1122
  let t = e.target.closest(_);
1095
- t && (this.#e.monitor.adapter.closestDraggable(e.target) || this.#t(t, e));
1123
+ t && (this.#e.monitor.adapter.closestDraggable(e.target) || this.#n(t, e));
1096
1124
  }, {
1097
1125
  capture: !0,
1098
1126
  signal: n
1099
1127
  });
1100
1128
  }
1101
- #t(e, r) {
1129
+ #n(e, r) {
1102
1130
  let i = r.clientX, o = r.clientY, s = this.#e.monitor.adapter.getNamespace(e), c = parseFloat(e.getAttribute(t.scaleRatioAttr) || "1"), l = document.createElement("div");
1103
1131
  l.classList.add(n.marquee), e.appendChild(l);
1104
1132
  let u = !1, d = (t) => {
@@ -1106,22 +1134,38 @@ var U = class {
1106
1134
  if (!u && Math.abs(n) < a.MARQUEE_MOVE && Math.abs(r) < a.MARQUEE_MOVE) return;
1107
1135
  u = !0;
1108
1136
  let d = V(i, o, t.clientX, t.clientY);
1109
- this.#n(l, d, e, c);
1110
- let f = this.#r(e, s, d), p = this.#e.monitor.selectedByNs;
1137
+ this.#r(l, d, e, c);
1138
+ let f = this.#i(e, s, d), p = this.#e.monitor.selectedByNs;
1111
1139
  p && (z(p, s), f.length > 0 && R(p, f, s));
1112
- }, f = () => {
1113
- if (l.remove(), document.removeEventListener("pointermove", d), document.removeEventListener("pointerup", f), !u) {
1114
- let e = this.#e.monitor.selectedByNs;
1140
+ }, f = new AbortController(), p = [f.signal, this.#t].filter(Boolean), m = () => {
1141
+ if (!f.signal.aborted) {
1142
+ try {
1143
+ l.remove();
1144
+ } catch {}
1145
+ f.abort();
1146
+ }
1147
+ }, h = () => {
1148
+ if (!u) {
1149
+ let e = this.#e.monitor?.selectedByNs;
1115
1150
  e && z(e, s);
1116
1151
  }
1152
+ m();
1117
1153
  };
1118
- document.addEventListener("pointermove", d), document.addEventListener("pointerup", f);
1154
+ for (let e of p) {
1155
+ if (e.aborted) {
1156
+ m();
1157
+ return;
1158
+ }
1159
+ e.addEventListener("abort", m, { once: !0 });
1160
+ }
1161
+ let g = { signal: f.signal };
1162
+ document.addEventListener("pointermove", d, g), document.addEventListener("pointerup", h, g), document.addEventListener("pointercancel", m, g), window.addEventListener("blur", m, g);
1119
1163
  }
1120
- #n(e, t, n, r) {
1164
+ #r(e, t, n, r) {
1121
1165
  let i = n.getBoundingClientRect(), a = (t.left - i.left) / r, o = (t.top - i.top) / r, s = t.width / r, c = t.height / r;
1122
1166
  e.style.left = `${a}px`, e.style.top = `${o}px`, e.style.width = `${s}px`, e.style.height = `${c}px`;
1123
1167
  }
1124
- #r(e, n, r) {
1168
+ #i(e, n, r) {
1125
1169
  let i = this.#e.monitor.adapter, a = e.querySelectorAll(`[${t.dragAttr}], [${t.dragdropAttr}]`), o = [];
1126
1170
  for (let e of a) {
1127
1171
  if (i.getNamespace(e) !== n) continue;
@@ -1703,7 +1747,7 @@ var we = class {
1703
1747
  }
1704
1748
  }, Te = {
1705
1749
  name: "@ptahjs/dnd",
1706
- version: "0.0.4"
1750
+ version: "0.1.1"
1707
1751
  };
1708
1752
  //#endregion
1709
1753
  export { j as ACTIVE_WRAP_CLASS, B as ActiveSelectionService, D as AutoScrollService, M as DOT_CLASS, re as Dnd, Te as DndVersion, le as DropIndicatorService, x as DropService, U as MarqueeSelectionService, S as MirrorService, N as ROTATE_CLASS, we as TransformControllerService, z as clearAll, R as setMultipleActive };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptahjs/dnd",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -14,9 +14,13 @@
14
14
  "./style": "./dist/dnd.css"
15
15
  },
16
16
  "scripts": {
17
- "build": "vite build"
17
+ "build": "vite build",
18
+ "test": "vitest run"
18
19
  },
19
20
  "devDependencies": {
20
- "vite": "8.0.8"
21
+ "@vitest/coverage-v8": "^4.1.5",
22
+ "jsdom": "^29.1.1",
23
+ "vite": "8.0.11",
24
+ "vitest": "catalog:"
21
25
  }
22
26
  }