@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/README.md +26 -7
- package/lib/asset-urls.js +1 -1
- package/lib/defaults.d.ts +1 -1
- package/lib/defaults.js +4 -1
- package/lib/design.schema.json +1 -1
- package/lib/errors.d.ts +12 -5
- package/lib/errors.js +1 -3
- package/lib/migrate.d.ts +7 -1
- package/lib/migrate.js +68 -7
- package/lib/normalize.d.ts +5 -0
- package/lib/normalize.js +6 -2
- package/lib/schema.d.ts +333 -134
- package/lib/schema.js +153 -73
- package/lib/text.d.ts +17 -0
- package/lib/text.js +43 -0
- package/lib/types.d.ts +73 -3
- package/lib/types.js +1 -1
- package/lib/variable-text.d.ts +8 -0
- package/lib/variable-text.js +109 -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
|
-
export const CURRENT_SCHEMA_VERSION =
|
|
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
|
-
];
|
|
40
|
+
export const CURRENT_SCHEMA_VERSION = 6;
|
|
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('')
|
|
@@ -387,7 +400,31 @@ function build(o) {
|
|
|
387
400
|
backgroundPadding: z
|
|
388
401
|
.number()
|
|
389
402
|
.default(0.5)
|
|
390
|
-
.describe('
|
|
403
|
+
.describe('How far the text background and border reach past what they paint around (the spread), as a fraction of half a line at the authored font size. Never moves the text.'),
|
|
404
|
+
// A plain string, not an enum, for the reason `textOverflow` gives below:
|
|
405
|
+
// the editor's model throws on an enum value it does not know.
|
|
406
|
+
backgroundShape: z
|
|
407
|
+
.string()
|
|
408
|
+
.optional()
|
|
409
|
+
.describe("What the text background and border paint around: 'lines' (each line, the default) or 'box' (the element's frame). Unknown values count as 'lines'."),
|
|
410
|
+
// Text padding (docs/text-padding.md). Optional with NO default, like the
|
|
411
|
+
// text-sizing fields, so existing JSON and every snapshot stay unchanged.
|
|
412
|
+
paddingTop: z
|
|
413
|
+
.number()
|
|
414
|
+
.optional()
|
|
415
|
+
.describe('Space between the text and the top edge of its frame, in pixels. Missing means 0.'),
|
|
416
|
+
paddingRight: z
|
|
417
|
+
.number()
|
|
418
|
+
.optional()
|
|
419
|
+
.describe('Space between the text and the right edge of its frame, in pixels. Missing means 0.'),
|
|
420
|
+
paddingBottom: z
|
|
421
|
+
.number()
|
|
422
|
+
.optional()
|
|
423
|
+
.describe('Space between the text and the bottom edge of its frame, in pixels. Missing means 0.'),
|
|
424
|
+
paddingLeft: z
|
|
425
|
+
.number()
|
|
426
|
+
.optional()
|
|
427
|
+
.describe('Space between the text and the left edge of its frame, in pixels. Missing means 0.'),
|
|
391
428
|
borderColor: z.string().default('black').describe('Text box border color.'),
|
|
392
429
|
borderSize: z
|
|
393
430
|
.number()
|
|
@@ -401,10 +438,12 @@ function build(o) {
|
|
|
401
438
|
.number()
|
|
402
439
|
.default(TEXT_DEFAULTS.curvePower)
|
|
403
440
|
.describe('Text curve power (-1 to 1, 0 = straight).'),
|
|
441
|
+
// Accepted so strict validation still passes a design saved before v6;
|
|
442
|
+
// migrateDesign turns it into `backgroundShape: 'box'` and nothing writes it.
|
|
404
443
|
legacyBackground: z
|
|
405
444
|
.boolean()
|
|
406
445
|
.optional()
|
|
407
|
-
.describe(
|
|
446
|
+
.describe("Deprecated input: migrateDesign turns true into backgroundShape 'box'. Never written."),
|
|
408
447
|
// Text sizing. All optional with NO default, so existing JSON and every
|
|
409
448
|
// normalizeDesign snapshot stay unchanged; a missing field means "follow
|
|
410
449
|
// the global flag". @polotno/core/text-sizing resolves them.
|
|
@@ -435,10 +474,12 @@ function build(o) {
|
|
|
435
474
|
const imageShape = {
|
|
436
475
|
...shapeBase,
|
|
437
476
|
...imageProps,
|
|
477
|
+
bindings: mediaBindings,
|
|
438
478
|
type: z.literal('image').describe('Element type identifier.'),
|
|
439
479
|
};
|
|
440
480
|
const svgShape = {
|
|
441
481
|
...shapeBase,
|
|
482
|
+
bindings: mediaBindings,
|
|
442
483
|
type: z.literal('svg').describe('Element type identifier.'),
|
|
443
484
|
src: z.string().default('').describe('URL or data URI of the SVG.'),
|
|
444
485
|
maskSrc: z
|
|
@@ -491,6 +532,7 @@ function build(o) {
|
|
|
491
532
|
};
|
|
492
533
|
const figureShape = {
|
|
493
534
|
...shapeBase,
|
|
535
|
+
bindings: paintBindings,
|
|
494
536
|
type: z.literal('figure').describe('Element type identifier.'),
|
|
495
537
|
subType: z
|
|
496
538
|
.string()
|
|
@@ -508,6 +550,7 @@ function build(o) {
|
|
|
508
550
|
const gifShape = {
|
|
509
551
|
...shapeBase,
|
|
510
552
|
...imageProps,
|
|
553
|
+
bindings: mediaBindings,
|
|
511
554
|
type: z.literal('gif').describe('Element type identifier.'),
|
|
512
555
|
duration: z.number().default(0).describe('GIF duration in milliseconds.'),
|
|
513
556
|
keepRatio: z
|
|
@@ -524,6 +567,7 @@ function build(o) {
|
|
|
524
567
|
const videoShape = {
|
|
525
568
|
...shapeBase,
|
|
526
569
|
...imageProps,
|
|
570
|
+
bindings: mediaBindings,
|
|
527
571
|
type: z.literal('video').describe('Element type identifier.'),
|
|
528
572
|
duration: z.number().default(0).describe('Video duration in milliseconds.'),
|
|
529
573
|
startTime: z.number().default(0).describe('Start offset (0-1).'),
|
|
@@ -552,7 +596,10 @@ function build(o) {
|
|
|
552
596
|
.literal('tablecell')
|
|
553
597
|
.default('tablecell')
|
|
554
598
|
.describe('Cell type identifier.'),
|
|
555
|
-
text: z
|
|
599
|
+
text: z
|
|
600
|
+
.string()
|
|
601
|
+
.default('')
|
|
602
|
+
.describe('Text shown in the cell: plain, or rich HTML. `{{name}}` prints a variable; `<a href>` links the words it wraps.'),
|
|
556
603
|
opacity: z.number().optional().describe('Opacity level (0-1).'),
|
|
557
604
|
rowSpan: z.number().optional().describe('Number of rows this cell spans.'),
|
|
558
605
|
colSpan: z
|
|
@@ -639,10 +686,28 @@ function build(o) {
|
|
|
639
686
|
.string()
|
|
640
687
|
.optional()
|
|
641
688
|
.describe('Cell background color (inherits from the table when omitted).'),
|
|
689
|
+
cellPaddingTop: z
|
|
690
|
+
.number()
|
|
691
|
+
.optional()
|
|
692
|
+
.describe('Space between the text and the top of the cell, in pixels (inherits from the table when omitted).'),
|
|
693
|
+
cellPaddingRight: z
|
|
694
|
+
.number()
|
|
695
|
+
.optional()
|
|
696
|
+
.describe('Space between the text and the right of the cell, in pixels (inherits from the table when omitted).'),
|
|
697
|
+
cellPaddingBottom: z
|
|
698
|
+
.number()
|
|
699
|
+
.optional()
|
|
700
|
+
.describe('Space between the text and the bottom of the cell, in pixels (inherits from the table when omitted).'),
|
|
701
|
+
cellPaddingLeft: z
|
|
702
|
+
.number()
|
|
703
|
+
.optional()
|
|
704
|
+
.describe('Space between the text and the left of the cell, in pixels (inherits from the table when omitted).'),
|
|
705
|
+
// Deprecated input until polotno 5.0: migrateDesign expands it to the four
|
|
706
|
+
// sides and nothing writes it (docs/text-padding.md §6).
|
|
642
707
|
cellPadding: z
|
|
643
708
|
.number()
|
|
644
709
|
.optional()
|
|
645
|
-
.describe('
|
|
710
|
+
.describe('Deprecated input: all four cell paddings at once. migrateDesign expands it; never written.'),
|
|
646
711
|
});
|
|
647
712
|
// Effect props are nonsensical for tables and not part of its serialized form —
|
|
648
713
|
// omit them via a typed destructure (so `tableShape` keeps precise field types).
|
|
@@ -735,16 +800,36 @@ function build(o) {
|
|
|
735
800
|
.string()
|
|
736
801
|
.default('transparent')
|
|
737
802
|
.describe('Default cell background color.'),
|
|
738
|
-
|
|
803
|
+
cellPaddingTop: z
|
|
804
|
+
.number()
|
|
805
|
+
.default(4)
|
|
806
|
+
.describe('Default space between the text and the top of a cell, in pixels.'),
|
|
807
|
+
cellPaddingRight: z
|
|
808
|
+
.number()
|
|
809
|
+
.default(4)
|
|
810
|
+
.describe('Default space between the text and the right of a cell, in pixels.'),
|
|
811
|
+
cellPaddingBottom: z
|
|
812
|
+
.number()
|
|
813
|
+
.default(4)
|
|
814
|
+
.describe('Default space between the text and the bottom of a cell, in pixels.'),
|
|
815
|
+
cellPaddingLeft: z
|
|
739
816
|
.number()
|
|
740
817
|
.default(4)
|
|
741
|
-
.describe('Default cell
|
|
818
|
+
.describe('Default space between the text and the left of a cell, in pixels.'),
|
|
819
|
+
// Deprecated input until polotno 5.0: migrateDesign expands it to the four
|
|
820
|
+
// sides and nothing writes it (docs/text-padding.md §6).
|
|
821
|
+
cellPadding: z
|
|
822
|
+
.number()
|
|
823
|
+
.optional()
|
|
824
|
+
.describe('Deprecated input: all four default cell paddings at once. migrateDesign expands it; never written.'),
|
|
742
825
|
};
|
|
743
826
|
const groupShape = (element) => ({
|
|
827
|
+
bindings,
|
|
744
828
|
id: shapeBase.id,
|
|
745
829
|
type: z.literal('group').describe('Element type identifier.'),
|
|
746
830
|
name: shapeBase.name,
|
|
747
831
|
custom: shapeBase.custom,
|
|
832
|
+
href: shapeBase.href,
|
|
748
833
|
opacity: shapeBase.opacity,
|
|
749
834
|
blendMode: z
|
|
750
835
|
.enum(GROUP_BLEND_MODES)
|
|
@@ -761,9 +846,8 @@ function build(o) {
|
|
|
761
846
|
.default([])
|
|
762
847
|
.describe('Child elements in this group.'),
|
|
763
848
|
});
|
|
764
|
-
const element = z.lazy(() =>
|
|
765
|
-
|
|
766
|
-
z.discriminatedUnion('type', [
|
|
849
|
+
const element = z.lazy(() => z.discriminatedUnion('type', elements));
|
|
850
|
+
const elements = [
|
|
767
851
|
o(textShape),
|
|
768
852
|
o(imageShape),
|
|
769
853
|
o(svgShape),
|
|
@@ -774,8 +858,8 @@ function build(o) {
|
|
|
774
858
|
o(videoShape),
|
|
775
859
|
o(tableShape),
|
|
776
860
|
o(groupShape(element)),
|
|
777
|
-
]
|
|
778
|
-
const
|
|
861
|
+
];
|
|
862
|
+
const font = o({
|
|
779
863
|
fontFamily: z.string().describe('Font family name.'),
|
|
780
864
|
url: z
|
|
781
865
|
.string()
|
|
@@ -789,8 +873,8 @@ function build(o) {
|
|
|
789
873
|
}))
|
|
790
874
|
.optional()
|
|
791
875
|
.describe('Per-weight/style variants for multi-variant families.'),
|
|
792
|
-
};
|
|
793
|
-
const
|
|
876
|
+
});
|
|
877
|
+
const audio = o({
|
|
794
878
|
id: z.string().describe('Unique identifier for the audio track.'),
|
|
795
879
|
name: z
|
|
796
880
|
.string()
|
|
@@ -810,7 +894,7 @@ function build(o) {
|
|
|
810
894
|
.default(1)
|
|
811
895
|
.describe('Playback speed multiplier (1 = normal).'),
|
|
812
896
|
custom: z.unknown().optional(),
|
|
813
|
-
};
|
|
897
|
+
});
|
|
814
898
|
const page = o({
|
|
815
899
|
id: z.string().describe('Unique identifier for the page.'),
|
|
816
900
|
width: z
|
|
@@ -853,6 +937,41 @@ function build(o) {
|
|
|
853
937
|
children: z.array(element).default([]).describe('Elements on this page.'),
|
|
854
938
|
});
|
|
855
939
|
const design = o({
|
|
940
|
+
variables: z
|
|
941
|
+
.array(o({
|
|
942
|
+
name: name.describe('Variable name: letters, digits and `_`, with `.` between path segments.'),
|
|
943
|
+
label: z.string().optional().describe('Name shown to people.'),
|
|
944
|
+
description: z
|
|
945
|
+
.string()
|
|
946
|
+
.optional()
|
|
947
|
+
.describe('Help text for whoever fills the variable.'),
|
|
948
|
+
required: z
|
|
949
|
+
.boolean()
|
|
950
|
+
.optional()
|
|
951
|
+
.describe('A record must supply the value; `default` does not.'),
|
|
952
|
+
default: z
|
|
953
|
+
.union([z.string(), z.boolean()])
|
|
954
|
+
.optional()
|
|
955
|
+
.describe('Value used when none is supplied.'),
|
|
956
|
+
value: z
|
|
957
|
+
.union([z.string(), z.boolean()])
|
|
958
|
+
.optional()
|
|
959
|
+
.describe('Value the design draws now.'),
|
|
960
|
+
}))
|
|
961
|
+
.superRefine((definitions, ctx) => {
|
|
962
|
+
const seen = new Set();
|
|
963
|
+
definitions.forEach(({ name }, i) => {
|
|
964
|
+
if (seen.has(name))
|
|
965
|
+
ctx.addIssue({
|
|
966
|
+
code: 'custom',
|
|
967
|
+
path: [i, 'name'],
|
|
968
|
+
message: 'Duplicate variable name',
|
|
969
|
+
});
|
|
970
|
+
seen.add(name);
|
|
971
|
+
});
|
|
972
|
+
})
|
|
973
|
+
.optional()
|
|
974
|
+
.describe('Template variables: the value of each and how to present it. Text tokens and bindings read them.'),
|
|
856
975
|
// Labels the OUTPUT only. migrateDesign runs before this parse and reads
|
|
857
976
|
// the RAW value, where a missing schemaVersion means 0 (the oldest format,
|
|
858
977
|
// see migrate.ts) — not the current one. The two are not in conflict: by
|
|
@@ -878,35 +997,14 @@ function build(o) {
|
|
|
878
997
|
.unknown()
|
|
879
998
|
.optional()
|
|
880
999
|
.describe('Custom data attached to the design.'),
|
|
881
|
-
fonts: z
|
|
882
|
-
.array(o(fontShape))
|
|
883
|
-
.default([])
|
|
884
|
-
.describe('Fonts used in the design.'),
|
|
1000
|
+
fonts: z.array(font).default([]).describe('Fonts used in the design.'),
|
|
885
1001
|
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.'),
|
|
1002
|
+
audios: z.array(audio).default([]).describe('Audio tracks in the design.'),
|
|
890
1003
|
});
|
|
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 };
|
|
1004
|
+
return { design, elements, font, audio };
|
|
908
1005
|
}
|
|
909
1006
|
const lenient = build(z.object);
|
|
1007
|
+
export const ELEMENT_TYPES = lenient.elements.map((schema) => schema.shape.type.value);
|
|
910
1008
|
/** Non-strict: strips unknown keys, fills defaults. `normalizeDesign` uses this. */
|
|
911
1009
|
export const designSchemaLenient = lenient.design;
|
|
912
1010
|
/**
|
|
@@ -915,24 +1013,6 @@ export const designSchemaLenient = lenient.design;
|
|
|
915
1013
|
* as the single inferred source (this tree is only ever parsed, never inferred).
|
|
916
1014
|
*/
|
|
917
1015
|
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
1016
|
function toErrors(issues) {
|
|
937
1017
|
return issues.map((issue) => ({
|
|
938
1018
|
// 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;
|
|
@@ -215,12 +235,34 @@ export interface TextElement extends ShapeAttributes {
|
|
|
215
235
|
backgroundColor: string;
|
|
216
236
|
backgroundOpacity: number;
|
|
217
237
|
backgroundCornerRadius: number;
|
|
238
|
+
/**
|
|
239
|
+
* How far the background and border reach past what they paint around (the
|
|
240
|
+
* spread), as a fraction of half a line at the authored font size. Never
|
|
241
|
+
* moves the text.
|
|
242
|
+
*/
|
|
218
243
|
backgroundPadding: number;
|
|
244
|
+
/**
|
|
245
|
+
* What the background and border paint around: 'lines' (each line, the
|
|
246
|
+
* default) or 'box' (the element's frame). A plain string: unknown values
|
|
247
|
+
* count as 'lines'.
|
|
248
|
+
*/
|
|
249
|
+
backgroundShape?: string;
|
|
250
|
+
/** Space between the text and the top edge of its frame, px. Missing is 0. */
|
|
251
|
+
paddingTop?: number;
|
|
252
|
+
/** Space between the text and the right edge of its frame, px. Missing is 0. */
|
|
253
|
+
paddingRight?: number;
|
|
254
|
+
/** Space between the text and the bottom edge of its frame, px. Missing is 0. */
|
|
255
|
+
paddingBottom?: number;
|
|
256
|
+
/** Space between the text and the left edge of its frame, px. Missing is 0. */
|
|
257
|
+
paddingLeft?: number;
|
|
219
258
|
borderColor: string;
|
|
220
259
|
borderSize: number;
|
|
221
260
|
curveEnabled: boolean;
|
|
222
261
|
curvePower: number;
|
|
223
|
-
/**
|
|
262
|
+
/**
|
|
263
|
+
* @deprecated Input only: `migrateDesign` turns `true` into
|
|
264
|
+
* `backgroundShape: 'box'`. Never written.
|
|
265
|
+
*/
|
|
224
266
|
legacyBackground?: boolean;
|
|
225
267
|
/**
|
|
226
268
|
* 'resize' | 'ellipsis' | 'fit'. A plain string so an unknown future value
|
|
@@ -236,6 +278,13 @@ export interface TextElement extends ShapeAttributes {
|
|
|
236
278
|
}
|
|
237
279
|
/** Image-like crop/border properties shared by image, gif, and video. */
|
|
238
280
|
export interface BaseImageProps {
|
|
281
|
+
/** Fit the source in the frame instead of the explicit crop. */
|
|
282
|
+
fit?: 'cover' | 'contain';
|
|
283
|
+
/** Where the source sits in the frame, 0-1 on each axis. Default center. */
|
|
284
|
+
focus?: {
|
|
285
|
+
x: number;
|
|
286
|
+
y: number;
|
|
287
|
+
};
|
|
239
288
|
/** URL or data URI of the source. */
|
|
240
289
|
src: string;
|
|
241
290
|
/** Crop X offset (0-1). */
|
|
@@ -257,9 +306,11 @@ export interface BaseImageProps {
|
|
|
257
306
|
stretchEnabled: boolean;
|
|
258
307
|
}
|
|
259
308
|
export interface ImageElement extends ShapeAttributes, BaseImageProps {
|
|
309
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
260
310
|
type: 'image';
|
|
261
311
|
}
|
|
262
312
|
export interface SVGElement extends ShapeAttributes {
|
|
313
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
263
314
|
type: 'svg';
|
|
264
315
|
src: string;
|
|
265
316
|
maskSrc: string;
|
|
@@ -288,6 +339,7 @@ export interface LineElement extends ShapeAttributes {
|
|
|
288
339
|
endHead: string;
|
|
289
340
|
}
|
|
290
341
|
export interface FigureElement extends ShapeAttributes {
|
|
342
|
+
bindings?: Pick<Bindings, 'visible' | 'fill'>;
|
|
291
343
|
type: 'figure';
|
|
292
344
|
/** Figure subtype: rect, ellipse, triangle, etc. */
|
|
293
345
|
subType: string;
|
|
@@ -298,6 +350,7 @@ export interface FigureElement extends ShapeAttributes {
|
|
|
298
350
|
cornerRadius: number;
|
|
299
351
|
}
|
|
300
352
|
export interface GifElement extends ShapeAttributes, BaseImageProps {
|
|
353
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
301
354
|
type: 'gif';
|
|
302
355
|
/** Duration in milliseconds. */
|
|
303
356
|
duration: number;
|
|
@@ -306,6 +359,7 @@ export interface LottieElement extends ShapeAttributes, BaseImageProps {
|
|
|
306
359
|
type: 'lottie';
|
|
307
360
|
}
|
|
308
361
|
export interface VideoElement extends ShapeAttributes, BaseImageProps {
|
|
362
|
+
bindings?: Pick<Bindings, 'visible' | 'src'>;
|
|
309
363
|
type: 'video';
|
|
310
364
|
/** Duration in milliseconds. */
|
|
311
365
|
duration: number;
|
|
@@ -359,6 +413,14 @@ export interface TableCell {
|
|
|
359
413
|
strokeWidth?: number;
|
|
360
414
|
stroke?: string;
|
|
361
415
|
cellBackground?: string;
|
|
416
|
+
cellPaddingTop?: number;
|
|
417
|
+
cellPaddingRight?: number;
|
|
418
|
+
cellPaddingBottom?: number;
|
|
419
|
+
cellPaddingLeft?: number;
|
|
420
|
+
/**
|
|
421
|
+
* @deprecated Input only until polotno 5.0: `migrateDesign` expands it to
|
|
422
|
+
* the four `cellPadding*` sides. Never written.
|
|
423
|
+
*/
|
|
362
424
|
cellPadding?: number;
|
|
363
425
|
}
|
|
364
426
|
/**
|
|
@@ -392,5 +454,13 @@ export interface TableElement extends Omit<ShapeAttributes, 'blurEnabled' | 'blu
|
|
|
392
454
|
strokeWidth: number;
|
|
393
455
|
stroke: string;
|
|
394
456
|
cellBackground: string;
|
|
395
|
-
|
|
457
|
+
cellPaddingTop: number;
|
|
458
|
+
cellPaddingRight: number;
|
|
459
|
+
cellPaddingBottom: number;
|
|
460
|
+
cellPaddingLeft: number;
|
|
461
|
+
/**
|
|
462
|
+
* @deprecated Input only until polotno 5.0: `migrateDesign` expands it to
|
|
463
|
+
* the four `cellPadding*` sides. Never written.
|
|
464
|
+
*/
|
|
465
|
+
cellPadding?: number;
|
|
396
466
|
}
|
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,8 @@
|
|
|
1
|
+
export declare const VARIABLE_NAME: RegExp;
|
|
2
|
+
/** Each `{{name}}` token in plain `text`: where it starts and ends, and its name. */
|
|
3
|
+
export declare function variableTokens(text: string): {
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
name: string;
|
|
7
|
+
}[];
|
|
8
|
+
export declare function substituteText(text: string, read: (name: string) => string | undefined): string;
|