@blitzreels/contracts 0.2.17 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/carousel-board-api.d.ts +3585 -0
  2. package/dist/carousel-board-api.d.ts.map +1 -0
  3. package/dist/carousel-board-api.js +463 -0
  4. package/dist/carousel-board-api.js.map +1 -0
  5. package/dist/carousel-board-deck.d.ts +176 -0
  6. package/dist/carousel-board-deck.d.ts.map +1 -0
  7. package/dist/carousel-board-deck.js +572 -0
  8. package/dist/carousel-board-deck.js.map +1 -0
  9. package/dist/carousel-board-generate.d.ts +305 -0
  10. package/dist/carousel-board-generate.d.ts.map +1 -0
  11. package/dist/carousel-board-generate.js +219 -0
  12. package/dist/carousel-board-generate.js.map +1 -0
  13. package/dist/carousel-board-images.d.ts +34 -0
  14. package/dist/carousel-board-images.d.ts.map +1 -0
  15. package/dist/carousel-board-images.js +263 -0
  16. package/dist/carousel-board-images.js.map +1 -0
  17. package/dist/carousel-board-layout.d.ts +57 -0
  18. package/dist/carousel-board-layout.d.ts.map +1 -0
  19. package/dist/carousel-board-layout.js +226 -0
  20. package/dist/carousel-board-layout.js.map +1 -0
  21. package/dist/carousel-board-revert.d.ts +15 -0
  22. package/dist/carousel-board-revert.d.ts.map +1 -0
  23. package/dist/carousel-board-revert.js +232 -0
  24. package/dist/carousel-board-revert.js.map +1 -0
  25. package/dist/carousel-board-templates.d.ts +42 -0
  26. package/dist/carousel-board-templates.d.ts.map +1 -0
  27. package/dist/carousel-board-templates.js +6860 -0
  28. package/dist/carousel-board-templates.js.map +1 -0
  29. package/dist/carousel-board.d.ts +5893 -0
  30. package/dist/carousel-board.d.ts.map +1 -0
  31. package/dist/carousel-board.js +741 -0
  32. package/dist/carousel-board.js.map +1 -0
  33. package/dist/carousels-contract.d.ts +2 -0
  34. package/dist/carousels-contract.d.ts.map +1 -1
  35. package/dist/carousels-contract.js +1 -1
  36. package/dist/carousels-contract.js.map +1 -1
  37. package/dist/content-intelligence-contract.d.ts +2040 -0
  38. package/dist/content-intelligence-contract.d.ts.map +1 -0
  39. package/dist/content-intelligence-contract.js +396 -0
  40. package/dist/content-intelligence-contract.js.map +1 -0
  41. package/dist/creative-composition-edit.d.ts +31 -0
  42. package/dist/creative-composition-edit.d.ts.map +1 -1
  43. package/dist/creative-composition-edit.js +49 -2
  44. package/dist/creative-composition-edit.js.map +1 -1
  45. package/dist/creative-composition-structure.d.ts.map +1 -1
  46. package/dist/creative-composition-structure.js +6 -0
  47. package/dist/creative-composition-structure.js.map +1 -1
  48. package/dist/creative-composition-types.d.ts +4 -0
  49. package/dist/creative-composition-types.d.ts.map +1 -1
  50. package/dist/creative-element-contract.d.ts.map +1 -1
  51. package/dist/creative-element-contract.js +8 -0
  52. package/dist/creative-element-contract.js.map +1 -1
  53. package/dist/creative-style-contract.d.ts +1 -0
  54. package/dist/creative-style-contract.d.ts.map +1 -1
  55. package/dist/creative-style-contract.js +4 -0
  56. package/dist/creative-style-contract.js.map +1 -1
  57. package/dist/creative-text-runs.d.ts +116 -0
  58. package/dist/creative-text-runs.d.ts.map +1 -0
  59. package/dist/creative-text-runs.js +239 -0
  60. package/dist/creative-text-runs.js.map +1 -0
  61. package/dist/editor-command-contract.d.ts +30 -0
  62. package/dist/editor-command-contract.d.ts.map +1 -1
  63. package/dist/generation-resolution.d.ts.map +1 -1
  64. package/dist/generation-resolution.js +15 -14
  65. package/dist/generation-resolution.js.map +1 -1
  66. package/dist/hook-suggestions.d.ts +116 -0
  67. package/dist/hook-suggestions.d.ts.map +1 -0
  68. package/dist/hook-suggestions.js +64 -0
  69. package/dist/hook-suggestions.js.map +1 -0
  70. package/dist/index.d.ts +11 -0
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +11 -0
  73. package/dist/index.js.map +1 -1
  74. package/dist/metered-operations.d.ts +36 -0
  75. package/dist/metered-operations.d.ts.map +1 -1
  76. package/dist/metered-operations.js +21 -0
  77. package/dist/metered-operations.js.map +1 -1
  78. package/dist/model-prices.d.ts.map +1 -1
  79. package/dist/model-prices.js +14 -2
  80. package/dist/model-prices.js.map +1 -1
  81. package/package.json +1 -1
