@polotno/schema 0.4.0 → 0.6.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/LICENSE.md CHANGED
@@ -1,10 +1,17 @@
1
- Polotno SDK License
1
+ # Polotno Schema License
2
2
 
3
- Copyright (c) 2014–present Anton Lavrenov.
3
+ Copyright (c) 2014–present Anton Lavrenov. All rights reserved.
4
4
 
5
- This package is part of the Polotno SDK. It is not open source. Use in
6
- production requires a valid Polotno subscription.
5
+ You can use and change this package for free, with no time limit, including in
6
+ production. The Polotno design format is open: any person, and any AI agent, can
7
+ read and write it.
7
8
 
8
- Full license terms: https://polotno.com/legal/license
9
+ This package is not open source. Do not republish it as a library, and do not
10
+ sublicense it.
9
11
 
10
- The software is provided "as is", without warranty of any kind.
12
+ This package is provided "as is", without warranty. To the maximum limit that
13
+ the law permits, the copyright holder has no liability for a claim or for
14
+ damages.
15
+
16
+ Subscription terms for other Polotno packages:
17
+ https://polotno.com/legal/license
package/README.md CHANGED
@@ -76,6 +76,4 @@ import type { Design, Page, Element, TextElement } from '@polotno/schema';
76
76
 
77
77
  ## License
78
78
 
79
- Part of the Polotno SDK — not open source; production use requires a valid Polotno
80
- subscription. See [LICENSE.md](./LICENSE.md) ·
81
- [full terms](https://polotno.com/legal/license).
79
+ See [LICENSE.md](./LICENSE.md).
package/lib/defaults.d.ts CHANGED
@@ -53,3 +53,29 @@ export declare const TEXT_DEFAULTS: {
53
53
  readonly curveEnabled: false;
54
54
  readonly curvePower: 0.5;
55
55
  };
56
+ /**
57
+ * The smallest font size a text element, a table or a table cell can hold. Not
58
+ * a rendering preference — an INVARIANT of the format: each of those models
59
+ * clamps `fontSize` below it at both doors, a loaded snapshot and `set()`, so
60
+ * no element the canvas has ever touched holds less. Clamping one door only is
61
+ * how the table path drifted: `set()` held the floor while a loaded design
62
+ * kept its sub-1 size, and the canvas and the exporters disagreed again.
63
+ * `schema-equivalence.test.ts` is the seam that proves they no longer do.
64
+ *
65
+ * The schema clamps to it for the same reason: a renderer that meets a smaller
66
+ * value has no canvas behavior to mirror, so it invents one. pdf-export read
67
+ * `font-size: 0px` as *absent* and inherited a 16px root default, svg-export's
68
+ * shrink loop walked to `-1px`, html-export emitted an invisible `0px`. Three
69
+ * renderers, three answers, none of them the canvas.
70
+ *
71
+ * @polotno/core's `FIT_MIN_FONT_SIZE` (the floor its fit loops converge to) and
72
+ * the editor's model clamps both derive from this — one rule, stated once.
73
+ */
74
+ export declare const MIN_FONT_SIZE = 1;
75
+ /**
76
+ * Apply that floor. The one implementation: the zod schema, the text model and
77
+ * the table model all call this, so the rule cannot be spelled two ways.
78
+ * `undefined` passes through — an omitted table-cell size means "inherit from
79
+ * the table", which is not a size to clamp.
80
+ */
81
+ export declare const clampFontSize: <T extends number | undefined>(size: T) => T;
package/lib/defaults.js CHANGED
@@ -101,3 +101,29 @@ export const TEXT_DEFAULTS = {
101
101
  curveEnabled: false,
102
102
  curvePower: 0.5,
103
103
  };
104
+ /**
105
+ * The smallest font size a text element, a table or a table cell can hold. Not
106
+ * a rendering preference — an INVARIANT of the format: each of those models
107
+ * clamps `fontSize` below it at both doors, a loaded snapshot and `set()`, so
108
+ * no element the canvas has ever touched holds less. Clamping one door only is
109
+ * how the table path drifted: `set()` held the floor while a loaded design
110
+ * kept its sub-1 size, and the canvas and the exporters disagreed again.
111
+ * `schema-equivalence.test.ts` is the seam that proves they no longer do.
112
+ *
113
+ * The schema clamps to it for the same reason: a renderer that meets a smaller
114
+ * value has no canvas behavior to mirror, so it invents one. pdf-export read
115
+ * `font-size: 0px` as *absent* and inherited a 16px root default, svg-export's
116
+ * shrink loop walked to `-1px`, html-export emitted an invisible `0px`. Three
117
+ * renderers, three answers, none of them the canvas.
118
+ *
119
+ * @polotno/core's `FIT_MIN_FONT_SIZE` (the floor its fit loops converge to) and
120
+ * the editor's model clamps both derive from this — one rule, stated once.
121
+ */
122
+ export const MIN_FONT_SIZE = 1;
123
+ /**
124
+ * Apply that floor. The one implementation: the zod schema, the text model and
125
+ * the table model all call this, so the rule cannot be spelled two ways.
126
+ * `undefined` passes through — an omitted table-cell size means "inherit from
127
+ * the table", which is not a size to clamp.
128
+ */
129
+ export const clampFontSize = (size) => (size != null && size < MIN_FONT_SIZE ? MIN_FONT_SIZE : size);
@@ -456,7 +456,7 @@
456
456
  },
