@polotno/schema 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/defaults.d.ts +20 -0
- package/lib/defaults.js +30 -0
- package/lib/design.schema.json +5 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.js +2 -1
- package/lib/migrate.d.ts +21 -0
- package/lib/migrate.js +133 -0
- package/lib/normalize.js +18 -19
- package/lib/schema.d.ts +5 -1
- package/lib/schema.js +492 -122
- package/lib/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,8 +26,8 @@ npm install @polotno/schema
|
|
|
26
26
|
```ts
|
|
27
27
|
import { validateDesign, assertValidDesign } from '@polotno/schema';
|
|
28
28
|
|
|
29
|
-
validateDesign(json, { mode: 'partial' });
|
|
30
|
-
validateDesign(json, { mode: 'canonical' });
|
|
29
|
+
validateDesign(json, { mode: 'partial' }); // lenient — defaults may be missing
|
|
30
|
+
validateDesign(json, { mode: 'canonical' }); // strict — rejects unknown keys
|
|
31
31
|
// → { valid: boolean, errors: { path: string, message: string }[] }
|
|
32
32
|
|
|
33
33
|
assertValidDesign(json); // throws on invalid
|
package/lib/defaults.d.ts
CHANGED
|
@@ -16,6 +16,26 @@ export declare const resolveTableCellBorder: (cell: Record<string, any>, element
|
|
|
16
16
|
style: string;
|
|
17
17
|
color: string;
|
|
18
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* How many pixels each border eats from a cell's interior — the inset between
|
|
21
|
+
* the cell rect and the box its text is laid out in.
|
|
22
|
+
*
|
|
23
|
+
* Outer perimeter borders are fully inset (the whole stroke lies inside the
|
|
24
|
+
* cell); inner gridlines are centred on the shared boundary, so only half the
|
|
25
|
+
* stroke intrudes and the other half lives in the neighbour. `cell` must carry
|
|
26
|
+
* its grid position (`row`, `col`, and the spans when it spans).
|
|
27
|
+
*
|
|
28
|
+
* Mirrors the editor's `TableCell.borderInsets` (polotno's table-model.ts),
|
|
29
|
+
* which is what the canvas places cell text with — that model still owns its
|
|
30
|
+
* own copy because it resolves borders through MST views with an isAlive
|
|
31
|
+
* guard.
|
|
32
|
+
*/
|
|
33
|
+
export declare const tableCellBorderInsets: (cell: Record<string, any>, element: Record<string, any>) => {
|
|
34
|
+
top: number;
|
|
35
|
+
right: number;
|
|
36
|
+
bottom: number;
|
|
37
|
+
left: number;
|
|
38
|
+
};
|
|
19
39
|
export declare const TEXT_DEFAULTS: {
|
|
20
40
|
readonly fontSize: 14;
|
|
21
41
|
readonly fontFamily: "Roboto";
|
package/lib/defaults.js
CHANGED
|
@@ -54,6 +54,36 @@ export const resolveTableCellBorder = (cell, element, side) => {
|
|
|
54
54
|
color: overrides?.color ?? element.borderColor,
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* How many pixels each border eats from a cell's interior — the inset between
|
|
59
|
+
* the cell rect and the box its text is laid out in.
|
|
60
|
+
*
|
|
61
|
+
* Outer perimeter borders are fully inset (the whole stroke lies inside the
|
|
62
|
+
* cell); inner gridlines are centred on the shared boundary, so only half the
|
|
63
|
+
* stroke intrudes and the other half lives in the neighbour. `cell` must carry
|
|
64
|
+
* its grid position (`row`, `col`, and the spans when it spans).
|
|
65
|
+
*
|
|
66
|
+
* Mirrors the editor's `TableCell.borderInsets` (polotno's table-model.ts),
|
|
67
|
+
* which is what the canvas places cell text with — that model still owns its
|
|
68
|
+
* own copy because it resolves borders through MST views with an isAlive
|
|
69
|
+
* guard.
|
|
70
|
+
*/
|
|
71
|
+
export const tableCellBorderInsets = (cell, element) => {
|
|
72
|
+
const rowSpan = cell.rowSpan || 1;
|
|
73
|
+
const colSpan = cell.colSpan || 1;
|
|
74
|
+
const inset = (side, isOuter) => {
|
|
75
|
+
const b = resolveTableCellBorder(cell, element, side);
|
|
76
|
+
if (b.style === 'none' || !b.width)
|
|
77
|
+
return 0;
|
|
78
|
+
return isOuter ? b.width : b.width / 2;
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
top: inset('top', cell.row === 0),
|
|
82
|
+
bottom: inset('bottom', cell.row + rowSpan === element.rows),
|
|
83
|
+
left: inset('left', cell.col === 0),
|
|
84
|
+
right: inset('right', cell.col + colSpan === element.cols),
|
|
85
|
+
};
|
|
86
|
+
};
|
|
57
87
|
export const TEXT_DEFAULTS = {
|
|
58
88
|
fontSize: 14,
|
|
59
89
|
fontFamily: 'Roboto',
|
package/lib/design.schema.json
CHANGED
|
@@ -175,6 +175,11 @@
|
|
|
175
175
|
"type": "string",
|
|
176
176
|
"description": "Unique identifier for the audio track."
|
|
177
177
|
},
|
|
178
|
+
"name": {
|
|
179
|
+
"default": "",
|
|
180
|
+
"description": "Display name shown on the timeline.",
|
|
181
|
+
"type": "string"
|
|
182
|
+
},
|
|
178
183
|
"src": {
|
|
179
184
|
"default": "",
|
|
180
185
|
"description": "URL or data URI of the audio file.",
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export type { PolotnoError, PolotnoErrorCode } from './errors.js';
|
|
2
2
|
export { normalizeDesign, parseDesign } from './normalize.js';
|
|
3
3
|
export type { ParseOptions } from './normalize.js';
|
|
4
|
+
export { migrateDesign } from './migrate.js';
|
|
5
|
+
export type { MigrateOptions } from './migrate.js';
|
|
4
6
|
export { resolveBleed, ZERO_BLEED } from './bleed.js';
|
|
5
7
|
export type { ResolvedBleed } from './bleed.js';
|
|
6
|
-
export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, } from './defaults.js';
|
|
8
|
+
export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, tableCellBorderInsets, } from './defaults.js';
|
|
7
9
|
export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
|
|
8
10
|
export type { DesignInput, ValidationError, ValidationResult, ValidateOptions, } from './schema.js';
|
|
9
11
|
export type * from './types.js';
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// the generated JSON Schema; `./types` are the hand-written public TypeScript
|
|
4
4
|
// types. zod is a regular dependency (normalize parses with it).
|
|
5
5
|
export { normalizeDesign, parseDesign } from './normalize.js';
|
|
6
|
+
export { migrateDesign } from './migrate.js';
|
|
6
7
|
export { resolveBleed, ZERO_BLEED } from './bleed.js';
|
|
7
8
|
// Also available zod-free via the `@polotno/schema/defaults` subpath.
|
|
8
|
-
export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, } from './defaults.js';
|
|
9
|
+
export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, tableCellBorderInsets, } from './defaults.js';
|
|
9
10
|
export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
|
package/lib/migrate.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface MigrateOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Whether the consumer renders text with the modern engines (HTML /
|
|
4
|
+
* render-tag). Two migrations only make sense there, and the editor can still
|
|
5
|
+
* be switched to the legacy Konva.Text renderer. Defaults to true because
|
|
6
|
+
* every converter uses the modern path; the editor passes its actual flag.
|
|
7
|
+
*/
|
|
8
|
+
modernTextRenderer?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Skip the v2→3 `legacyBackground` stamp, so a pre-v3 text background renders
|
|
11
|
+
* with the new per-line polygon instead of being frozen as a full rect.
|
|
12
|
+
* Default false — preserve the original visuals.
|
|
13
|
+
*/
|
|
14
|
+
modernizeTextBackground?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Bring a design up to `CURRENT_SCHEMA_VERSION`. Returns a new object; the
|
|
18
|
+
* input is never mutated. A design already at the current version is returned
|
|
19
|
+
* unchanged apart from the clone.
|
|
20
|
+
*/
|
|
21
|
+
export declare function migrateDesign<T>(json: T, options?: MigrateOptions): T;
|
package/lib/migrate.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Design-format migrations, one version step per block.
|
|
2
|
+
//
|
|
3
|
+
// These used to live inside `polotno`'s `store.loadJSON`, which meant only the
|
|
4
|
+
// editor ran them: every converter received raw JSON and rendered a pre-v3
|
|
5
|
+
// document with post-v3 semantics. A design's meaning must not depend on which
|
|
6
|
+
// package opened it, so the migrations live here, in the layer below everything.
|
|
7
|
+
//
|
|
8
|
+
// Two rules keep this file honest:
|
|
9
|
+
//
|
|
10
|
+
// 1. **Pure JSON in, pure JSON out.** No DOM, no measurement, no MST. Schema is
|
|
11
|
+
// the bottom layer and dependency-free, and migrations run before any model
|
|
12
|
+
// or renderer exists. A change that can only be decided by measuring text
|
|
13
|
+
// is not a migration — stamp intent instead and let each renderer resolve it
|
|
14
|
+
// at paint time. `legacyBackground` (v2→3) is exactly that pattern: it
|
|
15
|
+
// records "this predates the per-line background" rather than baking in a
|
|
16
|
+
// rect, and every renderer branches on it.
|
|
17
|
+
// 2. **A missing `schemaVersion` means 0**, the oldest format — not the current
|
|
18
|
+
// one. Designs written before the field existed are precisely the ones that
|
|
19
|
+
// need migrating, so absence has to mean "old". `store.toJSON()` always
|
|
20
|
+
// stamps the current version, so a round-tripped design is never re-migrated.
|
|
21
|
+
import { CURRENT_SCHEMA_VERSION } from './schema.js';
|
|
22
|
+
import { TEXT_DEFAULTS } from './defaults.js';
|
|
23
|
+
/** Named effects that never used the -100..100 intensity range. */
|
|
24
|
+
const UNSCALED_FILTERS = ['warm', 'cold', 'natural'];
|
|
25
|
+
/**
|
|
26
|
+
* Local element walk. Schema sits below core, so it cannot use core's
|
|
27
|
+
* `forEveryNode` — but it visits the same element nodes: nested `children`
|
|
28
|
+
* recursively AND table `cells` one level deep. Miss the cells and a table's
|
|
29
|
+
* text silently skips every migration while the rest of the design gets it.
|
|
30
|
+
*
|
|
31
|
+
* Deliberately NOT the page objects themselves, which `forEveryNode` does hand
|
|
32
|
+
* to its callback (it treats pages as the root's children). Every migration
|
|
33
|
+
* here is element-scoped and no page-level key exists to migrate, so a page
|
|
34
|
+
* would only ever be a node the callback ignores. A future page-level migration
|
|
35
|
+
* needs its own `data.pages` loop — do not widen this one.
|
|
36
|
+
*/
|
|
37
|
+
function forEveryElement(pages, cb) {
|
|
38
|
+
if (!Array.isArray(pages))
|
|
39
|
+
return;
|
|
40
|
+
const walk = (node) => {
|
|
41
|
+
if (Array.isArray(node.children)) {
|
|
42
|
+
for (const child of node.children) {
|
|
43
|
+
if (!child || typeof child !== 'object')
|
|
44
|
+
continue;
|
|
45
|
+
cb(child);
|
|
46
|
+
walk(child);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(node.cells)) {
|
|
50
|
+
for (const cell of node.cells) {
|
|
51
|
+
if (cell && typeof cell === 'object')
|
|
52
|
+
cb(cell);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
for (const page of pages) {
|
|
57
|
+
if (page && typeof page === 'object')
|
|
58
|
+
walk(page);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Bring a design up to `CURRENT_SCHEMA_VERSION`. Returns a new object; the
|
|
63
|
+
* input is never mutated. A design already at the current version is returned
|
|
64
|
+
* unchanged apart from the clone.
|
|
65
|
+
*/
|
|
66
|
+
export function migrateDesign(json, options = {}) {
|
|
67
|
+
const { modernTextRenderer = true, modernizeTextBackground = false } = options;
|
|
68
|
+
// Guard BEFORE the round-trip: JSON.parse(JSON.stringify(undefined)) throws a
|
|
69
|
+
// raw SyntaxError, and a circular object throws TypeError. Non-designs must
|
|
70
|
+
// reach the zod pass untouched so they surface as a structured
|
|
71
|
+
// DESIGN_INVALID, per the error contract in the root CLAUDE.md.
|
|
72
|
+
if (!json || typeof json !== 'object')
|
|
73
|
+
return json;
|
|
74
|
+
const data = JSON.parse(JSON.stringify(json));
|
|
75
|
+
const from = data.schemaVersion || 0;
|
|
76
|
+
if (from < 1 && modernTextRenderer) {
|
|
77
|
+
// letterSpacing used to be applied as `rem` in the HTML render, which does
|
|
78
|
+
// not match the canvas. It became `letterSpacing * fontSize` in px, so the
|
|
79
|
+
// stored ratio has to be rebased from the 16px root font size.
|
|
80
|
+
const ROOT_FONT_PX = 16;
|
|
81
|
+
forEveryElement(data.pages, (element) => {
|
|
82
|
+
if (element.type !== 'text')
|
|
83
|
+
return;
|
|
84
|
+
// Guard the arithmetic: an element that never stored letterSpacing would
|
|
85
|
+
// otherwise be stamped with NaN, which serializes to null and fails
|
|
86
|
+
// validation downstream. Number.isFinite (not the global) so a
|
|
87
|
+
// stringly-typed value is rejected rather than coerced.
|
|
88
|
+
const { letterSpacing } = element;
|
|
89
|
+
if (!Number.isFinite(letterSpacing))
|
|
90
|
+
return;
|
|
91
|
+
// `fontSize` is OPTIONAL in raw JSON — the schema fills it from
|
|
92
|
+
// TEXT_DEFAULTS, but that happens AFTER migration, so reading it here
|
|
93
|
+
// finds nothing. Mirror the default instead of bailing: bailing leaves
|
|
94
|
+
// the rem-based value in place and the text renders with wrong spacing,
|
|
95
|
+
// silently. Division by zero is still a bail — no default rescues that.
|
|
96
|
+
const fontSize = Number.isFinite(element.fontSize)
|
|
97
|
+
? element.fontSize
|
|
98
|
+
: TEXT_DEFAULTS.fontSize;
|
|
99
|
+
if (fontSize === 0)
|
|
100
|
+
return;
|
|
101
|
+
element.letterSpacing = (letterSpacing * ROOT_FONT_PX) / fontSize;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (from < 2) {
|
|
105
|
+
// Filter intensity moved from a -100..100 range to -1..1.
|
|
106
|
+
forEveryElement(data.pages, (element) => {
|
|
107
|
+
if (!element.filters)
|
|
108
|
+
return;
|
|
109
|
+
for (const filterType of Object.keys(element.filters)) {
|
|
110
|
+
// warm / cold / natural never used the numeric range.
|
|
111
|
+
if (UNSCALED_FILTERS.includes(filterType))
|
|
112
|
+
continue;
|
|
113
|
+
const filter = element.filters[filterType];
|
|
114
|
+
if (filter && typeof filter.intensity === 'number') {
|
|
115
|
+
filter.intensity = filter.intensity / 100;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (from < 3 && modernTextRenderer && !modernizeTextBackground) {
|
|
121
|
+
// Freeze pre-v3 rich-text backgrounds as full rects, so the old render is
|
|
122
|
+
// preserved exactly. See rule 2 in the header: intent, not geometry.
|
|
123
|
+
forEveryElement(data.pages, (element) => {
|
|
124
|
+
if (element.type === 'text' && element.backgroundEnabled) {
|
|
125
|
+
element.legacyBackground = true;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
// v3→4 is a marker only: v4 is the video time-anchor render change. Nothing
|
|
130
|
+
// to transform, but the bump lets a future migration target pre-v4 designs.
|
|
131
|
+
data.schemaVersion = CURRENT_SCHEMA_VERSION;
|
|
132
|
+
return data;
|
|
133
|
+
}
|
package/lib/normalize.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
// normalizeDesign = drop nulls -> zod parse (fills defaults, strips
|
|
2
|
-
// keys).
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// missing schemaVersion defaults to the current version; old documents that
|
|
6
|
-
// need migrating should round-trip through the core `polotno` store.
|
|
1
|
+
// normalizeDesign = migrate -> drop nulls -> zod parse (fills defaults, strips
|
|
2
|
+
// unknown keys). The migration step is what makes a design mean the same thing
|
|
3
|
+
// in every package; migrate.ts explains why it lives here and why a missing
|
|
4
|
+
// `schemaVersion` counts as 0.
|
|
7
5
|
import { designSchemaLenient, parseValidDesign, cloneDropNulls, ELEMENT_TYPES, } from './schema.js';
|
|
8
6
|
import { polotnoError } from './errors.js';
|
|
7
|
+
import { migrateDesign } from './migrate.js';
|
|
9
8
|
export function normalizeDesign(json) {
|
|
10
|
-
return designSchemaLenient.parse(cloneDropNulls(json));
|
|
9
|
+
return designSchemaLenient.parse(cloneDropNulls(migrateDesign(json)));
|
|
11
10
|
}
|
|
12
11
|
// Fill missing `id`s with DETERMINISTIC structural ids — the index path from
|
|
13
12
|
// the document root (`page-0`, `el-0-2`, nested `el-0-2-1`, `cell-0-2-3`).
|
|
@@ -184,28 +183,28 @@ function restoreClientData(original, design) {
|
|
|
184
183
|
*/
|
|
185
184
|
export function parseDesign(input, opts = {}) {
|
|
186
185
|
const original = input;
|
|
186
|
+
// Migrate first, so the pre-transforms and the validation below see the
|
|
187
|
+
// current format. The result is ALSO the working copy they mutate in place:
|
|
188
|
+
// migrateDesign deep-clones via a JSON round-trip, which is not just a copy —
|
|
189
|
+
// it coerces NaN/Infinity to null exactly like real store serialization, and
|
|
190
|
+
// exporter tolerance tests pin that (pdf dpi.test.js). So the caller's input
|
|
191
|
+
// is never touched and nothing needs cloning again below.
|
|
192
|
+
const migrated = migrateDesign(input);
|
|
187
193
|
const pre = (opts.preserveClientData || opts.fillMissingIds) &&
|
|
188
|
-
|
|
189
|
-
typeof
|
|
194
|
+
migrated &&
|
|
195
|
+
typeof migrated === 'object';
|
|
190
196
|
let extracted = [];
|
|
191
197
|
if (pre) {
|
|
192
|
-
// ONE working copy for all pre-transforms; the walkers mutate it in
|
|
193
|
-
// place. The caller's input is never touched. The JSON round-trip is NOT
|
|
194
|
-
// just a copy — it coerces non-JSON values (NaN/Infinity → null, which
|
|
195
|
-
// the parse then treats as "unset") exactly like real store JSON
|
|
196
|
-
// serialization; exporter tolerance tests pin that (pdf dpi.test.js).
|
|
197
|
-
// Don't replace it with cloneDropNulls to save the second traversal.
|
|
198
|
-
input = JSON.parse(JSON.stringify(input));
|
|
199
198
|
// fillIds first so unknown elements get ids too, numbered off the full
|
|
200
199
|
// tree (extraction would hide them from the structural paths).
|
|
201
200
|
if (opts.fillMissingIds)
|
|
202
|
-
fillIds(
|
|
201
|
+
fillIds(migrated);
|
|
203
202
|
if (opts.preserveClientData)
|
|
204
|
-
extracted = extractUnknownElements(
|
|
203
|
+
extracted = extractUnknownElements(migrated);
|
|
205
204
|
}
|
|
206
205
|
// Single schema pass: validates (drops null-valued fields, so it agrees
|
|
207
206
|
// with the core store on what's valid) and fills defaults in one parse.
|
|
208
|
-
const design = parseValidDesign(
|
|
207
|
+
const design = parseValidDesign(migrated, opts);
|
|
209
208
|
if (pre && opts.preserveClientData) {
|
|
210
209
|
if (extracted.length)
|
|
211
210
|
spliceUnknownElements(design, extracted);
|
package/lib/schema.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export declare const designSchemaLenient: z.ZodObject<{
|
|
|
40
40
|
}, z.core.$strip>>>;
|
|
41
41
|
audios: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
42
42
|
id: z.ZodString;
|
|
43
|
+
name: z.ZodDefault<z.ZodString>;
|
|
43
44
|
src: z.ZodDefault<z.ZodString>;
|
|
44
45
|
duration: z.ZodDefault<z.ZodNumber>;
|
|
45
46
|
startTime: z.ZodDefault<z.ZodNumber>;
|
|
@@ -99,6 +100,7 @@ export declare const designSchemaCanonical: z.ZodObject<{
|
|
|
99
100
|
}, z.core.$strip>>>;
|
|
100
101
|
audios: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
101
102
|
id: z.ZodString;
|
|
103
|
+
name: z.ZodDefault<z.ZodString>;
|
|
102
104
|
src: z.ZodDefault<z.ZodString>;
|
|
103
105
|
duration: z.ZodDefault<z.ZodNumber>;
|
|
104
106
|
startTime: z.ZodDefault<z.ZodNumber>;
|
|
@@ -683,6 +685,7 @@ export declare const leafSchemas: {
|
|
|
683
685
|
}, z.core.$strip>;
|
|
684
686
|
audio: z.ZodObject<{
|
|
685
687
|
id: z.ZodString;
|
|
688
|
+
name: z.ZodDefault<z.ZodString>;
|
|
686
689
|
src: z.ZodDefault<z.ZodString>;
|
|
687
690
|
duration: z.ZodDefault<z.ZodNumber>;
|
|
688
691
|
startTime: z.ZodDefault<z.ZodNumber>;
|
|
@@ -710,7 +713,8 @@ export declare function assertValidDesign(input: unknown, opts?: ValidateOptions
|
|
|
710
713
|
/**
|
|
711
714
|
* Validate AND normalize in a single schema pass (safeParse both raises the
|
|
712
715
|
* same DESIGN_INVALID as `assertValidDesign` and returns the defaults-filled
|
|
713
|
-
* result)
|
|
716
|
+
* result). The only other copy on this path is the one `migrateDesign` makes,
|
|
717
|
+
* which parseDesign reuses as its working copy rather than cloning again.
|
|
714
718
|
* For canonical mode the single pass is equally faithful: input that passes
|
|
715
719
|
* the strict schema has no unknown keys, so its parse result equals the
|
|
716
720
|
* lenient one.
|
package/lib/schema.js
CHANGED
|
@@ -40,104 +40,262 @@ export const ELEMENT_TYPES = [
|
|
|
40
40
|
// break `z.infer` of the leaf schemas (and thus the type-drift guard).
|
|
41
41
|
function build(o) {
|
|
42
42
|
const animation = o({
|
|
43
|
-
delay: z
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
delay: z
|
|
44
|
+
.number()
|
|
45
|
+
.default(0)
|
|
46
|
+
.describe('Delay before the animation starts, in milliseconds.'),
|
|
47
|
+
duration: z
|
|
48
|
+
.number()
|
|
49
|
+
.default(500)
|
|
50
|
+
.describe('Animation duration in milliseconds.'),
|
|
51
|
+
enabled: z
|
|
52
|
+
.boolean()
|
|
53
|
+
.default(true)
|
|
54
|
+
.describe('Whether the animation is enabled.'),
|
|
46
55
|
type: z.enum(['enter', 'exit', 'loop']).describe('Animation phase.'),
|
|
47
56
|
name: z.string().default('none').describe('Animation name identifier.'),
|
|
48
57
|
data: z.unknown().default({}).describe('Animation-specific data.'),
|
|
49
58
|
});
|
|
50
59
|
const filters = z
|
|
51
|
-
.record(z.string(), o({
|
|
60
|
+
.record(z.string(), o({
|
|
61
|
+
intensity: z
|
|
62
|
+
.number()
|
|
63
|
+
.default(1)
|
|
64
|
+
.describe('Filter intensity (-1 to 1).'),
|
|
65
|
+
}))
|
|
52
66
|
.describe('Applied image filters keyed by filter name.');
|
|
53
67
|
// Fields shared by every Shape-derived element. Defaults inline.
|
|
54
68
|
const shapeBase = {
|
|
55
69
|
id: z.string().describe('Unique identifier for the element.'),
|
|
56
70
|
type: z.string().describe('Element type identifier.'),
|
|
57
71
|
name: z.string().default('').describe('Display name for the element.'),
|
|
58
|
-
custom: z
|
|
72
|
+
custom: z
|
|
73
|
+
.unknown()
|
|
74
|
+
.optional()
|
|
75
|
+
.describe('Custom data attached to the element.'),
|
|
59
76
|
x: z.number().default(0).describe('X position in pixels.'),
|
|
60
77
|
y: z.number().default(0).describe('Y position in pixels.'),
|
|
61
78
|
width: z.number().default(100).describe('Width in pixels.'),
|
|
62
79
|
height: z.number().default(100).describe('Height in pixels.'),
|
|
63
80
|
rotation: z.number().default(0).describe('Rotation angle in degrees.'),
|
|
64
81
|
opacity: z.number().default(1).describe('Opacity level (0-1).'),
|
|
65
|
-
animations: z
|
|
66
|
-
|
|
82
|
+
animations: z
|
|
83
|
+
.array(animation)
|
|
84
|
+
.default([])
|
|
85
|
+
.describe('Animation configurations.'),
|
|
86
|
+
blurEnabled: z
|
|
87
|
+
.boolean()
|
|
88
|
+
.default(false)
|
|
89
|
+
.describe('Whether blur effect is enabled.'),
|
|
67
90
|
blurRadius: z.number().default(10).describe('Blur radius in pixels.'),
|
|
68
|
-
brightnessEnabled: z
|
|
91
|
+
brightnessEnabled: z
|
|
92
|
+
.boolean()
|
|
93
|
+
.default(false)
|
|
94
|
+
.describe('Whether brightness effect is enabled.'),
|
|
69
95
|
brightness: z.number().default(0).describe('Brightness level (-1 to 1).'),
|
|
70
|
-
sepiaEnabled: z
|
|
71
|
-
|
|
96
|
+
sepiaEnabled: z
|
|
97
|
+
.boolean()
|
|
98
|
+
.default(false)
|
|
99
|
+
.describe('Whether sepia effect is enabled.'),
|
|
100
|
+
grayscaleEnabled: z
|
|
101
|
+
.boolean()
|
|
102
|
+
.default(false)
|
|
103
|
+
.describe('Whether grayscale effect is enabled.'),
|
|
72
104
|
filters: filters.default({}),
|
|
73
|
-
shadowEnabled: z
|
|
105
|
+
shadowEnabled: z
|
|
106
|
+
.boolean()
|
|
107
|
+
.default(false)
|
|
108
|
+
.describe('Whether shadow is enabled.'),
|
|
74
109
|
shadowBlur: z.number().default(5).describe('Shadow blur radius in pixels.'),
|
|
75
|
-
shadowOffsetX: z
|
|
76
|
-
|
|
110
|
+
shadowOffsetX: z
|
|
111
|
+
.number()
|
|
112
|
+
.default(0)
|
|
113
|
+
.describe('Shadow horizontal offset in pixels.'),
|
|
114
|
+
shadowOffsetY: z
|
|
115
|
+
.number()
|
|
116
|
+
.default(0)
|
|
117
|
+
.describe('Shadow vertical offset in pixels.'),
|
|
77
118
|
shadowColor: z.string().default('black').describe('Shadow color.'),
|
|
78
119
|
shadowOpacity: z.number().default(1).describe('Shadow opacity (0-1).'),
|
|
79
|
-
visible: z
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
120
|
+
visible: z
|
|
121
|
+
.boolean()
|
|
122
|
+
.default(true)
|
|
123
|
+
.describe('Whether the element is visible.'),
|
|
124
|
+
draggable: z
|
|
125
|
+
.boolean()
|
|
126
|
+
.default(true)
|
|
127
|
+
.describe('Whether the element can be dragged.'),
|
|
128
|
+
resizable: z
|
|
129
|
+
.boolean()
|
|
130
|
+
.default(true)
|
|
131
|
+
.describe('Whether the element can be resized.'),
|
|
132
|
+
selectable: z
|
|
133
|
+
.boolean()
|
|
134
|
+
.default(true)
|
|
135
|
+
.describe('Whether the element can be selected.'),
|
|
136
|
+
removable: z
|
|
137
|
+
.boolean()
|
|
138
|
+
.default(true)
|
|
139
|
+
.describe('Whether the element can be removed.'),
|
|
140
|
+
contentEditable: z
|
|
141
|
+
.boolean()
|
|
142
|
+
.default(true)
|
|
143
|
+
.describe('Whether the element content can be edited.'),
|
|
144
|
+
styleEditable: z
|
|
145
|
+
.boolean()
|
|
146
|
+
.default(true)
|
|
147
|
+
.describe('Whether the element style can be edited.'),
|
|
148
|
+
alwaysOnTop: z
|
|
149
|
+
.boolean()
|
|
150
|
+
.default(false)
|
|
151
|
+
.describe('Whether the element stays on top of others.'),
|
|
152
|
+
showInExport: z
|
|
153
|
+
.boolean()
|
|
154
|
+
.default(true)
|
|
155
|
+
.describe('Whether the element appears in exports.'),
|
|
88
156
|
};
|
|
89
157
|
const imageProps = {
|
|
90
158
|
src: z.string().default('').describe('URL or data URI of the image file.'),
|
|
91
159
|
cropX: z.number().default(0).describe('Crop X offset as a fraction (0-1).'),
|
|
92
160
|
cropY: z.number().default(0).describe('Crop Y offset as a fraction (0-1).'),
|
|
93
|
-
cropWidth: z
|
|
94
|
-
|
|
161
|
+
cropWidth: z
|
|
162
|
+
.number()
|
|
163
|
+
.default(1)
|
|
164
|
+
.describe('Crop width as a fraction (0-1).'),
|
|
165
|
+
cropHeight: z
|
|
166
|
+
.number()
|
|
167
|
+
.default(1)
|
|
168
|
+
.describe('Crop height as a fraction (0-1).'),
|
|
95
169
|
cornerRadius: z.number().default(0).describe('Corner radius in pixels.'),
|
|
96
|
-
flipX: z
|
|
97
|
-
|
|
98
|
-
|
|
170
|
+
flipX: z
|
|
171
|
+
.boolean()
|
|
172
|
+
.default(false)
|
|
173
|
+
.describe('Whether the image is flipped horizontally.'),
|
|
174
|
+
flipY: z
|
|
175
|
+
.boolean()
|
|
176
|
+
.default(false)
|
|
177
|
+
.describe('Whether the image is flipped vertically.'),
|
|
178
|
+
clipSrc: z
|
|
179
|
+
.string()
|
|
180
|
+
.default('')
|
|
181
|
+
.describe('URL or data URI for a clipping mask.'),
|
|
99
182
|
borderColor: z.string().default('black').describe('Border color.'),
|
|
100
183
|
borderSize: z.number().default(0).describe('Border width in pixels.'),
|
|
101
|
-
keepRatio: z
|
|
102
|
-
|
|
184
|
+
keepRatio: z
|
|
185
|
+
.boolean()
|
|
186
|
+
.default(false)
|
|
187
|
+
.describe('Whether to maintain aspect ratio when resizing.'),
|
|
188
|
+
stretchEnabled: z
|
|
189
|
+
.boolean()
|
|
190
|
+
.default(false)
|
|
191
|
+
.describe('Whether stretch mode is enabled.'),
|
|
103
192
|
};
|
|
104
193
|
const textShape = {
|
|
105
194
|
...shapeBase,
|
|
106
195
|
type: z.literal('text'),
|
|
107
196
|
text: z.string().default('').describe('Visible text shown on canvas.'),
|
|
108
|
-
placeholder: z
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
197
|
+
placeholder: z
|
|
198
|
+
.string()
|
|
199
|
+
.default('')
|
|
200
|
+
.describe('Placeholder text shown when empty.'),
|
|
201
|
+
fontSize: z
|
|
202
|
+
.number()
|
|
203
|
+
.default(TEXT_DEFAULTS.fontSize)
|
|
204
|
+
.describe('Font size in pixels.'),
|
|
205
|
+
fontFamily: z
|
|
206
|
+
.string()
|
|
207
|
+
.default(TEXT_DEFAULTS.fontFamily)
|
|
208
|
+
.describe('Font family name.'),
|
|
209
|
+
fontStyle: z
|
|
210
|
+
.string()
|
|
211
|
+
.default(TEXT_DEFAULTS.fontStyle)
|
|
212
|
+
.describe("Font style: 'normal' or 'italic'."),
|
|
213
|
+
fontWeight: z
|
|
214
|
+
.string()
|
|
215
|
+
.default(TEXT_DEFAULTS.fontWeight)
|
|
216
|
+
.describe("Font weight: 'normal', 'bold', or numeric string."),
|
|
217
|
+
textDecoration: z
|
|
218
|
+
.string()
|
|
219
|
+
.default(TEXT_DEFAULTS.textDecoration)
|
|
220
|
+
.describe('Text decoration (e.g. underline, line-through).'),
|
|
221
|
+
textTransform: z
|
|
222
|
+
.string()
|
|
223
|
+
.default(TEXT_DEFAULTS.textTransform)
|
|
224
|
+
.describe('Text transform: uppercase, lowercase, capitalize, none.'),
|
|
115
225
|
fill: z.string().default(TEXT_DEFAULTS.fill).describe('Text color.'),
|
|
116
226
|
// model stores align as a free string (loadJSON can emit e.g. 'start')
|
|
117
|
-
align: z
|
|
227
|
+
align: z
|
|
228
|
+
.string()
|
|
229
|
+
.default(TEXT_DEFAULTS.align)
|
|
230
|
+
.describe('Horizontal alignment (commonly left | center | right | justify).'),
|
|
118
231
|
width: z.number().default(100).describe('Width in pixels.'),
|
|
119
|
-
height: z
|
|
120
|
-
|
|
121
|
-
|
|
232
|
+
height: z
|
|
233
|
+
.number()
|
|
234
|
+
.default(0)
|
|
235
|
+
.describe('Height in pixels (0 until measured/rendered).'),
|
|
236
|
+
verticalAlign: z
|
|
237
|
+
.string()
|
|
238
|
+
.default('top')
|
|
239
|
+
.describe('Vertical alignment: top, middle, bottom.'),
|
|
240
|
+
strokeWidth: z
|
|
241
|
+
.number()
|
|
242
|
+
.default(TEXT_DEFAULTS.strokeWidth)
|
|
243
|
+
.describe('Stroke width in pixels.'),
|
|
122
244
|
stroke: z.string().default(TEXT_DEFAULTS.stroke).describe('Stroke color.'),
|
|
123
|
-
strokeLineJoin: z
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
245
|
+
strokeLineJoin: z
|
|
246
|
+
.string()
|
|
247
|
+
.default(TEXT_DEFAULTS.strokeLineJoin)
|
|
248
|
+
.describe('Stroke corner join: round, miter, bevel.'),
|
|
249
|
+
lineHeight: z
|
|
250
|
+
.union([z.number(), z.string()])
|
|
251
|
+
.default(TEXT_DEFAULTS.lineHeight)
|
|
252
|
+
.describe("Line height multiplier or 'auto'."),
|
|
253
|
+
letterSpacing: z
|
|
254
|
+
.number()
|
|
255
|
+
.default(TEXT_DEFAULTS.letterSpacing)
|
|
256
|
+
.describe('Letter spacing as a fraction of font size.'),
|
|
257
|
+
backgroundEnabled: z
|
|
258
|
+
.boolean()
|
|
259
|
+
.default(false)
|
|
260
|
+
.describe('Whether the text background is enabled.'),
|
|
261
|
+
backgroundColor: z
|
|
262
|
+
.string()
|
|
263
|
+
.default('#7ED321')
|
|
264
|
+
.describe('Text background color.'),
|
|
265
|
+
backgroundOpacity: z
|
|
266
|
+
.number()
|
|
267
|
+
.default(1)
|
|
268
|
+
.describe('Text background opacity (0-1).'),
|
|
269
|
+
backgroundCornerRadius: z
|
|
270
|
+
.number()
|
|
271
|
+
.default(0.5)
|
|
272
|
+
.describe('Text background corner radius.'),
|
|
273
|
+
backgroundPadding: z
|
|
274
|
+
.number()
|
|
275
|
+
.default(0.5)
|
|
276
|
+
.describe('Text background padding.'),
|
|
277
|
+
curveEnabled: z
|
|
278
|
+
.boolean()
|
|
279
|
+
.default(TEXT_DEFAULTS.curveEnabled)
|
|
280
|
+
.describe('Whether the text curve effect is enabled.'),
|
|
281
|
+
curvePower: z
|
|
282
|
+
.number()
|
|
283
|
+
.default(TEXT_DEFAULTS.curvePower)
|
|
284
|
+
.describe('Text curve power (-1 to 1, 0 = straight).'),
|
|
285
|
+
legacyBackground: z
|
|
286
|
+
.boolean()
|
|
287
|
+
.optional()
|
|
288
|
+
.describe('Internal compatibility flag stamped by the v2→v3 migration.'),
|
|
134
289
|
};
|
|
135
290
|
const imageShape = { ...shapeBase, ...imageProps, type: z.literal('image') };
|
|
136
291
|
const svgShape = {
|
|
137
292
|
...shapeBase,
|
|
138
293
|
type: z.literal('svg'),
|
|
139
294
|
src: z.string().default('').describe('URL or data URI of the SVG.'),
|
|
140
|
-
maskSrc: z
|
|
295
|
+
maskSrc: z
|
|
296
|
+
.string()
|
|
297
|
+
.default('')
|
|
298
|
+
.describe('URL or data URI for an SVG mask.'),
|
|
141
299
|
cropX: z.number().default(0),
|
|
142
300
|
cropY: z.number().default(0),
|
|
143
301
|
cropWidth: z.number().default(1),
|
|
@@ -151,7 +309,10 @@ function build(o) {
|
|
|
151
309
|
borderColor: z.string().default('black'),
|
|
152
310
|
borderSize: z.number().default(0),
|
|
153
311
|
cornerRadius: z.number().default(0),
|
|
154
|
-
colorsReplace: z
|
|
312
|
+
colorsReplace: z
|
|
313
|
+
.record(z.string(), z.string())
|
|
314
|
+
.default({})
|
|
315
|
+
.describe('Color replacement mappings (original → replacement).'),
|
|
155
316
|
};
|
|
156
317
|
const lineShape = {
|
|
157
318
|
...shapeBase,
|
|
@@ -160,13 +321,22 @@ function build(o) {
|
|
|
160
321
|
height: z.number().default(10),
|
|
161
322
|
color: z.string().default('black').describe('Line color.'),
|
|
162
323
|
dash: z.array(z.number()).default([]).describe('Dash pattern array.'),
|
|
163
|
-
startHead: z
|
|
164
|
-
|
|
324
|
+
startHead: z
|
|
325
|
+
.string()
|
|
326
|
+
.default('')
|
|
327
|
+
.describe('Start arrow head: empty, arrow, circle, square, bar.'),
|
|
328
|
+
endHead: z
|
|
329
|
+
.string()
|
|
330
|
+
.default('')
|
|
331
|
+
.describe('End arrow head: empty, arrow, circle, square, bar.'),
|
|
165
332
|
};
|
|
166
333
|
const figureShape = {
|
|
167
334
|
...shapeBase,
|
|
168
335
|
type: z.literal('figure'),
|
|
169
|
-
subType: z
|
|
336
|
+
subType: z
|
|
337
|
+
.string()
|
|
338
|
+
.default('rect')
|
|
339
|
+
.describe('Figure subtype: rect, ellipse, triangle, etc.'),
|
|
170
340
|
fill: z.string().default('rgb(0, 161, 255)').describe('Fill color.'),
|
|
171
341
|
dash: z.array(z.number()).default([]).describe('Dash pattern array.'),
|
|
172
342
|
strokeWidth: z.number().default(0),
|
|
@@ -188,45 +358,119 @@ function build(o) {
|
|
|
188
358
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
189
359
|
endTime: z.number().default(1).describe('End offset (0-1).'),
|
|
190
360
|
volume: z.number().default(1).describe('Volume (0-1).'),
|
|
191
|
-
speed: z
|
|
361
|
+
speed: z
|
|
362
|
+
.number()
|
|
363
|
+
.default(1)
|
|
364
|
+
.describe('Playback speed multiplier (1 = normal).'),
|
|
192
365
|
};
|
|
193
366
|
const borderSide = o({
|
|
194
367
|
color: z.string().optional().describe('Border color for this side.'),
|
|
195
|
-
width: z
|
|
196
|
-
|
|
368
|
+
width: z
|
|
369
|
+
.number()
|
|
370
|
+
.optional()
|
|
371
|
+
.describe('Border width in pixels for this side.'),
|
|
372
|
+
style: z
|
|
373
|
+
.enum(['solid', 'dashed', 'dotted', 'none'])
|
|
374
|
+
.optional()
|
|
375
|
+
.describe('Border style for this side.'),
|
|
197
376
|
});
|
|
198
377
|
// Cell styling fields are optional (inherit from the table when omitted).
|
|
199
378
|
const tableCell = o({
|
|
200
379
|
id: z.string().describe('Unique identifier for the cell.'),
|
|
201
|
-
type: z
|
|
380
|
+
type: z
|
|
381
|
+
.literal('tablecell')
|
|
382
|
+
.default('tablecell')
|
|
383
|
+
.describe('Cell type identifier.'),
|
|
202
384
|
text: z.string().default('').describe('Visible text shown in the cell.'),
|
|
203
385
|
opacity: z.number().optional().describe('Opacity level (0-1).'),
|
|
204
386
|
rowSpan: z.number().optional().describe('Number of rows this cell spans.'),
|
|
205
|
-
colSpan: z
|
|
206
|
-
|
|
387
|
+
colSpan: z
|
|
388
|
+
.number()
|
|
389
|
+
.optional()
|
|
390
|
+
.describe('Number of columns this cell spans.'),
|
|
391
|
+
mergedInto: z
|
|
392
|
+
.string()
|
|
393
|
+
.optional()
|
|
394
|
+
.describe('Id of the cell this one is merged into, if any.'),
|
|
207
395
|
name: z.string().optional().describe('Display name for the cell.'),
|
|
208
|
-
custom: z
|
|
396
|
+
custom: z
|
|
397
|
+
.unknown()
|
|
398
|
+
.optional()
|
|
399
|
+
.describe('Custom data attached to the cell.'),
|
|
209
400
|
borders: o({
|
|
210
401
|
top: borderSide.optional().describe('Top border override for this cell.'),
|
|
211
|
-
right: borderSide
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
402
|
+
right: borderSide
|
|
403
|
+
.optional()
|
|
404
|
+
.describe('Right border override for this cell.'),
|
|
405
|
+
bottom: borderSide
|
|
406
|
+
.optional()
|
|
407
|
+
.describe('Bottom border override for this cell.'),
|
|
408
|
+
left: borderSide
|
|
409
|
+
.optional()
|
|
410
|
+
.describe('Left border override for this cell.'),
|
|
411
|
+
})
|
|
412
|
+
.optional()
|
|
413
|
+
.describe('Per-side border overrides for this cell.'),
|
|
414
|
+
fontSize: z
|
|
415
|
+
.number()
|
|
416
|
+
.optional()
|
|
417
|
+
.describe('Font size in pixels (inherits from the table when omitted).'),
|
|
418
|
+
fontFamily: z
|
|
419
|
+
.string()
|
|
420
|
+
.optional()
|
|
421
|
+
.describe('Font family name (inherits from the table when omitted).'),
|
|
422
|
+
fontWeight: z
|
|
423
|
+
.string()
|
|
424
|
+
.optional()
|
|
425
|
+
.describe("Font weight: 'normal', 'bold', or numeric string (inherits from the table when omitted)."),
|
|
426
|
+
fontStyle: z
|
|
427
|
+
.string()
|
|
428
|
+
.optional()
|
|
429
|
+
.describe("Font style: 'normal' or 'italic' (inherits from the table when omitted)."),
|
|
430
|
+
textDecoration: z
|
|
431
|
+
.string()
|
|
432
|
+
.optional()
|
|
433
|
+
.describe('Text decoration, e.g. underline, line-through (inherits from the table when omitted).'),
|
|
434
|
+
textTransform: z
|
|
435
|
+
.string()
|
|
436
|
+
.optional()
|
|
437
|
+
.describe('Text transform: uppercase, lowercase, capitalize, none (inherits from the table when omitted).'),
|
|
438
|
+
fill: z
|
|
439
|
+
.string()
|
|
440
|
+
.optional()
|
|
441
|
+
.describe('Text color (inherits from the table when omitted).'),
|
|
442
|
+
align: z
|
|
443
|
+
.string()
|
|
444
|
+
.optional()
|
|
445
|
+
.describe('Horizontal alignment: left, center, right, justify (inherits from the table when omitted).'),
|
|
446
|
+
verticalAlign: z
|
|
447
|
+
.string()
|
|
448
|
+
.optional()
|
|
449
|
+
.describe('Vertical alignment: top, middle, bottom (inherits from the table when omitted).'),
|
|
450
|
+
lineHeight: z
|
|
451
|
+
.union([z.number(), z.string()])
|
|
452
|
+
.optional()
|
|
453
|
+
.describe("Line height multiplier or 'auto' (inherits from the table when omitted)."),
|
|
454
|
+
letterSpacing: z
|
|
455
|
+
.number()
|
|
456
|
+
.optional()
|
|
457
|
+
.describe('Letter spacing as a fraction of font size (inherits from the table when omitted).'),
|
|
458
|
+
strokeWidth: z
|
|
459
|
+
.number()
|
|
460
|
+
.optional()
|
|
461
|
+
.describe('Stroke width in pixels (inherits from the table when omitted).'),
|
|
462
|
+
stroke: z
|
|
463
|
+
.string()
|
|
464
|
+
.optional()
|
|
465
|
+
.describe('Stroke color (inherits from the table when omitted).'),
|
|
466
|
+
cellBackground: z
|
|
467
|
+
.string()
|
|
468
|
+
.optional()
|
|
469
|
+
.describe('Cell background color (inherits from the table when omitted).'),
|
|
470
|
+
cellPadding: z
|
|
471
|
+
.number()
|
|
472
|
+
.optional()
|
|
473
|
+
.describe('Cell padding in pixels (inherits from the table when omitted).'),
|
|
230
474
|
});
|
|
231
475
|
// Effect props are nonsensical for tables and not part of its serialized form —
|
|
232
476
|
// omit them via a typed destructure (so `tableShape` keeps precise field types).
|
|
@@ -238,27 +482,84 @@ function build(o) {
|
|
|
238
482
|
height: z.number().default(200).describe('Height in pixels.'),
|
|
239
483
|
rows: z.number().default(3).describe('Number of rows.'),
|
|
240
484
|
cols: z.number().default(3).describe('Number of columns.'),
|
|
241
|
-
colWidths: z
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
485
|
+
colWidths: z
|
|
486
|
+
.array(z.number())
|
|
487
|
+
.default([])
|
|
488
|
+
.describe('Column width fractions (sum to 1).'),
|
|
489
|
+
rowHeights: z
|
|
490
|
+
.array(z.number())
|
|
491
|
+
.default([])
|
|
492
|
+
.describe('Row height fractions (sum to 1).'),
|
|
493
|
+
cells: z
|
|
494
|
+
.array(tableCell)
|
|
495
|
+
.default([])
|
|
496
|
+
.describe('Flat cell array in row-major order.'),
|
|
497
|
+
borderColor: z
|
|
498
|
+
.string()
|
|
499
|
+
.default('#000000')
|
|
500
|
+
.describe('Default border color for the table grid.'),
|
|
501
|
+
borderWidth: z
|
|
502
|
+
.number()
|
|
503
|
+
.default(1)
|
|
504
|
+
.describe('Default border width in pixels.'),
|
|
505
|
+
borderStyle: z
|
|
506
|
+
.enum(['solid', 'dashed', 'dotted', 'none'])
|
|
507
|
+
.default('solid')
|
|
508
|
+
.describe('Default border style.'),
|
|
509
|
+
fontSize: z
|
|
510
|
+
.number()
|
|
511
|
+
.default(30)
|
|
512
|
+
.describe('Default font size in pixels for cells.'),
|
|
513
|
+
fontFamily: z
|
|
514
|
+
.string()
|
|
515
|
+
.default('Roboto')
|
|
516
|
+
.describe('Default font family name for cells.'),
|
|
517
|
+
fontWeight: z
|
|
518
|
+
.string()
|
|
519
|
+
.default('normal')
|
|
520
|
+
.describe("Default font weight: 'normal', 'bold', or numeric string."),
|
|
521
|
+
fontStyle: z
|
|
522
|
+
.string()
|
|
523
|
+
.default('normal')
|
|
524
|
+
.describe("Default font style: 'normal' or 'italic'."),
|
|
525
|
+
textDecoration: z
|
|
526
|
+
.string()
|
|
527
|
+
.default('')
|
|
528
|
+
.describe('Default text decoration (e.g. underline, line-through).'),
|
|
529
|
+
textTransform: z
|
|
530
|
+
.string()
|
|
531
|
+
.default('none')
|
|
532
|
+
.describe('Default text transform: uppercase, lowercase, capitalize, none.'),
|
|
253
533
|
fill: z.string().default('black').describe('Default text color for cells.'),
|
|
254
|
-
align: z
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
534
|
+
align: z
|
|
535
|
+
.string()
|
|
536
|
+
.default('center')
|
|
537
|
+
.describe('Default horizontal alignment: left, center, right, justify.'),
|
|
538
|
+
verticalAlign: z
|
|
539
|
+
.string()
|
|
540
|
+
.default('middle')
|
|
541
|
+
.describe('Default vertical alignment: top, middle, bottom.'),
|
|
542
|
+
lineHeight: z
|
|
543
|
+
.union([z.number(), z.string()])
|
|
544
|
+
.default(1.2)
|
|
545
|
+
.describe("Default line height multiplier or 'auto'."),
|
|
546
|
+
letterSpacing: z
|
|
547
|
+
.number()
|
|
548
|
+
.default(0)
|
|
549
|
+
.describe('Default letter spacing as a fraction of font size.'),
|
|
550
|
+
strokeWidth: z
|
|
551
|
+
.number()
|
|
552
|
+
.default(0)
|
|
553
|
+
.describe('Default stroke width in pixels.'),
|
|
259
554
|
stroke: z.string().default('black').describe('Default stroke color.'),
|
|
260
|
-
cellBackground: z
|
|
261
|
-
|
|
555
|
+
cellBackground: z
|
|
556
|
+
.string()
|
|
557
|
+
.default('transparent')
|
|
558
|
+
.describe('Default cell background color.'),
|
|
559
|
+
cellPadding: z
|
|
560
|
+
.number()
|
|
561
|
+
.default(4)
|
|
562
|
+
.describe('Default cell padding in pixels.'),
|
|
262
563
|
};
|
|
263
564
|
const groupShape = (element) => ({
|
|
264
565
|
id: z.string(),
|
|
@@ -272,7 +573,10 @@ function build(o) {
|
|
|
272
573
|
alwaysOnTop: z.boolean().default(false),
|
|
273
574
|
showInExport: z.boolean().default(true),
|
|
274
575
|
animations: z.array(animation).default([]),
|
|
275
|
-
children: z
|
|
576
|
+
children: z
|
|
577
|
+
.array(element)
|
|
578
|
+
.default([])
|
|
579
|
+
.describe('Child elements in this group.'),
|
|
276
580
|
});
|
|
277
581
|
const element = z.lazy(() =>
|
|
278
582
|
// Keep ELEMENT_TYPES (below) in sync with this union.
|
|
@@ -289,7 +593,10 @@ function build(o) {
|
|
|
289
593
|
]));
|
|
290
594
|
const fontShape = {
|
|
291
595
|
fontFamily: z.string().describe('Font family name.'),
|
|
292
|
-
url: z
|
|
596
|
+
url: z
|
|
597
|
+
.string()
|
|
598
|
+
.default('')
|
|
599
|
+
.describe('URL to load the font from (single-variant fonts).'),
|
|
293
600
|
styles: z
|
|
294
601
|
.array(o({
|
|
295
602
|
src: z.string().describe('Font source, typically in CSS url() form.'),
|
|
@@ -301,39 +608,98 @@ function build(o) {
|
|
|
301
608
|
};
|
|
302
609
|
const audioShape = {
|
|
303
610
|
id: z.string().describe('Unique identifier for the audio track.'),
|
|
611
|
+
name: z
|
|
612
|
+
.string()
|
|
613
|
+
.default('')
|
|
614
|
+
.describe('Display name shown on the timeline.'),
|
|
304
615
|
src: z.string().default('').describe('URL or data URI of the audio file.'),
|
|
305
616
|
duration: z.number().default(0).describe('Duration in milliseconds.'),
|
|
306
617
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
307
618
|
endTime: z.number().default(1).describe('End offset (0-1).'),
|
|
308
619
|
volume: z.number().default(1).describe('Volume level (0-1).'),
|
|
309
|
-
delay: z
|
|
310
|
-
|
|
620
|
+
delay: z
|
|
621
|
+
.number()
|
|
622
|
+
.default(0)
|
|
623
|
+
.describe('Delay before playing, in milliseconds.'),
|
|
624
|
+
speed: z
|
|
625
|
+
.number()
|
|
626
|
+
.default(1)
|
|
627
|
+
.describe('Playback speed multiplier (1 = normal).'),
|
|
311
628
|
custom: z.unknown().optional(),
|
|
312
629
|
};
|
|
313
630
|
const page = o({
|
|
314
631
|
id: z.string().describe('Unique identifier for the page.'),
|
|
315
|
-
width: z
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
632
|
+
width: z
|
|
633
|
+
.union([z.number(), z.literal('auto')])
|
|
634
|
+
.default('auto')
|
|
635
|
+
.describe("Page width in pixels or 'auto'."),
|
|
636
|
+
height: z
|
|
637
|
+
.union([z.number(), z.literal('auto')])
|
|
638
|
+
.default('auto')
|
|
639
|
+
.describe("Page height in pixels or 'auto'."),
|
|
640
|
+
background: z
|
|
641
|
+
.string()
|
|
642
|
+
.default('white')
|
|
643
|
+
.describe('Background color or image.'),
|
|
644
|
+
bleed: z
|
|
645
|
+
.number()
|
|
646
|
+
.default(0)
|
|
647
|
+
.describe('Bleed area in pixels for print (all sides; base value for per-side overrides).'),
|
|
648
|
+
bleedTop: z
|
|
649
|
+
.number()
|
|
650
|
+
.optional()
|
|
651
|
+
.describe('Per-side bleed override (top), in pixels. Inherits `bleed` when omitted.'),
|
|
652
|
+
bleedRight: z
|
|
653
|
+
.number()
|
|
654
|
+
.optional()
|
|
655
|
+
.describe('Per-side bleed override (right), in pixels. Inherits `bleed` when omitted.'),
|
|
656
|
+
bleedBottom: z
|
|
657
|
+
.number()
|
|
658
|
+
.optional()
|
|
659
|
+
.describe('Per-side bleed override (bottom), in pixels. Inherits `bleed` when omitted.'),
|
|
660
|
+
bleedLeft: z
|
|
661
|
+
.number()
|
|
662
|
+
.optional()
|
|
663
|
+
.describe('Per-side bleed override (left), in pixels. Inherits `bleed` when omitted.'),
|
|
323
664
|
custom: z.unknown().optional(),
|
|
324
|
-
duration: z
|
|
665
|
+
duration: z
|
|
666
|
+
.number()
|
|
667
|
+
.default(5000)
|
|
668
|
+
.describe('Page duration in milliseconds.'),
|
|
325
669
|
children: z.array(element).default([]).describe('Elements on this page.'),
|
|
326
670
|
});
|
|
327
671
|
const design = o({
|
|
328
|
-
|
|
672
|
+
// Labels the OUTPUT only. migrateDesign runs before this parse and reads
|
|
673
|
+
// the RAW value, where a missing schemaVersion means 0 (the oldest format,
|
|
674
|
+
// see migrate.ts) — not the current one. The two are not in conflict: by
|
|
675
|
+
// the time this default applies, the design has already been migrated up.
|
|
676
|
+
schemaVersion: z
|
|
677
|
+
.number()
|
|
678
|
+
.default(CURRENT_SCHEMA_VERSION)
|
|
679
|
+
.describe('Schema version for compatibility / migration.'),
|
|
329
680
|
width: z.number().default(1080).describe('Canvas width in pixels.'),
|
|
330
681
|
height: z.number().default(1080).describe('Canvas height in pixels.'),
|
|
331
|
-
unit: z
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
682
|
+
unit: z
|
|
683
|
+
.enum(['px', 'pt', 'mm', 'cm', 'in'])
|
|
684
|
+
.default('px')
|
|
685
|
+
.describe('Unit type for print measurements.'),
|
|
686
|
+
dpi: z
|
|
687
|
+
.number()
|
|
688
|
+
.default(72)
|
|
689
|
+
.describe('Dots per inch for print calculations.'),
|
|
690
|
+
custom: z
|
|
691
|
+
.unknown()
|
|
692
|
+
.optional()
|
|
693
|
+
.describe('Custom data attached to the design.'),
|
|
694
|
+
fonts: z
|
|
695
|
+
.array(o(fontShape))
|
|
696
|
+
.default([])
|
|
697
|
+
.describe('Fonts used in the design.'),
|
|
335
698
|
pages: z.array(page).default([]).describe('Pages in the design.'),
|
|
336
|
-
audios: z
|
|
699
|
+
audios: z
|
|
700
|
+
.array(o(audioShape))
|
|
701
|
+
.default([])
|
|
702
|
+
.describe('Audio tracks in the design.'),
|
|
337
703
|
});
|
|
338
704
|
// Raw per-type field bags (not zod objects). The leaf schemas for the type
|
|
339
705
|
// guard are wrapped from these once at module level (below) so they aren't
|
|
@@ -433,7 +799,10 @@ export function validateDesign(input, opts = {}) {
|
|
|
433
799
|
// Message prefix `Invalid Polotno design:` is frozen (legacy matchers);
|
|
434
800
|
// the code + details are the stable contract going forward.
|
|
435
801
|
function throwInvalidDesign(errors) {
|
|
436
|
-
const summary = errors
|
|
802
|
+
const summary = errors
|
|
803
|
+
.slice(0, 10)
|
|
804
|
+
.map((e) => ` ${e.path}: ${e.message}`)
|
|
805
|
+
.join('\n');
|
|
437
806
|
throw polotnoError('DESIGN_INVALID', `Invalid Polotno design:\n${summary}`, {
|
|
438
807
|
errors,
|
|
439
808
|
});
|
|
@@ -446,7 +815,8 @@ export function assertValidDesign(input, opts = {}) {
|
|
|
446
815
|
/**
|
|
447
816
|
* Validate AND normalize in a single schema pass (safeParse both raises the
|
|
448
817
|
* same DESIGN_INVALID as `assertValidDesign` and returns the defaults-filled
|
|
449
|
-
* result)
|
|
818
|
+
* result). The only other copy on this path is the one `migrateDesign` makes,
|
|
819
|
+
* which parseDesign reuses as its working copy rather than cloning again.
|
|
450
820
|
* For canonical mode the single pass is equally faithful: input that passes
|
|
451
821
|
* the strict schema has no unknown keys, so its parse result equals the
|
|
452
822
|
* lenient one.
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED