@polotno/schema 0.1.6 → 0.3.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.
Files changed (52) hide show
  1. package/lib/bleed.d.ts +0 -1
  2. package/lib/bleed.js +0 -1
  3. package/lib/defaults.d.ts +24 -434
  4. package/lib/defaults.js +62 -243
  5. package/lib/design.schema.json +5 -0
  6. package/lib/errors.d.ts +34 -0
  7. package/lib/errors.js +22 -0
  8. package/lib/index.d.ts +2 -1
  9. package/lib/index.js +2 -1
  10. package/lib/normalize.d.ts +20 -3
  11. package/lib/normalize.js +192 -44
  12. package/lib/schema.d.ts +11 -1
  13. package/lib/schema.js +61 -22
  14. package/lib/types-parity.d.ts +0 -1
  15. package/lib/types-parity.js +0 -1
  16. package/lib/types.d.ts +2 -1
  17. package/lib/types.js +0 -1
  18. package/package.json +8 -8
  19. package/lib/bleed.d.ts.map +0 -1
  20. package/lib/bleed.js.map +0 -1
  21. package/lib/constants.d.ts +0 -6
  22. package/lib/constants.d.ts.map +0 -1
  23. package/lib/constants.js +0 -61
  24. package/lib/constants.js.map +0 -1
  25. package/lib/defaults.d.ts.map +0 -1
  26. package/lib/defaults.js.map +0 -1
  27. package/lib/index.d.ts.map +0 -1
  28. package/lib/index.js.map +0 -1
  29. package/lib/migrate.d.ts +0 -6
  30. package/lib/migrate.d.ts.map +0 -1
  31. package/lib/migrate.js +0 -58
  32. package/lib/migrate.js.map +0 -1
  33. package/lib/normalize.d.ts.map +0 -1
  34. package/lib/normalize.js.map +0 -1
  35. package/lib/processors.d.ts +0 -13
  36. package/lib/processors.d.ts.map +0 -1
  37. package/lib/processors.js +0 -170
  38. package/lib/processors.js.map +0 -1
  39. package/lib/schema.d.ts.map +0 -1
  40. package/lib/schema.js.map +0 -1
  41. package/lib/traverse.d.ts +0 -3
  42. package/lib/traverse.d.ts.map +0 -1
  43. package/lib/traverse.js +0 -32
  44. package/lib/traverse.js.map +0 -1
  45. package/lib/types-parity.d.ts.map +0 -1
  46. package/lib/types-parity.js.map +0 -1
  47. package/lib/types.d.ts.map +0 -1
  48. package/lib/types.js.map +0 -1
  49. package/lib/validate.d.ts +0 -19
  50. package/lib/validate.d.ts.map +0 -1
  51. package/lib/validate.js +0 -383
  52. package/lib/validate.js.map +0 -1
