@polotno/schema 0.11.0 → 0.13.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 +4 -0
- package/lib/defaults.d.ts +32 -16
- package/lib/defaults.js +26 -7
- package/lib/design.schema.json +1 -1
- package/lib/errors.d.ts +2 -2
- package/lib/errors.js +2 -2
- package/lib/migrate.js +1 -1
- package/lib/normalize.js +39 -5
- package/lib/schema.d.ts +246 -166
- package/lib/schema.js +47 -12
- package/lib/types.d.ts +6 -3
- package/package.json +4 -5
package/lib/normalize.js
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
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';
|
|
8
9
|
export function normalizeDesign(json) {
|
|
9
|
-
|
|
10
|
+
const migrated = migrateDesign(json);
|
|
11
|
+
clampElementSizes(migrated);
|
|
12
|
+
return designSchemaLenient.parse(cloneDropNulls(migrated));
|
|
13
|
+
}
|
|
14
|
+
const isRecord = (value) => !!value && typeof value === 'object';
|
|
15
|
+
// Read every element width and height the way the editor's model loads it
|
|
16
|
+
// (MIN_ELEMENT_SIZE in ./defaults), so a converter never meets a size the
|
|
17
|
+
// Canvas does not draw. It runs before the zod pass, which would read a null
|
|
18
|
+
// size as missing and fill the type's default where the model reads 1; NaN and
|
|
19
|
+
// Infinity arrive here as null, from migrateDesign's JSON round-trip. Mutates
|
|
20
|
+
// in place — callers pass their private migrated copy.
|
|
21
|
+
function clampElementSizes(design) {
|
|
22
|
+
const walk = (children) => {
|
|
23
|
+
if (!Array.isArray(children))
|
|
24
|
+
return;
|
|
25
|
+
for (const element of children) {
|
|
26
|
+
if (!isRecord(element))
|
|
27
|
+
continue;
|
|
28
|
+
if ('width' in element)
|
|
29
|
+
element.width = clampElementSize(element.width);
|
|
30
|
+
// Text keeps its height: 0 there means "not measured yet".
|
|
31
|
+
if ('height' in element && element.type !== 'text') {
|
|
32
|
+
element.height = clampElementSize(element.height);
|
|
33
|
+
}
|
|
34
|
+
walk(element.children);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
if (!isRecord(design) || !Array.isArray(design.pages))
|
|
38
|
+
return;
|
|
39
|
+
for (const page of design.pages) {
|
|
40
|
+
if (isRecord(page))
|
|
41
|
+
walk(page.children);
|
|
42
|
+
}
|
|
10
43
|
}
|
|
11
44
|
// Fill missing `id`s with DETERMINISTIC structural ids — the index path from
|
|
12
45
|
// the document root (`page-0`, `el-0-2`, nested `el-0-2-1`, `cell-0-2-3`).
|
|
@@ -190,6 +223,7 @@ export function parseDesign(input, opts = {}) {
|
|
|
190
223
|
// exporter tolerance tests pin that (pdf dpi.test.js). So the caller's input
|
|
191
224
|
// is never touched and nothing needs cloning again below.
|
|
192
225
|
const migrated = migrateDesign(input);
|
|
226
|
+
clampElementSizes(migrated);
|
|
193
227
|
const pre = (opts.preserveClientData || opts.fillMissingIds) &&
|
|
194
228
|
migrated &&
|
|
195
229
|
typeof migrated === 'object';
|