@snowcone-app/canvas 0.41.3 → 0.41.5

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 (58) hide show
  1. package/dist/{CanvasRenderer-JcBrIZ6I.js → CanvasRenderer-3M-52sAd.js} +4 -4
  2. package/dist/{CanvasRenderer-JcBrIZ6I.js.map → CanvasRenderer-3M-52sAd.js.map} +1 -1
  3. package/dist/{CanvasStateV1-BJ5-75NN.js → CanvasStateV1-C9tFMK5q.js} +3 -3
  4. package/dist/{CanvasStateV1-BJ5-75NN.js.map → CanvasStateV1-C9tFMK5q.js.map} +1 -1
  5. package/dist/{ContextualToolbars-wmbHxn5a.js → ContextualToolbars-B2LTNu7r.js} +358 -358
  6. package/dist/{ContextualToolbars-wmbHxn5a.js.map → ContextualToolbars-B2LTNu7r.js.map} +1 -1
  7. package/dist/{EditorCanvasImpl-CSDnCIpN.js → EditorCanvasImpl-CKqnla_y.js} +26 -26
  8. package/dist/EditorCanvasImpl-CKqnla_y.js.map +1 -0
  9. package/dist/{ExportManager-CHlByUYV.js → ExportManager-9_RjQEv0.js} +2 -2
  10. package/dist/{ExportManager-CHlByUYV.js.map → ExportManager-9_RjQEv0.js.map} +1 -1
  11. package/dist/{FontAnalyzer-Bj2KrX80.js → FontAnalyzer-Ch1jgTq7.js} +88 -68
  12. package/dist/FontAnalyzer-Ch1jgTq7.js.map +1 -0
  13. package/dist/ImagePanel-DY9Ve3X7.js +1016 -0
  14. package/dist/{ImagePanel-0ZwDDaCy.js.map → ImagePanel-DY9Ve3X7.js.map} +1 -1
  15. package/dist/SnowconeCanvas-BSFMazgl.js +1098 -0
  16. package/dist/SnowconeCanvas-BSFMazgl.js.map +1 -0
  17. package/dist/advanced.mjs +44 -44
  18. package/dist/api/stable.d.ts +1 -1
  19. package/dist/components/embed/SnowconeCanvas.d.ts +15 -0
  20. package/dist/components/embed/kernel.d.ts +3 -1
  21. package/dist/core/ArtboardElement.d.ts +2 -0
  22. package/dist/core/ArtboardManager.d.ts +11 -0
  23. package/dist/{custom-icons-up58ZbC9.js → custom-icons-D_XezOul.js} +2 -2
  24. package/dist/{custom-icons-up58ZbC9.js.map → custom-icons-D_XezOul.js.map} +1 -1
  25. package/dist/design-builder-BFM94eA9.js +1859 -0
  26. package/dist/design-builder-BFM94eA9.js.map +1 -0
  27. package/dist/{embed-GXevsHX5.js → embed-KG_YnRHA.js} +10 -10
  28. package/dist/{embed-GXevsHX5.js.map → embed-KG_YnRHA.js.map} +1 -1
  29. package/dist/embed.mjs +12 -12
  30. package/dist/{guards-CQeF53PN.js → guards-D_mw4DgS.js} +2 -2
  31. package/dist/{guards-CQeF53PN.js.map → guards-D_mw4DgS.js.map} +1 -1
  32. package/dist/index.mjs +9 -9
  33. package/dist/internals.mjs +6 -6
  34. package/dist/{placement-D31o3YYC.js → placement-tSiIVhrL.js} +3 -3
  35. package/dist/{placement-D31o3YYC.js.map → placement-tSiIVhrL.js.map} +1 -1
  36. package/dist/placement.mjs +2 -2
  37. package/dist/{template-fields-tobyHoMe.js → template-fields-C0qoE_ZH.js} +32 -32
  38. package/dist/{template-fields-tobyHoMe.js.map → template-fields-C0qoE_ZH.js.map} +1 -1
  39. package/dist/testing.mjs +3 -3
  40. package/dist/{useCommands-Cm3mDB-l.js → useCommands-DSapP-w1.js} +7 -7
  41. package/dist/{useCommands-Cm3mDB-l.js.map → useCommands-DSapP-w1.js.map} +1 -1
  42. package/dist/{usePieceGuideBundle-BTnUn33k.js → usePieceGuideBundle-oBykb3X8.js} +2 -2
  43. package/dist/{usePieceGuideBundle-BTnUn33k.js.map → usePieceGuideBundle-oBykb3X8.js.map} +1 -1
  44. package/dist/utils/newImagePlacement.d.ts +56 -0
  45. package/dist/{validation-D4zxugNJ.js → validation-D6zVSeTh.js} +307 -307
  46. package/dist/{validation-D4zxugNJ.js.map → validation-D6zVSeTh.js.map} +1 -1
  47. package/dist/vendor/ui/primitives/badge.d.ts +1 -1
  48. package/dist/vendor/ui/primitives/link.d.ts +1 -1
  49. package/dist/vendor/ui/primitives/spinner.d.ts +1 -1
  50. package/example/package.json +1 -1
  51. package/package.json +4 -4
  52. package/dist/EditorCanvasImpl-CSDnCIpN.js.map +0 -1
  53. package/dist/FontAnalyzer-Bj2KrX80.js.map +0 -1
  54. package/dist/ImagePanel-0ZwDDaCy.js +0 -984
  55. package/dist/SnowconeCanvas-BZk7PRpk.js +0 -1215
  56. package/dist/SnowconeCanvas-BZk7PRpk.js.map +0 -1
  57. package/dist/design-builder-DUEwCUmA.js +0 -1676
  58. package/dist/design-builder-DUEwCUmA.js.map +0 -1