package/lib/defaults.js CHANGED
@@ -1,254 +1,73 @@
1
- // Canonical default values for every Polotno element / container, extracted
2
- // verbatim from the core MST models (`packages/polotno/src/model/*-model.ts`).
3
- //
4
- // `normalizeDesign` / `validateDesign` read these. INTERIM STATE: core still
5
- // declares its own `.props({...})` literals (e.g. text-model.ts) — these tables
6
- // are currently a mirror, NOT yet the single source. The golden equivalence
7
- // test (`packages/polotno/src/model/schema-equivalence.test.ts`) makes any drift
8
- // between the two fail CI. Making core spread these back (so each literal lives
9
- // exactly once) is the deferred follow-up — see the plan's task #5.
10
- //
11
- // Rules:
12
- // - Only SCALAR defaults live here. Structural MST-typed fields (arrays, maps:
13
- // `animations`, `filters`, `children`, `cells`, `dash`, `colorsReplace`,
14
- // `colWidths`, `rowHeights`) are handled by the normalizer, not stored here.
15
- // - `id` (identifier) has no default; `custom` (frozen) defaults to undefined and
16
- // is only present when set — neither is listed.
17
- // - Private (`_`-prefixed) props are stripped on serialization — not listed.
18
- // - `legacyBackground` (maybe) has no default — only present when a migration sets it.
19
- export const NODE_DEFAULTS = {
20
- type: 'none',
21
- name: '',
22
- opacity: 1,
23
- visible: true,
24
- selectable: true,
25
- removable: true,
26
- alwaysOnTop: false,
27
- showInExport: true,
28
- };
29
- export const SHAPE_DEFAULTS = {
30
- ...NODE_DEFAULTS,
31
- x: 0,
32
- y: 0,
33
- width: 100,
34
- height: 100,
35
- rotation: 0,
36
- opacity: 1,
37
- blurEnabled: false,
38
- blurRadius: 10,
39
- brightnessEnabled: false,
40
- brightness: 0,
41
- sepiaEnabled: false,
42
- grayscaleEnabled: false,
43
- shadowEnabled: false,
44
- shadowBlur: 5,
45
- shadowOffsetX: 0,
46
- shadowOffsetY: 0,
47
- shadowColor: 'black',
48
- shadowOpacity: 1,
49
- visible: true,
50
- draggable: true,
51
- resizable: true,
52
- selectable: true,
53
- contentEditable: true,
54
- styleEditable: true,
55
- alwaysOnTop: false,
56
- showInExport: true,
57
- };
58
- export const IMAGE_DEFAULTS = {
59
- ...SHAPE_DEFAULTS,
60
- type: 'image',
61
- width: 100,
62
- height: 100,
63
- src: '',
64
- cropX: 0,
65
- cropY: 0,
66
- cropWidth: 1,
67
- cropHeight: 1,
68
- cornerRadius: 0,
69
- flipX: false,
70
- flipY: false,
71
- clipSrc: '',
72
- borderColor: 'black',
73
- borderSize: 0,
74
- keepRatio: false,
75
- stretchEnabled: false,
1
+ // Canonical text RENDERING defaults — the values every renderer assumes when a
2
+ // field is missing. Single source of truth: the zod text schema builds its
3
+ // `.default()`s from this map, and @polotno/core (`text-types.ts`) applies it
4
+ // to loose, un-normalized elements at render/measure time. zod-free on purpose
5
+ // so runtime consumers can import `@polotno/schema/defaults` without pulling
6
+ // the validator.
7
+ // Table-cell properties that inherit from the table element when a cell
8
+ // leaves them undefined. Shared by the editor's table model and the svg/html
9
+ // exporters so the inheritance vocabulary can't drift. (pdf-export uses a
10
+ // deliberate subset without stroke/strokeWidth — see its table.ts.)
11
+ export const TABLE_CELL_OVERRIDABLE_KEYS = [
12
+ 'fontSize',
13
+ 'fontFamily',
14
+ 'fontWeight',
15
+ 'fontStyle',
16
+ 'textDecoration',
17
+ 'textTransform',
18
+ 'fill',
19
+ 'align',
20
+ 'verticalAlign',
21
+ 'lineHeight',
22
+ 'letterSpacing',
23
+ 'strokeWidth',
24
+ 'stroke',
25
+ 'cellBackground',
26
+ 'cellPadding',
27
+ ];
28
+ /**
29
+ * Resolve cell inheritance: every overridable text property a cell leaves
30
+ * undefined is filled from the table element (canvas parity — no fallback
31
+ * literals downstream). Returns shallow clones; the input is untouched
32
+ * (source may be frozen JSON).
33
+ */
34
+ export const resolveTableCellDefaults = (element) => (element.cells || []).map((cell) => {
35
+ if (!cell)
36
+ return cell;
37
+ const c = { ...cell };
38
+ for (const key of TABLE_CELL_OVERRIDABLE_KEYS) {
39
+ if (c[key] === undefined)
40
+ c[key] = element[key];
41
+ }
42
+ return c;
43
+ });
44
+ /**
45
+ * Per-side border resolution: a cell's `borders[side]` overrides win over
46
+ * the table element's `border{Width,Style,Color}` (canvas table-model
47
+ * parity). Shared by the svg/html/pdf exporters.
48
+ */
49
+ export const resolveTableCellBorder = (cell, element, side) => {
50
+ const overrides = cell.borders?.[side];
51
+ return {
52
+ width: overrides?.width ?? element.borderWidth,
53
+ style: overrides?.style ?? element.borderStyle,
54
+ color: overrides?.color ?? element.borderColor,
55
+ };
76
56
  };
77
57
  export const TEXT_DEFAULTS = {
78
- ...SHAPE_DEFAULTS,
79
- type: 'text',
80
- text: '',
81
- placeholder: '',
82
58
  fontSize: 14,
83
59
  fontFamily: 'Roboto',
84
- fontStyle: 'normal',
85
60
  fontWeight: 'normal',
86
- textDecoration: '',
87
- textTransform: 'none',
88
- fill: 'black',
89
- align: 'center',
90
- width: 100,
91
- height: 0, // afterCreate sets round(fontSize * lineHeight) when falsy
92
- verticalAlign: 'top',
93
- strokeWidth: 0,
94
- stroke: 'black',
61
+ fontStyle: 'normal',
95
62
  lineHeight: 1.2,
96
63
  letterSpacing: 0,
97
- backgroundEnabled: false,
98
- backgroundColor: '#7ED321',
99
- backgroundOpacity: 1,
100
- backgroundCornerRadius: 0.5,
101
- backgroundPadding: 0.5,
102
- curveEnabled: false,
103
- curvePower: 0.5,
104
- };
105
- export const SVG_DEFAULTS = {
106
- ...SHAPE_DEFAULTS,
107
- type: 'svg',
108
- src: '',
109
- maskSrc: '',
110
- cropX: 0,
111
- cropY: 0,
112
- cropWidth: 1,
113
- cropHeight: 1,
114
- keepRatio: true,
115
- stretchEnabled: true,
116
- flipX: false,
117
- flipY: false,
118
- width: 100,
119
- height: 100,
120
- borderColor: 'black',
121
- borderSize: 0,
122
- cornerRadius: 0,
123
- };
124
- export const LINE_DEFAULTS = {
125
- ...SHAPE_DEFAULTS,
126
- type: 'line',
127
- width: 400,
128
- height: 10,
129
- color: 'black',
130
- startHead: '',
131
- endHead: '',
132
- };
133
- export const FIGURE_DEFAULTS = {
134
- ...SHAPE_DEFAULTS,
135
- type: 'figure',
136
- subType: 'rect',
137
- fill: 'rgb(0, 161, 255)',
138
- strokeWidth: 0,
139
- stroke: 'rgba(98, 197, 255, 1)',
140
- cornerRadius: 0,
141
- };
142
- export const GIF_DEFAULTS = {
143
- ...IMAGE_DEFAULTS,
144
- type: 'gif',
145
- duration: 0,
146
- keepRatio: true,
147
- };
148
- export const VIDEO_DEFAULTS = {
149
- ...IMAGE_DEFAULTS,
150
- type: 'video',
151
- duration: 0,
152
- startTime: 0,
153
- endTime: 1,
154
- volume: 1,
155
- speed: 1,
156
- };
157
- // TableElement inherits Shape (so blur/shadow are FILLED then stripped by the
158
- // table postProcessor — keep them here to match MST's fill-then-strip).
159
- export const TABLE_DEFAULTS = {
160
- ...SHAPE_DEFAULTS,
161
- type: 'table',
162
- width: 300,
163
- height: 200,
164
- rows: 3,
165
- cols: 3,
166
- borderColor: '#000000',
167
- borderWidth: 1,
168
- borderStyle: 'solid',
169
- fontSize: 30,
170
- fontFamily: 'Roboto',
171
- fontWeight: 'normal',
172
- fontStyle: 'normal',
173
- textDecoration: '',
174
64
  textTransform: 'none',
175
- fill: 'black',
65
+ textDecoration: '',
176
66
  align: 'center',
177
- verticalAlign: 'middle',
178
- lineHeight: 1.2,
179
- letterSpacing: 0,
180
- strokeWidth: 0,
67
+ fill: 'black',
181
68
  stroke: 'black',
182
- cellBackground: 'transparent',
183
- cellPadding: 4,
184
- };
185
- export const GROUP_DEFAULTS = {
186
- ...NODE_DEFAULTS,
187
- type: 'group',
188
- };
189
- // TableCell direct defaults (overridable `_`-prefixed styling props inherit from
190
- // the table and are not defaulted here).
191
- export const TABLE_CELL_DEFAULTS = {
192
- type: 'tablecell',
193
- text: '',
194
- opacity: 1,
195
- rowSpan: 1,
196
- colSpan: 1,
197
- name: '',
198
- };
199
- export const PAGE_DEFAULTS = {
200
- width: 'auto',
201
- height: 'auto',
202
- background: 'white',
203
- bleed: 0,
204
- duration: 5000,
205
- };
206
- export const AUDIO_DEFAULTS = {
207
- src: '',
208
- duration: 0,
209
- startTime: 0,
210
- endTime: 1,
211
- volume: 1,
212
- delay: 0,
213
- speed: 1,
214
- };
215
- export const FONT_DEFAULTS = {
216
- url: '',
217
- };
218
- // Serialized store-level fields (the subset `store.toJSON()` emits).
219
- export const STORE_DEFAULTS = {
220
- width: 1080,
221
- height: 1080,
222
- unit: 'px',
223
- dpi: 72,
224
- schemaVersion: 4,
225
- };
226
- export const CURRENT_SCHEMA_VERSION = 4;
227
- // Maps a built-in element `type` to its default table. Unknown/custom types
228
- // fall back to SHAPE_DEFAULTS (base normalization + passthrough).
229
- export const DEFAULTS_BY_TYPE = {
230
- image: IMAGE_DEFAULTS,
231
- text: TEXT_DEFAULTS,
232
- svg: SVG_DEFAULTS,
233
- line: LINE_DEFAULTS,
234
- figure: FIGURE_DEFAULTS,
235
- gif: GIF_DEFAULTS,
236
- video: VIDEO_DEFAULTS,
237
- table: TABLE_DEFAULTS,
238
- group: GROUP_DEFAULTS,
239
- };
240
- // Structural (MST-typed) empty defaults per element type — the arrays/maps MST
241
- // serializes as []/{} (DEFAULTS_BY_TYPE carries only scalars). Single source for
242
- // both normalize (fills them) and validate (marks them optional in partial mode).
243
- export const STRUCTURAL_DEFAULTS_BY_TYPE = {
244
- image: { animations: [], filters: {} },
245
- text: { animations: [], filters: {} },
246
- svg: { animations: [], filters: {}, colorsReplace: {} },
247
- line: { animations: [], filters: {}, dash: [] },
248
- figure: { animations: [], filters: {}, dash: [] },
249
- gif: { animations: [], filters: {} },
250
- video: { animations: [], filters: {} },
251
- table: { animations: [], filters: {}, colWidths: [], rowHeights: [], cells: [] },
252
- group: { animations: [], children: [] },
69
+ strokeWidth: 0,
70
+ strokeLineJoin: 'round',
71
+ curveEnabled: false,
72
+ curvePower: 0.5,
253
73
  };
254
- //# sourceMappingURL=defaults.js.map
@@ -504,6 +504,11 @@
504
504
  "description": "Stroke color.",
505
505
  "type": "string"
506
506
  },
