@polotno/schema 0.10.0 → 0.12.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 +1 -0
- package/lib/defaults.d.ts +14 -16
- package/lib/defaults.js +5 -7
- package/lib/design.schema.json +1 -3147
- package/lib/errors.d.ts +2 -2
- package/lib/errors.js +2 -2
- package/lib/migrate.js +1 -1
- package/lib/schema.d.ts +246 -166
- package/lib/schema.js +95 -10
- package/lib/types.d.ts +17 -7
- package/package.json +4 -5
package/lib/schema.js
CHANGED
|
@@ -23,6 +23,13 @@
|
|
|
23
23
|
// format rather than a behavior of one entry point. Leaving it out did not keep
|
|
24
24
|
// the schema neutral, it just moved the decision into each renderer — see
|
|
25
25
|
// MIN_FONT_SIZE in ./defaults for what they each decided.
|
|
26
|
+
//
|
|
27
|
+
// A type used in more than one place takes `.meta({ id })`, so the generated
|
|
28
|
+
// JSON Schema defines it once under `$defs` and `$ref`s it, rather than copying
|
|
29
|
+
// it into every element (Animation alone was ~6 KB of descriptions, nine
|
|
30
|
+
// times). It changes the JSON only: zod drops `id` from the output, and the two
|
|
31
|
+
// `build()` variants are never converted together. Keep the id on the type and
|
|
32
|
+
// the `.describe()` on each use, so every property still has a description.
|
|
26
33
|
import { z } from 'zod';
|
|
27
34
|
import { polotnoError } from './errors.js';
|
|
28
35
|
import { ANIMATION_EASINGS, BLEND_MODES, GROUP_BLEND_MODES, MIN_FONT_SIZE, clampFontSize, DESIGN_SIZE, TEXT_DEFAULTS, } from './defaults.js';
|
|
@@ -36,6 +43,7 @@ export const ELEMENT_TYPES = [
|
|
|
36
43
|
'line',
|
|
37
44
|
'figure',
|
|
38
45
|
'gif',
|
|
46
|
+
'lottie',
|
|
39
47
|
'video',
|
|
40
48
|
'table',
|
|
41
49
|
'group',
|
|
@@ -45,22 +53,88 @@ export const ELEMENT_TYPES = [
|
|
|
45
53
|
// — a `strict ? z.strictObject : z.object` ternary would widen to a union and
|
|
46
54
|
// break `z.infer` of the leaf schemas (and thus the type-drift guard).
|
|
47
55
|
function build(o) {
|
|
56
|
+
// These descriptions are the public reference for writing an animation by
|
|
57
|
+
// hand: polotno.com/docs/schema/animation prints each one in a table cell of
|
|
58
|
+
// an MDX page. So a description stays on one line, with no raw `{` or `<`,
|
|
59
|
+
// and the object itself takes none (it would land in unquoted YAML
|
|
60
|
+
// frontmatter). They mirror what the canvas plays (polotno
|
|
61
|
+
// `utils/animations.ts`) and what the Animate panel offers per element type.
|
|
62
|
+
// `cameraZoom` stays out: it is going away (`docs/next-major.md`).
|
|
48
63
|
const animation = o({
|
|
49
64
|
delay: z
|
|
50
65
|
.number()
|
|
51
66
|
.default(0)
|
|
52
|
-
.describe(
|
|
67
|
+
.describe([
|
|
68
|
+
"Time in milliseconds, measured on the element's page, not on the whole design.",
|
|
69
|
+
"On 'enter': how long after the page starts the element appears. It is hidden until then.",
|
|
70
|
+
"On 'exit': how long before the page ends the element disappears. The exit finishes at that moment, and the element stays hidden after it.",
|
|
71
|
+
"On 'loop': how long after the page starts the first cycle begins. It does not wait for the entrance, so a loop can be mid-cycle when the element appears. Until then the element is at rest.",
|
|
72
|
+
'kenBurns ignores it, and letterWave also waits for an entrance that is a text preset to finish.',
|
|
73
|
+
].join(' ')),
|
|
53
74
|
duration: z
|
|
54
75
|
.number()
|
|
55
76
|
.default(500)
|
|
56
|
-
.describe(
|
|
77
|
+
.describe([
|
|
78
|
+
'Time in milliseconds.',
|
|
79
|
+
"On 'enter': how long the entrance plays, from its `delay`.",
|
|
80
|
+
"On 'exit': how long the exit plays, up to the moment the element disappears.",
|
|
81
|
+
"If the two together are longer than the element's time on the page, both play faster by the same factor to fit.",
|
|
82
|
+
"On 'loop': the length of one cycle. Cycles repeat until the element disappears or the page ends.",
|
|
83
|
+
"kenBurns ignores it and plays once across the element's whole time on the page.",
|
|
84
|
+
'The default suits a transition of the whole element. Picking a preset in the editor sets a longer one: 1000 for lineRise, wordRise, wordFade, letterPop, wordBlur, lineWipe and linePeek, 50 per letter from 1000 to 4000 for typewriter, 4000 for rotate, 2500 for float, 2000 for letterWave and glow, 1500 for blink and pulse, and 1000 for shake.',
|
|
85
|
+
].join(' ')),
|
|
57
86
|
enabled: z
|
|
58
87
|
.boolean()
|
|
59
88
|
.default(true)
|
|
60
|
-
.describe(
|
|
61
|
-
type: z
|
|
62
|
-
|
|
63
|
-
|
|
89
|
+
.describe("Whether the animation plays. A disabled 'enter' or 'exit' also loses its `delay`, so the element is on the page from its start or to its end. To time an element with no transition, keep it enabled with the name 'none'."),
|
|
90
|
+
type: z
|
|
91
|
+
.enum(['enter', 'exit', 'loop'])
|
|
92
|
+
.describe("Animation phase: 'enter' brings the element onto the page, 'exit' takes it off, and 'loop' repeats while it is there, also during the entrance and the exit."),
|
|
93
|
+
name: z
|
|
94
|
+
.string()
|
|
95
|
+
.default('none')
|
|
96
|
+
.describe([
|
|
97
|
+
'Preset to play. Which names are valid depends on `type`.',
|
|
98
|
+
"On 'enter' and 'exit': 'none' | 'fade' | 'move' | 'zoom' | 'pop' | 'rise' | 'flip' | 'growTurn' | 'wipe' | 'blur' | 'count' | 'typewriter' | 'lineRise' | 'wordRise' | 'wordFade' | 'letterPop' | 'wordBlur' | 'lineWipe' | 'linePeek'.",
|
|
99
|
+
"On 'loop': 'none' | 'rotate' | 'blink' | 'bounce' | 'pulse' | 'float' | 'shake' | 'glow' | 'kenBurns' | 'letterWave'.",
|
|
100
|
+
"Use a name only on its own type: a fade on 'loop' plays once and leaves the element hidden.",
|
|
101
|
+
'An unknown name passes validation, logs a console error and does not animate.',
|
|
102
|
+
'none is a hard cut: the element still appears or disappears at its `delay`, with no transition, and `duration` is ignored.',
|
|
103
|
+
'As entrances: fade fades in, move slides in along a straight line, zoom scales into place, pop grows into place and fades in over its first third, rise moves up into place while fading in, flip turns half round about its vertical center line from its mirrored back, growTurn turns into place from half size while fading in, wipe shows the element behind a travelling edge, blur comes into focus while fading in, and count counts the first number in the text up to its value, keeping the style of the text and the format of the number, such as its thousands separator.',
|
|
104
|
+
'An exit plays the same preset backwards, except that move, rise, growTurn and wipe carry on the way they were going.',
|
|
105
|
+
'Presets that scale keep the center of the element in place.',
|
|
106
|
+
"The text presets play on each unit of the text in turn, and `duration` covers them all: typewriter shows letters, letterPop pops letters, wordRise, wordFade and wordBlur play rise, fade and blur on words, lineRise and lineWipe play rise and wipe on lines, and linePeek slides each line into place behind a wipe's edge, clipped to the line's own row. An exit runs from the last unit back to the first, and a linePeek line carries on the way it came.",
|
|
107
|
+
'As loops: rotate turns clockwise once per cycle, blink fades out and back, pulse grows a little and back, float lifts and settles, shake swings side to side and dies out within the cycle, glow swells and fades a halo around the element, letterWave bobs each letter in turn, and kenBurns zooms, and can pan, inside the frame of the element. bounce shrinks and grows back: the editor no longer offers it, so prefer pulse.',
|
|
108
|
+
"Element limits: count, the text presets and letterWave are for 'text' elements. count plays nothing without a number in the text. Curved text or any other element plays a text preset whole, as its base preset: typewriter and wordFade as fade, lineRise and wordRise as rise, letterPop as pop, wordBlur as blur, lineWipe and linePeek as wipe and letterWave as float.",
|
|
109
|
+
"blur blurs only 'text', 'image', 'svg', 'video' and 'gif', and any other element only fades.",
|
|
110
|
+
"glow plays nothing on 'table' and 'group'. kenBurns plays on the crop, so only on 'image', 'svg', 'video' and 'gif'.",
|
|
111
|
+
'Every other preset plays on every element, groups included.',
|
|
112
|
+
].join(' ')),
|
|
113
|
+
data: z
|
|
114
|
+
.unknown()
|
|
115
|
+
.default({})
|
|
116
|
+
.describe([
|
|
117
|
+
'Object of options for the preset in `name`. Every option is optional, and a preset not listed here takes none.',
|
|
118
|
+
'`strength` is always a multiplier, default 1, and the editor offers 0.1 to 3.',
|
|
119
|
+
"move: `direction` 'right' | 'left' | 'up' | 'down' | 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left', default 'right', the way the element travels on both 'enter' and 'exit'. `strength` 1 travels 20% of the shorter side of the page: it covers that distance, not the way to the edge of the page.",
|
|
120
|
+
"zoom: `direction` 'in' | 'out', default 'in'. 'in' starts smaller and grows into place, 'out' starts larger and shrinks into place, and any other value plays as 'out'. `strength` 1 starts 25% away from full size.",
|
|
121
|
+
'pop: `strength` 1 starts from nothing, and 0.5 from half size.',
|
|
122
|
+
'rise: `strength` 1 travels 5% of the shorter side of the page.',
|
|
123
|
+
'growTurn: `strength` 1 turns 180 degrees.',
|
|
124
|
+
"wipe: `direction` 'right' | 'left' | 'up' | 'down', default 'right', the way its edge travels.",
|
|
125
|
+
'lineWipe: `direction` as wipe; without one, each line wipes in its own reading direction.',
|
|
126
|
+
"linePeek: `direction` 'right' | 'left' | 'up' | 'down', default 'up', the way each line travels.",
|
|
127
|
+
'blur: `strength` 1 adds 20 to `blurRadius` at its most blurred.',
|
|
128
|
+
"count: `from`, a number, default 0, where the count starts on 'enter' and ends on 'exit'.",
|
|
129
|
+
'bounce: `strength` 1 shrinks to a third of the size, and 0.5 to half.',
|
|
130
|
+
'pulse: `strength` 1 grows 8%.',
|
|
131
|
+
'float: `strength` 1 lifts 2% of the shorter side of the page.',
|
|
132
|
+
'shake: `strength` 1 swings 1.5% of the shorter side of the page.',
|
|
133
|
+
'letterWave: `strength` 1 lifts each letter 0.3 of its font size.',
|
|
134
|
+
"glow: `strength` 1 spreads 2% of the shorter side of the page. It has no color option: it uses the element's `shadowColor` when `shadowEnabled` is true or `shadowColor` is not 'black', else its `fill` (`color` on a line), else black. So a `shadowColor` with `shadowEnabled` false glows in that color and shows no shadow at rest.",
|
|
135
|
+
"kenBurns: `zoom` 'in' | 'out', default 'in'. 'in' narrows the saved crop, and 'out' widens to it. `direction` 'none' | 'right' | 'left' | 'up' | 'down', default 'none', the way the view pans. `strength` 1 narrows the crop by 15%.",
|
|
136
|
+
'A design below `schemaVersion` 5, or with none, has its move `strength` rescaled on load to keep the fixed 200px those versions travelled.',
|
|
137
|
+
].join(' ')),
|
|
64
138
|
// Optional with NO default, so normalizeDesign output stays unchanged for
|
|
65
139
|
// a design that never sets it. A plain string, not an enum, for the reason
|
|
66
140
|
// `textOverflow` gives below: an older SDK must read a curve added later as
|
|
@@ -69,7 +143,7 @@ function build(o) {
|
|
|
69
143
|
.string()
|
|
70
144
|
.optional()
|
|
71
145
|
.describe(`Timing curve: ${ANIMATION_EASINGS.map((e) => `'${e}'`).join(' | ')}. Missing (or unknown) plays linear. Setting a preset in the editor writes its recommended curve: fade, move, zoom, blur, count, rise, flip, growTurn and wipe ease out on enter and ease in on exit, pop overshoots (backOut on enter, backIn on exit), blink and bounce ease in and out on each sweep, kenBurns eases in and out across the element's time, a text preset eases each of its letters, words or lines as the preset it plays, and every other animation is linear.`),
|
|
72
|
-
});
|
|
146
|
+
}).meta({ id: 'Animation' });
|
|
73
147
|
const filters = z
|
|
74
148
|
.record(z.string(), o({
|
|
75
149
|
intensity: z
|
|
@@ -77,9 +151,11 @@ function build(o) {
|
|
|
77
151
|
.default(1)
|
|
78
152
|
.describe('Filter intensity (-1 to 1).'),
|
|
79
153
|
}))
|
|
154
|
+
.meta({ id: 'Filters' })
|
|
80
155
|
.describe('Applied image filters keyed by filter name.');
|
|
81
156
|
const blendMode = z
|
|
82
157
|
.enum(BLEND_MODES)
|
|
158
|
+
.meta({ id: 'BlendMode' })
|
|
83
159
|
.optional()
|
|
84
160
|
.describe('How the element composites with the content beneath it. Omitted means normal.');
|
|
85
161
|
// Fields shared by every Shape-derived element. Defaults inline.
|
|
@@ -101,7 +177,7 @@ function build(o) {
|
|
|
101
177
|
animations: z
|
|
102
178
|
.array(animation)
|
|
103
179
|
.default([])
|
|
104
|
-
.describe('
|
|
180
|
+
.describe("The element's animations, at most one of each `type`, so an 'enter', an 'exit' and a 'loop' can play together. A group's animations play on top of its children's."),
|
|
105
181
|
blurEnabled: z
|
|
106
182
|
.boolean()
|
|
107
183
|
.default(false)
|
|
@@ -422,6 +498,12 @@ function build(o) {
|
|
|
422
498
|
.default(true)
|
|
423
499
|
.describe('Whether to maintain aspect ratio when resizing.'),
|
|
424
500
|
};
|
|
501
|
+
const lottieShape = {
|
|
502
|
+
...shapeBase,
|
|
503
|
+
...imageProps,
|
|
504
|
+
type: z.literal('lottie').describe('Element type identifier.'),
|
|
505
|
+
keepRatio: gifShape.keepRatio,
|
|
506
|
+
};
|
|
425
507
|
const videoShape = {
|
|
426
508
|
...shapeBase,
|
|
427
509
|
...imageProps,
|
|
@@ -445,7 +527,7 @@ function build(o) {
|
|
|
445
527
|
.enum(['solid', 'dashed', 'dotted', 'none'])
|
|
446
528
|
.optional()
|
|
447
529
|
.describe('Border style for this side.'),
|
|
448
|
-
});
|
|
530
|
+
}).meta({ id: 'BorderSide' });
|
|
449
531
|
// Cell styling fields are optional (inherit from the table when omitted).
|
|
450
532
|
const tableCell = o({
|
|
451
533
|
id: z.string().describe('Unique identifier for the cell.'),
|
|
@@ -665,6 +747,7 @@ function build(o) {
|
|
|
665
747
|
o(lineShape),
|
|
666
748
|
o(figureShape),
|
|
667
749
|
o(gifShape),
|
|
750
|
+
o(lottieShape),
|
|
668
751
|
o(videoShape),
|
|
669
752
|
o(tableShape),
|
|
670
753
|
o(groupShape(element)),
|
|
@@ -754,7 +837,7 @@ function build(o) {
|
|
|
754
837
|
schemaVersion: z
|
|
755
838
|
.number()
|
|
756
839
|
.default(CURRENT_SCHEMA_VERSION)
|
|
757
|
-
.describe('Schema version for compatibility / migration.'),
|
|
840
|
+
.describe('Schema version for compatibility / migration. A design without it counts as version 0, the oldest format, and is migrated on load, so write the current version in new JSON.'),
|
|
758
841
|
width: z.number().default(DESIGN_SIZE).describe('Canvas width in pixels.'),
|
|
759
842
|
height: z
|
|
760
843
|
.number()
|
|
@@ -792,6 +875,7 @@ function build(o) {
|
|
|
792
875
|
line: lineShape,
|
|
793
876
|
figure: figureShape,
|
|
794
877
|
gif: gifShape,
|
|
878
|
+
lottie: lottieShape,
|
|
795
879
|
video: videoShape,
|
|
796
880
|
table: tableShape,
|
|
797
881
|
font: fontShape,
|
|
@@ -820,6 +904,7 @@ export const leafSchemas = {
|
|
|
820
904
|
line: z.object(lenient.shapes.line),
|
|
821
905
|
figure: z.object(lenient.shapes.figure),
|
|
822
906
|
gif: z.object(lenient.shapes.gif),
|
|
907
|
+
lottie: z.object(lenient.shapes.lottie),
|
|
823
908
|
video: z.object(lenient.shapes.video),
|
|
824
909
|
table: z.object(lenient.shapes.table),
|
|
825
910
|
font: z.object(lenient.shapes.font),
|
package/lib/types.d.ts
CHANGED
|
@@ -86,20 +86,27 @@ export interface Page {
|
|
|
86
86
|
/** Elements on this page. */
|
|
87
87
|
children: Element[];
|
|
88
88
|
}
|
|
89
|
-
export type Element = TextElement | ImageElement | SVGElement | LineElement | VideoElement | FigureElement | GifElement | GroupElement | TableElement;
|
|
89
|
+
export type Element = TextElement | ImageElement | SVGElement | LineElement | VideoElement | FigureElement | GifElement | LottieElement | GroupElement | TableElement;
|
|
90
90
|
export type ElementType = Element['type'];
|
|
91
|
+
/**
|
|
92
|
+
* The JSON Schema's descriptions (`design.schema.json`) are the full reference:
|
|
93
|
+
* the preset names for each `type`, their options and element limits.
|
|
94
|
+
*/
|
|
91
95
|
export interface Animation {
|
|
92
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Milliseconds on the element's page: after its start on `enter` and `loop`,
|
|
98
|
+
* before its end on `exit`.
|
|
99
|
+
*/
|
|
93
100
|
delay: number;
|
|
94
|
-
/**
|
|
101
|
+
/** Length in milliseconds: of the transition, or of one `loop` cycle. */
|
|
95
102
|
duration: number;
|
|
96
|
-
/** Whether
|
|
103
|
+
/** Whether it plays. A disabled `enter` or `exit` also loses its `delay`. */
|
|
97
104
|
enabled: boolean;
|
|
98
105
|
/** Animation phase. */
|
|
99
106
|
type: 'enter' | 'exit' | 'loop';
|
|
100
|
-
/**
|
|
107
|
+
/** Preset to play. Which names are valid depends on `type`. */
|
|
101
108
|
name: string;
|
|
102
|
-
/**
|
|
109
|
+
/** Options for the preset in `name`. */
|
|
103
110
|
data: unknown;
|
|
104
111
|
/**
|
|
105
112
|
* Timing curve, one of {@link AnimationEasing}. Missing (or unknown) plays
|
|
@@ -131,7 +138,7 @@ export interface ShapeAttributes {
|
|
|
131
138
|
opacity: number;
|
|
132
139
|
/** How the element composites with the content beneath it. Omitted means normal. */
|
|
133
140
|
blendMode?: BlendMode;
|
|
134
|
-
/**
|
|
141
|
+
/** The element's animations, at most one of each `type`. */
|
|
135
142
|
animations: Animation[];
|
|
136
143
|
blurEnabled: boolean;
|
|
137
144
|
blurRadius: number;
|
|
@@ -295,6 +302,9 @@ export interface GifElement extends ShapeAttributes, BaseImageProps {
|
|
|
295
302
|
/** Duration in milliseconds. */
|
|
296
303
|
duration: number;
|
|
297
304
|
}
|
|
305
|
+
export interface LottieElement extends ShapeAttributes, BaseImageProps {
|
|
306
|
+
type: 'lottie';
|
|
307
|
+
}
|
|
298
308
|
export interface VideoElement extends ShapeAttributes, BaseImageProps {
|
|
299
309
|
type: 'video';
|
|
300
310
|
/** Duration in milliseconds. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polotno/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Canonical Polotno design format: types, normalize, validate, JSON Schema. Authored in zod, DOM-free.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"type": "module",
|
|
@@ -49,13 +49,12 @@
|
|
|
49
49
|
"zod": "^4.6.7"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"typescript": "~
|
|
53
|
-
"vitest": "^5.0
|
|
52
|
+
"typescript": "~7.0.2",
|
|
53
|
+
"vitest": "^5.1.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "rm -rf lib && tsc && node scripts/generate-schema.mjs && node ../../scripts/check-pack.mjs",
|
|
57
|
-
"
|
|
58
|
-
"typecheck": "node ../../scripts/typecheck.mjs --noEmit",
|
|
57
|
+
"typecheck": "tsc --noEmit && tsc -p tests/tsconfig.json",
|
|
59
58
|
"test": "vitest run"
|
|
60
59
|
}
|
|
61
60
|
}
|