@snowcone-app/ui 0.10.17 → 0.10.20
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 +25 -0
- package/dist/index.cjs +59 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -1
- package/dist/index.d.ts +29 -1
- package/dist/index.js +59 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/composed/HeroProductImage.tsx +42 -7
- package/src/composed/__tests__/HeroProductImage.strip.test.tsx +53 -0
- package/src/composed/carousels/types.ts +6 -0
- package/src/patterns/RealtimeProvider.tsx +63 -2
- package/src/patterns/__tests__/RealtimeProvider.views.test.tsx +90 -0
- package/src/patterns/__tests__/croppedFrameComposite.test.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/ui",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.20",
|
|
4
4
|
"description": "React components for merchandise visualization and customization",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
"react-instantsearch": "^7.15.5",
|
|
104
104
|
"react-zoom-pan-pinch": "^3.6.4",
|
|
105
105
|
"tailwind-merge": "^3.0.0",
|
|
106
|
-
"@snowcone-app/sdk": "0.31.
|
|
106
|
+
"@snowcone-app/sdk": "0.31.4"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
109
|
"@chromatic-com/storybook": "^4.1.2",
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
resolvePreviewVariantId,
|
|
28
28
|
resolveMockupId,
|
|
29
29
|
SNOWCONE_HOSTS,
|
|
30
|
+
type MockupView,
|
|
30
31
|
} from "@snowcone-app/sdk";
|
|
31
32
|
|
|
32
33
|
/** Slugify a placement label into the catalog's `placements[].key` form. */
|
|
@@ -84,7 +85,11 @@ function buildHeroMockupUrl(args: {
|
|
|
84
85
|
placements: PlacementLike[];
|
|
85
86
|
mockupId: string;
|
|
86
87
|
variantId: string;
|
|
87
|
-
|
|
88
|
+
/**
|
|
89
|
+
* A render width (16:9); the whole 8:3 strip at a width; or a crop of the strip (`view=`, delivered at its own px: no
|
|
90
|
+
* width).
|
|
91
|
+
*/
|
|
92
|
+
size: { width: number; aspect?: "8:3" } | { stripView: MockupView };
|
|
88
93
|
/** Canonical 6-hex background (see MockupBackground) — painted behind alpha scenes. */
|
|
89
94
|
bg?: string;
|
|
90
95
|
}): string | null {
|
|
@@ -99,13 +104,25 @@ function buildHeroMockupUrl(args: {
|
|
|
99
104
|
return getMockupUrl(args.productCode, {
|
|
100
105
|
shop: snow.shop,
|
|
101
106
|
design: publicDesign,
|
|
102
|
-
|
|
107
|
+
...("stripView" in args.size
|
|
108
|
+
? { aspect: "8:3" as const, stripView: args.size.stripView }
|
|
109
|
+
: { width: args.size.width, ...(args.size.aspect ? { aspect: args.size.aspect } : {}) }),
|
|
103
110
|
view: args.mockupId,
|
|
104
111
|
variant: args.variantId,
|
|
105
112
|
...(args.bg ? { bg: args.bg } : {}),
|
|
106
113
|
...(snow.resolver ? { base: snow.resolver } : {}),
|
|
107
114
|
});
|
|
108
115
|
}
|
|
116
|
+
/** A crop of the strip needs no width: it is delivered at its own px. */
|
|
117
|
+
function isCrop(stripView: MockupView | "whole-strip" | undefined): stripView is MockupView {
|
|
118
|
+
return stripView !== undefined && stripView !== "whole-strip";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function heroSize(stripView: MockupView | "whole-strip" | undefined, width: number): { width: number; aspect?: "8:3" } | { stripView: MockupView } {
|
|
122
|
+
if (isCrop(stripView)) return { stripView };
|
|
123
|
+
return stripView === "whole-strip" ? { width, aspect: "8:3" } : { width };
|
|
124
|
+
}
|
|
125
|
+
|
|
109
126
|
import { useImageTransition } from "../hooks/useImageTransition";
|
|
110
127
|
import { useContainerWidth } from "../hooks/viewport/useContainerWidth";
|
|
111
128
|
import { observe } from "../hooks/visibility/observerPool";
|
|
@@ -181,6 +198,16 @@ export interface HeroProductImageProps {
|
|
|
181
198
|
* phone, where cover would cut a 16:9 frame to its centre). Stamped as data-fit.
|
|
182
199
|
*/
|
|
183
200
|
fit?: "cover" | "whole";
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Ask for a crop of the mockup's 8:3 strip (`aspect=8:3&view=<r>@<f>`, no width: the crop comes at its own px), or
|
|
204
|
+
* `"whole-strip"` (`aspect=8:3` at the measured width: a window wider than the crop grid), instead of a width-sized
|
|
205
|
+
* 16:9 picture: the desktop hero of a mockup whose catalog entry carries `strip` (www heroStrip.ts). Only the static
|
|
206
|
+
* picture follows it; realtime frames (an edit) stay the session's 16:9. Stamped as data-strip-view.
|
|
207
|
+
*/
|
|
208
|
+
stripView?: MockupView | "whole-strip";
|
|
209
|
+
/** Called with the URL of each picture once it is ON SCREEN (its <img> loaded), not when it is asked for. */
|
|
210
|
+
onShown?: (url: string) => void;
|
|
184
211
|
}
|
|
185
212
|
|
|
186
213
|
/**
|
|
@@ -231,6 +258,8 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
231
258
|
unavailableText = "Preview unavailable",
|
|
232
259
|
viewportPolicy = false,
|
|
233
260
|
fit = "cover",
|
|
261
|
+
stripView,
|
|
262
|
+
onShown,
|
|
234
263
|
}: HeroProductImageProps) {
|
|
235
264
|
// Context
|
|
236
265
|
const context = useProductOptional();
|
|
@@ -302,8 +331,8 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
302
331
|
if (context?.loading) return null;
|
|
303
332
|
// Skip if no valid artwork
|
|
304
333
|
if (!effectiveArtworkSrc || effectiveArtworkSrc.trim() === "") return null;
|
|
305
|
-
// Skip if missing required IDs or width not yet measured
|
|
306
|
-
if (!actualProductId || !actualMockupId || !actualGvid || actualGvid === "default" || !width) return null;
|
|
334
|
+
// Skip if missing required IDs or width not yet measured (a strip crop needs no width)
|
|
335
|
+
if (!actualProductId || !actualMockupId || !actualGvid || actualGvid === "default" || !(isCrop(stripView) || width)) return null;
|
|
307
336
|
|
|
308
337
|
// Get placements for design creation
|
|
309
338
|
const allPlacements = product?.placements || context?.product?.placements || [];
|
|
@@ -340,10 +369,11 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
340
369
|
placements: allPlacements,
|
|
341
370
|
mockupId: actualMockupId,
|
|
342
371
|
variantId: actualGvid,
|
|
343
|
-
width,
|
|
372
|
+
size: heroSize(stripView, width),
|
|
344
373
|
bg: mockupBg,
|
|
345
374
|
});
|
|
346
375
|
}, [
|
|
376
|
+
stripView,
|
|
347
377
|
realtimeUrl,
|
|
348
378
|
mode,
|
|
349
379
|
context?.loading,
|
|
@@ -413,6 +443,8 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
413
443
|
// Ref for onUrlGenerated callback (used in realtime subscription without adding to deps)
|
|
414
444
|
const onUrlGeneratedRef = useRef(onUrlGenerated);
|
|
415
445
|
onUrlGeneratedRef.current = onUrlGenerated;
|
|
446
|
+
const onShownRef = useRef(onShown);
|
|
447
|
+
onShownRef.current = onShown;
|
|
416
448
|
|
|
417
449
|
// Track layers.length in a ref so we can read it without depending on it
|
|
418
450
|
// This prevents effect re-runs when layers change (which would cause duplicate requests)
|
|
@@ -915,7 +947,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
915
947
|
!actualMockupId ||
|
|
916
948
|
!actualGvid ||
|
|
917
949
|
actualGvid === "default" ||
|
|
918
|
-
!width
|
|
950
|
+
!(isCrop(stripView) || width)
|
|
919
951
|
) {
|
|
920
952
|
return;
|
|
921
953
|
}
|
|
@@ -935,7 +967,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
935
967
|
placements: allPlacements,
|
|
936
968
|
mockupId: actualMockupId,
|
|
937
969
|
variantId: actualGvid,
|
|
938
|
-
|
|
970
|
+
size: heroSize(stripView, width),
|
|
939
971
|
bg: mockupBg,
|
|
940
972
|
});
|
|
941
973
|
if (!url) return;
|
|
@@ -1027,6 +1059,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1027
1059
|
mockupBg,
|
|
1028
1060
|
images,
|
|
1029
1061
|
width,
|
|
1062
|
+
stripView,
|
|
1030
1063
|
effects,
|
|
1031
1064
|
// Use primitive values extracted at the top of the component
|
|
1032
1065
|
artworkSrc,
|
|
@@ -1232,6 +1265,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1232
1265
|
// Which picture this is (machine-readable: the canary and verify-www name pictures by it, never by DOM order).
|
|
1233
1266
|
data-mockup-id={actualMockupId || undefined}
|
|
1234
1267
|
data-fit={fit}
|
|
1268
|
+
data-strip-view={stripView}
|
|
1235
1269
|
data-picture-failed={imgFailed ? "E_PICTURE_UNDISPLAYED" : undefined}
|
|
1236
1270
|
data-picture-failed-src={failedSrc}
|
|
1237
1271
|
data-mockup-unavailable={unavailable ? unavailable.code : undefined}
|
|
@@ -1283,6 +1317,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
1283
1317
|
onLoad();
|
|
1284
1318
|
}
|
|
1285
1319
|
onUrlGeneratedRef.current?.(renderedUrl);
|
|
1320
|
+
if (renderedUrl) onShownRef.current?.(renderedUrl);
|
|
1286
1321
|
}}
|
|
1287
1322
|
onError={() => {
|
|
1288
1323
|
// First failure for this URL → retry once before giving up
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plate's desktop hero asks for the crop of its 8:3 strip the window shows (lane shapes #5520, the hero-crop design):
|
|
3
|
+
* stripView="<r>@<f>" builds `aspect=8:3&view=<r>@<f>` with NO width; "whole-strip" builds aspect=8:3 at a width; no
|
|
4
|
+
* stripView keeps today's width-sized 16:9 URL.
|
|
5
|
+
*/
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
7
|
+
import React from "react";
|
|
8
|
+
import { render, waitFor, cleanup } from "@testing-library/react";
|
|
9
|
+
import { HeroProductImage } from "../HeroProductImage";
|
|
10
|
+
|
|
11
|
+
const ART = { type: "regular", src: "https://example.test/art.png" } as const;
|
|
12
|
+
const PRODUCT = { id: "A9AVRY", placements: [{ label: "Front", key: "front" }] };
|
|
13
|
+
|
|
14
|
+
async function heroSrc(props: Partial<React.ComponentProps<typeof HeroProductImage>>): Promise<URL> {
|
|
15
|
+
const { container } = render(
|
|
16
|
+
<HeroProductImage productId="A9AVRY" mockupId="FVC6BW" gvid="gv1" artwork={ART} product={PRODUCT} width={1600} {...props} />,
|
|
17
|
+
);
|
|
18
|
+
await waitFor(() => expect(container.querySelector("img")).not.toBeNull());
|
|
19
|
+
return new URL(container.querySelector("img")!.getAttribute("src")!);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe("HeroProductImage stripView", () => {
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
(window as unknown as { snowcone?: unknown }).snowcone = { shop: "shop1" };
|
|
25
|
+
});
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
cleanup();
|
|
28
|
+
delete (window as unknown as { snowcone?: unknown }).snowcone;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("a crop: aspect=8:3 and view=, no width, stamped data-strip-view", async () => {
|
|
32
|
+
const src = await heroSrc({ stripView: "1.9@0.375" });
|
|
33
|
+
expect(src.searchParams.get("aspect")).toBe("8:3");
|
|
34
|
+
expect(src.searchParams.get("view")).toBe("1.9@0.375");
|
|
35
|
+
expect(src.searchParams.get("mockup")).toBe("FVC6BW");
|
|
36
|
+
expect(src.searchParams.has("width")).toBe(false);
|
|
37
|
+
expect((document.querySelector("[data-hero-image]") as HTMLElement).dataset.stripView).toBe("1.9@0.375");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("the whole strip: aspect=8:3 at a width, no view", async () => {
|
|
41
|
+
const src = await heroSrc({ stripView: "whole-strip" });
|
|
42
|
+
expect(src.searchParams.get("aspect")).toBe("8:3");
|
|
43
|
+
expect(src.searchParams.has("view")).toBe(false);
|
|
44
|
+
expect(src.searchParams.has("width")).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("no stripView: today's 16:9 URL at a width", async () => {
|
|
48
|
+
const src = await heroSrc({});
|
|
49
|
+
expect(src.searchParams.has("aspect")).toBe(false);
|
|
50
|
+
expect(src.searchParams.has("view")).toBe(false);
|
|
51
|
+
expect(src.searchParams.has("width")).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -57,6 +57,12 @@ export interface CarouselImage {
|
|
|
57
57
|
* Stamped data-gallery-role on the slide.
|
|
58
58
|
*/
|
|
59
59
|
galleryRole?: "size-picture";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A plate's 8:3 strip (the catalog's `mockups[].strip`): the desktop hero asks for the crop its window shows
|
|
63
|
+
* (`aspect=8:3&view=<r>@<f>`). Absent = the 16:9 hero. Phones ignore it.
|
|
64
|
+
*/
|
|
65
|
+
strip?: { shape: "8:3"; productX: number };
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
@@ -37,7 +37,7 @@ import React, {
|
|
|
37
37
|
useMemo,
|
|
38
38
|
} from "react";
|
|
39
39
|
import { useRealtimeMockup } from "@snowcone-app/sdk/react";
|
|
40
|
-
import { gallerySizePicture, mockupRole, resolvePreviewVariantId, resolveVariantId as resolveVariantIdUtil } from "@snowcone-app/sdk";
|
|
40
|
+
import { gallerySizePicture, mockupRole, resolvePreviewVariantId, resolveVariantId as resolveVariantIdUtil, type MockupView } from "@snowcone-app/sdk";
|
|
41
41
|
import type {
|
|
42
42
|
RealtimeRenderError,
|
|
43
43
|
MockupResult as SdkMockupResult,
|
|
@@ -281,6 +281,18 @@ export interface RealtimeContextValue {
|
|
|
281
281
|
/** The design editor opened or closed (Done). Drives the viewport policy's phase; see `editorOpen`. */
|
|
282
282
|
setEditorOpen: (open: boolean) => void;
|
|
283
283
|
|
|
284
|
+
/**
|
|
285
|
+
* The crop of each plate's 8:3 strip the page's hero shows (`view=<r>@<f>`, lane shapes #5520): those mockups' live
|
|
286
|
+
* frames come as the same crop (rendercenter rc#898), so an edit drops into the hero's box. Re-sent on every config;
|
|
287
|
+
* a change re-sends the config and re-seeds the cropped-delivery backgrounds (they are the old crop). `{}` = none.
|
|
288
|
+
*/
|
|
289
|
+
setMockupViews: (views: Readonly<Record<string, MockupView>>) => void;
|
|
290
|
+
/**
|
|
291
|
+
* What a frame URL this provider handed out is: the view it was rendered as, `null` for a full 16:9 frame, `undefined`
|
|
292
|
+
* for a URL it did not hand out (a static picture).
|
|
293
|
+
*/
|
|
294
|
+
frameViewOf: (url: string) => MockupView | null | undefined;
|
|
295
|
+
|
|
284
296
|
// Flush pending blobs immediately (call on mouse up, action complete, etc.)
|
|
285
297
|
flushPendingBlobs: () => void;
|
|
286
298
|
hasPendingBlobs: () => boolean;
|
|
@@ -540,7 +552,7 @@ export function RealtimeProvider({
|
|
|
540
552
|
sendCanvasBlob: sendCanvasBlobRaw,
|
|
541
553
|
sendCanvasState: sendCanvasStateRaw,
|
|
542
554
|
sendColorBlob,
|
|
543
|
-
sendConfig:
|
|
555
|
+
sendConfig: sendConfigHook,
|
|
544
556
|
updateMockupIds: updateMockupIdsRaw,
|
|
545
557
|
updatePlacementSettings: updatePlacementSettingsRaw,
|
|
546
558
|
connect,
|
|
@@ -553,6 +565,21 @@ export function RealtimeProvider({
|
|
|
553
565
|
isFrameSuperseded,
|
|
554
566
|
ackFrame,
|
|
555
567
|
} = realtimeMockup;
|
|
568
|
+
// The hero's crop per plate mockup (setMockupViews): attached to EVERY config this provider sends (the first, the
|
|
569
|
+
// crop flips, the full-frame fallbacks, the shape changes), so a re-send never drops it. The sdk sends only the views
|
|
570
|
+
// of the config's own mockupIds (sessionViews). `sentViewsRef` = the views of the config in force, which tag frames.
|
|
571
|
+
const viewsRef = useRef<Readonly<Record<string, MockupView>>>({});
|
|
572
|
+
const sentViewsRef = useRef<Readonly<Record<string, MockupView>>>({});
|
|
573
|
+
const frameViewsRef = useRef<Map<string, MockupView | null>>(new Map());
|
|
574
|
+
const sendConfigRaw = useCallback(
|
|
575
|
+
(config: Parameters<typeof sendConfigHook>[0]) => {
|
|
576
|
+
const views = viewsRef.current;
|
|
577
|
+
const withViews = Object.keys(views).length > 0 ? { ...config, views: { ...views } } : config;
|
|
578
|
+
sentViewsRef.current = withViews.views ?? {};
|
|
579
|
+
return sendConfigHook(withViews);
|
|
580
|
+
},
|
|
581
|
+
[sendConfigHook]
|
|
582
|
+
);
|
|
556
583
|
// Read through a ref: the composite pump's callbacks stay stable across renders.
|
|
557
584
|
const isFrameSupersededRef = useRef(isFrameSuperseded);
|
|
558
585
|
isFrameSupersededRef.current = isFrameSuperseded;
|
|
@@ -985,6 +1012,30 @@ export function RealtimeProvider({
|
|
|
985
1012
|
croppedActiveRef.current = false;
|
|
986
1013
|
}, []);
|
|
987
1014
|
|
|
1015
|
+
/**
|
|
1016
|
+
* The hero's crops changed (a new bucket on resize / panel drag; the page debounces it). Re-send the config in force
|
|
1017
|
+
* with them: the box re-renders on it (views are in its render fingerprint). The frozen cropped-delivery backgrounds
|
|
1018
|
+
* are the old crop, so they re-seed: this config goes full-frame and the delivery flips back once they have.
|
|
1019
|
+
*/
|
|
1020
|
+
const setMockupViews = useCallback(
|
|
1021
|
+
(views: Readonly<Record<string, MockupView>>) => {
|
|
1022
|
+
const key = (v: Readonly<Record<string, string>>) => JSON.stringify(Object.keys(v).sort().map((k) => [k, v[k]]));
|
|
1023
|
+
if (key(views) === key(viewsRef.current)) return;
|
|
1024
|
+
viewsRef.current = { ...views };
|
|
1025
|
+
const base = lastConfigBaseRef.current;
|
|
1026
|
+
if (!base || !configSentRef.current) return; // the first config will carry them
|
|
1027
|
+
invalidateBackgrounds();
|
|
1028
|
+
requestedMockupIdsRef.current = (base.mockupIds as string[] | undefined) ?? [];
|
|
1029
|
+
sendConfigRaw({
|
|
1030
|
+
...base,
|
|
1031
|
+
placementSettings: { ...placementSettingsOverridesRef.current },
|
|
1032
|
+
} as Parameters<typeof sendConfigRaw>[0]);
|
|
1033
|
+
},
|
|
1034
|
+
[invalidateBackgrounds, sendConfigRaw]
|
|
1035
|
+
);
|
|
1036
|
+
|
|
1037
|
+
const frameViewOf = useCallback((url: string) => frameViewsRef.current.get(url), []);
|
|
1038
|
+
|
|
988
1039
|
const currentShape = useCallback(
|
|
989
1040
|
() =>
|
|
990
1041
|
sessionShape({
|
|
@@ -1100,6 +1151,12 @@ export function RealtimeProvider({
|
|
|
1100
1151
|
if (!watched || shownUrl === result.imageUrl) ackFrameRef.current?.(result.mockupId, seq);
|
|
1101
1152
|
});
|
|
1102
1153
|
|
|
1154
|
+
// What each frame is (frameViewOf): the crop of the config in force, or a full 16:9 frame.
|
|
1155
|
+
results.forEach((result) => {
|
|
1156
|
+
frameViewsRef.current.set(result.imageUrl, sentViewsRef.current[result.mockupId] ?? null);
|
|
1157
|
+
if (frameViewsRef.current.size > 512) frameViewsRef.current.delete(frameViewsRef.current.keys().next().value as string);
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1103
1160
|
const clearedPlacements = new Set<string>();
|
|
1104
1161
|
results.forEach((result) => {
|
|
1105
1162
|
const wasPending = pendingMockupIdsRef.current.has(result.mockupId);
|
|
@@ -2110,6 +2167,8 @@ export function RealtimeProvider({
|
|
|
2110
2167
|
reportPictureView,
|
|
2111
2168
|
getSessionShape,
|
|
2112
2169
|
setEditorOpen: setEditorOpenState,
|
|
2170
|
+
setMockupViews,
|
|
2171
|
+
frameViewOf,
|
|
2113
2172
|
isMockupLoading,
|
|
2114
2173
|
registerBlobUrl,
|
|
2115
2174
|
markBlobShown,
|
|
@@ -2148,6 +2207,8 @@ export function RealtimeProvider({
|
|
|
2148
2207
|
getLatestSentVersionsStable,
|
|
2149
2208
|
reportPictureView,
|
|
2150
2209
|
getSessionShape,
|
|
2210
|
+
setMockupViews,
|
|
2211
|
+
frameViewOf,
|
|
2151
2212
|
isMockupLoading,
|
|
2152
2213
|
registerBlobUrl,
|
|
2153
2214
|
markBlobShown,
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editor's live frame cropped like the hero (lane shapes #5520, rendercenter rc#898): the page hands the provider the
|
|
3
|
+
* hero's crop per plate mockup (setMockupViews); EVERY config the provider sends carries them (the first, the cropped-
|
|
4
|
+
* delivery flip, a re-send), a new bucket re-sends the config full-frame (the frozen backgrounds are the old crop), and
|
|
5
|
+
* each frame handed out says which crop it is (frameViewOf), so the hero places it.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, it, beforeEach, vi } from "vitest";
|
|
8
|
+
import React from "react";
|
|
9
|
+
import { render, waitFor, act, cleanup } from "@testing-library/react";
|
|
10
|
+
import { RealtimeProvider, useRealtime, type RealtimeContextValue } from "../RealtimeProvider";
|
|
11
|
+
|
|
12
|
+
const h = vi.hoisted(() => ({
|
|
13
|
+
state: { results: [] as Array<Record<string, unknown>> },
|
|
14
|
+
sendConfig: vi.fn(),
|
|
15
|
+
stable: {
|
|
16
|
+
sendCanvasBlob: vi.fn(), sendColorBlob: vi.fn(), updateMockupIds: vi.fn(), updatePlacementSettings: vi.fn(),
|
|
17
|
+
connect: vi.fn(), disconnect: vi.fn(), flushPendingBlobs: vi.fn(), hasPendingBlobs: vi.fn(() => false),
|
|
18
|
+
getLatestSentVersions: vi.fn(() => ({})), rerender: vi.fn(() => true), sendCanvasState: vi.fn(() => true),
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
vi.mock("@snowcone-app/sdk/react", () => ({
|
|
23
|
+
useRealtimeMockup: () => ({
|
|
24
|
+
isConnected: true, isConfigured: true, renderError: null, mockupResults: h.state.results,
|
|
25
|
+
sendConfig: h.sendConfig, getLastSentVersion: () => 0, ...h.stable,
|
|
26
|
+
}),
|
|
27
|
+
}));
|
|
28
|
+
vi.mock("@snowcone-app/sdk", () => ({ mockupRole: () => "ordinary", gallerySizePicture: () => null, resolveVariantId: () => "gv1", resolvePreviewVariantId: (_c: unknown, v: string) => v }));
|
|
29
|
+
vi.mock("../Product", () => {
|
|
30
|
+
const product = { id: "A9AVRY", placements: [{ label: "Front", type: "image" }], mockups: [{ id: "FVC6BW" }, { id: "PM9BQA" }] };
|
|
31
|
+
return { useProductOptional: () => ({ product, selection: {} }) };
|
|
32
|
+
});
|
|
33
|
+
vi.mock("../MockupBackground", () => ({ useMockupBackgroundOptional: () => undefined }));
|
|
34
|
+
vi.mock("../croppedFrameComposite", async (importOriginal) => {
|
|
35
|
+
const actual = await importOriginal<typeof import("../croppedFrameComposite")>();
|
|
36
|
+
return { ...actual, compositeCroppedFrame: vi.fn(async () => new Blob(["x"])), loadImageElement: vi.fn(async () => ({ naturalWidth: 2736, naturalHeight: 1440 })) };
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
let ctx: RealtimeContextValue;
|
|
40
|
+
function Capture() { ctx = useRealtime(); return null; }
|
|
41
|
+
function Harness({ v }: { v: number }) {
|
|
42
|
+
void v;
|
|
43
|
+
return <RealtimeProvider autoEnable mockupWidth={2000}><Capture /></RealtimeProvider>;
|
|
44
|
+
}
|
|
45
|
+
type Sent = { mockupIds: string[]; views?: Record<string, string>; overlayCrop?: unknown };
|
|
46
|
+
const configs = () => h.sendConfig.mock.calls.map((c) => c[0] as Sent);
|
|
47
|
+
const frame = (id: string, url: string) => ({ mockupId: id, imageUrl: url, renderUrl: url, imageSize: 1, placement: "Front", requestVersion: 1 });
|
|
48
|
+
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
cleanup();
|
|
51
|
+
vi.clearAllMocks();
|
|
52
|
+
h.state.results = [];
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("RealtimeProvider views", () => {
|
|
56
|
+
it("re-sends the config with the hero's crop, and again (only) when the bucket changes", async () => {
|
|
57
|
+
render(<Harness v={1} />);
|
|
58
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
59
|
+
expect(configs()[0].views).toBeUndefined();
|
|
60
|
+
|
|
61
|
+
await act(async () => ctx.setMockupViews({ FVC6BW: "1.9@0.375" }));
|
|
62
|
+
expect(configs().at(-1)).toMatchObject({ mockupIds: ["FVC6BW", "PM9BQA"], views: { FVC6BW: "1.9@0.375" } });
|
|
63
|
+
expect(configs().at(-1)!.overlayCrop).toBeUndefined(); // the backgrounds re-seed with the new crop
|
|
64
|
+
|
|
65
|
+
const n = configs().length;
|
|
66
|
+
await act(async () => ctx.setMockupViews({ FVC6BW: "1.9@0.375" })); // same bucket: nothing
|
|
67
|
+
expect(configs()).toHaveLength(n);
|
|
68
|
+
|
|
69
|
+
await act(async () => ctx.setMockupViews({ FVC6BW: "1.8@0.35" })); // panel dragged: a new bucket
|
|
70
|
+
expect(configs()).toHaveLength(n + 1);
|
|
71
|
+
expect(configs().at(-1)!.views).toEqual({ FVC6BW: "1.8@0.35" });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("keeps the views on the cropped-delivery flip, and tags each frame with its crop", async () => {
|
|
75
|
+
globalThis.URL.createObjectURL = vi.fn(() => "blob:c");
|
|
76
|
+
const { rerender } = render(<Harness v={1} />);
|
|
77
|
+
await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
|
|
78
|
+
await act(async () => ctx.setMockupViews({ FVC6BW: "1.9@0.375" }));
|
|
79
|
+
|
|
80
|
+
h.state.results = [frame("FVC6BW", "https://r/fvc6bw-v1"), frame("PM9BQA", "https://r/pm9bqa-v1")];
|
|
81
|
+
await act(async () => rerender(<Harness v={2} />));
|
|
82
|
+
await waitFor(() => expect(configs().some((c) => c.overlayCrop === "auto")).toBe(true));
|
|
83
|
+
const flip = configs().find((c) => c.overlayCrop === "auto")!;
|
|
84
|
+
expect(flip.views).toEqual({ FVC6BW: "1.9@0.375" });
|
|
85
|
+
|
|
86
|
+
expect(ctx.frameViewOf("https://r/fvc6bw-v1")).toBe("1.9@0.375");
|
|
87
|
+
expect(ctx.frameViewOf("https://r/pm9bqa-v1")).toBeNull(); // a 16:9 frame
|
|
88
|
+
expect(ctx.frameViewOf("https://img.example/static")).toBeUndefined(); // not a realtime frame
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -35,6 +35,18 @@ describe("compositeOffset", () => {
|
|
|
35
35
|
expect(Math.abs(dy - 102)).toBeLessThanOrEqual(1);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
it("a hero crop (views, rc#898): the rect is in the VIEW's fractions and lands on the view frame's own px", () => {
|
|
39
|
+
// view 1.9@0.375 of a 3840x1440 strip is delivered 2736x1440; the box resolves crop=auto inside that window, so a
|
|
40
|
+
// print area at x 820..1960, y 300..1180 of the crop comes back as fractions of 2736 x 1440 (never of the 16:9 frame).
|
|
41
|
+
const rect = { x: 820 / 2736, y: 300 / 1440, w: 1140 / 2736, h: 880 / 1440 };
|
|
42
|
+
expect(compositeOffset(rect, 2736, 1440)).toEqual({ dx: 820, dy: 300 });
|
|
43
|
+
// Rounded to 4 decimals on the wire: still within 1 px of the crop's own px.
|
|
44
|
+
const wire = { x: 0.2997, y: 0.2083, w: 0.4167, h: 0.6111 };
|
|
45
|
+
const { dx, dy } = compositeOffset(wire, 2736, 1440);
|
|
46
|
+
expect(Math.abs(dx - 820)).toBeLessThanOrEqual(1);
|
|
47
|
+
expect(Math.abs(dy - 300)).toBeLessThanOrEqual(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
38
50
|
it("is zero at the origin", () => {
|
|
39
51
|
expect(compositeOffset({ x: 0, y: 0, w: 1, h: 1 }, 1400, 782)).toEqual({
|
|
40
52
|
dx: 0,
|