457
457
  "fontSize": {
458
458
  "default": 14,
459
- "description": "Font size in pixels.",
459
+ "description": "Font size in pixels (minimum 1).",
460
460
  "type": "number"
461
461
  },
462
462
  "fontFamily": {
@@ -2573,7 +2573,7 @@
2573
2573
  }
2574
2574
  },
2575
2575
  "fontSize": {
2576
- "description": "Font size in pixels (inherits from the table when omitted).",
2576
+ "description": "Font size in pixels (inherits from the table when omitted, minimum 1).",
2577
2577
  "type": "number"
2578
2578
  },
2579
2579
  "fontFamily": {
@@ -2668,7 +2668,7 @@
2668
2668
  },
2669
2669
  "fontSize": {
2670
2670
  "default": 30,
2671
- "description": "Default font size in pixels for cells.",
2671
+ "description": "Default font size in pixels for cells (minimum 1).",
2672
2672
  "type": "number"
2673
2673
  },
2674
2674
  "fontFamily": {
package/lib/migrate.d.ts CHANGED
@@ -7,9 +7,15 @@ export interface MigrateOptions {
7
7
  */
8
8
  modernTextRenderer?: boolean;
9
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.
10
+ * The v2→3 background policy. Three states, no new argument needed:
11
+ *
12
+ * - `undefined` (default) — **auto**: infer the pre-v3 renderer from the
13
+ * design, as `usesRichTextEditor` below explains.
14
+ * - `true` — **per-line**: never stamp; every pre-v3 background renders as
15
+ * the per-line polygon.
16
+ * - `false` — **full-rect**: always stamp; every pre-v3 background renders as
17
+ * a full rect. The escape hatch for a rich-text client whose designs happen
18
+ * to hold only bare strings, which auto cannot tell from a plain design.
13
19
  */
14
20
  modernizeTextBackground?: boolean;
15
21
  }
package/lib/migrate.js CHANGED
@@ -18,10 +18,63 @@
18
18
  // one. Designs written before the field existed are precisely the ones that
19
19
  // need migrating, so absence has to mean "old". `store.toJSON()` always
20
20
  // stamps the current version, so a round-tripped design is never re-migrated.
21
+ //
22
+ // **Overriding a migration? Migrate ONCE, then reuse the result.** The options
23
+ // below reach `store.loadJSON`, but the converters call `parseDesign`/
24
+ // `normalizeDesign`, which take no migration options and never will — a design's
25
+ // meaning must not depend on which entry point opened it. So resolve the intent
26
+ // yourself, before any renderer sees the JSON:
27
+ //
28
+ // const migrated = migrateDesign(raw, { modernizeTextBackground: false });
29
+ // store.loadJSON(migrated); // canvas
30
+ // await jsonToPDF(migrated); // and every converter
31
+ //
32
+ // `migrateDesign` bumps `schemaVersion` to the current one and writes the
33
+ // decision into the design (`legacyBackground`), so the second pass through any
34
+ // entry point is a no-op and every renderer reads the same resolved intent.
21
35
  import { CURRENT_SCHEMA_VERSION } from './schema.js';
22
36
  import { TEXT_DEFAULTS } from './defaults.js';
23
37
  /** Named effects that never used the -100..100 intensity range. */
24
38
  const UNSCALED_FILTERS = ['warm', 'cold', 'natural'];
39
+ /** The tags Quill actually emitted. Anything else is not evidence. */
40
+ const QUILL_TAG = /<\/?(?:p|br|span|strong|em|ol|ul|li)\b[^>]*>/i;
41
+ /**
42
+ * Was this design written in the rich-text (Quill) editor, rather than the
43
+ * plain one?
44
+ *
45
+ * This is the whole v2→3 decision. The two pre-v3 text engines drew the
46
+ * background differently — plain Konva per line, rich text as one full rect —
47
+ * and a pre-v3 design does not record which one wrote it. But it does record
48
+ * the text, and only the rich editor ever stored markup: the plain editor kept
49
+ * a bare string with `\n` for newlines, and the rich pipeline never rewrites a
50
+ * bare string into `<p>` (see the `includes('<')` short-circuits in
51
+ * `@polotno/core/rich-text-html`). So markup is evidence of provenance — and
52
+ * evidence the CONVERTERS can read, unlike the editor flag this originally
53
+ * keyed on, which nothing below `polotno` can see.
54
+ *
55
+ * **Asked once per design, deliberately.** The renderer was a client-wide
56
+ * setting, never per element, so one marked-up element means the whole design
57
+ * was written in the rich editor. That matters because the rich editor did not
58
+ * always keep the markup: it simplified some `<p>hello</p>` back to `hello`, so
59
+ * a rich design routinely mixes marked-up and bare elements. Asking per element
60
+ * would split such a design down the middle and give two of its backgrounds two
61
+ * different shapes.
62
+ *
63
+ * Only known Quill tags count, so plain text holding a less-than ("a < b") or
64
+ * something merely tag-shaped is not mistaken for markup. A design that is
65
+ * entirely bare stays ambiguous and no heuristic can resolve it; it lands on
66
+ * per-line, and `modernizeTextBackground: false` overrides that.
67
+ */
68
+ function usesRichTextEditor(pages) {
69
+ let rich = false;
70
+ forEveryElement(pages, (element) => {
71
+ if (rich || element.type !== 'text')
72
+ return;
73
+ if (QUILL_TAG.test(String(element.text ?? '')))
74
+ rich = true;
75
+ });
76
+ return rich;
77
+ }
25
78
  /**
26
79
  * Local element walk. Schema sits below core, so it cannot use core's
27
80
  * `forEveryNode` — but it visits the same element nodes: nested `children`
@@ -64,7 +117,9 @@ function forEveryElement(pages, cb) {
64
117
  * unchanged apart from the clone.
65
118
  */
66
119
  export function migrateDesign(json, options = {}) {
67
- const { modernTextRenderer = true, modernizeTextBackground = false } = options;
120
+ // No default for modernizeTextBackground: `undefined` is a real state here
121
+ // (decide per element), distinct from either explicit boolean.
122
+ const { modernTextRenderer = true, modernizeTextBackground } = options;
68
123
  // Guard BEFORE the round-trip: JSON.parse(JSON.stringify(undefined)) throws a
69
124
  // raw SyntaxError, and a circular object throws TypeError. Non-designs must
70
125
  // reach the zod pass untouched so they surface as a structured
@@ -117,14 +172,22 @@ export function migrateDesign(json, options = {}) {
117
172
  }
118
173
  });
