@varianta/sdk 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.esm.js CHANGED
@@ -1,303 +1,321 @@
1
- import { jsx as F } from "react/jsx-runtime";
2
- import { forwardRef as b, useRef as I, useEffect as S, useImperativeHandle as k, useState as l, useCallback as c } from "react";
3
- import { initCustomizer as N } from "./index.esm.js";
4
- const M = b(
5
- (A, p) => {
1
+ import { jsx as M } from "react/jsx-runtime";
2
+ import { forwardRef as B, useRef as A, useEffect as v, useImperativeHandle as j, useState as d, useCallback as a } from "react";
3
+ import { initCustomizer as H, CustomizerError as u } from "./index.esm.js";
4
+ const J = B(
5
+ (U, S) => {
6
6
  const {
7
- templateId: d,
8
- productId: f,
9
- apiUrl: n,
10
- theme: u = "light",
11
- mode: y = "edit",
12
- debug: D = !1,
13
- className: T,
14
- style: x,
15
- initialDesign: m,
16
- showCloseButton: U,
17
- showSaveButton: V,
18
- onReady: a,
19
- onChange: s,
20
- onLayerSelect: g,
21
- onLayerAdd: z,
22
- onLayerRemove: i,
23
- onLayerUpdate: w,
24
- onError: h,
25
- onFinalize: L,
26
- onViewChange: E,
27
- onClose: C,
28
- onSave: R
29
- } = A, v = I(null), e = I(null), o = I({
30
- onReady: a,
31
- onChange: s,
32
- onLayerSelect: g,
33
- onLayerAdd: z,
34
- onLayerRemove: i,
35
- onLayerUpdate: w,
36
- onError: h,
37
- onFinalize: L,
38
- onViewChange: E,
39
- onClose: C,
40
- onSave: R
7
+ templateId: w,
8
+ productId: L,
9
+ apiUrl: t,
10
+ theme: l = "light",
11
+ mode: g = "edit",
12
+ debug: O = !1,
13
+ className: _,
14
+ style: V,
15
+ initialDesign: z,
16
+ showCloseButton: Z,
17
+ showSaveButton: p,
18
+ onReady: f,
19
+ onChange: y,
20
+ onLayerSelect: I,
21
+ onLayerAdd: m,
22
+ onLayerRemove: h,
23
+ onLayerUpdate: T,
24
+ onError: E,
25
+ onFinalize: R,
26
+ onViewChange: s,
27
+ onClose: N,
28
+ onSave: C
29
+ } = U, D = A(null), e = A(null), c = A({
30
+ onReady: f,
31
+ onChange: y,
32
+ onLayerSelect: I,
33
+ onLayerAdd: m,
34
+ onLayerRemove: h,
35
+ onLayerUpdate: T,
36
+ onError: E,
37
+ onFinalize: R,
38
+ onViewChange: s,
39
+ onClose: N,
40
+ onSave: C
41
41
  });
42
- return S(() => {
43
- o.current = {
44
- onReady: a,
45
- onChange: s,
46
- onLayerSelect: g,
47
- onLayerAdd: z,
48
- onLayerRemove: i,
49
- onLayerUpdate: w,
50
- onError: h,
51
- onFinalize: L,
52
- onViewChange: E,
53
- onClose: C,
54
- onSave: R
42
+ return v(() => {
43
+ c.current = {
44
+ onReady: f,
45
+ onChange: y,
46
+ onLayerSelect: I,
47
+ onLayerAdd: m,
48
+ onLayerRemove: h,
49
+ onLayerUpdate: T,
50
+ onError: E,
51
+ onFinalize: R,
52
+ onViewChange: s,
53
+ onClose: N,
54
+ onSave: C
55
55
  };
56
- }, [a, s, g, z, i, w, h, L, E, C, R]), S(() => {
57
- if (!v.current) return;
58
- const r = {
59
- templateId: d,
60
- productId: f,
61
- apiUrl: n,
62
- theme: u,
63
- mode: y,
64
- debug: D,
65
- initialDesign: m,
66
- showCloseButton: U,
67
- showSaveButton: V,
56
+ }, [f, y, I, m, h, T, E, R, s, N, C]), v(() => {
57
+ if (!D.current) return;
58
+ const n = {
59
+ templateId: w,
60
+ productId: L,
61
+ apiUrl: t,
62
+ theme: l,
63
+ mode: g,
64
+ debug: O,
65
+ initialDesign: z,
66
+ showCloseButton: Z,
67
+ showSaveButton: p,
68
68
  // Use callback refs to always get latest callbacks
69
- onReady: () => o.current.onReady?.(),
70
- onChange: (t) => o.current.onChange?.(t),
71
- onLayerSelect: (t) => o.current.onLayerSelect?.(t),
72
- onLayerAdd: (t) => o.current.onLayerAdd?.(t),
73
- onLayerRemove: (t) => o.current.onLayerRemove?.(t),
74
- onLayerUpdate: (t) => o.current.onLayerUpdate?.(t),
75
- onError: (t) => o.current.onError?.(t),
76
- onFinalize: (t) => o.current.onFinalize?.(t),
77
- onViewChange: (t) => o.current.onViewChange?.(t),
78
- onClose: () => o.current.onClose?.(),
79
- onSave: (t) => o.current.onSave?.(t)
69
+ onReady: () => c.current.onReady?.(),
70
+ onChange: (r) => c.current.onChange?.(r),
71
+ onLayerSelect: (r) => c.current.onLayerSelect?.(r),
72
+ onLayerAdd: (r) => c.current.onLayerAdd?.(r),
73
+ onLayerRemove: (r) => c.current.onLayerRemove?.(r),
74
+ onLayerUpdate: (r) => c.current.onLayerUpdate?.(r),
75
+ onError: (r) => c.current.onError?.(r),
76
+ onFinalize: (r) => c.current.onFinalize?.(r),
77
+ onViewChange: (r) => c.current.onViewChange?.(r),
78
+ onClose: () => c.current.onClose?.(),
79
+ onSave: (r) => c.current.onSave?.(r)
80
80
  };
81
81
  try {
82
- const t = N(v.current, r);
83
- e.current = t;
84
- } catch (t) {
85
- console.error("[Customizer React] Failed to initialize:", t), o.current.onError?.({
86
- code: "INIT_ERROR",
87
- message: t instanceof Error ? t.message : "Unknown error",
88
- details: t
89
- });
82
+ const r = H(D.current, n);
83
+ e.current = r;
84
+ } catch (r) {
85
+ console.error("[Customizer React] Failed to initialize:", r);
86
+ const x = r instanceof u ? r : new u("INIT_ERROR", r instanceof Error ? r.message : "Unknown error", r);
87
+ c.current.onError?.(x);
90
88
  }
91
89
  return () => {
92
90
  e.current && (e.current.destroy(), e.current = null);
93
91
  };
94
- }, [d, f, n, D, m, U, V]), S(() => {
95
- e.current && e.current.setTheme(u);
96
- }, [u]), S(() => {
97
- e.current && e.current.setMode(y);
98
- }, [y]), k(
99
- p,
92
+ }, [w, L, t, O, z, Z, p]), v(() => {
93
+ e.current && e.current.setTheme(l);
94
+ }, [l]), v(() => {
95
+ e.current && e.current.setMode(g);
96
+ }, [g]), j(
97
+ S,
100
98
  () => ({
101
99
  getDesign: () => {
102
100
  if (!e.current)
103
- throw new Error("Editor not initialized");
101
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
104
102
  return e.current.getDesign();
105
103
  },
106
- setDesign: (r) => {
104
+ setDesign: (n) => {
107
105
  if (!e.current)
108
- throw new Error("Editor not initialized");
109
- e.current.setDesign(r);
106
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
107
+ e.current.setDesign(n);
110
108
  },
111
109
  undo: () => {
112
110
  if (!e.current)
113
- throw new Error("Editor not initialized");
111
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
114
112
  e.current.undo();
115
113
  },
116
114
  redo: () => {
117
115
  if (!e.current)
118
- throw new Error("Editor not initialized");
116
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
119
117
  e.current.redo();
120
118
  },
121
119
  canUndo: () => e.current ? e.current.canUndo() : !1,
122
120
  canRedo: () => e.current ? e.current.canRedo() : !1,
123
- finalize: async () => {
121
+ finalize: async (n) => {
122
+ if (!e.current)
123
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
124
+ return e.current.finalize(n);
125
+ },
126
+ waitForResult: async (n, r) => {
124
127
  if (!e.current)
125
- throw new Error("Editor not initialized");
126
- return e.current.finalize();
128
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
129
+ return e.current.waitForResult(n, r);
127
130
  },
128
- addTextLayer: (r) => {
131
+ addTextLayer: (n) => {
129
132
  if (!e.current)
130
- throw new Error("Editor not initialized");
131
- e.current.addTextLayer(r);
133
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
134
+ e.current.addTextLayer(n);
132
135
  },
133
- addImageLayer: async (r) => {
136
+ addImageLayer: async (n) => {
134
137
  if (!e.current)
135
- throw new Error("Editor not initialized");
136
- return e.current.addImageLayer(r);
138
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
139
+ return e.current.addImageLayer(n);
137
140
  },
138
- removeLayer: (r) => {
141
+ removeLayer: (n) => {
139
142
  if (!e.current)
140
- throw new Error("Editor not initialized");
141
- e.current.removeLayer(r);
143
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
144
+ e.current.removeLayer(n);
142
145
  },
143
- selectLayer: (r) => {
146
+ selectLayer: (n) => {
144
147
  if (!e.current)
145
- throw new Error("Editor not initialized");
146
- e.current.selectLayer(r);
148
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
149
+ e.current.selectLayer(n);
147
150
  },
148
151
  getSelectedLayerId: () => e.current ? e.current.getSelectedLayerId() : null,
149
152
  getActiveView: () => e.current ? e.current.getActiveView() : null,
150
153
  getViews: () => e.current ? e.current.getViews() : [],
151
- setActiveView: (r) => {
154
+ setActiveView: (n) => {
152
155
  if (!e.current)
153
- throw new Error("Editor not initialized");
154
- e.current.setActiveView(r);
156
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
157
+ e.current.setActiveView(n);
155
158
  },
156
- setTheme: (r) => {
159
+ setTheme: (n) => {
157
160
  if (!e.current)
158
- throw new Error("Editor not initialized");
159
- e.current.setTheme(r);
161
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
162
+ e.current.setTheme(n);
160
163
  },
161
- setMode: (r) => {
164
+ setMode: (n) => {
162
165
  if (!e.current)
163
- throw new Error("Editor not initialized");
164
- e.current.setMode(r);
166
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
167
+ e.current.setMode(n);
165
168
  },
166
169
  destroy: () => {
167
170
  e.current && (e.current.destroy(), e.current = null);
168
171
  },
169
172
  getElement: () => {
170
173
  if (!e.current)
171
- throw new Error("Editor not initialized");
174
+ throw new u("NOT_INITIALIZED", "Editor not initialized");
172
175
  return e.current.getElement();
173
176
  }
174
177
  }),
175
178
  []
176
179
  // Empty deps is OK - we always read from instanceRef.current
177
- ), /* @__PURE__ */ F(
180
+ ), /* @__PURE__ */ M(
178
181
  "div",
179
182
  {
180
- ref: v,
181
- className: T,
183
+ ref: D,
184
+ className: _,
182
185
  style: {
183
186
  width: "100%",
184
187
  height: "100%",
185
- ...x
188
+ ...V
186
189
  }
187
190
  }
188
191
  );
189
192
  }
190
193
  );
191
- M.displayName = "Customizer";
192
- function H(A = {}) {
194
+ J.displayName = "Customizer";
195
+ function P(U = {}) {
193
196
  const {
194
- autoSave: p = !1,
195
- autoSaveKey: d = "customizer-design",
196
- autoSaveDebounce: f = 1e3
197
- } = A, n = I(null), [u, y] = l(null), [D, T] = l(!1), [x, m] = l(!1), [U, V] = l(null), [a, s] = l(!1), [g, z] = l(
197
+ autoSave: S = !1,
198
+ autoSaveKey: w = "customizer-design",
199
+ autoSaveDebounce: L = 1e3
200
+ } = U, t = A(null), [l, g] = d(null), [O, _] = d(!1), [V, z] = d(!1), [Z, p] = d(null), [f, y] = d(!1), [I, m] = d(!1), [h, T] = d(
198
201
  null
199
- ), i = I(void 0);
200
- S(() => {
201
- if (!(!p || !u))
202
- return i.current && clearTimeout(i.current), i.current = setTimeout(() => {
202
+ ), [E, R] = d(null), s = A(void 0);
203
+ v(() => {
204
+ if (!(!S || !l))
205
+ return s.current && clearTimeout(s.current), s.current = setTimeout(() => {
203
206
  try {
204
- localStorage.setItem(d, JSON.stringify(u));
205
- } catch (r) {
206
- console.error("[useCustomizer] Auto-save failed:", r);
207
+ localStorage.setItem(w, JSON.stringify(l));
208
+ } catch (o) {
209
+ console.error("[useCustomizer] Auto-save failed:", o);
207
210
  }
208
- }, f), () => {
209
- i.current && clearTimeout(i.current);
211
+ }, L), () => {
212
+ s.current && clearTimeout(s.current);
210
213
  };
211
- }, [u, p, d, f]);
212
- const w = c(() => {
213
- if (!n.current) return null;
214
+ }, [l, S, w, L]);
215
+ const N = a(() => {
216
+ if (!t.current) return null;
214
217
  try {
215
- return n.current.getDesign();
216
- } catch (r) {
217
- return console.error("[useCustomizer] getDesign failed:", r), null;
218
+ return t.current.getDesign();
219
+ } catch (o) {
220
+ return console.error("[useCustomizer] getDesign failed:", o), null;
218
221
  }
219
- }, []), h = c((r) => {
220
- if (n.current)
222
+ }, []), C = a((o) => {
223
+ if (t.current)
221
224
  try {
222
- n.current.setDesign(r), y(r);
223
- } catch (t) {
224
- console.error("[useCustomizer] setDesign failed:", t);
225
+ t.current.setDesign(o), g(o);
226
+ } catch (i) {
227
+ console.error("[useCustomizer] setDesign failed:", i);
225
228
  }
226
- }, []), L = c(() => {
227
- if (n.current)
229
+ }, []), D = a(() => {
230
+ if (t.current)
228
231
  try {
229
- n.current.undo(), T(n.current.canUndo()), m(n.current.canRedo());
230
- } catch (r) {
231
- console.error("[useCustomizer] undo failed:", r);
232
+ t.current.undo(), _(t.current.canUndo()), z(t.current.canRedo());
233
+ } catch (o) {
234
+ console.error("[useCustomizer] undo failed:", o);
232
235
  }
233
- }, []), E = c(() => {
234
- if (n.current)
236
+ }, []), e = a(() => {
237
+ if (t.current)
235
238
  try {
236
- n.current.redo(), T(n.current.canUndo()), m(n.current.canRedo());
237
- } catch (r) {
238
- console.error("[useCustomizer] redo failed:", r);
239
+ t.current.redo(), _(t.current.canUndo()), z(t.current.canRedo());
240
+ } catch (o) {
241
+ console.error("[useCustomizer] redo failed:", o);
239
242
  }
240
- }, []), C = c(async () => {
241
- if (!n.current || a) return null;
242
- s(!0);
243
+ }, []), c = a(async (o) => {
244
+ if (!t.current || f) return null;
245
+ y(!0);
246
+ try {
247
+ const i = await t.current.finalize(o);
248
+ return T(i), i;
249
+ } catch (i) {
250
+ return console.error("[useCustomizer] finalize failed:", i), null;
251
+ } finally {
252
+ y(!1);
253
+ }
254
+ }, [f]), n = a(async (o, i) => {
255
+ if (!t.current || I) return null;
256
+ m(!0);
243
257
  try {
244
- const r = await n.current.finalize();
245
- return z(r), r;
246
- } catch (r) {
247
- return console.error("[useCustomizer] finalize failed:", r), null;
258
+ const F = await t.current.waitForResult(o, i);
259
+ return R(F), F;
260
+ } catch (F) {
261
+ return console.error("[useCustomizer] waitForResult failed:", F), null;
248
262
  } finally {
249
- s(!1);
263
+ m(!1);
250
264
  }
251
- }, [a]), R = c((r) => {
252
- if (n.current)
265
+ }, [I]), r = a((o) => {
266
+ if (t.current)
253
267
  try {
254
- n.current.addTextLayer(r);
255
- } catch (t) {
256
- console.error("[useCustomizer] addTextLayer failed:", t);
268
+ t.current.addTextLayer(o);
269
+ } catch (i) {
270
+ console.error("[useCustomizer] addTextLayer failed:", i);
257
271
  }
258
- }, []), v = c(async (r) => {
259
- if (n.current)
272
+ }, []), x = a(async (o) => {
273
+ if (t.current)
260
274
  try {
261
- await n.current.addImageLayer(r);
262
- } catch (t) {
263
- console.error("[useCustomizer] addImageLayer failed:", t);
275
+ await t.current.addImageLayer(o);
276
+ } catch (i) {
277
+ console.error("[useCustomizer] addImageLayer failed:", i);
264
278
  }
265
- }, []), e = c((r) => {
266
- if (n.current)
279
+ }, []), b = a((o) => {
280
+ if (t.current)
267
281
  try {
268
- n.current.removeLayer(r);
269
- } catch (t) {
270
- console.error("[useCustomizer] removeLayer failed:", t);
282
+ t.current.removeLayer(o);
283
+ } catch (i) {
284
+ console.error("[useCustomizer] removeLayer failed:", i);
271
285
  }
272
- }, []), o = c((r) => {
273
- if (n.current)
286
+ }, []), k = a((o) => {
287
+ if (t.current)
274
288
  try {
275
- n.current.selectLayer(r), V(r);
276
- } catch (t) {
277
- console.error("[useCustomizer] selectLayer failed:", t);
289
+ t.current.selectLayer(o), p(o);
290
+ } catch (i) {
291
+ console.error("[useCustomizer] selectLayer failed:", i);
278
292
  }
279
293
  }, []);
280
294
  return {
281
- customizerRef: n,
282
- design: u,
283
- canUndo: D,
284
- canRedo: x,
285
- selectedLayerId: U,
286
- isFinalizing: a,
287
- finalizeResult: g,
288
- getDesign: w,
289
- setDesign: h,
290
- undo: L,
291
- redo: E,
292
- finalize: C,
293
- addTextLayer: R,
294
- addImageLayer: v,
295
- removeLayer: e,
296
- selectLayer: o
295
+ customizerRef: t,
296
+ design: l,
297
+ canUndo: O,
298
+ canRedo: V,
299
+ selectedLayerId: Z,
300
+ isFinalizing: f,
301
+ isRendering: I,
302
+ finalizeResult: h,
303
+ renderResult: E,
304
+ getDesign: N,
305
+ setDesign: C,
306
+ undo: D,
307
+ redo: e,
308
+ finalize: c,
309
+ waitForResult: n,
310
+ addTextLayer: r,
311
+ addImageLayer: x,
312
+ removeLayer: b,
313
+ selectLayer: k
297
314
  };
298
315
  }
299
316
  export {
300
- M as Customizer,
301
- H as useCustomizer
317
+ J as Customizer,
318
+ u as CustomizerError,
319
+ P as useCustomizer
302
320
  };
303
321
  //# sourceMappingURL=react.esm.js.map