@dfosco/tiny-canvas 1.1.0 → 1.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.
- package/dist/tiny-canvas.css +1 -1
- package/dist/tiny-canvas.js +173 -173
- package/package.json +7 -17
package/dist/tiny-canvas.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
:root{--tc-border-radius: var(--borderRadius-large, 12px);--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .12));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .15));--tc-border-color: var(--borderColor-muted, rgba(0, 0, 0, .15));--tc-bg-muted: var(--bgColor-muted, #f6f8fa);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(0, 0, 0, .08));--tc-grid-size: var(--golden-number, 36px);--tc-grid-offset: calc(var(--tc-grid-size) / -2);--tc-font-stack: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif}.tc-canvas{min-width:100vw;min-height:100vh;margin:0;font-family:var(--tc-font-stack);position:relative;background-color:var(--tc-bg-muted);
|
|
1
|
+
:root{--tc-border-radius: var(--borderRadius-large, 12px);--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .12));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .15));--tc-border-color: var(--borderColor-muted, rgba(0, 0, 0, .15));--tc-bg-muted: var(--bgColor-muted, #f6f8fa);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(0, 0, 0, .08));--tc-grid-size: var(--golden-number, 36px);--tc-grid-offset: calc(var(--tc-grid-size) / -2);--tc-font-stack: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif}.tc-canvas{min-width:100vw;min-height:100vh;width:10000px;height:10000px;margin:0;font-family:var(--tc-font-stack);position:relative;background-color:var(--tc-bg-muted);color-scheme:light dark}.tc-canvas[data-color-mode=light]{color-scheme:light}.tc-canvas[data-color-mode=dark]{color-scheme:dark}.tc-canvas[data-dotted]{background-image:radial-gradient(var(--tc-dot-color) 2px,transparent 2px);background-size:var(--tc-grid-size) var(--tc-grid-size);background-position:var(--tc-grid-offset) var(--tc-grid-offset)}@media(prefers-color-scheme:dark){.tc-canvas:not([data-color-mode=light]){--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}}.tc-canvas[data-color-mode=dark]{--tc-bg-muted: var(--bgColor-muted, #161b22);--tc-dot-color: var(--overlay-backdrop-bgColor, rgba(255, 255, 255, .1));--tc-border-color: var(--borderColor-muted, rgba(255, 255, 255, .15));--tc-shadow-rest: var(--shadow-resting-small, 0 1px 3px rgba(0, 0, 0, .3));--tc-shadow-float: var(--shadow-floating-small, 0 4px 12px rgba(0, 0, 0, .4))}body .tc-draggable-inner:not(:active){touch-action:none}.tc-on-translation{transition:transform calc(var(--tc-translation-ms, .25s) * 2) cubic-bezier(.78,.1,.51,.78) var(--tc-translation-ms, .25s)}.tc-drag{position:absolute;width:fit-content}.tc-draggable-inner{position:relative;width:fit-content;height:min-content}.tc-draggable-inner:after{content:"";position:absolute;z-index:-1;width:100%;height:100%;top:0;border-radius:var(--tc-border-radius);box-shadow:var(--tc-shadow-float);opacity:0;transition:opacity .3s ease-in-out}.tc-on-translation .tc-draggable-inner:after,.tc-draggable-inner:active:after,.tc-draggable-inner:hover:after{opacity:0}.tc-on-translation .tc-draggable-inner,.tc-draggable-inner:active,.tc-draggable-inner:hover{outline:none}
|
package/dist/tiny-canvas.js
CHANGED
|
@@ -1,243 +1,244 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import me, { useRef as le, useState as
|
|
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
3
|
var ae = { dragStart: !0 }, se = { delay: 0, distance: 3 };
|
|
4
|
-
function Ae(e,
|
|
5
|
-
let n, o, { bounds: a, axis:
|
|
6
|
-
te(
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
let
|
|
10
|
-
function ge(
|
|
11
|
-
H && !
|
|
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 ?? {} };
|
|
9
|
+
let j = /* @__PURE__ */ new Set();
|
|
10
|
+
function ge(r) {
|
|
11
|
+
H && !T && O && X && Z && (T = !0, (function(s) {
|
|
12
12
|
re("neodrag:start", Se, s);
|
|
13
|
-
})(
|
|
13
|
+
})(r), A.add(D), C && (fe = ee.userSelect, ee.userSelect = "none"));
|
|
14
14
|
}
|
|
15
15
|
const ee = document.body.style, A = e.classList;
|
|
16
|
-
function te(
|
|
17
|
-
if (!
|
|
18
|
-
if (
|
|
19
|
-
let
|
|
20
|
-
return
|
|
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})`);
|
|
21
21
|
}
|
|
22
|
-
return
|
|
22
|
+
return z(e, "translate", `${+r}px ${+s}px`);
|
|
23
23
|
}
|
|
24
|
-
const
|
|
25
|
-
ie(
|
|
24
|
+
const f = y({ offsetX: r, offsetY: s, rootNode: e });
|
|
25
|
+
ie(f) && z(e, "transform", f);
|
|
26
26
|
}
|
|
27
|
-
function re(
|
|
28
|
-
const
|
|
29
|
-
return { offsetX: R, offsetY:
|
|
30
|
-
})(
|
|
31
|
-
e.dispatchEvent(new CustomEvent(
|
|
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);
|
|
32
32
|
}
|
|
33
|
-
const
|
|
33
|
+
const U = addEventListener, ne = new AbortController(), oe = { signal: ne.signal, capture: !1 };
|
|
34
34
|
function pe() {
|
|
35
|
-
let
|
|
36
|
-
return isNaN(
|
|
35
|
+
let r = e.offsetWidth / I.width;
|
|
36
|
+
return isNaN(r) && (r = 1), r;
|
|
37
37
|
}
|
|
38
|
-
return
|
|
39
|
-
if (
|
|
40
|
-
if (
|
|
41
|
-
if (
|
|
42
|
-
if (A.add(
|
|
43
|
-
if (!i) return [
|
|
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
44
|
if (ue(i)) return [i];
|
|
45
45
|
if (Array.isArray(i)) return i;
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
return Array.from(
|
|
49
|
-
})(
|
|
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
50
|
if (!i) return [];
|
|
51
51
|
if (ue(i)) return [i];
|
|
52
52
|
if (Array.isArray(i)) return i;
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
55
|
-
return Array.from(
|
|
56
|
-
})(
|
|
57
|
-
const s =
|
|
58
|
-
if (!
|
|
59
|
-
var
|
|
60
|
-
return i.contains(s) || ((
|
|
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
61
|
})) || ye(K, [s])) return;
|
|
62
|
-
Z =
|
|
63
|
-
const { clientX:
|
|
64
|
-
n && (
|
|
65
|
-
}), oe),
|
|
66
|
-
if (!H ||
|
|
67
|
-
if (!
|
|
68
|
-
if (
|
|
69
|
-
const h =
|
|
70
|
-
Math.sqrt(h ** 2 + i ** 2) >=
|
|
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));
|
|
71
71
|
}
|
|
72
|
-
if (!
|
|
72
|
+
if (!T) return;
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
let s =
|
|
76
|
-
const
|
|
77
|
-
if (
|
|
78
|
-
const h = { left:
|
|
79
|
-
s = he(s, h.left, h.right),
|
|
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);
|
|
80
80
|
}
|
|
81
|
-
if (Array.isArray(
|
|
82
|
-
let [h, i] =
|
|
81
|
+
if (Array.isArray(B)) {
|
|
82
|
+
let [h, i] = B;
|
|
83
83
|
if (isNaN(+h) || h < 0) throw new Error("1st argument of `grid` must be a valid positive number");
|
|
84
84
|
if (isNaN(+i) || i < 0) throw new Error("2nd argument of `grid` must be a valid positive number");
|
|
85
|
-
let
|
|
86
|
-
[
|
|
85
|
+
let b = s - L, E = f - k;
|
|
86
|
+
[b, E] = Ne([h / g, i / g], b, E), s = L + b, f = k + E;
|
|
87
87
|
}
|
|
88
|
-
n && (R = Math.round((s -
|
|
89
|
-
}), oe),
|
|
90
|
-
|
|
91
|
-
}), oe), { destroy: () => ne.abort(), update: (
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
const s = A.contains(
|
|
95
|
-
A.remove(
|
|
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());
|
|
96
96
|
} };
|
|
97
97
|
}
|
|
98
|
-
var he = (e,
|
|
99
|
-
const a = (
|
|
100
|
-
return [a(n, e), a(o,
|
|
101
|
-
}, ye = (e,
|
|
102
|
-
function ce(e,
|
|
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
103
|
if (e === void 0) return;
|
|
104
104
|
if (ue(e)) return e.getBoundingClientRect();
|
|
105
105
|
if (typeof e == "object") {
|
|
106
|
-
const { top: o = 0, left: a = 0, right:
|
|
107
|
-
return { top: o, right: window.innerWidth -
|
|
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 };
|
|
108
108
|
}
|
|
109
|
-
if (e === "parent") return
|
|
109
|
+
if (e === "parent") return t.parentNode.getBoundingClientRect();
|
|
110
110
|
const n = document.querySelector(e);
|
|
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
|
|
115
|
-
function
|
|
116
|
-
return e == null || typeof e == "string" || e instanceof HTMLElement ? e : "current" in e ? e.current : Array.isArray(e) ? e.map(((
|
|
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;
|
|
117
117
|
}
|
|
118
|
-
function qe(e,
|
|
119
|
-
const n = le(), [o, a] =
|
|
120
|
-
let { onDragStart:
|
|
121
|
-
function
|
|
122
|
-
|
|
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);
|
|
123
123
|
}
|
|
124
|
-
function
|
|
125
|
-
a(!0),
|
|
124
|
+
function v(d) {
|
|
125
|
+
a(!0), w(d, p);
|
|
126
126
|
}
|
|
127
|
-
function
|
|
128
|
-
|
|
127
|
+
function N(d) {
|
|
128
|
+
w(d, y);
|
|
129
129
|
}
|
|
130
|
-
function
|
|
131
|
-
a(!1),
|
|
130
|
+
function q(d) {
|
|
131
|
+
a(!1), w(d, C);
|
|
132
132
|
}
|
|
133
|
-
return
|
|
133
|
+
return Q((() => {
|
|
134
134
|
if (typeof window > "u") return;
|
|
135
|
-
const
|
|
136
|
-
if (!
|
|
137
|
-
({ onDragStart:
|
|
138
|
-
const { update:
|
|
139
|
-
return n.current =
|
|
140
|
-
}), []),
|
|
141
|
-
var
|
|
142
|
-
(
|
|
143
|
-
}), [
|
|
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 };
|
|
144
144
|
}
|
|
145
145
|
const Me = (e) => {
|
|
146
146
|
if (!e) return null;
|
|
147
|
-
let
|
|
147
|
+
let t = null;
|
|
148
148
|
const n = (o) => {
|
|
149
149
|
if (o.props && o.props.id) {
|
|
150
|
-
|
|
150
|
+
t = o.props.id;
|
|
151
151
|
return;
|
|
152
152
|
}
|
|
153
153
|
o.props && o.props.children && me.Children.forEach(o.props.children, n);
|
|
154
154
|
};
|
|
155
|
-
return n(e),
|
|
155
|
+
return n(e), t;
|
|
156
156
|
};
|
|
157
|
-
function
|
|
158
|
-
let
|
|
157
|
+
function Re(e) {
|
|
158
|
+
let t = 5381;
|
|
159
159
|
for (let n = 0; n < e.length; n++)
|
|
160
|
-
|
|
161
|
-
return
|
|
160
|
+
t = (t << 5) + t + e.charCodeAt(n) >>> 0;
|
|
161
|
+
return t.toString(16).padStart(8, "0");
|
|
162
162
|
}
|
|
163
163
|
function ve(e) {
|
|
164
|
-
var
|
|
164
|
+
var l;
|
|
165
165
|
if (e == null || typeof e == "boolean") return "";
|
|
166
166
|
if (typeof e == "string" || typeof e == "number") return "#text";
|
|
167
|
-
const
|
|
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;
|
|
168
168
|
if (o == null) return n;
|
|
169
169
|
const a = [];
|
|
170
|
-
return me.Children.forEach(o, (
|
|
171
|
-
const
|
|
172
|
-
|
|
170
|
+
return me.Children.forEach(o, (c) => {
|
|
171
|
+
const p = ve(c);
|
|
172
|
+
p && a.push(p);
|
|
173
173
|
}), a.length ? `${n}(${a.join(",")})` : n;
|
|
174
174
|
}
|
|
175
|
-
const
|
|
175
|
+
const $e = (e, t) => {
|
|
176
176
|
const n = ve(e);
|
|
177
|
-
return `tc-${
|
|
178
|
-
},
|
|
177
|
+
return `tc-${Re(n)}-${t}`;
|
|
178
|
+
}, Ie = (e) => {
|
|
179
179
|
try {
|
|
180
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 (
|
|
182
|
-
return console.error("Error getting saved coordinates:",
|
|
181
|
+
} catch (t) {
|
|
182
|
+
return console.error("Error getting saved coordinates:", t), { x: 0, y: 0 };
|
|
183
183
|
}
|
|
184
|
-
},
|
|
184
|
+
}, Te = () => {
|
|
185
185
|
try {
|
|
186
186
|
localStorage.getItem("tiny-canvas-queue") || localStorage.setItem("tiny-canvas-queue", JSON.stringify([]));
|
|
187
187
|
} catch (e) {
|
|
188
188
|
console.error("LocalStorage is not available:", e);
|
|
189
189
|
}
|
|
190
|
-
}, Le = (e,
|
|
190
|
+
}, Le = (e, t, n) => {
|
|
191
191
|
try {
|
|
192
|
-
const o = JSON.parse(localStorage.getItem("tiny-canvas-queue")) || [], a = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-"),
|
|
193
|
-
|
|
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));
|
|
194
194
|
} catch (o) {
|
|
195
195
|
console.error("Error saving drag position:", o);
|
|
196
196
|
}
|
|
197
197
|
}, ke = 250;
|
|
198
|
-
function Be({ children: e,
|
|
199
|
-
const
|
|
200
|
-
Math.random() < 0.5 ? -0.5 : 0.5
|
|
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
|
|
201
201
|
);
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
|
|
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
208
|
}, ke * 4);
|
|
209
|
-
return () =>
|
|
209
|
+
return () => {
|
|
210
|
+
clearTimeout(S);
|
|
211
|
+
};
|
|
210
212
|
}
|
|
211
|
-
}, []),
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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, {
|
|
215
219
|
axis: "both",
|
|
216
|
-
bounds: "body",
|
|
217
220
|
threshold: { delay: 50, distance: 30 },
|
|
218
221
|
defaultClass: "tc-drag",
|
|
219
222
|
defaultClassDragging: "tc-on",
|
|
220
223
|
defaultClassDragged: "tc-off",
|
|
221
224
|
applyUserSelectHack: !0,
|
|
222
|
-
position: { x:
|
|
223
|
-
onDrag: ({ offsetX:
|
|
224
|
-
onDragEnd: (
|
|
225
|
-
|
|
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);
|
|
226
229
|
}
|
|
227
|
-
}),
|
|
228
|
-
return
|
|
229
|
-
N(Math.random() < 0.5 ? -0.5 : 0.5);
|
|
230
|
-
}, [v]), /* @__PURE__ */ Q(
|
|
230
|
+
}), C = y ? `${c}deg` : "0deg";
|
|
231
|
+
return /* @__PURE__ */ W(
|
|
231
232
|
"article",
|
|
232
233
|
{
|
|
233
|
-
ref:
|
|
234
|
-
style: { cursor:
|
|
235
|
-
children: /* @__PURE__ */
|
|
234
|
+
ref: n,
|
|
235
|
+
style: { cursor: y ? "grabbing" : "grab" },
|
|
236
|
+
children: /* @__PURE__ */ W(
|
|
236
237
|
"div",
|
|
237
238
|
{
|
|
238
239
|
className: "tc-draggable-inner",
|
|
239
240
|
style: {
|
|
240
|
-
transform:
|
|
241
|
+
transform: y ? `rotate(${C})` : void 0,
|
|
241
242
|
transition: "transform ease-in-out 150ms"
|
|
242
243
|
},
|
|
243
244
|
children: e
|
|
@@ -246,44 +247,43 @@ function Be({ children: e, gridSize: r, dragId: n }) {
|
|
|
246
247
|
}
|
|
247
248
|
);
|
|
248
249
|
}
|
|
249
|
-
function
|
|
250
|
+
function Ye({
|
|
250
251
|
children: e,
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
colorMode: c = "auto"
|
|
252
|
+
dotted: t = !1,
|
|
253
|
+
grid: n = !1,
|
|
254
|
+
gridSize: o,
|
|
255
|
+
colorMode: a = "auto"
|
|
256
256
|
}) {
|
|
257
|
-
return /* @__PURE__ */
|
|
257
|
+
return /* @__PURE__ */ W(
|
|
258
258
|
"main",
|
|
259
259
|
{
|
|
260
260
|
className: "tc-canvas",
|
|
261
|
-
"data-dotted":
|
|
262
|
-
"data-color-mode":
|
|
263
|
-
children: Ee.map(e, (
|
|
264
|
-
const
|
|
265
|
-
return /* @__PURE__ */
|
|
261
|
+
"data-dotted": t || n || void 0,
|
|
262
|
+
"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);
|
|
266
266
|
})
|
|
267
267
|
}
|
|
268
268
|
);
|
|
269
269
|
}
|
|
270
|
-
function
|
|
270
|
+
function Oe({ reload: e = !1 } = {}) {
|
|
271
271
|
return Ce(() => {
|
|
272
272
|
try {
|
|
273
273
|
localStorage.removeItem("tiny-canvas-queue");
|
|
274
|
-
} catch (
|
|
275
|
-
console.error("Error clearing canvas state:",
|
|
274
|
+
} catch (t) {
|
|
275
|
+
console.error("Error clearing canvas state:", t);
|
|
276
276
|
}
|
|
277
277
|
e && window.location.reload();
|
|
278
278
|
}, [e]);
|
|
279
279
|
}
|
|
280
280
|
export {
|
|
281
|
-
|
|
281
|
+
Ye as Canvas,
|
|
282
282
|
Be as Draggable,
|
|
283
283
|
Me as findDragId,
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
284
|
+
$e as generateDragId,
|
|
285
|
+
Ie as getQueue,
|
|
286
|
+
Te as refreshStorage,
|
|
287
287
|
Le as saveDrag,
|
|
288
|
-
|
|
288
|
+
Oe as useResetCanvas
|
|
289
289
|
};
|
package/package.json
CHANGED
|
@@ -1,36 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dfosco/tiny-canvas",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A lightweight React canvas with draggable elements and persistent positions",
|
|
7
|
-
"main": "./
|
|
8
|
-
"module": "./
|
|
7
|
+
"main": "./dist/tiny-canvas.js",
|
|
8
|
+
"module": "./dist/tiny-canvas.js",
|
|
9
9
|
"types": "./src/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"import": "./
|
|
12
|
+
"import": "./dist/tiny-canvas.js",
|
|
13
13
|
"types": "./src/index.d.ts"
|
|
14
14
|
},
|
|
15
|
-
"./style.css": "./
|
|
16
|
-
},
|
|
17
|
-
"publishConfig": {
|
|
18
|
-
"main": "./dist/tiny-canvas.js",
|
|
19
|
-
"module": "./dist/tiny-canvas.js",
|
|
20
|
-
"exports": {
|
|
21
|
-
".": {
|
|
22
|
-
"import": "./dist/tiny-canvas.js",
|
|
23
|
-
"types": "./src/index.d.ts"
|
|
24
|
-
},
|
|
25
|
-
"./style.css": "./dist/tiny-canvas.css"
|
|
26
|
-
}
|
|
15
|
+
"./style.css": "./dist/tiny-canvas.css"
|
|
27
16
|
},
|
|
28
17
|
"files": [
|
|
29
18
|
"dist",
|
|
30
19
|
"src/index.d.ts"
|
|
31
20
|
],
|
|
32
21
|
"scripts": {
|
|
33
|
-
"build": "vite build"
|
|
22
|
+
"build": "vite build",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
34
24
|
},
|
|
35
25
|
"peerDependencies": {
|
|
36
26
|
"react": "^18.0.0 || ^19.0.0",
|