@snowcone-app/ui 0.10.18 → 0.10.21
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 +23 -0
- package/dist/index.cjs +38 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +38 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- 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.21",
|
|
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.5"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
109
|
"@chromatic-com/storybook": "^4.1.2",
|
|
@@ -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,
|