119
174
  }
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
- });
175
+ if (from < 3 && modernTextRenderer) {
176
+ // Freeze a pre-v3 RICH-TEXT design's backgrounds as full rects, so the old
177
+ // render is preserved exactly; leave a plain-text design to the per-line
178
+ // polygon it was authored with. `modernizeTextBackground` is the inverse of
179
+ // the stamp — "modernize" is exactly "do not freeze" — and when it is unset
180
+ // the design decides. See rule 2 in the header: intent, not geometry.
181
+ const stamp = modernizeTextBackground === undefined
182
+ ? usesRichTextEditor(data.pages)
183
+ : !modernizeTextBackground;
184
+ if (stamp) {
185
+ forEveryElement(data.pages, (element) => {
186
+ if (element.type === 'text' && element.backgroundEnabled) {
187
+ element.legacyBackground = true;
188
+ }
189
+ });
190
+ }
128
191
  }
129
192
  // v3→4 is a marker only: v4 is the video time-anchor render change. Nothing
130
193
  // to transform, but the bump lets a future migration target pre-v4 designs.
package/lib/schema.js CHANGED
@@ -15,11 +15,17 @@
15
15
  // `validateDesign({ mode: 'canonical' })`.
16
16
  //
17
17
  // Defaults match the core MST models. We do NOT reproduce loadJSON's imperative
