@dfosco/tiny-canvas 1.1.1 → 3.9.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 +237 -204
  2. package/package.json +6 -1
@@ -1,289 +1,322 @@
1
- import { jsx as W } from "react/jsx-runtime";
2
- import me, { useRef as le, useState as F, useEffect as Q, Children as Ee, useCallback as Ce } from "react";
3
- var ae = { dragStart: !0 }, se = { delay: 0, distance: 3 };
4
- function Ae(e, t = {}) {
5
- let n, o, { bounds: a, axis: l = "both", gpuAcceleration: c = !0, legacyTranslate: p = !1, transform: y, applyUserSelectHack: C = !0, disabled: u = !1, ignoreMultitouch: m = !1, recomputeBounds: S = ae, grid: B, threshold: w = se, position: v, cancel: N, handle: q, defaultClass: d = "neodrag", defaultClassDragging: D = "neodrag-dragging", defaultClassDragged: M = "neodrag-dragged", defaultPosition: be = { x: 0, y: 0 }, onDragStart: Se, onDrag: we, onDragEnd: De } = t, H = !1, T = !1, de = 0, X = !1, O = !1, R = 0, $ = 0, L = 0, k = 0, _ = 0, G = 0, { x: P, y: J } = v ? { x: (v == null ? void 0 : v.x) ?? 0, y: (v == null ? void 0 : v.y) ?? 0 } : be;
6
- te(P, J);
7
- let x, I, Y, K, Z, fe = "", xe = !!v;
8
- S = { ...ae, ...S }, w = { ...se, ...w ?? {} };
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 };
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
9
  let j = /* @__PURE__ */ new Set();
10
- function ge(r) {
11
- H && !T && O && X && Z && (T = !0, (function(s) {
12
- re("neodrag:start", Se, s);
13
- })(r), A.add(D), C && (fe = ee.userSelect, ee.userSelect = "none"));
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"));
14
14
  }