507
+ "strokeLineJoin": {
508
+ "default": "round",
509
+ "description": "Stroke corner join: round, miter, bevel.",
510
+ "type": "string"
511
+ },
507
512
  "lineHeight": {
508
513
  "default": 1.2,
509
514
  "description": "Line height multiplier or 'auto'.",
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Structured errors, Node.js-style: a plain `Error` with a semver-stable
3
+ * `code` property (branch on it, like `err.code === 'ENOENT'`) and optional
4
+ * JSON-serializable `details`. Messages are for humans and may change in any
5
+ * release — never match on them. No classes, no instanceof — a code check
6
+ * works on any copy of any package. See the error-codes table in the repo
7
+ * root CLAUDE.md.
8
+ *
9
+ * `@polotno/schema` is the layer *below* `@polotno/core`, so it can't import
10
+ * core's shared error map — it defines its own. It only ever throws
11
+ * `DESIGN_INVALID`; the `details` shape here is kept identical to core's entry
12
+ * for that code, so a consumer sees the same fields whichever layer threw it.
13
+ */
14
+ /** Codes thrown by this package. Append-only. */
15
+ export type PolotnoErrorCode = 'DESIGN_INVALID';
16
+ /** Exhaustive `details` shape per code (only `DESIGN_INVALID` here). */
17
+ export interface PolotnoErrorDetailsMap {
18
+ DESIGN_INVALID: {
19
+ errors: {
20
+ path: string;
21
+ message: string;
22
+ }[];
23
+ /** Recognised element types, when the design references an unknown one. */
24
+ knownTypes?: string[];
25
+ };
26
+ }
27
+ export type PolotnoError<Code extends PolotnoErrorCode = PolotnoErrorCode> = {
28
+ [K in Code]: Error & {
29
+ code: K;
30
+ details?: PolotnoErrorDetailsMap[K];
31
+ cause?: unknown;
32
+ };
33
+ }[Code];
34
+ export declare function polotnoError<Code extends PolotnoErrorCode>(code: Code, message: string, details?: PolotnoErrorDetailsMap[Code], cause?: unknown): PolotnoError<Code>;
package/lib/errors.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Structured errors, Node.js-style: a plain `Error` with a semver-stable
3
+ * `code` property (branch on it, like `err.code === 'ENOENT'`) and optional
4
+ * JSON-serializable `details`. Messages are for humans and may change in any
5
+ * release — never match on them. No classes, no instanceof — a code check
6
+ * works on any copy of any package. See the error-codes table in the repo
7
+ * root CLAUDE.md.
8
+ *
9
+ * `@polotno/schema` is the layer *below* `@polotno/core`, so it can't import
10
+ * core's shared error map — it defines its own. It only ever throws
11
+ * `DESIGN_INVALID`; the `details` shape here is kept identical to core's entry
12
+ * for that code, so a consumer sees the same fields whichever layer threw it.
13
+ */
14
+ export function polotnoError(code, message, details, cause) {
15
+ const err = new Error(message);
16
+ err.code = code;
17
+ if (details !== undefined)
18
+ err.details = details;
19
+ if (cause !== undefined)
20
+ err.cause = cause;
21
+ return err;
22
+ }
package/lib/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ export type { PolotnoError, PolotnoErrorCode } from './errors.js';
1
2
  export { normalizeDesign, parseDesign } from './normalize.js';
2
3
  export type { ParseOptions } from './normalize.js';
3
4
  export { resolveBleed, ZERO_BLEED } from './bleed.js';
4
5
  export type { ResolvedBleed } from './bleed.js';
6
+ export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, } from './defaults.js';
5
7
  export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
