@polotno/schema 0.2.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/bleed.d.ts +0 -1
- package/lib/bleed.js +0 -1
- package/lib/defaults.d.ts +44 -434
- package/lib/defaults.js +92 -243
- package/lib/design.schema.json +5 -0
- package/lib/errors.d.ts +23 -6
- package/lib/errors.js +5 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/migrate.d.ts +18 -3
- package/lib/migrate.js +120 -45
- package/lib/normalize.d.ts +20 -3
- package/lib/normalize.js +196 -50
- package/lib/schema.d.ts +14 -1
- package/lib/schema.js +537 -134
- package/lib/types-parity.d.ts +0 -1
- package/lib/types-parity.js +0 -1
- package/lib/types.d.ts +2 -1
- package/lib/types.js +0 -1
- package/package.json +8 -8
- package/lib/bleed.d.ts.map +0 -1
- package/lib/bleed.js.map +0 -1
- package/lib/constants.d.ts +0 -6
- package/lib/constants.d.ts.map +0 -1
- package/lib/constants.js +0 -61
- package/lib/constants.js.map +0 -1
- package/lib/defaults.d.ts.map +0 -1
- package/lib/defaults.js.map +0 -1
- package/lib/errors.d.ts.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/migrate.d.ts.map +0 -1
- package/lib/migrate.js.map +0 -1
- package/lib/normalize.d.ts.map +0 -1
- package/lib/normalize.js.map +0 -1
- package/lib/processors.d.ts +0 -13
- package/lib/processors.d.ts.map +0 -1
- package/lib/processors.js +0 -170
- package/lib/processors.js.map +0 -1
- package/lib/schema.d.ts.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/traverse.d.ts +0 -3
- package/lib/traverse.d.ts.map +0 -1
- package/lib/traverse.js +0 -32
- package/lib/traverse.js.map +0 -1
- package/lib/types-parity.d.ts.map +0 -1
- package/lib/types-parity.js.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/validate.d.ts +0 -19
- package/lib/validate.d.ts.map +0 -1
- package/lib/validate.js +0 -383
- package/lib/validate.js.map +0 -1
package/lib/schema.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The Polotno design format, authored once in zod v4. This is the SINGLE source
|
|
2
|
-
// of truth for the package: default values (inline `.default(...)
|
|
3
|
-
//
|
|
2
|
+
// of truth for the package: default values (inline `.default(...)`, except the
|
|
3
|
+
// text rendering defaults, which live in `./defaults` so renderers can share
|
|
4
|
+
// them), validation, and the generated JSON Schema all come from here. TypeScript types are the
|
|
4
5
|
// hand-written `./types` (zod can't cleanly infer the recursive group type).
|
|
5
6
|
//
|
|
6
7
|
// The whole schema is built by `build(o)` (where `o` is the zod object
|
|
@@ -18,111 +19,283 @@
|
|
|
18
19
|
// stripping) — normalize is "fill defaults + validate", exact for clean input.
|
|
19
20
|
import { z } from 'zod';
|
|
20
21
|
import { polotnoError } from './errors.js';
|
|
22
|
+
import { TEXT_DEFAULTS } from './defaults.js';
|
|
21
23
|
export const CURRENT_SCHEMA_VERSION = 4;
|
|
24
|
+
// Every element type the design format knows. Kept in sync with the
|
|
25
|
+
// element discriminatedUnion in build() below.
|
|
26
|
+
export const ELEMENT_TYPES = [
|
|
27
|
+
'text',
|
|
28
|
+
'image',
|
|
29
|
+
'svg',
|
|
30
|
+
'line',
|
|
31
|
+
'figure',
|
|
32
|
+
'gif',
|
|
33
|
+
'video',
|
|
34
|
+
'table',
|
|
35
|
+
'group',
|
|
36
|
+
];
|
|
22
37
|
// `o` is the object constructor (`z.object` for lenient, `z.strictObject` for
|
|
23
38
|
// canonical), passed concretely so its precise generic return type is preserved
|
|
24
39
|
// — a `strict ? z.strictObject : z.object` ternary would widen to a union and
|
|
25
40
|
// break `z.infer` of the leaf schemas (and thus the type-drift guard).
|
|
26
41
|
function build(o) {
|
|
27
42
|
const animation = o({
|
|
28
|
-
delay: z
|
|
29
|
-
|
|
30
|
-
|
|
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.'),
|
|
31
55
|
type: z.enum(['enter', 'exit', 'loop']).describe('Animation phase.'),
|
|
32
56
|
name: z.string().default('none').describe('Animation name identifier.'),
|
|
33
57
|
data: z.unknown().default({}).describe('Animation-specific data.'),
|
|
34
58
|
});
|
|
35
59
|
const filters = z
|
|
36
|
-
.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
|
+
}))
|
|
37
66
|
.describe('Applied image filters keyed by filter name.');
|
|
38
67
|
// Fields shared by every Shape-derived element. Defaults inline.
|
|
39
68
|
const shapeBase = {
|
|
40
69
|
id: z.string().describe('Unique identifier for the element.'),
|
|
41
70
|
type: z.string().describe('Element type identifier.'),
|
|
42
71
|
name: z.string().default('').describe('Display name for the element.'),
|
|
43
|
-
custom: z
|
|
72
|
+
custom: z
|
|
73
|
+
.unknown()
|
|
74
|
+
.optional()
|
|
75
|
+
.describe('Custom data attached to the element.'),
|
|
44
76
|
x: z.number().default(0).describe('X position in pixels.'),
|
|
45
77
|
y: z.number().default(0).describe('Y position in pixels.'),
|
|
46
78
|
width: z.number().default(100).describe('Width in pixels.'),
|
|
47
79
|
height: z.number().default(100).describe('Height in pixels.'),
|
|
48
80
|
rotation: z.number().default(0).describe('Rotation angle in degrees.'),
|
|
49
81
|
opacity: z.number().default(1).describe('Opacity level (0-1).'),
|
|
50
|
-
animations: z
|
|
51
|
-
|
|
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.'),
|
|
52
90
|
blurRadius: z.number().default(10).describe('Blur radius in pixels.'),
|
|
53
|
-
brightnessEnabled: z
|
|
91
|
+
brightnessEnabled: z
|
|
92
|
+
.boolean()
|
|
93
|
+
.default(false)
|
|
94
|
+
.describe('Whether brightness effect is enabled.'),
|
|
54
95
|
brightness: z.number().default(0).describe('Brightness level (-1 to 1).'),
|
|
55
|
-
sepiaEnabled: z
|
|
56
|
-
|
|
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.'),
|
|
57
104
|
filters: filters.default({}),
|
|
58
|
-
shadowEnabled: z
|
|
105
|
+
shadowEnabled: z
|
|
106
|
+
.boolean()
|
|
107
|
+
.default(false)
|
|
108
|
+
.describe('Whether shadow is enabled.'),
|
|
59
109
|
shadowBlur: z.number().default(5).describe('Shadow blur radius in pixels.'),
|
|
60
|
-
shadowOffsetX: z
|
|
61
|
-
|
|
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.'),
|
|
62
118
|
shadowColor: z.string().default('black').describe('Shadow color.'),
|
|
63
119
|
shadowOpacity: z.number().default(1).describe('Shadow opacity (0-1).'),
|
|
64
|
-
visible: z
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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.'),
|
|
73
156
|
};
|
|
74
157
|
const imageProps = {
|
|
75
158
|
src: z.string().default('').describe('URL or data URI of the image file.'),
|
|
76
159
|
cropX: z.number().default(0).describe('Crop X offset as a fraction (0-1).'),
|
|
77
160
|
cropY: z.number().default(0).describe('Crop Y offset as a fraction (0-1).'),
|
|
78
|
-
cropWidth: z
|
|
79
|
-
|
|
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).'),
|
|
80
169
|
cornerRadius: z.number().default(0).describe('Corner radius in pixels.'),
|
|
81
|
-
flipX: z
|
|
82
|
-
|
|
83
|
-
|
|
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.'),
|
|
84
182
|
borderColor: z.string().default('black').describe('Border color.'),
|
|
85
183
|
borderSize: z.number().default(0).describe('Border width in pixels.'),
|
|
86
|
-
keepRatio: z
|
|
87
|
-
|
|
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.'),
|
|
88
192
|
};
|
|
89
193
|
const textShape = {
|
|
90
194
|
...shapeBase,
|
|
91
195
|
type: z.literal('text'),
|
|
92
196
|
text: z.string().default('').describe('Visible text shown on canvas.'),
|
|
93
|
-
placeholder: z
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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.'),
|
|
225
|
+
fill: z.string().default(TEXT_DEFAULTS.fill).describe('Text color.'),
|
|
101
226
|
// model stores align as a free string (loadJSON can emit e.g. 'start')
|
|
102
|
-
align: z
|
|
227
|
+
align: z
|
|
228
|
+
.string()
|
|
229
|
+
.default(TEXT_DEFAULTS.align)
|
|
230
|
+
.describe('Horizontal alignment (commonly left | center | right | justify).'),
|
|
103
231
|
width: z.number().default(100).describe('Width in pixels.'),
|
|
104
|
-
height: z
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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.'),
|
|
244
|
+
stroke: z.string().default(TEXT_DEFAULTS.stroke).describe('Stroke color.'),
|
|
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.'),
|
|
119
289
|
};
|
|
120
290
|
const imageShape = { ...shapeBase, ...imageProps, type: z.literal('image') };
|
|
121
291
|
const svgShape = {
|
|
122
292
|
...shapeBase,
|
|
123
293
|
type: z.literal('svg'),
|
|
124
294
|
src: z.string().default('').describe('URL or data URI of the SVG.'),
|
|
125
|
-
maskSrc: z
|
|
295
|
+
maskSrc: z
|
|
296
|
+
.string()
|
|
297
|
+
.default('')
|
|
298
|
+
.describe('URL or data URI for an SVG mask.'),
|
|
126
299
|
cropX: z.number().default(0),
|
|
127
300
|
cropY: z.number().default(0),
|
|
128
301
|
cropWidth: z.number().default(1),
|
|
@@ -136,7 +309,10 @@ function build(o) {
|
|
|
136
309
|
borderColor: z.string().default('black'),
|
|
137
310
|
borderSize: z.number().default(0),
|
|
138
311
|
cornerRadius: z.number().default(0),
|
|
139
|
-
colorsReplace: z
|
|
312
|
+
colorsReplace: z
|
|
313
|
+
.record(z.string(), z.string())
|
|
314
|
+
.default({})
|
|
315
|
+
.describe('Color replacement mappings (original → replacement).'),
|
|
140
316
|
};
|
|
141
317
|
const lineShape = {
|
|
142
318
|
...shapeBase,
|
|
@@ -145,13 +321,22 @@ function build(o) {
|
|
|
145
321
|
height: z.number().default(10),
|
|
146
322
|
color: z.string().default('black').describe('Line color.'),
|
|
147
323
|
dash: z.array(z.number()).default([]).describe('Dash pattern array.'),
|
|
148
|
-
startHead: z
|
|
149
|
-
|
|
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.'),
|
|
150
332
|
};
|
|
151
333
|
const figureShape = {
|
|
152
334
|
...shapeBase,
|
|
153
335
|
type: z.literal('figure'),
|
|
154
|
-
subType: z
|
|
336
|
+
subType: z
|
|
337
|
+
.string()
|
|
338
|
+
.default('rect')
|
|
339
|
+
.describe('Figure subtype: rect, ellipse, triangle, etc.'),
|
|
155
340
|
fill: z.string().default('rgb(0, 161, 255)').describe('Fill color.'),
|
|
156
341
|
dash: z.array(z.number()).default([]).describe('Dash pattern array.'),
|
|
157
342
|
strokeWidth: z.number().default(0),
|
|
@@ -173,45 +358,119 @@ function build(o) {
|
|
|
173
358
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
174
359
|
endTime: z.number().default(1).describe('End offset (0-1).'),
|
|
175
360
|
volume: z.number().default(1).describe('Volume (0-1).'),
|
|
176
|
-
speed: z
|
|
361
|
+
speed: z
|
|
362
|
+
.number()
|
|
363
|
+
.default(1)
|
|
364
|
+
.describe('Playback speed multiplier (1 = normal).'),
|
|
177
365
|
};
|
|
178
366
|
const borderSide = o({
|
|
179
367
|
color: z.string().optional().describe('Border color for this side.'),
|
|
180
|
-
width: z
|
|
181
|
-
|
|
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.'),
|
|
182
376
|
});
|
|
183
377
|
// Cell styling fields are optional (inherit from the table when omitted).
|
|
184
378
|
const tableCell = o({
|
|
185
379
|
id: z.string().describe('Unique identifier for the cell.'),
|
|
186
|
-
type: z
|
|
380
|
+
type: z
|
|
381
|
+
.literal('tablecell')
|
|
382
|
+
.default('tablecell')
|
|
383
|
+
.describe('Cell type identifier.'),
|
|
187
384
|
text: z.string().default('').describe('Visible text shown in the cell.'),
|
|
188
385
|
opacity: z.number().optional().describe('Opacity level (0-1).'),
|
|
189
386
|
rowSpan: z.number().optional().describe('Number of rows this cell spans.'),
|
|
190
|
-
colSpan: z
|
|
191
|
-
|
|
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.'),
|
|
192
395
|
name: z.string().optional().describe('Display name for the cell.'),
|
|
193
|
-
custom: z
|
|
396
|
+
custom: z
|
|
397
|
+
.unknown()
|
|
398
|
+
.optional()
|
|
399
|
+
.describe('Custom data attached to the cell.'),
|
|
194
400
|
borders: o({
|
|
195
401
|
top: borderSide.optional().describe('Top border override for this cell.'),
|
|
196
|
-
right: borderSide
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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).'),
|
|
215
474
|
});
|
|
216
475
|
// Effect props are nonsensical for tables and not part of its serialized form —
|
|
217
476
|
// omit them via a typed destructure (so `tableShape` keeps precise field types).
|
|
@@ -223,27 +482,84 @@ function build(o) {
|
|
|
223
482
|
height: z.number().default(200).describe('Height in pixels.'),
|
|
224
483
|
rows: z.number().default(3).describe('Number of rows.'),
|
|
225
484
|
cols: z.number().default(3).describe('Number of columns.'),
|
|
226
|
-
colWidths: z
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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.'),
|
|
238
533
|
fill: z.string().default('black').describe('Default text color for cells.'),
|
|
239
|
-
align: z
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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.'),
|
|
244
554
|
stroke: z.string().default('black').describe('Default stroke color.'),
|
|
245
|
-
cellBackground: z
|
|
246
|
-
|
|
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.'),
|
|
247
563
|
};
|
|
248
564
|
const groupShape = (element) => ({
|
|
249
565
|
id: z.string(),
|
|
@@ -257,9 +573,14 @@ function build(o) {
|
|
|
257
573
|
alwaysOnTop: z.boolean().default(false),
|
|
258
574
|
showInExport: z.boolean().default(true),
|
|
259
575
|
animations: z.array(animation).default([]),
|
|
260
|
-
children: z
|
|
576
|
+
children: z
|
|
577
|
+
.array(element)
|
|
578
|
+
.default([])
|
|
579
|
+
.describe('Child elements in this group.'),
|
|
261
580
|
});
|
|
262
|
-
const element = z.lazy(() =>
|
|
581
|
+
const element = z.lazy(() =>
|
|
582
|
+
// Keep ELEMENT_TYPES (below) in sync with this union.
|
|
583
|
+
z.discriminatedUnion('type', [
|
|
263
584
|
o(textShape),
|
|
264
585
|
o(imageShape),
|
|
265
586
|
o(svgShape),
|
|
@@ -272,7 +593,10 @@ function build(o) {
|
|
|
272
593
|
]));
|
|
273
594
|
const fontShape = {
|
|
274
595
|
fontFamily: z.string().describe('Font family name.'),
|
|
275
|
-
url: z
|
|
596
|
+
url: z
|
|
597
|
+
.string()
|
|
598
|
+
.default('')
|
|
599
|
+
.describe('URL to load the font from (single-variant fonts).'),
|
|
276
600
|
styles: z
|
|
277
601
|
.array(o({
|
|
278
602
|
src: z.string().describe('Font source, typically in CSS url() form.'),
|
|
@@ -284,39 +608,98 @@ function build(o) {
|
|
|
284
608
|
};
|
|
285
609
|
const audioShape = {
|
|
286
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.'),
|
|
287
615
|
src: z.string().default('').describe('URL or data URI of the audio file.'),
|
|
288
616
|
duration: z.number().default(0).describe('Duration in milliseconds.'),
|
|
289
617
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
290
618
|
endTime: z.number().default(1).describe('End offset (0-1).'),
|
|
291
619
|
volume: z.number().default(1).describe('Volume level (0-1).'),
|
|
292
|
-
delay: z
|
|
293
|
-
|
|
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).'),
|
|
294
628
|
custom: z.unknown().optional(),
|
|
295
629
|
};
|
|
296
630
|
const page = o({
|
|
297
631
|
id: z.string().describe('Unique identifier for the page.'),
|
|
298
|
-
width: z
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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.'),
|
|
306
664
|
custom: z.unknown().optional(),
|
|
307
|
-
duration: z
|
|
665
|
+
duration: z
|
|
666
|
+
.number()
|
|
667
|
+
.default(5000)
|
|
668
|
+
.describe('Page duration in milliseconds.'),
|
|
308
669
|
children: z.array(element).default([]).describe('Elements on this page.'),
|
|
309
670
|
});
|
|
310
671
|
const design = o({
|
|
311
|
-
|
|
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.'),
|
|
312
680
|
width: z.number().default(1080).describe('Canvas width in pixels.'),
|
|
313
681
|
height: z.number().default(1080).describe('Canvas height in pixels.'),
|
|
314
|
-
unit: z
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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.'),
|
|
318
698
|
pages: z.array(page).default([]).describe('Pages in the design.'),
|
|
319
|
-
audios: z
|
|
699
|
+
audios: z
|
|
700
|
+
.array(o(audioShape))
|
|
701
|
+
.default([])
|
|
702
|
+
.describe('Audio tracks in the design.'),
|
|
320
703
|
});
|
|
321
704
|
// Raw per-type field bags (not zod objects). The leaf schemas for the type
|
|
322
705
|
// guard are wrapped from these once at module level (below) so they aren't
|
|
@@ -413,15 +796,35 @@ export function validateDesign(input, opts = {}) {
|
|
|
413
796
|
? { valid: true, errors: [] }
|
|
414
797
|
: { valid: false, errors: toErrors(result.error.issues) };
|
|
415
798
|
}
|
|
799
|
+
// Message prefix `Invalid Polotno design:` is frozen (legacy matchers);
|
|
800
|
+
// the code + details are the stable contract going forward.
|
|
801
|
+
function throwInvalidDesign(errors) {
|
|
802
|
+
const summary = errors
|
|
803
|
+
.slice(0, 10)
|
|
804
|
+
.map((e) => ` ${e.path}: ${e.message}`)
|
|
805
|
+
.join('\n');
|
|
806
|
+
throw polotnoError('DESIGN_INVALID', `Invalid Polotno design:\n${summary}`, {
|
|
807
|
+
errors,
|
|
808
|
+
});
|
|
809
|
+
}
|
|
416
810
|
export function assertValidDesign(input, opts = {}) {
|
|
417
811
|
const { valid, errors } = validateDesign(input, opts);
|
|
418
|
-
if (!valid)
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
812
|
+
if (!valid)
|
|
813
|
+
throwInvalidDesign(errors);
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Validate AND normalize in a single schema pass (safeParse both raises the
|
|
817
|
+
* same DESIGN_INVALID as `assertValidDesign` and returns the defaults-filled
|
|
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.
|
|
820
|
+
* For canonical mode the single pass is equally faithful: input that passes
|
|
821
|
+
* the strict schema has no unknown keys, so its parse result equals the
|
|
822
|
+
* lenient one.
|
|
823
|
+
*/
|
|
824
|
+
export function parseValidDesign(input, opts = {}) {
|
|
825
|
+
const schema = opts.mode === 'canonical' ? designSchemaCanonical : designSchemaLenient;
|
|
826
|
+
const result = schema.safeParse(cloneDropNulls(input));
|
|
827
|
+
if (!result.success)
|
|
828
|
+
throwInvalidDesign(toErrors(result.error.issues));
|
|
829
|
+
return result.data;
|
|
426
830
|
}
|
|
427
|
-
//# sourceMappingURL=schema.js.map
|