@@ -0,0 +1,741 @@
1
+ import { z } from "zod";
2
+ import { creativeCompositionSchema } from "./creative-composition-contract.js";
3
+ import { compositionEditSchema, editComposition, } from "./creative-composition-edit.js";
4
+ import { compositionElements } from "./creative-composition-normalize.js";
5
+ export const CAROUSEL_BOARD_SLIDES_ID = "slides";
6
+ /** Root group for elements parked off the slides; rendered in the editor, never exported. */
7
+ export const CAROUSEL_BOARD_SCRATCH_ID = "scratch";
8
+ /**
9
+ * Space between slides on the editor board. Scratch elements use board-world coordinates,
10
+ * where slide i starts at x = i * (frame width + gap).
11
+ */
12
+ export const CAROUSEL_BOARD_GAP = 120;
13
+ export const CAROUSEL_BOARD_MAX_SLIDES = 35;
14
+ export const CAROUSEL_BOARD_BRAND_LOGO_SRC = "brand:logo";
15
+ const ASSET_SRC_PREFIX = "asset:";
16
+ export const CAROUSEL_BOARD_TOKENS = {
17
+ background: "--br-background",
18
+ surface: "--br-surface",
19
+ text: "--br-text",
20
+ muted: "--br-muted",
21
+ accent: "--br-accent",
22
+ onAccent: "--br-on-accent",
23
+ accentSoft: "--br-accent-soft",
24
+ accentOnDark: "--br-accent-on-dark",
25
+ glow: "--br-glow",
26
+ line: "--br-line",
27
+ headingFont: "--br-heading-font",
28
+ bodyFont: "--br-body-font",
29
+ };
30
+ export function carouselBoardToken({ token, }) {
31
+ return `var(${CAROUSEL_BOARD_TOKENS[token]})`;
32
+ }
33
+ const hexColorSchema = z
34
+ .string()
35
+ .regex(/^#[0-9a-fA-F]{6}$/, "Expected #RRGGBB");
36
+ const STATIC_PATH = /^\/(?!\/)[A-Za-z0-9._\-/]+$/;
37
+ const BOARD_ELEMENT_TYPES = new Set([
38
+ "text",
39
+ "image",
40
+ "shape",
41
+ "svg",
42
+ "group",
43
+ "chart",
44
+ "code",
45
+ ]);
46
+ /** Board media may only point at workspace assets, the brand logo or bundled static files: never a remote URL. */
47
+ export function isCarouselBoardMediaSource({ src }) {
48
+ if (src === "" || src === CAROUSEL_BOARD_BRAND_LOGO_SRC)
49
+ return true;
50
+ if (parseCarouselBoardAssetSrc({ src }) !== null)
51
+ return true;
52
+ return STATIC_PATH.test(src) && !src.includes("..");
53
+ }
54
+ function hasExternalReference({ value }) {
55
+ const lower = value.toLowerCase();
56
+ if (/url\(\s*['"]?(?!#)/.test(lower))
57
+ return true;
58
+ if (/(?:xlink:)?href\s*=\s*['"]?(?!#)/.test(lower))
59
+ return true;
60
+ if (/\son[a-z]+\s*=/.test(lower))
61
+ return true;
62
+ if (/javascript:|data:text\/html/.test(lower))
63
+ return true;
64
+ return /<\s*(script|foreignobject|image|use|iframe)\b/.test(lower);
65
+ }
66
+ function assertSafeValue({ value, path }) {
67
+ if (typeof value === "string") {
68
+ if (hasExternalReference({ value }))
69
+ throw new Error(`${path} may not reference external resources`);
70
+ return;
71
+ }
72
+ if (Array.isArray(value)) {
73
+ for (const [index, entry] of value.entries())
74
+ assertSafeValue({ value: entry, path: `${path}[${index}]` });
75
+ return;
76
+ }
77
+ if (value && typeof value === "object")
78
+ for (const [key, entry] of Object.entries(value))
79
+ assertSafeValue({ value: entry, path: `${path}.${key}` });
80
+ }
81
+ function assertSafeElement({ element }) {
82
+ if (!BOARD_ELEMENT_TYPES.has(element.type))
83
+ throw new Error(`Element "${element.id}": ${element.type} elements are not supported on carousels`);
84
+ if (element.type === "image" &&
85
+ !isCarouselBoardMediaSource({ src: element.src }))
86
+ throw new Error(`Element "${element.id}": images must use asset:<id> from the workspace library`);
87
+ const { children, content, ...fields } = element;
88
+ void content;
89
+ assertSafeValue({ value: fields, path: `Element "${element.id}"` });
90
+ for (const child of children ?? [])
91
+ assertSafeElement({ element: child });
92
+ }
93
+ export const carouselBoardThemeSchema = z
94
+ .object({
95
+ name: z.string().trim().max(80).nullable(),
96
+ background: hexColorSchema,
97
+ text: hexColorSchema,
98
+ accent: hexColorSchema,
99
+ headingFont: z.string().trim().min(1).max(120),
100
+ bodyFont: z.string().trim().min(1).max(120),
101
+ logo: z
102
+ .string()
103
+ .min(1)
104
+ .max(4000)
105
+ .refine((value) => value !== CAROUSEL_BOARD_BRAND_LOGO_SRC &&
106
+ isCarouselBoardMediaSource({ src: value }), {
107
+ message: "Logo must be a workspace asset (asset:<id>) or a bundled file path",
108
+ })
109
+ .nullable(),
110
+ })
111
+ .strict();
112
+ export const DEFAULT_CAROUSEL_BOARD_THEME = {
113
+ name: null,
114
+ background: "#0E0E0E",
115
+ text: "#FAFAFA",
116
+ accent: "#17FFA6",
117
+ headingFont: "Space Grotesk",
118
+ bodyFont: "Inter",
119
+ logo: null,
120
+ };
121
+ export const carouselBoardFrameSchema = z
122
+ .object({
123
+ width: z.number().int().min(320).max(2160),
124
+ height: z.number().int().min(320).max(3840),
125
+ })
126
+ .strict();
127
+ export const carouselBoardLayersSchema = z
128
+ .object({
129
+ hidden: z.array(z.string().min(1).max(200)).max(2000),
130
+ locked: z.array(z.string().min(1).max(200)).max(2000),
131
+ })
132
+ .strict();
133
+ export const carouselBoardSchema = z
134
+ .object({
135
+ version: z.literal(1),
136
+ frame: carouselBoardFrameSchema,
137
+ theme: carouselBoardThemeSchema,
138
+ composition: z.custom((value) => value !== null && typeof value === "object"),
139
+ layers: carouselBoardLayersSchema.optional(),
140
+ })
141
+ .strict();
142
+ /** Element ids hidden from render/export, and ids the canvas will not pick or drag. */
143
+ export function carouselBoardLayerState({ board, elementId, }) {
144
+ return {
145
+ hidden: board.layers?.hidden.includes(elementId) ?? false,
146
+ locked: board.layers?.locked.includes(elementId) ?? false,
147
+ };
148
+ }
149
+ function hexToRgb({ hex }) {
150
+ const value = Number.parseInt(hex.slice(1), 16);
151
+ return { r: (value >> 16) & 255, g: (value >> 8) & 255, b: value & 255 };
152
+ }
153
+ function rgbToHex({ r, g, b }) {
154
+ return `#${[r, g, b]
155
+ .map((part) => Math.round(Math.min(255, Math.max(0, part)))
156
+ .toString(16)
157
+ .padStart(2, "0"))
158
+ .join("")}`.toUpperCase();
159
+ }
160
+ function mixHex({ from, to, amount, }) {
161
+ const a = hexToRgb({ hex: from });
162
+ const b = hexToRgb({ hex: to });
163
+ return rgbToHex({
164
+ r: a.r + (b.r - a.r) * amount,
165
+ g: a.g + (b.g - a.g) * amount,
166
+ b: a.b + (b.b - a.b) * amount,
167
+ });
168
+ }
169
+ function luminance({ hex }) {
170
+ const { r, g, b } = hexToRgb({ hex });
171
+ const channel = (value) => {
172
+ const scaled = value / 255;
173
+ return scaled <= 0.03928
174
+ ? scaled / 12.92
175
+ : ((scaled + 0.055) / 1.055) ** 2.4;
176
+ };
177
+ return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
178
+ }
179
+ export function resolveCarouselBoardTokens({ theme, }) {
180
+ return {
181
+ background: theme.background,
182
+ surface: mixHex({ from: theme.background, to: theme.text, amount: 0.08 }),
183
+ text: theme.text,
184
+ muted: mixHex({ from: theme.text, to: theme.background, amount: 0.38 }),
185
+ accent: readableAccent({ theme }),
186
+ onAccent: luminance({ hex: readableAccent({ theme }) }) > 0.45
187
+ ? "#0E0E0E"
188
+ : "#FAFAFA",
189
+ accentSoft: mixHex({
190
+ from: readableAccent({ theme }),
191
+ to: theme.background,
192
+ amount: 0.8,
193
+ }),
194
+ line: mixHex({ from: theme.text, to: theme.background, amount: 0.82 }),
195
+ accentOnDark: readableOn({ color: theme.accent, background: "#111111" }),
196
+ glow: saturation({ hex: theme.accent }) < 0.25
197
+ ? theme.background
198
+ : readableAccent({ theme }),
199
+ headingFont: theme.headingFont,
200
+ bodyFont: theme.bodyFont,
201
+ };
202
+ }
203
+ const TOKEN_PATTERN = /var\(\s*(--br-[a-z-]+)\s*(?:,\s*([^)]*))?\)/g;
204
+ function replaceTokens({ value, values, }) {
205
+ return value.replace(TOKEN_PATTERN, (match, name, fallback) => values.get(name) ?? fallback?.trim() ?? match);
206
+ }
207
+ function resolveValue({ value, values, }) {
208
+ if (typeof value === "string")
209
+ return replaceTokens({ value, values });
210
+ if (Array.isArray(value))
211
+ return value.map((entry) => resolveValue({ value: entry, values }));
212
+ if (value && typeof value === "object")
213
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => [
214
+ key,
215
+ resolveValue({ value: entry, values }),
216
+ ]));
217
+ return value;
218
+ }
219
+ export function parseCarouselBoardAssetSrc({ src, }) {
220
+ if (!src.startsWith(ASSET_SRC_PREFIX))
221
+ return null;
222
+ const id = src.slice(ASSET_SRC_PREFIX.length);
223
+ return /^[0-9a-f-]{36}$/i.test(id) ? id.toLowerCase() : null;
224
+ }
225
+ export function carouselBoardAssetSrc({ assetId }) {
226
+ return `${ASSET_SRC_PREFIX}${assetId.toLowerCase()}`;
227
+ }
228
+ export function collectCarouselBoardAssetIds({ board, }) {
229
+ const ids = new Set();
230
+ for (const { element } of compositionElements(board.composition)) {
231
+ if (element.type !== "image")
232
+ continue;
233
+ const id = parseCarouselBoardAssetSrc({ src: element.src });
234
+ if (id)
235
+ ids.add(id);
236
+ }
237
+ const logoId = board.theme.logo
238
+ ? parseCarouselBoardAssetSrc({ src: board.theme.logo })
239
+ : null;
240
+ if (logoId)
241
+ ids.add(logoId);
242
+ return [...ids];
243
+ }
244
+ /** Stand-in for an image whose source is not available (no logo yet, unsigned asset): renders nothing. */
245
+ function emptyMedia({ element, }) {
246
+ return {
247
+ id: element.id,
248
+ type: "group",
249
+ name: element.name,
250
+ layout: element.layout,
251
+ style: { ...element.style, opacity: 0 },
252
+ children: [],
253
+ };
254
+ }
255
+ function contrastRatio({ a, b }) {
256
+ const la = luminance({ hex: a });
257
+ const lb = luminance({ hex: b });
258
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
259
+ }
260
+ function saturation({ hex }) {
261
+ const { r, g, b } = hexToRgb({ hex });
262
+ const max = Math.max(r, g, b) / 255;
263
+ const min = Math.min(r, g, b) / 255;
264
+ return max === 0 ? 0 : (max - min) / max;
265
+ }
266
+ function readableOn({ color, background, }) {
267
+ if (contrastRatio({ a: color, b: background }) >= 3)
268
+ return color;
269
+ const target = luminance({ hex: background }) < 0.4 ? "#FFFFFF" : "#000000";
270
+ for (let amount = 0.15; amount <= 1; amount += 0.05) {
271
+ const candidate = mixHex({ from: color, to: target, amount });
272
+ if (contrastRatio({ a: candidate, b: background }) >= 3)
273
+ return candidate;
274
+ }
275
+ return target;
276
+ }
277
+ /** The brand accent, pushed toward white or black until it reads on the background. */
278
+ function readableAccent({ theme }) {
279
+ if (contrastRatio({ a: theme.accent, b: theme.background }) >= 2.4)
280
+ return theme.accent;
281
+ const target = luminance({ hex: theme.background }) < 0.4 ? "#FFFFFF" : "#000000";
282
+ for (let amount = 0.15; amount <= 1; amount += 0.05) {
283
+ const candidate = mixHex({ from: theme.accent, to: target, amount });
284
+ if (contrastRatio({ a: candidate, b: theme.background }) >= 2.4)
285
+ return candidate;
286
+ }
287
+ return theme.text;
288
+ }
289
+ /** Concrete composition for rendering: brand tokens, asset references and the brand logo resolved. */
290
+ export function resolveCarouselBoardComposition({ board, assetUrls, }) {
291
+ const tokens = resolveCarouselBoardTokens({ theme: board.theme });
292
+ const values = new Map(Object.entries(tokens).map(([token, value]) => [
293
+ CAROUSEL_BOARD_TOKENS[token],
294
+ value,
295
+ ]));
296
+ const hidden = new Set(board.layers?.hidden ?? []);
297
+ const resolveElement = (element) => {
298
+ const { children: _children, ...fields } = element;
299
+ const next = {
300
+ ...element,
301
+ ...resolveValue({ value: fields, values }),
302
+ };
303
+ if (next.type === "image") {
304
+ if (next.src === CAROUSEL_BOARD_BRAND_LOGO_SRC) {
305
+ const logoAssetId = board.theme.logo
306
+ ? parseCarouselBoardAssetSrc({ src: board.theme.logo })
307
+ : null;
308
+ const logo = logoAssetId
309
+ ? (assetUrls[logoAssetId] ?? null)
310
+ : board.theme.logo;
311
+ return logo ? { ...next, src: logo } : emptyMedia({ element: next });
312
+ }
313
+ const assetId = parseCarouselBoardAssetSrc({ src: next.src });
314
+ if (assetId) {
315
+ const url = assetUrls[assetId];
316
+ return url ? { ...next, src: url } : emptyMedia({ element: next });
317
+ }
318
+ if (!next.src)
319
+ return emptyMedia({ element: next });
320
+ }
321
+ if (next.type === "group")
322
+ return {
323
+ ...next,
324
+ children: next.children
325
+ .filter((child) => !hidden.has(child.id))
326
+ .map(resolveElement),
327
+ };
328
+ return next;
329
+ };
330
+ return {
331
+ ...board.composition,
332
+ defaults: {
333
+ ...board.composition.defaults,
334
+ fontFamily: tokens.bodyFont,
335
+ color: tokens.text,
336
+ },
337
+ elements: (board.composition.elements ?? [])
338
+ .filter((element) => !hidden.has(element.id))
339
+ .map(resolveElement),
340
+ };
341
+ }
342
+ function slidesGroup({ composition, }) {
343
+ const root = composition.elements?.[0];
344
+ if (composition.mode !== "elements" ||
345
+ !root ||
346
+ root.type !== "group" ||
347
+ root.id !== CAROUSEL_BOARD_SLIDES_ID)
348
+ throw new Error(`Carousel boards keep their slides in the first root group "${CAROUSEL_BOARD_SLIDES_ID}"`);
349
+ return root;
350
+ }
351
+ export function carouselBoardSlides({ board, }) {
352
+ return slidesGroup({ composition: board.composition }).children.map((element, index) => {
353
+ if (element.type !== "group")
354
+ throw new Error(`Slide ${index + 1} must be a group`);
355
+ return { id: element.id, index, name: element.name ?? null, element };
356
+ });
357
+ }
358
+ export function carouselSlideStyle({ frame, }) {
359
+ return {
360
+ position: "relative",
361
+ width: frame.width,
362
+ height: frame.height,
363
+ overflow: "hidden",
364
+ backgroundColor: carouselBoardToken({ token: "background" }),
365
+ };
366
+ }
367
+ export function createCarouselSlide({ slideId, frame, children, }) {
368
+ return {
369
+ id: slideId,
370
+ type: "group",
371
+ style: carouselSlideStyle({ frame }),
372
+ children,
373
+ };
374
+ }
375
+ function syncBoardSize({ composition, frame, }) {
376
+ const slides = slidesGroup({ composition });
377
+ const width = frame.width * Math.max(1, slides.children.length);
378
+ slides.style = {
379
+ ...slides.style,
380
+ position: "absolute",
381
+ left: 0,
382
+ top: 0,
383
+ width,
384
+ height: frame.height,
385
+ display: "flex",
386
+ flexDirection: "row",
387
+ };
388
+ composition.width = width;
389
+ composition.height = frame.height;
390
+ const elements = composition.elements ?? [];
391
+ const scratchIndex = elements.findIndex((element) => element.id === CAROUSEL_BOARD_SCRATCH_ID);
392
+ const scratch = scratchIndex >= 0 && elements[scratchIndex]?.type === "group"
393
+ ? elements[scratchIndex]
394
+ : {
395
+ id: CAROUSEL_BOARD_SCRATCH_ID,
396
+ type: "group",
397
+ name: "Off the slides",
398
+ children: [],
399
+ };
400
+ if (scratchIndex >= 0)
401
+ elements.splice(scratchIndex, 1);
402
+ elements.push({
403
+ ...scratch,
404
+ layout: "none",
405
+ style: { position: "absolute", left: 0, top: 0, width: 0, height: 0 },
406
+ });
407
+ composition.elements = elements;
408
+ return composition;
409
+ }
410
+ export function createCarouselBoard({ frame, theme, slides, }) {
411
+ const composition = {
412
+ id: "carousel-board",
413
+ name: "Carousel",
414
+ fps: 30,
415
+ width: frame.width * Math.max(1, slides.length),
416
+ height: frame.height,
417
+ durationInFrames: 1,
418
+ mode: "elements",
419
+ metadata: { kind: "carousel" },
420
+ elements: [
421
+ {
422
+ id: CAROUSEL_BOARD_SLIDES_ID,
423
+ type: "group",
424
+ layout: "none",
425
+ children: slides,
426
+ },
427
+ ],
428
+ };
429
+ return parseCarouselBoard({
430
+ value: {
431
+ version: 1,
432
+ frame,
433
+ theme,
434
+ composition: syncBoardSize({ composition, frame }),
435
+ },
436
+ });
437
+ }
438
+ export function parseCarouselBoard({ value, }) {
439
+ const board = carouselBoardSchema.parse(value);
440
+ const composition = syncBoardSize({
441
+ composition: creativeCompositionSchema.parse(board.composition),
442
+ frame: board.frame,
443
+ });
444
+ const slides = slidesGroup({ composition });
445
+ if (slides.children.length === 0)
446
+ throw new Error("A carousel needs at least one slide");
447
+ if (slides.children.length > CAROUSEL_BOARD_MAX_SLIDES)
448
+ throw new Error(`A carousel holds at most ${CAROUSEL_BOARD_MAX_SLIDES} slides`);
449
+ for (const [index, slide] of slides.children.entries()) {
450
+ if (slide.type !== "group")
451
+ throw new Error(`Slide ${index + 1} must be a group element`);
452
+ }
453
+ for (const element of composition.elements ?? [])
454
+ assertSafeElement({ element });
455
+ assertSafeValue({
456
+ value: composition.background ?? null,
457
+ path: "Background",
458
+ });
459
+ return { ...board, composition };
460
+ }
461
+ function renameSubtree({ element, suffix, }) {
462
+ const id = `${element.id}-${suffix}`;
463
+ if (element.type === "group")
464
+ return {
465
+ ...element,
466
+ id,
467
+ children: element.children.map((child) => renameSubtree({ element: child, suffix })),
468
+ };
469
+ return { ...element, id };
470
+ }
471
+ const slideTargetSchema = z.string().min(1).max(120);
472
+ export const carouselBoardOperationSchema = z.discriminatedUnion("kind", [
473
+ z
474
+ .object({
475
+ kind: z.literal("insertSlide"),
476
+ index: z.number().int().min(0).max(CAROUSEL_BOARD_MAX_SLIDES),
477
+ slide: z.custom((value) => value !== null &&
478
+ typeof value === "object" &&
479
+ value.type === "group", "slide must be a group element"),
480
+ })
481
+ .strict(),
482
+ z
483
+ .object({
484
+ kind: z.literal("duplicateSlide"),
485
+ slideId: slideTargetSchema,
486
+ idSuffix: z
487
+ .string()
488
+ .regex(/^[a-z0-9]{4,24}$/, "Use 4-24 lowercase letters or digits"),
489
+ })
490
+ .strict(),
491
+ z
492
+ .object({
493
+ kind: z.literal("moveSlide"),
494
+ slideId: slideTargetSchema,
495
+ index: z
496
+ .number()
497
+ .int()
498
+ .min(0)
499
+ .max(CAROUSEL_BOARD_MAX_SLIDES - 1),
500
+ })
501
+ .strict(),
502
+ z
503
+ .object({ kind: z.literal("removeSlide"), slideId: slideTargetSchema })
504
+ .strict(),
505
+ z
506
+ .object({
507
+ kind: z.literal("edit"),
508
+ edit: z.preprocess((value) => value !== null && typeof value === "object" && !("ownership" in value)
509
+ ? { ownership: "preserve", ...value }
510
+ : value, compositionEditSchema),
511
+ })
512
+ .strict(),
513
+ z
514
+ .object({
515
+ kind: z.literal("setTheme"),
516
+ theme: carouselBoardThemeSchema,
517
+ })
518
+ .strict(),
519
+ z
520
+ .object({
521
+ kind: z.literal("setLayer"),
522
+ elementId: z.string().min(1).max(200),
523
+ hidden: z
524
+ .boolean()
525
+ .optional()
526
+ .describe("Omitted keeps the element's current hidden state"),
527
+ locked: z
528
+ .boolean()
529
+ .optional()
530
+ .describe("Omitted keeps the element's current locked state"),
531
+ })
532
+ .strict()
533
+ .refine((operation) => operation.hidden !== undefined || operation.locked !== undefined, { message: "Supply hidden, locked or both" }),
534
+ ]);
535
+ export const carouselBoardEditSchema = z
536
+ .array(carouselBoardOperationSchema)
537
+ .min(1)
538
+ .max(200);
539
+ export function editCarouselBoard({ board, operations, }) {
540
+ let next = structuredClone(parseCarouselBoard({ value: board }));
541
+ const affected = new Set();
542
+ for (const operation of carouselBoardEditSchema.parse(operations)) {
543
+ if (operation.kind === "setTheme") {
544
+ next = { ...next, theme: operation.theme };
545
+ continue;
546
+ }
547
+ if (operation.kind === "setLayer") {
548
+ if (!boardElementIds({ composition: next.composition }).has(operation.elementId))
549
+ throw new Error(`Element "${operation.elementId}" not found`);
550
+ if ((operation.hidden || operation.locked) &&
551
+ isStructuralBoardElement({
552
+ composition: next.composition,
553
+ elementId: operation.elementId,
554
+ }))
555
+ throw new Error(`"${operation.elementId}" is a slide or a board container; hide or lock the elements inside it, or remove the slide`);
556
+ const without = (ids) => ids.filter((id) => id !== operation.elementId);
557
+ const current = next.layers ?? { hidden: [], locked: [] };
558
+ const flag = ({ ids, value, }) => {
559
+ if (value === undefined)
560
+ return ids;
561
+ return value ? [...without(ids), operation.elementId] : without(ids);
562
+ };
563
+ next = {
564
+ ...next,
565
+ layers: {
566
+ hidden: flag({ ids: current.hidden, value: operation.hidden }),
567
+ locked: flag({ ids: current.locked, value: operation.locked }),
568
+ },
569
+ };
570
+ affected.add(operation.elementId);
571
+ continue;
572
+ }
573
+ if (operation.kind === "edit") {
574
+ const result = editComposition({
575
+ composition: next.composition,
576
+ edit: operation.edit,
577
+ });
578
+ for (const id of result.affectedElementIds)
579
+ affected.add(id);
580
+ next = { ...next, composition: result.composition };
581
+ continue;
582
+ }
583
+ const slides = slidesGroup({ composition: next.composition });
584
+ const position = (slideId) => {
585
+ const index = slides.children.findIndex((slide) => slide.id === slideId);
586
+ if (index < 0)
587
+ throw new Error(`Slide "${slideId}" not found`);
588
+ return index;
589
+ };
590
+ if (operation.kind === "insertSlide") {
591
+ if (slides.children.length >= CAROUSEL_BOARD_MAX_SLIDES)
592
+ throw new Error(`A carousel holds at most ${CAROUSEL_BOARD_MAX_SLIDES} slides`);
593
+ const slide = {
594
+ ...operation.slide,
595
+ style: {
596
+ ...carouselSlideStyle({ frame: next.frame }),
597
+ ...pickBackground({ slide: operation.slide }),
598
+ },
599
+ };
600
+ slides.children.splice(Math.min(operation.index, slides.children.length), 0, slide);
601
+ affected.add(slide.id);
602
+ }
603
+ else if (operation.kind === "duplicateSlide") {
604
+ const index = position(operation.slideId);
605
+ const source = slides.children[index];
606
+ if (!source)
607
+ throw new Error(`Slide "${operation.slideId}" not found`);
608
+ const copy = renameSubtree({
609
+ element: source,
610
+ suffix: operation.idSuffix,
611
+ });
612
+ slides.children.splice(index + 1, 0, copy);
613
+ affected.add(copy.id);
614
+ }
615
+ else if (operation.kind === "moveSlide") {
616
+ const index = position(operation.slideId);
617
+ const [slide] = slides.children.splice(index, 1);
618
+ if (!slide)
619
+ throw new Error(`Slide "${operation.slideId}" not found`);
620
+ slides.children.splice(Math.min(operation.index, slides.children.length), 0, slide);
621
+ affected.add(slide.id);
622
+ }
623
+ else if (operation.kind === "removeSlide") {
624
+ if (slides.children.length === 1)
625
+ throw new Error("A carousel needs at least one slide");
626
+ const [removed] = slides.children.splice(position(operation.slideId), 1);
627
+ if (removed)
628
+ affected.add(removed.id);
629
+ }
630
+ syncBoardSize({ composition: next.composition, frame: next.frame });
631
+ }
632
+ syncBoardSize({ composition: next.composition, frame: next.frame });
633
+ if (next.layers) {
634
+ const existing = boardElementIds({ composition: next.composition });
635
+ const hidden = next.layers.hidden.filter((id) => existing.has(id));
636
+ const locked = next.layers.locked.filter((id) => existing.has(id));
637
+ const { layers: _layers, ...rest } = next;
638
+ next =
639
+ hidden.length === 0 && locked.length === 0
640
+ ? rest
641
+ : { ...rest, layers: { hidden, locked } };
642
+ }
643
+ return {
644
+ board: parseCarouselBoard({ value: next }),
645
+ affectedElementIds: [...affected],
646
+ };
647
+ }
648
+ function isStructuralBoardElement({ composition, elementId, }) {
649
+ if (elementId === CAROUSEL_BOARD_SLIDES_ID ||
650
+ elementId === CAROUSEL_BOARD_SCRATCH_ID)
651
+ return true;
652
+ return slidesGroup({ composition }).children.some((slide) => slide.id === elementId);
653
+ }
654
+ function boardElementIds({ composition, }) {
655
+ const ids = new Set();
656
+ const visit = (element) => {
657
+ ids.add(element.id);
658
+ if (element.type === "group")
659
+ for (const child of element.children)
660
+ visit(child);
661
+ };
662
+ for (const element of composition.elements ?? [])
663
+ visit(element);
664
+ return ids;
665
+ }
666
+ function pickBackground({ slide, }) {
667
+ const color = slide.style?.backgroundColor;
668
+ return color ? { backgroundColor: color } : {};
669
+ }
670
+ /** Board-space x of a slide's left edge. */
671
+ export function carouselSlideOffset({ board, slideId, }) {
672
+ const index = carouselBoardSlides({ board }).findIndex((slide) => slide.id === slideId);
673
+ if (index < 0)
674
+ throw new Error(`Slide "${slideId}" not found`);
675
+ return index * board.frame.width;
676
+ }
677
+ export const carouselBoardRestoreInputSchema = z
678
+ .object({
679
+ projectId: z.string().uuid(),
680
+ expectedRevision: z.number().int().min(0),
681
+ board: carouselBoardSchema,
682
+ })
683
+ .strict();
684
+ /** Root elements outside the slides row; they render across slide edges. */
685
+ export function carouselBoardSpanningElements({ composition, }) {
686
+ return (composition.elements ?? [])
687
+ .slice(1)
688
+ .filter((element) => element.id !== CAROUSEL_BOARD_SCRATCH_ID);
689
+ }
690
+ /** Elements parked off the slides, in board-world coordinates. */
691
+ export function carouselBoardScratchElements({ composition, }) {
692
+ const scratch = (composition.elements ?? []).find((element) => element.id === CAROUSEL_BOARD_SCRATCH_ID);
693
+ return scratch?.type === "group" ? scratch.children : [];
694
+ }
695
+ /** Board-world x of a slide's left edge (slides sit in one row separated by the board gap). */
696
+ export function carouselSlideWorldLeft({ frame, index, }) {
697
+ return index * (frame.width + CAROUSEL_BOARD_GAP);
698
+ }
699
+ /**
700
+ * One slide as a frame-sized composition: the slide group at the origin plus
701
+ * spanning elements shifted by the slide's offset. Editor tiles and exports render this.
702
+ */
703
+ export function sliceCarouselBoardComposition({ board, composition, slideId, }) {
704
+ const slides = slidesGroup({ composition });
705
+ const index = slides.children.findIndex((slide) => slide.id === slideId);
706
+ const slide = slides.children[index];
707
+ if (slide?.type !== "group")
708
+ throw new Error(`Slide "${slideId}" not found`);
709
+ const spanning = carouselBoardSpanningElements({ composition });
710
+ return {
711
+ ...composition,
712
+ id: `${composition.id}-${slideId}`,
713
+ width: board.frame.width,
714
+ height: board.frame.height,
715
+ elements: [
716
+ {
717
+ ...slide,
718
+ layout: "none",
719
+ style: { ...slide.style, position: "absolute", left: 0, top: 0 },
720
+ },
721
+ ...(spanning.length > 0
722
+ ? [
723
+ {
724
+ id: `${slideId}--spanning`,
725
+ type: "group",
726
+ layout: "none",
727
+ style: {
728
+ position: "absolute",
729
+ left: -index * board.frame.width,
730
+ top: 0,
731
+ width: board.frame.width * slides.children.length,
732
+ height: board.frame.height,
733
+ },
734
+ children: spanning,
735
+ },
736
+ ]
737
+ : []),
738
+ ],
739
+ };
740
+ }
741
+ //# sourceMappingURL=carousel-board.js.map