@polotno/schema 0.12.0 → 0.14.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 +22 -0
- package/lib/asset-urls.js +1 -1
- package/lib/defaults.d.ts +18 -0
- package/lib/defaults.js +21 -0
- package/lib/design.schema.json +1 -1
- package/lib/errors.d.ts +12 -5
- package/lib/errors.js +1 -3
- package/lib/normalize.d.ts +5 -0
- package/lib/normalize.js +43 -5
- package/lib/schema.d.ts +318 -132
- package/lib/schema.js +116 -75
- package/lib/text.d.ts +17 -0
- package/lib/text.js +43 -0
- package/lib/types.d.ts +35 -3
- package/lib/types.js +1 -1
- package/lib/variable-text.d.ts +2 -0
- package/lib/variable-text.js +101 -0
- package/lib/variables.d.ts +88 -0
- package/lib/variables.js +220 -0
- package/package.json +11 -3
package/lib/normalize.d.ts
CHANGED
|
@@ -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
|
@@ -1,12 +1,46 @@
|
|
|
1
|
-
// normalizeDesign = migrate -> drop nulls -> zod parse (fills
|
|
2
|
-
// unknown keys). The migration step is what makes a design
|
|
3
|
-
// in every package; migrate.ts explains why it lives here
|
|
4
|
-
// `schemaVersion` counts as 0.
|
|
1
|
+
// normalizeDesign = migrate -> size floor -> drop nulls -> zod parse (fills
|
|
2
|
+
// defaults, strips unknown keys). The migration step is what makes a design
|
|
3
|
+
// mean the same thing in every package; migrate.ts explains why it lives here
|
|
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
7
|
import { migrateDesign } from './migrate.js';
|
|
8
|
+
import { clampElementSize } from './defaults.js';
|
|
9
|
+
import { fillVariables } from './variables.js';
|
|
8
10
|
export function normalizeDesign(json) {
|
|
9
|
-
|
|
11
|
+
const migrated = migrateDesign(json);
|
|
12
|
+
clampElementSizes(migrated);
|
|
13
|
+
return designSchemaLenient.parse(cloneDropNulls(migrated));
|
|
14
|
+
}
|
|
15
|
+
const isRecord = (value) => !!value && typeof value === 'object';
|
|
16
|
+
// Read every element width and height the way the editor's model loads it
|
|
17
|
+
// (MIN_ELEMENT_SIZE in ./defaults), so a converter never meets a size the
|
|
18
|
+
// Canvas does not draw. It runs before the zod pass, which would read a null
|
|
19
|
+
// size as missing and fill the type's default where the model reads 1; NaN and
|
|
20
|
+
// Infinity arrive here as null, from migrateDesign's JSON round-trip. Mutates
|
|
21
|
+
// in place — callers pass their private migrated copy.
|
|
22
|
+
function clampElementSizes(design) {
|
|
23
|
+
const walk = (children) => {
|
|
24
|
+
if (!Array.isArray(children))
|
|
25
|
+
return;
|
|
26
|
+
for (const element of children) {
|
|
27
|
+
if (!isRecord(element))
|
|
28
|
+
continue;
|
|
29
|
+
if ('width' in element)
|
|
30
|
+
element.width = clampElementSize(element.width);
|
|
31
|
+
// Text keeps its height: 0 there means "not measured yet".
|
|
32
|
+
if ('height' in element && element.type !== 'text') {
|
|
33
|
+
element.height = clampElementSize(element.height);
|
|
34
|
+
}
|
|
35
|
+
walk(element.children);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
if (!isRecord(design) || !Array.isArray(design.pages))
|
|
39
|
+
return;
|
|
40
|
+
for (const page of design.pages) {
|
|
41
|
+
if (isRecord(page))
|
|
42
|
+
walk(page.children);
|
|
43
|
+
}
|
|
10
44
|
}
|
|
11
45
|
// Fill missing `id`s with DETERMINISTIC structural ids — the index path from
|
|
12
46
|
// the document root (`page-0`, `el-0-2`, nested `el-0-2-1`, `cell-0-2-3`).
|
|
@@ -190,6 +224,7 @@ export function parseDesign(input, opts = {}) {
|
|
|
190
224
|
// exporter tolerance tests pin that (pdf dpi.test.js). So the caller's input
|
|
191
225
|
// is never touched and nothing needs cloning again below.
|
|
192
226
|
const migrated = migrateDesign(input);
|
|
227
|
+
clampElementSizes(migrated);
|
|
193
228
|
const pre = (opts.preserveClientData || opts.fillMissingIds) &&
|
|
194
229
|
migrated &&
|
|
195
230
|
typeof migrated === 'object';
|
|
@@ -210,6 +245,9 @@ export function parseDesign(input, opts = {}) {
|
|
|
210
245
|
spliceUnknownElements(design, extracted);
|
|
211
246
|
restoreClientData(original, design);
|
|
212
247
|
}
|
|
248
|
+
// After validation, so a malformed registry is DESIGN_INVALID.
|
|
249
|
+
if (opts.fillVariables)
|
|
250
|
+
fillVariables(design);
|
|
213
251
|
if (opts.requirePages && design.pages.length === 0) {
|
|
214
252
|
throw polotnoError('DESIGN_INVALID', 'Invalid Polotno design: no pages — did you pass `store.toJSON()`?', { errors: [{ path: 'pages', message: 'no pages' }] });
|
|
215
253
|
}
|