@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/schema.js
CHANGED
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
// text and table models clamp it at every door, so it is an invariant of the
|
|
23
23
|
// format rather than a behavior of one entry point. Leaving it out did not keep
|
|
24
24
|
// the schema neutral, it just moved the decision into each renderer — see
|
|
25
|
-
// MIN_FONT_SIZE in ./defaults for what they each decided.
|
|
25
|
+
// MIN_FONT_SIZE in ./defaults for what they each decided. The element size
|
|
26
|
+
// floor (MIN_ELEMENT_SIZE) is the same kind of rule, but normalize applies it
|
|
27
|
+
// before this schema runs, because it also reads a null size as 1.
|
|
26
28
|
//
|
|
27
29
|
// A type used in more than one place takes `.meta({ id })`, so the generated
|
|
28
30
|
// JSON Schema defines it once under `$defs` and `$ref`s it, rather than copying
|
|
@@ -30,24 +32,12 @@
|
|
|
30
32
|
// times). It changes the JSON only: zod drops `id` from the output, and the two
|
|
31
33
|
// `build()` variants are never converted together. Keep the id on the type and
|
|
32
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';
|
|
33
37
|
import { z } from 'zod';
|
|
34
38
|
import { polotnoError } from './errors.js';
|
|
35
39
|
import { ANIMATION_EASINGS, BLEND_MODES, GROUP_BLEND_MODES, MIN_FONT_SIZE, clampFontSize, DESIGN_SIZE, TEXT_DEFAULTS, } from './defaults.js';
|
|
36
40
|
export const CURRENT_SCHEMA_VERSION = 5;
|
|
37
|
-
// Every element type the design format knows. Kept in sync with the
|
|
38
|
-
// element discriminatedUnion in build() below.
|
|
39
|
-
export const ELEMENT_TYPES = [
|
|
40
|
-
'text',
|
|
41
|
-
'image',
|
|
42
|
-
'svg',
|
|
43
|
-
'line',
|
|
44
|
-
'figure',
|
|
45
|
-
'gif',
|
|
46
|
-
'lottie',
|
|
47
|
-
'video',
|
|
48
|
-
'table',
|
|
49
|
-
'group',
|
|
50
|
-
];
|
|
51
41
|
// `o` is the object constructor (`z.object` for lenient, `z.strictObject` for
|
|
52
42
|
// canonical), passed concretely so its precise generic return type is preserved
|
|
53
43
|
// — a `strict ? z.strictObject : z.object` ternary would widen to a union and
|
|
@@ -159,18 +149,38 @@ function build(o) {
|
|
|
159
149
|
.optional()
|
|
160
150
|
.describe('How the element composites with the content beneath it. Omitted means normal.');
|
|
161
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);
|
|
162
161
|
const shapeBase = {
|
|
162
|
+
bindings,
|
|
163
163
|
id: z.string().describe('Unique identifier for the element.'),
|
|
164
164
|
type: z.string().describe('Element type identifier.'),
|
|
165
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."),
|
|
166
170
|
custom: z
|
|
167
171
|
.unknown()
|
|
168
172
|
.optional()
|
|
169
173
|
.describe('Custom data attached to the element.'),
|
|
170
174
|
x: z.number().default(0).describe('X position in pixels.'),
|
|
171
175
|
y: z.number().default(0).describe('Y position in pixels.'),
|
|
172
|
-
width: z
|
|
173
|
-
|
|
176
|
+
width: z
|
|
177
|
+
.number()
|
|
178
|
+
.default(100)
|
|
179
|
+
.describe('Width in pixels. A value of 0 or less reads as 1.'),
|
|
180
|
+
height: z
|
|
181
|
+
.number()
|
|
182
|
+
.default(100)
|
|
183
|
+
.describe('Height in pixels. A value of 0 or less reads as 1.'),
|
|
174
184
|
rotation: z.number().default(0).describe('Rotation angle in degrees.'),
|
|
175
185
|
opacity: z.number().default(1).describe('Opacity level (0-1).'),
|
|
176
186
|
blendMode,
|
|
@@ -250,6 +260,13 @@ function build(o) {
|
|
|
250
260
|
.describe('Whether the element appears in exports.'),
|
|
251
261
|
};
|
|
252
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.'),
|
|
253
270
|
src: z.string().default('').describe('URL or data URI of the image file.'),
|
|
254
271
|
cropX: z.number().default(0).describe('Crop X offset as a fraction (0-1).'),
|
|
255
272
|
cropY: z.number().default(0).describe('Crop Y offset as a fraction (0-1).'),
|
|
@@ -287,8 +304,12 @@ function build(o) {
|
|
|
287
304
|
};
|
|
288
305
|
const textShape = {
|
|
289
306
|
...shapeBase,
|
|
307
|
+
bindings: paintBindings,
|
|
290
308
|
type: z.literal('text').describe('Element type identifier.'),
|
|
291
|
-
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.'),
|
|
292
313
|
placeholder: z
|
|
293
314
|
.string()
|
|
294
315
|
.default('')
|
|
@@ -331,7 +352,10 @@ function build(o) {
|
|
|
331
352
|
.string()
|
|
332
353
|
.default(TEXT_DEFAULTS.align)
|
|
333
354
|
.describe('Horizontal alignment (commonly left | center | right | justify).'),
|
|
334
|
-
width: z
|
|
355
|
+
width: z
|
|
356
|
+
.number()
|
|
357
|
+
.default(100)
|
|
358
|
+
.describe('Width in pixels. A value of 0 or less reads as 1.'),
|
|
335
359
|
height: z
|
|
336
360
|
.number()
|
|
337
361
|
.default(0)
|
|
@@ -424,10 +448,12 @@ function build(o) {
|
|
|
424
448
|
const imageShape = {
|
|
425
449
|
...shapeBase,
|
|
426
450
|
...imageProps,
|
|
451
|
+
bindings: mediaBindings,
|
|
427
452
|
type: z.literal('image').describe('Element type identifier.'),
|
|
428
453
|
};
|
|
429
454
|
const svgShape = {
|
|
430
455
|
...shapeBase,
|
|
456
|
+
bindings: mediaBindings,
|
|
431
457
|
type: z.literal('svg').describe('Element type identifier.'),
|
|
432
458
|
src: z.string().default('').describe('URL or data URI of the SVG.'),
|
|
433
459
|
maskSrc: z
|
|
@@ -459,8 +485,14 @@ function build(o) {
|
|
|
459
485
|
const lineShape = {
|
|
460
486
|
...shapeBase,
|
|
461
487
|
type: z.literal('line').describe('Element type identifier.'),
|
|
462
|
-
width: z
|
|
463
|
-
|
|
488
|
+
width: z
|
|
489
|
+
.number()
|
|
490
|
+
.default(400)
|
|
491
|
+
.describe('Width in pixels. A value of 0 or less reads as 1.'),
|
|
492
|
+
height: z
|
|
493
|
+
.number()
|
|
494
|
+
.default(10)
|
|
495
|
+
.describe('Height in pixels. A value of 0 or less reads as 1.'),
|
|
464
496
|
color: z.string().default('black').describe('Line color.'),
|
|
465
497
|
dash: z.array(z.number()).default([]).describe('Dash pattern array.'),
|
|
466
498
|
startHead: z
|
|
@@ -474,6 +506,7 @@ function build(o) {
|
|
|
474
506
|
};
|
|
475
507
|
const figureShape = {
|
|
476
508
|
...shapeBase,
|
|
509
|
+
bindings: paintBindings,
|
|
477
510
|
type: z.literal('figure').describe('Element type identifier.'),
|
|
478
511
|
subType: z
|
|
479
512
|
.string()
|
|
@@ -491,6 +524,7 @@ function build(o) {
|
|
|
491
524
|
const gifShape = {
|
|
492
525
|
...shapeBase,
|
|
493
526
|
...imageProps,
|
|
527
|
+
bindings: mediaBindings,
|
|
494
528
|
type: z.literal('gif').describe('Element type identifier.'),
|
|
495
529
|
duration: z.number().default(0).describe('GIF duration in milliseconds.'),
|
|
496
530
|
keepRatio: z
|
|
@@ -507,6 +541,7 @@ function build(o) {
|
|
|
507
541
|
const videoShape = {
|
|
508
542
|
...shapeBase,
|
|
509
543
|
...imageProps,
|
|
544
|
+
bindings: mediaBindings,
|
|
510
545
|
type: z.literal('video').describe('Element type identifier.'),
|
|
511
546
|
duration: z.number().default(0).describe('Video duration in milliseconds.'),
|
|
512
547
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
@@ -535,7 +570,10 @@ function build(o) {
|
|
|
535
570
|
.literal('tablecell')
|
|
536
571
|
.default('tablecell')
|
|
537
572
|
.describe('Cell type identifier.'),
|
|
538
|
-
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.'),
|
|
539
577
|
opacity: z.number().optional().describe('Opacity level (0-1).'),
|
|
540
578
|
rowSpan: z.number().optional().describe('Number of rows this cell spans.'),
|
|
541
579
|
colSpan: z
|
|
@@ -633,8 +671,14 @@ function build(o) {
|
|
|
633
671
|
const tableShape = {
|
|
634
672
|
...shapeBaseNoEffects,
|
|
635
673
|
type: z.literal('table').describe('Element type identifier.'),
|
|
636
|
-
width: z
|
|
637
|
-
|
|
674
|
+
width: z
|
|
675
|
+
.number()
|
|
676
|
+
.default(300)
|
|
677
|
+
.describe('Width in pixels. A value of 0 or less reads as 1.'),
|
|
678
|
+
height: z
|
|
679
|
+
.number()
|
|
680
|
+
.default(200)
|
|
681
|
+
.describe('Height in pixels. A value of 0 or less reads as 1.'),
|
|
638
682
|
rows: z.number().default(3).describe('Number of rows.'),
|
|
639
683
|
cols: z.number().default(3).describe('Number of columns.'),
|
|
640
684
|
colWidths: z
|
|
@@ -718,10 +762,12 @@ function build(o) {
|
|
|
718
762
|
.describe('Default cell padding in pixels.'),
|
|
719
763
|
};
|
|
720
764
|
const groupShape = (element) => ({
|
|
765
|
+
bindings,
|
|
721
766
|
id: shapeBase.id,
|
|
722
767
|
type: z.literal('group').describe('Element type identifier.'),
|
|
723
768
|
name: shapeBase.name,
|
|
724
769
|
custom: shapeBase.custom,
|
|
770
|
+
href: shapeBase.href,
|
|
725
771
|
opacity: shapeBase.opacity,
|
|
726
772
|
blendMode: z
|
|
727
773
|
.enum(GROUP_BLEND_MODES)
|
|
@@ -738,9 +784,8 @@ function build(o) {
|
|
|
738
784
|
.default([])
|
|
739
785
|
.describe('Child elements in this group.'),
|
|
740
786
|
});
|
|
741
|
-
const element = z.lazy(() =>
|
|
742
|
-
|
|
743
|
-
z.discriminatedUnion('type', [
|
|
787
|
+
const element = z.lazy(() => z.discriminatedUnion('type', elements));
|
|
788
|
+
const elements = [
|
|
744
789
|
o(textShape),
|
|
745
790
|
o(imageShape),
|
|
746
791
|
o(svgShape),
|
|
@@ -751,8 +796,8 @@ function build(o) {
|
|
|
751
796
|
o(videoShape),
|
|
752
797
|
o(tableShape),
|
|
753
798
|
o(groupShape(element)),
|
|
754
|
-
]
|
|
755
|
-
const
|
|
799
|
+
];
|
|
800
|
+
const font = o({
|
|
756
801
|
fontFamily: z.string().describe('Font family name.'),
|
|
757
802
|
url: z
|
|
758
803
|
.string()
|
|
@@ -766,8 +811,8 @@ function build(o) {
|
|
|
766
811
|
}))
|
|
767
812
|
.optional()
|
|
768
813
|
.describe('Per-weight/style variants for multi-variant families.'),
|
|
769
|
-
};
|
|
770
|
-
const
|
|
814
|
+
});
|
|
815
|
+
const audio = o({
|
|
771
816
|
id: z.string().describe('Unique identifier for the audio track.'),
|
|
772
817
|
name: z
|
|
773
818
|
.string()
|
|
@@ -787,7 +832,7 @@ function build(o) {
|
|
|
787
832
|
.default(1)
|
|
788
833
|
.describe('Playback speed multiplier (1 = normal).'),
|
|
789
834
|
custom: z.unknown().optional(),
|
|
790
|
-
};
|
|
835
|
+
});
|
|
791
836
|
const page = o({
|
|
792
837
|
id: z.string().describe('Unique identifier for the page.'),
|
|
793
838
|
width: z
|
|
@@ -830,6 +875,41 @@ function build(o) {
|
|
|
830
875
|
children: z.array(element).default([]).describe('Elements on this page.'),
|
|
831
876
|
});
|
|
832
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.'),
|
|
833
913
|
// Labels the OUTPUT only. migrateDesign runs before this parse and reads
|
|
834
914
|
// the RAW value, where a missing schemaVersion means 0 (the oldest format,
|
|
835
915
|
// see migrate.ts) — not the current one. The two are not in conflict: by
|
|
@@ -855,35 +935,14 @@ function build(o) {
|
|
|
855
935
|
.unknown()
|
|
856
936
|
.optional()
|
|
857
937
|
.describe('Custom data attached to the design.'),
|
|
858
|
-
fonts: z
|
|
859
|
-
.array(o(fontShape))
|
|
860
|
-
.default([])
|
|
861
|
-
.describe('Fonts used in the design.'),
|
|
938
|
+
fonts: z.array(font).default([]).describe('Fonts used in the design.'),
|
|
862
939
|
pages: z.array(page).default([]).describe('Pages in the design.'),
|
|
863
|
-
audios: z
|
|
864
|
-
.array(o(audioShape))
|
|
865
|
-
.default([])
|
|
866
|
-
.describe('Audio tracks in the design.'),
|
|
940
|
+
audios: z.array(audio).default([]).describe('Audio tracks in the design.'),
|
|
867
941
|
});
|
|
868
|
-
|
|
869
|
-
// guard are wrapped from these once at module level (below) so they aren't
|
|
870
|
-
// rebuilt on the canonical `build()` call.
|
|
871
|
-
const shapes = {
|
|
872
|
-
text: textShape,
|
|
873
|
-
image: imageShape,
|
|
874
|
-
svg: svgShape,
|
|
875
|
-
line: lineShape,
|
|
876
|
-
figure: figureShape,
|
|
877
|
-
gif: gifShape,
|
|
878
|
-
lottie: lottieShape,
|
|
879
|
-
video: videoShape,
|
|
880
|
-
table: tableShape,
|
|
881
|
-
font: fontShape,
|
|
882
|
-
audio: audioShape,
|
|
883
|
-
};
|
|
884
|
-
return { design, shapes };
|
|
942
|
+
return { design, elements, font, audio };
|
|
885
943
|
}
|
|
886
944
|
const lenient = build(z.object);
|
|
945
|
+
export const ELEMENT_TYPES = lenient.elements.map((schema) => schema.shape.type.value);
|
|
887
946
|
/** Non-strict: strips unknown keys, fills defaults. `normalizeDesign` uses this. */
|
|
888
947
|
export const designSchemaLenient = lenient.design;
|
|
889
948
|
/**
|
|
@@ -892,24 +951,6 @@ export const designSchemaLenient = lenient.design;
|
|
|
892
951
|
* as the single inferred source (this tree is only ever parsed, never inferred).
|
|
893
952
|
*/
|
|
894
953
|
export const designSchemaCanonical = build(z.strictObject).design;
|
|
895
|
-
/**
|
|
896
|
-
* Lenient per-type leaf schemas, used by the compile-time type-drift guard.
|
|
897
|
-
* `z.infer` is identical whether strict or not, so the non-strict wrapping here
|
|
898
|
-
* matches the hand-written `./types`. Recursive group/page/design are excluded.
|
|
899
|
-
*/
|
|
900
|
-
export const leafSchemas = {
|
|
901
|
-
text: z.object(lenient.shapes.text),
|
|
902
|
-
image: z.object(lenient.shapes.image),
|
|
903
|
-
svg: z.object(lenient.shapes.svg),
|
|
904
|
-
line: z.object(lenient.shapes.line),
|
|
905
|
-
figure: z.object(lenient.shapes.figure),
|
|
906
|
-
gif: z.object(lenient.shapes.gif),
|
|
907
|
-
lottie: z.object(lenient.shapes.lottie),
|
|
908
|
-
video: z.object(lenient.shapes.video),
|
|
909
|
-
table: z.object(lenient.shapes.table),
|
|
910
|
-
font: z.object(lenient.shapes.font),
|
|
911
|
-
audio: z.object(lenient.shapes.audio),
|
|
912
|
-
};
|
|
913
954
|
function toErrors(issues) {
|
|
914
955
|
return issues.map((issue) => ({
|
|
915
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. */
|
|
@@ -128,9 +145,9 @@ export interface ShapeAttributes {
|
|
|
128
145
|
x: number;
|
|
129
146
|
/** Y position in pixels. */
|
|
130
147
|
y: number;
|
|
131
|
-
/** Width in pixels. */
|
|
148
|
+
/** Width in pixels. A value of 0 or less reads as 1. */
|
|
132
149
|
width: number;
|
|
133
|
-
/** Height in pixels. */
|
|
150
|
+
/** Height in pixels. A value of 0 or less reads as 1, except on text. */
|
|
134
151
|
height: number;
|
|
135
152
|
/** Rotation in degrees. */
|
|
136
153
|
rotation: number;
|
|
@@ -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
|
+
}
|