@snowcone-app/design 0.0.0-stage → 0.2.1
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 +19 -0
- package/README.md +58 -2
- package/dist/conformance/index.cjs +234 -0
- package/dist/conformance/index.cjs.map +1 -0
- package/dist/conformance/index.d.cts +39 -0
- package/dist/conformance/index.d.ts +39 -0
- package/dist/conformance/index.js +228 -0
- package/dist/conformance/index.js.map +1 -0
- package/dist/index.cjs +674 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +238 -0
- package/dist/index.d.ts +238 -0
- package/dist/index.js +638 -0
- package/dist/index.js.map +1 -0
- package/dist/saved-BOsUdd8W.d.cts +2464 -0
- package/dist/saved-BOsUdd8W.d.ts +2464 -0
- package/dist/wire/index.cjs +161 -0
- package/dist/wire/index.cjs.map +1 -0
- package/dist/wire/index.d.cts +168 -0
- package/dist/wire/index.d.ts +168 -0
- package/dist/wire/index.js +147 -0
- package/dist/wire/index.js.map +1 -0
- package/package.json +80 -3
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// src/wire/adjustments.ts
|
|
2
|
+
var NEUTRAL_ADJUSTMENTS = Object.freeze({
|
|
3
|
+
brightness: 1,
|
|
4
|
+
contrast: 1,
|
|
5
|
+
saturation: 1
|
|
6
|
+
});
|
|
7
|
+
var EPSILON = 1e-3;
|
|
8
|
+
function isNeutralAdjustments(adjustments) {
|
|
9
|
+
if (!adjustments) return true;
|
|
10
|
+
return Math.abs(adjustments.brightness - 1) < EPSILON && Math.abs(adjustments.contrast - 1) < EPSILON && Math.abs(adjustments.saturation - 1) < EPSILON;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/wire/crop.ts
|
|
14
|
+
var MASKED_CROP_FRAME = "frame";
|
|
15
|
+
var FULL_CROP = Object.freeze({ cropX: 0, cropY: 0, cropWidth: 1, cropHeight: 1 });
|
|
16
|
+
function isFullCrop(c) {
|
|
17
|
+
const e = 1e-9;
|
|
18
|
+
return Math.abs(c.cropX ?? 0) < e && Math.abs(c.cropY ?? 0) < e && Math.abs((c.cropWidth ?? 1) - 1) < e && Math.abs((c.cropHeight ?? 1) - 1) < e;
|
|
19
|
+
}
|
|
20
|
+
function isLegacyMaskedCrop(td, hasMasks) {
|
|
21
|
+
return hasMasks && td?.maskedCrop !== MASKED_CROP_FRAME;
|
|
22
|
+
}
|
|
23
|
+
function effectiveImageCrop(td, hasMasks) {
|
|
24
|
+
if (!td || isLegacyMaskedCrop(td, hasMasks)) return { ...FULL_CROP };
|
|
25
|
+
return {
|
|
26
|
+
cropX: td.cropX ?? 0,
|
|
27
|
+
cropY: td.cropY ?? 0,
|
|
28
|
+
cropWidth: td.cropWidth ?? 1,
|
|
29
|
+
cropHeight: td.cropHeight ?? 1
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function imageFrameSize(td, hasMasks) {
|
|
33
|
+
const c = effectiveImageCrop(td, hasMasks);
|
|
34
|
+
return { width: (td?.width ?? 0) * c.cropWidth, height: (td?.height ?? 0) * c.cropHeight };
|
|
35
|
+
}
|
|
36
|
+
function hasImageMasks(el) {
|
|
37
|
+
return Array.isArray(el?.masks) && el.masks.length > 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/wire/index.ts
|
|
41
|
+
var CANVAS_STATE_SCHEMA_VERSION = 1;
|
|
42
|
+
function q(n) {
|
|
43
|
+
return typeof n === "number" && Number.isFinite(n) ? Math.round(n * 100) / 100 : n;
|
|
44
|
+
}
|
|
45
|
+
function quantizeGeometry(el) {
|
|
46
|
+
return {
|
|
47
|
+
...el,
|
|
48
|
+
...el.x !== void 0 && { x: q(el.x) },
|
|
49
|
+
...el.y !== void 0 && { y: q(el.y) },
|
|
50
|
+
...el.width !== void 0 && { width: q(el.width) },
|
|
51
|
+
...el.height !== void 0 && { height: q(el.height) },
|
|
52
|
+
...el.rotation !== void 0 && { rotation: q(el.rotation) }
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function serializeStateForServer(state) {
|
|
56
|
+
const srcArtboards = state.artboards?.length ? state.artboards : [{ name: "Front", width: 800, height: 800 }];
|
|
57
|
+
const elementsByArtboard = srcArtboards.map(() => []);
|
|
58
|
+
for (const el of state.elements || []) {
|
|
59
|
+
const tag = el?.artboard;
|
|
60
|
+
const idx = typeof tag === "string" ? srcArtboards.findIndex((ab) => (ab.name || "Front") === tag) : -1;
|
|
61
|
+
if (el?.visible === false) continue;
|
|
62
|
+
elementsByArtboard[idx >= 0 ? idx : 0].push(serializeElement(el));
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
schemaVersion: CANVAS_STATE_SCHEMA_VERSION,
|
|
66
|
+
artboards: srcArtboards.map((srcArtboard, i) => ({
|
|
67
|
+
id: `artboard-${i + 1}`,
|
|
68
|
+
name: srcArtboard.name || "Front",
|
|
69
|
+
x: 0,
|
|
70
|
+
y: 0,
|
|
71
|
+
width: q(srcArtboard.width || 800),
|
|
72
|
+
height: q(srcArtboard.height || 800),
|
|
73
|
+
backgroundColor: srcArtboard.backgroundColor || "transparent",
|
|
74
|
+
// Pass the artboard's flag through — the server worker only paints the
|
|
75
|
+
// background when this is true. Hardcoding `false` here silently dropped
|
|
76
|
+
// explicitly-set artboard backgrounds from every realtime mockup while
|
|
77
|
+
// the editor kept showing them (WYSIWYG drift).
|
|
78
|
+
exportBackground: srcArtboard.exportBackground ?? false,
|
|
79
|
+
elements: elementsByArtboard[i],
|
|
80
|
+
distressTexture: srcArtboard.distressTexture,
|
|
81
|
+
imageMask: srcArtboard.imageMask
|
|
82
|
+
})),
|
|
83
|
+
// Custom (brand) font dependencies ride the wire so the server worker can
|
|
84
|
+
// load them — without this, server renders of custom-font designs fall
|
|
85
|
+
// back to the wrong family even when the bundle supports custom fonts.
|
|
86
|
+
...state.customFonts?.length ? { customFonts: state.customFonts } : {}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function serializeElement(el) {
|
|
90
|
+
const type = el.type || el.transformType;
|
|
91
|
+
if (type === "image") {
|
|
92
|
+
return serializeImageElement(el);
|
|
93
|
+
}
|
|
94
|
+
const { artboard: _artboard, ...rest } = el;
|
|
95
|
+
if (type === "group" && Array.isArray(el.children)) {
|
|
96
|
+
rest.children = el.children.filter((c) => c && c.visible !== false).map(serializeElement);
|
|
97
|
+
}
|
|
98
|
+
return quantizeGeometry({
|
|
99
|
+
...rest,
|
|
100
|
+
type
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function serializeImageElement(el) {
|
|
104
|
+
const td = el.transformData || {};
|
|
105
|
+
const hasMasks = hasImageMasks(el);
|
|
106
|
+
const crop = effectiveImageCrop(td, hasMasks);
|
|
107
|
+
const fullWidth = td.width || 0;
|
|
108
|
+
const fullHeight = td.height || 0;
|
|
109
|
+
const visibleWidth = fullWidth * crop.cropWidth;
|
|
110
|
+
const visibleHeight = fullHeight * crop.cropHeight;
|
|
111
|
+
const serialized = {
|
|
112
|
+
id: el.id,
|
|
113
|
+
type: "image",
|
|
114
|
+
x: el.x || 0,
|
|
115
|
+
y: el.y || 0,
|
|
116
|
+
width: visibleWidth,
|
|
117
|
+
height: visibleHeight,
|
|
118
|
+
rotation: el.rotation || 0,
|
|
119
|
+
imageUrl: el.imageUrl,
|
|
120
|
+
imageAspectRatio: el.imageAspectRatio
|
|
121
|
+
};
|
|
122
|
+
if (!isFullCrop(crop)) {
|
|
123
|
+
serialized.cropX = crop.cropX;
|
|
124
|
+
serialized.cropY = crop.cropY;
|
|
125
|
+
serialized.cropWidth = crop.cropWidth;
|
|
126
|
+
serialized.cropHeight = crop.cropHeight;
|
|
127
|
+
serialized.needsCropPixelConversion = true;
|
|
128
|
+
}
|
|
129
|
+
if (td.flipHorizontal) serialized.flipHorizontal = td.flipHorizontal;
|
|
130
|
+
if (td.flipVertical) serialized.flipVertical = td.flipVertical;
|
|
131
|
+
if (td.borderRadius) serialized.borderRadius = td.borderRadius;
|
|
132
|
+
if (el.shadow) serialized.shadow = el.shadow;
|
|
133
|
+
if (!isNeutralAdjustments(td.adjustments)) serialized.adjustments = { ...td.adjustments };
|
|
134
|
+
if (el.opacity !== void 0) serialized.opacity = el.opacity;
|
|
135
|
+
if (el.distressEffect) serialized.distressEffect = el.distressEffect;
|
|
136
|
+
if (el.masks?.length > 0) serialized.masks = el.masks;
|
|
137
|
+
if (el.blendMode) serialized.blendMode = el.blendMode;
|
|
138
|
+
if (el.knockoutParts) serialized.knockoutParts = el.knockoutParts;
|
|
139
|
+
if (el.stroke) serialized.stroke = el.stroke;
|
|
140
|
+
return quantizeGeometry(serialized);
|
|
141
|
+
}
|
|
142
|
+
var RENDER_WIRE_VERSION = CANVAS_STATE_SCHEMA_VERSION;
|
|
143
|
+
var toRenderWire = serializeStateForServer;
|
|
144
|
+
|
|
145
|
+
export { CANVAS_STATE_SCHEMA_VERSION, FULL_CROP, MASKED_CROP_FRAME, NEUTRAL_ADJUSTMENTS, RENDER_WIRE_VERSION, effectiveImageCrop, hasImageMasks, imageFrameSize, isFullCrop, isLegacyMaskedCrop, isNeutralAdjustments, serializeStateForServer, toRenderWire };
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/wire/adjustments.ts","../../src/wire/crop.ts","../../src/wire/index.ts"],"names":[],"mappings":";AAQO,IAAM,mBAAA,GAAkD,OAAO,MAAA,CAAO;AAAA,EAC3E,UAAA,EAAY,CAAA;AAAA,EACZ,QAAA,EAAU,CAAA;AAAA,EACV,UAAA,EAAY;AACd,CAAC;AAED,IAAM,OAAA,GAAU,IAAA;AAGT,SAAS,qBAAqB,WAAA,EAA2D;AAC9F,EAAA,IAAI,CAAC,aAAa,OAAO,IAAA;AACzB,EAAA,OACE,KAAK,GAAA,CAAI,WAAA,CAAY,aAAa,CAAC,CAAA,GAAI,WACvC,IAAA,CAAK,GAAA,CAAI,YAAY,QAAA,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AA,GAAS,EAAA,CAAG,MAAA;AAItC,EAAA,IAAI,CAAC,oBAAA,CAAqB,EAAA,CAAG,WAAW,CAAA,aAAc,WAAA,GAAc,EAAE,GAAG,EAAA,CAAG,WAAA,EAAY;AACxF,EAAA,IAAI,EAAA,CAAG,OAAA,KAAY,MAAA,EAAW,UAAA,CAAW,UAAU,EAAA,CAAG,OAAA;AAGtD,EAAA,IAAI,EAAA,CAAG,cAAA,EAAgB,UAAA,CAAW,cAAA,GAAiB,EAAA,CAAG,cAAA;AACtD,EAAA,IAAI,GAAG,KAAA,EAAO,MAAA,GAAS,CAAA,EAAG,UAAA,CAAW,QAAQ,EAAA,CAAG,KAAA;AAChD,EAAA,IAAI,EAAA,CAAG,SAAA,EAAW,UAAA,CAAW,SAAA,GAAY,EAAA,CAAG,SAAA;AAC5C,EAAA,IAAI,EAAA,CAAG,aAAA,EAAe,UAAA,CAAW,aAAA,GAAgB,EAAA,CAAG,aAAA;AACpD,EAAA,IAAI,EAAA,CAAG,MAAA,EAAQ,UAAA,CAAW,MAAA,GAAS,EAAA,CAAG,MAAA;AAGtC,EAAA,OAAO,iBAAiB,UAAU,CAAA;AACpC;AAGO,IAAM,mBAAA,GAAsB;AAE5B,IAAM,YAAA,GAAe","file":"index.js","sourcesContent":["/** Brightness / contrast / saturation of an image (1 = neutral). */\nexport interface ImageAdjustments {\n brightness: number;\n contrast: number;\n saturation: number;\n}\n\n/** The neutral adjustments value: equal to omitting `adjustments` entirely. */\nexport const NEUTRAL_ADJUSTMENTS: Readonly<ImageAdjustments> = Object.freeze({\n brightness: 1,\n contrast: 1,\n saturation: 1,\n});\n\nconst EPSILON = 0.001;\n\n/** True when the adjustments are absent or all channels are ~1 (no-op). */\nexport function isNeutralAdjustments(adjustments: ImageAdjustments | undefined | null): boolean {\n if (!adjustments) return true;\n return (\n Math.abs(adjustments.brightness - 1) < EPSILON &&\n Math.abs(adjustments.contrast - 1) < EPSILON &&\n Math.abs(adjustments.saturation - 1) < EPSILON\n );\n}\n","/**\n * Masked crop (moved from @snowcone-app/canvas core/masked-crop.ts, ADR-0115 step 1b): how an image's crop combines with its masks.\n *\n * One model for every render path (the live editor, the worker export, the\n * realtime wire to the render server and the server's own normalizer):\n *\n * the element box is the FRAME; crop picks which part of the photo fills\n * that frame; every mask is fitted to the frame.\n *\n * So \"move / zoom the photo inside its shape\" is the ordinary crop gesture:\n * the frame (and the shape in it) stays put on the artboard while crop pans\n * and zooms the photo behind it.\n *\n * `transformData.maskedCrop === 'frame'` marks an image saved under this\n * model. Every ImageElement the editor constructs carries it. An image that\n * has masks but NOT the marker was saved before it existed (2026-11): back\n * then every exporter ignored crop under a mask and drew the full photo at\n * the element's position, so the effective crop of such an image is the full\n * frame — its pixels never change. (Unmasked images are unaffected either\n * way: crop always applied to them.)\n */\n\n/** The crop fields of an image's transformData (normalized 0..1). */\nexport interface CropFields {\n cropX: number;\n cropY: number;\n cropWidth: number;\n cropHeight: number;\n}\n\n/** Marker value: crop applies under masks (the frame model). */\nexport const MASKED_CROP_FRAME = 'frame' as const;\n\n/** The minimal transformData shape this module reads. Loose on purpose: it\n * is also fed raw persisted JSON. */\nexport interface MaskedCropTransformData {\n width?: number;\n height?: number;\n cropX?: number;\n cropY?: number;\n cropWidth?: number;\n cropHeight?: number;\n maskedCrop?: string;\n}\n\nexport const FULL_CROP: Readonly<CropFields> = Object.freeze({ cropX: 0, cropY: 0, cropWidth: 1, cropHeight: 1 });\n\n/** True when the crop is the whole picture (within float noise). */\nexport function isFullCrop(c: Partial<CropFields>): boolean {\n const e = 1e-9;\n return (\n Math.abs(c.cropX ?? 0) < e &&\n Math.abs(c.cropY ?? 0) < e &&\n Math.abs((c.cropWidth ?? 1) - 1) < e &&\n Math.abs((c.cropHeight ?? 1) - 1) < e\n );\n}\n\n/** A legacy masked image: masks, no frame marker. Its crop is ignored. */\nexport function isLegacyMaskedCrop(td: MaskedCropTransformData | undefined, hasMasks: boolean): boolean {\n return hasMasks && td?.maskedCrop !== MASKED_CROP_FRAME;\n}\n\n/** The crop every renderer must use for this image. */\nexport function effectiveImageCrop(td: MaskedCropTransformData | undefined, hasMasks: boolean): CropFields {\n if (!td || isLegacyMaskedCrop(td, hasMasks)) return { ...FULL_CROP };\n return {\n cropX: td.cropX ?? 0,\n cropY: td.cropY ?? 0,\n cropWidth: td.cropWidth ?? 1,\n cropHeight: td.cropHeight ?? 1,\n };\n}\n\n/** The element box (the frame the photo and its masks fill), in design px. */\nexport function imageFrameSize(\n td: MaskedCropTransformData | undefined,\n hasMasks: boolean\n): { width: number; height: number } {\n const c = effectiveImageCrop(td, hasMasks);\n return { width: (td?.width ?? 0) * c.cropWidth, height: (td?.height ?? 0) * c.cropHeight };\n}\n\n/** Whether an element config carries at least one mask. */\nexport function hasImageMasks(el: { masks?: unknown[] | null } | null | undefined): boolean {\n return Array.isArray(el?.masks) && el.masks.length > 0;\n}\n","/**\n * Serialize canvas state (from onChange/toJSON) to the format the export worker expects.\n *\n * This bridges the gap between SnowconeCanvas.onChange() output (element configs with\n * transformType and nested transformData) and the export worker's expected format\n * (elements with 'type' field and flattened image properties).\n *\n * Use this when sending canvas state to a server-side renderer that runs the export\n * worker bundle. The client-side serializeForWorkerExport() requires live element\n * instances; this function works on plain JSON from onChange/toJSON.\n */\n\n// Moved from @snowcone-app/canvas (rendering/serialize-for-server.ts) by ADR-0115 step 1b: the ONE conversion from a\n// saved design to the render wire, shared by the editor and the render box. No zod here: this module is in the\n// editor's always-mounted state graph.\nimport { isNeutralAdjustments } from './adjustments';\nimport { effectiveImageCrop, hasImageMasks, isFullCrop } from './crop';\n\n/**\n * Wire-schema version of the server-render `canvas_state` payload (ADR-0079).\n *\n * This is a PUBLIC contract: third parties send `ServerRenderRequest` (produced\n * by {@link serializeStateForServer}) to Snowcone's realtime renderer. Bump the\n * MAJOR whenever the element/artboard shape changes incompatibly so a renderer\n * can reject states it cannot render instead of silently mis-rendering. Servers\n * treat a MISSING `schemaVersion` as `1` — legacy clients that predate the\n * stamp keep working.\n */\nexport const CANVAS_STATE_SCHEMA_VERSION = 1;\n\n// NOTE: this is a `type`, not an `interface`, ON PURPOSE. The SDK's\n// `RenderSession.renderState(placement, state)` takes a `RenderState` that has a\n// string index signature (`[key: string]: unknown`). A named `interface` does\n// NOT get TypeScript's implicit index signature, so an `interface` value is not\n// assignable to an indexed type — passing `serializeStateForServer()`'s result\n// straight into `renderState()` (the headline realtime flow) fails with TS2345\n// \"Index signature for type 'string' is missing\". A `type` alias DOES get the\n// implicit signature, so the flow type-checks with no cast. Do not convert this\n// back to `interface`. Pinned by @snowcone-app/contracts\n// (src/realtime-getting-started.contract.ts).\nexport type ServerRenderRequest = {\n /** Wire-schema version — see {@link CANVAS_STATE_SCHEMA_VERSION}. */\n schemaVersion: number;\n artboards: Array<{\n id: string;\n name: string;\n x: number;\n y: number;\n width: number;\n height: number;\n backgroundColor: string;\n exportBackground: boolean;\n elements: any[];\n distressTexture?: any;\n imageMask?: any;\n }>;\n /** Custom (brand) font dependencies — the server worker's loadFonts needs\n * these to fetch non-Google faces. Omitted when the design uses none. */\n customFonts?: Array<{ family: string; url: string; weight?: number; style?: string }>;\n};\n\n/**\n * Quantize a geometry value to a stable precision (0.01px).\n *\n * `SnowconeCanvas.onChange` fires on the render loop and reports element\n * geometry with sub-pixel float drift (e.g. `height: 355.9609375`) even when the\n * design is idle. Serialized verbatim, every frame produces a *different* wire\n * state, so a `onChange → renderState` wiring streams renders continuously while\n * the user does nothing — silently burning the realtime render budget. Rounding\n * geometry to a precision well below a pixel collapses that drift so identical\n * idle frames serialize identically (and the SDK's send-dedupe drops them).\n */\nfunction q(n: unknown): unknown {\n return typeof n === 'number' && Number.isFinite(n) ? Math.round(n * 100) / 100 : n;\n}\n\n/** Return a shallow copy with geometry fields quantized (see {@link q}). */\nfunction quantizeGeometry<T extends Record<string, any>>(el: T): T {\n return {\n ...el,\n ...(el.x !== undefined && { x: q(el.x) }),\n ...(el.y !== undefined && { y: q(el.y) }),\n ...(el.width !== undefined && { width: q(el.width) }),\n ...(el.height !== undefined && { height: q(el.height) }),\n ...(el.rotation !== undefined && { rotation: q(el.rotation) }),\n };\n}\n\n/**\n * Convert onChange canvas state to the server render format.\n *\n * Emits EVERY artboard in the state, each holding its own elements: elements\n * are grouped by their `artboard` tag (the artboard NAME `SnowconeCanvas.onChange`\n * stamps on each element). Elements without a tag — legacy saved states — land\n * on the first artboard, which also keeps single-artboard output byte-identical\n * to before. The tag itself is client-side metadata and is stripped from the\n * wire elements, so the per-placement `{schemaVersion, artboards: [...]}` shape\n * the server consumes is unchanged.\n *\n * For a multi-placement product, slice the result per placement:\n *\n * ```ts\n * const wire = serializeStateForServer(state);\n * for (const ab of wire.artboards) {\n * session.renderState(ab.name, { schemaVersion: wire.schemaVersion, artboards: [ab] });\n * }\n * ```\n *\n * @param state - The state from SnowconeCanvas onChange callback\n * @returns ServerRenderRequest ready to send via sendCanvasState()\n */\nexport function serializeStateForServer(state: {\n elements?: any[];\n artboards?: Array<{\n name?: string;\n width?: number;\n height?: number;\n backgroundColor?: string;\n exportBackground?: boolean;\n clipShape?: any;\n distressTexture?: any;\n imageMask?: any;\n /** Pacing role (multi-page templates, ADR-0086) — client/template\n * metadata, stripped from the wire like the element `artboard` tag. */\n role?: string;\n }>;\n activeArtboard?: string;\n customFonts?: Array<{ family: string; url: string; weight?: number; style?: string }>;\n}): ServerRenderRequest {\n const srcArtboards = state.artboards?.length\n ? state.artboards\n : [{ name: 'Front', width: 800, height: 800 }];\n\n // Group elements by their artboard tag (artboard NAME, stamped by\n // SnowconeCanvas.onChange). Untagged or unknown-tagged elements fall back to\n // the first artboard — today's behavior for legacy flat states.\n const elementsByArtboard = srcArtboards.map<any[]>(() => []);\n for (const el of state.elements || []) {\n const tag = el?.artboard;\n const idx = typeof tag === 'string'\n ? srcArtboards.findIndex((ab) => (ab.name || 'Front') === tag)\n : -1;\n // A hidden layer is not part of the design (the editor doesn't paint it) — it must not reach a mockup.\n if (el?.visible === false) continue;\n elementsByArtboard[idx >= 0 ? idx : 0].push(serializeElement(el));\n }\n\n // Wire artboards are built field-by-field: template metadata on the source\n // artboards (the `role` pacing tag, ADR-0086) never reaches the server —\n // same rule as the element `artboard` membership tag below.\n return {\n schemaVersion: CANVAS_STATE_SCHEMA_VERSION,\n artboards: srcArtboards.map((srcArtboard, i) => ({\n id: `artboard-${i + 1}`,\n name: srcArtboard.name || 'Front',\n x: 0,\n y: 0,\n width: q(srcArtboard.width || 800) as number,\n height: q(srcArtboard.height || 800) as number,\n backgroundColor: srcArtboard.backgroundColor || 'transparent',\n // Pass the artboard's flag through — the server worker only paints the\n // background when this is true. Hardcoding `false` here silently dropped\n // explicitly-set artboard backgrounds from every realtime mockup while\n // the editor kept showing them (WYSIWYG drift).\n exportBackground: srcArtboard.exportBackground ?? false,\n elements: elementsByArtboard[i],\n distressTexture: srcArtboard.distressTexture,\n imageMask: srcArtboard.imageMask,\n })),\n // Custom (brand) font dependencies ride the wire so the server worker can\n // load them — without this, server renders of custom-font designs fall\n // back to the wrong family even when the bundle supports custom fonts.\n ...(state.customFonts?.length ? { customFonts: state.customFonts } : {}),\n };\n}\n\n/**\n * Serialize a single element from toJSON() format to export worker format.\n */\nfunction serializeElement(el: any): any {\n const type = el.type || el.transformType;\n\n if (type === 'image') {\n return serializeImageElement(el);\n }\n\n // Text, shape, group, path elements: map transformType → type, keep everything else\n // Text, shape, group, path elements: pass through as-is with type mapped.\n // Builtin mask URLs (builtin-mask:*) are kept as-is — the server worker\n // generates them procedurally via generateMaskBitmap().\n // Geometry is quantized (see `q`) so idle render-loop float drift doesn't\n // produce a new wire state every frame.\n // The `artboard` membership tag is CLIENT-side metadata (it already decided\n // which wire artboard this element belongs to) — strip it so the server\n // payload shape stays unchanged.\n const { artboard: _artboard, ...rest } = el;\n // A group's children render through the same pipeline as top-level elements (absolute coordinates, like the\n // live GroupElement), so they need the same wire shape — image children flattened, hidden children dropped.\n if (type === 'group' && Array.isArray(el.children)) {\n rest.children = el.children.filter((c: any) => c && c.visible !== false).map(serializeElement);\n }\n return quantizeGeometry({\n ...rest,\n type,\n });\n}\n\n/**\n * Serialize an image element, flattening transformData to top-level fields\n * as expected by SerializedImageElement and renderImageElement.\n */\nfunction serializeImageElement(el: any): any {\n const td = el.transformData || {};\n const hasMasks = hasImageMasks(el);\n\n // Visible dimensions = the element box (the FRAME), i.e. the cropped area.\n // Masks are fitted to this same box; a legacy masked image (no frame\n // marker) has its crop ignored — see core/masked-crop.ts.\n const crop = effectiveImageCrop(td, hasMasks);\n const fullWidth = td.width || 0;\n const fullHeight = td.height || 0;\n const visibleWidth = fullWidth * crop.cropWidth;\n const visibleHeight = fullHeight * crop.cropHeight;\n\n const serialized: any = {\n id: el.id,\n type: 'image' as const,\n x: el.x || 0,\n y: el.y || 0,\n width: visibleWidth,\n height: visibleHeight,\n rotation: el.rotation || 0,\n imageUrl: el.imageUrl,\n imageAspectRatio: el.imageAspectRatio,\n };\n\n // Crop (only if actually cropped — masked or not, see above)\n if (!isFullCrop(crop)) {\n // Note: crop values stay as fractions here — the server's Chrome tab\n // will convert to pixels using the actual bitmap dimensions after fetch.\n // The client doesn't have the bitmap dimensions available in onChange state.\n serialized.cropX = crop.cropX;\n serialized.cropY = crop.cropY;\n serialized.cropWidth = crop.cropWidth;\n serialized.cropHeight = crop.cropHeight;\n serialized.needsCropPixelConversion = true; // Flag for server to convert\n }\n\n // Flip and border radius\n if (td.flipHorizontal) serialized.flipHorizontal = td.flipHorizontal;\n if (td.flipVertical) serialized.flipVertical = td.flipVertical;\n if (td.borderRadius) serialized.borderRadius = td.borderRadius;\n // Drop shadow — non-image elements pass it through via the {...rest} spread;\n // this allowlist is the only place an image element's shadow could get lost.\n if (el.shadow) serialized.shadow = el.shadow;\n // Brightness/contrast/saturation — applied by the server worker's\n // renderImageElement after it fetches the bitmap. Omitted when neutral;\n // older renderers ignore the unknown field (additive wire change).\n if (!isNeutralAdjustments(td.adjustments)) serialized.adjustments = { ...td.adjustments };\n if (el.opacity !== undefined) serialized.opacity = el.opacity;\n\n // Effects\n if (el.distressEffect) serialized.distressEffect = el.distressEffect;\n if (el.masks?.length > 0) serialized.masks = el.masks;\n if (el.blendMode) serialized.blendMode = el.blendMode;\n if (el.knockoutParts) serialized.knockoutParts = el.knockoutParts;\n if (el.stroke) serialized.stroke = el.stroke;\n\n // Quantize geometry so idle render-loop float drift doesn't churn the wire state.\n return quantizeGeometry(serialized);\n}\n\n/** The render wire's version: the same number as {@link CANVAS_STATE_SCHEMA_VERSION}. */\nexport const RENDER_WIRE_VERSION = CANVAS_STATE_SCHEMA_VERSION;\n/** {@link serializeStateForServer} under the design package's name: a saved design to the render wire. */\nexport const toRenderWire = serializeStateForServer;\nexport * from './crop';\nexport { NEUTRAL_ADJUSTMENTS, isNeutralAdjustments, type ImageAdjustments } from './adjustments';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,83 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/design",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "The Snowcone design format: the versioned JSON every editor writes and the server reads. Zod schemas, validateDesign(), migrateDesign(), typed errors, and a conformance kit of golden designs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"snowcone",
|
|
7
|
+
"design",
|
|
8
|
+
"schema",
|
|
9
|
+
"zod",
|
|
10
|
+
"print-on-demand"
|
|
11
|
+
],
|
|
12
|
+
"author": "Snowcone <dev@snowcone.app>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/snowcone-app/snowcone-monorepo.git",
|
|
17
|
+
"directory": "packages/design"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/snowcone-app/snowcone-monorepo/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://developers.snowcone.app",
|
|
23
|
+
"private": false,
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"require": "./dist/index.cjs",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./wire": {
|
|
35
|
+
"types": "./dist/wire/index.d.ts",
|
|
36
|
+
"import": "./dist/wire/index.js",
|
|
37
|
+
"require": "./dist/wire/index.cjs",
|
|
38
|
+
"default": "./dist/wire/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./conformance": {
|
|
41
|
+
"types": "./dist/conformance/index.d.ts",
|
|
42
|
+
"import": "./dist/conformance/index.js",
|
|
43
|
+
"require": "./dist/conformance/index.cjs",
|
|
44
|
+
"default": "./dist/conformance/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
"wire": [
|
|
51
|
+
"./dist/wire/index.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"conformance": [
|
|
54
|
+
"./dist/conformance/index.d.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"dist",
|
|
60
|
+
"README.md",
|
|
61
|
+
"CHANGELOG.md"
|
|
62
|
+
],
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"sideEffects": false,
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"zod": "^4.1.12"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"tsup": "^8.0.1",
|
|
72
|
+
"typescript": "^5.4.5",
|
|
73
|
+
"vitest": "^2.1.9",
|
|
74
|
+
"@snowcone-app/mockup-url": "0.7.0"
|
|
75
|
+
},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "tsup",
|
|
78
|
+
"clean": "rm -rf dist",
|
|
79
|
+
"typecheck": "tsc --noEmit",
|
|
80
|
+
"test": "vitest run"
|
|
81
|
+
},
|
|
82
|
+
"module": "./dist/index.js"
|
|
6
83
|
}
|