@polotno/schema 0.13.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 +18 -0
- package/lib/asset-urls.js +1 -1
- 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 +4 -0
- package/lib/schema.d.ts +318 -132
- package/lib/schema.js +85 -67
- package/lib/text.d.ts +17 -0
- package/lib/text.js +43 -0
- package/lib/types.d.ts +33 -1
- 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/schema.js
CHANGED
|
@@ -32,24 +32,12 @@
|
|
|
32
32
|
// times). It changes the JSON only: zod drops `id` from the output, and the two
|
|
33
33
|
// `build()` variants are never converted together. Keep the id on the type and
|
|
34
34
|
// the `.describe()` on each use, so every property still has a description.
|
|
35
|
+
import { VARIABLE_NAME } from './variable-text.js';
|
|
36
|
+
import { BINDABLE_PROPS } from './variables.js';
|
|
35
37
|
import { z } from 'zod';
|
|
36
38
|
import { polotnoError } from './errors.js';
|
|
37
39
|
import { ANIMATION_EASINGS, BLEND_MODES, GROUP_BLEND_MODES, MIN_FONT_SIZE, clampFontSize, DESIGN_SIZE, TEXT_DEFAULTS, } from './defaults.js';
|
|
38
40
|
export const CURRENT_SCHEMA_VERSION = 5;
|
|
39
|
-
// Every element type the design format knows. Kept in sync with the
|
|
40
|
-
// element discriminatedUnion in build() below.
|
|
41
|
-
export const ELEMENT_TYPES = [
|
|
42
|
-
'text',
|
|
43
|
-
'image',
|
|
44
|
-
'svg',
|
|
45
|
-
'line',
|
|
46
|
-
'figure',
|
|
47
|
-
'gif',
|
|
48
|
-
'lottie',
|
|
49
|
-
'video',
|
|
50
|
-
'table',
|
|
51
|
-
'group',
|
|
52
|
-
];
|
|
53
41
|
// `o` is the object constructor (`z.object` for lenient, `z.strictObject` for
|
|
54
42
|
// canonical), passed concretely so its precise generic return type is preserved
|
|
55
43
|
// — a `strict ? z.strictObject : z.object` ternary would widen to a union and
|
|
@@ -161,10 +149,24 @@ function build(o) {
|
|
|
161
149
|
.optional()
|
|
162
150
|
.describe('How the element composites with the content beneath it. Omitted means normal.');
|
|
163
151
|
// Fields shared by every Shape-derived element. Defaults inline.
|
|
152
|
+
const name = z.string().regex(VARIABLE_NAME);
|
|
153
|
+
// Built from the zod-free table the resolver and the editor read.
|
|
154
|
+
const bindingsOf = (props) => z
|
|
155
|
+
.strictObject(Object.fromEntries(props.map((p) => [p, name.optional()])))
|
|
156
|
+
.optional()
|
|
157
|
+
.describe('Variable names bound to element properties.');
|
|
158
|
+
const bindings = bindingsOf(['visible']);
|
|
159
|
+
const paintBindings = bindingsOf(BINDABLE_PROPS.text);
|
|
160
|
+
const mediaBindings = bindingsOf(BINDABLE_PROPS.image);
|
|
164
161
|
const shapeBase = {
|
|
162
|
+
bindings,
|
|
165
163
|
id: z.string().describe('Unique identifier for the element.'),
|
|
166
164
|
type: z.string().describe('Element type identifier.'),
|
|
167
165
|
name: z.string().default('').describe('Display name for the element.'),
|
|
166
|
+
href: z
|
|
167
|
+
.string()
|
|
168
|
+
.optional()
|
|
169
|
+
.describe("Click destination: an http(s), `mailto:` or `tel:` URL, or `#` and a page ID. A group's link applies to each child without one."),
|
|
168
170
|
custom: z
|
|
169
171
|
.unknown()
|
|
170
172
|
.optional()
|
|
@@ -258,6 +260,13 @@ function build(o) {
|
|
|
258
260
|
.describe('Whether the element appears in exports.'),
|
|
259
261
|
};
|
|
260
262
|
const imageProps = {
|
|
263
|
+
fit: z
|
|
264
|
+
.enum(['cover', 'contain'])
|
|
265
|
+
.optional()
|
|
266
|
+
.describe('`cover` fills the frame and crops the source; `contain` shows the whole source. Unset, the crop fields apply.'),
|
|
267
|
+
focus: o({ x: z.number().min(0).max(1), y: z.number().min(0).max(1) })
|
|
268
|
+
.optional()
|
|
269
|
+
.describe('Where the source sits when `fit` is set: 0 is the left or top edge, 1 the right or bottom; 0.5 by default.'),
|
|
261
270
|
src: z.string().default('').describe('URL or data URI of the image file.'),
|
|
262
271
|
cropX: z.number().default(0).describe('Crop X offset as a fraction (0-1).'),
|
|
263
272
|
cropY: z.number().default(0).describe('Crop Y offset as a fraction (0-1).'),
|
|
@@ -295,8 +304,12 @@ function build(o) {
|
|
|
295
304
|
};
|
|
296
305
|
const textShape = {
|
|
297
306
|
...shapeBase,
|
|
307
|
+
bindings: paintBindings,
|
|
298
308
|
type: z.literal('text').describe('Element type identifier.'),
|
|
299
|
-
text: z
|
|
309
|
+
text: z
|
|
310
|
+
.string()
|
|
311
|
+
.default('')
|
|
312
|
+
.describe('Text shown on canvas: plain, or rich HTML. `{{name}}` prints a variable; `<a href>` links the words it wraps.'),
|
|
300
313
|
placeholder: z
|
|
301
314
|
.string()
|
|
302
315
|
.default('')
|
|
@@ -435,10 +448,12 @@ function build(o) {
|
|
|
435
448
|
const imageShape = {
|
|
436
449
|
...shapeBase,
|
|
437
450
|
...imageProps,
|
|
451
|
+
bindings: mediaBindings,
|
|
438
452
|
type: z.literal('image').describe('Element type identifier.'),
|
|
439
453
|
};
|
|
440
454
|
const svgShape = {
|
|
441
455
|
...shapeBase,
|
|
456
|
+
bindings: mediaBindings,
|
|
442
457
|
type: z.literal('svg').describe('Element type identifier.'),
|
|
443
458
|
src: z.string().default('').describe('URL or data URI of the SVG.'),
|
|
444
459
|
maskSrc: z
|
|
@@ -491,6 +506,7 @@ function build(o) {
|
|
|
491
506
|
};
|
|
492
507
|
const figureShape = {
|
|
493
508
|
...shapeBase,
|
|
509
|
+
bindings: paintBindings,
|
|
494
510
|
type: z.literal('figure').describe('Element type identifier.'),
|
|
495
511
|
subType: z
|
|
496
512
|
.string()
|
|
@@ -508,6 +524,7 @@ function build(o) {
|
|
|
508
524
|
const gifShape = {
|
|
509
525
|
...shapeBase,
|
|
510
526
|
...imageProps,
|
|
527
|
+
bindings: mediaBindings,
|
|
511
528
|
type: z.literal('gif').describe('Element type identifier.'),
|
|
512
529
|
duration: z.number().default(0).describe('GIF duration in milliseconds.'),
|
|
513
530
|
keepRatio: z
|
|
@@ -524,6 +541,7 @@ function build(o) {
|
|
|
524
541
|
const videoShape = {
|
|
525
542
|
...shapeBase,
|
|
526
543
|
...imageProps,
|
|
544
|
+
bindings: mediaBindings,
|
|
527
545
|
type: z.literal('video').describe('Element type identifier.'),
|
|
528
546
|
duration: z.number().default(0).describe('Video duration in milliseconds.'),
|
|
529
547
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
@@ -552,7 +570,10 @@ function build(o) {
|
|
|
552
570
|
.literal('tablecell')
|
|
553
571
|
.default('tablecell')
|
|
554
572
|
.describe('Cell type identifier.'),
|
|
555
|
-
text: z
|
|
573
|
+
text: z
|
|
574
|
+
.string()
|
|
575
|
+
.default('')
|
|
576
|
+
.describe('Text shown in the cell: plain, or rich HTML. `{{name}}` prints a variable; `<a href>` links the words it wraps.'),
|
|
556
577
|
opacity: z.number().optional().describe('Opacity level (0-1).'),
|
|
557
578
|
rowSpan: z.number().optional().describe('Number of rows this cell spans.'),
|
|
558
579
|
colSpan: z
|
|
@@ -741,10 +762,12 @@ function build(o) {
|
|
|
741
762
|
.describe('Default cell padding in pixels.'),
|
|
742
763
|
};
|
|
743
764
|
const groupShape = (element) => ({
|
|
765
|
+
bindings,
|
|
744
766
|
id: shapeBase.id,
|
|
745
767
|
type: z.literal('group').describe('Element type identifier.'),
|
|
746
768
|
name: shapeBase.name,
|
|
747
769
|
custom: shapeBase.custom,
|
|
770
|
+
href: shapeBase.href,
|
|
748
771
|
opacity: shapeBase.opacity,
|
|
749
772
|
blendMode: z
|
|
750
773
|
.enum(GROUP_BLEND_MODES)
|
|
@@ -761,9 +784,8 @@ function build(o) {
|
|
|
761
784
|
.default([])
|
|
762
785
|
.describe('Child elements in this group.'),
|
|
763
786
|
});
|
|
764
|
-
const element = z.lazy(() =>
|
|
765
|
-
|
|
766
|
-
z.discriminatedUnion('type', [
|
|
787
|
+
const element = z.lazy(() => z.discriminatedUnion('type', elements));
|
|
788
|
+
const elements = [
|
|
767
789
|
o(textShape),
|
|
768
790
|
o(imageShape),
|
|
769
791
|
o(svgShape),
|
|
@@ -774,8 +796,8 @@ function build(o) {
|
|
|
774
796
|
o(videoShape),
|
|
775
797
|
o(tableShape),
|
|
776
798
|
o(groupShape(element)),
|
|
777
|
-
]
|
|
778
|
-
const
|
|
799
|
+
];
|
|
800
|
+
const font = o({
|
|
779
801
|
fontFamily: z.string().describe('Font family name.'),
|
|
780
802
|
url: z
|
|
781
803
|
.string()
|
|
@@ -789,8 +811,8 @@ function build(o) {
|
|
|
789
811
|
}))
|
|
790
812
|
.optional()
|
|
791
813
|
.describe('Per-weight/style variants for multi-variant families.'),
|
|
792
|
-
};
|
|
793
|
-
const
|
|
814
|
+
});
|
|
815
|
+
const audio = o({
|
|
794
816
|
id: z.string().describe('Unique identifier for the audio track.'),
|
|
795
817
|
name: z
|
|
796
818
|
.string()
|
|
@@ -810,7 +832,7 @@ function build(o) {
|
|
|
810
832
|
.default(1)
|
|
811
833
|
.describe('Playback speed multiplier (1 = normal).'),
|
|
812
834
|
custom: z.unknown().optional(),
|
|
813
|
-
};
|
|
835
|
+
});
|
|
814
836
|
const page = o({
|
|
815
837
|
id: z.string().describe('Unique identifier for the page.'),
|
|
816
838
|
width: z
|
|
@@ -853,6 +875,41 @@ function build(o) {
|
|
|
853
875
|
children: z.array(element).default([]).describe('Elements on this page.'),
|
|
854
876
|
});
|
|
855
877
|
const design = o({
|
|
878
|
+
variables: z
|
|
879
|
+
.array(o({
|
|
880
|
+
name: name.describe('Variable name: letters, digits and `_`, with `.` between path segments.'),
|
|
881
|
+
label: z.string().optional().describe('Name shown to people.'),
|
|
882
|
+
description: z
|
|
883
|
+
.string()
|
|
884
|
+
.optional()
|
|
885
|
+
.describe('Help text for whoever fills the variable.'),
|
|
886
|
+
required: z
|
|
887
|
+
.boolean()
|
|
888
|
+
.optional()
|
|
889
|
+
.describe('A record must supply the value; `default` does not.'),
|
|
890
|
+
default: z
|
|
891
|
+
.union([z.string(), z.boolean()])
|
|
892
|
+
.optional()
|
|
893
|
+
.describe('Value used when none is supplied.'),
|
|
894
|
+
value: z
|
|
895
|
+
.union([z.string(), z.boolean()])
|
|
896
|
+
.optional()
|
|
897
|
+
.describe('Value the design draws now.'),
|
|
898
|
+
}))
|
|
899
|
+
.superRefine((definitions, ctx) => {
|
|
900
|
+
const seen = new Set();
|
|
901
|
+
definitions.forEach(({ name }, i) => {
|
|
902
|
+
if (seen.has(name))
|
|
903
|
+
ctx.addIssue({
|
|
904
|
+
code: 'custom',
|
|
905
|
+
path: [i, 'name'],
|
|
906
|
+
message: 'Duplicate variable name',
|
|
907
|
+
});
|
|
908
|
+
seen.add(name);
|
|
909
|
+
});
|
|
910
|
+
})
|
|
911
|
+
.optional()
|
|
912
|
+
.describe('Template variables: the value of each and how to present it. Text tokens and bindings read them.'),
|
|
856
913
|
// Labels the OUTPUT only. migrateDesign runs before this parse and reads
|
|
857
914
|
// the RAW value, where a missing schemaVersion means 0 (the oldest format,
|
|
858
915
|
// see migrate.ts) — not the current one. The two are not in conflict: by
|
|
@@ -878,35 +935,14 @@ function build(o) {
|
|
|
878
935
|
.unknown()
|
|
879
936
|
.optional()
|
|
880
937
|
.describe('Custom data attached to the design.'),
|
|
881
|
-
fonts: z
|
|
882
|
-
.array(o(fontShape))
|
|
883
|
-
.default([])
|
|
884
|
-
.describe('Fonts used in the design.'),
|
|
938
|
+
fonts: z.array(font).default([]).describe('Fonts used in the design.'),
|
|
885
939
|
pages: z.array(page).default([]).describe('Pages in the design.'),
|
|
886
|
-
audios: z
|
|
887
|
-
.array(o(audioShape))
|
|
888
|
-
.default([])
|
|
889
|
-
.describe('Audio tracks in the design.'),
|
|
940
|
+
audios: z.array(audio).default([]).describe('Audio tracks in the design.'),
|
|
890
941
|
});
|
|
891
|
-
|
|
892
|
-
// guard are wrapped from these once at module level (below) so they aren't
|
|
893
|
-
// rebuilt on the canonical `build()` call.
|
|
894
|
-
const shapes = {
|
|
895
|
-
text: textShape,
|
|
896
|
-
image: imageShape,
|
|
897
|
-
svg: svgShape,
|
|
898
|
-
line: lineShape,
|
|
899
|
-
figure: figureShape,
|
|
900
|
-
gif: gifShape,
|
|
901
|
-
lottie: lottieShape,
|
|
902
|
-
video: videoShape,
|
|
903
|
-
table: tableShape,
|
|
904
|
-
font: fontShape,
|
|
905
|
-
audio: audioShape,
|
|
906
|
-
};
|
|
907
|
-
return { design, shapes };
|
|
942
|
+
return { design, elements, font, audio };
|
|
908
943
|
}
|
|
909
944
|
const lenient = build(z.object);
|
|
945
|
+
export const ELEMENT_TYPES = lenient.elements.map((schema) => schema.shape.type.value);
|
|
910
946
|
/** Non-strict: strips unknown keys, fills defaults. `normalizeDesign` uses this. */
|
|
911
947
|
export const designSchemaLenient = lenient.design;
|
|
912
948
|
/**
|
|
@@ -915,24 +951,6 @@ export const designSchemaLenient = lenient.design;
|
|
|
915
951
|
* as the single inferred source (this tree is only ever parsed, never inferred).
|
|
916
952
|
*/
|
|
917
953
|
export const designSchemaCanonical = build(z.strictObject).design;
|
|
918
|
-
/**
|
|
919
|
-
* Lenient per-type leaf schemas, used by the compile-time type-drift guard.
|
|
920
|
-
* `z.infer` is identical whether strict or not, so the non-strict wrapping here
|
|
921
|
-
* matches the hand-written `./types`. Recursive group/page/design are excluded.
|
|
922
|
-
*/
|
|
923
|
-
export const leafSchemas = {
|
|
924
|
-
text: z.object(lenient.shapes.text),
|
|
925
|
-
image: z.object(lenient.shapes.image),
|
|
926
|
-
svg: z.object(lenient.shapes.svg),
|
|
927
|
-
line: z.object(lenient.shapes.line),
|
|
928
|
-
figure: z.object(lenient.shapes.figure),
|
|
929
|
-
gif: z.object(lenient.shapes.gif),
|
|
930
|
-
lottie: z.object(lenient.shapes.lottie),
|
|
931
|
-
video: z.object(lenient.shapes.video),
|
|
932
|
-
table: z.object(lenient.shapes.table),
|
|
933
|
-
font: z.object(lenient.shapes.font),
|
|
934
|
-
audio: z.object(lenient.shapes.audio),
|
|
935
|
-
};
|
|
936
954
|
function toErrors(issues) {
|
|
937
955
|
return issues.map((issue) => ({
|
|
938
956
|
// dotted path into the design, e.g. "pages.0.children.0.opacity" ('' = root)
|
package/lib/text.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether text carries markup, as every renderer reads it: a line break or a
|
|
3
|
+
* closed tag. A lone `<` (`x<y and z>w`) is literal text, not an open tag.
|
|
4
|
+
*/
|
|
5
|
+
export declare const hasMarkup: (text: string) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Whether a text element's `text` is markup or a literal string. Entities count
|
|
8
|
+
* as HTML too: the DOM parse is what decodes &/</… — plain text
|
|
9
|
+
* containing entities must not skip it.
|
|
10
|
+
*
|
|
11
|
+
* html-export has to ask the SAME question: it is the one exporter that hands
|
|
12
|
+
* this string to a browser, so text this says is literal must be escaped on
|
|
13
|
+
* the way out, and text this says is markup must not be. A second spelling of
|
|
14
|
+
* the rule there double-escaped exactly the entity-only case.
|
|
15
|
+
*/
|
|
16
|
+
export declare const looksLikeHtml: (text: string) => boolean;
|
|
17
|
+
export declare function removeTags(str?: string): string;
|
package/lib/text.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Text predicates every renderer shares. zod-free, like `defaults.ts`.
|
|
2
|
+
/**
|
|
3
|
+
* Whether text carries markup, as every renderer reads it: a line break or a
|
|
4
|
+
* closed tag. A lone `<` (`x<y and z>w`) is literal text, not an open tag.
|
|
5
|
+
*/
|
|
6
|
+
export const hasMarkup = (text) => /<br\s*\/?>/i.test(text) ||
|
|
7
|
+
/<[a-z][a-z0-9]*(\s[^>]*)?>[^<]*<\/[a-z][a-z0-9]*>/i.test(text);
|
|
8
|
+
/**
|
|
9
|
+
* Whether a text element's `text` is markup or a literal string. Entities count
|
|
10
|
+
* as HTML too: the DOM parse is what decodes &/</… — plain text
|
|
11
|
+
* containing entities must not skip it.
|
|
12
|
+
*
|
|
13
|
+
* html-export has to ask the SAME question: it is the one exporter that hands
|
|
14
|
+
* this string to a browser, so text this says is literal must be escaped on
|
|
15
|
+
* the way out, and text this says is markup must not be. A second spelling of
|
|
16
|
+
* the rule there double-escaped exactly the entity-only case.
|
|
17
|
+
*/
|
|
18
|
+
export const looksLikeHtml = (text) => hasMarkup(text) || /&[a-z]+;|&#x?[0-9a-f]+;/i.test(text);
|
|
19
|
+
const NAMED_ENTITIES = {
|
|
20
|
+
amp: '&',
|
|
21
|
+
lt: '<',
|
|
22
|
+
gt: '>',
|
|
23
|
+
quot: '"',
|
|
24
|
+
apos: "'",
|
|
25
|
+
nbsp: ' ',
|
|
26
|
+
};
|
|
27
|
+
export function removeTags(str = '') {
|
|
28
|
+
// Regular expression to identify HTML tags in
|
|
29
|
+
// the input string. Replacing the identified
|
|
30
|
+
// HTML tag with a null string.
|
|
31
|
+
// We don't need the last line if we display HTML content in plain text render
|
|
32
|
+
const closingPTags = str.match(/<\/p>/gi) || [];
|
|
33
|
+
if (closingPTags.length > 1) {
|
|
34
|
+
str = str.replace(/<\/p>/gi, (match, offset, string) => {
|
|
35
|
+
const remainingTags = string.slice(offset + match.length).match(/<\/p>/gi) || [];
|
|
36
|
+
return remainingTags.length > 0 ? '\n' : match;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// One pass: `&lt;` is the text `<`, never `<`.
|
|
40
|
+
return str
|
|
41
|
+
.replace(/(<([^>]+)>)/gi, '')
|
|
42
|
+
.replace(/&(amp|lt|gt|quot|apos|nbsp);/g, (_, name) => NAMED_ENTITIES[name]);
|
|
43
|
+
}
|
package/lib/types.d.ts
CHANGED
|
@@ -2,7 +2,22 @@ import type { ANIMATION_EASINGS, BLEND_MODES, GROUP_BLEND_MODES } from './defaul
|
|
|
2
2
|
export type BlendMode = (typeof BLEND_MODES)[number];
|
|
3
3
|
export type GroupBlendMode = (typeof GROUP_BLEND_MODES)[number];
|
|
4
4
|
export type AnimationEasing = (typeof ANIMATION_EASINGS)[number];
|
|
5
|
+
export interface VariableDefinition {
|
|
6
|
+
name: string;
|
|
7
|
+
label?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
default?: string | boolean;
|
|
11
|
+
/** The current value the editor draws and an export fills. */
|
|
12
|
+
value?: string | boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface Bindings {
|
|
15
|
+
visible?: string;
|
|
16
|
+
src?: string;
|
|
17
|
+
fill?: string;
|
|
18
|
+
}
|
|
5
19
|
export interface Design {
|
|
20
|
+
variables?: VariableDefinition[];
|
|
6
21
|
/** Schema version for compatibility / migration. */
|
|
7
22
|
schemaVersion: number;
|
|
8
23
|
/** Canvas width in pixels. */
|
|
@@ -116,6 +131,8 @@ export interface Animation {
|
|
|
116
131
|
}
|
|
117
132
|
/** Properties shared by all non-group elements. */
|
|
118
133
|
export interface ShapeAttributes {
|
|
134
|
+
bindings?: Pick<Bindings, 'visible'>;
|
|
135
|
+
href?: string;
|
|
119
136
|
/** Unique identifier. */
|
|
120
137
|
id: string;
|
|
121
138
|
/** Element type identifier. */
|
|
@@ -167,6 +184,8 @@ export interface ShapeAttributes {
|
|
|
167
184
|
showInExport: boolean;
|
|
168
185
|
}
|
|
169
186
|
export interface GroupElement {
|
|
187
|
+
bindings?: Pick<Bindings, 'visible'>;
|
|
188
|
+
href?: string;
|
|
170
189
|
id: string;
|
|
171
190
|
type: 'group';
|
|
172
191
|
/** Display name (defaults to ''). */
|
|
@@ -185,8 +204,9 @@ export interface GroupElement {
|
|
|
185
204
|
children: Element[];
|
|
186
205
|
}
|
|
187
206
|
export interface TextElement extends ShapeAttributes {
|
|
207
|
+
bindings?: Pick<Bindings, 'visible' | 'fill'>;
|
|
188
208
|
type: 'text';
|
|
189
|
-
/**
|
|
209
|
+
/** Plain or rich HTML. `{{name}}` prints a variable; `<a href>` links words. */
|
|
190
210
|
text: string;
|
|
191
211
|
/** Placeholder text shown when empty. */
|
|
192
212
|
placeholder: string;
|
|
@@ -236,6 +256,13 @@ export interface TextElement extends ShapeAttributes {
|
|
|
236
256
|
}
|
|
237
257
|
/** Image-like crop/border properties shared by image, gif, and video. */
|
|
238
258
|
export interface BaseImageProps {
|
|
259
|
+
/** Fit the source in the frame instead of the explicit crop. */
|
|
260
|
+
fit?: 'cover' | 'contain';
|
|
261
|
+
/** Where the source sits in the frame, 0-1 on each axis. Default center. */
|
|
262
|
+
focus?: {
|
|
263
|
+
x: number;
|
|
264
|
+
y: number;
|
|
265
|
+
};
|
|
239
266
|
/** URL or data URI of the source. */
|
|
240
267
|
src: string;
|
|
241
268
|
/** Crop X offset (0-1). */
|
|
@@ -257,9 +284,11 @@ export interface BaseImageProps {
|
|
|
257
284
|
stretchEnabled: boolean;
|
|
258
285
|
}
|
|
259
286
|
export interface ImageElement extends ShapeAttributes, BaseImageProps {
|
|
287
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
260
288
|
type: 'image';
|
|
261
289
|
}
|
|
262
290
|
export interface SVGElement extends ShapeAttributes {
|
|
291
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
263
292
|
type: 'svg';
|
|
264
293
|
src: string;
|
|
265
294
|
maskSrc: string;
|
|
@@ -288,6 +317,7 @@ export interface LineElement extends ShapeAttributes {
|
|
|
288
317
|
endHead: string;
|
|
289
318
|
}
|
|
290
319
|
export interface FigureElement extends ShapeAttributes {
|
|
320
|
+
bindings?: Pick<Bindings, 'visible' | 'fill'>;
|
|
291
321
|
type: 'figure';
|
|
292
322
|
/** Figure subtype: rect, ellipse, triangle, etc. */
|
|
293
323
|
subType: string;
|
|
@@ -298,6 +328,7 @@ export interface FigureElement extends ShapeAttributes {
|
|
|
298
328
|
cornerRadius: number;
|
|
299
329
|
}
|
|
300
330
|
export interface GifElement extends ShapeAttributes, BaseImageProps {
|
|
331
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
301
332
|
type: 'gif';
|
|
302
333
|
/** Duration in milliseconds. */
|
|
303
334
|
duration: number;
|
|
@@ -306,6 +337,7 @@ export interface LottieElement extends ShapeAttributes, BaseImageProps {
|
|
|
306
337
|
type: 'lottie';
|
|
307
338
|
}
|
|
308
339
|
export interface VideoElement extends ShapeAttributes, BaseImageProps {
|
|
340
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
309
341
|
type: 'video';
|
|
310
342
|
/** Duration in milliseconds. */
|
|
311
343
|
duration: number;
|
package/lib/types.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
//
|
|
11
11
|
// DRIFT IS GUARDED, NOT TRUSTED: `./types-parity.ts` asserts (at compile time,
|
|
12
12
|
// via `tsc`) that every leaf element type plus `Font`/`Audio` equals `z.infer`
|
|
13
|
-
// of its zod
|
|
13
|
+
// of its zod schema. Shared field groups
|
|
14
14
|
// (`ShapeAttributes`, `BaseImageProps`, `Animation`, `BorderSide`, `TableCell`,
|
|
15
15
|
// …) are guarded transitively through those leaves. Only the three types that
|
|
16
16
|
// reference `Element` recursively — `GroupElement`, `Page`, `Design` — are
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { looksLikeHtml } from './text.js';
|
|
2
|
+
const NAME = '[A-Za-z0-9_]+(?:\\.[A-Za-z0-9_]+)*';
|
|
3
|
+
export const VARIABLE_NAME = new RegExp('^' + NAME + '$');
|
|
4
|
+
const TOKEN_SOURCE = '\\{\\{[ \\t]*(' + NAME + ')[ \\t]*\\}\\}';
|
|
5
|
+
const TOKEN = new RegExp(TOKEN_SOURCE, 'g');
|
|
6
|
+
const WHOLE_TOKEN = new RegExp('^' + TOKEN_SOURCE + '$');
|
|
7
|
+
const escapeText = (text) => text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
8
|
+
// Keep the original tags and attributes byte-for-byte. Match within a block's
|
|
9
|
+
// visible runs, and write each replacement in the run containing its first brace.
|
|
10
|
+
const TAG = /<!--[\s\S]*?-->|<\/?[a-z][\w:-]*(?:\s+(?:[^>"']|"[^"]*"|'[^']*')*)?\s*\/?>/gi;
|
|
11
|
+
const BLOCK = /^<\/?(?:p|div|br|li|ul|ol|h[1-6])\b/i;
|
|
12
|
+
// A link target is the one attribute a token resolves in. A single token that
|
|
13
|
+
// is the whole target is a URL and goes in as given; any other token is one URL
|
|
14
|
+
// component and is percent-encoded. The renderer's sanitizer still decides
|
|
15
|
+
// which schemes a link may use. Attributes are read in order, each quoted value
|
|
16
|
+
// whole, so an `href=` inside another attribute's value stays text.
|
|
17
|
+
const ATTRIBUTE = /([^\s"'<>/=]+)(?:(\s*=\s*)("[^"]*"|'[^']*'|[^\s"'<>=`]+))?/g;
|
|
18
|
+
const escapeAttribute = (text) => escapeText(text).replace(/"/g, '"').replace(/'/g, ''');
|
|
19
|
+
function substituteHref(tag, read) {
|
|
20
|
+
if (!tag.includes('{{'))
|
|
21
|
+
return tag;
|
|
22
|
+
return tag.replace(ATTRIBUTE, (attribute, name, equals, value) => {
|
|
23
|
+
if (name.toLowerCase() !== 'href' || !/^["']/.test(value ?? ''))
|
|
24
|
+
return attribute;
|
|
25
|
+
const quote = value[0];
|
|
26
|
+
const target = value.slice(1, -1);
|
|
27
|
+
const whole = WHOLE_TOKEN.test(target);
|
|
28
|
+
const filled = target.replace(TOKEN, (token, key) => {
|
|
29
|
+
const value = read(key);
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return token;
|
|
32
|
+
return whole
|
|
33
|
+
? escapeAttribute(value)
|
|
34
|
+
: encodeURIComponent(value).replace(/'/g, '%27');
|
|
35
|
+
});
|
|
36
|
+
return name + equals + quote + filled + quote;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
export function substituteText(text, read) {
|
|
40
|
+
if (!text.includes('{'))
|
|
41
|
+
return text;
|
|
42
|
+
if (!looksLikeHtml(text)) {
|
|
43
|
+
const result = text.replace(TOKEN, (token, name) => read(name) ?? token);
|
|
44
|
+
return looksLikeHtml(result) ? escapeText(result) : result;
|
|
45
|
+
}
|
|
46
|
+
const output = [];
|
|
47
|
+
let runs = [];
|
|
48
|
+
let body = '';
|
|
49
|
+
function appendRun(value) {
|
|
50
|
+
if (!value)
|
|
51
|
+
return;
|
|
52
|
+
runs.push({
|
|
53
|
+
start: body.length,
|
|
54
|
+
end: body.length + value.length,
|
|
55
|
+
index: output.length,
|
|
56
|
+
});
|
|
57
|
+
body += value;
|
|
58
|
+
output.push(value);
|
|
59
|
+
}
|
|
60
|
+
function flush() {
|
|
61
|
+
// Edit right-to-left so offsets into the untouched prefix stay valid.
|
|
62
|
+
let i = runs.length - 1;
|
|
63
|
+
const edits = Array.from(body.matchAll(TOKEN), (match) => ({
|
|
64
|
+
start: match.index,
|
|
65
|
+
end: match.index + match[0].length,
|
|
66
|
+
value: read(match[1]),
|
|
67
|
+
}));
|
|
68
|
+
for (const { start, end, value } of edits.reverse()) {
|
|
69
|
+
if (value === undefined)
|
|
70
|
+
continue;
|
|
71
|
+
while (runs[i].start >= end)
|
|
72
|
+
i--;
|
|
73
|
+
for (; i >= 0; i--) {
|
|
74
|
+
const run = runs[i];
|
|
75
|
+
const original = output[run.index];
|
|
76
|
+
const from = Math.max(0, start - run.start);
|
|
77
|
+
const to = Math.min(run.end, end) - run.start;
|
|
78
|
+
const first = start >= run.start;
|
|
79
|
+
output[run.index] =
|
|
80
|
+
original.slice(0, from) +
|
|
81
|
+
(first ? escapeText(value).replace(/\r\n?|\n/g, '<br/>') : '') +
|
|
82
|
+
original.slice(to);
|
|
83
|
+
if (first)
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
body = '';
|
|
88
|
+
runs = [];
|
|
89
|
+
}
|
|
90
|
+
let cursor = 0;
|
|
91
|
+
for (const match of text.matchAll(TAG)) {
|
|
92
|
+
appendRun(text.slice(cursor, match.index));
|
|
93
|
+
if (BLOCK.test(match[0]))
|
|
94
|
+
flush();
|
|
95
|
+
output.push(substituteHref(match[0], read));
|
|
96
|
+
cursor = match.index + match[0].length;
|
|
97
|
+
}
|
|
98
|
+
appendRun(text.slice(cursor));
|
|
99
|
+
flush();
|
|
100
|
+
return output.join('');
|
|
101
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Bindings, VariableDefinition } from './types.js';
|
|
2
|
+
export { VARIABLE_NAME } from './variable-text.js';
|
|
3
|
+
/** The properties each element type can bind. Any other element binds `visible`. */
|
|
4
|
+
export declare const BINDABLE_PROPS: {
|
|
5
|
+
readonly image: readonly ["src", "visible"];
|
|
6
|
+
readonly svg: readonly ["src", "visible"];
|
|
7
|
+
readonly gif: readonly ["src", "visible"];
|
|
8
|
+
readonly video: readonly ["src", "visible"];
|
|
9
|
+
readonly text: readonly ["fill", "visible"];
|
|
10
|
+
readonly figure: readonly ["fill", "visible"];
|
|
11
|
+
};
|
|
12
|
+
export declare const bindableProps: (type: string) => readonly (keyof Bindings)[];
|
|
13
|
+
/** The type a binding gives its variable. */
|
|
14
|
+
export declare const bindingType: (elementType: string | undefined, prop: keyof Bindings) => Variable['type'];
|
|
15
|
+
export interface Variable extends VariableDefinition {
|
|
16
|
+
type: 'text' | 'image' | 'video' | 'color' | 'boolean';
|
|
17
|
+
declared: boolean;
|
|
18
|
+
usages: {
|
|
19
|
+
elementId: string;
|
|
20
|
+
property: 'text' | keyof Bindings;
|
|
21
|
+
}[];
|
|
22
|
+
}
|
|
23
|
+
export interface Issue {
|
|
24
|
+
severity: 'warning' | 'error';
|
|
25
|
+
reason: 'orphan' | 'mixed-types' | 'missing-required' | 'type-mismatch' | 'unknown-key';
|
|
26
|
+
variable?: string;
|
|
27
|
+
key?: string;
|
|
28
|
+
}
|
|
29
|
+
interface TemplateNode {
|
|
30
|
+
id?: string;
|
|
31
|
+
type?: string;
|
|
32
|
+
text?: string;
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
height?: number;
|
|
35
|
+
textOverflow?: string;
|
|
36
|
+
bindings?: Bindings;
|
|
37
|
+
children?: readonly TemplateNode[];
|
|
38
|
+
cells?: readonly TemplateNode[];
|
|
39
|
+
src?: string;
|
|
40
|
+
fill?: string;
|
|
41
|
+
visible?: boolean;
|
|
42
|
+
}
|
|
43
|
+
interface Template {
|
|
44
|
+
variables?: readonly VariableDefinition[];
|
|
45
|
+
pages: readonly {
|
|
46
|
+
children?: readonly TemplateNode[];
|
|
47
|
+
}[];
|
|
48
|
+
}
|
|
49
|
+
export declare function getVariables(design: Template): {
|
|
50
|
+
variables: Variable[];
|
|
51
|
+
issues: Issue[];
|
|
52
|
+
};
|
|
53
|
+
/** The record value a dotted variable name reads, or undefined. */
|
|
54
|
+
export declare function readPath(record: Record<string, unknown>, name: string): unknown;
|
|
55
|
+
/** The registry by name, for `fillElement`: build it once per registry. */
|
|
56
|
+
export declare const variablesByName: (variables: readonly VariableDefinition[] | undefined) => ReadonlyMap<string, VariableDefinition>;
|
|
57
|
+
/**
|
|
58
|
+
* Fill `design` in place, from `record` or else from its stored values, and
|
|
59
|
+
* strip the template. Returns the issues about the record.
|
|
60
|
+
*/
|
|
61
|
+
export declare function fillVariables(design: Template, record?: Record<string, unknown>): Issue[];
|
|
62
|
+
type Unbound<N> = N extends object ? {
|
|
63
|
+
[K in keyof N as K extends 'bindings' ? never : K]: K extends 'children' | 'cells' ? UnboundList<N[K]> : N[K];
|
|
64
|
+
} : N;
|
|
65
|
+
type UnboundList<L> = L extends undefined ? L : L extends readonly (infer Item)[] ? Unbound<Item>[] : L;
|
|
66
|
+
/** `T` as `resolveVariables` returns it: no registry, no bindings. */
|
|
67
|
+
export type Resolved<T> = {
|
|
68
|
+
[K in keyof T as K extends 'variables' ? never : K]: K extends 'pages' ? T[K] extends readonly (infer Page)[] ? {
|
|
69
|
+
[P in keyof Page]: P extends 'children' ? UnboundList<Page[P]> : Page[P];
|
|
70
|
+
}[] : T[K] : T[K];
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* A plain copy of `template` filled from `record`, or from the stored values
|
|
74
|
+
* when no record is passed.
|
|
75
|
+
*/
|
|
76
|
+
export declare function resolveVariables<T extends Template>(template: T, record?: Record<string, unknown>, options?: {
|
|
77
|
+
strict?: boolean;
|
|
78
|
+
}): {
|
|
79
|
+
design: Resolved<T>;
|
|
80
|
+
issues: Issue[];
|
|
81
|
+
};
|
|
82
|
+
type Drawn = Pick<TemplateNode, 'text' | 'placeholder' | 'src' | 'fill' | 'visible'>;
|
|
83
|
+
/**
|
|
84
|
+
* The properties an element draws with the stored values in `variables`, for
|
|
85
|
+
* an editor that shows filled values while keeping the template. Only the
|
|
86
|
+
* properties that change are returned.
|
|
87
|
+
*/
|
|
88
|
+
export declare function fillElement(node: TemplateNode, variables: ReadonlyMap<string, VariableDefinition>): Partial<Drawn>;
|