18
- // quirk handling (locked shorthand, svgSource fallback, fontSize clamp, cell
19
- // stripping) — normalize is "fill defaults + validate", exact for clean input.
18
+ // quirk handling (locked shorthand, svgSource fallback, cell stripping) —
19
+ // normalize is "fill defaults + validate", exact for clean input.
20
+ //
21
+ // The fontSize floor is the ONE exception, and it is not a loadJSON quirk: the
22
+ // text and table models clamp it at every door, so it is an invariant of the
23
+ // format rather than a behavior of one entry point. Leaving it out did not keep
24
+ // the schema neutral, it just moved the decision into each renderer — see
25
+ // MIN_FONT_SIZE in ./defaults for what they each decided.
20
26
  import { z } from 'zod';
21
27
  import { polotnoError } from './errors.js';
22
- import { TEXT_DEFAULTS } from './defaults.js';
28
+ import { MIN_FONT_SIZE, clampFontSize, TEXT_DEFAULTS } from './defaults.js';
23
29
  export const CURRENT_SCHEMA_VERSION = 4;
24
30
  // Every element type the design format knows. Kept in sync with the
25
31
  // element discriminatedUnion in build() below.
@@ -198,10 +204,18 @@ function build(o) {
198
204
  .string()
199
205
  .default('')
200
206
  .describe('Placeholder text shown when empty.'),
207
+ // Every authored font size is clamped (see clampFontSize in ./defaults) — a
208
+ // clamp and not a `.min()` refusal, because the editor accepts these
209
+ // designs and rejecting them here would make the schema stricter than the
210
+ // canvas. `.overwrite()` and not `.transform()`: it keeps the node a
211
+ // ZodNumber, so `.default()`/`.optional()` still chain onto it, `z.infer`
212
+ // stays `number` (types-parity.ts fails on a ZodPipe here), and the JSON
213
+ // Schema generator still has a number to emit.
201
214
  fontSize: z
202
215
  .number()
216
+ .overwrite(clampFontSize)
203
217
  .default(TEXT_DEFAULTS.fontSize)
204
- .describe('Font size in pixels.'),
218
+ .describe(`Font size in pixels (minimum ${MIN_FONT_SIZE}).`),
205
219
  fontFamily: z
206
220
  .string()
207
221
  .default(TEXT_DEFAULTS.fontFamily)
@@ -413,8 +427,9 @@ function build(o) {
413
427
  .describe('Per-side border overrides for this cell.'),
414
428
  fontSize: z
415
429
  .number()
430
+ .overwrite(clampFontSize)
416
431
  .optional()
417
- .describe('Font size in pixels (inherits from the table when omitted).'),
432
+ .describe(`Font size in pixels (inherits from the table when omitted, minimum ${MIN_FONT_SIZE}).`),
418
433
  fontFamily: z
419
434
  .string()
420
435
  .optional()
@@ -508,8 +523,9 @@ function build(o) {
508
523
  .describe('Default border style.'),
509
524
  fontSize: z
510
525
  .number()
526
+ .overwrite(clampFontSize)
511
527
  .default(30)
512
- .describe('Default font size in pixels for cells.'),
528
+ .describe(`Default font size in pixels for cells (minimum ${MIN_FONT_SIZE}).`),
513
529
  fontFamily: z
514
530
  .string()
515
531
  .default('Roboto')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polotno/schema",
3
- "version": "0.4.0",
3
+ "version": "0.6.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",