@printine/pod-editor 0.0.0-stage → 0.1.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.
Files changed (222) hide show
  1. package/README.md +242 -2
  2. package/dist/DesignerRuntimeContext.d.ts +3 -0
  3. package/dist/PrintineDesigner.d.ts +9 -0
  4. package/dist/api/asset-client.d.ts +8 -0
  5. package/dist/api/asset-client.js +40 -0
  6. package/dist/api/asset-endpoints.d.ts +36 -0
  7. package/dist/api/client.d.ts +8 -0
  8. package/dist/api/client.js +82 -0
  9. package/dist/api/common-endpoints.d.ts +12 -0
  10. package/dist/api/design-endpoints.d.ts +4 -0
  11. package/dist/api/endpoints.d.ts +23 -0
  12. package/dist/api/endpoints.js +33 -0
  13. package/dist/api/files-endpoints.d.ts +1 -0
  14. package/dist/assets/icons/aspect_ratio.d.ts +3 -0
  15. package/dist/assets/icons/delete.d.ts +3 -0
  16. package/dist/assets/icons/fit_screen.d.ts +3 -0
  17. package/dist/assets/icons/index.d.ts +11 -0
  18. package/dist/assets/icons/loading_1.d.ts +3 -0
  19. package/dist/assets/icons/loading_2.d.ts +3 -0
  20. package/dist/assets/icons/loading_3.d.ts +3 -0
  21. package/dist/assets/icons/pan.d.ts +3 -0
  22. package/dist/assets/icons/setting.d.ts +3 -0
  23. package/dist/assets/icons/text_color.d.ts +3 -0
  24. package/dist/assets/icons/text_h.d.ts +3 -0
  25. package/dist/assets/icons/text_v.d.ts +3 -0
  26. package/dist/bundled-assets.d.ts +4 -0
  27. package/dist/components/add-design.d.ts +8 -0
  28. package/dist/components/assets-dialog.d.ts +24 -0
  29. package/dist/components/canvas-context-menu.d.ts +6 -0
  30. package/dist/components/color-picker/index.d.ts +11 -0
  31. package/dist/components/context.d.ts +24 -0
  32. package/dist/components/design.d.ts +9 -0
  33. package/dist/components/fonts.d.ts +26 -0
  34. package/dist/components/image-crop.d.ts +5 -0
  35. package/dist/components/image-edit.d.ts +1 -0
  36. package/dist/components/layout/aside.d.ts +4 -0
  37. package/dist/components/layout/bottom-nav/element-menu-sheet.d.ts +11 -0
  38. package/dist/components/layout/bottom-nav/element-sheet.d.ts +11 -0
  39. package/dist/components/layout/bottom-nav/index.d.ts +1 -0
  40. package/dist/components/layout/header.d.ts +1 -0
  41. package/dist/components/loading.d.ts +3 -0
  42. package/dist/components/menus/ai.d.ts +5 -0
  43. package/dist/components/menus/background.d.ts +6 -0
  44. package/dist/components/menus/clipart.d.ts +6 -0
  45. package/dist/components/menus/files.d.ts +7 -0
  46. package/dist/components/menus/image.d.ts +8 -0
  47. package/dist/components/menus/index.d.ts +13 -0
  48. package/dist/components/menus/layer.d.ts +5 -0
  49. package/dist/components/menus/product.d.ts +3 -0
  50. package/dist/components/menus/sca.d.ts +5 -0
  51. package/dist/components/menus/shapes.d.ts +6 -0
  52. package/dist/components/menus/templates.d.ts +6 -0
  53. package/dist/components/menus/text-effects.d.ts +5 -0
  54. package/dist/components/menus/text.d.ts +6 -0
  55. package/dist/components/placement-tab.d.ts +7 -0
  56. package/dist/components/preview/3d.d.ts +8 -0
  57. package/dist/components/preview/graphic.d.ts +15 -0
  58. package/dist/components/preview/index.d.ts +20 -0
  59. package/dist/components/preview/open.d.ts +7 -0
  60. package/dist/components/preview/real-time/3d.d.ts +12 -0
  61. package/dist/components/preview/real-time/graphic.d.ts +31 -0
  62. package/dist/components/preview/real-time/index.d.ts +12 -0
  63. package/dist/components/scale-tools.d.ts +7 -0
  64. package/dist/components/semi-deisgn-config.d.ts +13 -0
  65. package/dist/components/semi-design-link.d.ts +9 -0
  66. package/dist/components/semi-interact-editor.d.ts +27 -0
  67. package/dist/components/semi-renderer.d.ts +74 -0
  68. package/dist/components/store.d.ts +1 -0
  69. package/dist/components/toolbar.d.ts +2 -0
  70. package/dist/config-D2qm60gf.js +16 -0
  71. package/dist/contracts/api-design.d.ts +180 -0
  72. package/dist/contracts/design.d.ts +86 -0
  73. package/dist/design-C_TmcalT.js +21449 -0
  74. package/dist/designer-store-Da9fICz8.js +153 -0
  75. package/dist/engine/canvas-designer.d.ts +101 -0
  76. package/dist/engine/canvas-designer.js +313 -0
  77. package/dist/engine/constants.d.ts +26 -0
  78. package/dist/engine/constants.js +147 -0
  79. package/dist/engine/editor.d.ts +199 -0
  80. package/dist/engine/editor.js +801 -0
  81. package/dist/engine/events.d.ts +35 -0
  82. package/dist/engine/events.js +7 -0
  83. package/dist/engine/extensions/curved-text.d.ts +59 -0
  84. package/dist/engine/fabric.d.ts +4 -0
  85. package/dist/engine/fabric.js +187 -0
  86. package/dist/engine/history.d.ts +27 -0
  87. package/dist/engine/history.js +56 -0
  88. package/dist/engine/managers/event.d.ts +101 -0
  89. package/dist/engine/managers/event.js +410 -0
  90. package/dist/engine/managers/resize.d.ts +26 -0
  91. package/dist/engine/managers/resize.js +64 -0
  92. package/dist/engine/managers/snapline.d.ts +70 -0
  93. package/dist/engine/managers/snapline.js +124 -0
  94. package/dist/engine/managers/workspace.d.ts +29 -0
  95. package/dist/engine/managers/workspace.js +75 -0
  96. package/dist/engine/processors/image.d.ts +88 -0
  97. package/dist/engine/processors/image.js +473 -0
  98. package/dist/engine/processors/template.d.ts +45 -0
  99. package/dist/engine/processors/template.js +314 -0
  100. package/dist/engine/processors/text.d.ts +74 -0
  101. package/dist/engine/processors/text.js +215 -0
  102. package/dist/engine/semi-export-editor.d.ts +215 -0
  103. package/dist/engine/types.d.ts +28 -0
  104. package/dist/engine/utils/common.d.ts +25 -0
  105. package/dist/engine/utils/common.js +68 -0
  106. package/dist/engine/utils/dialog.d.ts +25 -0
  107. package/dist/engine/utils/font.d.ts +5 -0
  108. package/dist/engine/utils/font.js +39 -0
  109. package/dist/engine/utils/image.d.ts +8 -0
  110. package/dist/engine/utils/image.js +50 -0
  111. package/dist/engine/utils/loading.d.ts +17 -0
  112. package/dist/engine/utils/loading.js +71 -0
  113. package/dist/engine/utils/measure.d.ts +6 -0
  114. package/dist/engine/utils/measure.js +6 -0
  115. package/dist/engine/utils/mockup.d.ts +59 -0
  116. package/dist/engine/utils/setting.d.ts +4 -0
  117. package/dist/engine/utils/setting.js +73 -0
  118. package/dist/fabric-types.d.ts +59 -0
  119. package/dist/full-designer.js +14 -0
  120. package/dist/hooks/pagination.d.ts +19 -0
  121. package/dist/hooks/use-adobe-express.d.ts +50 -0
  122. package/dist/hooks/use-before-unload.d.ts +1 -0
  123. package/dist/hooks/use-image-drop.d.ts +18 -0
  124. package/dist/hooks/use-oss-upload.d.ts +36 -0
  125. package/dist/hooks/use-preview-assets.d.ts +2 -0
  126. package/dist/hooks/use-screen.d.ts +1 -0
  127. package/dist/i18n/index.d.ts +12 -0
  128. package/dist/images/design/asset_empty.png +0 -0
  129. package/dist/images/design/design/background.svg +3 -0
  130. package/dist/images/design/design/color-picker.svg +15 -0
  131. package/dist/images/design/design/computer.svg +1 -0
  132. package/dist/images/design/design/copy.svg +1 -0
  133. package/dist/images/design/design/image.svg +10 -0
  134. package/dist/images/design/design/line.svg +1 -0
  135. package/dist/images/design/design/text.svg +1 -0
  136. package/dist/images/design/design/title.svg +1 -0
  137. package/dist/images/design/empty.png +0 -0
  138. package/dist/images/design/layer_empty.png +0 -0
  139. package/dist/images/design/menu/ai.svg +1 -0
  140. package/dist/images/design/menu/back.svg +4 -0
  141. package/dist/images/design/menu/background.svg +6 -0
  142. package/dist/images/design/menu/clipart.svg +6 -0
  143. package/dist/images/design/menu/file.svg +4 -0
  144. package/dist/images/design/menu/fill.svg +1 -0
  145. package/dist/images/design/menu/image.svg +7 -0
  146. package/dist/images/design/menu/layer.svg +6 -0
  147. package/dist/images/design/menu/product.svg +3 -0
  148. package/dist/images/design/menu/sca.svg +5 -0
  149. package/dist/images/design/menu/shapes.svg +5 -0
  150. package/dist/images/design/menu/templates.svg +6 -0
  151. package/dist/images/design/menu/text.svg +6 -0
  152. package/dist/images/design/menu/uploads.svg +7 -0
  153. package/dist/images/design/spinner.svg +3 -0
  154. package/dist/images/design/spinners.svg +17 -0
  155. package/dist/images/design/transparent_pattern.svg +5 -0
  156. package/dist/index.d.ts +51 -0
  157. package/dist/index.js +168 -0
  158. package/dist/lib/analytics.d.ts +17 -0
  159. package/dist/lib/cn.d.ts +3 -0
  160. package/dist/lib/common.d.ts +16 -0
  161. package/dist/lib/config.d.ts +14 -0
  162. package/dist/lib/navigation.d.ts +2 -0
  163. package/dist/pixel-map-dSys7UAx.js +137 -0
  164. package/dist/renderer/ThreeDPreview.d.ts +14 -0
  165. package/dist/renderer/latest-task-queue.d.ts +17 -0
  166. package/dist/renderer/pixel-map.d.ts +37 -0
  167. package/dist/renderer/preview-asset-cache.d.ts +4 -0
  168. package/dist/renderer/render-layer-output.d.ts +5 -0
  169. package/dist/renderer/semi-resource-cache.d.ts +28 -0
  170. package/dist/renderer/sticker.d.ts +34 -0
  171. package/dist/renderer/sticker.js +1837 -0
  172. package/dist/renderer/three-d-preview.js +32 -0
  173. package/dist/semi-interact-editor-DfzphWy3.js +1409 -0
  174. package/dist/semi-renderer.d.ts +7 -0
  175. package/dist/semi-renderer.js +5 -0
  176. package/dist/state/designer-store.d.ts +43 -0
  177. package/dist/state/designer-store.js +8 -0
  178. package/dist/style.css +1 -0
  179. package/dist/types.d.ts +305 -0
  180. package/dist/ui/accordion.d.ts +7 -0
  181. package/dist/ui/button-group.d.ts +11 -0
  182. package/dist/ui/button.d.ts +10 -0
  183. package/dist/ui/checkbox.d.ts +4 -0
  184. package/dist/ui/command.d.ts +18 -0
  185. package/dist/ui/confirm-dialog.d.ts +15 -0
  186. package/dist/ui/context-menu.d.ts +25 -0
  187. package/dist/ui/dialog.d.ts +15 -0
  188. package/dist/ui/drawer.d.ts +15 -0
  189. package/dist/ui/editable-combobox.d.ts +11 -0
  190. package/dist/ui/hint.d.ts +8 -0
  191. package/dist/ui/image.d.ts +24 -0
  192. package/dist/ui/input-group.d.ts +16 -0
  193. package/dist/ui/input-with-addon.d.ts +17 -0
  194. package/dist/ui/input.d.ts +3 -0
  195. package/dist/ui/label.d.ts +4 -0
  196. package/dist/ui/loading-button.d.ts +15 -0
  197. package/dist/ui/popover.d.ts +9 -0
  198. package/dist/ui/scroll-area.d.ts +5 -0
  199. package/dist/ui/select.d.ts +22 -0
  200. package/dist/ui/separator.d.ts +4 -0
  201. package/dist/ui/shared-loading-button.d.ts +20 -0
  202. package/dist/ui/simple-dialog.d.ts +19 -0
  203. package/dist/ui/simple-popover.d.ts +13 -0
  204. package/dist/ui/simple-select.d.ts +20 -0
  205. package/dist/ui/skeleton.d.ts +2 -0
  206. package/dist/ui/slider.d.ts +4 -0
  207. package/dist/ui/spinner-box.d.ts +12 -0
  208. package/dist/ui/spinner.d.ts +2 -0
  209. package/dist/ui/switch.d.ts +6 -0
  210. package/dist/ui/tab/index.d.ts +15 -0
  211. package/dist/ui/textarea.d.ts +3 -0
  212. package/dist/ui/tooltip.d.ts +7 -0
  213. package/dist/workers/image-resize.worker.d.ts +1 -0
  214. package/dist/workers/mockup.worker.d.ts +5 -0
  215. package/dist/workers/pixel-map.worker.d.ts +1 -0
  216. package/dist/workers/render-asset-cache.d.ts +18 -0
  217. package/dist/workers/render.worker.d.ts +1 -0
  218. package/docs/HOST_DATA_CONTRACT.md +426 -0
  219. package/docs/INTEGRATION_API.md +549 -0
  220. package/docs/SEMI_RENDERER.md +141 -0
  221. package/docs/auth-api.md +3 -0
  222. package/package.json +208 -4
