@polotno/schema 0.13.0 → 0.15.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/lib/migrate.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Keys the migration reads and deletes. They are the design's own retired
3
+ * fields, never client data: `parseDesign` must not restore them, or the next
4
+ * parse would replay the old value over later edits.
5
+ */
6
+ export declare const MIGRATED_AWAY_KEYS: ReadonlySet<string>;
1
7
  export interface MigrateOptions {
2
8
  /**
3
9
  * Whether the consumer renders text with the modern engines (HTML /
@@ -22,6 +28,6 @@ export interface MigrateOptions {
22
28
  /**
23
29
  * Bring a design up to `CURRENT_SCHEMA_VERSION`. Returns a new object; the
24
30
  * input is never mutated. A design already at the current version is returned
25
- * unchanged apart from the clone.
31
+ * unchanged apart from the clone and the deprecated inputs (see the header).
26
32
  */
27
33
  export declare function migrateDesign<T>(json: T, options?: MigrateOptions): T;
package/lib/migrate.js CHANGED
@@ -11,9 +11,9 @@
11
11
  // the bottom layer and dependency-free, and migrations run before any model
12
12
  // or renderer exists. A change that can only be decided by measuring text
13
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.
14
+ // at paint time. The v2→3 step is exactly that pattern: it records
15
+ // `backgroundShape: 'box'` ("paint the frame") rather than baking in a
16
+ // rect's size, and every renderer draws the box from the element.
17
17
  // 2. **A missing `schemaVersion` means 0**, the oldest format — not the current
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
@@ -30,10 +30,24 @@
30
30
  // await jsonToPDF(migrated); // and every converter
31
31
  //
32
32
  // `migrateDesign` bumps `schemaVersion` to the current one and writes the
33
- // decision into the design (`legacyBackground`), so the second pass through any
33
+ // decision into the design (`backgroundShape`), so the second pass through any
34
34
  // entry point is a no-op and every renderer reads the same resolved intent.
35
+ //
36
+ // **Deprecated inputs are expanded at every version.** `cellPadding` (table and
37
+ // cell) became four sides in v6, but client code may still write it into a v6
38
+ // design until polotno 5.0 removes the fallback. Nothing in the SDK writes it,
39
+ // so when it is present it is the client's intent: it replaces the four sides.
35
40
  import { CURRENT_SCHEMA_VERSION } from './schema.js';
36
41
  import { DESIGN_SIZE, TEXT_DEFAULTS } from './defaults.js';
42
+ /**
43
+ * Keys the migration reads and deletes. They are the design's own retired
44
+ * fields, never client data: `parseDesign` must not restore them, or the next
45
+ * parse would replay the old value over later edits.
46
+ */
47
+ export const MIGRATED_AWAY_KEYS = new Set([
48
+ 'cellPadding',
49
+ 'legacyBackground',
50
+ ]);
37
51
  /** Named effects that never used the -100..100 intensity range. */
38
52
  const UNSCALED_FILTERS = ['warm', 'cold', 'natural'];
39
53
  /** The tags Quill actually emitted. Anything else is not evidence. */
@@ -111,10 +125,40 @@ function forEveryElement(pages, cb) {
111
125
  walk(page);
112
126
  }
113
127
  }
128
+ const CELL_PADDING_SIDES = [
129
+ 'cellPaddingTop',
130
+ 'cellPaddingRight',
131
+ 'cellPaddingBottom',
132
+ 'cellPaddingLeft',
133
+ ];
134
+ /**
135
+ * The deprecated `cellPadding` of a table and of its cells, as the four sides
136
+ * it stands for (see the header). A value that is not a number (NaN arrives
137
+ * here as null) is dropped without touching the sides.
138
+ */
139
+ function expandCellPadding(pages) {
140
+ const expand = (node) => {
141
+ if (!node || typeof node !== 'object' || !('cellPadding' in node))
142
+ return;
143
+ const value = node.cellPadding;
144
+ delete node.cellPadding;
145
+ if (typeof value !== 'number')
146
+ return;
147
+ for (const side of CELL_PADDING_SIDES)
148
+ node[side] = value;
149
+ };
150
+ forEveryElement(pages, (element) => {
151
+ if (element.type !== 'table')
152
+ return;
153
+ expand(element);
154
+ if (Array.isArray(element.cells))
155
+ element.cells.forEach(expand);
156
+ });
157
+ }
114
158
  /**
115
159
  * Bring a design up to `CURRENT_SCHEMA_VERSION`. Returns a new object; the
116
160
  * input is never mutated. A design already at the current version is returned
117
- * unchanged apart from the clone.
161
+ * unchanged apart from the clone and the deprecated inputs (see the header).
118
162
  */
