@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
|
@@ -0,0 +1,109 @@
|
|
|
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
|
+
/** Each `{{name}}` token in plain `text`: where it starts and ends, and its name. */
|
|
8
|
+
export function variableTokens(text) {
|
|
9
|
+
return Array.from(text.matchAll(TOKEN), (match) => ({
|
|
10
|
+
start: match.index,
|
|
11
|
+
end: match.index + match[0].length,
|
|
12
|
+
name: match[1],
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
const escapeText = (text) => text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
16
|
+
// Keep the original tags and attributes byte-for-byte. Match within a block's
|
|
17
|
+
// visible runs, and write each replacement in the run containing its first brace.
|
|
18
|
+
const TAG = /<!--[\s\S]*?-->|<\/?[a-z][\w:-]*(?:\s+(?:[^>"']|"[^"]*"|'[^']*')*)?\s*\/?>/gi;
|
|
19
|
+
const BLOCK = /^<\/?(?:p|div|br|li|ul|ol|h[1-6])\b/i;
|
|
20
|
+
// A link target is the one attribute a token resolves in. A single token that
|
|
21
|
+
// is the whole target is a URL and goes in as given; any other token is one URL
|
|
22
|
+
// component and is percent-encoded. The renderer's sanitizer still decides
|
|
23
|
+
// which schemes a link may use. Attributes are read in order, each quoted value
|
|
24
|
+
// whole, so an `href=` inside another attribute's value stays text.
|
|
25
|
+
const ATTRIBUTE = /([^\s"'<>/=]+)(?:(\s*=\s*)("[^"]*"|'[^']*'|[^\s"'<>=`]+))?/g;
|
|
26
|
+
const escapeAttribute = (text) => escapeText(text).replace(/"/g, '"').replace(/'/g, ''');
|
|
27
|
+
function substituteHref(tag, read) {
|
|
28
|
+
if (!tag.includes('{{'))
|
|
29
|
+
return tag;
|
|
30
|
+
return tag.replace(ATTRIBUTE, (attribute, name, equals, value) => {
|
|
31
|
+
if (name.toLowerCase() !== 'href' || !/^["']/.test(value ?? ''))
|
|
32
|
+
return attribute;
|
|
33
|
+
const quote = value[0];
|
|
34
|
+
const target = value.slice(1, -1);
|
|
35
|
+
const whole = WHOLE_TOKEN.test(target);
|
|
36
|
+
const filled = target.replace(TOKEN, (token, key) => {
|
|
37
|
+
const value = read(key);
|
|
38
|
+
if (value === undefined)
|
|
39
|
+
return token;
|
|
40
|
+
return whole
|
|
41
|
+
? escapeAttribute(value)
|
|
42
|
+
: encodeURIComponent(value).replace(/'/g, '%27');
|
|
43
|
+
});
|
|
44
|
+
return name + equals + quote + filled + quote;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function substituteText(text, read) {
|
|
48
|
+
if (!text.includes('{'))
|
|
49
|
+
return text;
|
|
50
|
+
if (!looksLikeHtml(text)) {
|
|
51
|
+
const result = text.replace(TOKEN, (token, name) => read(name) ?? token);
|
|
52
|
+
return looksLikeHtml(result) ? escapeText(result) : result;
|
|
53
|
+
}
|
|
54
|
+
const output = [];
|
|
55
|
+
let runs = [];
|
|
56
|
+
let body = '';
|
|
57
|
+
function appendRun(value) {
|
|
58
|
+
if (!value)
|
|
59
|
+
return;
|
|
60
|
+
runs.push({
|
|
61
|
+
start: body.length,
|
|
62
|
+
end: body.length + value.length,
|
|
63
|
+
index: output.length,
|
|
64
|
+
});
|
|
65
|
+
body += value;
|
|
66
|
+
output.push(value);
|
|
67
|
+
}
|
|
68
|
+
function flush() {
|
|
69
|
+
// Edit right-to-left so offsets into the untouched prefix stay valid.
|
|
70
|
+
let i = runs.length - 1;
|
|
71
|
+
const edits = variableTokens(body).map(({ start, end, name }) => ({
|
|
72
|
+
start,
|
|
73
|
+
end,
|
|
74
|
+
value: read(name),
|
|
75
|
+
}));
|
|
76
|
+
for (const { start, end, value } of edits.reverse()) {
|
|
77
|
+
if (value === undefined)
|
|
78
|
+
continue;
|
|
79
|
+
while (runs[i].start >= end)
|
|
80
|
+
i--;
|
|
81
|
+
for (; i >= 0; i--) {
|
|
82
|
+
const run = runs[i];
|
|
83
|
+
const original = output[run.index];
|
|
84
|
+
const from = Math.max(0, start - run.start);
|
|
85
|
+
const to = Math.min(run.end, end) - run.start;
|
|
86
|
+
const first = start >= run.start;
|
|
87
|
+
output[run.index] =
|
|
88
|
+
original.slice(0, from) +
|
|
89
|
+
(first ? escapeText(value).replace(/\r\n?|\n/g, '<br/>') : '') +
|
|
90
|
+
original.slice(to);
|
|
91
|
+
if (first)
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
body = '';
|
|
96
|
+
runs = [];
|
|
97
|
+
}
|
|
98
|
+
let cursor = 0;
|
|
99
|
+
for (const match of text.matchAll(TAG)) {
|
|
100
|
+
appendRun(text.slice(cursor, match.index));
|
|
101
|
+
if (BLOCK.test(match[0]))
|
|
102
|
+
flush();
|
|
103
|
+
output.push(substituteHref(match[0], read));
|
|
104
|
+
cursor = match.index + match[0].length;
|
|
105
|
+
}
|
|
106
|
+
appendRun(text.slice(cursor));
|
|
107
|
+
flush();
|
|
108
|
+
return output.join('');
|
|
109
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Bindings, VariableDefinition } from './types.js';
|
|
2
|
+
export { VARIABLE_NAME, variableTokens } 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>;
|
package/lib/variables.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { substituteText } from './variable-text.js';
|
|
2
|
+
import { removeTags } from './text.js';
|
|
3
|
+
export { VARIABLE_NAME, variableTokens } from './variable-text.js';
|
|
4
|
+
import { polotnoError } from './errors.js';
|
|
5
|
+
const MEDIA = ['src', 'visible'];
|
|
6
|
+
const PAINT = ['fill', 'visible'];
|
|
7
|
+
/** The properties each element type can bind. Any other element binds `visible`. */
|
|
8
|
+
export const BINDABLE_PROPS = {
|
|
9
|
+
image: MEDIA,
|
|
10
|
+
svg: MEDIA,
|
|
11
|
+
gif: MEDIA,
|
|
12
|
+
video: MEDIA,
|
|
13
|
+
text: PAINT,
|
|
14
|
+
figure: PAINT,
|
|
15
|
+
};
|
|
16
|
+
export const bindableProps = (type) => BINDABLE_PROPS[type] ?? [
|
|
17
|
+
'visible',
|
|
18
|
+
];
|
|
19
|
+
/** The type a binding gives its variable. */
|
|
20
|
+
export const bindingType = (elementType, prop) => prop === 'src'
|
|
21
|
+
? elementType === 'video'
|
|
22
|
+
? 'video'
|
|
23
|
+
: 'image'
|
|
24
|
+
: prop === 'fill'
|
|
25
|
+
? 'color'
|
|
26
|
+
: 'boolean';
|
|
27
|
+
function visit(design, callback) {
|
|
28
|
+
function walk(nodes = []) {
|
|
29
|
+
for (const node of nodes) {
|
|
30
|
+
callback(node, node.type);
|
|
31
|
+
if (node.type === 'group')
|
|
32
|
+
walk(node.children);
|
|
33
|
+
if (node.type === 'table') {
|
|
34
|
+
// A cell may omit its discriminator in sparse design JSON.
|
|
35
|
+
for (const cell of node.cells ?? [])
|
|
36
|
+
callback(cell, 'tablecell');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const page of design.pages)
|
|
41
|
+
walk(page.children);
|
|
42
|
+
}
|
|
43
|
+
function uses(node, nodeType, apply, write) {
|
|
44
|
+
if ((nodeType === 'text' || nodeType === 'tablecell') && node.text) {
|
|
45
|
+
const text = substituteText(node.text, (name) => {
|
|
46
|
+
const value = apply(name, 'text', 'text');
|
|
47
|
+
return typeof value === 'string' ? value : undefined;
|
|
48
|
+
});
|
|
49
|
+
if (write && text !== node.text) {
|
|
50
|
+
node.text = text;
|
|
51
|
+
// Zero is the shared "measure on first render" sentinel. Only an
|
|
52
|
+
// explicit 'resize' box follows its content: a missing mode follows the
|
|
53
|
+
// renderer's global, which may fit the text to the authored box.
|
|
54
|
+
if (nodeType === 'text' && node.textOverflow === 'resize')
|
|
55
|
+
node.height = 0;
|
|
56
|
+
// A blank value stays blank: an empty text would draw its placeholder.
|
|
57
|
+
if (node.placeholder && !removeTags(text))
|
|
58
|
+
node.placeholder = '';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!node.bindings)
|
|
62
|
+
return;
|
|
63
|
+
for (const prop of bindableProps(nodeType ?? '')) {
|
|
64
|
+
const name = node.bindings[prop];
|
|
65
|
+
if (name === undefined)
|
|
66
|
+
continue;
|
|
67
|
+
const value = apply(name, prop, bindingType(nodeType, prop));
|
|
68
|
+
// The resolver returns a boolean for `visible` and a string otherwise.
|
|
69
|
+
if (write && value !== undefined)
|
|
70
|
+
Object.assign(node, { [prop]: value });
|
|
71
|
+
}
|
|
72
|
+
if (write)
|
|
73
|
+
delete node.bindings;
|
|
74
|
+
}
|
|
75
|
+
export function getVariables(design) {
|
|
76
|
+
const variables = new Map();
|
|
77
|
+
// Binding kinds only: any variable may also print in text.
|
|
78
|
+
const bound = new Map();
|
|
79
|
+
for (const definition of design.variables ?? []) {
|
|
80
|
+
variables.set(definition.name, {
|
|
81
|
+
...definition,
|
|
82
|
+
type: 'text',
|
|
83
|
+
declared: true,
|
|
84
|
+
usages: [],
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
visit(design, (node, nodeType) => uses(node, nodeType, (name, property, type) => {
|
|
88
|
+
const item = variables.get(name) ?? {
|
|
89
|
+
name,
|
|
90
|
+
type,
|
|
91
|
+
declared: false,
|
|
92
|
+
usages: [],
|
|
93
|
+
};
|
|
94
|
+
if (type !== 'text') {
|
|
95
|
+
const kinds = bound.get(name) ?? new Set();
|
|
96
|
+
if (!kinds.size)
|
|
97
|
+
item.type = type;
|
|
98
|
+
bound.set(name, kinds.add(type));
|
|
99
|
+
}
|
|
100
|
+
item.usages.push({ elementId: node.id ?? '', property });
|
|
101
|
+
variables.set(name, item);
|
|
102
|
+
return undefined;
|
|
103
|
+
}, false));
|
|
104
|
+
const issues = [];
|
|
105
|
+
for (const item of variables.values()) {
|
|
106
|
+
if (!item.usages.length)
|
|
107
|
+
issues.push({
|
|
108
|
+
severity: 'warning',
|
|
109
|
+
reason: 'orphan',
|
|
110
|
+
variable: item.name,
|
|
111
|
+
});
|
|
112
|
+
if ((bound.get(item.name)?.size ?? 0) > 1)
|
|
113
|
+
issues.push({
|
|
114
|
+
severity: 'warning',
|
|
115
|
+
reason: 'mixed-types',
|
|
116
|
+
variable: item.name,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return { variables: [...variables.values()], issues };
|
|
120
|
+
}
|
|
121
|
+
/** The record value a dotted variable name reads, or undefined. */
|
|
122
|
+
export function readPath(record, name) {
|
|
123
|
+
let current = record;
|
|
124
|
+
for (const key of name.split('.')) {
|
|
125
|
+
if (!current ||
|
|
126
|
+
typeof current !== 'object' ||
|
|
127
|
+
!Object.prototype.hasOwnProperty.call(current, key))
|
|
128
|
+
return undefined;
|
|
129
|
+
current = current[key];
|
|
130
|
+
}
|
|
131
|
+
return current;
|
|
132
|
+
}
|
|
133
|
+
/** A value as the property it fills reads it, or undefined. */
|
|
134
|
+
function typed(value, property) {
|
|
135
|
+
if (property === 'text')
|
|
136
|
+
return ['string', 'number', 'boolean'].includes(typeof value)
|
|
137
|
+
? String(value)
|
|
138
|
+
: undefined;
|
|
139
|
+
if (property === 'visible')
|
|
140
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
141
|
+
return typeof value === 'string' ? value : undefined;
|
|
142
|
+
}
|
|
143
|
+
/** The registry by name, for `fillElement`: build it once per registry. */
|
|
144
|
+
export const variablesByName = (variables) => new Map(variables?.map((v) => [v.name, v]));
|
|
145
|
+
/** What a variable reads: a record replaces the stored values. */
|
|
146
|
+
function reader(definitions, record) {
|
|
147
|
+
return (name) => (record ? readPath(record, name) : definitions.get(name)?.value) ??
|
|
148
|
+
definitions.get(name)?.default;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Fill `design` in place, from `record` or else from its stored values, and
|
|
152
|
+
* strip the template. Returns the issues about the record.
|
|
153
|
+
*/
|
|
154
|
+
export function fillVariables(design, record) {
|
|
155
|
+
const read = reader(variablesByName(design.variables), record);
|
|
156
|
+
const issues = [];
|
|
157
|
+
const keys = new Set();
|
|
158
|
+
function error(variable, reason) {
|
|
159
|
+
if (!issues.some((i) => i.reason === reason && i.variable === variable))
|
|
160
|
+
issues.push({ severity: 'error', reason, variable });
|
|
161
|
+
}
|
|
162
|
+
for (const definition of design.variables ?? []) {
|
|
163
|
+
keys.add(definition.name.split('.')[0]);
|
|
164
|
+
if (record && definition.required)
|
|
165
|
+
if (readPath(record, definition.name) == null)
|
|
166
|
+
error(definition.name, 'missing-required');
|
|
167
|
+
}
|
|
168
|
+
visit(design, (node, nodeType) => uses(node, nodeType, (name, property) => {
|
|
169
|
+
keys.add(name.split('.')[0]);
|
|
170
|
+
const value = read(name);
|
|
171
|
+
if (value == null)
|
|
172
|
+
return undefined;
|
|
173
|
+
const filled = typed(value, property);
|
|
174
|
+
if (filled === undefined)
|
|
175
|
+
error(name, 'type-mismatch');
|
|
176
|
+
return filled;
|
|
177
|
+
}, true));
|
|
178
|
+
delete design.variables;
|
|
179
|
+
for (const key of Object.keys(record ?? {})) {
|
|
180
|
+
if (!keys.has(key))
|
|
181
|
+
issues.push({ severity: 'warning', reason: 'unknown-key', key });
|
|
182
|
+
}
|
|
183
|
+
return issues;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A plain copy of `template` filled from `record`, or from the stored values
|
|
187
|
+
* when no record is passed.
|
|
188
|
+
*/
|
|
189
|
+
export function resolveVariables(template, record, options = {}) {
|
|
190
|
+
const design = JSON.parse(JSON.stringify(template));
|
|
191
|
+
const issues = fillVariables(design, record);
|
|
192
|
+
const first = issues.find((i) => i.severity === 'error');
|
|
193
|
+
if (options.strict &&
|
|
194
|
+
first &&
|
|
195
|
+
(first.reason === 'missing-required' || first.reason === 'type-mismatch')) {
|
|
196
|
+
throw polotnoError('VARIABLES_INVALID', 'Invalid template record: ' + first.reason + ' (' + first.variable + ')', { reason: first.reason, issues });
|
|
197
|
+
}
|
|
198
|
+
return { design, issues };
|
|
199
|
+
}
|
|
200
|
+
const DRAWN = ['text', 'placeholder', 'src', 'fill', 'visible'];
|
|
201
|
+
// One shared result for an element nothing fills: an editor view returning it
|
|
202
|
+
// again notifies no observer, so a registry edit leaves those elements alone.
|
|
203
|
+
const UNFILLED = Object.freeze({});
|
|
204
|
+
/**
|
|
205
|
+
* The properties an element draws with the stored values in `variables`, for
|
|
206
|
+
* an editor that shows filled values while keeping the template. Only the
|
|
207
|
+
* properties that change are returned.
|
|
208
|
+
*/
|
|
209
|
+
export function fillElement(node, variables) {
|
|
210
|
+
if (!node.bindings && !node.text?.includes('{{'))
|
|
211
|
+
return UNFILLED;
|
|
212
|
+
const read = reader(variables);
|
|
213
|
+
const filled = { ...node };
|
|
214
|
+
uses(filled, node.type, (name, property) => typed(read(name), property), true);
|
|
215
|
+
const changed = {};
|
|
216
|
+
for (const key of DRAWN)
|
|
217
|
+
if (filled[key] !== node[key])
|
|
218
|
+
Object.assign(changed, { [key]: filled[key] });
|
|
219
|
+
return changed;
|
|
220
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polotno/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Canonical Polotno design format: types, normalize, validate, JSON Schema. Authored in zod, DOM-free.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"type": "module",
|
|
@@ -23,7 +23,15 @@
|
|
|
23
23
|
"./json": {
|
|
24
24
|
"import": "./lib/design.schema.json"
|
|
25
25
|
},
|
|
26
|
-
"./design.schema.json": "./lib/design.schema.json"
|
|
26
|
+
"./design.schema.json": "./lib/design.schema.json",
|
|
27
|
+
"./variables": {
|
|
28
|
+
"import": "./lib/variables.js",
|
|
29
|
+
"types": "./lib/variables.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"./text": {
|
|
32
|
+
"import": "./lib/text.js",
|
|
33
|
+
"types": "./lib/text.d.ts"
|
|
34
|
+
}
|
|
27
35
|
},
|
|
28
36
|
"author": "Anton Lavrenov",
|
|
29
37
|
"homepage": "https://polotno.com",
|
|
@@ -50,7 +58,7 @@
|
|
|
50
58
|
},
|
|
51
59
|
"devDependencies": {
|
|
52
60
|
"typescript": "~7.0.2",
|
|
53
|
-
"vitest": "^5.1.
|
|
61
|
+
"vitest": "^5.1.1"
|
|
54
62
|
},
|
|
55
63
|
"scripts": {
|
|
56
64
|
"build": "rm -rf lib && tsc && node scripts/generate-schema.mjs && node ../../scripts/check-pack.mjs",
|