@snowcone-app/canvas 0.40.13 → 0.40.14

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.
@@ -1,1112 +0,0 @@
1
- import { p as Y, c as Q, u as Te, I as Be, v as $e } from "./FontAnalyzer-DWHS41ba.js";
2
- import { jsx as p, Fragment as We, jsxs as X } from "react/jsx-runtime";
3
- import { useMemo as Se, useRef as M, useEffect as S, Component as He, useState as q, useCallback as K, useImperativeHandle as ze, forwardRef as Ae } from "react";
4
- import { a as Ke, b as Oe, c as Ue, G as je, d as Ve, Z as Ge, g as Xe, e as Ye, D as Qe, r as Je, v as Ze, S as qe, K as _e, f as et } from "./validation-Q1_OcfJ_.js";
5
- import { ImagePanel as tt } from "./ImagePanel-BIzwlaf5.js";
6
- import { C as rt, a as nt } from "./ContextualToolbars-D-EX9Q9L.js";
7
- import { n as at, o as ot, E as st, C as it, b as me, k as ct } from "./useCommands-CLnk8kvW.js";
8
- import { C as lt, D as dt } from "./template-fields-BzAlpOEB.js";
9
- import { E as ut, b as ee } from "./design-builder-CoCtjIwj.js";
10
- import { T as xe } from "./ThemeContext-Rjz25x9X.js";
11
- import { c as mt, g as ft } from "./placement-Br5jI_-u.js";
12
- import { G as re, E as gt, H as ht } from "./ExportManager-DLOH2Kzk.js";
13
- import { C as le } from "./CanvasRenderer-COHWEK1r.js";
14
- const fe = Q("KitLayout"), Z = "canvas", de = 320;
15
- let ye = !1;
16
- function pt() {
17
- ye || (ye = !0, Ke({
18
- EffectsPanel: Oe,
19
- ExportPanel: Ue,
20
- ImagePanel: tt,
21
- GlyphPanel: je,
22
- ArtboardTabs: Ve,
23
- ContextualToolbars: rt,
24
- ZoomControls: Ge,
25
- CropPanel: nt
26
- }));
27
- }
28
- function bt({ children: n }) {
29
- const t = M(null);
30
- if (t.current === null) {
31
- const r = new at();
32
- r.registerCanvas("legacy-kit-canvas"), t.current = {
33
- store: new dt({ elements: [], artboards: [], activeArtboard: "" }),
34
- commerce: new lt({ productId: "", mockupIds: [] }),
35
- registry: r,
36
- ...ot((a) => fe.error("[kit section]", a))
37
- };
38
- }
39
- return /* @__PURE__ */ p(st.Provider, { value: t.current, children: n });
40
- }
41
- function ne({ ids: n }) {
42
- return !n || n.length === 0 ? null : /* @__PURE__ */ p(bt, { children: n.map((t) => {
43
- var e;
44
- if (t === Z) return null;
45
- const r = Xe(t);
46
- if (!r)
47
- return typeof Y < "u" && ((e = Y.env) == null ? void 0 : e.NODE_ENV) === "development" && fe.warn(
48
- `Kit layout references unknown section "${t}" — skipping. Register it with registerSection("${t}", { ... }).`
49
- ), null;
50
- const a = r.component;
51
- return /* @__PURE__ */ p(a, {}, t);
52
- }) });
53
- }
54
- function vt({ kit: n, canvas: t }) {
55
- var b, u, v, E;
56
- pt();
57
- const r = n.layout.slots, a = !!((b = r.topbar) != null && b.some((y) => y !== Z)), e = !!((u = r.left) != null && u.some((y) => y !== Z)), s = !!((v = r.right) != null && v.some((y) => y !== Z)), c = !!((E = r.bottombar) != null && E.some((y) => y !== Z)), l = Se(
58
- () => !a && !e && !s && !c,
59
- [a, e, s, c]
60
- ), i = M(null), m = a || e || s || c, f = M(!1);
61
- return S(() => {
62
- var o;
63
- if (typeof Y > "u" || ((o = Y.env) == null ? void 0 : o.NODE_ENV) !== "development" || !m) return;
64
- const y = i.current;
65
- if (!y || typeof ResizeObserver > "u") return;
66
- const R = (d) => {
67
- d > 0 && d < de && !f.current ? (f.current = !0, fe.warn(
68
- `Kit "${n.name}" declares chrome (toolbar / panels) but its container is only ${Math.round(d)}px wide — below the ~${de}px needed to render it, so the toolbar / panels are suppressed and the editor looks like a bare canvas. Give the editor more container width, or pass kit="embed-only" if a chrome-less canvas is intended.`
69
- )) : d >= de && (f.current = !1);
70
- };
71
- R(y.getBoundingClientRect().width);
72
- const F = new ResizeObserver((d) => {
73
- for (const h of d) R(h.contentRect.width);
74
- });
75
- return F.observe(y), () => F.disconnect();
76
- }, [m, n.name]), l ? /* @__PURE__ */ p(We, { children: t }) : /* @__PURE__ */ X("div", { ref: i, className: "sc-kit-layout", style: { display: "flex", flexDirection: "column", height: "100%", minHeight: 0 }, children: [
77
- a && /* @__PURE__ */ p(
78
- "div",
79
- {
80
- className: "sc-kit-topbar sc-toolbar",
81
- style: { display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap", flexShrink: 0 },
82
- children: /* @__PURE__ */ p(ne, { ids: r.topbar })
83
- }
84
- ),
85
- /* @__PURE__ */ X("div", { className: "sc-kit-body", style: { display: "flex", flex: 1, minHeight: 0 }, children: [
86
- e && /* @__PURE__ */ p("div", { className: "sc-kit-left", style: { flexShrink: 0, overflow: "auto" }, children: /* @__PURE__ */ p(ne, { ids: r.left }) }),
87
- /* @__PURE__ */ p("div", { className: "sc-kit-canvas", style: { flex: 1, minWidth: 0, position: "relative" }, children: t }),
88
- s && /* @__PURE__ */ p("div", { className: "sc-kit-right", style: { flexShrink: 0, overflow: "auto" }, children: /* @__PURE__ */ p(ne, { ids: r.right }) })
89
- ] }),
90
- c && /* @__PURE__ */ p(
91
- "div",
92
- {
93
- className: "sc-kit-bottombar sc-toolbar",
94
- style: { display: "flex", alignItems: "center", gap: 8, flexShrink: 0 },
95
- children: /* @__PURE__ */ p(ne, { ids: r.bottombar })
96
- }
97
- )
98
- ] });
99
- }
100
- const Et = Q("ErrorBoundary");
101
- class xt extends He {
102
- constructor(t) {
103
- super(t), this.resetError = () => {
104
- this.setState({
105
- hasError: !1,
106
- error: null
107
- });
108
- }, this.state = {
109
- hasError: !1,
110
- error: null
111
- };
112
- }
113
- static getDerivedStateFromError(t) {
114
- return {
115
- hasError: !0,
116
- error: t
117
- };
118
- }
119
- componentDidCatch(t, r) {
120
- if (Et.error("Caught error:", t, r), this.props.onError && this.props.onError(t, r), this.props.onCanvasError) {
121
- const a = {
122
- category: "unknown",
123
- message: t.message,
124
- originalError: t,
125
- recoverable: !1
126
- };
127
- this.props.onCanvasError(a);
128
- }
129
- }
130
- render() {
131
- return this.state.hasError && this.state.error ? this.props.renderError ? this.props.renderError(this.state.error, this.resetError) : this.props.fallback ? typeof this.props.fallback == "function" ? this.props.fallback(this.state.error, this.resetError) : this.props.fallback : /* @__PURE__ */ X(
132
- "div",
133
- {
134
- style: {
135
- padding: "24px",
136
- border: "2px solid var(--danger)",
137
- borderRadius: "8px",
138
- backgroundColor: "color-mix(in oklch, var(--danger) 10%, var(--background))",
139
- color: "var(--danger)",
140
- fontFamily: "system-ui, sans-serif"
141
- },
142
- children: [
143
- /* @__PURE__ */ p("h2", { style: { margin: "0 0 16px 0", fontSize: "18px", fontWeight: 600 }, children: "Something went wrong" }),
144
- /* @__PURE__ */ p(
145
- "pre",
146
- {
147
- style: {
148
- margin: "0 0 16px 0",
149
- padding: "12px",
150
- backgroundColor: "color-mix(in oklch, var(--danger) 15%, var(--background))",
151
- borderRadius: "4px",
152
- fontSize: "14px",
153
- overflow: "auto",
154
- maxHeight: "200px"
155
- },
156
- children: this.state.error.message
157
- }
158
- ),
159
- /* @__PURE__ */ p(
160
- "button",
161
- {
162
- onClick: this.resetError,
163
- style: {
164
- padding: "8px 16px",
165
- backgroundColor: "var(--danger)",
166
- color: "var(--danger-foreground)",
167
- border: "none",
168
- borderRadius: "4px",
169
- fontSize: "14px",
170
- fontWeight: 500,
171
- cursor: "pointer"
172
- },
173
- children: "Try again"
174
- }
175
- )
176
- ]
177
- }
178
- ) : this.props.children;
179
- }
180
- }
181
- function yt({
182
- inheritTheme: n,
183
- externalProvider: t,
184
- services: r,
185
- viewPadding: a,
186
- children: e
187
- }) {
188
- const s = t ? /* @__PURE__ */ p(xe, { defaultTheme: "light", passive: n, children: e }) : /* @__PURE__ */ p(xe, { defaultTheme: "light", passive: n, children: /* @__PURE__ */ p(ut, { viewPadding: a, children: e }) });
189
- return r ? /* @__PURE__ */ p(it, { services: r, children: s }) : s;
190
- }
191
- const wt = Q("useAutoExport");
192
- function St(n) {
193
- const { config: t, historyManager: r, artboards: a, elements: e, onExport: s, onExportScheduled: c, isCanvasReady: l = !1 } = n, i = M(null), m = M(!1), f = M({
194
- totalExports: 0,
195
- skippedExports: 0,
196
- lastExportTime: null,
197
- avgExportInterval: 0
198
- }), [b, u] = q(f.current), v = M(s);
199
- v.current = s, S(() => {
200
- const o = new Ye({
201
- ...Qe,
202
- ...t
203
- });
204
- return o.onExport(async () => {
205
- try {
206
- await v.current(), f.current = o.getStats();
207
- } catch (d) {
208
- wt.error("[useAutoExport] Export failed:", d);
209
- }
210
- }), i.current = o, () => {
211
- o.destroy();
212
- };
213
- }, []), S(() => {
214
- l && m.current && i.current && (m.current = !1, i.current.forceExport());
215
- }, [l]), S(() => {
216
- i.current && t && i.current.updateConfig(t);
217
- }, [t == null ? void 0 : t.enabled, t == null ? void 0 : t.debounceMs, t == null ? void 0 : t.maxWaitMs]), S(() => {
218
- i.current && c && i.current.onExportScheduled(c);
219
- }, [c]), S(() => {
220
- if (!r || !i.current)
221
- return;
222
- const o = r.onCommandExecuted(() => {
223
- if (i.current) {
224
- if (!l) {
225
- m.current = !0;
226
- return;
227
- }
228
- i.current.scheduleExport();
229
- }
230
- });
231
- return () => {
232
- o();
233
- };
234
- }, [r, l]), S(() => {
235
- if (i.current) {
236
- if (!l) {
237
- m.current = !0;
238
- return;
239
- }
240
- i.current.scheduleExport();
241
- }
242
- }, [e, a, l]), S(() => Te((d) => {
243
- if (!i.current || !l) return;
244
- e.some((x) => x.id === d) && i.current.scheduleExport();
245
- }), [e, l]);
246
- const E = K((o) => {
247
- i.current && i.current.updateConfig(o);
248
- }, []), y = K(async () => {
249
- i.current && (await i.current.forceExport(), f.current = i.current.getStats(), u(f.current));
250
- }, []), R = K(() => {
251
- i.current && (i.current.resetStats(), f.current = i.current.getStats(), u(f.current));
252
- }, []), F = K(() => {
253
- i.current && i.current.resetChangeDetection();
254
- }, []);
255
- return {
256
- stats: b,
257
- updateConfig: E,
258
- forceExport: y,
259
- resetStats: R,
260
- resetChangeDetection: F
261
- };
262
- }
263
- const Ie = Q("useContentReady");
264
- function At(n) {
265
- var r;
266
- const t = /* @__PURE__ */ new Set();
267
- for (const a of n)
268
- if ("fontFamily" in a && typeof a.fontFamily == "string" && a.fontFamily && t.add(a.fontFamily), "getRichText" in a && typeof a.getRichText == "function")
269
- try {
270
- const e = a.getRichText();
271
- if (e != null && e.spans)
272
- for (const s of e.spans)
273
- (r = s.style) != null && r.fontFamily && t.add(s.style.fontFamily);
274
- } catch {
275
- }
276
- return Array.from(t);
277
- }
278
- async function It(n) {
279
- if (n.length === 0 || typeof document > "u" || !document.fonts) return;
280
- const t = [];
281
- for (const r of n) {
282
- const a = `16px "${r}"`;
283
- t.push(document.fonts.load(a));
284
- }
285
- await Promise.all(t), document.fonts.ready && await Promise.race([
286
- document.fonts.ready,
287
- new Promise((r) => setTimeout(r, 250))
288
- ]), Ie.debug(`Awaited ${t.length} font(s)`);
289
- }
290
- function kt() {
291
- return new Promise((n) => requestAnimationFrame(() => n()));
292
- }
293
- function Rt(n) {
294
- const { isCanvasReady: t, elements: r, hasInitialElements: a, initialElementsLoaded: e } = n, [s, c] = q(!1), l = M(!1);
295
- return S(() => {
296
- if (s || !t || a && !e || a && r.length === 0 || l.current) return;
297
- l.current = !0;
298
- let i = !1;
299
- return (async () => {
300
- try {
301
- const m = At(r);
302
- if (m.length > 0 && (await It(m), i) || (await kt(), i)) return;
303
- c(!0);
304
- } catch (m) {
305
- Ie.error("Error during content readiness check:", m), i || c(!0);
306
- } finally {
307
- l.current = !1;
308
- }
309
- })(), () => {
310
- i = !0, l.current = !1;
311
- };
312
- }, [t, r, a, e, s]), s;
313
- }
314
- const Ct = {
315
- timeout: 3e4,
316
- retries: 1,
317
- retryDelay: 1e3,
318
- validateUrl: !0
319
- };
320
- function Ft(n) {
321
- try {
322
- const t = new URL(n);
323
- return ["http:", "https:", "data:", "blob:"].includes(t.protocol);
324
- } catch {
325
- return !1;
326
- }
327
- }
328
- async function Mt(n, t = {}) {
329
- const r = { ...Ct, ...t };
330
- if (r.validateUrl && !Ft(n))
331
- return {
332
- success: !1,
333
- error: new Error(`Invalid image URL: ${n}`)
334
- };
335
- let a;
336
- for (let e = 0; e <= r.retries; e++)
337
- try {
338
- return await Nt(n, r.timeout);
339
- } catch (s) {
340
- a = s instanceof Error ? s : new Error(String(s)), e < r.retries && await Lt(r.retryDelay);
341
- }
342
- return {
343
- success: !1,
344
- error: a || new Error("Failed to load image after retries")
345
- };
346
- }
347
- function Nt(n, t) {
348
- return new Promise((r) => {
349
- const a = new Image();
350
- a.crossOrigin = "anonymous";
351
- let e = null, s = !1;
352
- const c = () => {
353
- e && (clearTimeout(e), e = null);
354
- }, l = () => {
355
- if (s) return;
356
- s = !0, c();
357
- const m = a.naturalWidth || a.width, f = a.naturalHeight || a.height;
358
- r({
359
- success: !0,
360
- element: a,
361
- width: m,
362
- height: f,
363
- aspectRatio: f > 0 ? m / f : 1
364
- });
365
- }, i = (m) => {
366
- s || (s = !0, c(), r({
367
- success: !1,
368
- error: new Error(m)
369
- }));
370
- };
371
- a.onload = l, a.onerror = () => {
372
- i(`Failed to load image: ${n}`);
373
- }, e = setTimeout(() => {
374
- i(`Image load timed out after ${t}ms: ${n}`);
375
- }, t), a.src = n;
376
- });
377
- }
378
- async function Pt(n, t, r = {}, a = {}) {
379
- var u, v, E, y, R, F, o;
380
- const e = await Mt(n, a);
381
- if (!e.success || !e.width || !e.height)
382
- return {
383
- success: !1,
384
- error: e.error || new Error("Failed to get image dimensions")
385
- };
386
- const s = {
387
- width: t.artboard.width,
388
- height: t.artboard.height,
389
- scale: t.scale,
390
- align: ft(t.alignment),
391
- offsetX: t.offsetX,
392
- offsetY: t.offsetY,
393
- scaleMode: t.scaleMode,
394
- marginTop: t.marginTop,
395
- marginRight: t.marginRight,
396
- marginBottom: t.marginBottom,
397
- marginLeft: t.marginLeft
398
- }, c = mt(
399
- { width: e.width, height: e.height },
400
- s
401
- ), l = t.artboard.x + c.x, i = t.artboard.y + c.y, m = l + c.width / 2, f = i + c.height / 2;
402
- return {
403
- success: !0,
404
- element: new Be({
405
- ...r,
406
- x: m,
407
- y: f,
408
- imageUrl: n,
409
- imageAspectRatio: e.aspectRatio,
410
- preserveDimensions: !0,
411
- // Don't recalculate dimensions on image load
412
- transformData: {
413
- type: "image",
414
- width: c.width,
415
- height: c.height,
416
- cropX: ((u = r.transformData) == null ? void 0 : u.cropX) ?? 0,
417
- cropY: ((v = r.transformData) == null ? void 0 : v.cropY) ?? 0,
418
- cropWidth: ((E = r.transformData) == null ? void 0 : E.cropWidth) ?? 1,
419
- cropHeight: ((y = r.transformData) == null ? void 0 : y.cropHeight) ?? 1,
420
- flipHorizontal: ((R = r.transformData) == null ? void 0 : R.flipHorizontal) ?? !1,
421
- flipVertical: ((F = r.transformData) == null ? void 0 : F.flipVertical) ?? !1,
422
- borderRadius: ((o = r.transformData) == null ? void 0 : o.borderRadius) ?? 0
423
- }
424
- }),
425
- placement: {
426
- artworkSize: { width: e.width, height: e.height },
427
- finalSize: { width: c.width, height: c.height },
428
- position: { x: m, y: f },
429
- scale: c.scale
430
- }
431
- };
432
- }
433
- function Lt(n) {
434
- return new Promise((t) => setTimeout(t, n));
435
- }
436
- const _ = Q("SnowconeCanvas");
437
- function ue(n, t, r, a) {
438
- if (a <= 0 || !isFinite(a))
439
- return r;
440
- const e = n * r, s = t * r;
441
- if (Math.max(e, s) <= a)
442
- return r;
443
- const l = Math.max(n, t), i = a / l;
444
- return Math.min(i, r);
445
- }
446
- function Dt(n) {
447
- const t = M(n);
448
- t.current = n;
449
- const r = K((e) => {
450
- var s;
451
- _.error(`[${e.category}] ${e.message}`, e.originalError), (s = t.current) == null || s.call(t, e);
452
- }, []);
453
- S(() => (le.onRenderError = r, () => {
454
- le.onRenderError === r && (le.onRenderError = null);
455
- }), [r]);
456
- const a = K((e) => {
457
- r({
458
- category: "unknown",
459
- message: e.message,
460
- originalError: e,
461
- recoverable: !1
462
- });
463
- }, [r]);
464
- return { emitError: r, handleBoundaryError: a };
465
- }
466
- function Tt(n) {
467
- const {
468
- artboardConfigs: t,
469
- width: r,
470
- height: a,
471
- pieceGuides: e,
472
- controlledActiveArtboard: s,
473
- onArtboardChange: c,
474
- resetDocumentOnMount: l
475
- } = n, {
476
- setElements: i,
477
- setSelectedId: m,
478
- setMultiSelection: f,
479
- artboardManager: b,
480
- refreshArtboards: u
481
- } = ee(), { createArtboard: v, selectArtboard: E, artboards: y } = me(), R = M(!1), F = M(!1);
482
- S(() => {
483
- if (R.current) return;
484
- R.current = !0;
485
- const o = t || [{ name: "Design", width: r, height: a }], d = e ? "transparent" : void 0, h = (w) => w.exportBackground ?? (!!w.backgroundColor && w.backgroundColor !== "transparent");
486
- if (l) {
487
- b.clear(), i([]), m(null), f([]);
488
- for (const g of o)
489
- b.createArtboard({
490
- name: g.name,
491
- width: g.width,
492
- height: g.height,
493
- backgroundColor: g.backgroundColor ?? d,
494
- exportBackground: h(g),
495
- clipShape: g.clipShape,
496
- role: g.role
497
- });
498
- const w = b.getAllArtboards();
499
- w.length > 0 && !s && b.setActiveArtboard(w[0].id), u();
500
- return;
501
- }
502
- const x = b.getAllArtboards();
503
- if (x.length > 0) {
504
- const w = o[0], g = x[0];
505
- g.name = w.name, g.width = w.width, g.height = w.height, g.clipShape = w.clipShape, g.exportBackground = h(w), g.role = w.role, w.backgroundColor ? g.backgroundColor = w.backgroundColor : d && (g.backgroundColor = d);
506
- for (let C = 1; C < o.length; C++)
507
- v(o[C].width, o[C].height, {
508
- name: o[C].name,
509
- backgroundColor: o[C].backgroundColor ?? d,
510
- exportBackground: h(o[C]),
511
- clipShape: o[C].clipShape,
512
- role: o[C].role
513
- });
514
- !s && o.length > 1 && b.setActiveArtboard(g.id);
515
- }
516
- u();
517
- }, []), S(() => {
518
- if (!s) {
519
- F.current = !0;
520
- return;
521
- }
522
- const o = y.find((d) => d.name === s);
523
- if (o) {
524
- const d = b.getActiveArtboard();
525
- (d == null ? void 0 : d.name) !== s && E(o.id), F.current = !0;
526
- }
527
- }, [s, y]), S(() => {
528
- if (!c || !F.current) return;
529
- const o = b.getActiveArtboard();
530
- o && c(o.name);
531
- }, [b.getActiveArtboardId()]);
532
- }
533
- function Bt(n) {
534
- const { initialElements: t, emitError: r, customFonts: a } = n, { setElements: e, artboardManager: s } = ee(), c = M(!1), [l, i] = q(!1), m = M(!0);
535
- return S(() => (m.current = !0, () => {
536
- m.current = !1;
537
- }), []), S(() => {
538
- if (!(!t || t.length === 0) && !c.current) {
539
- c.current = !0, i(!0);
540
- try {
541
- a && a.length > 0 && re.register(a);
542
- const f = gt.createManyFromJSON(t), b = s.getAllArtboards();
543
- if (b.length > 0) {
544
- const v = new Map(b.map((E) => [E.name, E]));
545
- f.forEach((E, y) => {
546
- var o;
547
- const R = (o = t[y]) == null ? void 0 : o.artboard, F = R && v.get(R) || b[0];
548
- s.addElementToArtboard(E.id, F.id);
549
- });
550
- }
551
- e(f);
552
- const u = /* @__PURE__ */ new Set();
553
- for (const v of f) {
554
- const E = v.fontFamily;
555
- typeof E == "string" && E.trim().length > 0 && u.add(E);
556
- }
557
- if (u.size > 0 && typeof document < "u") {
558
- const v = document.fonts ?? null, E = Array.from(u).filter(
559
- (o) => re.has(o)
560
- ), y = ht(
561
- re.forFamilies(E),
562
- {
563
- onLoadError: (o) => {
564
- m.current && r({
565
- category: "import",
566
- message: o.message,
567
- originalError: o,
568
- recoverable: !0
569
- });
570
- }
571
- }
572
- ), F = Array.from(u).filter(
573
- (o) => !re.has(o)
574
- ).map((o) => {
575
- const d = `font-${o.replace(/\s+/g, "-").toLowerCase()}`;
576
- let h = document.getElementById(d);
577
- return h || (h = document.createElement("link"), h.id = d, h.rel = "stylesheet", h.href = `https://fonts.googleapis.com/css2?family=${o.replace(
578
- /\s+/g,
579
- "+"
580
- )}:wght@400;700&display=swap`, document.head.appendChild(h)), new Promise((x) => {
581
- if (h.sheet) {
582
- x(o);
583
- return;
584
- }
585
- const w = () => x(o);
586
- h.addEventListener("load", w, { once: !0 }), h.addEventListener("error", w, { once: !0 }), setTimeout(w, 4e3);
587
- });
588
- });
589
- Promise.all([Promise.all(F), y]).then(([o]) => {
590
- if (!(!m.current || !v))
591
- return Promise.all(
592
- o.flatMap((d) => [
593
- v.load(`400 16px "${d}"`).catch(() => {
594
- }),
595
- v.load(`700 16px "${d}"`).catch(() => {
596
- })
597
- ])
598
- );
599
- }).then(() => {
600
- m.current && e((o) => o.slice());
601
- });
602
- }
603
- } catch (f) {
604
- _.error("Failed to load initial elements:", f);
605
- const b = f instanceof Error ? f : new Error(String(f));
606
- r({
607
- category: "import",
608
- message: `Failed to load initial elements: ${b.message}`,
609
- originalError: b,
610
- recoverable: !0
611
- });
612
- }
613
- }
614
- }, [t, s, e, r, a]), { initialElementsLoadedState: l };
615
- }
616
- function $t(n) {
617
- const {
618
- initialImage: t,
619
- initialImageAlignment: r,
620
- initialImageScale: a,
621
- initialImageScaleMode: e,
622
- artboardConfigs: s,
623
- onImageLoad: c,
624
- onImageError: l,
625
- emitError: i
626
- } = n, { elements: m, setElements: f, artboardManager: b } = ee(), [u, v] = q("idle"), [E, y] = q(null), R = M(void 0);
627
- return S(() => {
628
- if (!t || R.current === t) return;
629
- if (m.length > 0) {
630
- R.current = t;
631
- return;
632
- }
633
- R.current = t, (async () => {
634
- v("loading"), y(null);
635
- try {
636
- const o = b.getAllArtboards();
637
- if (o.length === 0)
638
- throw new Error("No artboards available");
639
- const d = [];
640
- for (const h of o) {
641
- const x = s == null ? void 0 : s.find((C) => C.name === h.name), w = (x == null ? void 0 : x.scaleMode) || e, g = await Pt(
642
- t,
643
- {
644
- artboard: {
645
- width: h.width,
646
- height: h.height,
647
- x: h.x,
648
- y: h.y
649
- },
650
- alignment: (x == null ? void 0 : x.fitAlign) || r,
651
- scale: a,
652
- scaleMode: w,
653
- marginTop: x == null ? void 0 : x.fitMarginTop,
654
- marginRight: x == null ? void 0 : x.fitMarginRight,
655
- marginBottom: x == null ? void 0 : x.fitMarginBottom,
656
- marginLeft: x == null ? void 0 : x.fitMarginLeft
657
- }
658
- );
659
- g.success && g.element ? (b.addElementToArtboard(g.element.id, h.id), d.push(g.element)) : _.error("Failed to load image for artboard:", h.name, g.error);
660
- }
661
- if (d.length > 0)
662
- f((h) => [...h, ...d]), v("success"), c == null || c(t);
663
- else {
664
- const h = new Error("Failed to load image into any artboard");
665
- v("error"), y(h), l == null || l(t, h), i({
666
- category: "image",
667
- message: h.message,
668
- originalError: h,
669
- recoverable: !0
670
- });
671
- }
672
- } catch (o) {
673
- const d = o instanceof Error ? o : new Error(String(o));
674
- v("error"), y(d), l == null || l(t, d), i({
675
- category: "image",
676
- message: d.message,
677
- originalError: d,
678
- recoverable: !0
679
- });
680
- }
681
- })();
682
- }, [t, r, a, e]), { imageLoadingState: u, imageError: E };
683
- }
684
- function Wt(n) {
685
- const { onChange: t, onSelectionChange: r } = n, { elements: a, selectedId: e, artboardManager: s } = ee(), { artboards: c } = me(), l = M(r);
686
- l.current = r, S(() => {
687
- var m;
688
- (m = l.current) == null || m.call(l, e);
689
- }, [e]);
690
- const i = M(t);
691
- i.current = t, S(() => {
692
- const m = i.current;
693
- if (!m) return;
694
- const f = s.getActiveArtboard(), b = {
695
- // Stamp each element with the NAME of the artboard it lives on (the
696
- // manager is the membership source of truth). Without this the public
697
- // state is flat and a multi-placement design cannot round-trip: restore
698
- // collapses everything onto the first artboard, and per-placement render
699
- // states can't be derived from onChange. Pinned by
700
- // SnowconeCanvas.artboard-membership.test.tsx.
701
- elements: a.map((u) => {
702
- const v = u.toJSON(), E = s.getArtboardForElement(u.id);
703
- return E ? { ...v, artboard: E.name } : v;
704
- }),
705
- artboards: c.map((u) => ({
706
- name: u.name,
707
- width: u.width,
708
- height: u.height,
709
- clipShape: u.clipShape,
710
- backgroundColor: u.backgroundColor,
711
- // Carried so serializeStateForServer / saved states keep an
712
- // explicitly-set background renderable (mockup matches editor).
713
- exportBackground: u.exportBackground,
714
- // Pacing role for multi-page templates (ADR-0086) — emitted only when
715
- // set so non-template states stay byte-identical.
716
- ...u.role ? { role: u.role } : {}
717
- })),
718
- activeArtboard: (f == null ? void 0 : f.name) || "Design"
719
- };
720
- m(b);
721
- }, [a, c]);
722
- }
723
- function Ht(n, t) {
724
- const {
725
- autoExportInterval: r,
726
- autoExportConfig: a,
727
- autoExportFormat: e,
728
- autoExportAll: s,
729
- exportScale: c,
730
- maxExportSize: l,
731
- exportImageFormat: i,
732
- exportImageQuality: m,
733
- onExport: f,
734
- onExportScheduled: b,
735
- onExportStatus: u,
736
- onExportReady: v,
737
- onReady: E,
738
- hasInitialElements: y,
739
- initialElementsLoaded: R,
740
- emitError: F
741
- } = n, {
742
- elements: o,
743
- artboardManager: d,
744
- historyManager: h,
745
- isCanvasReady: x,
746
- setCanvasReady: w
747
- } = ee(), { artboards: g } = me(), { exportArtboard: C, exportAllArtboards: j, exportArtboardAsBlob: z, exportAllArtboardsAsBlobs: V } = ct(), ae = M(0), oe = M(0);
748
- ze(t, () => ({
749
- exportArtboards: async (P = {}) => {
750
- const L = P.format || e, D = P.scale || c, W = P.all ?? !1;
751
- let T = D;
752
- if (l > 0 && isFinite(l))
753
- for (const k of g) {
754
- const I = ue(k.width, k.height, D, l);
755
- T = Math.min(T, I);
756
- }
757
- const A = { scale: T, format: i, quality: m };
758
- if (W) {
759
- const k = L === "blob" ? await V(A) : await j(A), I = {};
760
- for (const [N, O] of Object.entries(k)) {
761
- const B = g.find((U) => U.id === N);
762
- B && (I[B.name] = O);
763
- }
764
- return I;
765
- } else {
766
- const k = d.getActiveArtboard();
767
- if (!k)
768
- throw new Error("[SnowconeCanvas] No active artboard found");
769
- if (L === "blob") {
770
- const I = await z(k.id, A);
771
- return { [k.name]: I };
772
- } else {
773
- const I = await C(k.id, A);
774
- return { [k.name]: I };
775
- }
776
- }
777
- }
778
- }), [C, j, z, V, e, c, l, i, m, d, g]), S(() => {
779
- v && v(async () => {
780
- const L = d.getActiveArtboard();
781
- if (!L)
782
- throw new Error("[SnowconeCanvas] No active artboard found");
783
- const D = ue(
784
- L.width,
785
- L.height,
786
- c,
787
- l
788
- ), W = e === "blob" ? await z(L.id, { scale: D }) : await C(L.id, { scale: D });
789
- return { [L.name]: W };
790
- });
791
- }, [v, C, z, d, e, c, l]);
792
- const se = K(async () => {
793
- if (!f && !u) return;
794
- const P = ++oe.current, L = Date.now();
795
- let D = c;
796
- if (l > 0 && isFinite(l))
797
- for (const A of g) {
798
- const k = ue(A.width, A.height, c, l);
799
- D = Math.min(D, k);
800
- }
801
- const W = d.getActiveArtboard(), T = (W == null ? void 0 : W.id) ?? "unknown";
802
- u == null || u({ status: "rendering", artboardId: T }), ae.current = L;
803
- try {
804
- let A;
805
- const k = {
806
- format: i,
807
- scale: D,
808
- quality: m
809
- };
810
- if (e === "blob")
811
- if (s) {
812
- const I = await V(k);
813
- A = {};
814
- for (const [N, O] of Object.entries(I)) {
815
- const B = g.find((U) => U.id === N);
816
- B && (A[B.name] = O);
817
- }
818
- } else {
819
- const I = d.getActiveArtboard();
820
- if (!I) return;
821
- const N = await z(I.id, k);
822
- A = { [I.name]: N };
823
- }
824
- else if (s) {
825
- const I = await j(k);
826
- A = {};
827
- for (const [N, O] of Object.entries(I)) {
828
- const B = g.find((U) => U.id === N);
829
- B && (A[B.name] = O);
830
- }
831
- } else {
832
- const I = d.getActiveArtboard();
833
- if (!I) return;
834
- const N = await C(I.id, k);
835
- A = { [I.name]: N };
836
- }
837
- f == null || f(A), u == null || u({ status: "complete", artboardId: T, result: A });
838
- } catch (A) {
839
- const k = A instanceof Error ? A.message : String(A);
840
- if (k.includes("Canvas ref not available"))
841
- return;
842
- const I = Date.now() - L;
843
- _.error("Export failed (export #" + P + "):", {
844
- duration: `${I}ms`,
845
- error: k
846
- });
847
- const N = A instanceof Error ? A : new Error(k);
848
- u == null || u({ status: "error", artboardId: T, error: N }), F({
849
- category: "export",
850
- message: N.message,
851
- originalError: N,
852
- artboardId: T,
853
- recoverable: !0
854
- });
855
- }
856
- }, [f, u, s, e, c, l, i, m, C, j, z, V, g, d, o, a, F]);
857
- S(() => {
858
- r !== void 0 && r > 0 && _.warn(
859
- "autoExportInterval is deprecated and ignored. Use autoExportConfig={{ enabled: true, debounceMs: 100, maxWaitMs: 1000 }} instead."
860
- );
861
- }, []);
862
- const ie = K(() => {
863
- if (b == null || b(), u) {
864
- const P = d.getActiveArtboard();
865
- u({ status: "scheduled", artboardId: (P == null ? void 0 : P.id) ?? "unknown" });
866
- }
867
- }, [b, u, d]), J = Rt({
868
- isCanvasReady: x,
869
- elements: o,
870
- hasInitialElements: y,
871
- initialElementsLoaded: R
872
- }), te = M(!1);
873
- S(() => {
874
- J && !te.current && (te.current = !0, E == null || E());
875
- }, [J, E]), St({
876
- config: a ? {
877
- enabled: a.enabled ?? !0,
878
- debounceMs: a.debounceMs ?? 100,
879
- maxWaitMs: a.maxWaitMs ?? 1e3
880
- } : {
881
- enabled: !1
882
- // Only use smart export if autoExportConfig is provided
883
- },
884
- historyManager: h,
885
- artboards: g,
886
- elements: o,
887
- onExport: se,
888
- onExportScheduled: ie,
889
- isCanvasReady: J
890
- // Gate exports until content is fully ready (fonts loaded, elements deserialized)
891
- }), S(() => {
892
- if (g.length > 0 && !x) {
893
- const P = requestAnimationFrame(() => {
894
- w(!0);
895
- });
896
- return () => cancelAnimationFrame(P);
897
- }
898
- }, [g.length, x, w]), S(() => () => {
899
- w(!1);
900
- }, [w]);
901
- }
902
- const we = Q("SnowconeCanvas"), ke = Ae((n, t) => {
903
- var Ee;
904
- const {
905
- exportConfig: r,
906
- imageConfig: a,
907
- layoutConfig: e,
908
- kit: s,
909
- artboards: c,
910
- activeArtboard: l,
911
- onArtboardChange: i,
912
- initialElements: m,
913
- customFonts: f,
914
- onChange: b,
915
- onSelectionChange: u,
916
- autoExportInterval: v,
917
- onExport: E,
918
- onExportScheduled: y,
919
- onExportStatus: R,
920
- onExportReady: F,
921
- onImageLoad: o,
922
- onImageError: d,
923
- onImageLoadError: h,
924
- onError: x,
925
- onReady: w,
926
- className: g,
927
- style: C,
928
- enableShortcuts: j,
929
- overlay: z,
930
- resetDocumentOnMount: V
931
- } = n, ae = v, oe = n.autoExportConfig ?? (r == null ? void 0 : r.autoExportConfig), se = n.autoExportFormat ?? (r == null ? void 0 : r.format) ?? "dataUrl", ie = n.autoExportAll ?? (r == null ? void 0 : r.exportAll) ?? !1, J = n.exportScale ?? (r == null ? void 0 : r.scale) ?? 2, te = n.maxExportSize ?? (r == null ? void 0 : r.maxSize) ?? 4e3, P = n.exportImageFormat ?? (r == null ? void 0 : r.imageFormat) ?? "png", L = n.exportImageQuality ?? (r == null ? void 0 : r.imageQuality) ?? 0.92, D = n.initialImage ?? (a == null ? void 0 : a.src), W = n.initialImageAlignment ?? (a == null ? void 0 : a.alignment) ?? "center", T = n.initialImageScale ?? (a == null ? void 0 : a.scale) ?? 1, A = n.initialImageScaleMode ?? (a == null ? void 0 : a.scaleMode) ?? "cover", k = n.width ?? (e == null ? void 0 : e.width) ?? 1200, I = n.height ?? (e == null ? void 0 : e.height) ?? 1200, N = n.viewPadding ?? (e == null ? void 0 : e.viewPadding) ?? 0.9, O = n.artboardBorderRadius ?? (e == null ? void 0 : e.artboardBorderRadius) ?? 0, B = n.fixedMargin ?? (e == null ? void 0 : e.fixedMargin), U = n.maxHeight ?? (e == null ? void 0 : e.maxHeight), Re = n.showRotationHandle ?? (e == null ? void 0 : e.showRotationHandle) ?? !0, Ce = n.hideCanvas ?? (e == null ? void 0 : e.hideCanvas) ?? !1, ge = n.canvasWrapperClassName ?? (e == null ? void 0 : e.canvasWrapperClassName), he = n.canvasWrapperStyle ?? (e == null ? void 0 : e.canvasWrapperStyle), Fe = n.canvasCutouts ?? (e == null ? void 0 : e.canvasCutouts), pe = n.pieceGuides, Me = n.pieceFocus, Ne = j ?? !0, G = Se(() => Je(s ?? "pro-studio"), [s]);
932
- S(() => {
933
- var H;
934
- if (typeof Y < "u" && ((H = Y.env) == null ? void 0 : H.NODE_ENV) === "development" && G) {
935
- const $ = Ze(G);
936
- $.valid || we.warn("Kit validation warnings:", $.errors), $.warnings.length > 0 && we.warn("Kit validation warnings:", $.warnings);
937
- }
938
- }, [G]), S(() => {
939
- if (h)
940
- return $e(h);
941
- }, [h]);
942
- const { emitError: ce, handleBoundaryError: Pe } = Dt(x);
943
- Tt({
944
- artboardConfigs: c,
945
- width: k,
946
- height: I,
947
- pieceGuides: pe,
948
- controlledActiveArtboard: l,
949
- onArtboardChange: i,
950
- resetDocumentOnMount: V
951
- });
952
- const { initialElementsLoadedState: Le } = Bt({
953
- initialElements: m,
954
- customFonts: f,
955
- emitError: ce
956
- }), { imageLoadingState: be, imageError: ve } = $t({
957
- initialImage: D,
958
- initialImageAlignment: W,
959
- initialImageScale: T,
960
- initialImageScaleMode: A,
961
- artboardConfigs: c,
962
- onImageLoad: o,
963
- onImageError: d,
964
- emitError: ce
965
- });
966
- if (Wt({ onChange: b, onSelectionChange: u }), Ht(
967
- {
968
- autoExportInterval: ae,
969
- autoExportConfig: oe,
970
- autoExportFormat: se,
971
- autoExportAll: ie,
972
- exportScale: J,
973
- maxExportSize: te,
974
- exportImageFormat: P,
975
- exportImageQuality: L,
976
- onExport: E,
977
- onExportScheduled: y,
978
- onExportStatus: R,
979
- onExportReady: F,
980
- onReady: w,
981
- hasInitialElements: !!m && m.length > 0,
982
- initialElementsLoaded: Le,
983
- emitError: ce
984
- },
985
- t
986
- ), be === "loading")
987
- return /* @__PURE__ */ p(
988
- "div",
989
- {
990
- className: g,
991
- style: {
992
- ...C,
993
- display: "flex",
994
- alignItems: "center",
995
- justifyContent: "center",
996
- backgroundColor: "var(--surface, #f5f5f5)"
997
- },
998
- children: /* @__PURE__ */ p(qe, { size: 48 })
999
- }
1000
- );
1001
- if (be === "error" && ve)
1002
- return /* @__PURE__ */ p(
1003
- "div",
1004
- {
1005
- className: g,
1006
- style: {
1007
- ...C,
1008
- display: "flex",
1009
- alignItems: "center",
1010
- justifyContent: "center",
1011
- backgroundColor: "var(--surface, #f5f5f5)",
1012
- color: "var(--danger, #dc2626)",
1013
- padding: "1rem"
1014
- },
1015
- children: /* @__PURE__ */ X("div", { className: "sc:text-center", children: [
1016
- /* @__PURE__ */ p("div", { className: "sc:font-medium", children: "Failed to load image" }),
1017
- /* @__PURE__ */ p("div", { className: "sc:text-sm sc:mt-1 sc:opacity-70", children: ve.message })
1018
- ] })
1019
- }
1020
- );
1021
- const De = Object.entries(((Ee = G.layout) == null ? void 0 : Ee.slots) ?? {}).every(
1022
- ([H, $]) => H === "canvas" || !$ || $.length === 0
1023
- ) ? { "--color-canvas-bg": "transparent" } : void 0;
1024
- return /* @__PURE__ */ p(_e, { kit: G, children: /* @__PURE__ */ p("div", { className: `sc-root ${g || ""}`, style: { ...De, ...C, position: "relative" }, children: /* @__PURE__ */ X(
1025
- xt,
1026
- {
1027
- onError: Pe,
1028
- fallback: (H, $) => /* @__PURE__ */ X(
1029
- "div",
1030
- {
1031
- style: {
1032
- display: "flex",
1033
- flexDirection: "column",
1034
- alignItems: "center",
1035
- justifyContent: "center",
1036
- padding: "24px",
1037
- minHeight: "200px",
1038
- backgroundColor: "var(--surface, #f5f5f5)",
1039
- color: "var(--foreground, #333)",
1040
- fontFamily: "system-ui, sans-serif",
1041
- textAlign: "center"
1042
- },
1043
- children: [
1044
- /* @__PURE__ */ p("div", { style: { fontSize: "18px", fontWeight: 600, marginBottom: "8px" }, children: "Something went wrong" }),
1045
- /* @__PURE__ */ p("div", { style: { fontSize: "14px", opacity: 0.7, marginBottom: "16px", maxWidth: "400px" }, children: H.message }),
1046
- /* @__PURE__ */ p(
1047
- "button",
1048
- {
1049
- onClick: $,
1050
- style: {
1051
- padding: "8px 20px",
1052
- backgroundColor: "var(--primary, #333)",
1053
- color: "var(--primary-foreground, #fff)",
1054
- border: "none",
1055
- borderRadius: "6px",
1056
- fontSize: "14px",
1057
- fontWeight: 500,
1058
- cursor: "pointer"
1059
- },
1060
- children: "Try again"
1061
- }
1062
- )
1063
- ]
1064
- }
1065
- ),
1066
- children: [
1067
- !Ce && /* @__PURE__ */ (() => {
1068
- const H = /* @__PURE__ */ p(
1069
- et,
1070
- {
1071
- style: { width: "100%" },
1072
- fitPadding: N,
1073
- artboardBorderRadius: O,
1074
- fixedMargin: B,
1075
- maxHeight: U,
1076
- showRotationHandle: Re,
1077
- enableShortcuts: Ne,
1078
- canvasCutouts: Fe,
1079
- pieceGuides: pe,
1080
- pieceFocus: Me
1081
- }
1082
- );
1083
- return /* @__PURE__ */ p(vt, { kit: G, canvas: ge || he ? /* @__PURE__ */ p("div", { className: ge, style: he, children: H }) : H });
1084
- })(),
1085
- z
1086
- ]
1087
- }
1088
- ) }) });
1089
- });
1090
- ke.displayName = "SnowconeCanvasInner";
1091
- const zt = Ae((n, t) => {
1092
- var c;
1093
- const { inheritTheme: r, externalProvider: a, services: e } = n, s = n.viewPadding ?? ((c = n.layoutConfig) == null ? void 0 : c.viewPadding);
1094
- return /* @__PURE__ */ p(
1095
- yt,
1096
- {
1097
- inheritTheme: r,
1098
- externalProvider: a,
1099
- services: e,
1100
- viewPadding: s,
1101
- children: /* @__PURE__ */ p(ke, { ref: t, ...n })
1102
- }
1103
- );
1104
- });
1105
- zt.displayName = "SnowconeCanvas";
1106
- export {
1107
- xt as E,
1108
- zt as S,
1109
- Rt as a,
1110
- St as u
1111
- };
1112
- //# sourceMappingURL=SnowconeCanvas-991f9eds.js.map