6
8
  export type { DesignInput, ValidationError, ValidationResult, ValidateOptions, } from './schema.js';
7
9
  export type * from './types.js';
8
- //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -4,5 +4,6 @@
4
4
  // types. zod is a regular dependency (normalize parses with it).
5
5
  export { normalizeDesign, parseDesign } from './normalize.js';
6
6
  export { resolveBleed, ZERO_BLEED } from './bleed.js';
7
+ // Also available zod-free via the `@polotno/schema/defaults` subpath.
8
+ export { TEXT_DEFAULTS, TABLE_CELL_OVERRIDABLE_KEYS, resolveTableCellDefaults, resolveTableCellBorder, } from './defaults.js';
7
9
  export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
8
- //# sourceMappingURL=index.js.map
@@ -12,10 +12,28 @@ export interface ParseOptions extends ValidateOptions {
12
12
  * Assign a synthetic `id` to any page/element/cell that lacks one before
13
13
  * validating. `id` is otherwise required, but it's identity metadata the
14
14
  * renderer doesn't draw, so consumers that accept hand-built JSON (the export
15
- * converters) stay tolerant. Real `store.toJSON()` output always has ids, so
16
- * this is a no-op there. Does not touch the caller's input (works on a copy).
15
+ * converters) stay tolerant. Ids are DETERMINISTIC (structural index paths
16
+ * like `el-0-2`) so identical input yields identical output. Real
17
+ * `store.toJSON()` output always has ids, so this is a no-op there. Does not
18
+ * touch the caller's input (works on a copy).
17
19
  */