@@ -0,0 +1,1409 @@
1
+ import { jsxs as _e, jsx as z } from "react/jsx-runtime";
2
+ import { forwardRef as Te, useRef as R, useState as re, useCallback as N, useEffect as te, useImperativeHandle as Le } from "react";
3
+ import { LAYER_BLACK_LIST as Se, BLEND_MODES_MAP as We } from "./engine/constants.js";
4
+ import { P as Re, r as qe } from "./pixel-map-dSys7UAx.js";
5
+ import "./engine/fabric.js";
6
+ import { v4 as He } from "uuid";
7
+ import { cloneObject as G, urlToDataURL as ze } from "./engine/utils/common.js";
8
+ import { ImageProcessor as De } from "./engine/processors/image.js";
9
+ import { TemplateProcessor as Fe } from "./engine/processors/template.js";
10
+ import { TextProcessor as Ne } from "./engine/processors/text.js";
11
+ import { ResizeManager as Ve } from "./engine/managers/resize.js";
12
+ import { loadOverlaySvgImage as Ge, loadImage as Je } from "./engine/utils/image.js";
13
+ import { WorkspaceManager as Ke } from "./engine/managers/workspace.js";
14
+ import { HistoryManager as Qe } from "./engine/history.js";
15
+ import Ze from "./engine/events.js";
16
+ import { fabric as U } from "fabric";
17
+ import { setDefaultControls as $e } from "./engine/utils/setting.js";
18
+ class Me {
19
+ constructor(e, s, r, t = 0.85) {
20
+ this.imageCache = /* @__PURE__ */ new Map(), this.overlayCache = /* @__PURE__ */ new Map(), this.destroyed = !1, this.deleteObject = (i) => {
21
+ if (i) {
22
+ const a = this.canvas._objects.find((n) => n.id === i);
23
+ a && (this.canvas.remove(a), this.revokeImgBlobLink(a));
24
+ } else {
25
+ const a = this.canvas.getActiveObjects();
26
+ a.length > 0 && (a.forEach((n) => {
27
+ this.canvas.remove(n), this.revokeImgBlobLink(n);
28
+ }), this.canvas.discardActiveObject());
29
+ }
30
+ this.canvas.requestRenderAll();
31
+ }, this.canvas = new U.Canvas(e, {
32
+ enableRetinaScaling: !0,
33
+ width: s.width,
34
+ height: s.height,
35
+ preserveObjectStacking: !0,
36
+ stopContextMenu: !1,
37
+ centeredRotation: !0,
38
+ hoverCursor: "grab",
39
+ moveCursor: "grabbing",
40
+ allowTouchScrolling: !0,
41
+ selection: !0,
42
+ interactive: !0,
43
+ controlsAboveOverlay: !0,
44
+ fireRightClick: !0
45
+ // 允许识别右键点击
46
+ }), this.overlayScale = t, this.imageProcessor = new De(this), this.templateProcessor = new Fe(this), this.textProcessor = new Ne(this), this.resizeManager = new Ve(this, r), this.workspaceManager = new Ke(this);
47
+ }
48
+ /**
49
+ * ImageProcessor/TextProcessor are shared with the interactive editor and
50
+ * expect a loading-overlay factory. SemiExportEditor renders on a hidden
51
+ * canvas, so drawing a spinner would only add work and could leak into the
52
+ * exported texture. Keep the shared processor contract with a no-op handle.
53
+ */
54
+ createLoading() {
55
+ return {
56
+ addLoadingGroup() {
57
+ },
58
+ removeLoadingGroup() {
59
+ }
60
+ };
61
+ }
62
+ /**
63
+ * 渲染 overlayImage(SVG)
64
+ * @param mockup 模板信息
65
+ * @param changeView 是否切换视图,如果为true,则需要重新设置画布和背景图
66
+
67
+ */
68
+ async renderOverlayImage(e, s = !1) {
69
+ const { image: r, clipPath: t } = await this.getOverlayImage(e.image), i = Math.min(this.canvas.width, this.canvas.height) * this.overlayScale, a = i / r.width, n = i / r.height, c = Math.min(a, n);
70
+ return r.set({
71
+ scaleX: c,
72
+ scaleY: c,
73
+ left: (this.canvas.width - r.width * c) / 2,
74
+ top: (this.canvas.height - r.height * c) / 2,
75
+ selectable: !1
76
+ }), t && t.set({
77
+ scaleX: c,
78
+ scaleY: c,
79
+ left: (this.canvas.width - t.width * c) / 2,
80
+ top: (this.canvas.height - t.height * c) / 2,
81
+ absolutePositioned: !0,
82
+ selectable: !1,
83
+ evented: !1
84
+ }), s && this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]), new Promise((o) => {
85
+ this.canvas.setOverlayImage(r, async () => {
86
+ this.workspaceManager.setClipPath(t ?? r, {
87
+ width: r.width,
88
+ height: r.height
89
+ }), this.setupOverlayBorders(r, t), this.canvas.renderAll(), o();
90
+ });
91
+ });
92
+ }
93
+ /**
94
+ * 获取 overlay 图:优先使用会话级缓存,避免重复网络请求
95
+ * 会返回深拷贝,确保不同视图间互不影响
96
+ */
97
+ async getOverlayImage(e) {
98
+ const s = this.overlayCache.get(e);
99
+ if (s) {
100
+ const a = await G(s.image), n = s.clipPath ? await G(s.clipPath) : void 0;
101
+ return { image: a, clipPath: n };
102
+ }
103
+ const r = await Ge(e);
104
+ this.overlayCache.set(e, r);
105
+ const t = await G(r.image), i = r.clipPath ? await G(r.clipPath) : void 0;
106
+ return { image: t, clipPath: i };
107
+ }
108
+ /**
109
+ * 绘制可设计区域边框,优先使用 clipPath 的真实形状
110
+ */
111
+ setupOverlayBorders(e, s) {
112
+ this.overlayBorderHandler && this.canvas.off("after:render", this.overlayBorderHandler);
113
+ const r = s ?? e;
114
+ this.overlayBorderHandler = () => {
115
+ const t = this.canvas.getContext(), i = this.canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
116
+ t.save(), t.transform(i[0], i[1], i[2], i[3], i[4], i[5]);
117
+ const a = {
118
+ stroke: r.stroke,
119
+ strokeWidth: r.strokeWidth,
120
+ strokeDashArray: r.strokeDashArray,
121
+ fill: r.fill,
122
+ strokeUniform: r.strokeUniform
123
+ };
124
+ r.set({
125
+ stroke: "#C3C3CC",
126
+ strokeWidth: 1,
127
+ strokeDashArray: [],
128
+ fill: "transparent",
129
+ strokeUniform: !0,
130
+ objectCaching: !1
131
+ }), r.render(t);
132
+ const n = 6, c = (r.getScaledWidth() + n * 2) / r.getScaledWidth(), o = (r.getScaledHeight() + n * 2) / r.getScaledHeight(), m = r.left ?? 0, f = r.top ?? 0, P = r.scaleX ?? 1, u = r.scaleY ?? 1;
133
+ r.set({
134
+ strokeDashArray: [4, 4],
135
+ stroke: "#C3C3CC",
136
+ strokeWidth: 1,
137
+ strokeUniform: !0,
138
+ scaleX: P * c,
139
+ scaleY: u * o,
140
+ left: m - n,
141
+ top: f - n
142
+ }), r.render(t), r.set({
143
+ stroke: a.stroke,
144
+ strokeWidth: a.strokeWidth,
145
+ strokeDashArray: a.strokeDashArray,
146
+ fill: a.fill,
147
+ strokeUniform: a.strokeUniform,
148
+ scaleX: P,
149
+ scaleY: u,
150
+ left: m,
151
+ top: f
152
+ }), t.restore();
153
+ }, this.canvas.on("after:render", this.overlayBorderHandler);
154
+ }
155
+ /**
156
+ * 渲染背景图(Bitmap)png、webp 等位图
157
+ * @param mockup 模板信息
158
+ * @param color 背景颜色
159
+ * @param changeView 是否切换视图,如果为true,则需要重新设置画布和背景图
160
+ */
161
+ // async renderBitmapBackground(mockup: Mockup, color: string, changeView: boolean = false) {
162
+ // const renderImage = await loadImage(mockup.generateImageUrl);
163
+ // renderImage.set({
164
+ // scaleX: this.canvas.width! / renderImage.width!,
165
+ // scaleY: this.canvas.height! / renderImage.height!,
166
+ // });
167
+ // if (changeView) {
168
+ // this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
169
+ // }
170
+ // return new Promise((resolve) => {
171
+ // this.canvas.setOverlayImage(renderImage, async () => {
172
+ // this.workspaceManager.setClipPath(mockup);
173
+ // // 渲染背景遮罩
174
+ // this.setBackgroundColor(color);
175
+ // await this.setBackgroundMask(mockup.maskImageUrl);
176
+ // this.canvas.renderAll();
177
+ // resolve(null);
178
+ // });
179
+ // });
180
+ // }
181
+ /**
182
+ * 设置背景遮罩
183
+ */
184
+ async setBackgroundMask(e) {
185
+ const s = document.querySelector(".lower-canvas");
186
+ if (s && !e)
187
+ return s.style.backgroundImage = "", Promise.resolve();
188
+ const r = await Je(e);
189
+ if (r == null) return Promise.resolve();
190
+ r.set({
191
+ scaleX: this.canvas.width / r.width,
192
+ scaleY: this.canvas.height / r.height
193
+ });
194
+ const t = r.toDataURL({ format: "png" });
195
+ s && (s.style.backgroundImage = `url(${t})`, s.style.backgroundSize = "100% 100%");
196
+ }
197
+ /**
198
+ * 设置背景颜色、透明图案
199
+ */
200
+ async setBackground(e) {
201
+ e ? await this.workspaceManager.setFillColor(e) : await this.workspaceManager.setTransparentPattern(), this.canvas.requestRenderAll();
202
+ }
203
+ /**
204
+ * 获取当前选中的对象
205
+ */
206
+ get activeObject() {
207
+ return this.canvas.getActiveObject();
208
+ }
209
+ /**
210
+ * 获取对象经过画布转换后的实际尺寸
211
+ */
212
+ getObjectActualSize(e) {
213
+ const { left: s = 0, top: r = 0, width: t = 0, height: i = 0 } = e.getBoundingRect(), a = e.getCenterPoint(), n = this.canvas.viewportTransform;
214
+ return {
215
+ left: s * n[0] + n[4],
216
+ top: r * n[3] + n[5],
217
+ width: t * n[0],
218
+ height: i * n[3],
219
+ centerX: a.x * n[0] + n[4],
220
+ centerY: a.y * n[3] + n[5]
221
+ };
222
+ }
223
+ /**
224
+ * 添加对象
225
+ *
226
+ */
227
+ addObject(e, s = !0) {
228
+ e.set({
229
+ id: He()
230
+ }), this.canvas.add(e), s && this.canvas.setActiveObject(e);
231
+ }
232
+ /**
233
+ * 恢复对象
234
+ */
235
+ async restoreObject(e) {
236
+ return new Promise((s) => {
237
+ U.util.enlivenObjects([e], (r) => s(r), "");
238
+ });
239
+ }
240
+ /**
241
+ * 半定制文本更新:如果存在 originalWidth / originalHeight,则按固定框重新计算 fitScale;
242
+ * 否则只更新文本内容/字体等基础属性。
243
+ */
244
+ async updateText(e, s) {
245
+ if (!(typeof e?.type == "string" && (e.type.includes("i-text") || e.type === "text"))) return null;
246
+ await this.textProcessor.ensureFontLoaded(s);
247
+ const [t] = await this.restoreObject(e);
248
+ if (!t) return null;
249
+ const i = e.scaleX || 1, a = e.scaleY || 1, n = e?.customConfig, c = typeof n?.originalWidth == "number" && n.originalWidth > 0 && typeof n?.originalHeight == "number" && n.originalHeight > 0 && !n.moveable, o = t.type === "advanced-i-text" && t.radius, m = () => ({
250
+ ...typeof t.__baseWidth < "u" ? { __baseWidth: t.__baseWidth } : {},
251
+ ...typeof t.__baseHeight < "u" ? { __baseHeight: t.__baseHeight } : {},
252
+ ...typeof t.__basePadding < "u" ? { __basePadding: t.__basePadding } : {},
253
+ ...typeof t.__baseObjectCaching < "u" ? { __baseObjectCaching: t.__baseObjectCaching } : {},
254
+ ...typeof t.__curvedCenterX < "u" ? { __curvedCenterX: t.__curvedCenterX } : {},
255
+ ...typeof t.__curvedCenterY < "u" ? { __curvedCenterY: t.__curvedCenterY } : {},
256
+ ...typeof t.__lastCurvedWidth < "u" ? { __lastCurvedWidth: t.__lastCurvedWidth } : {},
257
+ ...typeof t.__lastCurvedHeight < "u" ? { __lastCurvedHeight: t.__lastCurvedHeight } : {},
258
+ ...typeof t.__lastCurvedCenterX < "u" ? { __lastCurvedCenterX: t.__lastCurvedCenterX } : {},
259
+ ...typeof t.__lastCurvedCenterY < "u" ? { __lastCurvedCenterY: t.__lastCurvedCenterY } : {},
260
+ ...typeof t.__rawText < "u" ? { __rawText: t.__rawText } : {},
261
+ ...typeof t.orientation < "u" ? { orientation: t.orientation } : {},
262
+ ...typeof t.radius < "u" ? { radius: t.radius } : {},
263
+ ...typeof t.spacing < "u" ? { spacing: t.spacing } : {}
264
+ });
265
+ if (!c) {
266
+ const b = o ? t.getCenterPoint() : null;
267
+ t.set(s), o && (t.__restoredGeometry = !1);
268
+ try {
269
+ t.initDimensions?.();
270
+ } catch {
271
+ }
272
+ return b && t.setPositionByOrigin(b, "center", "center"), t.setCoords(), {
273
+ ...s,
274
+ text: t.text,
275
+ left: t.left,
276
+ top: t.top,
277
+ width: t.width,
278
+ height: t.height,
279
+ scaleX: t.scaleX,
280
+ scaleY: t.scaleY,
281
+ angle: t.angle,
282
+ originX: t.originX,
283
+ originY: t.originY,
284
+ selectable: e.selectable,
285
+ ...m()
286
+ };
287
+ }
288
+ const f = e?.__semiTargetInfo, P = !!f && typeof f.width == "number" && f.width > 0 && typeof f.height == "number" && f.height > 0 && typeof f.centerX == "number" && typeof f.centerY == "number";
289
+ let u;
290
+ if (P) {
291
+ const b = t.getCenterPoint();
292
+ u = {
293
+ width: n.originalWidth,
294
+ height: n.originalHeight,
295
+ centerX: typeof f.centerX == "number" ? f.centerX : b.x,
296
+ centerY: typeof f.centerY == "number" ? f.centerY : b.y,
297
+ signX: typeof f.signX == "number" && f.signX !== 0 ? Math.sign(f.signX) : Math.sign(i) || 1,
298
+ signY: typeof f.signY == "number" && f.signY !== 0 ? Math.sign(f.signY) : Math.sign(a) || 1
299
+ };
300
+ } else {
301
+ const b = t.getCenterPoint();
302
+ u = {
303
+ width: n.originalWidth,
304
+ height: n.originalHeight,
305
+ centerX: b.x,
306
+ centerY: b.y,
307
+ signX: Math.sign(i) || 1,
308
+ signY: Math.sign(a) || 1
309
+ };
310
+ }
311
+ t.set({
312
+ ...s,
313
+ scaleX: u.signX,
314
+ scaleY: u.signY
315
+ }), o && (t.__restoredGeometry = !1);
316
+ try {
317
+ t.initDimensions?.();
318
+ } catch {
319
+ }
320
+ t.setCoords();
321
+ const Y = t.getBoundingRect(!0, !0), M = Math.max(1, Y.width || 1), X = Math.max(1, Y.height || 1);
322
+ let C = Math.min(u.width / M, u.height / X);
323
+ return (!Number.isFinite(C) || C <= 0) && (C = 1), t.set({
324
+ scaleX: C * u.signX,
325
+ scaleY: C * u.signY
326
+ }), t.setPositionByOrigin(new U.Point(u.centerX, u.centerY), "center", "center"), t.setCoords(), {
327
+ ...s,
328
+ text: t.text,
329
+ left: t.left,
330
+ top: t.top,
331
+ width: t.width,
332
+ height: t.height,
333
+ scaleX: t.scaleX,
334
+ scaleY: t.scaleY,
335
+ angle: t.angle,
336
+ originX: t.originX,
337
+ originY: t.originY,
338
+ selectable: e.selectable,
339
+ __semiTargetInfo: u,
340
+ ...m()
341
+ };
342
+ }
343
+ /**
344
+ * 半定制图片替换:使用 cover 模式重新计算缩放,保持原始占位框中心不变。
345
+ */
346
+ async updateImage(e, s) {
347
+ if (e?.type !== "image") return null;
348
+ const r = s.src;
349
+ if (!r) return null;
350
+ const [t] = await this.restoreObject(e);
351
+ if (!t) return null;
352
+ const i = t, a = e.scaleX || 1, n = e.scaleY || 1, c = e.width || 1, o = e.height || 1, m = c * Math.abs(a), f = o * Math.abs(n), P = t.getCenterPoint();
353
+ return new Promise((u) => {
354
+ i.setSrc(r, () => {
355
+ const Y = i.width || 1, M = i.height || 1, X = Math.max(m / Y, f / M);
356
+ i.set({
357
+ ...s,
358
+ scaleX: X * Math.sign(a),
359
+ scaleY: X * Math.sign(n)
360
+ }), i.setPositionByOrigin(P, "center", "center"), i.setCoords(), u({
361
+ ...s,
362
+ width: Y,
363
+ height: M,
364
+ left: i.left,
365
+ top: i.top,
366
+ scaleX: i.scaleX,
367
+ scaleY: i.scaleY,
368
+ angle: i.angle,
369
+ originX: i.originX,
370
+ originY: i.originY,
371
+ selectable: e.selectable
372
+ });
373
+ });
374
+ });
375
+ }
376
+ /**
377
+ * 清理图片 blob URL(只清理不在缓存中的临时 blob)
378
+ * @param obj 对象
379
+ */
380
+ revokeImgBlobLink(e) {
381
+ if (e.type === "image" && e.getSrc) {
382
+ const s = e, r = s.getSrc(), t = s.originSrc || r;
383
+ if (r && r.startsWith("blob:")) {
384
+ const i = this.imageCache.get(t);
385
+ if (!i || i.blobUrl !== r)
386
+ try {
387
+ URL.revokeObjectURL(r);
388
+ } catch {
389
+ }
390
+ }
391
+ }
392
+ }
393
+ /**
394
+ * 修改对象的属性
395
+ * @param {String} id 对象id
396
+ * @param {Record<string, any>} obj 对象属性集合
397
+ */
398
+ updateObject(e, s) {
399
+ const r = this.canvas._objects.find((t) => t.id === e);
400
+ if (r) {
401
+ if (s.hasOwnProperty("angle"))
402
+ r.rotate(s.angle);
403
+ else if (s.hasOwnProperty("align")) {
404
+ const { left: t, right: i, top: a, bottom: n, center: c } = this.workspaceManager.dimension, o = r.getBoundingRect(!0, !0);
405
+ switch (s.align) {
406
+ case "top":
407
+ r.set({
408
+ top: r.top + (a - o.top)
409
+ });
410
+ break;
411
+ case "left":
412
+ r.set({
413
+ left: r.left + (t - o.left)
414
+ });
415
+ break;
416
+ case "horizontal-center":
417
+ r.set({
418
+ left: r.left + (c.x - (o.left + o.width / 2))
419
+ });
420
+ break;
421
+ case "right":
422
+ r.set({
423
+ left: r.left + (i - (o.left + o.width))
424
+ });
425
+ break;
426
+ case "vertical-center":
427
+ r.set({
428
+ top: r.top + (c.y - (o.top + o.height / 2))
429
+ });
430
+ break;
431
+ case "bottom":
432
+ r.set({
433
+ top: r.top + (n - (o.top + o.height))
434
+ });
435
+ break;
436
+ }
437
+ } else if (s.hasOwnProperty("zIndex"))
438
+ this.canvas.moveTo(r, s.zIndex);
439
+ else if (s.hasOwnProperty("scale"))
440
+ this.updateObjectScale(r, s.scale);
441
+ else if (s.hasOwnProperty("width") || s.hasOwnProperty("height")) {
442
+ const t = s.hasOwnProperty("width") ? r.width : r.height, a = (s.width || s.height) / t * this.workspaceManager.scale;
443
+ this.updateObjectScale(r, a);
444
+ } else
445
+ r.set(s);
446
+ r.setCoords(), this.canvas.requestRenderAll(), s.hasOwnProperty("selectable") || this.canvas.fire("object:modified", {
447
+ target: r
448
+ });
449
+ }
450
+ }
451
+ /**
452
+ * 对象缩放
453
+ */
454
+ updateObjectScale(e, s) {
455
+ const r = e.getCenterPoint(), t = this.workspaceManager.dimension.width * 0.01 / e.width;
456
+ e.scale(s < t ? t : s), e.setPositionByOrigin(r, "center", "center");
457
+ }
458
+ /**
459
+ * 修改画布上的对象的选中状态和事件状态
460
+ * @param status
461
+ */
462
+ updateObjectsEvents(e) {
463
+ this.canvas.forEachObject((s) => {
464
+ s.set({
465
+ selectable: e,
466
+ evented: e
467
+ });
468
+ });
469
+ }
470
+ /**
471
+ * 获取当前可选择的对象,不包括背景图、裁剪区域
472
+ */
473
+ getLayerList() {
474
+ return this.canvas._objects.filter((e) => !Se.includes(e.id || ""));
475
+ }
476
+ /**
477
+ * 设置当前选中的对象
478
+ */
479
+ setActiveObject(e) {
480
+ const s = this.canvas.getActiveObjects(), r = s.some((i) => i.id === e), t = this.getLayerList().find((i) => i.id === e);
481
+ t && (this.canvas.discardActiveObject(), (!r || s.length > 1) && this.canvas.setActiveObject(t), this.canvas.requestRenderAll());
482
+ }
483
+ /**
484
+ * 同时选中多个对象
485
+ */
486
+ setActiveObjects(e) {
487
+ const s = new U.ActiveSelection(e, { canvas: this.canvas });
488
+ this.canvas.setActiveObject(s), this.canvas.requestRenderAll();
489
+ }
490
+ /**
491
+ * 以画布中心点缩放画布
492
+ */
493
+ zoomToCenter(e) {
494
+ if (e === 1)
495
+ this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
496
+ else {
497
+ const s = this.canvas.viewportTransform, r = s[0], t = s[4], i = s[5], a = this.canvas.width / 2, n = this.canvas.height / 2, c = e - r, o = t - a * c, m = i - n * c;
498
+ this.canvas.setViewportTransform([e, 0, 0, e, o, m]);
499
+ }
500
+ this.canvas.requestRenderAll();
501
+ }
502
+ /**
503
+ * 获取当前画布上渲染的对象
504
+ */
505
+ getRenderedObjects() {
506
+ const { objects: e } = this.canvas.toJSON(Qe.DEFAULT_SAVE_PROPERTIES), s = this.workspaceManager.dimension;
507
+ return {
508
+ objects: e.filter((r) => !Se.includes(r.id || "")),
509
+ workspaceMetadata: {
510
+ width: s.width,
511
+ height: s.height,
512
+ centerX: s.center.x,
513
+ centerY: s.center.y
514
+ }
515
+ };
516
+ }
517
+ /**
518
+ * 清空当前画布
519
+ */
520
+ clearCanvas() {
521
+ this.canvas._objects.length && (this.canvas.clear(), this.canvas.requestRenderAll());
522
+ }
523
+ /**
524
+ * 将图片对象的 clipPath 重新关联到画布上的对象
525
+ */
526
+ relinkClipPaths() {
527
+ const e = /* @__PURE__ */ new Map(), s = this.canvas.getObjects(), r = (i) => {
528
+ const a = i.id;
529
+ a && e.set(String(a), i), i.type === "group" && i._objects?.forEach(r);
530
+ };
531
+ s.forEach(r);
532
+ const t = (i) => {
533
+ const n = i.clipPath?.id;
534
+ if (n && e.has(String(n))) {
535
+ const c = e.get(String(n));
536
+ c.set({ absolutePositioned: !0 }), i.set("clipPath", c), i.dirty = !0;
537
+ }
538
+ i.type === "group" && i._objects?.forEach(t);
539
+ };
540
+ s.forEach(t);
541
+ }
542
+ /**
543
+ * 切换视图,重新渲染画布
544
+ */
545
+ async switchView(e, s, r) {
546
+ if (this.canvas.clear(), r && r.objects && r.objects.length)
547
+ return await this.textProcessor.preloadFontsFromState(r), new Promise((t) => {
548
+ this.canvas.loadFromJSON(
549
+ r,
550
+ async () => {
551
+ this.relinkClipPaths(), await this.renderOverlayImage(e, !0), await this.setBackground(s), r.workspaceMetadata && this._scaleObjectsToWorkspace(r.workspaceMetadata);
552
+ const i = this.canvas.getObjects().filter((a) => a.type === "image");
553
+ i.length > 0 && await Promise.all(
554
+ i.map((a) => new Promise((n) => {
555
+ const c = a.getElement(), o = a.getSrc() || a.originSrc;
556
+ if (!o) {
557
+ n();
558
+ return;
559
+ }
560
+ c && c.complete && c.naturalWidth > 0 ? (this.registerImageCache(o, a), n()) : c ? (c.onload = () => {
561
+ this.registerImageCache(o, a), n();
562
+ }, c.onerror = () => {
563
+ n();
564
+ }) : n();
565
+ }))
566
+ ), this.canvas.renderAll(), Ze.emit("design:layerChange"), t();
567
+ },
568
+ (i, a) => {
569
+ i.clipPath && !r.objects.some((n) => n.id === i.clipPath.id) && (delete a.clipPath, U.util.enlivenObjects(
570
+ [i.clipPath],
571
+ (n) => {
572
+ const c = n[0];
573
+ c && (a.set("clipPath", c), a.dirty = !0, this.canvas.requestRenderAll());
574
+ },
575
+ ""
576
+ ));
577
+ }
578
+ );
579
+ });
580
+ await this.renderOverlayImage(e, !0), await this.setBackground(s), r && r.object && await this.imageProcessor.addBitmapImage(r.object);
581
+ }
582
+ /**
583
+ * 半定制固定文本在修改内容/字体后,按 originalWidth/originalHeight 重新计算自适应缩放。
584
+ * 仅供半定制 Pixi 链路使用,不影响通用编辑器。
585
+ */
586
+ /**
587
+ * 根据 workspace 尺寸变化,按比例缩放所有对象
588
+ */
589
+ _scaleObjectsToWorkspace(e) {
590
+ const s = this.workspaceManager.dimension, r = Math.min(
591
+ s.width / e.width,
592
+ s.height / e.height
593
+ ), t = e.centerX, i = e.centerY, a = s.center.x, n = s.center.y, c = Math.abs(a - t), o = Math.abs(n - i), m = Math.abs(r - 1) >= 1e-3, f = c >= 0.1 || o >= 0.1;
594
+ if (!m && !f) return;
595
+ const P = this.canvas.getObjects(), u = new Set(P), Y = /* @__PURE__ */ new Set(), M = (h) => {
596
+ const p = h.clipPath;
597
+ p && u.has(p) && Y.add(p);
598
+ };
599
+ P.forEach(M);
600
+ const X = (h) => {
601
+ const p = (h.left || 0) - t, T = (h.top || 0) - i, E = a + p * r, l = n + T * r;
602
+ h.set({
603
+ left: E,
604
+ top: l,
605
+ scaleX: (h.scaleX || 1) * r,
606
+ scaleY: (h.scaleY || 1) * r
607
+ }), h.setCoords(), h.dirty = !0;
608
+ }, C = /* @__PURE__ */ new Set(), b = (h) => {
609
+ !h.selectable && !Y.has(h) || (X(h), C.add(h));
610
+ };
611
+ P.forEach(b);
612
+ const A = (h) => {
613
+ const p = h.clipPath;
614
+ p && p.absolutePositioned && !u.has(p) && !C.has(p) && (X(p), C.add(p));
615
+ };
616
+ P.forEach(A);
617
+ }
618
+ /**
619
+ * 计算对象相对于画布的全局变换矩阵(包括父组的影响)
620
+ */
621
+ getGlobalTransformMatrix(e) {
622
+ return e.calcTransformMatrix();
623
+ }
624
+ /**
625
+ * 判断对象是否位于 activeSelection 中
626
+ */
627
+ isInActiveSelection(e) {
628
+ let s = e.group;
629
+ for (; s; ) {
630
+ if (s.type === "activeSelection") return !0;
631
+ s = s.group;
632
+ }
633
+ return !1;
634
+ }
635
+ /**
636
+ * 注册图片到缓存(会话级缓存,整个编辑期间保持)
637
+ * @param src 图片源地址(originSrc)
638
+ * @param img fabric.Image 对象
639
+ * @param blobUrl 可选的 blob URL(用于撤销/重做)
640
+ */
641
+ registerImageCache(e, s, r) {
642
+ if (!e || e.startsWith("data:")) return;
643
+ const t = s.getElement(), i = this.imageCache.get(e);
644
+ this.imageCache.set(e, {
645
+ element: t,
646
+ blobUrl: r || i?.blobUrl,
647
+ lastUsed: Date.now()
648
+ });
649
+ }
650
+ /**
651
+ * 清除整个图片缓存(在关闭编辑器时调用)
652
+ */
653
+ clearImageCache() {
654
+ this.imageCache.forEach((e) => {
655
+ if (e.blobUrl)
656
+ try {
657
+ URL.revokeObjectURL(e.blobUrl);
658
+ } catch {
659
+ }
660
+ }), this.imageCache.clear();
661
+ }
662
+ /**
663
+ * 销毁编辑器,释放所有资源
664
+ */
665
+ destroy() {
666
+ if (this.destroyed) return;
667
+ this.destroyed = !0, this.resizeManager.destroy(), this.clearImageCache();
668
+ const e = this.canvas;
669
+ e.contextContainer && e.contextTop && (e.cancelRequestedRender?.(), e.dispose());
670
+ }
671
+ /**
672
+ * 按需生成图片 dataURL(不缓存,减少内存占用)
673
+ */
674
+ async getCachedDataURL(e) {
675
+ if (!e)
676
+ return "";
677
+ const s = this.imageCache.get(e);
678
+ if (s?.element && s.element.complete) {
679
+ const r = document.createElement("canvas");
680
+ r.width = s.element.naturalWidth || s.element.width, r.height = s.element.naturalHeight || s.element.height;
681
+ const t = r.getContext("2d");
682
+ if (t) {
683
+ t.drawImage(s.element, 0, 0);
684
+ const i = r.toDataURL("image/png");
685
+ return r.width = 0, r.height = 0, i;
686
+ }
687
+ return "";
688
+ }
689
+ return await ze(e);
690
+ }
691
+ /**
692
+ * 从缓存克隆图片,避免重复下载
693
+ */
694
+ async cloneImageFromCache(e, s) {
695
+ const r = e.getSrc() || "", t = e.originSrc || "", i = e.isCropped, a = t || r;
696
+ if (a.startsWith("data:"))
697
+ return new Promise((c) => {
698
+ e.clone(
699
+ (o) => {
700
+ o.set(s), c(o);
701
+ },
702
+ ["absolutePositioned"]
703
+ );
704
+ });
705
+ const n = this.imageCache.get(a);
706
+ return n?.element && n.element.complete ? new Promise(async (c) => {
707
+ const o = new U.Image(n.element, {
708
+ width: e.width,
709
+ height: e.height,
710
+ ...s
711
+ });
712
+ if (e.clipPath)
713
+ try {
714
+ const m = await G(e.clipPath, ["absolutePositioned"]);
715
+ o.set({ clipPath: m });
716
+ } catch {
717
+ }
718
+ i && (o.isCropped = !0, o.cropBlob = e.cropBlob), c(o);
719
+ }) : new Promise((c) => {
720
+ e.clone(
721
+ (o) => {
722
+ this.registerImageCache(a, o), o.set(s), i && (o.isCropped = !0, o.cropBlob = e.cropBlob), c(o);
723
+ },
724
+ ["absolutePositioned"]
725
+ );
726
+ });
727
+ }
728
+ /**
729
+ * Rasterize the current printable area for a live product preview without
730
+ * cloning every Fabric object into a second canvas.
731
+ *
732
+ * This mirrors the full designer's fast preview path: hide the mockup
733
+ * overlay and selection controls, capture the existing lower canvas, and
734
+ * scale only the printable workspace to the requested longest edge.
735
+ */
736
+ async exportPreviewBitmap(e = 600, s) {
737
+ if (this.destroyed) throw new Error("Canvas is not ready");
738
+ const r = this.workspaceManager.dimension, t = this.workspaceManager.printAreaSize?.width ?? r.width, i = this.workspaceManager.printAreaSize?.height ?? r.height;
739
+ if (!r.width || !r.height || !t || !i)
740
+ throw new Error("Printable workspace has no dimensions");
741
+ const a = Math.max(t, i), n = Math.max(1, e) / a, c = Math.max(1, Math.round(t * n)), o = Math.max(1, Math.round(i * n)), m = this.canvas.viewportTransform?.slice(), f = this.canvas.overlayImage?.visible, P = this.canvas.backgroundColor, u = this.canvas.getActiveObject(), Y = u ? { hasBorders: u.hasBorders, hasControls: u.hasControls } : null;
742
+ try {
743
+ this.canvas.overlayImage && (this.canvas.overlayImage.visible = !1), u?.set({ hasBorders: !1, hasControls: !1 }), this.canvas.backgroundColor = s || "transparent", this.canvas.viewportTransform = [1, 0, 0, 1, 0, 0], this.canvas.renderAll();
744
+ const M = document.createElement("canvas");
745
+ M.width = c, M.height = o;
746
+ const X = M.getContext("2d", { alpha: !0 });
747
+ if (!X) throw new Error("Could not create preview canvas context");
748
+ const C = this.canvas, b = C.getRetinaScaling?.() ?? window.devicePixelRatio ?? 1;
749
+ X.drawImage(
750
+ C.lowerCanvasEl,
751
+ r.left * b,
752
+ r.top * b,
753
+ r.width * b,
754
+ r.height * b,
755
+ 0,
756
+ 0,
757
+ c,
758
+ o
759
+ );
760
+ try {
761
+ return await createImageBitmap(M);
762
+ } finally {
763
+ M.width = 0, M.height = 0;
764
+ }
765
+ } finally {
766
+ this.canvas.backgroundColor = P, this.canvas.overlayImage && f !== void 0 && (this.canvas.overlayImage.visible = f), u && Y && u.set(Y), m && (this.canvas.viewportTransform = m), this.destroyed || this.canvas.requestRenderAll();
767
+ }
768
+ }
769
+ /**
770
+ * 导出 clipPath 区域的 SVG(按 overlay 原始尺寸)
771
+ * @param {boolean} embedImages 是否将图片转为 dataURL 内嵌
772
+ * @param {String?} color 背景颜色
773
+ * @returns {Promise<string[]>} SVG 字符串
774
+ */
775
+ async export(e = !1, s, r) {
776
+ const t = this.workspaceManager.dimension, i = this.workspaceManager.printAreaSize?.width ?? t.width, a = this.workspaceManager.printAreaSize?.height ?? t.height, n = window.devicePixelRatio || 1, c = Math.max(i / t.width, a / t.height), o = r?.objectCloneMultiplierCap ?? 8, m = Math.max(1, Math.min(r?.minObjectCloneMultiplier ?? 2, o)), f = Math.max(m, Math.min(o, Math.ceil(c * n)));
777
+ let P = new U.Rect({
778
+ left: 0,
779
+ top: 0,
780
+ width: t.width,
781
+ height: t.height,
782
+ absolutePositioned: !0,
783
+ strokeWidth: 0
784
+ });
785
+ this.canvas.clipPath && (P = await G(this.canvas.clipPath, ["absolutePositioned"]), P.set({
786
+ left: this.canvas.clipPath.left - t.left,
787
+ top: this.canvas.clipPath.top - t.top,
788
+ absolutePositioned: !0
789
+ }));
790
+ const u = new U.StaticCanvas(null, {
791
+ width: t.width,
792
+ height: t.height,
793
+ enableRetinaScaling: !1,
794
+ backgroundColor: s || "transparent",
795
+ preserveObjectStacking: !0,
796
+ clipPath: P
797
+ }), Y = this.getLayerList();
798
+ (await Promise.all(
799
+ Y.map(
800
+ (l) => new Promise((y) => {
801
+ try {
802
+ const I = l.sourceType === "svg";
803
+ if (l.type === "image") {
804
+ const _ = l;
805
+ let v = {
806
+ selectable: !1,
807
+ evented: !1,
808
+ flipX: l.flipX || !1,
809
+ flipY: l.flipY || !1
810
+ };
811
+ if (this.isInActiveSelection(l)) {
812
+ const S = this.getGlobalTransformMatrix(l), g = U.util.qrDecompose(S);
813
+ v = {
814
+ ...v,
815
+ left: (g.translateX || 0) - (t.left || 0),
816
+ top: (g.translateY || 0) - (t.top || 0),
817
+ angle: g.angle || 0,
818
+ scaleX: g.scaleX || 1,
819
+ scaleY: g.scaleY || 1,
820
+ originX: "center",
821
+ originY: "center"
822
+ };
823
+ } else
824
+ v = {
825
+ ...v,
826
+ left: l.left - t.left,
827
+ top: l.top - t.top,
828
+ angle: l.angle || 0,
829
+ scaleX: l.scaleX || 1,
830
+ scaleY: l.scaleY || 1,
831
+ originX: "left",
832
+ originY: "top"
833
+ };
834
+ this.cloneImageFromCache(_, v).then(async (S) => {
835
+ if (S.clipPath) {
836
+ const g = S.clipPath;
837
+ g.absolutePositioned && g.set({
838
+ left: (g.left || 0) - t.left,
839
+ top: (g.top || 0) - t.top
840
+ });
841
+ }
842
+ if (e && S.type === "image")
843
+ try {
844
+ const g = S, L = g.getSrc() || g.originSrc || "";
845
+ if (L) {
846
+ const O = await this.getCachedDataURL(L);
847
+ g.setSrc(O, () => y(S));
848
+ return;
849
+ }
850
+ } catch {
851
+ }
852
+ y(S);
853
+ }).catch(() => {
854
+ y(l);
855
+ });
856
+ } else I ? G(l, ["absolutePositioned"]).then((_) => {
857
+ if (this.isInActiveSelection(l)) {
858
+ const S = this.getGlobalTransformMatrix(l), g = U.util.qrDecompose(S);
859
+ _.set({
860
+ left: (g.translateX || 0) - (t.left || 0),
861
+ top: (g.translateY || 0) - (t.top || 0),
862
+ angle: g.angle || 0,
863
+ scaleX: g.scaleX || 1,
864
+ scaleY: g.scaleY || 1,
865
+ flipX: l.flipX || !1,
866
+ flipY: l.flipY || !1,
867
+ selectable: !1,
868
+ evented: !1,
869
+ originX: "center",
870
+ originY: "center"
871
+ });
872
+ } else
873
+ _.set({
874
+ left: (l.left || 0) - t.left,
875
+ top: (l.top || 0) - t.top,
876
+ angle: l.angle || 0,
877
+ scaleX: l.scaleX || 1,
878
+ scaleY: l.scaleY || 1,
879
+ flipX: l.flipX || !1,
880
+ flipY: l.flipY || !1,
881
+ selectable: !1,
882
+ evented: !1,
883
+ originX: l.originX || "left",
884
+ originY: l.originY || "top"
885
+ });
886
+ const v = _.clipPath;
887
+ v && v.absolutePositioned && v.set({
888
+ left: (v.left || 0) - t.left,
889
+ top: (v.top || 0) - t.top
890
+ }), _.setCoords(), _.dirty = !0, y(_);
891
+ }).catch(() => y(l)) : l.cloneAsImage(
892
+ (_) => {
893
+ if (this.isInActiveSelection(l)) {
894
+ const v = this.getGlobalTransformMatrix(l), S = U.util.qrDecompose(v);
895
+ _.set({
896
+ left: (S.translateX || 0) - (t.left || 0),
897
+ top: (S.translateY || 0) - (t.top || 0),
898
+ angle: S.angle || 0,
899
+ scaleX: (S.scaleX || 1) / f,
900
+ scaleY: (S.scaleY || 1) / f,
901
+ selectable: !1,
902
+ hasControls: !1,
903
+ hasBorders: !1,
904
+ originX: "center",
905
+ originY: "center"
906
+ });
907
+ } else {
908
+ const v = l.getBoundingRect(!0, !0);
909
+ _.set({
910
+ left: v.left - t.left,
911
+ top: v.top - t.top,
912
+ scaleX: 1 / f,
913
+ scaleY: 1 / f,
914
+ angle: 0,
915
+ selectable: !1,
916
+ hasControls: !1,
917
+ hasBorders: !1,
918
+ originX: "left",
919
+ originY: "top"
920
+ });
921
+ }
922
+ y(_);
923
+ },
924
+ { multiplier: f }
925
+ );
926
+ } catch {
927
+ y(l);
928
+ }
929
+ })
930
+ )
931
+ )).forEach((l) => u.add(l));
932
+ const X = i / t.width, C = a / t.height;
933
+ u.setViewportTransform([X, 0, 0, C, 0, 0]), u.renderAll();
934
+ const b = r?.minOutputSize ?? 1024;
935
+ let A = 1;
936
+ (i > b || a > b) && (A = Math.max(1, b / Math.max(i, a))), typeof r?.outputMultiplierCap == "number" && (A = Math.min(A, r.outputMultiplierCap));
937
+ let h = "";
938
+ const p = () => u.toDataURL({
939
+ format: "webp",
940
+ quality: r?.quality ?? 0.5,
941
+ width: i,
942
+ height: a,
943
+ multiplier: A
944
+ }), T = r?.preferImageBitmap ?? !0, E = r?.returnBlobUrl ?? !1;
945
+ if (T && window.createImageBitmap && typeof u.toCanvasElement == "function") {
946
+ const l = u.toCanvasElement(A, { width: i, height: a });
947
+ try {
948
+ h = await createImageBitmap(l);
949
+ } finally {
950
+ l.width = 0, l.height = 0;
951
+ }
952
+ } else if (E && typeof u.toCanvasElement == "function") {
953
+ const l = u.toCanvasElement(A, { width: i, height: a }), y = await new Promise((I) => l.toBlob(I, "image/webp", r?.quality ?? 0.5));
954
+ l.width = 0, l.height = 0, h = y ? URL.createObjectURL(y) : p();
955
+ } else
956
+ h = p();
957
+ return u.dispose(), h;
958
+ }
959
+ }
960
+ class je {
961
+ constructor(e, s) {
962
+ this.onBusy = e, this.onIdle = s, this.pending = null, this.running = !1;
963
+ }
964
+ get active() {
965
+ return this.running;
966
+ }
967
+ enqueue(e) {
968
+ this.pending = e, this.running || this.drain();
969
+ }
970
+ clear() {
971
+ this.pending = null;
972
+ }
973
+ async drain() {
974
+ this.running = !0, this.onBusy();
975
+ try {
976
+ for (; this.pending; ) {
977
+ const e = this.pending;
978
+ this.pending = null, await e();
979
+ }
980
+ } finally {
981
+ this.running = !1, this.onIdle(), this.pending && this.drain();
982
+ }
983
+ }
984
+ }
985
+ class et {
986
+ constructor(e = 32 * 1024 * 1024, s = 4, r) {
987
+ this.budget = e, this.concurrency = s, this.onEvict = r, this.entries = /* @__PURE__ */ new Map(), this.bytes = 0, this.active = 0, this.waiting = [], this.pinned = /* @__PURE__ */ new Set();
988
+ }
989
+ setPinnedSources(e) {
990
+ this.pinned = new Set(e), this.waiting.forEach((s) => {
991
+ s.foreground = this.pinned.has(s.source);
992
+ }), this.waiting.sort((s, r) => Number(r.foreground) - Number(s.foreground)), this.trim();
993
+ }
994
+ trim() {
995
+ for (const [e, s] of this.entries) {
996
+ if (this.bytes <= this.budget) break;
997
+ !s.bytes || this.pinned.has(e) || (this.entries.delete(e), this.bytes -= s.bytes, this.onEvict?.(e));
998
+ }
999
+ }
1000
+ clear() {
1001
+ for (const e of this.waiting.splice(0)) e.cancel();
1002
+ for (const e of this.entries.keys()) this.onEvict?.(e);
1003
+ this.entries.clear(), this.pinned.clear(), this.bytes = 0;
1004
+ }
1005
+ promote(e) {
1006
+ const s = this.waiting.findIndex((i) => i.source === e && !i.foreground);
1007
+ if (s < 0) return;
1008
+ const [r] = this.waiting.splice(s, 1);
1009
+ r.foreground = !0;
1010
+ const t = this.waiting.findIndex((i) => !i.foreground);
1011
+ this.waiting.splice(t < 0 ? this.waiting.length : t, 0, r);
1012
+ }
1013
+ async fetchBlob(e, s) {
1014
+ await new Promise((r, t) => {
1015
+ const i = () => {
1016
+ this.active += 1, r();
1017
+ };
1018
+ this.active < this.concurrency ? i() : (this.waiting.push({ source: e, foreground: !1, start: i, cancel: () => t(new Error("Resource cache was released")) }), s && this.promote(e));
1019
+ });
1020
+ try {
1021
+ const r = await fetch(e);
1022
+ if (!r.ok) throw new Error(`Could not load render asset (${r.status})`);
1023
+ return await r.blob();
1024
+ } finally {
1025
+ this.active -= 1, this.waiting.shift()?.start();
1026
+ }
1027
+ }
1028
+ load(e, s = !0) {
1029
+ const r = this.entries.get(e);
1030
+ if (r)
1031
+ return this.entries.delete(e), this.entries.set(e, r), s && this.promote(e), r.ready;
1032
+ const t = { ready: this.fetchBlob(e, s), bytes: 0 };
1033
+ return this.entries.set(e, t), t.ready.then((i) => {
1034
+ this.entries.get(e) === t && (t.bytes = i.size, this.bytes += t.bytes, this.trim());
1035
+ }).catch(() => {
1036
+ this.entries.get(e) === t && this.entries.delete(e);
1037
+ }), t.ready;
1038
+ }
1039
+ }
1040
+ class Oe {
1041
+ constructor() {
1042
+ this.urls = /* @__PURE__ */ new Map(), this.disposed = !1, this.current = /* @__PURE__ */ new Set(), this.holds = /* @__PURE__ */ new Map(), this.cache = new et(32 * 1024 * 1024, 4, (e) => {
1043
+ const s = this.urls.get(e);
1044
+ s && URL.revokeObjectURL(s), this.urls.delete(e);
1045
+ });
1046
+ }
1047
+ key(e) {
1048
+ return new URL(e, document.baseURI).href;
1049
+ }
1050
+ sources(e) {
1051
+ return e.filter((s) => s && !s.startsWith("data:") && !s.startsWith("blob:")).map((s) => this.key(s));
1052
+ }
1053
+ updatePins() {
1054
+ this.cache.setPinnedSources([...this.current, ...this.holds.keys()]);
1055
+ }
1056
+ pin(e) {
1057
+ this.current = new Set(this.sources(e)), this.updatePins();
1058
+ }
1059
+ hold(e) {
1060
+ const s = new Set(this.sources(e));
1061
+ return s.forEach((r) => this.holds.set(r, (this.holds.get(r) ?? 0) + 1)), this.updatePins(), () => {
1062
+ s.forEach((r) => {
1063
+ const t = (this.holds.get(r) ?? 1) - 1;
1064
+ t ? this.holds.set(r, t) : this.holds.delete(r);
1065
+ }), this.updatePins();
1066
+ };
1067
+ }
1068
+ async preload(e, s = !1) {
1069
+ await Promise.all([...new Set(this.sources(e))].map((r) => this.cache.load(r, s)));
1070
+ }
1071
+ async url(e) {
1072
+ if (!e || e.startsWith("data:") || e.startsWith("blob:")) return e;
1073
+ const s = this.key(e), r = await this.cache.load(s, !0);
1074
+ if (this.disposed) throw new Error("Resource session was released");
1075
+ let t = this.urls.get(s);
1076
+ return t || (t = URL.createObjectURL(r), this.urls.set(s, t)), t;
1077
+ }
1078
+ async acquire(e) {
1079
+ const s = this.hold(e);
1080
+ try {
1081
+ return { urls: Object.fromEntries(await Promise.all([...new Set(e.filter(Boolean))].map(async (t) => [t, await this.url(t)]))), release: s };
1082
+ } catch (r) {
1083
+ throw s(), r;
1084
+ }
1085
+ }
1086
+ dispose() {
1087
+ this.disposed = !0, this.cache.clear();
1088
+ }
1089
+ }
1090
+ function ge(d) {
1091
+ const e = /* @__PURE__ */ new Set(), s = (r) => {
1092
+ if (!r || typeof r != "object") return;
1093
+ if (Array.isArray(r)) {
1094
+ r.forEach(s);
1095
+ return;
1096
+ }
1097
+ const t = r;
1098
+ typeof t.src == "string" && t.src && e.add(t.src), typeof t.sourcePath == "string" && /^(https?:|\/)/.test(t.sourcePath) && e.add(t.sourcePath), Object.values(t).forEach(s);
1099
+ };
1100
+ return s(d), [...e];
1101
+ }
1102
+ async function xe(d, e) {
1103
+ const s = JSON.parse(JSON.stringify(d)), r = new Map(await Promise.all(ge(s).map(async (i) => [i, await e.url(i)]))), t = (i) => {
1104
+ if (!i || typeof i != "object") return;
1105
+ if (Array.isArray(i)) {
1106
+ i.forEach(t);
1107
+ return;
1108
+ }
1109
+ const a = i;
1110
+ typeof a.src == "string" && (a.src = r.get(a.src) ?? a.src, a.originSrc && (a.originSrc = a.src)), typeof a.sourcePath == "string" && (a.sourcePath = r.get(a.sourcePath) ?? a.sourcePath), Object.values(a).forEach(t);
1111
+ };
1112
+ return t(s), s;
1113
+ }
1114
+ function tt(d, e, s) {
1115
+ const r = JSON.parse(JSON.stringify(d)), t = new Map(Object.entries(e.urls).map(([n, c]) => [c, n])), i = /* @__PURE__ */ new Map(), a = (n, c) => {
1116
+ if (!n || typeof n != "object") return;
1117
+ if (Array.isArray(n)) {
1118
+ n.forEach((m) => a(m, c));
1119
+ return;
1120
+ }
1121
+ const o = n;
1122
+ c(o), Object.values(o).forEach((m) => a(m, c));
1123
+ };
1124
+ return a(s, (n) => {
1125
+ n.id !== void 0 && i.set(String(n.id), n);
1126
+ }), a(r, (n) => {
1127
+ const c = n.id !== void 0 ? i.get(String(n.id)) : void 0;
1128
+ for (const o of ["src", "originSrc", "sourcePath"])
1129
+ typeof n[o] == "string" && (c && typeof c[o] == "string" ? n[o] = c[o] : n[o] = t.get(n[o]) ?? n[o]);
1130
+ }), r;
1131
+ }
1132
+ function Z(d) {
1133
+ return d.designKey || d.area?.name || d.name;
1134
+ }
1135
+ function Ye(d, e, s) {
1136
+ return s ?? d.render_areas.filter((r) => r.design).map((r) => e[Z(r)]?.color).find((r) => typeof r == "string");
1137
+ }
1138
+ function fe(d, e, s, r = !0) {
1139
+ if (!r) return d.render_areas;
1140
+ const t = Ye(d, e, s);
1141
+ return d.render_areas.filter((i) => {
1142
+ const a = e[Z(i)];
1143
+ return qe(i, t, !!(i.area && Array.isArray(a?.objects) && a.objects.length));
1144
+ });
1145
+ }
1146
+ function ke(d, e, s, r = !0) {
1147
+ return fe(d, e, s, r).flatMap((t) => {
1148
+ const i = e[Z(t)];
1149
+ return [t.image, ...t.design && t.area && Array.isArray(i?.objects) && i.objects.length ? [t.area.image, ...ge(i)] : []];
1150
+ }).filter(Boolean);
1151
+ }
1152
+ function rt(d) {
1153
+ return d.render_areas.find((e) => e.name === "bg")?.image || d.image || "";
1154
+ }
1155
+ function st(d, e) {
1156
+ const s = d.render_areas.find((t) => t.name === "bg") ?? d.render_areas.find((t) => !t.design), r = Math.max(s?.width ?? 0, s?.height ?? 0);
1157
+ return Math.max(1, Math.min(768, Math.round(r ? r * e : 600)));
1158
+ }
1159
+ function de(d) {
1160
+ if (typeof d != "string") {
1161
+ d.close();
1162
+ return;
1163
+ }
1164
+ d.startsWith("blob:") && URL.revokeObjectURL(d);
1165
+ }
1166
+ const Ct = Te(
1167
+ function({
1168
+ render: e,
1169
+ designData: s,
1170
+ enabled: r = !0,
1171
+ preloadSources: t,
1172
+ preloadDesignData: i,
1173
+ onAssetReady: a,
1174
+ backgroundColor: n,
1175
+ scale: c = 0.8,
1176
+ renderer: o = "pixel-map",
1177
+ className: m,
1178
+ style: f,
1179
+ alt: P = "Product preview",
1180
+ onLoadingChange: u,
1181
+ onRendered: Y,
1182
+ onError: M
1183
+ }, X) {
1184
+ const C = R(null), b = R(null), A = R(null), h = R(null), p = R(null), T = R(void 0), E = R(null), l = R(null), y = R(0), I = R(/* @__PURE__ */ new Map()), _ = R(null), v = R(0), S = R(!1), g = R(!1), L = R({ onLoadingChange: u, onRendered: Y, onError: M, onAssetReady: a });
1185
+ L.current = { onLoadingChange: u, onRendered: Y, onError: M, onAssetReady: a };
1186
+ const O = o === "pixel-map" && Re.isSupported(), J = R(O);
1187
+ J.current = O;
1188
+ const [le, se] = re(""), [k, he] = re(!1), [Xe, Ae] = re(""), K = N(() => (p.current ??= new Oe(), p.current), []), $ = N((w) => {
1189
+ he(w), L.current.onLoadingChange?.(w);
1190
+ }, []), ie = N(() => {
1191
+ if (A.current) return A.current;
1192
+ if (!C.current || !b.current) return null;
1193
+ const w = 800;
1194
+ return C.current.width = w, C.current.height = w, A.current = new Me(
1195
+ C.current,
1196
+ { width: w, height: w },
1197
+ b.current,
1198
+ 0.85
1199
+ ), A.current;
1200
+ }, []), pe = N(async () => {
1201
+ if (h.current) return h.current;
1202
+ if (O) h.current = new Re();
1203
+ else {
1204
+ const { StickerPlugin: w } = await import("./renderer/sticker.js");
1205
+ if (g.current || J.current !== O) return null;
1206
+ if (h.current) return h.current;
1207
+ h.current = new w({ width: 1, height: 1 }, !0, { subdiv: 8 });
1208
+ }
1209
+ return h.current;
1210
+ }, [O]), ne = N(() => {
1211
+ A.current?.destroy(), A.current = null, h.current?.destroy(), h.current = null, p.current?.dispose(), p.current = null, T.current = void 0, I.current.forEach(({ texture: w }) => de(w)), I.current.clear(), l.current && URL.revokeObjectURL(l.current), l.current = null, E.current = null;
1212
+ }, []), me = N(() => (_.current || (_.current = new je(
1213
+ () => $(!0),
1214
+ () => {
1215
+ g.current ? ne() : $(!1);
1216
+ }
1217
+ )), _.current), [ne, $]), ae = N(() => {
1218
+ const w = ++y.current, Q = v.current;
1219
+ me().enqueue(async () => {
1220
+ let D;
1221
+ try {
1222
+ if (g.current || w !== y.current) return;
1223
+ const B = ie();
1224
+ S.current && (h.current?.destroy(), h.current = null, S.current = !1);
1225
+ const V = await pe();
1226
+ if (!B || !V || g.current || w !== y.current) return;
1227
+ const q = K();
1228
+ D = q.hold(ke(e, s, n, O));
1229
+ const ve = fe(e, s, n, O), Ie = e.render_areas.filter((x) => !!x.design), ce = st(e, c), ye = Promise.all(ve.map(async (x) => [x.image, await q.url(x.image)]));
1230
+ ye.catch(() => {
1231
+ });
1232
+ for (const x of Ie) {
1233
+ const H = Z(x), F = s[H];
1234
+ if (!Array.isArray(F?.objects) || !F.objects.length || !x.area) {
1235
+ const ee = I.current.get(H);
1236
+ ee && (de(ee.texture), I.current.delete(H));
1237
+ continue;
1238
+ }
1239
+ const W = JSON.stringify([
1240
+ Q,
1241
+ ce,
1242
+ x.area.id,
1243
+ x.area.image,
1244
+ F
1245
+ ]);
1246
+ if (I.current.get(H)?.fingerprint === W) continue;
1247
+ const j = typeof F.color == "string" ? F.color : "", [Be, Ue] = await Promise.all([
1248
+ q.url(x.area.image),
1249
+ xe(F, q)
1250
+ ]);
1251
+ if (g.current || w !== y.current || (await B.switchView({ ...x.area, image: Be }, j, Ue), g.current || w !== y.current)) return;
1252
+ const Pe = typeof createImageBitmap == "function" ? await B.exportPreviewBitmap(ce, j) : await B.export(!1, j, {
1253
+ minOutputSize: ce,
1254
+ preferImageBitmap: !1,
1255
+ returnBlobUrl: !1,
1256
+ outputMultiplierCap: 1,
1257
+ quality: 0.72
1258
+ });
1259
+ if (Pe) {
1260
+ const ee = I.current.get(H);
1261
+ ee && de(ee.texture), I.current.set(H, { fingerprint: W, texture: Pe });
1262
+ }
1263
+ if (g.current || w !== y.current) return;
1264
+ }
1265
+ const Ee = new Map(await ye);
1266
+ if (g.current || w !== y.current) return;
1267
+ const be = Ye(e, s, n), Ce = ve.map((x) => {
1268
+ const H = s[Z(x)], F = x.design && Array.isArray(H?.objects) && H.objects.length ? I.current.get(Z(x))?.texture : void 0, W = x.texture_transform, j = typeof W?.blendMode == "number" ? W.blendMode : typeof W?.blendMode == "string" ? We[W.blendMode.toLowerCase()] : void 0;
1269
+ return {
1270
+ ...x,
1271
+ image: Ee.get(x.image) ?? x.image,
1272
+ texture: F,
1273
+ // PixelMapRenderer consumes the source blend-mode string; Pixi
1274
+ // still receives its enum value exactly as before.
1275
+ config: W ? O ? { ...W } : { ...W, blendMode: j } : void 0
1276
+ };
1277
+ }), oe = O ? await V.onOffscreenRender(Ce, {
1278
+ color: be,
1279
+ targetSize: ce,
1280
+ quality: 0.85,
1281
+ format: "image/png",
1282
+ cacheKey: e.id
1283
+ }) : await V.onOffscreenRender(Ce, {
1284
+ color: be,
1285
+ cleanCache: !1,
1286
+ scale: c,
1287
+ quality: 0.85
1288
+ });
1289
+ if (g.current || w !== y.current) return;
1290
+ if (!oe) throw new Error("Could not render the product preview");
1291
+ const ue = URL.createObjectURL(oe);
1292
+ l.current && URL.revokeObjectURL(l.current), E.current = oe, l.current = ue, se(ue), L.current.onRendered?.({ renderId: e.id, blob: oe, url: ue });
1293
+ } catch (B) {
1294
+ !g.current && w === y.current && L.current.onError?.(B instanceof Error ? B : new Error("Could not render the product preview"));
1295
+ } finally {
1296
+ D?.();
1297
+ }
1298
+ });
1299
+ }, [n, s, ie, me, pe, K, e, c, O]);
1300
+ te(() => (g.current = !1, () => {
1301
+ g.current = !0, y.current += 1, _.current?.clear(), _.current?.active || ne();
1302
+ }), [ne]), te(() => {
1303
+ h.current?.destroy(), h.current = null;
1304
+ }, [o, O]), te(() => {
1305
+ const w = K(), Q = i ?? s, D = ke(e, Q, n, O);
1306
+ w.pin(D);
1307
+ let B = !0;
1308
+ return w.preload(D, !0).then(async () => {
1309
+ if (B)
1310
+ for (const V of fe(e, Q, n, O).map((q) => q.image).filter(Boolean)) {
1311
+ const q = await w.url(V);
1312
+ if (!B) return;
1313
+ L.current.onAssetReady?.(V, q), V === rt(e) && Ae(q);
1314
+ }
1315
+ }).catch(() => {
1316
+ }).finally(() => {
1317
+ B && t?.length && T.current !== t && (T.current = t, w.preload(t).catch(() => {
1318
+ }));
1319
+ }), () => {
1320
+ B = !1;
1321
+ };
1322
+ }, [n, s, i, K, t, e, O]), te(() => (r ? ae() : $(!1), () => {
1323
+ y.current += 1, _.current?.clear();
1324
+ }), [r, ae, $]), Le(X, () => ({
1325
+ acquireResources: (w) => K().acquire(w),
1326
+ getPreviewBlob: async () => E.current,
1327
+ getPreviewUrl: async () => E.current ? URL.createObjectURL(E.current) : null,
1328
+ refresh: () => {
1329
+ v.current += 1, S.current = !0, r && ae();
1330
+ },
1331
+ updateText: async (w, Q) => {
1332
+ const D = ie();
1333
+ return D ? D.updateText(w, Q) : null;
1334
+ }
1335
+ }), [r, ie, K, ae]);
1336
+ const we = le || Xe;
1337
+ return /* @__PURE__ */ _e("div", { className: m, style: { position: "relative", ...f }, children: [
1338
+ we && /* @__PURE__ */ z("img", { src: we, alt: P, style: { display: "block", width: "100%", height: "100%", objectFit: "contain" } }),
1339
+ k && /* @__PURE__ */ z("div", { "aria-live": "polite", "aria-label": "Rendering product preview", style: { position: "absolute", inset: 0 } }),
1340
+ /* @__PURE__ */ z("div", { "aria-hidden": "true", style: { position: "absolute", width: 1, height: 1, overflow: "hidden", opacity: 0, pointerEvents: "none" }, children: /* @__PURE__ */ z("div", { ref: b, style: { width: 800, height: 800 }, children: /* @__PURE__ */ z("canvas", { ref: C, width: 800, height: 800 }) }) })
1341
+ ] });
1342
+ }
1343
+ );
1344
+ function Pt({
1345
+ viewArea: d,
1346
+ viewState: e,
1347
+ activeObjectId: s,
1348
+ onChange: r,
1349
+ acquireResources: t,
1350
+ onLoadingChange: i,
1351
+ className: a,
1352
+ style: n
1353
+ }) {
1354
+ const c = R(null), o = R(null), m = R(null), f = R(r), P = R(i), u = R(null), Y = R(e), [M, X] = re(!1), [C, b] = re("");
1355
+ f.current = r, P.current = i;
1356
+ const A = N(() => {
1357
+ const h = m.current;
1358
+ if (!h) return;
1359
+ const p = h.getRenderedObjects();
1360
+ f.current(u.current ? tt(p, u.current, Y.current) : p);
1361
+ }, []);
1362
+ return te(() => {
1363
+ let h = !1, p = null, T = !1, E = !1, l = null, y = null;
1364
+ const I = () => {
1365
+ T || (T = !0, p?.destroy(), m.current === p && (m.current = null), l?.release(), u.current === l && (u.current = null), y?.dispose());
1366
+ };
1367
+ return (async () => {
1368
+ if (P.current?.(!0), await new Promise((k) => requestAnimationFrame(() => k())), h) return;
1369
+ const v = c.current, S = o.current;
1370
+ if (!v || !S) return;
1371
+ const g = v.clientWidth || 400, L = v.clientHeight || 400, O = [d.image, ...ge(e)];
1372
+ if (t ? l = await t(O) : (y = new Oe(), l = await y.acquire(O)), h) return;
1373
+ u.current = l;
1374
+ const J = await xe(e, { url: async (k) => l.urls[k] ?? k });
1375
+ if (h) return;
1376
+ p = new Me(S, { width: g, height: L }, v, 0.85), m.current = p, $e(typeof window < "u" && window.innerWidth < 768 ? 16 : 8);
1377
+ const le = {
1378
+ ...J,
1379
+ objects: (Array.isArray(J.objects) ? J.objects : []).map(
1380
+ (k) => k?.selectable === !1 ? { ...k, selectable: !0 } : k
1381
+ )
1382
+ };
1383
+ if (await p.switchView({ ...d, image: l.urls[d.image] ?? d.image }, typeof e.color == "string" ? e.color : "", le), h) return;
1384
+ const se = new Set(
1385
+ (Array.isArray(e.objects) ? e.objects : []).filter((k) => k?.customConfig?.moveable === !0).map((k) => String(k.id ?? ""))
1386
+ );
1387
+ if (p.canvas.forEachObject((k) => {
1388
+ se.has(String(k.id ?? "")) || k.set({ selectable: !1, evented: !1 });
1389
+ }), s) {
1390
+ const k = p.canvas.getObjects().find((he) => String(he.id ?? "") === String(s));
1391
+ k && se.has(String(s)) && p.canvas.setActiveObject(k);
1392
+ }
1393
+ p.canvas.on("object:modified", () => A()), p.canvas.requestRenderAll(), await new Promise((k) => requestAnimationFrame(() => k())), h || X(!0);
1394
+ })().catch((v) => {
1395
+ h || b(v instanceof Error ? v.message : "Could not load the editor.");
1396
+ }).finally(() => {
1397
+ E = !0, h && I(), P.current?.(!1);
1398
+ }), () => {
1399
+ h = !0, X(!1), E && I();
1400
+ };
1401
+ }, []), /* @__PURE__ */ _e("div", { className: a, style: { position: "relative", width: "100%", height: "100%", ...n }, children: [
1402
+ /* @__PURE__ */ z("div", { ref: c, style: { position: "absolute", inset: 0, overflow: "hidden" }, children: /* @__PURE__ */ z("canvas", { ref: o }) }),
1403
+ !M && /* @__PURE__ */ z("div", { style: { position: "absolute", inset: 0, background: "#f2f2f2" }, "aria-label": C ? "Editor failed to load" : "Loading editor", children: C && /* @__PURE__ */ z("span", { role: "alert", children: C }) })
1404
+ ] });
1405
+ }
1406
+ export {
1407
+ Pt as P,
1408
+ Ct as a
1409
+ };