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