@dfosco/tiny-canvas 3.10.0 → 3.11.0-beta.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/tiny-canvas.js +194 -192
  2. package/package.json +1 -1
@@ -1,145 +1,145 @@
1
- import { jsx as nt } from "react/jsx-runtime";
2
- import xt, { useRef as F, useState as G, useEffect as rt, Children as Ct, useCallback as Tt } from "react";
1
+ import { jsx as rt } from "react/jsx-runtime";
2
+ import xt, { useRef as G, useState as J, useEffect as nt, Children as Ct, useCallback as It } from "react";
3
3
  var gt = { dragStart: !0 }, pt = { delay: 0, distance: 3 };
4
- function It(t, e = {}) {
5
- let o, a, { bounds: s, axis: d = "both", gpuAcceleration: l = !0, legacyTranslate: f = !1, transform: v, applyUserSelectHack: x = !0, disabled: M = !1, ignoreMultitouch: N = !1, recomputeBounds: E = gt, grid: X, threshold: S = pt, position: b, cancel: D, handle: T, defaultClass: g = "neodrag", defaultClassDragging: i = "neodrag-dragging", defaultClassDragged: n = "neodrag-dragged", defaultPosition: w = { x: 0, y: 0 }, onDragStart: O, onDrag: I, onDragEnd: J } = e, Y = !1, R = !1, p = 0, C = !1, k = !1, $ = 0, _ = 0, B = 0, U = 0, ot = 0, at = 0, { x: z, y: V } = b ? { x: (b == null ? void 0 : b.x) ?? 0, y: (b == null ? void 0 : b.y) ?? 0 } : w;
4
+ function Tt(t, e = {}) {
5
+ let r, a, { bounds: s, axis: d = "both", gpuAcceleration: i = !0, legacyTranslate: f = !1, transform: v, applyUserSelectHack: M = !0, disabled: A = !1, ignoreMultitouch: I = !1, recomputeBounds: D = gt, grid: R, threshold: b = pt, position: E, cancel: L, handle: X, defaultClass: c = "neodrag", defaultClassDragging: N = "neodrag-dragging", defaultClassDragged: O = "neodrag-dragged", defaultPosition: p = { x: 0, y: 0 }, onDragStart: o, onDrag: w, onDragEnd: H } = e, S = !1, T = !1, j = 0, $ = !1, g = !1, x = 0, Y = 0, B = 0, U = 0, ot = 0, at = 0, { x: z, y: V } = E ? { x: (E == null ? void 0 : E.x) ?? 0, y: (E == null ? void 0 : E.y) ?? 0 } : p;
6
6
  ut(z, V);
7
- let P, H, j, st, it, vt = "", Nt = !!b;
8
- E = { ...gt, ...E }, S = { ...pt, ...S ?? {} };
7
+ let P, k, F, st, it, vt = "", Nt = !!E;
8
+ D = { ...gt, ...D }, b = { ...pt, ...b ?? {} };
9
9
  let W = /* @__PURE__ */ new Set();
10
- function bt(r) {
11
- Y && !R && k && C && it && (R = !0, (function(c) {
12
- lt("neodrag:start", O, c);
13
- })(r), q.add(i), x && (vt = ct.userSelect, ct.userSelect = "none"));
10
+ function bt(n) {
11
+ S && !T && g && $ && it && (T = !0, (function(u) {
12
+ lt("neodrag:start", o, u);
13
+ })(n), _.add(N), M && (vt = ct.userSelect, ct.userSelect = "none"));
14
14
  }
15
- const ct = document.body.style, q = t.classList;
16
- function ut(r = $, c = _) {
15
+ const ct = document.body.style, _ = t.classList;
16
+ function ut(n = x, u = Y) {
17
17
  if (!v) {
18
18
  if (f) {
19
- let h = `${+r}px, ${+c}px`;
20
- return K(t, "transform", l ? `translate3d(${h}, 0)` : `translate(${h})`);
19
+ let y = `${+n}px, ${+u}px`;
20
+ return K(t, "transform", i ? `translate3d(${y}, 0)` : `translate(${y})`);
21
21
  }
22
- return K(t, "translate", `${+r}px ${+c}px`);
22
+ return K(t, "translate", `${+n}px ${+u}px`);
23
23
  }
24
- const m = v({ offsetX: r, offsetY: c, rootNode: t });
24
+ const m = v({ offsetX: n, offsetY: u, rootNode: t });
25
25
  mt(m) && K(t, "transform", m);
26
26
  }
27
- function lt(r, c, m) {
28
- const h = /* @__PURE__ */ (function(y) {
29
- return { offsetX: $, offsetY: _, rootNode: t, currentNode: it, event: y };
27
+ function lt(n, u, m) {
28
+ const y = /* @__PURE__ */ (function(h) {
29
+ return { offsetX: x, offsetY: Y, rootNode: t, currentNode: it, event: h };
30
30
  })(m);
31
- t.dispatchEvent(new CustomEvent(r, { detail: h })), c == null || c(h);
31
+ t.dispatchEvent(new CustomEvent(n, { detail: y })), u == null || u(y);
32
32
  }
33
33
  const Z = addEventListener, dt = new AbortController(), ft = { signal: dt.signal, capture: !1 };
34
34
  function Et() {
35
- let r = t.offsetWidth / H.width;
36
- return isNaN(r) && (r = 1), r;
35
+ let n = t.offsetWidth / k.width;
36
+ return isNaN(n) && (n = 1), n;
37
37
  }
38
- return K(t, "touch-action", "none"), Z("pointerdown", ((r) => {
39
- if (M || r.button === 2) return;
40
- if (W.add(r.pointerId), N && W.size > 1) return r.preventDefault();
41
- if (E.dragStart && (P = ht(s, t)), mt(T) && mt(D) && T === D) throw new Error("`handle` selector can't be same as `cancel` selector");
42
- if (q.add(g), j = (function(u, A) {
43
- if (!u) return [A];
44
- if (yt(u)) return [u];
45
- if (Array.isArray(u)) return u;
46
- const L = A.querySelectorAll(u);
47
- if (L === null) throw new Error("Selector passed for `handle` option should be child of the element on which the action is applied");
48
- return Array.from(L.values());
49
- })(T, t), st = (function(u, A) {
50
- if (!u) return [];
51
- if (yt(u)) return [u];
52
- if (Array.isArray(u)) return u;
53
- const L = A.querySelectorAll(u);
54
- if (L === null) throw new Error("Selector passed for `cancel` option should be child of the element on which the action is applied");
55
- return Array.from(L.values());
56
- })(D, t), o = /(both|x)/.test(d), a = /(both|y)/.test(d), Dt(st, j)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
- const c = r.composedPath()[0];
58
- if (!j.some(((u) => {
59
- var A;
60
- return u.contains(c) || ((A = u.shadowRoot) == null ? void 0 : A.contains(c));
61
- })) || Dt(st, [c])) return;
62
- it = j.length === 1 ? t : j.find(((u) => u.contains(c))), Y = !0, p = Date.now(), S.delay || (C = !0), H = t.getBoundingClientRect();
63
- const { clientX: m, clientY: h } = r, y = Et();
64
- o && (B = m - z / y), a && (U = h - V / y), P && (ot = m - H.left, at = h - H.top);
65
- }), ft), Z("pointermove", ((r) => {
66
- if (!Y || N && W.size > 1) return;
67
- if (!R) {
68
- if (C || Date.now() - p >= S.delay && (C = !0, bt(r)), !k) {
69
- const y = r.clientX - B, u = r.clientY - U;
70
- Math.sqrt(y ** 2 + u ** 2) >= S.distance && (k = !0, bt(r));
38
+ return K(t, "touch-action", "none"), Z("pointerdown", ((n) => {
39
+ if (A || n.button === 2) return;
40
+ if (W.add(n.pointerId), I && W.size > 1) return n.preventDefault();
41
+ if (D.dragStart && (P = yt(s, t)), mt(X) && mt(L) && X === L) throw new Error("`handle` selector can't be same as `cancel` selector");
42
+ if (_.add(c), F = (function(l, C) {
43
+ if (!l) return [C];
44
+ if (ht(l)) return [l];
45
+ if (Array.isArray(l)) return l;
46
+ const q = C.querySelectorAll(l);
47
+ if (q === null) throw new Error("Selector passed for `handle` option should be child of the element on which the action is applied");
48
+ return Array.from(q.values());
49
+ })(X, t), st = (function(l, C) {
50
+ if (!l) return [];
51
+ if (ht(l)) return [l];
52
+ if (Array.isArray(l)) return l;
53
+ const q = C.querySelectorAll(l);
54
+ if (q === null) throw new Error("Selector passed for `cancel` option should be child of the element on which the action is applied");
55
+ return Array.from(q.values());
56
+ })(L, t), r = /(both|x)/.test(d), a = /(both|y)/.test(d), Dt(st, F)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
+ const u = n.composedPath()[0];
58
+ if (!F.some(((l) => {
59
+ var C;
60
+ return l.contains(u) || ((C = l.shadowRoot) == null ? void 0 : C.contains(u));
61
+ })) || Dt(st, [u])) return;
62
+ it = F.length === 1 ? t : F.find(((l) => l.contains(u))), S = !0, j = Date.now(), b.delay || ($ = !0), k = t.getBoundingClientRect();
63
+ const { clientX: m, clientY: y } = n, h = Et();
64
+ r && (B = m - z / h), a && (U = y - V / h), P && (ot = m - k.left, at = y - k.top);
65
+ }), ft), Z("pointermove", ((n) => {
66
+ if (!S || I && W.size > 1) return;
67
+ if (!T) {
68
+ if ($ || Date.now() - j >= b.delay && ($ = !0, bt(n)), !g) {
69
+ const h = n.clientX - B, l = n.clientY - U;
70
+ Math.sqrt(h ** 2 + l ** 2) >= b.distance && (g = !0, bt(n));
71
71
  }
72
- if (!R) return;
72
+ if (!T) return;
73
73
  }
74
- E.drag && (P = ht(s, t)), r.preventDefault(), H = t.getBoundingClientRect();
75
- let c = r.clientX, m = r.clientY;
76
- const h = Et();
74
+ D.drag && (P = yt(s, t)), n.preventDefault(), k = t.getBoundingClientRect();
75
+ let u = n.clientX, m = n.clientY;
76
+ const y = Et();
77
77
  if (P) {
78
- const y = { left: P.left + ot, top: P.top + at, right: P.right + ot - H.width, bottom: P.bottom + at - H.height };
79
- c = St(c, y.left, y.right), m = St(m, y.top, y.bottom);
78
+ const h = { left: P.left + ot, top: P.top + at, right: P.right + ot - k.width, bottom: P.bottom + at - k.height };
79
+ u = St(u, h.left, h.right), m = St(m, h.top, h.bottom);
80
80
  }
81
- if (Array.isArray(X)) {
82
- let [y, u] = X;
83
- if (isNaN(+y) || y < 0) throw new Error("1st argument of `grid` must be a valid positive number");
84
- if (isNaN(+u) || u < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
85
- let A = c - B, L = m - U;
86
- [A, L] = Rt([y / h, u / h], A, L), c = B + A, m = U + L;
81
+ if (Array.isArray(R)) {
82
+ let [h, l] = R;
83
+ if (isNaN(+h) || h < 0) throw new Error("1st argument of `grid` must be a valid positive number");
84
+ if (isNaN(+l) || l < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
85
+ let C = u - B, q = m - U;
86
+ [C, q] = Rt([h / y, l / y], C, q), u = B + C, m = U + q;
87
87
  }
88
- o && ($ = Math.round((c - B) * h)), a && (_ = Math.round((m - U) * h)), z = $, V = _, lt("neodrag", I, r), ut();
89
- }), ft), Z("pointerup", ((r) => {
90
- W.delete(r.pointerId), Y && (R && (Z("click", ((c) => c.stopPropagation()), { once: !0, signal: dt.signal, capture: !0 }), E.dragEnd && (P = ht(s, t)), q.remove(i), q.add(n), x && (ct.userSelect = vt), lt("neodrag:end", J, r), o && (B = $), a && (U = _)), Y = !1, R = !1, C = !1, k = !1);
91
- }), ft), { destroy: () => dt.abort(), update: (r) => {
92
- var m, h;
93
- d = r.axis || "both", M = r.disabled ?? !1, N = r.ignoreMultitouch ?? !1, T = r.handle, s = r.bounds, E = r.recomputeBounds ?? gt, D = r.cancel, x = r.applyUserSelectHack ?? !0, X = r.grid, l = r.gpuAcceleration ?? !0, f = r.legacyTranslate ?? !1, v = r.transform, S = { ...pt, ...r.threshold ?? {} };
94
- const c = q.contains(n);
95
- q.remove(g, n), g = r.defaultClass ?? "neodrag", i = r.defaultClassDragging ?? "neodrag-dragging", n = r.defaultClassDragged ?? "neodrag-dragged", q.add(g), c && q.add(n), Nt && (z = $ = ((m = r.position) == null ? void 0 : m.x) ?? $, V = _ = ((h = r.position) == null ? void 0 : h.y) ?? _, ut());
88
+ r && (x = Math.round((u - B) * y)), a && (Y = Math.round((m - U) * y)), z = x, V = Y, lt("neodrag", w, n), ut();
89
+ }), ft), Z("pointerup", ((n) => {
90
+ W.delete(n.pointerId), S && (T && (Z("click", ((u) => u.stopPropagation()), { once: !0, signal: dt.signal, capture: !0 }), D.dragEnd && (P = yt(s, t)), _.remove(N), _.add(O), M && (ct.userSelect = vt), lt("neodrag:end", H, n), r && (B = x), a && (U = Y)), S = !1, T = !1, $ = !1, g = !1);
91
+ }), ft), { destroy: () => dt.abort(), update: (n) => {
92
+ var m, y;
93
+ d = n.axis || "both", A = n.disabled ?? !1, I = n.ignoreMultitouch ?? !1, X = n.handle, s = n.bounds, D = n.recomputeBounds ?? gt, L = n.cancel, M = n.applyUserSelectHack ?? !0, R = n.grid, i = n.gpuAcceleration ?? !0, f = n.legacyTranslate ?? !1, v = n.transform, b = { ...pt, ...n.threshold ?? {} };
94
+ const u = _.contains(O);
95
+ _.remove(c, O), c = n.defaultClass ?? "neodrag", N = n.defaultClassDragging ?? "neodrag-dragging", O = n.defaultClassDragged ?? "neodrag-dragged", _.add(c), u && _.add(O), Nt && (z = x = ((m = n.position) == null ? void 0 : m.x) ?? x, V = Y = ((y = n.position) == null ? void 0 : y.y) ?? Y, ut());
96
96
  } };
97
97
  }
98
- var St = (t, e, o) => Math.min(Math.max(t, e), o), mt = (t) => typeof t == "string", Rt = ([t, e], o, a) => {
99
- const s = (d, l) => l === 0 ? 0 : Math.ceil(d / l) * l;
100
- return [s(o, t), s(a, e)];
101
- }, Dt = (t, e) => t.some(((o) => e.some(((a) => o.contains(a)))));
102
- function ht(t, e) {
98
+ var St = (t, e, r) => Math.min(Math.max(t, e), r), mt = (t) => typeof t == "string", Rt = ([t, e], r, a) => {
99
+ const s = (d, i) => i === 0 ? 0 : Math.ceil(d / i) * i;
100
+ return [s(r, t), s(a, e)];
101
+ }, Dt = (t, e) => t.some(((r) => e.some(((a) => r.contains(a)))));
102
+ function yt(t, e) {
103
103
  if (t === void 0) return;
104
- if (yt(t)) return t.getBoundingClientRect();
104
+ if (ht(t)) return t.getBoundingClientRect();
105
105
  if (typeof t == "object") {
106
- const { top: a = 0, left: s = 0, right: d = 0, bottom: l = 0 } = t;
107
- return { top: a, right: window.innerWidth - d, bottom: window.innerHeight - l, left: s };
106
+ const { top: a = 0, left: s = 0, right: d = 0, bottom: i = 0 } = t;
107
+ return { top: a, right: window.innerWidth - d, bottom: window.innerHeight - i, left: s };
108
108
  }
109
109
  if (t === "parent") return e.parentNode.getBoundingClientRect();
110
- const o = document.querySelector(t);
111
- if (o === null) throw new Error("The selector provided for bound doesn't exists in the document.");
112
- return o.getBoundingClientRect();
110
+ const r = document.querySelector(t);
111
+ if (r === null) throw new Error("The selector provided for bound doesn't exists in the document.");
112
+ return r.getBoundingClientRect();
113
113
  }
114
- var K = (t, e, o) => t.style.setProperty(e, o), yt = (t) => t instanceof HTMLElement;
114
+ var K = (t, e, r) => t.style.setProperty(e, r), ht = (t) => t instanceof HTMLElement;
115
115
  function tt(t) {
116
116
  return t == null || typeof t == "string" || t instanceof HTMLElement ? t : "current" in t ? t.current : Array.isArray(t) ? t.map(((e) => e instanceof HTMLElement ? e : e.current)) : void 0;
117
117
  }
118
- function Pt(t, e = {}) {
119
- const o = F(), [a, s] = G(!1), [d, l] = G();
120
- let { onDragStart: f, onDrag: v, onDragEnd: x, handle: M, cancel: N } = e, E = tt(M), X = tt(N);
121
- function S(g, i) {
122
- l(g), i == null || i(g);
118
+ function Lt(t, e = {}) {
119
+ const r = G(), [a, s] = J(!1), [d, i] = J();
120
+ let { onDragStart: f, onDrag: v, onDragEnd: M, handle: A, cancel: I } = e, D = tt(A), R = tt(I);
121
+ function b(c, N) {
122
+ i(c), N == null || N(c);
123
123
  }
124
- function b(g) {
125
- s(!0), S(g, f);
124
+ function E(c) {
125
+ s(!0), b(c, f);
126
126
  }
127
- function D(g) {
128
- S(g, v);
127
+ function L(c) {
128
+ b(c, v);
129
129
  }
130
- function T(g) {
131
- s(!1), S(g, x);
130
+ function X(c) {
131
+ s(!1), b(c, M);
132
132
  }
133
- return rt((() => {
133
+ return nt((() => {
134
134
  if (typeof window > "u") return;
135
- const g = t.current;
136
- if (!g) return;
137
- ({ onDragStart: f, onDrag: v, onDragEnd: x } = e);
138
- const { update: i, destroy: n } = It(g, { ...e, handle: E, cancel: X, onDragStart: b, onDrag: D, onDragEnd: T });
139
- return o.current = i, n;
140
- }), []), rt((() => {
141
- var g;
142
- (g = o.current) == null || g.call(o, { ...e, handle: tt(M), cancel: tt(N), onDragStart: b, onDrag: D, onDragEnd: T });
135
+ const c = t.current;
136
+ if (!c) return;
137
+ ({ onDragStart: f, onDrag: v, onDragEnd: M } = e);
138
+ const { update: N, destroy: O } = Tt(c, { ...e, handle: D, cancel: R, onDragStart: E, onDrag: L, onDragEnd: X });
139
+ return r.current = N, O;
140
+ }), []), nt((() => {
141
+ var c;
142
+ (c = r.current) == null || c.call(r, { ...e, handle: tt(A), cancel: tt(I), onDragStart: E, onDrag: L, onDragEnd: X });
143
143
  }), [e]), { isDragging: a, dragState: d };
144
144
  }
145
145
  const Q = "tiny-canvas-queue";
@@ -153,39 +153,39 @@ function Mt() {
153
153
  return localStorage.setItem(Q, JSON.stringify([])), [];
154
154
  }
155
155
  }
156
- const Lt = (t) => {
156
+ const Xt = (t) => {
157
157
  if (!t) return null;
158
158
  let e = null;
159
- const o = (a) => {
159
+ const r = (a) => {
160
160
  if (a.props && a.props.id) {
161
161
  e = a.props.id;
162
162
  return;
163
163
  }
164
- a.props && a.props.children && xt.Children.forEach(a.props.children, o);
164
+ a.props && a.props.children && xt.Children.forEach(a.props.children, r);
165
165
  };
166
- return o(t), e;
166
+ return r(t), e;
167
167
  };
168
- function Xt(t) {
168
+ function Yt(t) {
169
169
  let e = 5381;
170
- for (let o = 0; o < t.length; o++)
171
- e = (e << 5) + e + t.charCodeAt(o) >>> 0;
170
+ for (let r = 0; r < t.length; r++)
171
+ e = (e << 5) + e + t.charCodeAt(r) >>> 0;
172
172
  return e.toString(16).padStart(8, "0");
173
173
  }
174
174
  function At(t) {
175
175
  var d;
176
176
  if (t == null || typeof t == "boolean") return "";
177
177
  if (typeof t == "string" || typeof t == "number") return "#text";
178
- const e = t.type, o = typeof e == "function" ? e.displayName || e.name || "Anonymous" : typeof e == "string" ? e : "Fragment", a = (d = t.props) == null ? void 0 : d.children;
179
- if (a == null) return o;
178
+ const e = t.type, r = typeof e == "function" ? e.displayName || e.name || "Anonymous" : typeof e == "string" ? e : "Fragment", a = (d = t.props) == null ? void 0 : d.children;
179
+ if (a == null) return r;
180
180
  const s = [];
181
- return xt.Children.forEach(a, (l) => {
182
- const f = At(l);
181
+ return xt.Children.forEach(a, (i) => {
182
+ const f = At(i);
183
183
  f && s.push(f);
184
- }), s.length ? `${o}(${s.join(",")})` : o;
184
+ }), s.length ? `${r}(${s.join(",")})` : r;
185
185
  }
186
- const Yt = (t, e) => {
187
- const o = At(t);
188
- return `tc-${Xt(o)}-${e}`;
186
+ const Pt = (t, e) => {
187
+ const r = At(t);
188
+ return `tc-${Yt(r)}-${e}`;
189
189
  }, Ft = (t) => {
190
190
  try {
191
191
  return Mt().reduce((a, s) => (a[s.id] = { id: s.id, x: s.x, y: s.y }, a), {})[t] || { x: 0, y: 0 };
@@ -198,16 +198,16 @@ const Yt = (t, e) => {
198
198
  } catch (t) {
199
199
  console.error("LocalStorage is not available:", t);
200
200
  }
201
- }, qt = (t, e, o) => {
201
+ }, qt = (t, e, r) => {
202
202
  try {
203
- const a = Mt(), s = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), d = { id: t, x: e, y: o, time: s }, l = a.findIndex((f) => f.id === t);
204
- l >= 0 ? a[l] = d : a.push(d), localStorage.setItem(Q, JSON.stringify(a));
203
+ const a = Mt(), s = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), d = { id: t, x: e, y: r, time: s }, i = a.findIndex((f) => f.id === t);
204
+ i >= 0 ? a[i] = d : a.push(d), localStorage.setItem(Q, JSON.stringify(a));
205
205
  } catch (a) {
206
206
  console.error("Error saving drag position:", a);
207
207
  }
208
208
  }, Ot = 250, wt = 4, $t = 20, et = 1.5, _t = 150, Ht = 8;
209
- function kt({ children: t, dragId: e, initialPosition: o, onDragEnd: a, handle: s }) {
210
- const d = F(null), l = o || { x: 0, y: 0 }, f = F(l), v = F(!1), [x, M] = G(!1), N = F({
209
+ function kt({ children: t, dragId: e, initialPosition: r, onDragEnd: a, handle: s }) {
210
+ const d = G(null), i = r || { x: 0, y: 0 }, f = G(i), v = G(!1), [M, A] = J(!1), I = G({
211
211
  timer: null,
212
212
  startX: 0,
213
213
  startY: 0,
@@ -215,65 +215,67 @@ function kt({ children: t, dragId: e, initialPosition: o, onDragEnd: a, handle:
215
215
  distanceMet: !1,
216
216
  active: !1,
217
217
  lastEvent: null
218
- }), [E, X] = G(l), [S, b] = G(
218
+ }), [D, R] = J(i), [b, E] = J(i);
219
+ r && (r.x !== b.x || r.y !== b.y) && (E(r), R(r));
220
+ const [L, X] = J(
219
221
  () => Math.random() < 0.5 ? -et : et
220
222
  );
221
- rt(() => {
222
- const i = d.current;
223
- if (i && e && (l.x !== 0 || l.y !== 0)) {
224
- i.classList.add("tc-on-translation");
225
- const n = setTimeout(() => {
226
- i.classList.remove("tc-on-translation");
223
+ nt(() => {
224
+ const p = d.current;
225
+ if (p && e && (i.x !== 0 || i.y !== 0)) {
226
+ p.classList.add("tc-on-translation");
227
+ const o = setTimeout(() => {
228
+ p.classList.remove("tc-on-translation");
227
229
  }, Ot * 4);
228
230
  return () => {
229
- clearTimeout(n);
231
+ clearTimeout(o);
230
232
  };
231
233
  }
232
- }, [e, l.x, l.y]), rt(() => {
233
- const i = d.current;
234
- if (!i || !s) return;
235
- const n = N.current;
234
+ }, [e, i.x, i.y]), nt(() => {
235
+ const p = d.current;
236
+ if (!p || !s) return;
237
+ const o = I.current;
236
238
  let w = null;
237
- function O(p) {
238
- const C = i.querySelector(s);
239
- return C && C.contains(p.target);
239
+ function H(g) {
240
+ const x = p.querySelector(s);
241
+ return x && x.contains(g.target);
240
242
  }
241
- function I(p) {
242
- if (p === w) {
243
+ function S(g) {
244
+ if (g === w) {
243
245
  w = null;
244
246
  return;
245
247
  }
246
- O(p) && (p.stopImmediatePropagation(), n.active = !0, n.delayMet = !1, n.distanceMet = !1, n.startX = p.clientX, n.startY = p.clientY, n.target = p.target, n.pointerId = p.pointerId, n.button = p.button, n.pointerType = p.pointerType, clearTimeout(n.timer), n.timer = setTimeout(() => {
247
- n.delayMet = !0, J();
248
+ H(g) && (g.stopImmediatePropagation(), o.active = !0, o.delayMet = !1, o.distanceMet = !1, o.startX = g.clientX, o.startY = g.clientY, o.target = g.target, o.pointerId = g.pointerId, o.button = g.button, o.pointerType = g.pointerType, clearTimeout(o.timer), o.timer = setTimeout(() => {
249
+ o.delayMet = !0, T();
248
250
  }, _t));
249
251
  }
250
- function J() {
251
- if (n.delayMet && n.distanceMet && n.active && n.target) {
252
- const p = new PointerEvent("pointerdown", {
252
+ function T() {
253
+ if (o.delayMet && o.distanceMet && o.active && o.target) {
254
+ const g = new PointerEvent("pointerdown", {
253
255
  bubbles: !0,
254
256
  cancelable: !0,
255
- pointerId: n.pointerId,
256
- clientX: n.startX,
257
- clientY: n.startY,
258
- button: n.button,
259
- pointerType: n.pointerType
257
+ pointerId: o.pointerId,
258
+ clientX: o.startX,
259
+ clientY: o.startY,
260
+ button: o.button,
261
+ pointerType: o.pointerType
260
262
  });
261
- w = p, n.target.dispatchEvent(p), n.target = null;
263
+ w = g, o.target.dispatchEvent(g), o.target = null;
262
264
  }
263
265
  }
264
- function Y(p) {
265
- if (!n.active || n.distanceMet) return;
266
- const C = p.clientX - n.startX, k = p.clientY - n.startY;
267
- Math.hypot(C, k) >= Ht && (n.distanceMet = !0, J());
266
+ function j(g) {
267
+ if (!o.active || o.distanceMet) return;
268
+ const x = g.clientX - o.startX, Y = g.clientY - o.startY;
269
+ Math.hypot(x, Y) >= Ht && (o.distanceMet = !0, T());
268
270
  }
269
- function R() {
270
- clearTimeout(n.timer), n.active = !1, n.target = null;
271
+ function $() {
272
+ clearTimeout(o.timer), o.active = !1, o.target = null;
271
273
  }
272
- return i.addEventListener("pointerdown", I, !0), document.addEventListener("pointermove", Y), document.addEventListener("pointerup", R), () => {
273
- i.removeEventListener("pointerdown", I, !0), document.removeEventListener("pointermove", Y), document.removeEventListener("pointerup", R), clearTimeout(n.timer);
274
+ return p.addEventListener("pointerdown", S, !0), document.addEventListener("pointermove", j), document.addEventListener("pointerup", $), () => {
275
+ p.removeEventListener("pointerdown", S, !0), document.removeEventListener("pointermove", j), document.removeEventListener("pointerup", $), clearTimeout(o.timer);
274
276
  };
275
277
  }, [s]);
276
- const { isDragging: D } = Pt(d, {
278
+ const { isDragging: c } = Lt(d, {
277
279
  axis: "both",
278
280
  bounds: "parent",
279
281
  defaultClass: "tc-drag",
@@ -281,32 +283,32 @@ function kt({ children: t, dragId: e, initialPosition: o, onDragEnd: a, handle:
281
283
  defaultClassDragged: "tc-off",
282
284
  applyUserSelectHack: !0,
283
285
  ...s ? { handle: s } : {},
284
- position: { x: E.x, y: E.y },
286
+ position: { x: D.x, y: D.y },
285
287
  onDragStart: () => {
286
- f.current = E, v.current = !1, M(!1);
288
+ f.current = D, v.current = !1, A(!1);
287
289
  },
288
- onDrag: ({ offsetX: i, offsetY: n }) => {
289
- const w = i - f.current.x, O = n - f.current.y, I = Math.hypot(w, O);
290
- !v.current && I >= wt && (v.current = !0), !x && I >= $t && M(!0), X({ x: Math.max(0, i), y: Math.max(0, n) });
290
+ onDrag: ({ offsetX: p, offsetY: o }) => {
291
+ const w = p - f.current.x, H = o - f.current.y, S = Math.hypot(w, H);
292
+ !v.current && S >= wt && (v.current = !0), !M && S >= $t && A(!0), R({ x: Math.max(0, p), y: Math.max(0, o) });
291
293
  },
292
- onDragEnd: (i) => {
293
- const n = Math.max(0, i.offsetX), w = Math.max(0, i.offsetY);
294
- X({ x: n, y: w }), b(Math.random() < 0.5 ? -et : et), M(!1);
295
- const O = n - f.current.x, I = w - f.current.y;
296
- (v.current || Math.hypot(O, I) >= wt) && (e && qt(e, n, w), a == null || a(e, { x: n, y: w }));
294
+ onDragEnd: (p) => {
295
+ const o = Math.max(0, p.offsetX), w = Math.max(0, p.offsetY);
296
+ R({ x: o, y: w }), X(Math.random() < 0.5 ? -et : et), A(!1);
297
+ const H = o - f.current.x, S = w - f.current.y;
298
+ (v.current || Math.hypot(H, S) >= wt) && (e && qt(e, o, w), a == null || a(e, { x: o, y: w }));
297
299
  }
298
- }), T = D && x ? `${S}deg` : "0deg";
299
- return /* @__PURE__ */ nt(
300
+ }), N = c && M ? `${L}deg` : "0deg";
301
+ return /* @__PURE__ */ rt(
300
302
  "article",
301
303
  {
302
304
  ref: d,
303
- style: { cursor: s ? D ? "grabbing" : void 0 : D ? "grabbing" : "grab" },
304
- children: /* @__PURE__ */ nt(
305
+ style: { cursor: s ? c ? "grabbing" : void 0 : c ? "grabbing" : "grab" },
306
+ children: /* @__PURE__ */ rt(
305
307
  "div",
306
308
  {
307
309
  className: "tc-draggable-inner",
308
310
  style: {
309
- transform: D ? `rotate(${T})` : void 0,
311
+ transform: c ? `rotate(${N})` : void 0,
310
312
  transition: "transform ease-in-out 150ms"
311
313
  },
312
314
  children: t
@@ -317,8 +319,8 @@ function kt({ children: t, dragId: e, initialPosition: o, onDragEnd: a, handle:
317
319
  }
318
320
  function Bt(t) {
319
321
  var a, s;
320
- const e = Number((a = t == null ? void 0 : t.props) == null ? void 0 : a["data-tc-x"]), o = Number((s = t == null ? void 0 : t.props) == null ? void 0 : s["data-tc-y"]);
321
- return Number.isFinite(e) && Number.isFinite(o) ? { x: Math.max(0, e), y: Math.max(0, o) } : null;
322
+ const e = Number((a = t == null ? void 0 : t.props) == null ? void 0 : a["data-tc-x"]), r = Number((s = t == null ? void 0 : t.props) == null ? void 0 : s["data-tc-y"]);
323
+ return Number.isFinite(e) && Number.isFinite(r) ? { x: Math.max(0, e), y: Math.max(0, r) } : null;
322
324
  }
323
325
  function Ut(t) {
324
326
  var e;
@@ -327,27 +329,27 @@ function Ut(t) {
327
329
  function Qt({
328
330
  children: t,
329
331
  dotted: e = !1,
330
- grid: o = !1,
332
+ grid: r = !1,
331
333
  gridSize: a,
332
334
  colorMode: s = "auto",
333
335
  onDragEnd: d
334
336
  }) {
335
- return /* @__PURE__ */ nt(
337
+ return /* @__PURE__ */ rt(
336
338
  "main",
337
339
  {
338
340
  className: "tc-canvas",
339
- "data-dotted": e || o || void 0,
341
+ "data-dotted": e || r || void 0,
340
342
  "data-color-mode": s !== "auto" ? s : void 0,
341
343
  children: Ct.map(t, (f, v) => {
342
- const x = Lt(f) ?? Yt(f, v), M = Bt(f), N = Ut(f);
343
- return /* @__PURE__ */ nt(
344
+ const M = Xt(f) ?? Pt(f, v), A = Bt(f), I = Ut(f);
345
+ return /* @__PURE__ */ rt(
344
346
  kt,
345
347
  {
346
348
  gridSize: a,
347
- dragId: x,
348
- initialPosition: M,
349
+ dragId: M,
350
+ initialPosition: A,
349
351
  onDragEnd: d,
350
- handle: N,
352
+ handle: I,
351
353
  children: f
352
354
  },
353
355
  v
@@ -357,7 +359,7 @@ function Qt({
357
359
  );
358
360
  }
359
361
  function zt({ reload: t = !1 } = {}) {
360
- return Tt(() => {
362
+ return It(() => {
361
363
  try {
362
364
  localStorage.removeItem("tiny-canvas-queue");
363
365
  } catch (e) {
@@ -369,8 +371,8 @@ function zt({ reload: t = !1 } = {}) {
369
371
  export {
370
372
  Qt as Canvas,
371
373
  kt as Draggable,
372
- Lt as findDragId,
373
- Yt as generateDragId,
374
+ Xt as findDragId,
375
+ Pt as generateDragId,
374
376
  Ft as getQueue,
375
377
  Gt as refreshStorage,
376
378
  qt as saveDrag,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfosco/tiny-canvas",
3
- "version": "3.10.0",
3
+ "version": "3.11.0-beta.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "A lightweight React canvas with draggable elements and persistent positions",