18
20
  fillMissingIds?: boolean;
21
+ /**
22
+ * Editor-export compatibility: preserve client-owned content the way the
23
+ * editor's own SVG/HTML export historically did (it rendered raw JSON).
24
+ * - Elements whose type is outside the schema union (the editor's
25
+ * `registerShapeModel` extensions) skip validation and stay in place —
26
+ * handed through as shallow copies of the CALLER's objects (`id` filled
27
+ * when `fillMissingIds` is set), so they survive to element hooks.
28
+ * Renderers fall back to a placeholder for them.
29
+ * - Unrecognized ad-hoc keys the zod pass strips are copied back onto
30
+ * parsed elements/pages/cells/audios from the caller's input.
31
+ * Schema-recognized keys keep their validated values; `null` stays
32
+ * "unset".
33
+ * - `custom` needs no special handling: it is JSON-like data by contract
34
+ * and the schema keeps it everywhere, so it survives parsing as-is.
35
+ */
36
+ preserveClientData?: boolean;
19
37
  }
20
38
  /**
21
39
  * The one entry a consumer should use to accept design JSON: validate, then
@@ -24,4 +42,3 @@ export interface ParseOptions extends ValidateOptions {
24
42
  * with `assertValidDesign`.
25
43
  */
26
44
  export declare function parseDesign(input: unknown, opts?: ParseOptions): Design;
27
- //# sourceMappingURL=normalize.d.ts.map