15
- const ee = document.body.style, A = e.classList;
16
- function te(r = R, s = $) {
17
- if (!y) {
18
- if (p) {
19
- let g = `${+r}px, ${+s}px`;
20
- return z(e, "transform", c ? `translate3d(${g}, 0)` : `translate(${g})`);
15
+ const rt = document.body.style, R = t.classList;
16
+ function nt(r = T, c = O) {
17
+ if (!v) {
18
+ if (d) {
19
+ let h = `${+r}px, ${+c}px`;
20
+ return Q(t, "transform", u ? `translate3d(${h}, 0)` : `translate(${h})`);
21
21
  }
22
- return z(e, "translate", `${+r}px ${+s}px`);
22
+ return Q(t, "translate", `${+r}px ${+c}px`);
23
23
  }
24
- const f = y({ offsetX: r, offsetY: s, rootNode: e });
25
- ie(f) && z(e, "transform", f);
24
+ const p = v({ offsetX: r, offsetY: c, rootNode: t });
25
+ lt(p) && Q(t, "transform", p);
26
26
  }
27
- function re(r, s, f) {
28
- const g = /* @__PURE__ */ (function(h) {
29
- return { offsetX: R, offsetY: $, rootNode: e, currentNode: Z, event: h };
30
- })(f);
31
- e.dispatchEvent(new CustomEvent(r, { detail: g })), s == null || s(g);
27
+ function ot(r, c, p) {
28
+ const h = /* @__PURE__ */ (function(y) {
29
+ return { offsetX: T, offsetY: O, rootNode: t, currentNode: et, event: y };
30
+ })(p);
31
+ t.dispatchEvent(new CustomEvent(r, { detail: h })), c == null || c(h);
32
32
  }
33
- const U = addEventListener, ne = new AbortController(), oe = { signal: ne.signal, capture: !1 };
34
- function pe() {
35
- let r = e.offsetWidth / I.width;
33
+ const F = addEventListener, at = new AbortController(), st = { signal: at.signal, capture: !1 };
34
+ function mt() {
35
+ let r = t.offsetWidth / P.width;
36
36
  return isNaN(r) && (r = 1), r;
37
37
  }
38
- return z(e, "touch-action", "none"), U("pointerdown", ((r) => {
39
- if (u || r.button === 2) return;
40
- if (j.add(r.pointerId), m && j.size > 1) return r.preventDefault();
41
- if (S.dragStart && (x = ce(a, e)), ie(q) && ie(N) && q === N) throw new Error("`handle` selector can't be same as `cancel` selector");
42
- if (A.add(d), Y = (function(i, b) {
43
- if (!i) return [b];
44
- if (ue(i)) return [i];
45
- if (Array.isArray(i)) return i;
46
- const E = b.querySelectorAll(i);
47
- if (E === 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(E.values());
49
- })(q, e), K = (function(i, b) {
50
- if (!i) return [];
51
- if (ue(i)) return [i];
52
- if (Array.isArray(i)) return i;
53
- const E = b.querySelectorAll(i);
54
- if (E === 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(E.values());
56
- })(N, e), n = /(both|x)/.test(l), o = /(both|y)/.test(l), ye(K, Y)) throw new Error("Element being dragged can't be a child of the element on which `cancel` is applied");
57
- const s = r.composedPath()[0];
58
- if (!Y.some(((i) => {
59
- var b;
60
- return i.contains(s) || ((b = i.shadowRoot) == null ? void 0 : b.contains(s));
61
- })) || ye(K, [s])) return;
62
- Z = Y.length === 1 ? e : Y.find(((i) => i.contains(s))), H = !0, de = Date.now(), w.delay || (X = !0), I = e.getBoundingClientRect();
63
- const { clientX: f, clientY: g } = r, h = pe();
64
- n && (L = f - P / h), o && (k = g - J / h), x && (_ = f - I.left, G = g - I.top);
65
- }), oe), U("pointermove", ((r) => {
66
- if (!H || m && j.size > 1) return;
67
- if (!T) {
68
- if (X || Date.now() - de >= w.delay && (X = !0, ge(r)), !O) {
69
- const h = r.clientX - L, i = r.clientY - k;
70
- Math.sqrt(h ** 2 + i ** 2) >= w.distance && (O = !0, ge(r));
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");
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));
71
71
  }
72
- if (!T) return;
72
+ if (!$) return;
73
73
  }
74
- S.drag && (x = ce(a, e)), r.preventDefault(), I = e.getBoundingClientRect();
75
- let s = r.clientX, f = r.clientY;
76
- const g = pe();
77
- if (x) {
78
- const h = { left: x.left + _, top: x.top + G, right: x.right + _ - I.width, bottom: x.bottom + G - I.height };
79
- s = he(s, h.left, h.right), f = he(f, h.top, h.bottom);
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);
80
80
  }
81
- if (Array.isArray(B)) {
82
- let [h, i] = B;
83
- if (isNaN(+h) || h < 0) throw new Error("1st argument of `grid` must be a valid positive number");
84
- if (isNaN(+i) || i < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
85
- let b = s - L, E = f - k;
86
- [b, E] = Ne([h / g, i / g], b, E), s = L + b, f = k + E;
81
+ if (Array.isArray(I)) {
82
+ let [y, l] = I;
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;
87
87
  }
88
- n && (R = Math.round((s - L) * g)), o && ($ = Math.round((f - k) * g)), P = R, J = $, re("neodrag", we, r), te();
89
- }), oe), U("pointerup", ((r) => {
90
- j.delete(r.pointerId), H && (T && (U("click", ((s) => s.stopPropagation()), { once: !0, signal: ne.signal, capture: !0 }), S.dragEnd && (x = ce(a, e)), A.remove(D), A.add(M), C && (ee.userSelect = fe), re("neodrag:end", De, r), n && (L = R), o && (k = $)), H = !1, T = !1, X = !1, O = !1);
91
- }), oe), { destroy: () => ne.abort(), update: (r) => {
92
- var f, g;
93
- l = r.axis || "both", u = r.disabled ?? !1, m = r.ignoreMultitouch ?? !1, q = r.handle, a = r.bounds, S = r.recomputeBounds ?? ae, N = r.cancel, C = r.applyUserSelectHack ?? !0, B = r.grid, c = r.gpuAcceleration ?? !0, p = r.legacyTranslate ?? !1, y = r.transform, w = { ...se, ...r.threshold ?? {} };
94
- const s = A.contains(M);
95
- A.remove(d, M), d = r.defaultClass ?? "neodrag", D = r.defaultClassDragging ?? "neodrag-dragging", M = r.defaultClassDragged ?? "neodrag-dragged", A.add(d), s && A.add(M), xe && (P = R = ((f = r.position) == null ? void 0 : f.x) ?? R, J = $ = ((g = r.position) == null ? void 0 : g.y) ?? $, te());
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());
96
96
  } };
97
97
  }
98
- var he = (e, t, n) => Math.min(Math.max(e, t), n), ie = (e) => typeof e == "string", Ne = ([e, t], n, o) => {
99
- const a = (l, c) => c === 0 ? 0 : Math.ceil(l / c) * c;
100
- return [a(n, e), a(o, t)];
101
- }, ye = (e, t) => e.some(((n) => t.some(((o) => n.contains(o)))));
102
- function ce(e, t) {
103
- if (e === void 0) return;
104
- if (ue(e)) return e.getBoundingClientRect();
105
- if (typeof e == "object") {
106
- const { top: o = 0, left: a = 0, right: l = 0, bottom: c = 0 } = e;
107
- return { top: o, right: window.innerWidth - l, bottom: window.innerHeight - c, left: a };
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) {
103
+ if (t === void 0) return;
104
+ if (ft(t)) return t.getBoundingClientRect();
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 };
108
108
  }
109
- if (e === "parent") return t.parentNode.getBoundingClientRect();
110
- const n = document.querySelector(e);
109
+ if (t === "parent") return e.parentNode.getBoundingClientRect();
110
+ const n = document.querySelector(t);
111
111
  if (n === null) throw new Error("The selector provided for bound doesn't exists in the document.");
112
112
  return n.getBoundingClientRect();
113
113
  }
114
- var z = (e, t, n) => e.style.setProperty(t, n), ue = (e) => e instanceof HTMLElement;
115
- function V(e) {
116
- return e == null || typeof e == "string" || e instanceof HTMLElement ? e : "current" in e ? e.current : Array.isArray(e) ? e.map(((t) => t instanceof HTMLElement ? t : t.current)) : void 0;
114
+ var Q = (t, e, n) => t.style.setProperty(e, n), ft = (t) => t instanceof HTMLElement;
115
+ function z(t) {
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 qe(e, t = {}) {
119
- const n = le(), [o, a] = F(!1), [l, c] = F();
120
- let { onDragStart: p, onDrag: y, onDragEnd: C, handle: u, cancel: m } = t, S = V(u), B = V(m);
121
- function w(d, D) {
122
- c(d), D == null || D(d);
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);
123
123
  }
124
- function v(d) {
125
- a(!0), w(d, p);
124
+ function S(s) {
125
+ a(!0), m(s, d);
126
126
  }
127
- function N(d) {
128
- w(d, y);
127
+ function f(s) {
128
+ m(s, v);
129
129
  }
130
- function q(d) {
131
- a(!1), w(d, C);
130
+ function g(s) {
131
+ a(!1), m(s, x);
132
132
  }
133
- return Q((() => {
133
+ return dt((() => {
134
134
  if (typeof window > "u") return;
135
- const d = e.current;
136
- if (!d) return;
137
- ({ onDragStart: p, onDrag: y, onDragEnd: C } = t);
138
- const { update: D, destroy: M } = Ae(d, { ...t, handle: S, cancel: B, onDragStart: v, onDrag: N, onDragEnd: q });
139
- return n.current = D, M;
140
- }), []), Q((() => {
141
- var d;
142
- (d = n.current) == null || d.call(n, { ...t, handle: V(u), cancel: V(m), onDragStart: v, onDrag: N, onDragEnd: q });
143
- }), [t]), { isDragging: o, dragState: l };
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 };
144
144
  }
145
- const Me = (e) => {
146
- if (!e) return null;
147
- let t = null;
145
+ const Y = "tiny-canvas-queue";
146
+ function wt() {
147
+ const t = localStorage.getItem(Y);
148
+ if (!t) return [];
149
+ try {
150
+ const e = JSON.parse(t);
151
+ return Array.isArray(e) ? e : [];
152
+ } catch {
153
+ return localStorage.setItem(Y, JSON.stringify([])), [];
154
+ }
155
+ }
156
+ const Pt = (t) => {
157
+ if (!t) return null;
158
+ let e = null;
148
159
  const n = (o) => {
149
160
  if (o.props && o.props.id) {
150
- t = o.props.id;
161
+ e = o.props.id;
151
162
  return;
152
163
  }
153
- o.props && o.props.children && me.Children.forEach(o.props.children, n);
164
+ o.props && o.props.children && xt.Children.forEach(o.props.children, n);
154
165
  };
155
- return n(e), t;
166
+ return n(t), e;
156
167
  };
157
- function Re(e) {
158
- let t = 5381;
159
- for (let n = 0; n < e.length; n++)
160
- t = (t << 5) + t + e.charCodeAt(n) >>> 0;
161
- return t.toString(16).padStart(8, "0");
168
+ function $t(t) {
169
+ let e = 5381;
170
+ for (let n = 0; n < t.length; n++)
171
+ e = (e << 5) + e + t.charCodeAt(n) >>> 0;
172
+ return e.toString(16).padStart(8, "0");
162
173
  }
163
- function ve(e) {
164
- var l;
165
- if (e == null || typeof e == "boolean") return "";
166
- if (typeof e == "string" || typeof e == "number") return "#text";
167
- const t = e.type, n = typeof t == "function" ? t.displayName || t.name || "Anonymous" : typeof t == "string" ? t : "Fragment", o = (l = e.props) == null ? void 0 : l.children;
174
+ function Dt(t) {
175
+ var i;
176
+ if (t == null || typeof t == "boolean") return "";
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;
168
179
  if (o == null) return n;
169
180
  const a = [];
170
- return me.Children.forEach(o, (c) => {
171
- const p = ve(c);
172
- p && a.push(p);
181
+ return xt.Children.forEach(o, (u) => {
182
+ const d = Dt(u);
183
+ d && a.push(d);
173
184
  }), a.length ? `${n}(${a.join(",")})` : n;
174
185
  }
175
- const $e = (e, t) => {
176
- const n = ve(e);
177
- return `tc-${Re(n)}-${t}`;
178
- }, Ie = (e) => {
186
+ const qt = (t, e) => {
187
+ const n = Dt(t);
188
+ return `tc-${$t(n)}-${e}`;
189
+ }, Ut = (t) => {
179
190
  try {
180
- return (JSON.parse(localStorage.getItem("tiny-canvas-queue")) || []).reduce((o, a) => (o[a.id] = { id: a.id, x: a.x, y: a.y }, o), {})[e] || { x: 0, y: 0 };
181
- } catch (t) {
182
- return console.error("Error getting saved coordinates:", t), { x: 0, y: 0 };
191
+ return wt().reduce((o, a) => (o[a.id] = { id: a.id, x: a.x, y: a.y }, o), {})[t] || { x: 0, y: 0 };
192
+ } catch (e) {
193
+ return console.error("Error getting saved coordinates:", e), { x: 0, y: 0 };
183
194
  }
184
- }, Te = () => {
195
+ }, Jt = () => {
185
196
  try {
186
- localStorage.getItem("tiny-canvas-queue") || localStorage.setItem("tiny-canvas-queue", JSON.stringify([]));
187
- } catch (e) {
188
- console.error("LocalStorage is not available:", e);
197
+ localStorage.getItem(Y) || localStorage.setItem(Y, JSON.stringify([]));
198
+ } catch (t) {
199
+ console.error("LocalStorage is not available:", t);
189
200
  }
190
- }, Le = (e, t, n) => {
201
+ }, Lt = (t, e, n) => {
191
202
  try {
192
- const o = JSON.parse(localStorage.getItem("tiny-canvas-queue")) || [], a = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"), l = { id: e, x: t, y: n, time: a }, c = o.findIndex((p) => p.id === e);
193
- c >= 0 ? o[c] = l : o.push(l), localStorage.setItem("tiny-canvas-queue", JSON.stringify(o));
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));
194
205
  } catch (o) {
195
206
  console.error("Error saving drag position:", o);
196
207
  }
197
- }, ke = 250;
198
- function Be({ children: e, dragId: t }) {
199
- const n = le(null), o = le(Ie(t)), [a, l] = F({ x: 0, y: 0 }), [c, p] = F(
200
- () => Math.random() < 0.5 ? -0.5 : 0.5
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
201
212
  );
202
- Q(() => {
203
- const u = n.current, m = o.current;
204
- if (u && t && m && (m.x !== 0 || m.y !== 0)) {
205
- u.classList.add("tc-on-translation");
206
- const S = setTimeout(() => {
207
- u.classList.remove("tc-on-translation");
208
- }, ke * 4);
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);
209
220
  return () => {
210
- clearTimeout(S);
221
+ clearTimeout(g);
211
222
  };
212
223
  }
213
- }, [t]), Q(() => {
214
- Te();
215
- const u = o.current;
216
- n.current && u && l({ x: u.x, y: u.y });
217
- }, []);
218
- const { isDragging: y } = qe(n, {
224
+ }, [e, i.x, i.y]);
225
+ const { isDragging: m } = Ot(a, {
219
226
  axis: "both",
227
+ bounds: "parent",
220
228
  threshold: { delay: 50, distance: 30 },
221
229
  defaultClass: "tc-drag",
222
230
  defaultClassDragging: "tc-on",
223
231
  defaultClassDragged: "tc-off",
224
232
  applyUserSelectHack: !0,
225
- position: { x: a.x, y: a.y },
226
- onDrag: ({ offsetX: u, offsetY: m }) => l({ x: u, y: m }),
227
- onDragEnd: (u) => {
228
- l({ x: u.offsetX, y: u.offsetY }), p(Math.random() < 0.5 ? -0.5 : 0.5), t !== null && Le(t, u.offsetX, u.offsetY);
233
+ position: { x: w.x, y: w.y },
234
+ onDragStart: () => {
235
+ u.current = w, d.current = !1, x(!1);
236
+ },
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) });
240
+ },
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 }));
229
246
  }
230
- }), C = y ? `${c}deg` : "0deg";
231
- return /* @__PURE__ */ W(
247
+ }), S = m && v ? `${N}deg` : "0deg";
248
+ return /* @__PURE__ */ Z(
232
249
  "article",
233
250
  {
234
- ref: n,
235
- style: { cursor: y ? "grabbing" : "grab" },
236
- children: /* @__PURE__ */ W(
251
+ ref: a,
252
+ style: { cursor: m ? "grabbing" : "grab" },
253
+ children: /* @__PURE__ */ Z(
237
254
  "div",
238
255
  {
239
256
  className: "tc-draggable-inner",
240
257
  style: {
241
- transform: y ? `rotate(${C})` : void 0,
258
+ transform: m ? `rotate(${S})` : void 0,
242
259
  transition: "transform ease-in-out 150ms"
243
260
  },
244
- children: e
261
+ children: t
245
262
  }
246
263
  )
247
264
  }
248
265
  );
249
266
  }
250
- function Ye({
251
- children: e,
252
- dotted: t = !1,
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;
271
+ }
272
+ function jt({
273
+ children: t,
274
+ dotted: e = !1,
253
275
  grid: n = !1,
254
276
  gridSize: o,
255
- colorMode: a = "auto"
277
+ colorMode: a = "auto",
278
+ onDragEnd: i
256
279
  }) {
257
- return /* @__PURE__ */ W(
280
+ return /* @__PURE__ */ Z(
258
281
  "main",
259
282
  {
260
283
  className: "tc-canvas",
261
- "data-dotted": t || n || void 0,
284
+ "data-dotted": e || n || void 0,
262
285
  "data-color-mode": a !== "auto" ? a : void 0,
263
- children: Ee.map(e, (c, p) => {
264
- const y = Me(c) ?? $e(c, p);
265
- return /* @__PURE__ */ W(Be, { gridSize: o, dragId: y, children: c }, p);
286
+ children: Ct.map(t, (d, v) => {
287
+ const x = Pt(d) ?? qt(d, v), w = Ht(d);
288
+ return /* @__PURE__ */ Z(
289
+ Bt,
290
+ {
291
+ gridSize: o,
292
+ dragId: x,
293
+ initialPosition: w,
294
+ onDragEnd: i,
295
+ children: d
296
+ },
297
+ v
298
+ );
266
299
  })
267
300
  }
268
301
  );
269
302
  }
270
- function Oe({ reload: e = !1 } = {}) {
271
- return Ce(() => {
303
+ function Ft({ reload: t = !1 } = {}) {
304
+ return Rt(() => {
272
305
  try {
273
306
  localStorage.removeItem("tiny-canvas-queue");
274
- } catch (t) {
275
- console.error("Error clearing canvas state:", t);
307
+ } catch (e) {
308
+ console.error("Error clearing canvas state:", e);
276
309
  }
277
- e && window.location.reload();
278
- }, [e]);
310
+ t && window.location.reload();
311
+ }, [t]);
279
312
  }
280
313
  export {
281
- Ye as Canvas,
282
- Be as Draggable,
283
- Me as findDragId,
284
- $e as generateDragId,
285
- Ie as getQueue,
286
- Te as refreshStorage,
287
- Le as saveDrag,
288
- Oe as useResetCanvas
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
289
322
  };
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@dfosco/tiny-canvas",
3
- "version": "1.1.1",
3
+ "version": "3.9.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "A lightweight React canvas with draggable elements and persistent positions",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dfosco/storyboard.git",
10
+ "directory": "packages/tiny-canvas"
11
+ },
7
12
  "main": "./dist/tiny-canvas.js",
8
13
  "module": "./dist/tiny-canvas.js",
9
14
  "types": "./src/index.d.ts",