119
163
  export function migrateDesign(json, options = {}) {
120
164
  // No default for modernizeTextBackground: `undefined` is a real state here
@@ -183,8 +227,11 @@ export function migrateDesign(json, options = {}) {
183
227
  : !modernizeTextBackground;
184
228
  if (stamp) {
185
229
  forEveryElement(data.pages, (element) => {
186
- if (element.type === 'text' && element.backgroundEnabled) {
187
- element.legacyBackground = true;
230
+ // An explicit shape is the author's: unversioned JSON may be new.
231
+ if (element.type === 'text' &&
232
+ element.backgroundEnabled &&
233
+ element.backgroundShape === undefined) {
234
+ element.backgroundShape = 'box';
188
235
  }
189
236
  });
190
237
  }
@@ -221,6 +268,20 @@ export function migrateDesign(json, options = {}) {
221
268
  });
222
269
  }
223
270
  }
271
+ if (from < 6) {
272
+ // v3–v5 stamped the full-rect background as `legacyBackground: true`. v6
273
+ // draws that rect as `backgroundShape: 'box'` (the frame grown by the
274
+ // spread), so the flag becomes the shape and goes. Exact by construction:
275
+ // a migrated element has no padding, and box is the legacy rect's branch.
276
+ forEveryElement(data.pages, (element) => {
277
+ if (element.type !== 'text' || !('legacyBackground' in element))
278
+ return;
279
+ if (element.legacyBackground === true)
280
+ element.backgroundShape = 'box';
281
+ delete element.legacyBackground;
282
+ });
283
+ }
284
+ expandCellPadding(data.pages);
224
285
  data.schemaVersion = CURRENT_SCHEMA_VERSION;
225
286
  return data;
226
287
  }
@@ -34,6 +34,11 @@ export interface ParseOptions extends ValidateOptions {
34
34
  * and the schema keeps it everywhere, so it survives parsing as-is.
35
35
  */
36
36
  preserveClientData?: boolean;
37
+ /**
38
+ * Fill text tokens and bindings from the design's stored variable values,
39
+ * as the editor draws them, and drop the template. Every exporter sets it.
40
+ */
41
+ fillVariables?: boolean;
37
42
  }
38
43
  /**
39
44
  * The one entry a consumer should use to accept design JSON: validate, then
package/lib/normalize.js CHANGED
@@ -4,8 +4,9 @@
4
4
  // and why a missing `schemaVersion` counts as 0.
5
5
  import { designSchemaLenient, parseValidDesign, cloneDropNulls, ELEMENT_TYPES, } from './schema.js';
6
6
  import { polotnoError } from './errors.js';
7
- import { migrateDesign } from './migrate.js';
7
+ import { MIGRATED_AWAY_KEYS, migrateDesign } from './migrate.js';
8
8
  import { clampElementSize } from './defaults.js';
9
+ import { fillVariables } from './variables.js';
9
10
  export function normalizeDesign(json) {
10
11
  const migrated = migrateDesign(json);
11
12
  clampElementSizes(migrated);
@@ -151,7 +152,7 @@ function spliceUnknownElements(design, extracted) {
151
152
  function restoreClientData(original, design) {
152
153
  const restoreKeys = (orig, parsed) => {
153
154
  for (const key of Object.keys(orig)) {
154
- if (orig[key] != null && !(key in parsed))
155
+ if (orig[key] != null && !(key in parsed) && !MIGRATED_AWAY_KEYS.has(key))
155
156
  parsed[key] = orig[key];
156
157
  }
157
158
  };
@@ -244,6 +245,9 @@ export function parseDesign(input, opts = {}) {
244
245
  spliceUnknownElements(design, extracted);
245
246
  restoreClientData(original, design);
246
247
  }
248
+ // After validation, so a malformed registry is DESIGN_INVALID.
249
+ if (opts.fillVariables)
250
+ fillVariables(design);
247
251
  if (opts.requirePages && design.pages.length === 0) {
248
252
  throw polotnoError('DESIGN_INVALID', 'Invalid Polotno design: no pages — did you pass `store.toJSON()`?', { errors: [{ path: 'pages', message: 'no pages' }] });
249
253
  }