@@ -0,0 +1,1859 @@
1
+ import { jsx as L } from "react/jsx-runtime";
2
+ import { useContext as G, useSyncExternalStore as Ee, useCallback as h, createContext as Q, useRef as gt, useState as E, useEffect as $t, useMemo as q } from "react";
3
+ import { H as Se, t as ot, U as Re, B as Ie, G as Jt, R as Te, D as Ce, J as Fe, K as ve, M as Pe, c as qt, z as Oe, N as Ne, O as ze, I as Vt, Q as Ue, e as He, E as Yt, q as ke, S as De, V as Le, X as Xt, Y as Ve, Z as _e, _ as $e, $ as Be, a0 as Me, i as We } from "./FontAnalyzer-Ch1jgTq7.js";
4
+ import { g as Qt, a as te } from "./placement-tSiIVhrL.js";
5
+ const ee = Q(null), Ye = () => () => {
6
+ }, Gt = () => 0;
7
+ function zo() {
8
+ const e = G(ee), t = Ee(
9
+ e ? e.subscribe : Ye,
10
+ e ? e.getVersion : Gt,
11
+ e ? e.getVersion : Gt
12
+ );
13
+ return h(
14
+ (o) => !!(e && o && e.getBoundLayer(o)),
15
+ // `version` is the reactivity key: a registry change must mint a new
16
+ // callback so memoized consumers re-evaluate.
17
+ [e, t]
18
+ );
19
+ }
20
+ const wt = qt("useCommandHistory");
21
+ function Xe(e, t, o, n) {
22
+ const i = gt(new Se(o)), r = G(ee), a = h(
23
+ (l, p) => {
24
+ const w = l == null ? void 0 : l.name;
25
+ if (!r || !w) return;
26
+ const A = r.getBoundLayer(w);
27
+ if (A)
28
+ throw new ot(
29
+ "E_BOUND_LAYER_MUTATION",
30
+ `cannot ${p} layer "${w}" — a mounted <${A.part} layer={design.layer("${w}")}> is bound to it. Unmount that field first, or keep the layer and set its text through the field / its binding instead.`,
31
+ { layer: w, part: A.part, operation: p, docSlug: "embed-mode" }
32
+ );
33
+ },
34
+ [r]
35
+ ), [s, c] = E({
36
+ canUndo: !1,
37
+ canRedo: !1,
38
+ // Per-artboard state for the active artboard
39
+ canUndoActiveArtboard: !1,
40
+ canRedoActiveArtboard: !1
41
+ }), d = h(() => {
42
+ const l = i.current, p = o.getActiveArtboardId();
43
+ c({
44
+ canUndo: l.canUndo(),
45
+ canRedo: l.canRedo(),
46
+ // Per-artboard state - only check active artboard's history
47
+ canUndoActiveArtboard: p ? l.canUndoArtboard(p) : !1,
48
+ canRedoActiveArtboard: p ? l.canRedoArtboard(p) : !1
49
+ });
50
+ }, [o]), S = h(
51
+ (l, p) => {
52
+ l && l.name !== p.name && a(l, "rename");
53
+ const w = new Re(
54
+ p.id,
55
+ l || null,
56
+ p,
57
+ (C) => {
58
+ t((Y) => Y.map((N) => N.id === C.id ? C : N));
59
+ }
60
+ ), A = o.getArtboardIdForElement(p.id);
61
+ if (A)
62
+ i.current.executeOnArtboard(A, w);
63
+ else {
64
+ const C = o.getActiveArtboardId();
65
+ C && i.current.executeOnArtboard(C, w);
66
+ }
67
+ d();
68
+ },
69
+ [t, o, d, a]
70
+ ), O = h(
71
+ (l, p, w, A) => {
72
+ const C = p || o.getActiveArtboardId();
73
+ if (!C) {
74
+ wt.warn("No active artboard to add element to");
75
+ return;
76
+ }
77
+ const Y = new Ie(
78
+ l,
79
+ (N) => {
80
+ const _ = N;
81
+ t((z) => {
82
+ if (A) {
83
+ const D = z.findIndex((P) => P.id === A);
84
+ if (D !== -1) {
85
+ const P = [...z];
86
+ return P.splice(D, 0, _), P;
87
+ }
88
+ }
89
+ if (w) {
90
+ const D = z.findIndex((P) => P.id === w);
91
+ if (D !== -1) {
92
+ const P = [...z];
93
+ return P.splice(D + 1, 0, _), P;
94
+ }
95
+ for (let P = 0; P < z.length; P++) {
96
+ const K = z[P];
97
+ if (K instanceof Jt && K.children) {
98
+ const rt = K.children.findIndex((Z) => Z.id === w);
99
+ if (rt !== -1) {
100
+ const Z = K.clone();
101
+ Z.children.splice(rt + 1, 0, _);
102
+ const j = [...z];
103
+ return j[P] = Z, j;
104
+ }
105
+ }
106
+ }
107
+ }
108
+ return [...z, _];
109
+ }), o.addElementToArtboard(N.id, C);
110
+ },
111
+ (N) => {
112
+ t((_) => _.filter((z) => z.id !== N)), o.removeElementFromArtboard(N);
113
+ }
114
+ );
115
+ i.current.executeOnArtboard(C, Y), d();
116
+ },
117
+ [t, o, d]
118
+ ), H = h(
119
+ (l) => {
120
+ a(l, "delete");
121
+ const p = o.getArtboardIdForElement(l.id);
122
+ if (!p) {
123
+ wt.warn("Element not found in any artboard");
124
+ return;
125
+ }
126
+ const w = new Te(
127
+ l,
128
+ (A) => {
129
+ t((C) => [...C, A]), o.addElementToArtboard(A.id, p);
130
+ },
131
+ (A) => {
132
+ t((C) => C.filter((Y) => Y.id !== A)), o.removeElementFromArtboard(A);
133
+ }
134
+ );
135
+ i.current.executeOnArtboard(p, w), d();
136
+ },
137
+ [t, o, d, a]
138
+ ), y = h(
139
+ (l) => {
140
+ const p = new Ce(l, o);
141
+ i.current.executeGlobal(p), d(), n == null || n();
142
+ },
143
+ [o, d, n]
144
+ ), k = h(
145
+ (l) => {
146
+ const p = new Fe(l, o);
147
+ i.current.executeGlobal(p), d(), n == null || n();
148
+ },
149
+ [o, d, n]
150
+ ), f = h(
151
+ (l, p, w) => {
152
+ const A = new ve(l, p, w, o);
153
+ i.current.executeGlobal(A), d(), n == null || n();
154
+ },
155
+ [o, d, n]
156
+ ), m = h(
157
+ (l) => {
158
+ if (l.length === 0)
159
+ return;
160
+ const p = o.getActiveArtboardId();
161
+ if (!p) {
162
+ wt.warn("No active artboard for batch execution");
163
+ return;
164
+ }
165
+ const w = l.map((A) => new class extends Object {
166
+ execute() {
167
+ A.execute();
168
+ }
169
+ undo() {
170
+ A.undo();
171
+ }
172
+ }());
173
+ i.current.executeBatchOnArtboard(p, w), d();
174
+ },
175
+ [o, d]
176
+ ), x = h(() => {
177
+ i.current.undo() && (d(), n == null || n());
178
+ }, [d, n]), I = h(() => {
179
+ i.current.redo() && (d(), n == null || n());
180
+ }, [d, n]), T = h(() => {
181
+ const l = o.getActiveArtboardId();
182
+ l && i.current.undoArtboard(l) && (d(), n == null || n());
183
+ }, [o, d, n]), R = h(() => {
184
+ const l = o.getActiveArtboardId();
185
+ l && i.current.redoArtboard(l) && (d(), n == null || n());
186
+ }, [o, d, n]), M = h(() => {
187
+ i.current.clear(), d();
188
+ }, [d]), nt = h(
189
+ (l) => {
190
+ i.current.clearArtboardHistory(l), d();
191
+ },
192
+ [d]
193
+ ), V = gt(null);
194
+ $t(() => {
195
+ const l = o.getActiveArtboardId();
196
+ l !== V.current && (V.current = l, d());
197
+ });
198
+ const W = h(
199
+ (l, p, w) => {
200
+ const A = e.map((N) => N.id), C = o.getArtboardIdForElement(l);
201
+ if (!C) {
202
+ wt.warn("Element not found in any artboard");
203
+ return;
204
+ }
205
+ const Y = new Pe(l, p, w, A, (N) => {
206
+ t((_) => {
207
+ const z = new Map(_.map((D) => [D.id, D]));
208
+ return N.map((D) => z.get(D)).filter(Boolean);
209
+ });
210
+ });
211
+ i.current.executeOnArtboard(C, Y), d();
212
+ },
213
+ [e, t, o, d]
214
+ );
215
+ return {
216
+ // Element operations
217
+ executeElementUpdate: S,
218
+ executeAddElement: O,
219
+ executeRemoveElement: H,
220
+ executeReorderElement: W,
221
+ // Batch operations
222
+ executeCommandBatch: m,
223
+ // Artboard operations
224
+ executeCreateArtboard: y,
225
+ executeDeleteArtboard: k,
226
+ executeUpdateArtboard: f,
227
+ // Undo/redo (smart - considers last operation and active artboard)
228
+ undo: x,
229
+ redo: I,
230
+ // Undo/redo for active artboard only (element operations)
231
+ undoActiveArtboard: T,
232
+ redoActiveArtboard: R,
233
+ clearHistory: M,
234
+ clearArtboardHistory: nt,
235
+ // State (smart - considers last operation)
236
+ canUndo: s.canUndo,
237
+ canRedo: s.canRedo,
238
+ // State for active artboard only
239
+ canUndoActiveArtboard: s.canUndoActiveArtboard,
240
+ canRedoActiveArtboard: s.canRedoActiveArtboard,
241
+ historyManager: i.current
242
+ };
243
+ }
244
+ function dt(e) {
245
+ return e && "fontSize" in e && "color" in e && "fontFamily" in e;
246
+ }
247
+ function Ge(e, t) {
248
+ const [o, n] = E(32), [i, r] = E("#333333"), [a, s] = E("Arial"), [c, d] = E("center");
249
+ $t(() => {
250
+ if (e && dt(e)) {
251
+ const f = e.transformType === "circle" && "getEffectiveFontSize" in e ? Math.round(e.getEffectiveFontSize()) : Math.round(e.fontSize), m = e.color, x = e.fontFamily, I = e.textAlign;
252
+ n((T) => T !== f ? f : T), r((T) => T !== m ? m : T), s((T) => T !== x ? x : T), d((T) => T !== I ? I : T);
253
+ }
254
+ }, [e]);
255
+ const S = h(
256
+ (f) => {
257
+ if (!e || !dt(e)) return;
258
+ const m = Math.max(Ne, Math.min(ze, o + f));
259
+ n(m);
260
+ const x = e.clone();
261
+ x.transformType === "circle" && "setEffectiveFontSize" in x ? x.setEffectiveFontSize(m) : x.setFontSize(m), t(x);
262
+ },
263
+ [e, o, t]
264
+ ), O = h(
265
+ (f) => {
266
+ if (!e || !dt(e)) return;
267
+ n(f);
268
+ const m = e.clone();
269
+ m.transformType === "circle" && "setEffectiveFontSize" in m ? m.setEffectiveFontSize(f) : m.setFontSize(f), t(m);
270
+ },
271
+ [e, t]
272
+ ), H = h(
273
+ (f) => {
274
+ if (!e || !dt(e)) return;
275
+ r(f);
276
+ const m = e.clone();
277
+ if (m.color = f, m.richText)
278
+ for (const x of m.richText.spans)
279
+ x.style.color = void 0;
280
+ t(m);
281
+ },
282
+ [e, t]
283
+ ), y = h(
284
+ (f) => {
285
+ if (!e || !dt(e)) return;
286
+ const m = e.fontFamily;
287
+ m !== f && Oe.clearCache(m), s(f);
288
+ const x = e.clone();
289
+ x.fontFamily = f, t(x);
290
+ },
291
+ [e, t]
292
+ ), k = h(
293
+ (f) => {
294
+ if (!e || !dt(e)) return;
295
+ d(f);
296
+ const m = e.clone();
297
+ m.textAlign = f, t(m);
298
+ },
299
+ [e, t]
300
+ );
301
+ return {
302
+ // State
303
+ fontSize: o,
304
+ fontColor: i,
305
+ fontFamily: a,
306
+ textAlign: c,
307
+ // State setters (for UI only)
308
+ setFontSize: n,
309
+ setFontColor: r,
310
+ setFontFamily: s,
311
+ setTextAlign: d,
312
+ // Update functions (update element + UI)
313
+ updateFontSize: S,
314
+ setFontSizeValue: O,
315
+ updateFontColor: H,
316
+ updateFontFamily: y,
317
+ updateTextAlign: k
318
+ };
319
+ }
320
+ const oe = "data-preserve-selection";
321
+ function Ke(e) {
322
+ if (!(e instanceof Element))
323
+ return !1;
324
+ let t = e;
325
+ const o = [];
326
+ for (; t; ) {
327
+ const n = t.id ? `#${t.id}` : "", i = t.className ? `.${String(t.className).split(" ").slice(0, 2).join(".")}` : "";
328
+ if (o.push(`${t.tagName.toLowerCase()}${n}${i}`), t.hasAttribute(oe))
329
+ return !0;
330
+ const r = t.getAttribute("role");
331
+ if (r === "dialog" || r === "alertdialog" || r === "menu" || r === "listbox")
332
+ return !0;
333
+ const a = t.getAttribute("data-slot");
334
+ if (a && (a.includes("popover") || a.includes("dialog")) || t.hasAttribute("data-radix-popper-content-wrapper"))
335
+ return !0;
336
+ t = t.parentElement;
337
+ }
338
+ return !1;
339
+ }
340
+ const Uo = {
341
+ [oe]: ""
342
+ };
343
+ function Ze(e) {
344
+ const t = ne(e);
345
+ return t !== null && t < 0.5;
346
+ }
347
+ function Ho(e) {
348
+ const t = ne(e);
349
+ return t !== null && t >= 0.5;
350
+ }
351
+ function ne(e) {
352
+ const t = e.trim();
353
+ if (t.startsWith("#")) {
354
+ const n = t.slice(1);
355
+ let i, r, a;
356
+ if (n.length === 3)
357
+ i = parseInt(n[0] + n[0], 16), r = parseInt(n[1] + n[1], 16), a = parseInt(n[2] + n[2], 16);
358
+ else if (n.length === 6)
359
+ i = parseInt(n.slice(0, 2), 16), r = parseInt(n.slice(2, 4), 16), a = parseInt(n.slice(4, 6), 16);
360
+ else return null;
361
+ return Number.isNaN(i) || Number.isNaN(r) || Number.isNaN(a) ? null : Kt(i, r, a);
362
+ }
363
+ const o = t.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/);
364
+ return o ? Kt(Number(o[1]), Number(o[2]), Number(o[3])) : null;
365
+ }
366
+ function Kt(e, t, o) {
367
+ const n = (i) => {
368
+ const r = i / 255;
369
+ return r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
370
+ };
371
+ return 0.2126 * n(e) + 0.7152 * n(t) + 0.0722 * n(o);
372
+ }
373
+ const je = "#333333", Je = "#f4f4f5";
374
+ function qe(e) {
375
+ return e && Ze(e) ? Je : je;
376
+ }
377
+ const Qe = {
378
+ timeout: 3e4,
379
+ retries: 1,
380
+ retryDelay: 1e3,
381
+ validateUrl: !0
382
+ };
383
+ function to(e) {
384
+ try {
385
+ const t = new URL(e);
386
+ return ["http:", "https:", "data:", "blob:"].includes(t.protocol);
387
+ } catch {
388
+ return !1;
389
+ }
390
+ }
391
+ async function re(e, t = {}) {
392
+ const o = { ...Qe, ...t };
393
+ if (o.validateUrl && !to(e))
394
+ return {
395
+ success: !1,
396
+ error: new Error(`Invalid image URL: ${e}`)
397
+ };
398
+ let n;
399
+ for (let i = 0; i <= o.retries; i++)
400
+ try {
401
+ return await eo(e, o.timeout);
402
+ } catch (r) {
403
+ n = r instanceof Error ? r : new Error(String(r)), i < o.retries && await oo(o.retryDelay);
404
+ }
405
+ return {
406
+ success: !1,
407
+ error: n || new Error("Failed to load image after retries")
408
+ };
409
+ }
410
+ function eo(e, t) {
411
+ return new Promise((o) => {
412
+ const n = new Image();
413
+ n.crossOrigin = "anonymous";
414
+ let i = null, r = !1;
415
+ const a = () => {
416
+ i && (clearTimeout(i), i = null);
417
+ }, s = () => {
418
+ if (r) return;
419
+ r = !0, a();
420
+ const d = n.naturalWidth || n.width, S = n.naturalHeight || n.height;
421
+ o({
422
+ success: !0,
423
+ element: n,
424
+ width: d,
425
+ height: S,
426
+ aspectRatio: S > 0 ? d / S : 1
427
+ });
428
+ }, c = (d) => {
429
+ r || (r = !0, a(), o({
430
+ success: !1,
431
+ error: new Error(d)
432
+ }));
433
+ };
434
+ n.onload = s, n.onerror = () => {
435
+ c(`Failed to load image: ${e}`);
436
+ }, i = setTimeout(() => {
437
+ c(`Image load timed out after ${t}ms: ${e}`);
438
+ }, t), n.src = e;
439
+ });
440
+ }
441
+ async function ko(e, t, o = {}, n = {}) {
442
+ var H, y, k, f, m, x, I;
443
+ const i = await re(e, n);
444
+ if (!i.success || !i.width || !i.height)
445
+ return {
446
+ success: !1,
447
+ error: i.error || new Error("Failed to get image dimensions")
448
+ };
449
+ const r = {
450
+ width: t.artboard.width,
451
+ height: t.artboard.height,
452
+ scale: t.scale,
453
+ align: Qt(t.alignment),
454
+ offsetX: t.offsetX,
455
+ offsetY: t.offsetY,
456
+ scaleMode: t.scaleMode,
457
+ marginTop: t.marginTop,
458
+ marginRight: t.marginRight,
459
+ marginBottom: t.marginBottom,
460
+ marginLeft: t.marginLeft
461
+ }, a = te(
462
+ { width: i.width, height: i.height },
463
+ r
464
+ ), s = t.artboard.x + a.x, c = t.artboard.y + a.y, d = s + a.width / 2, S = c + a.height / 2;
465
+ return {
466
+ success: !0,
467
+ element: new Vt({
468
+ ...o,
469
+ x: d,
470
+ y: S,
471
+ imageUrl: e,
472
+ imageAspectRatio: i.aspectRatio,
473
+ preserveDimensions: !0,
474
+ // Don't recalculate dimensions on image load
475
+ transformData: {
476
+ type: "image",
477
+ width: a.width,
478
+ height: a.height,
479
+ cropX: ((H = o.transformData) == null ? void 0 : H.cropX) ?? 0,
480
+ cropY: ((y = o.transformData) == null ? void 0 : y.cropY) ?? 0,
481
+ cropWidth: ((k = o.transformData) == null ? void 0 : k.cropWidth) ?? 1,
482
+ cropHeight: ((f = o.transformData) == null ? void 0 : f.cropHeight) ?? 1,
483
+ flipHorizontal: ((m = o.transformData) == null ? void 0 : m.flipHorizontal) ?? !1,
484
+ flipVertical: ((x = o.transformData) == null ? void 0 : x.flipVertical) ?? !1,
485
+ borderRadius: ((I = o.transformData) == null ? void 0 : I.borderRadius) ?? 0
486
+ }
487
+ }),
488
+ placement: {
489
+ artworkSize: { width: i.width, height: i.height },
490
+ finalSize: { width: a.width, height: a.height },
491
+ position: { x: d, y: S },
492
+ scale: a.scale
493
+ }
494
+ };
495
+ }
496
+ function oo(e) {
497
+ return new Promise((t) => setTimeout(t, e));
498
+ }
499
+ function Do(e) {
500
+ if (e != null && e.scaleMode)
501
+ return {
502
+ scaleMode: e.scaleMode,
503
+ marginTop: e.fitMarginTop,
504
+ marginRight: e.fitMarginRight,
505
+ marginBottom: e.fitMarginBottom,
506
+ marginLeft: e.fitMarginLeft,
507
+ align: e.fitAlign
508
+ };
509
+ }
510
+ function ie(e) {
511
+ var t;
512
+ return ((t = e == null ? void 0 : e.imageFit) == null ? void 0 : t.scaleMode) === "contain";
513
+ }
514
+ function no(e, t) {
515
+ if (!ie(e)) return null;
516
+ if (!(t.width > 0) || !(t.height > 0))
517
+ throw new Error(
518
+ `E_NEW_IMAGE_SIZE: a new picture of ${t.width}x${t.height} cannot be placed on a ${e.width}x${e.height} area; pass the picture's natural size (loadImageFromURL).`
519
+ );
520
+ const o = e.imageFit, n = te(t, {
521
+ width: e.width,
522
+ height: e.height,
523
+ scaleMode: "contain",
524
+ align: Qt(o.align),
525
+ marginTop: o.marginTop,
526
+ marginRight: o.marginRight,
527
+ marginBottom: o.marginBottom,
528
+ marginLeft: o.marginLeft
529
+ });
530
+ return {
531
+ x: e.x + n.x + n.width / 2,
532
+ y: e.y + n.y + n.height / 2,
533
+ width: n.width,
534
+ height: n.height
535
+ };
536
+ }
537
+ async function ro(e, t) {
538
+ if (!ie(e)) return null;
539
+ const o = await re(t, { validateUrl: !1 });
540
+ return !o.success || !o.width || !o.height ? null : no(e, { width: o.width, height: o.height });
541
+ }
542
+ const se = Q(null), io = 0.1, so = 4, Zt = 0.25, ao = ({
543
+ children: e,
544
+ artboardManager: t,
545
+ canvasRef: o,
546
+ viewPadding: n = 0.9
547
+ }) => {
548
+ const [i, r] = E(1), [a, s] = E({ x: 0, y: 0 }), [c, d] = E(!1), [S, O] = E(1), H = h(() => {
549
+ O(1), s({ x: 0, y: 0 });
550
+ }, []), y = h(() => {
551
+ r((I) => Math.min(I + Zt, so));
552
+ }, []), k = h(() => {
553
+ r((I) => Math.max(I - Zt, io));
554
+ }, []), f = h(() => {
555
+ const I = t.getActiveArtboard();
556
+ if (!I || !o.current) return;
557
+ let T = o.current.parentElement, R = 0, M = 0;
558
+ for (; T && (R = T.clientWidth, M = T.clientHeight, !(R > 0 && M > 0)); )
559
+ T = T.parentElement;
560
+ if (!R || !M) return;
561
+ const nt = Math.min(
562
+ R * n / I.width,
563
+ M * n / I.height,
564
+ 1
565
+ // Never zoom in beyond 100%
566
+ );
567
+ r(nt), s({ x: 0, y: 0 });
568
+ }, [t, o, n]), m = h(() => {
569
+ r(1), s({ x: 0, y: 0 });
570
+ }, []), x = q(
571
+ () => ({
572
+ zoom: i,
573
+ panOffset: a,
574
+ isPanning: c,
575
+ zoomIn: y,
576
+ zoomOut: k,
577
+ zoomToFit: f,
578
+ resetView: m,
579
+ setZoom: r,
580
+ setPanOffset: s,
581
+ setIsPanning: d,
582
+ userZoom: S,
583
+ setUserZoom: O,
584
+ resetUserView: H
585
+ }),
586
+ [i, a, c, y, k, f, m, S, H]
587
+ );
588
+ return /* @__PURE__ */ L(se.Provider, { value: x, children: e });
589
+ };
590
+ function co() {
591
+ const e = G(se);
592
+ if (!e)
593
+ throw new Error("useViewportContext must be used within a ViewportProvider (or EditorProvider)");
594
+ return e;
595
+ }
596
+ const ae = Q(null), lo = ({ children: e }) => {
597
+ const [t, o] = E(null), [n, i] = E([]), [r, a] = E(null), [s, c] = E(null), [d, S] = E(!1), O = h((k) => {
598
+ o(k);
599
+ }, []), H = h((k) => {
600
+ a(k);
601
+ }, []), y = q(
602
+ () => ({
603
+ selectedId: t,
604
+ multiSelection: n,
605
+ activeChildElement: r,
606
+ hoveredElementId: s,
607
+ hideHandles: d,
608
+ setSelectedId: o,
609
+ setMultiSelection: i,
610
+ setHoveredElementId: c,
611
+ setHideHandles: S,
612
+ handleSelectionChange: O,
613
+ handleActiveChildChange: H
614
+ }),
615
+ [
616
+ t,
617
+ n,
618
+ r,
619
+ s,
620
+ d,
621
+ O,
622
+ H
623
+ ]
624
+ );
625
+ return /* @__PURE__ */ L(ae.Provider, { value: y, children: e });
626
+ };
627
+ function uo() {
628
+ const e = G(ae);
629
+ if (!e)
630
+ throw new Error("useSelectionContext must be used within a SelectionProvider (or EditorProvider)");
631
+ return e;
632
+ }
633
+ const ce = Q(null), ho = ({ children: e, value: t }) => {
634
+ const o = q(
635
+ () => ({
636
+ undo: t.undo,
637
+ redo: t.redo,
638
+ canUndo: t.canUndo,
639
+ canRedo: t.canRedo,
640
+ undoActiveArtboard: t.undoActiveArtboard,
641
+ redoActiveArtboard: t.redoActiveArtboard,
642
+ canUndoActiveArtboard: t.canUndoActiveArtboard,
643
+ canRedoActiveArtboard: t.canRedoActiveArtboard
644
+ }),
645
+ [
646
+ t.undo,
647
+ t.redo,
648
+ t.canUndo,
649
+ t.canRedo,
650
+ t.undoActiveArtboard,
651
+ t.redoActiveArtboard,
652
+ t.canUndoActiveArtboard,
653
+ t.canRedoActiveArtboard
654
+ ]
655
+ );
656
+ return /* @__PURE__ */ L(ce.Provider, { value: o, children: e });
657
+ };
658
+ function Lo() {
659
+ const e = G(ce);
660
+ if (!e)
661
+ throw new Error("useHistoryContext must be used within a HistoryProvider (or EditorProvider)");
662
+ return e;
663
+ }
664
+ const de = Q(null), mo = ({ children: e }) => {
665
+ const [t, o] = E(null), [n, i] = E(!1), [r, a] = E(0), [s, c] = E(!1), [d, S] = E(!1), O = q(
666
+ () => ({
667
+ expandedPanelType: t,
668
+ setExpandedPanelType: o,
669
+ isToolbarMenuOpen: n,
670
+ setIsToolbarMenuOpen: i,
671
+ textSelectionVersion: r,
672
+ setTextSelectionVersion: a,
673
+ isCanvasReady: s,
674
+ setCanvasReady: c,
675
+ isRotating: d,
676
+ setIsRotating: S
677
+ }),
678
+ [t, n, r, s, d]
679
+ );
680
+ return /* @__PURE__ */ L(de.Provider, { value: O, children: e });
681
+ };
682
+ function fo() {
683
+ const e = G(de);
684
+ if (!e)
685
+ throw new Error("useToolStateContext must be used within a ToolStateProvider (or EditorProvider)");
686
+ return e;
687
+ }
688
+ const le = Q(null), po = ({ children: e, value: t }) => {
689
+ const o = q(
690
+ () => ({
691
+ elements: t.elements,
692
+ elementStore: t.elementStore,
693
+ artboards: t.artboards,
694
+ artboardManager: t.artboardManager,
695
+ artboardVersion: t.artboardVersion,
696
+ setElements: t.setElements,
697
+ refreshArtboards: t.refreshArtboards,
698
+ getElementById: t.getElementById,
699
+ handleAddElement: t.handleAddElement,
700
+ handleElementUpdate: t.handleElementUpdate,
701
+ handleLoadWorkspace: t.handleLoadWorkspace,
702
+ handleCopyElements: t.handleCopyElements,
703
+ handlePasteElements: t.handlePasteElements,
704
+ fontSize: t.fontSize,
705
+ fontColor: t.fontColor,
706
+ fontFamily: t.fontFamily,
707
+ textAlign: t.textAlign,
708
+ updateFontSize: t.updateFontSize,
709
+ setFontSizeValue: t.setFontSizeValue,
710
+ updateFontColor: t.updateFontColor,
711
+ updateFontFamily: t.updateFontFamily,
712
+ updateTextAlign: t.updateTextAlign,
713
+ canvasRef: t.canvasRef,
714
+ canvasEditorRef: t.canvasEditorRef
715
+ }),
716
+ [
717
+ t.elements,
718
+ t.elementStore,
719
+ t.artboards,
720
+ t.artboardManager,
721
+ t.artboardVersion,
722
+ t.setElements,
723
+ t.refreshArtboards,
724
+ t.getElementById,
725
+ t.handleAddElement,
726
+ t.handleElementUpdate,
727
+ t.handleLoadWorkspace,
728
+ t.handleCopyElements,
729
+ t.handlePasteElements,
730
+ t.fontSize,
731
+ t.fontColor,
732
+ t.fontFamily,
733
+ t.textAlign,
734
+ t.updateFontSize,
735
+ t.setFontSizeValue,
736
+ t.updateFontColor,
737
+ t.updateFontFamily,
738
+ t.updateTextAlign,
739
+ t.canvasRef,
740
+ t.canvasEditorRef
741
+ ]
742
+ );
743
+ return /* @__PURE__ */ L(le.Provider, { value: o, children: e });
744
+ };
745
+ function Vo() {
746
+ const e = G(le);
747
+ if (!e)
748
+ throw new Error("useElementsContext must be used within an ElementsProvider (or EditorProvider)");
749
+ return e;
750
+ }
751
+ const ue = Q(null), go = ({ children: e, value: t }) => {
752
+ const o = q(
753
+ () => ({
754
+ executeElementUpdate: t.executeElementUpdate,
755
+ executeAddElement: t.executeAddElement,
756
+ executeRemoveElement: t.executeRemoveElement,
757
+ executeReorderElement: t.executeReorderElement,
758
+ executeCreateArtboard: t.executeCreateArtboard,
759
+ executeDeleteArtboard: t.executeDeleteArtboard,
760
+ executeUpdateArtboard: t.executeUpdateArtboard,
761
+ executeCommandBatch: t.executeCommandBatch,
762
+ undo: t.undo,
763
+ redo: t.redo,
764
+ canUndo: t.canUndo,
765
+ canRedo: t.canRedo,
766
+ undoActiveArtboard: t.undoActiveArtboard,
767
+ redoActiveArtboard: t.redoActiveArtboard,
768
+ canUndoActiveArtboard: t.canUndoActiveArtboard,
769
+ canRedoActiveArtboard: t.canRedoActiveArtboard,
770
+ historyManager: t.historyManager
771
+ }),
772
+ [
773
+ t.executeElementUpdate,
774
+ t.executeAddElement,
775
+ t.executeRemoveElement,
776
+ t.executeReorderElement,
777
+ t.executeCreateArtboard,
778
+ t.executeDeleteArtboard,
779
+ t.executeUpdateArtboard,
780
+ t.executeCommandBatch,
781
+ t.undo,
782
+ t.redo,
783
+ t.canUndo,
784
+ t.canRedo,
785
+ t.undoActiveArtboard,
786
+ t.redoActiveArtboard,
787
+ t.canUndoActiveArtboard,
788
+ t.canRedoActiveArtboard,
789
+ t.historyManager
790
+ ]
791
+ );
792
+ return /* @__PURE__ */ L(ue.Provider, { value: o, children: e });
793
+ };
794
+ function _o() {
795
+ const e = G(ue);
796
+ if (!e)
797
+ throw new Error("useCommandContext must be used within a CommandProvider (or EditorProvider)");
798
+ return e;
799
+ }
800
+ const Ao = qt("EditorContext"), he = Q(null), me = Q(null), $o = () => {
801
+ const e = G(me);
802
+ if (!e)
803
+ throw new Error("useEditorHandles must be used within EditorProvider");
804
+ return e;
805
+ }, yo = ({ children: e, artboardManager: t, canvasRef: o, canvasEditorRef: n }) => {
806
+ const i = gt(null), r = q(() => ({ get: () => i.current }), []), a = co(), s = uo(), c = fo(), [d, S] = E(
807
+ () => t.getAllArtboards()
808
+ ), [O, H] = E(0), [y, k] = E(
809
+ () => new Yt()
810
+ ), f = q(() => y.toArray(), [y]), m = h((u) => {
811
+ k((g) => {
812
+ const b = g.toArray(), F = typeof u == "function" ? u(b) : u;
813
+ return F === b ? g : Yt.fromArray(F);
814
+ });
815
+ }, []), x = h(
816
+ (u) => y.get(u),
817
+ [y]
818
+ ), [I, T] = E([]), R = s.selectedId ? y.get(s.selectedId) : void 0, M = h(() => {
819
+ S([...t.getAllArtboards()]), H((u) => u + 1);
820
+ }, [t]), {
821
+ historyManager: nt,
822
+ executeElementUpdate: V,
823
+ executeAddElement: W,
824
+ executeRemoveElement: l,
825
+ executeReorderElement: p,
826
+ executeCommandBatch: w,
827
+ executeCreateArtboard: A,
828
+ executeDeleteArtboard: C,
829
+ executeUpdateArtboard: Y,
830
+ undo: N,
831
+ redo: _,
832
+ canUndo: z,
833
+ canRedo: D,
834
+ undoActiveArtboard: P,
835
+ redoActiveArtboard: K,
836
+ canUndoActiveArtboard: rt,
837
+ canRedoActiveArtboard: Z
838
+ } = Xe(
839
+ f,
840
+ m,
841
+ t,
842
+ M
843
+ ), j = h(
844
+ (u) => {
845
+ const g = y.get(u.id);
846
+ V(g, u);
847
+ },
848
+ [y, V]
849
+ ), ye = h(
850
+ (u) => {
851
+ if (!R || !(R instanceof Jt))
852
+ return;
853
+ const g = R.clone(), b = g.children.findIndex(
854
+ (F) => F.id === u.id
855
+ );
856
+ b >= 0 && (g.children[b] = u, j(g));
857
+ },
858
+ [R, j]
859
+ ), xe = s.activeChildElement || R, we = s.activeChildElement ? ye : j, {
860
+ fontSize: Et,
861
+ fontColor: St,
862
+ fontFamily: Rt,
863
+ textAlign: It,
864
+ updateFontSize: Tt,
865
+ setFontSizeValue: Ct,
866
+ updateFontColor: Ft,
867
+ updateFontFamily: vt,
868
+ updateTextAlign: Pt
869
+ } = Ge(xe, we), At = h(
870
+ async (u) => {
871
+ var Mt;
872
+ const g = u.type, b = u, F = t.getActiveArtboardId();
873
+ if (!F)
874
+ throw new ot(
875
+ "E_NO_ACTIVE_ARTBOARD",
876
+ `addElement({ type: "${g}" }) needs an active artboard and none exists. <Editor.Root> seeds one automatically; with a raw EditorProvider create one first (executeCreateArtboard) or select one (artboardManager.setActiveArtboard).`,
877
+ { type: g, docSlug: "embed-mode" }
878
+ );
879
+ const v = `element-${Date.now()}`, B = t.getActiveArtboard(), ht = B ? B.x + B.width / 2 : window.innerWidth / 2, st = B ? B.y + B.height / 2 : window.innerHeight / 2, J = ((Mt = n.current) == null ? void 0 : Mt.getZoom()) || a.zoom || 1;
880
+ if (g === "image") {
881
+ const ft = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", et = 200 / J, $ = b == null ? void 0 : b.src, pt = $ ? await ro(B, $) : null;
882
+ if (pt) {
883
+ const U = new Vt({
884
+ id: v,
885
+ name: u.name,
886
+ x: pt.x,
887
+ y: pt.y,
888
+ imageUrl: "",
889
+ preserveDimensions: !0,
890
+ transformData: { type: "image", width: pt.width, height: pt.height }
891
+ });
892
+ return U.imageUrl = $, U.onLoadCallback = (xt) => V(U, xt), W(U, F), s.setSelectedId(U.id), U.loadImage($), { id: U.id, name: u.name ?? "" };
893
+ }
894
+ const X = new Vt({
895
+ id: v,
896
+ name: u.name,
897
+ x: ht,
898
+ y: st,
899
+ // Defer loading `src` until after the element is in the store so a
900
+ // synchronously-cached image can't fire onLoadCallback too early.
901
+ imageUrl: $ ? "" : ft,
902
+ transformData: {
903
+ type: "image",
904
+ width: et,
905
+ height: et,
906
+ cropX: 0,
907
+ cropY: 0,
908
+ cropWidth: 1,
909
+ cropHeight: 1,
910
+ flipHorizontal: !1,
911
+ flipVertical: !1,
912
+ borderRadius: 0
913
+ }
914
+ });
915
+ return $ && (X.imageLoaded = !1, X.imageElement = null, X.isCropping = !1, X.imageUrl = $, X.preserveDimensions = !1, X.onLoadCallback = (U) => {
916
+ const xt = U.transformData.width, Wt = U.transformData.height, Dt = Math.min(et / xt, et / Wt, 1);
917
+ Dt < 1 && (U.transformData.width = xt * Dt, U.transformData.height = Wt * Dt), U.transformData.cropX = 0, U.transformData.cropY = 0, U.transformData.cropWidth = 1, U.transformData.cropHeight = 1, V(X, U);
918
+ }), W(X, F), s.setSelectedId(X.id), $ && X.loadImage($), { id: X.id, name: u.name ?? "" };
919
+ }
920
+ let tt;
921
+ const ct = {
922
+ text: "custom",
923
+ circle: "circle",
924
+ wave: "wave",
925
+ arch: "arch",
926
+ shape: "shape"
927
+ }[g] || "custom", Ht = ke(ct);
928
+ if (!Ht || !Ht.Component)
929
+ throw new Error(`[EditorContext] no transform registered for element type "${g}"`);
930
+ const mt = t.getActiveArtboard(), be = mt ? Math.min(mt.width, mt.height) * 0.1 : 80, kt = mt ? mt.width * 0.5 : 300;
931
+ if (g === "shape") {
932
+ const ft = (b == null ? void 0 : b.shapeType) ?? "rectangle";
933
+ let et = 200 / J, $ = 200 / J;
934
+ ft === "ellipse" ? (et = 150 / J, $ = 250 / J) : ft === "line" && (et = 300 / J, $ = 8 / J), tt = new De({
935
+ id: v,
936
+ name: u.name,
937
+ x: ht,
938
+ y: st,
939
+ transformData: {
940
+ type: "shape",
941
+ shapeType: ft,
942
+ width: et,
943
+ height: $,
944
+ fillColor: "#3b82f6",
945
+ fillOpacity: 1
946
+ }
947
+ });
948
+ } else
949
+ tt = new Ht.Component({
950
+ id: v,
951
+ name: u.name,
952
+ text: "New Text",
953
+ x: ht,
954
+ y: st,
955
+ fontSize: be,
956
+ fontFamily: "Poppins",
957
+ color: qe(B == null ? void 0 : B.backgroundColor),
958
+ transformData: ct === "arch" ? {
959
+ type: "arch",
960
+ width: kt,
961
+ archHeight: Le.archHeight
962
+ } : ct === "wave" ? {
963
+ type: "wave",
964
+ width: kt,
965
+ amplitude: Xt.amplitude,
966
+ frequency: Xt.frequency
967
+ } : ct === "circle" ? {
968
+ type: "circle",
969
+ radius: kt / 2
970
+ } : void 0
971
+ });
972
+ return t.addElementToArtboard(v, F), W(
973
+ tt,
974
+ F,
975
+ s.selectedId || void 0
976
+ ), s.setSelectedId(v), { id: v, name: u.name ?? "" };
977
+ },
978
+ [t, W, V, s, a.zoom, n]
979
+ ), Ot = h(
980
+ async (u, g) => {
981
+ await At({ ...g, type: u });
982
+ },
983
+ [At]
984
+ ), Nt = h(() => {
985
+ const u = [];
986
+ if (s.multiSelection.length > 0) {
987
+ const g = f.filter(
988
+ (b) => s.multiSelection.includes(b.id)
989
+ );
990
+ u.push(...g);
991
+ } else R && u.push(R);
992
+ if (u.length > 0) {
993
+ const g = u.map((b) => b.clone());
994
+ T(g);
995
+ }
996
+ }, [R, s.multiSelection, f]), zt = h(() => {
997
+ if (I.length === 0) return;
998
+ const u = [], g = 30, b = 30;
999
+ I.forEach((F) => {
1000
+ const v = y.get(F.id), B = v ? v.x : F.x, ht = v ? v.y : F.y, st = F.toJSON();
1001
+ delete st.id;
1002
+ const J = F.constructor, tt = new J(st);
1003
+ tt.x = B + g, tt.y = ht + b;
1004
+ let at = null;
1005
+ if (v && (at = t.getArtboardIdForElement(
1006
+ v.id
1007
+ )), at || (at = t.getActiveArtboardId()), !at) {
1008
+ Ao.warn("[handlePasteElements] No artboard found for pasting");
1009
+ return;
1010
+ }
1011
+ const ct = v ? v.id : void 0;
1012
+ W(tt, at, ct), u.push(tt.id);
1013
+ }), u.length === 1 ? s.handleSelectionChange(u[0]) : u.length > 1 && s.setMultiSelection(u);
1014
+ }, [
1015
+ I,
1016
+ y,
1017
+ t,
1018
+ W,
1019
+ s
1020
+ ]), yt = h(() => {
1021
+ if (c.expandedPanelType === "crop" && (R == null ? void 0 : R.transformType) === "image") {
1022
+ const u = R;
1023
+ if (u.isCropping) {
1024
+ const g = u.clone();
1025
+ g.exitCropMode(), V(u, g);
1026
+ }
1027
+ }
1028
+ c.setExpandedPanelType(null);
1029
+ }, [
1030
+ c.expandedPanelType,
1031
+ c.setExpandedPanelType,
1032
+ R,
1033
+ V
1034
+ ]);
1035
+ $t(() => {
1036
+ const u = (g) => {
1037
+ const b = g.target;
1038
+ if (b.tagName === "CANVAS" || Ke(b))
1039
+ return;
1040
+ const v = document.activeElement;
1041
+ if (v != null && v.hasAttribute("data-preserve-selection")) {
1042
+ v.blur();
1043
+ return;
1044
+ }
1045
+ if (c.expandedPanelType !== null) {
1046
+ yt();
1047
+ return;
1048
+ }
1049
+ s.selectedId !== null && s.setSelectedId(null), s.multiSelection.length > 0 && s.setMultiSelection([]);
1050
+ };
1051
+ return document.addEventListener("pointerdown", u), () => document.removeEventListener("pointerdown", u);
1052
+ }, [c.expandedPanelType, yt, s]);
1053
+ const Ut = h(
1054
+ (u, g, b) => {
1055
+ t.clear(), u.forEach((F) => {
1056
+ t.createArtboard(F.toJSON());
1057
+ }), b && t.setActiveArtboard(b), S([...t.getAllArtboards()]), H((F) => F + 1), m(g), s.setSelectedId(null);
1058
+ },
1059
+ [t, s, m]
1060
+ ), Bt = q(() => ({
1061
+ // From ViewportContext
1062
+ zoom: a.zoom,
1063
+ panOffset: a.panOffset,
1064
+ isPanning: a.isPanning,
1065
+ zoomIn: a.zoomIn,
1066
+ zoomOut: a.zoomOut,
1067
+ zoomToFit: a.zoomToFit,
1068
+ resetView: a.resetView,
1069
+ setZoom: a.setZoom,
1070
+ setPanOffset: a.setPanOffset,
1071
+ setIsPanning: a.setIsPanning,
1072
+ // From SelectionContext
1073
+ selectedId: s.selectedId,
1074
+ multiSelection: s.multiSelection,
1075
+ activeChildElement: s.activeChildElement,
1076
+ hoveredElementId: s.hoveredElementId,
1077
+ hideHandles: s.hideHandles,
1078
+ setSelectedId: s.setSelectedId,
1079
+ setMultiSelection: s.setMultiSelection,
1080
+ setHoveredElementId: s.setHoveredElementId,
1081
+ setHideHandles: s.setHideHandles,
1082
+ handleSelectionChange: s.handleSelectionChange,
1083
+ handleActiveChildChange: s.handleActiveChildChange,
1084
+ // From ToolStateContext
1085
+ expandedPanelType: c.expandedPanelType,
1086
+ setExpandedPanelType: c.setExpandedPanelType,
1087
+ isToolbarMenuOpen: c.isToolbarMenuOpen,
1088
+ setIsToolbarMenuOpen: c.setIsToolbarMenuOpen,
1089
+ textSelectionVersion: c.textSelectionVersion,
1090
+ setTextSelectionVersion: c.setTextSelectionVersion,
1091
+ isCanvasReady: c.isCanvasReady,
1092
+ setCanvasReady: c.setCanvasReady,
1093
+ isRotating: c.isRotating,
1094
+ setIsRotating: c.setIsRotating,
1095
+ // Cross-context handler
1096
+ clearExpandedPanel: yt,
1097
+ // Core state (element/artboard domain)
1098
+ artboardManager: t,
1099
+ artboards: d,
1100
+ elements: f,
1101
+ elementStore: y,
1102
+ getElementById: x,
1103
+ // Derived state (cross-context)
1104
+ selectedElement: R,
1105
+ // Refs
1106
+ canvasRef: o,
1107
+ canvasEditorRef: n,
1108
+ // Element setters
1109
+ setElements: m,
1110
+ // Artboard version counter
1111
+ artboardVersion: O,
1112
+ // Handlers
1113
+ refreshArtboards: M,
1114
+ handleElementUpdate: j,
1115
+ addElement: At,
1116
+ handleAddElement: Ot,
1117
+ handleLoadWorkspace: Ut,
1118
+ handleCopyElements: Nt,
1119
+ handlePasteElements: zt,
1120
+ hasClipboardContent: I.length > 0,
1121
+ // Command history (undo/redo actions + execute functions)
1122
+ historyManager: nt,
1123
+ executeElementUpdate: V,
1124
+ executeAddElement: W,
1125
+ executeRemoveElement: l,
1126
+ executeReorderElement: p,
1127
+ executeCommandBatch: w,
1128
+ executeCreateArtboard: A,
1129
+ executeDeleteArtboard: C,
1130
+ executeUpdateArtboard: Y,
1131
+ undo: N,
1132
+ redo: _,
1133
+ canUndo: z,
1134
+ canRedo: D,
1135
+ undoActiveArtboard: P,
1136
+ redoActiveArtboard: K,
1137
+ canUndoActiveArtboard: rt,
1138
+ canRedoActiveArtboard: Z,
1139
+ // Element properties
1140
+ fontSize: Et,
1141
+ fontColor: St,
1142
+ fontFamily: Rt,
1143
+ textAlign: It,
1144
+ updateFontSize: Tt,
1145
+ setFontSizeValue: Ct,
1146
+ updateFontColor: Ft,
1147
+ updateFontFamily: vt,
1148
+ updateTextAlign: Pt
1149
+ }), [
1150
+ // Viewport
1151
+ a.zoom,
1152
+ a.panOffset,
1153
+ a.isPanning,
1154
+ a.zoomIn,
1155
+ a.zoomOut,
1156
+ a.zoomToFit,
1157
+ a.resetView,
1158
+ a.setZoom,
1159
+ a.setPanOffset,
1160
+ a.setIsPanning,
1161
+ // Selection
1162
+ s.selectedId,
1163
+ s.multiSelection,
1164
+ s.activeChildElement,
1165
+ s.hoveredElementId,
1166
+ s.hideHandles,
1167
+ s.setSelectedId,
1168
+ s.setMultiSelection,
1169
+ s.setHoveredElementId,
1170
+ s.setHideHandles,
1171
+ s.handleSelectionChange,
1172
+ s.handleActiveChildChange,
1173
+ // Tool state
1174
+ c.expandedPanelType,
1175
+ c.setExpandedPanelType,
1176
+ c.isToolbarMenuOpen,
1177
+ c.setIsToolbarMenuOpen,
1178
+ c.textSelectionVersion,
1179
+ c.setTextSelectionVersion,
1180
+ c.isCanvasReady,
1181
+ c.setCanvasReady,
1182
+ c.isRotating,
1183
+ c.setIsRotating,
1184
+ yt,
1185
+ // Element/artboard domain
1186
+ t,
1187
+ d,
1188
+ f,
1189
+ y,
1190
+ x,
1191
+ R,
1192
+ o,
1193
+ n,
1194
+ m,
1195
+ O,
1196
+ M,
1197
+ j,
1198
+ At,
1199
+ Ot,
1200
+ Ut,
1201
+ Nt,
1202
+ zt,
1203
+ I,
1204
+ // Command history
1205
+ nt,
1206
+ V,
1207
+ W,
1208
+ l,
1209
+ p,
1210
+ w,
1211
+ A,
1212
+ C,
1213
+ Y,
1214
+ N,
1215
+ _,
1216
+ z,
1217
+ D,
1218
+ P,
1219
+ K,
1220
+ rt,
1221
+ Z,
1222
+ // Element properties
1223
+ Et,
1224
+ St,
1225
+ Rt,
1226
+ It,
1227
+ Tt,
1228
+ Ct,
1229
+ Ft,
1230
+ vt,
1231
+ Pt
1232
+ ]);
1233
+ return i.current = Bt, /* @__PURE__ */ L(me.Provider, { value: r, children: /* @__PURE__ */ L(
1234
+ ho,
1235
+ {
1236
+ value: {
1237
+ undo: N,
1238
+ redo: _,
1239
+ canUndo: z,
1240
+ canRedo: D,
1241
+ undoActiveArtboard: P,
1242
+ redoActiveArtboard: K,
1243
+ canUndoActiveArtboard: rt,
1244
+ canRedoActiveArtboard: Z
1245
+ },
1246
+ children: /* @__PURE__ */ L(
1247
+ po,
1248
+ {
1249
+ value: {
1250
+ elements: f,
1251
+ elementStore: y,
1252
+ artboards: d,
1253
+ artboardManager: t,
1254
+ artboardVersion: O,
1255
+ setElements: m,
1256
+ refreshArtboards: M,
1257
+ getElementById: x,
1258
+ handleAddElement: Ot,
1259
+ handleElementUpdate: j,
1260
+ handleLoadWorkspace: Ut,
1261
+ handleCopyElements: Nt,
1262
+ handlePasteElements: zt,
1263
+ fontSize: Et,
1264
+ fontColor: St,
1265
+ fontFamily: Rt,
1266
+ textAlign: It,
1267
+ updateFontSize: Tt,
1268
+ setFontSizeValue: Ct,
1269
+ updateFontColor: Ft,
1270
+ updateFontFamily: vt,
1271
+ updateTextAlign: Pt,
1272
+ canvasRef: o,
1273
+ canvasEditorRef: n
1274
+ },
1275
+ children: /* @__PURE__ */ L(
1276
+ go,
1277
+ {
1278
+ value: {
1279
+ executeElementUpdate: V,
1280
+ executeAddElement: W,
1281
+ executeRemoveElement: l,
1282
+ executeReorderElement: p,
1283
+ executeCommandBatch: w,
1284
+ executeCreateArtboard: A,
1285
+ executeDeleteArtboard: C,
1286
+ executeUpdateArtboard: Y,
1287
+ undo: N,
1288
+ redo: _,
1289
+ canUndo: z,
1290
+ canRedo: D,
1291
+ undoActiveArtboard: P,
1292
+ redoActiveArtboard: K,
1293
+ canUndoActiveArtboard: rt,
1294
+ canRedoActiveArtboard: Z,
1295
+ historyManager: nt
1296
+ },
1297
+ children: /* @__PURE__ */ L(he.Provider, { value: Bt, children: e })
1298
+ }
1299
+ )
1300
+ }
1301
+ )
1302
+ }
1303
+ ) });
1304
+ }, Bo = ({
1305
+ children: e,
1306
+ initialArtboardConfig: t = {},
1307
+ viewPadding: o = 0.9
1308
+ }) => {
1309
+ const [n] = E(() => {
1310
+ const a = new He(), s = {
1311
+ name: t.name || "Artboard 1",
1312
+ x: t.x ?? 0,
1313
+ y: t.y ?? 0,
1314
+ width: t.width ?? 1e3,
1315
+ height: t.height ?? 1e3,
1316
+ backgroundColor: t.backgroundColor || Ue
1317
+ };
1318
+ return a.createArtboard(s), a;
1319
+ }), i = gt(null), r = gt(null);
1320
+ return /* @__PURE__ */ L(
1321
+ ao,
1322
+ {
1323
+ artboardManager: n,
1324
+ canvasRef: i,
1325
+ viewPadding: o,
1326
+ children: /* @__PURE__ */ L(lo, { children: /* @__PURE__ */ L(mo, { children: /* @__PURE__ */ L(
1327
+ yo,
1328
+ {
1329
+ artboardManager: n,
1330
+ canvasRef: i,
1331
+ canvasEditorRef: r,
1332
+ children: e
1333
+ }
1334
+ ) }) })
1335
+ }
1336
+ );
1337
+ }, Mo = () => {
1338
+ const e = G(he);
1339
+ if (!e)
1340
+ throw new Error("useEditor must be used within EditorProvider");
1341
+ return e;
1342
+ }, ut = [];
1343
+ function Wo(e) {
1344
+ _e() && ut.push(e);
1345
+ }
1346
+ function Yo() {
1347
+ return [...ut];
1348
+ }
1349
+ function Xo() {
1350
+ ut.length = 0;
1351
+ }
1352
+ function Go() {
1353
+ if (ut.length === 0) return;
1354
+ const e = ut.map((t) => Ve(t) ? `${t.code}: ${t.message}` : `${t.name}: ${t.message}`).join(`
1355
+ - `);
1356
+ throw ut.length = 0, new Error(
1357
+ `expectNoEditorErrors: ${e.includes(`
1358
+ `) ? "errors were" : "an error was"} contained by <Editor.Root>'s boundary during this test:
1359
+ - ${e}
1360
+ A contained error renders an error card in prod — this composition is broken even if the test rendered "successfully".`
1361
+ );
1362
+ }
1363
+ const xo = 1;
1364
+ function it(e) {
1365
+ return typeof e == "number" && Number.isFinite(e) ? Math.round(e * 100) / 100 : e;
1366
+ }
1367
+ function fe(e) {
1368
+ return {
1369
+ ...e,
1370
+ ...e.x !== void 0 && { x: it(e.x) },
1371
+ ...e.y !== void 0 && { y: it(e.y) },
1372
+ ...e.width !== void 0 && { width: it(e.width) },
1373
+ ...e.height !== void 0 && { height: it(e.height) },
1374
+ ...e.rotation !== void 0 && { rotation: it(e.rotation) }
1375
+ };
1376
+ }
1377
+ function wo(e) {
1378
+ var n, i;
1379
+ const t = (n = e.artboards) != null && n.length ? e.artboards : [{ name: "Front", width: 800, height: 800 }], o = t.map(() => []);
1380
+ for (const r of e.elements || []) {
1381
+ const a = r == null ? void 0 : r.artboard, s = typeof a == "string" ? t.findIndex((c) => (c.name || "Front") === a) : -1;
1382
+ (r == null ? void 0 : r.visible) !== !1 && o[s >= 0 ? s : 0].push(pe(r));
1383
+ }
1384
+ return {
1385
+ schemaVersion: xo,
1386
+ artboards: t.map((r, a) => ({
1387
+ id: `artboard-${a + 1}`,
1388
+ name: r.name || "Front",
1389
+ x: 0,
1390
+ y: 0,
1391
+ width: it(r.width || 800),
1392
+ height: it(r.height || 800),
1393
+ backgroundColor: r.backgroundColor || "transparent",
1394
+ // Pass the artboard's flag through — the server worker only paints the
1395
+ // background when this is true. Hardcoding `false` here silently dropped
1396
+ // explicitly-set artboard backgrounds from every realtime mockup while
1397
+ // the editor kept showing them (WYSIWYG drift).
1398
+ exportBackground: r.exportBackground ?? !1,
1399
+ elements: o[a],
1400
+ distressTexture: r.distressTexture,
1401
+ imageMask: r.imageMask
1402
+ })),
1403
+ // Custom (brand) font dependencies ride the wire so the server worker can
1404
+ // load them — without this, server renders of custom-font designs fall
1405
+ // back to the wrong family even when the bundle supports custom fonts.
1406
+ ...(i = e.customFonts) != null && i.length ? { customFonts: e.customFonts } : {}
1407
+ };
1408
+ }
1409
+ function pe(e) {
1410
+ const t = e.type || e.transformType;
1411
+ if (t === "image")
1412
+ return bo(e);
1413
+ const { artboard: o, ...n } = e;
1414
+ return t === "group" && Array.isArray(e.children) && (n.children = e.children.filter((i) => i && i.visible !== !1).map(pe)), fe({
1415
+ ...n,
1416
+ type: t
1417
+ });
1418
+ }
1419
+ function bo(e) {
1420
+ var d;
1421
+ const t = e.transformData || {}, o = $e(e), n = Be(t, o), i = t.width || 0, r = t.height || 0, a = i * n.cropWidth, s = r * n.cropHeight, c = {
1422
+ id: e.id,
1423
+ type: "image",
1424
+ x: e.x || 0,
1425
+ y: e.y || 0,
1426
+ width: a,
1427
+ height: s,
1428
+ rotation: e.rotation || 0,
1429
+ imageUrl: e.imageUrl,
1430
+ imageAspectRatio: e.imageAspectRatio
1431
+ };
1432
+ return Me(n) || (c.cropX = n.cropX, c.cropY = n.cropY, c.cropWidth = n.cropWidth, c.cropHeight = n.cropHeight, c.needsCropPixelConversion = !0), t.flipHorizontal && (c.flipHorizontal = t.flipHorizontal), t.flipVertical && (c.flipVertical = t.flipVertical), t.borderRadius && (c.borderRadius = t.borderRadius), e.shadow && (c.shadow = e.shadow), We(t.adjustments) || (c.adjustments = { ...t.adjustments }), e.opacity !== void 0 && (c.opacity = e.opacity), e.distressEffect && (c.distressEffect = e.distressEffect), ((d = e.masks) == null ? void 0 : d.length) > 0 && (c.masks = e.masks), e.blendMode && (c.blendMode = e.blendMode), e.knockoutParts && (c.knockoutParts = e.knockoutParts), e.stroke && (c.stroke = e.stroke), fe(c);
1433
+ }
1434
+ const jt = "center", ge = Symbol.for("snowcone.canvas.layerToken");
1435
+ function Ko(e) {
1436
+ return typeof e == "object" && e !== null && e[ge] === !0 && typeof e.name == "string" && (e.kind === "text" || e.kind === "image");
1437
+ }
1438
+ function Eo(e, t) {
1439
+ return Object.freeze({ kind: e, name: t, [ge]: !0 });
1440
+ }
1441
+ const Ae = Symbol.for("snowcone.canvas.colorToken");
1442
+ function So(e) {
1443
+ return typeof e == "object" && e !== null && e[Ae] === !0 && typeof e.name == "string" && typeof e.value == "string";
1444
+ }
1445
+ function Ro(e, t) {
1446
+ return Object.freeze({ name: e, value: t, [Ae]: !0 });
1447
+ }
1448
+ function Io(e) {
1449
+ let t = e.trim().toUpperCase();
1450
+ return /^#[0-9A-F]{3}$/.test(t) && (t = "#" + t[1] + t[1] + t[2] + t[2] + t[3] + t[3]), /^#[0-9A-F]{6}$/.test(t) ? t : null;
1451
+ }
1452
+ function bt(e) {
1453
+ return So(e) ? e.value : e;
1454
+ }
1455
+ function _t(e) {
1456
+ const t = e.transformType;
1457
+ return t === "image" ? "image" : t === "shape" || t === "group" || t === "path" ? null : "text";
1458
+ }
1459
+ function To(e) {
1460
+ return e === "top-left" || e === "left" || e === "bottom-left" ? "left" : e === "top-right" || e === "right" || e === "bottom-right" ? "right" : "center";
1461
+ }
1462
+ function Co(e) {
1463
+ return e === "top-left" || e === "top" || e === "top-right" ? "top" : e === "bottom-left" || e === "bottom" || e === "bottom-right" ? "bottom" : "middle";
1464
+ }
1465
+ function Lt(e, t, o, n, i, r) {
1466
+ const a = To(e), s = Co(e), c = a === "left" ? t / 2 + r : a === "right" ? n - t / 2 - r : n / 2, d = s === "top" ? o / 2 + r : s === "bottom" ? i - o / 2 - r : i / 2;
1467
+ return { x: c, y: d };
1468
+ }
1469
+ function Fo(e, t, o) {
1470
+ const n = t / o;
1471
+ if (e >= n) {
1472
+ const a = o * e, s = t / a;
1473
+ return {
1474
+ width: a,
1475
+ height: o,
1476
+ cropX: (1 - s) / 2,
1477
+ cropY: 0,
1478
+ cropWidth: s,
1479
+ cropHeight: 1
1480
+ };
1481
+ }
1482
+ const i = t / e, r = o / i;
1483
+ return {
1484
+ width: t,
1485
+ height: i,
1486
+ cropX: 0,
1487
+ cropY: (1 - r) / 2,
1488
+ cropWidth: 1,
1489
+ cropHeight: r
1490
+ };
1491
+ }
1492
+ class lt {
1493
+ constructor(t) {
1494
+ if (this.els = [], this.colorRoles = /* @__PURE__ */ new Map(), this.idCounter = 0, !(t.width > 0) || !(t.height > 0))
1495
+ throw new Error("design(): artboard width and height must be positive numbers");
1496
+ for (const [o, n] of Object.entries(t.colors ?? {})) {
1497
+ const i = Io(n);
1498
+ if (i === null)
1499
+ throw new ot(
1500
+ "E_COLOR_ROLE_INVALID",
1501
+ `color role "${o}" is ${JSON.stringify(n)} — roles must be #RGB/#RRGGBB hex (useColorBinding matches by normalized hex value; other syntaxes can't bind).`,
1502
+ { roleName: o, value: n, docSlug: "embed-mode" }
1503
+ );
1504
+ const r = [...this.colorRoles.entries()].find(([, a]) => a === i);
1505
+ if (r)
1506
+ throw new ot(
1507
+ "E_COLOR_ROLE_COLLISION",
1508
+ `color roles "${r[0]}" and "${o}" both normalize to ${i}. useColorBinding matches elements by VALUE, so colliding roles would repaint each other's elements — give every role a distinct hex.`,
1509
+ { roles: [r[0], o], value: i, docSlug: "embed-mode" }
1510
+ );
1511
+ this.colorRoles.set(o, i);
1512
+ }
1513
+ this.current = {
1514
+ name: t.name || "Front",
1515
+ width: t.width,
1516
+ height: t.height,
1517
+ backgroundColor: bt(t.backgroundColor),
1518
+ role: t.role
1519
+ }, this.activeName = this.current.name;
1520
+ }
1521
+ /**
1522
+ * Seed a builder from a previously saved canvas state — the editor shape
1523
+ * `<SnowconeCanvas onChange>` emits (and `toJSON()` produces).
1524
+ *
1525
+ * This is how a saved editable template re-enters the turnkey embed path:
1526
+ * load the template's state JSON, lift it back into a `DesignBuilder`, and
1527
+ * hand it to `<RealtimeEmbed design={…}>`. By default the active artboard
1528
+ * becomes the builder's artboard; elements tagged for other artboards are
1529
+ * skipped (untagged elements are assumed to belong to the active one).
1530
+ *
1531
+ * Pass `{ artboards: 'all' }` to load a multi-page template (ADR-0086)
1532
+ * whole: every artboard (with its pacing `role`) and every element are
1533
+ * kept, membership tags preserved, so `toJSON()` / `toServerRequest()`
1534
+ * emit all pages and `TemplateField.artboard` can scope fields per page.
1535
+ */
1536
+ static fromState(t, o = {}) {
1537
+ const n = t.artboards.find((r) => r.name === t.activeArtboard) ?? t.artboards[0];
1538
+ if (!n)
1539
+ throw new Error("DesignBuilder.fromState(): state has no artboards");
1540
+ const i = new lt({
1541
+ name: n.name,
1542
+ width: n.width,
1543
+ height: n.height,
1544
+ backgroundColor: n.backgroundColor,
1545
+ role: n.role
1546
+ });
1547
+ if (o.artboards === "all") {
1548
+ i.allArtboards = t.artboards.map((r) => ({ ...r }));
1549
+ for (const r of t.elements)
1550
+ i.els.push(r.artboard ? { ...r } : { ...r, artboard: n.name });
1551
+ return i;
1552
+ }
1553
+ for (const r of t.elements)
1554
+ r.artboard && r.artboard !== n.name || i.els.push({ ...r });
1555
+ return i;
1556
+ }
1557
+ nextId(t) {
1558
+ return `${t}-${++this.idCounter}`;
1559
+ }
1560
+ /** A wire-shape snapshot of an authoring artboard, applying the same
1561
+ * background-export rule everywhere (a design background is part of the
1562
+ * design — exports and server renders paint it). */
1563
+ static snapshotArtboard(t) {
1564
+ return {
1565
+ name: t.name,
1566
+ width: t.width,
1567
+ height: t.height,
1568
+ ...t.backgroundColor ? {
1569
+ backgroundColor: t.backgroundColor,
1570
+ exportBackground: t.backgroundColor !== "transparent"
1571
+ } : {},
1572
+ ...t.role ? { role: t.role } : {}
1573
+ };
1574
+ }
1575
+ /**
1576
+ * Open a NEW artboard (placement) and make it the authoring context: layer
1577
+ * calls after this target the new artboard — anchor math uses its
1578
+ * dimensions, and the elements carry its membership tag (the public
1579
+ * `artboard` field, PR #482). The FIRST artboard (from `design({ name })`)
1580
+ * remains the initially-active placement when the design mounts; switch at
1581
+ * runtime with `usePlacements()` / `<Editor.Placements/>` (ADR-0088).
1582
+ *
1583
+ * `label` must be the product's placement label (case-sensitive) — the same
1584
+ * artboard-name-is-placement-label rule as `design({ name })`.
1585
+ *
1586
+ * ```ts
1587
+ * const seed = design({ name: 'Front', width: 1480, height: 2328 })
1588
+ * .text('YOUR NAME', { anchor: 'top', name: 'headline' })
1589
+ * .artboard('Back', { width: 1480, height: 2328 })
1590
+ * .text('EST. 2026', { anchor: 'center', name: 'backline' });
1591
+ * ```
1592
+ *
1593
+ * @throws EditorError `E_DUPLICATE_ARTBOARD` — the design already has an
1594
+ * artboard with this label.
1595
+ */
1596
+ artboard(t, o) {
1597
+ if (!(o.width > 0) || !(o.height > 0))
1598
+ throw new Error("design().artboard(): width and height must be positive numbers");
1599
+ if (this.allArtboards || (this.allArtboards = [lt.snapshotArtboard(this.current)]), this.allArtboards.some((n) => n.name === t))
1600
+ throw new ot(
1601
+ "E_DUPLICATE_ARTBOARD",
1602
+ `the design already has an artboard "${t}" — artboard names are placement labels and must be unique. Existing: ${JSON.stringify(this.allArtboards.map((n) => n.name))}.`,
1603
+ { artboardName: t, validNames: this.allArtboards.map((n) => n.name), docSlug: "embed-mode" }
1604
+ );
1605
+ for (const n of this.els)
1606
+ n.artboard || (n.artboard = this.current.name);
1607
+ return this.current = {
1608
+ name: t,
1609
+ width: o.width,
1610
+ height: o.height,
1611
+ backgroundColor: bt(o.backgroundColor),
1612
+ role: o.role
1613
+ }, this.allArtboards.push(lt.snapshotArtboard(this.current)), this;
1614
+ }
1615
+ background(t, o = {}) {
1616
+ const { width: n, height: i } = this.current;
1617
+ let r;
1618
+ if (o.aspectRatio && o.aspectRatio > 0) {
1619
+ const s = Fo(o.aspectRatio, n, i);
1620
+ r = {
1621
+ type: "image",
1622
+ width: s.width,
1623
+ height: s.height,
1624
+ cropX: s.cropX,
1625
+ cropY: s.cropY,
1626
+ cropWidth: s.cropWidth,
1627
+ cropHeight: s.cropHeight,
1628
+ flipHorizontal: !1,
1629
+ flipVertical: !1,
1630
+ borderRadius: 0
1631
+ };
1632
+ } else
1633
+ r = {
1634
+ type: "image",
1635
+ width: n,
1636
+ height: i,
1637
+ cropX: 0,
1638
+ cropY: 0,
1639
+ cropWidth: 1,
1640
+ cropHeight: 1,
1641
+ flipHorizontal: !1,
1642
+ flipVertical: !1,
1643
+ borderRadius: 0
1644
+ };
1645
+ const a = {
1646
+ id: this.nextId("bg"),
1647
+ transformType: "image",
1648
+ x: n / 2,
1649
+ y: i / 2,
1650
+ rotation: 0,
1651
+ opacity: o.opacity ?? 1,
1652
+ imageUrl: t,
1653
+ imageAspectRatio: o.aspectRatio,
1654
+ transformData: r,
1655
+ name: o.name ?? "Background"
1656
+ };
1657
+ return this.els.unshift(a), this;
1658
+ }
1659
+ image(t, o = {}) {
1660
+ const { width: n, height: i } = this.current, r = o.width ?? Math.round(n / 3), a = o.height ?? (o.aspectRatio ? Math.round(r / o.aspectRatio) : r), { x: s, y: c } = Lt(o.anchor ?? jt, r, a, n, i, o.padding ?? 0), d = {
1661
+ id: this.nextId("img"),
1662
+ transformType: "image",
1663
+ x: s,
1664
+ y: c,
1665
+ rotation: o.rotation ?? 0,
1666
+ opacity: o.opacity ?? 1,
1667
+ imageUrl: t,
1668
+ imageAspectRatio: o.aspectRatio,
1669
+ transformData: {
1670
+ type: "image",
1671
+ width: r,
1672
+ height: a,
1673
+ cropX: 0,
1674
+ cropY: 0,
1675
+ cropWidth: 1,
1676
+ cropHeight: 1,
1677
+ flipHorizontal: !1,
1678
+ flipVertical: !1,
1679
+ borderRadius: 0
1680
+ },
1681
+ ...o.name ? { name: o.name } : {}
1682
+ };
1683
+ return this.els.push(d), this;
1684
+ }
1685
+ text(t, o = {}) {
1686
+ const { width: n, height: i } = this.current, r = o.fontSize ?? Math.round(this.current.height / 12), { x: a, y: s } = Lt(o.anchor ?? "top", 0, r, n, i, o.padding ?? Math.round(i / 20)), c = {
1687
+ id: this.nextId("text"),
1688
+ transformType: "custom",
1689
+ x: a,
1690
+ y: s,
1691
+ rotation: o.rotation ?? 0,
1692
+ text: t,
1693
+ fontSize: r,
1694
+ fontFamily: o.fontFamily ?? "Anton",
1695
+ color: bt(o.color) ?? "#ffffff",
1696
+ textAlign: o.textAlign ?? "center",
1697
+ bold: o.bold ?? !1,
1698
+ italic: o.italic ?? !1,
1699
+ transformData: { type: "custom" },
1700
+ ...o.name ? { name: o.name } : {}
1701
+ };
1702
+ return this.els.push(c), this;
1703
+ }
1704
+ /**
1705
+ * Mint a {@link LayerToken} for a named layer — the only currency
1706
+ * `Editor.TextField` accepts (ADR-0085 §1). Compile time: `name` is
1707
+ * constrained to this design's declared layer names, kind inferred from
1708
+ * which per-kind union it falls in. Runtime: validated EAGERLY against the
1709
+ * built elements (nearest the mistake), so CMS/JSON-loaded designs get the
1710
+ * same protection with `string` typing.
1711
+ *
1712
+ * @throws EditorError `E_LAYER_NOT_FOUND` — no layer with this name; lists
1713
+ * the valid names per kind.
1714
+ * @throws EditorError `E_LAYER_WRONG_KIND` — the name matches a layer that
1715
+ * is not bindable (shape/group/path).
1716
+ */
1717
+ layer(t) {
1718
+ const o = this.els.find((i) => i.name === t);
1719
+ if (!o) {
1720
+ const i = this.layerNamesOfKind("text"), r = this.layerNamesOfKind("image");
1721
+ throw new ot(
1722
+ "E_LAYER_NOT_FOUND",
1723
+ `no layer "${t}" in this design. Text layers: ${JSON.stringify(i)}; image layers: ${JSON.stringify(r)}. Fix the name, or add the layer first — e.g. design(…).text('…', { name: '${t}' }).`,
1724
+ { layerName: t, validNames: [...i, ...r], textNames: i, imageNames: r, docSlug: "embed-mode" }
1725
+ );
1726
+ }
1727
+ const n = _t(o);
1728
+ if (!n)
1729
+ throw new ot(
1730
+ "E_LAYER_WRONG_KIND",
1731
+ `layer "${t}" is a ${o.transformType} element — only text and image layers are bindable. Bindable layers: ${JSON.stringify([...this.layerNamesOfKind("text"), ...this.layerNamesOfKind("image")])}.`,
1732
+ { layerName: t, actualType: o.transformType, docSlug: "embed-mode" }
1733
+ );
1734
+ return Eo(n, t);
1735
+ }
1736
+ /**
1737
+ * Mint a {@link ColorToken} for a declared color role. Compile time: `name`
1738
+ * is constrained to the roles declared in `design({ colors })`. Runtime:
1739
+ * validated against the declared roles (CMS/JSON-built designs degrade to
1740
+ * `string` typing and rely on this throw).
1741
+ *
1742
+ * Use the token wherever the design paints the role (`fill`, `color`) AND
1743
+ * as the `useColorBinding` argument — one source of truth, no hand-synced
1744
+ * hex pairs.
1745
+ *
1746
+ * @throws EditorError `E_COLOR_ROLE_NOT_FOUND` — no role with this name;
1747
+ * lists the declared roles.
1748
+ */
1749
+ color(t) {
1750
+ const o = this.colorRoles.get(t);
1751
+ if (o === void 0)
1752
+ throw new ot(
1753
+ "E_COLOR_ROLE_NOT_FOUND",
1754
+ `no color role "${t}" in this design. Declared roles: ${JSON.stringify([...this.colorRoles.keys()])}. Declare roles up front — design({ …, colors: { ${t}: '#FF2D8A' } }).`,
1755
+ { roleName: t, validNames: [...this.colorRoles.keys()], docSlug: "embed-mode" }
1756
+ );
1757
+ return Ro(t, o);
1758
+ }
1759
+ /** Names of bindable layers of one kind, in stack order. @internal */
1760
+ layerNamesOfKind(t) {
1761
+ return this.els.filter((o) => _t(o) === t).map((o) => o.name).filter((o) => !!o);
1762
+ }
1763
+ /** An anchored vector shape. */
1764
+ shape(t, o = {}) {
1765
+ const { width: n, height: i } = this.current, r = o.size ?? Math.round(n / 6), a = typeof r == "number" ? r : r.width, s = typeof r == "number" ? r : r.height, { x: c, y: d } = Lt(o.anchor ?? jt, a, s, n, i, o.padding ?? 0), S = {
1766
+ type: "shape",
1767
+ shapeType: t,
1768
+ width: a,
1769
+ height: s,
1770
+ fillColor: bt(o.fill) ?? "#000000",
1771
+ fillOpacity: o.opacity ?? 1,
1772
+ ...o.points !== void 0 ? { points: o.points } : {},
1773
+ ...o.sides !== void 0 ? { sides: o.sides } : {}
1774
+ }, O = {
1775
+ id: this.nextId("shape"),
1776
+ transformType: "shape",
1777
+ x: c,
1778
+ y: d,
1779
+ rotation: o.rotation ?? 0,
1780
+ transformData: S,
1781
+ ...o.name ? { name: o.name } : {}
1782
+ };
1783
+ return this.els.push(O), this;
1784
+ }
1785
+ /** The composed elements, in render order (background first). Editor shape. */
1786
+ get elements() {
1787
+ return this.els.map((t) => ({ ...t }));
1788
+ }
1789
+ /** Editor-shape canvas state for `<SnowconeCanvas initialElements=… />` and
1790
+ * `serializeStateForServer`. `activeArtboard` is the artboard name. A
1791
+ * builder loaded with `fromState(…, { artboards: 'all' })` emits every
1792
+ * page (artboard `role`s and element membership tags included). */
1793
+ toJSON() {
1794
+ return this.allArtboards ? {
1795
+ // Elements added via builder methods since the last context switch are
1796
+ // untagged — they belong to the CURRENT authoring artboard; stamp them
1797
+ // so the multi-artboard state groups them correctly.
1798
+ elements: this.els.map(
1799
+ (t) => t.artboard ? { ...t } : { ...t, artboard: this.current.name }
1800
+ ),
1801
+ artboards: this.allArtboards.map((t) => ({ ...t })),
1802
+ // The initially-active placement (first artboard / saved state's
1803
+ // active) — NOT the last `.artboard()` authoring context.
1804
+ activeArtboard: this.activeName
1805
+ } : {
1806
+ elements: this.elements,
1807
+ // A background set on a design is part of the design: flag it renderable
1808
+ // so exports and server renders paint it, matching the editor (the
1809
+ // export worker skips it without `exportBackground`) — see snapshotArtboard.
1810
+ artboards: [lt.snapshotArtboard(this.current)],
1811
+ activeArtboard: this.current.name
1812
+ };
1813
+ }
1814
+ /** Server-render payload for `RenderSession.renderState(placement, …)`.
1815
+ * Equivalent to `serializeStateForServer(design.toJSON())`. */
1816
+ toServerRequest() {
1817
+ return wo(this.toJSON());
1818
+ }
1819
+ }
1820
+ function Zo(e) {
1821
+ return new lt(e);
1822
+ }
1823
+ function jo(e) {
1824
+ return _t(e);
1825
+ }
1826
+ export {
1827
+ ee as B,
1828
+ xo as C,
1829
+ lt as D,
1830
+ Bo as E,
1831
+ oe as P,
1832
+ _o as a,
1833
+ Mo as b,
1834
+ uo as c,
1835
+ co as d,
1836
+ Zo as e,
1837
+ Go as f,
1838
+ Yo as g,
1839
+ ko as h,
1840
+ Do as i,
1841
+ jo as j,
1842
+ Ko as k,
1843
+ re as l,
1844
+ So as m,
1845
+ Uo as n,
1846
+ Ke as o,
1847
+ ro as p,
1848
+ Lo as q,
1849
+ Xo as r,
1850
+ wo as s,
1851
+ fo as t,
1852
+ Vo as u,
1853
+ Wo as v,
1854
+ zo as w,
1855
+ $o as x,
1856
+ Ze as y,
1857
+ Ho as z
1858
+ };
1859
+ //# sourceMappingURL=design-builder-BFM94eA9.js.map