@snowcone-app/sdk 0.20.2 → 0.23.0
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/CHANGELOG.md +24 -0
- package/dist/{chunk-IL3FXE2X.js → chunk-FPR6ABXM.js} +2 -0
- package/dist/{chunk-MKLLTNKJ.js → chunk-P3ZYTA5F.js} +91 -6
- package/dist/index.cjs +93 -6
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +2 -2
- package/dist/react.cjs +424 -9
- package/dist/react.d.cts +145 -3
- package/dist/react.d.ts +145 -3
- package/dist/react.js +339 -4
- package/dist/server.cjs +2 -0
- package/dist/server.js +1 -1
- package/dist/{websocket-BG81_dZv.d.cts → websocket-DjAOhBaN.d.cts} +91 -1
- package/dist/{websocket-BG81_dZv.d.ts → websocket-DjAOhBaN.d.ts} +91 -1
- package/package.json +2 -2
package/dist/react.cjs
CHANGED
|
@@ -20,8 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/react.ts
|
|
21
21
|
var react_exports = {};
|
|
22
22
|
__export(react_exports, {
|
|
23
|
+
MockupStage: () => MockupStage,
|
|
23
24
|
ReactAdapter: () => ReactAdapter,
|
|
24
25
|
createReactComponent: () => createReactComponent,
|
|
26
|
+
useMockupStage: () => useMockupStage,
|
|
25
27
|
useReactAdapter: () => useFrameworkAdapter,
|
|
26
28
|
useRealtimeMockup: () => useRealtimeMockup
|
|
27
29
|
});
|
|
@@ -34,6 +36,13 @@ var import_react = require("react");
|
|
|
34
36
|
var REALTIME_WS_URL = "wss://cdn.snowcone.app/realtime";
|
|
35
37
|
|
|
36
38
|
// src/realtime/websocket.ts
|
|
39
|
+
function pruneUndefined(obj) {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
42
|
+
if (v !== void 0) out[k] = v;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
37
46
|
function randHex(len) {
|
|
38
47
|
const arr = new Uint8Array(len);
|
|
39
48
|
globalThis.crypto.getRandomValues(arr);
|
|
@@ -52,6 +61,9 @@ var RealtimeMockupService = class {
|
|
|
52
61
|
config = null;
|
|
53
62
|
configSent = false;
|
|
54
63
|
sessionId = null;
|
|
64
|
+
// Render node that answered the handshake (`connected.box`), for labs and
|
|
65
|
+
// support: a realtime session has no response headers to read it from.
|
|
66
|
+
box = null;
|
|
55
67
|
isConfigured = false;
|
|
56
68
|
mockupResults = [];
|
|
57
69
|
status = "Disconnected";
|
|
@@ -97,6 +109,13 @@ var RealtimeMockupService = class {
|
|
|
97
109
|
// Session-grant auth: when set, connect() fetches a short-lived token, opens
|
|
98
110
|
// the WS with `?token=`, and renews ~15s before expiry via a `renew` message.
|
|
99
111
|
tokenProvider;
|
|
112
|
+
// Binary-delivered frames waiting for their mockup_rendered metadata,
|
|
113
|
+
// keyed `${requestId ?? ''}:${mockupId}`. The server sends the binary
|
|
114
|
+
// frame before the JSON result on the same socket, so ordering guarantees
|
|
115
|
+
// the entry exists when the result arrives.
|
|
116
|
+
pendingFrames = /* @__PURE__ */ new Map();
|
|
117
|
+
// Last object URL handed out per mockup — revoked when replaced.
|
|
118
|
+
liveObjectUrls = /* @__PURE__ */ new Map();
|
|
100
119
|
renewTimer = null;
|
|
101
120
|
isConnecting = false;
|
|
102
121
|
connectAttempt = 0;
|
|
@@ -111,6 +130,7 @@ var RealtimeMockupService = class {
|
|
|
111
130
|
return {
|
|
112
131
|
isConnected: this.ws?.readyState === WebSocket.OPEN,
|
|
113
132
|
sessionId: this.sessionId,
|
|
133
|
+
box: this.box,
|
|
114
134
|
isConfigured: this.isConfigured,
|
|
115
135
|
mockupResults: [...this.mockupResults],
|
|
116
136
|
status: this.status,
|
|
@@ -151,6 +171,7 @@ var RealtimeMockupService = class {
|
|
|
151
171
|
const url = token ? `${this.wsUrl}${this.wsUrl.includes("?") ? "&" : "?"}token=${encodeURIComponent(token)}` : this.wsUrl;
|
|
152
172
|
this.addLog(`Connecting to ${this.wsUrl}...`);
|
|
153
173
|
this.ws = new WebSocket(url);
|
|
174
|
+
this.ws.binaryType = "arraybuffer";
|
|
154
175
|
this.ws.onopen = () => {
|
|
155
176
|
console.log(`[WS] connection OPENED to ${this.wsUrl}`);
|
|
156
177
|
this.addLog("WebSocket connection opened");
|
|
@@ -158,6 +179,10 @@ var RealtimeMockupService = class {
|
|
|
158
179
|
this.isConnecting = false;
|
|
159
180
|
};
|
|
160
181
|
this.ws.onmessage = (event) => {
|
|
182
|
+
if (event.data instanceof ArrayBuffer) {
|
|
183
|
+
this.handleBinaryFrame(event.data);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
161
186
|
try {
|
|
162
187
|
const data = JSON.parse(event.data);
|
|
163
188
|
this.addLog(JSON.stringify(data, null, 2), "received");
|
|
@@ -171,6 +196,7 @@ var RealtimeMockupService = class {
|
|
|
171
196
|
this.addLog(`WebSocket connection closed (code: ${event.code})`);
|
|
172
197
|
this.status = "Disconnected";
|
|
173
198
|
this.sessionId = null;
|
|
199
|
+
this.box = null;
|
|
174
200
|
this.isConfigured = false;
|
|
175
201
|
this.configSent = false;
|
|
176
202
|
this.clearRenew();
|
|
@@ -210,10 +236,11 @@ var RealtimeMockupService = class {
|
|
|
210
236
|
switch (data.type) {
|
|
211
237
|
case "connected":
|
|
212
238
|
this.sessionId = data.sessionId || null;
|
|
239
|
+
this.box = data.box ?? null;
|
|
213
240
|
this.status = `Connected (Session: ${data.sessionId || "N/A"})`;
|
|
214
|
-
this.addLog(`\u2705 Session established: ${data.sessionId}`);
|
|
241
|
+
this.addLog(`\u2705 Session established: ${data.sessionId}${this.box ? ` on ${this.box}` : ""}`);
|
|
215
242
|
if (data.sessionId) {
|
|
216
|
-
this.callbacks.onConnected?.(data.sessionId);
|
|
243
|
+
this.callbacks.onConnected?.(data.sessionId, { box: this.box ?? void 0 });
|
|
217
244
|
}
|
|
218
245
|
if (this.config && !this.configSent) {
|
|
219
246
|
this.addLog("\u{1F4E4} Auto-sending cached config after connection...");
|
|
@@ -267,6 +294,7 @@ var RealtimeMockupService = class {
|
|
|
267
294
|
this.addLog("\u{1F3A8} Mockup rendering has started...");
|
|
268
295
|
break;
|
|
269
296
|
case "mockup_rendered": {
|
|
297
|
+
this.applyBinaryFrame(data);
|
|
270
298
|
const sentAt = data.requestId ? this.requestSendTimes.get(data.requestId) : void 0;
|
|
271
299
|
const clientRoundTripMs = sentAt !== void 0 ? Date.now() - sentAt : void 0;
|
|
272
300
|
console.log(`[WS] mockup_rendered received: mockupId=${data.mockupId} hasImageUrl=${!!data.imageUrl} v=${data.requestVersion} req=${data.requestId ?? "?"} placement="${data.placement}" renderMs=${data.renderMs} blobToRenderMs=${data.blobToRenderMs} clientRoundTripMs=${clientRoundTripMs ?? "?"} at ${Date.now()}`);
|
|
@@ -294,7 +322,12 @@ var RealtimeMockupService = class {
|
|
|
294
322
|
blobToRenderMs: data.blobToRenderMs,
|
|
295
323
|
costUsd: data.costUsd,
|
|
296
324
|
renderCache: data.renderCache,
|
|
297
|
-
|
|
325
|
+
cropRect: data.cropRect,
|
|
326
|
+
serverTiming: data.serverTiming,
|
|
327
|
+
canvasRenderTiming: data.canvasRenderTiming,
|
|
328
|
+
rtTiming: data.rtTiming,
|
|
329
|
+
clientRoundTripMs,
|
|
330
|
+
imageDelivery: data.imageDelivery === "ws" ? "ws" : "url"
|
|
298
331
|
};
|
|
299
332
|
const existingIndex = this.mockupResults.findIndex((m) => m.mockupId === data.mockupId);
|
|
300
333
|
if (existingIndex >= 0) {
|
|
@@ -329,7 +362,18 @@ var RealtimeMockupService = class {
|
|
|
329
362
|
}
|
|
330
363
|
return true;
|
|
331
364
|
});
|
|
332
|
-
this.mockupResults = freshMockups
|
|
365
|
+
this.mockupResults = freshMockups.map((mockup) => {
|
|
366
|
+
const existing = this.mockupResults.find(
|
|
367
|
+
(m) => m.mockupId === mockup.mockupId && (mockup.requestId === void 0 || m.requestId === mockup.requestId)
|
|
368
|
+
);
|
|
369
|
+
if (!existing) return mockup;
|
|
370
|
+
const merged = { ...existing, ...pruneUndefined(mockup) };
|
|
371
|
+
if (existing.imageUrl.startsWith("blob:")) {
|
|
372
|
+
merged.imageUrl = existing.imageUrl;
|
|
373
|
+
merged.renderUrl = existing.renderUrl;
|
|
374
|
+
}
|
|
375
|
+
return merged;
|
|
376
|
+
});
|
|
333
377
|
this.lastError = null;
|
|
334
378
|
this.renderError = null;
|
|
335
379
|
this.addLog(`\u{1F389} All mockups rendered: ${freshMockups.length} fresh (${data.mockups.length - freshMockups.length} stale filtered)`);
|
|
@@ -342,7 +386,8 @@ var RealtimeMockupService = class {
|
|
|
342
386
|
const errorMessage = data.message || "Unknown error occurred";
|
|
343
387
|
const renderError = {
|
|
344
388
|
code: data.code || "render_error",
|
|
345
|
-
message: errorMessage
|
|
389
|
+
message: errorMessage,
|
|
390
|
+
...typeof data.mockupId === "string" ? { mockupId: data.mockupId } : {}
|
|
346
391
|
};
|
|
347
392
|
this.lastError = errorMessage;
|
|
348
393
|
this.renderError = renderError;
|
|
@@ -365,6 +410,35 @@ var RealtimeMockupService = class {
|
|
|
365
410
|
this.ws = null;
|
|
366
411
|
}
|
|
367
412
|
}
|
|
413
|
+
handleBinaryFrame(buf) {
|
|
414
|
+
try {
|
|
415
|
+
const view = new DataView(buf);
|
|
416
|
+
const headerLen = view.getUint32(0);
|
|
417
|
+
const header = JSON.parse(new TextDecoder().decode(new Uint8Array(buf, 4, headerLen)));
|
|
418
|
+
if (header.type !== "mockup_frame") return;
|
|
419
|
+
const bytes = new Uint8Array(buf, 4 + headerLen);
|
|
420
|
+
const objectUrl = URL.createObjectURL(new Blob([bytes], { type: header.contentType || "image/avif" }));
|
|
421
|
+
const key = `${header.requestId ?? ""}:${header.mockupId}`;
|
|
422
|
+
const prev = this.pendingFrames.get(key);
|
|
423
|
+
if (prev) URL.revokeObjectURL(prev.objectUrl);
|
|
424
|
+
this.pendingFrames.set(key, { objectUrl, bytes: bytes.byteLength });
|
|
425
|
+
this.addLog(`\u{1F4E6} mockup_frame received over ws: ${header.mockupId} (${bytes.byteLength}B)`);
|
|
426
|
+
} catch (e) {
|
|
427
|
+
this.addLog(`\u26A0\uFE0F failed to parse binary frame: ${e}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** Swap a ws-delivered frame in for the imageUrl on a render result. */
|
|
431
|
+
applyBinaryFrame(data) {
|
|
432
|
+
if (data.imageDelivery !== "ws" || !data.mockupId) return;
|
|
433
|
+
const key = `${data.requestId ?? ""}:${data.mockupId}`;
|
|
434
|
+
const frame = this.pendingFrames.get(key);
|
|
435
|
+
if (!frame) return;
|
|
436
|
+
this.pendingFrames.delete(key);
|
|
437
|
+
const prevLive = this.liveObjectUrls.get(data.mockupId);
|
|
438
|
+
if (prevLive) URL.revokeObjectURL(prevLive);
|
|
439
|
+
this.liveObjectUrls.set(data.mockupId, frame.objectUrl);
|
|
440
|
+
data.imageUrl = frame.objectUrl;
|
|
441
|
+
}
|
|
368
442
|
sendConfig(config) {
|
|
369
443
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
370
444
|
this.addLog("WebSocket not connected, caching config");
|
|
@@ -411,7 +485,11 @@ var RealtimeMockupService = class {
|
|
|
411
485
|
this.configSent = true;
|
|
412
486
|
const message = {
|
|
413
487
|
type: "config",
|
|
414
|
-
|
|
488
|
+
// binaryFrames: ask the server to push rendered image bytes down this
|
|
489
|
+
// socket (mockup_frame binary messages) instead of making us fetch the
|
|
490
|
+
// imageUrl — saves a network round trip per render. Servers that don't
|
|
491
|
+
// know the flag ignore it and we fall back to the URL.
|
|
492
|
+
config: { ...config, binaryFrames: true }
|
|
415
493
|
};
|
|
416
494
|
const messageStr = JSON.stringify(message);
|
|
417
495
|
this.addLog(`\u{1F4E4} Sending config: ${JSON.stringify(message, null, 2)}`, "sent");
|
|
@@ -705,6 +783,15 @@ ${versionToSend}
|
|
|
705
783
|
hasPendingBlobs() {
|
|
706
784
|
return Object.keys(this.throttleTimeouts).length > 0;
|
|
707
785
|
}
|
|
786
|
+
/**
|
|
787
|
+
* Version stamped on the most recent canvas_state send (0 before any).
|
|
788
|
+
* Every frame answers with `requestVersion`, so a caller can tell which
|
|
789
|
+
* send a frame belongs to — the pipeline-depth send policy counts a send
|
|
790
|
+
* as landed when every mockup of its version has arrived.
|
|
791
|
+
*/
|
|
792
|
+
getLastSentVersion() {
|
|
793
|
+
return this.lastSentVersion;
|
|
794
|
+
}
|
|
708
795
|
sendColorBlob(placement, hexColor) {
|
|
709
796
|
this.colors.set(placement, hexColor);
|
|
710
797
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN || !this.isConfigured) {
|
|
@@ -810,6 +897,7 @@ function useRealtimeMockup(options = {}) {
|
|
|
810
897
|
const [state, setState] = (0, import_react.useState)({
|
|
811
898
|
isConnected: false,
|
|
812
899
|
sessionId: null,
|
|
900
|
+
box: null,
|
|
813
901
|
isConfigured: false,
|
|
814
902
|
mockupResults: [],
|
|
815
903
|
status: "Disconnected",
|
|
@@ -821,9 +909,9 @@ function useRealtimeMockup(options = {}) {
|
|
|
821
909
|
const service = new RealtimeMockupService(wsUrl);
|
|
822
910
|
serviceRef.current = service;
|
|
823
911
|
service.setCallbacks({
|
|
824
|
-
onConnected: (sessionId) => {
|
|
912
|
+
onConnected: (sessionId, info) => {
|
|
825
913
|
setState(service.getState());
|
|
826
|
-
options.onConnected?.(sessionId);
|
|
914
|
+
options.onConnected?.(sessionId, info);
|
|
827
915
|
},
|
|
828
916
|
onDisconnected: () => {
|
|
829
917
|
setState(service.getState());
|
|
@@ -913,6 +1001,9 @@ function useRealtimeMockup(options = {}) {
|
|
|
913
1001
|
const hasPendingBlobs = (0, import_react.useCallback)(() => {
|
|
914
1002
|
return serviceRef.current?.hasPendingBlobs() || false;
|
|
915
1003
|
}, []);
|
|
1004
|
+
const getLastSentVersion = (0, import_react.useCallback)(() => {
|
|
1005
|
+
return serviceRef.current?.getLastSentVersion() ?? 0;
|
|
1006
|
+
}, []);
|
|
916
1007
|
return {
|
|
917
1008
|
...state,
|
|
918
1009
|
connect,
|
|
@@ -930,9 +1021,331 @@ function useRealtimeMockup(options = {}) {
|
|
|
930
1021
|
clearLogs,
|
|
931
1022
|
clearMockups,
|
|
932
1023
|
flushPendingBlobs,
|
|
933
|
-
hasPendingBlobs
|
|
1024
|
+
hasPendingBlobs,
|
|
1025
|
+
getLastSentVersion
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
// src/realtime/stage.ts
|
|
1030
|
+
var import_react2 = require("react");
|
|
1031
|
+
var FULL_RECT = { x: 0, y: 0, w: 1, h: 1 };
|
|
1032
|
+
function useMockupStage(options) {
|
|
1033
|
+
const {
|
|
1034
|
+
shop,
|
|
1035
|
+
product,
|
|
1036
|
+
mockup,
|
|
1037
|
+
variant,
|
|
1038
|
+
placements,
|
|
1039
|
+
ar,
|
|
1040
|
+
bg,
|
|
1041
|
+
getToken,
|
|
1042
|
+
wsUrl,
|
|
1043
|
+
delivery = "cropped",
|
|
1044
|
+
debugKey
|
|
1045
|
+
} = options;
|
|
1046
|
+
const mockupIds = (0, import_react2.useMemo)(
|
|
1047
|
+
() => [mockup, ...options.extraMockups ?? []],
|
|
1048
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1049
|
+
[mockup, JSON.stringify(options.extraMockups ?? [])]
|
|
1050
|
+
);
|
|
1051
|
+
const [stages, setStages] = (0, import_react2.useState)({});
|
|
1052
|
+
const [isRendering, setIsRendering] = (0, import_react2.useState)(false);
|
|
1053
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
1054
|
+
const [lastResult, setLastResult] = (0, import_react2.useState)(null);
|
|
1055
|
+
const backgroundsRef = (0, import_react2.useRef)({});
|
|
1056
|
+
const croppedActiveRef = (0, import_react2.useRef)(false);
|
|
1057
|
+
const pendingEditsRef = (0, import_react2.useRef)(0);
|
|
1058
|
+
const optionsRef = (0, import_react2.useRef)(options);
|
|
1059
|
+
optionsRef.current = options;
|
|
1060
|
+
const mockupIdsRef = (0, import_react2.useRef)(mockupIds);
|
|
1061
|
+
mockupIdsRef.current = mockupIds;
|
|
1062
|
+
const onFrameRef = (0, import_react2.useRef)(options.onFrame);
|
|
1063
|
+
onFrameRef.current = options.onFrame;
|
|
1064
|
+
const onErrorRef = (0, import_react2.useRef)(options.onError);
|
|
1065
|
+
onErrorRef.current = options.onError;
|
|
1066
|
+
const {
|
|
1067
|
+
isConnected,
|
|
1068
|
+
sessionId,
|
|
1069
|
+
isConfigured,
|
|
1070
|
+
connect,
|
|
1071
|
+
sendConfig,
|
|
1072
|
+
sendCanvasState,
|
|
1073
|
+
sendCanvasBlob,
|
|
1074
|
+
sendInitialEmptyCanvases
|
|
1075
|
+
} = useRealtimeMockup({
|
|
1076
|
+
wsUrl,
|
|
1077
|
+
getToken,
|
|
1078
|
+
onMockupRendered: (result) => {
|
|
1079
|
+
if (!result.imageUrl || !result.mockupId) return;
|
|
1080
|
+
const id = result.mockupId;
|
|
1081
|
+
setLastResult(result);
|
|
1082
|
+
pendingEditsRef.current = Math.max(0, pendingEditsRef.current - 1);
|
|
1083
|
+
if (pendingEditsRef.current === 0) setIsRendering(false);
|
|
1084
|
+
onFrameRef.current?.(result);
|
|
1085
|
+
if (!backgroundsRef.current[id]) {
|
|
1086
|
+
backgroundsRef.current[id] = result.imageUrl;
|
|
1087
|
+
setStages((prev) => ({
|
|
1088
|
+
...prev,
|
|
1089
|
+
[id]: { background: result.imageUrl, frame: { url: result.imageUrl, rect: FULL_RECT } }
|
|
1090
|
+
}));
|
|
1091
|
+
const allSeeded = mockupIdsRef.current.every((m) => backgroundsRef.current[m]);
|
|
1092
|
+
if (allSeeded && !croppedActiveRef.current && optionsRef.current.delivery !== "full-frame") {
|
|
1093
|
+
croppedActiveRef.current = true;
|
|
1094
|
+
sendStageConfig(true);
|
|
1095
|
+
}
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
setStages((prev) => ({
|
|
1099
|
+
...prev,
|
|
1100
|
+
[id]: {
|
|
1101
|
+
background: backgroundsRef.current[id],
|
|
1102
|
+
frame: { url: result.imageUrl, rect: result.cropRect ?? FULL_RECT }
|
|
1103
|
+
}
|
|
1104
|
+
}));
|
|
1105
|
+
},
|
|
1106
|
+
onError: (_msg, detail) => {
|
|
1107
|
+
setError(detail);
|
|
1108
|
+
setIsRendering(false);
|
|
1109
|
+
onErrorRef.current?.(detail);
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
const sendConfigRef = (0, import_react2.useRef)(sendConfig);
|
|
1113
|
+
sendConfigRef.current = sendConfig;
|
|
1114
|
+
const sendStageConfig = (0, import_react2.useCallback)(
|
|
1115
|
+
(withCrop) => {
|
|
1116
|
+
const o = optionsRef.current;
|
|
1117
|
+
const width = typeof o.width === "function" ? o.width() : o.width ?? 1400;
|
|
1118
|
+
const config = {
|
|
1119
|
+
productId: o.product,
|
|
1120
|
+
mockupIds: mockupIdsRef.current,
|
|
1121
|
+
variantId: o.variant ?? "",
|
|
1122
|
+
shop: o.shop,
|
|
1123
|
+
width,
|
|
1124
|
+
...o.ar && { ar: o.ar },
|
|
1125
|
+
...o.bg && { bg: o.bg },
|
|
1126
|
+
...withCrop && { overlayCrop: "auto" },
|
|
1127
|
+
...o.debugKey && { debugKey: o.debugKey }
|
|
1128
|
+
};
|
|
1129
|
+
sendConfigRef.current(config);
|
|
1130
|
+
},
|
|
1131
|
+
[]
|
|
1132
|
+
);
|
|
1133
|
+
(0, import_react2.useEffect)(() => {
|
|
1134
|
+
connect();
|
|
1135
|
+
}, [connect]);
|
|
1136
|
+
(0, import_react2.useEffect)(() => {
|
|
1137
|
+
if (isConnected && sessionId) {
|
|
1138
|
+
sendStageConfig(croppedActiveRef.current);
|
|
1139
|
+
}
|
|
1140
|
+
}, [isConnected, sessionId, shop, product, mockupIds, variant, ar, bg, sendStageConfig]);
|
|
1141
|
+
const initialSentRef = (0, import_react2.useRef)(false);
|
|
1142
|
+
(0, import_react2.useEffect)(() => {
|
|
1143
|
+
const placementsToSeed = optionsRef.current.initialEmptyPlacements;
|
|
1144
|
+
if (isConfigured && placementsToSeed?.length && !initialSentRef.current) {
|
|
1145
|
+
initialSentRef.current = true;
|
|
1146
|
+
void sendInitialEmptyCanvases(placementsToSeed);
|
|
1147
|
+
}
|
|
1148
|
+
}, [isConfigured, sendInitialEmptyCanvases]);
|
|
1149
|
+
const setDesign = (0, import_react2.useCallback)(
|
|
1150
|
+
(placement, state) => {
|
|
1151
|
+
pendingEditsRef.current += 1;
|
|
1152
|
+
setIsRendering(true);
|
|
1153
|
+
setError(null);
|
|
1154
|
+
sendCanvasState(placement, state, 1, 0);
|
|
1155
|
+
},
|
|
1156
|
+
[sendCanvasState]
|
|
1157
|
+
);
|
|
1158
|
+
const setArtwork = (0, import_react2.useCallback)(
|
|
1159
|
+
(placement, blob) => {
|
|
1160
|
+
pendingEditsRef.current += 1;
|
|
1161
|
+
setIsRendering(true);
|
|
1162
|
+
setError(null);
|
|
1163
|
+
sendCanvasBlob(placement, blob, 1, 0);
|
|
1164
|
+
},
|
|
1165
|
+
[sendCanvasBlob]
|
|
1166
|
+
);
|
|
1167
|
+
const refreeze = (0, import_react2.useCallback)(() => {
|
|
1168
|
+
backgroundsRef.current = {};
|
|
1169
|
+
croppedActiveRef.current = false;
|
|
1170
|
+
setStages({});
|
|
1171
|
+
sendStageConfig(false);
|
|
1172
|
+
}, [sendStageConfig]);
|
|
1173
|
+
const primary = stages[mockup] ?? { background: null, frame: null };
|
|
1174
|
+
const status = error ? "error" : !isConnected ? "connecting" : !isConfigured ? "configuring" : !primary.background ? "first-frame" : "live";
|
|
1175
|
+
void placements;
|
|
1176
|
+
return {
|
|
1177
|
+
status,
|
|
1178
|
+
background: primary.background,
|
|
1179
|
+
frame: primary.frame,
|
|
1180
|
+
stages,
|
|
1181
|
+
isRendering,
|
|
1182
|
+
error,
|
|
1183
|
+
lastResult,
|
|
1184
|
+
setDesign,
|
|
1185
|
+
setArtwork,
|
|
1186
|
+
refreeze
|
|
934
1187
|
};
|
|
935
1188
|
}
|
|
1189
|
+
function MockupStage(props) {
|
|
1190
|
+
const {
|
|
1191
|
+
stage,
|
|
1192
|
+
mockupId,
|
|
1193
|
+
feather = 12,
|
|
1194
|
+
aspect = 16 / 9,
|
|
1195
|
+
className,
|
|
1196
|
+
style,
|
|
1197
|
+
placeholder,
|
|
1198
|
+
renderingIndicator
|
|
1199
|
+
} = props;
|
|
1200
|
+
const layers = mockupId ? stage.stages[mockupId] ?? { background: null, frame: null } : { background: stage.background, frame: stage.frame };
|
|
1201
|
+
const [displayed, setDisplayed] = (0, import_react2.useState)(null);
|
|
1202
|
+
const [incoming, setIncoming] = (0, import_react2.useState)(null);
|
|
1203
|
+
(0, import_react2.useEffect)(() => {
|
|
1204
|
+
if (!layers.frame) return;
|
|
1205
|
+
if (!displayed) {
|
|
1206
|
+
setDisplayed(layers.frame);
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
if (layers.frame.url !== displayed.url) setIncoming(layers.frame);
|
|
1210
|
+
}, [layers.frame]);
|
|
1211
|
+
const handleIncomingLoad = (0, import_react2.useCallback)(() => {
|
|
1212
|
+
setIncoming((inc) => {
|
|
1213
|
+
if (inc) setDisplayed(inc);
|
|
1214
|
+
return null;
|
|
1215
|
+
});
|
|
1216
|
+
}, []);
|
|
1217
|
+
const frameStyle = (0, import_react2.useCallback)(
|
|
1218
|
+
(f) => {
|
|
1219
|
+
const full = f.rect.x === 0 && f.rect.y === 0 && f.rect.w === 1 && f.rect.h === 1;
|
|
1220
|
+
const base = {
|
|
1221
|
+
position: "absolute",
|
|
1222
|
+
left: `${f.rect.x * 100}%`,
|
|
1223
|
+
top: `${f.rect.y * 100}%`,
|
|
1224
|
+
width: `${f.rect.w * 100}%`,
|
|
1225
|
+
height: `${f.rect.h * 100}%`
|
|
1226
|
+
};
|
|
1227
|
+
if (!full && feather > 0) {
|
|
1228
|
+
const m = `linear-gradient(to right, transparent 0, black ${feather}px, black calc(100% - ${feather}px), transparent 100%), linear-gradient(to bottom, transparent 0, black ${feather}px, black calc(100% - ${feather}px), transparent 100%)`;
|
|
1229
|
+
base.WebkitMaskImage = m;
|
|
1230
|
+
base.maskImage = m;
|
|
1231
|
+
base.WebkitMaskComposite = "source-in";
|
|
1232
|
+
base.maskComposite = "intersect";
|
|
1233
|
+
}
|
|
1234
|
+
return base;
|
|
1235
|
+
},
|
|
1236
|
+
[feather]
|
|
1237
|
+
);
|
|
1238
|
+
const containerStyle = (0, import_react2.useMemo)(
|
|
1239
|
+
() => ({
|
|
1240
|
+
position: "relative",
|
|
1241
|
+
aspectRatio: String(aspect),
|
|
1242
|
+
overflow: "hidden",
|
|
1243
|
+
...style
|
|
1244
|
+
}),
|
|
1245
|
+
[aspect, style]
|
|
1246
|
+
);
|
|
1247
|
+
const children = [];
|
|
1248
|
+
if (layers.background) {
|
|
1249
|
+
children.push(
|
|
1250
|
+
(0, import_react2.createElement)("img", {
|
|
1251
|
+
key: "bg",
|
|
1252
|
+
src: layers.background,
|
|
1253
|
+
alt: "",
|
|
1254
|
+
"data-sc-part": "background",
|
|
1255
|
+
style: { position: "absolute", inset: 0, width: "100%", height: "100%" }
|
|
1256
|
+
})
|
|
1257
|
+
);
|
|
1258
|
+
}
|
|
1259
|
+
if (displayed) {
|
|
1260
|
+
children.push(
|
|
1261
|
+
(0, import_react2.createElement)("img", {
|
|
1262
|
+
key: `frame-${displayed.url}`,
|
|
1263
|
+
src: displayed.url,
|
|
1264
|
+
alt: "",
|
|
1265
|
+
"data-sc-part": "frame",
|
|
1266
|
+
style: frameStyle(displayed)
|
|
1267
|
+
})
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
if (incoming) {
|
|
1271
|
+
children.push(
|
|
1272
|
+
(0, import_react2.createElement)("img", {
|
|
1273
|
+
key: `incoming-${incoming.url}`,
|
|
1274
|
+
src: incoming.url,
|
|
1275
|
+
alt: "",
|
|
1276
|
+
"data-sc-part": "frame-incoming",
|
|
1277
|
+
onLoad: handleIncomingLoad,
|
|
1278
|
+
style: { ...frameStyle(incoming), opacity: 0.999 }
|
|
1279
|
+
})
|
|
1280
|
+
);
|
|
1281
|
+
}
|
|
1282
|
+
if (!layers.background) {
|
|
1283
|
+
children.push(
|
|
1284
|
+
(0, import_react2.createElement)(
|
|
1285
|
+
"div",
|
|
1286
|
+
{
|
|
1287
|
+
key: "placeholder",
|
|
1288
|
+
"data-sc-part": "placeholder",
|
|
1289
|
+
style: {
|
|
1290
|
+
position: "absolute",
|
|
1291
|
+
inset: 0,
|
|
1292
|
+
display: "flex",
|
|
1293
|
+
alignItems: "center",
|
|
1294
|
+
justifyContent: "center"
|
|
1295
|
+
}
|
|
1296
|
+
},
|
|
1297
|
+
placeholder !== void 0 ? placeholder : (0, import_react2.createElement)("div", {
|
|
1298
|
+
style: {
|
|
1299
|
+
width: "38%",
|
|
1300
|
+
height: "55%",
|
|
1301
|
+
borderRadius: 12,
|
|
1302
|
+
background: "rgba(127,127,127,0.15)",
|
|
1303
|
+
animation: "sc-stage-pulse 1.2s ease-in-out infinite"
|
|
1304
|
+
}
|
|
1305
|
+
})
|
|
1306
|
+
)
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
if (stage.isRendering && layers.background) {
|
|
1310
|
+
children.push(
|
|
1311
|
+
(0, import_react2.createElement)(
|
|
1312
|
+
"div",
|
|
1313
|
+
{
|
|
1314
|
+
key: "rendering",
|
|
1315
|
+
"data-sc-part": "rendering-indicator",
|
|
1316
|
+
style: { position: "absolute", inset: 0, pointerEvents: "none" }
|
|
1317
|
+
},
|
|
1318
|
+
renderingIndicator !== void 0 ? renderingIndicator : (0, import_react2.createElement)("div", {
|
|
1319
|
+
style: {
|
|
1320
|
+
position: "absolute",
|
|
1321
|
+
top: 8,
|
|
1322
|
+
right: 8,
|
|
1323
|
+
width: 8,
|
|
1324
|
+
height: 8,
|
|
1325
|
+
borderRadius: "50%",
|
|
1326
|
+
background: "rgba(127,127,127,0.6)",
|
|
1327
|
+
animation: "sc-stage-pulse 0.8s ease-in-out infinite"
|
|
1328
|
+
}
|
|
1329
|
+
})
|
|
1330
|
+
)
|
|
1331
|
+
);
|
|
1332
|
+
}
|
|
1333
|
+
children.push(
|
|
1334
|
+
(0, import_react2.createElement)("style", { key: "kf" }, "@keyframes sc-stage-pulse{0%,100%{opacity:.35}50%{opacity:.9}}")
|
|
1335
|
+
);
|
|
1336
|
+
return (0, import_react2.createElement)(
|
|
1337
|
+
"div",
|
|
1338
|
+
{
|
|
1339
|
+
className,
|
|
1340
|
+
style: containerStyle,
|
|
1341
|
+
"data-sc-part": "stage",
|
|
1342
|
+
"data-sc-status": stage.status,
|
|
1343
|
+
"data-sc-rendering": stage.isRendering ? "true" : "false",
|
|
1344
|
+
...stage.error && { "data-sc-error": stage.error.code ?? "error" }
|
|
1345
|
+
},
|
|
1346
|
+
...children
|
|
1347
|
+
);
|
|
1348
|
+
}
|
|
936
1349
|
|
|
937
1350
|
// src/framework/adapters/react.ts
|
|
938
1351
|
var ReactAdapter = class {
|
|
@@ -1054,8 +1467,10 @@ function useFrameworkAdapter(React) {
|
|
|
1054
1467
|
}
|
|
1055
1468
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1056
1469
|
0 && (module.exports = {
|
|
1470
|
+
MockupStage,
|
|
1057
1471
|
ReactAdapter,
|
|
1058
1472
|
createReactComponent,
|
|
1473
|
+
useMockupStage,
|
|
1059
1474
|
useReactAdapter,
|
|
1060
1475
|
useRealtimeMockup
|
|
1061
1476
|
});
|