line-flex-message-preview 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +254 -2
- package/dist/define.d.ts +1 -0
- package/dist/define.js +4 -0
- package/dist/dom.d.ts +15 -0
- package/dist/dom.js +38 -0
- package/dist/element.d.ts +44 -0
- package/dist/element.js +174 -0
- package/dist/flex-message.bundle.js +132 -0
- package/dist/html.d.ts +30 -0
- package/dist/html.js +86 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +7 -0
- package/dist/profile.d.ts +75 -0
- package/dist/profile.js +86 -0
- package/dist/styles.d.ts +4 -0
- package/dist/styles.js +152 -0
- package/dist/tree.d.ts +29 -0
- package/dist/tree.js +503 -0
- package/dist/types.d.ts +174 -0
- package/dist/types.js +9 -0
- package/dist/validate.d.ts +46 -0
- package/dist/validate.js +473 -0
- package/dist/values.d.ts +24 -0
- package/dist/values.js +78 -0
- package/docs/measurements.md +119 -0
- package/package.json +62 -4
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Profile } from './profile.js';
|
|
2
|
+
import type { FlexAction } from './types.js';
|
|
3
|
+
export interface VNode {
|
|
4
|
+
tag: string;
|
|
5
|
+
classes: string[];
|
|
6
|
+
/** CSS declarations in insertion order. Values are always generated, never raw input. */
|
|
7
|
+
style: Map<string, string>;
|
|
8
|
+
attrs: Record<string, string>;
|
|
9
|
+
children: (VNode | string)[];
|
|
10
|
+
/** The action a tap on this element would trigger, and where it is in the JSON. */
|
|
11
|
+
action?: {
|
|
12
|
+
action: FlexAction;
|
|
13
|
+
path: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface TreeOptions {
|
|
17
|
+
profile: Profile;
|
|
18
|
+
/** Adds `data-path` (a JSON Pointer into the container) to every component. */
|
|
19
|
+
annotate?: boolean;
|
|
20
|
+
}
|
|
21
|
+
type Json = Record<string, unknown>;
|
|
22
|
+
/** Accepts a Flex message (`{ type: "flex", contents }`) or a bare container. */
|
|
23
|
+
export declare function containerOf(input: unknown): Json | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Builds the element tree for a message. Returns an empty root when the input
|
|
26
|
+
* is not a bubble or a carousel.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildTree(input: unknown, options: TreeOptions): VNode;
|
|
29
|
+
export {};
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// Flex JSON → a small element tree.
|
|
2
|
+
//
|
|
3
|
+
// Both outputs (live DOM and HTML string) are produced from this one tree, so
|
|
4
|
+
// they cannot drift apart. The builder is tolerant: a value it cannot parse is
|
|
5
|
+
// dropped and the default applies, and it never throws on malformed input.
|
|
6
|
+
// Use validate() to find out what was wrong.
|
|
7
|
+
import { cssLength, isNonNegativeInteger, isObject, lookup, parseAngle, parseAspectRatio, parseColor, parseHttpsUrl, parseLength, } from './values.js';
|
|
8
|
+
const ZWSP = '\u200b';
|
|
9
|
+
function h(tag, classes, children = []) {
|
|
10
|
+
return { tag, classes, style: new Map(), attrs: {}, children };
|
|
11
|
+
}
|
|
12
|
+
function px(n) {
|
|
13
|
+
return `${n}px`;
|
|
14
|
+
}
|
|
15
|
+
/** A spacing keyword or a px / % length → CSS length. */
|
|
16
|
+
function spacingValue(value, profile, percent = false) {
|
|
17
|
+
const keyword = lookup(profile.spacing, value);
|
|
18
|
+
if (keyword !== undefined)
|
|
19
|
+
return px(keyword);
|
|
20
|
+
const length = parseLength(value, { percent });
|
|
21
|
+
return length && cssLength(length);
|
|
22
|
+
}
|
|
23
|
+
function fontSizeValue(value, profile) {
|
|
24
|
+
const keyword = lookup(profile.textSize, value);
|
|
25
|
+
if (keyword !== undefined)
|
|
26
|
+
return px(keyword);
|
|
27
|
+
const length = parseLength(value);
|
|
28
|
+
return length && length.n >= 0 ? cssLength(length) : undefined;
|
|
29
|
+
}
|
|
30
|
+
function isAbsolute(node) {
|
|
31
|
+
return node.position === 'absolute';
|
|
32
|
+
}
|
|
33
|
+
function setAction(v, node, path) {
|
|
34
|
+
if (isObject(node.action) && typeof node.action.type === 'string') {
|
|
35
|
+
v.action = { action: node.action, path };
|
|
36
|
+
v.classes.push('act');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** margin, flex, position, offsets and gravity: what most components share. */
|
|
40
|
+
function applyCommon(v, node, parent, ctx, opts = {}) {
|
|
41
|
+
const { profile } = ctx;
|
|
42
|
+
const absolute = isAbsolute(node);
|
|
43
|
+
const vertical = parent.layout === 'vertical';
|
|
44
|
+
// Space before this component: its own margin, else the parent's spacing.
|
|
45
|
+
if (!absolute) {
|
|
46
|
+
const own = node.margin === undefined ? undefined : spacingValue(node.margin, profile);
|
|
47
|
+
// Keyword spacing skips fillers, spacers and whatever follows a spacer; px
|
|
48
|
+
// spacing does not. Both as the Simulator's stylesheet has it.
|
|
49
|
+
const gapless = node.type === 'filler' || node.type === 'spacer' || parent.previous === 'spacer';
|
|
50
|
+
const skip = parent.index === 0 || (gapless && parent.spacingKeyword === true);
|
|
51
|
+
const inherited = skip ? undefined : parent.spacing;
|
|
52
|
+
const margin = own ?? (node.margin === undefined ? inherited : undefined);
|
|
53
|
+
if (margin !== undefined)
|
|
54
|
+
v.style.set(vertical ? 'margin-block-start' : 'margin-inline-start', margin);
|
|
55
|
+
}
|
|
56
|
+
if (parent.block) {
|
|
57
|
+
v.style.set('flex', '1 0 auto');
|
|
58
|
+
}
|
|
59
|
+
else if (opts.flex === 'none') {
|
|
60
|
+
v.style.set('flex', 'none');
|
|
61
|
+
}
|
|
62
|
+
else if (isNonNegativeInteger(node.flex)) {
|
|
63
|
+
if (node.flex === 0)
|
|
64
|
+
v.style.set('flex', 'none');
|
|
65
|
+
else
|
|
66
|
+
v.style.set('flex', vertical ? `${node.flex} 0 auto` : `${node.flex} 0 0px`);
|
|
67
|
+
}
|
|
68
|
+
if (absolute)
|
|
69
|
+
v.classes.push('abs');
|
|
70
|
+
const offsets = [
|
|
71
|
+
['offsetTop', 'top'],
|
|
72
|
+
['offsetBottom', 'bottom'],
|
|
73
|
+
['offsetStart', 'inset-inline-start'],
|
|
74
|
+
['offsetEnd', 'inset-inline-end'],
|
|
75
|
+
];
|
|
76
|
+
for (const [key, property] of offsets) {
|
|
77
|
+
const value = spacingValue(node[key], profile, true);
|
|
78
|
+
if (value !== undefined)
|
|
79
|
+
v.style.set(property, value);
|
|
80
|
+
}
|
|
81
|
+
if (node.gravity === 'center')
|
|
82
|
+
v.style.set('justify-content', 'center');
|
|
83
|
+
if (node.gravity === 'bottom')
|
|
84
|
+
v.style.set('justify-content', 'flex-end');
|
|
85
|
+
}
|
|
86
|
+
function applyTextStyle(v, node, profile) {
|
|
87
|
+
const size = fontSizeValue(node.size, profile);
|
|
88
|
+
if (size !== undefined)
|
|
89
|
+
v.style.set('font-size', size);
|
|
90
|
+
if (node.weight === 'bold')
|
|
91
|
+
v.style.set('font-weight', '700');
|
|
92
|
+
if (node.weight === 'regular')
|
|
93
|
+
v.style.set('font-weight', '400');
|
|
94
|
+
if (node.style === 'italic')
|
|
95
|
+
v.style.set('font-style', 'italic');
|
|
96
|
+
if (node.style === 'normal')
|
|
97
|
+
v.style.set('font-style', 'normal');
|
|
98
|
+
if (node.decoration === 'underline')
|
|
99
|
+
v.style.set('text-decoration', 'underline');
|
|
100
|
+
if (node.decoration === 'line-through')
|
|
101
|
+
v.style.set('text-decoration', 'line-through');
|
|
102
|
+
if (node.decoration === 'none')
|
|
103
|
+
v.style.set('text-decoration', 'none');
|
|
104
|
+
const color = parseColor(node.color);
|
|
105
|
+
if (color)
|
|
106
|
+
v.style.set('color', color);
|
|
107
|
+
}
|
|
108
|
+
const JUSTIFY = new Set(['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly']);
|
|
109
|
+
const ALIGN = new Set(['flex-start', 'center', 'flex-end']);
|
|
110
|
+
function box(node, parent, path, ctx) {
|
|
111
|
+
const { profile } = ctx;
|
|
112
|
+
const layout = node.layout === 'horizontal' || node.layout === 'baseline' ? node.layout : 'vertical';
|
|
113
|
+
const v = h('div', ['c', 'box', layout === 'vertical' ? 'v' : 'h']);
|
|
114
|
+
if (layout === 'baseline')
|
|
115
|
+
v.classes.push('bl');
|
|
116
|
+
const width = parseLength(node.width, { percent: true });
|
|
117
|
+
const height = parseLength(node.height, { percent: true });
|
|
118
|
+
// A fixed size along the parent's main axis takes the box out of flex sizing.
|
|
119
|
+
const fixedMain = !parent.block && (parent.layout === 'vertical' ? height !== undefined : width !== undefined);
|
|
120
|
+
applyCommon(v, node, parent, ctx, fixedMain ? { flex: 'none' } : {});
|
|
121
|
+
setAction(v, node, path);
|
|
122
|
+
const absolute = isAbsolute(node);
|
|
123
|
+
// A row fills its parent's width even when the parent does not stretch it.
|
|
124
|
+
if (layout !== 'vertical' && parent.layout === 'vertical' && !absolute && !ctx.inAbsolute && !width) {
|
|
125
|
+
v.classes.push('fill');
|
|
126
|
+
}
|
|
127
|
+
// Padding: the block's default, then paddingAll, then the individual sides.
|
|
128
|
+
const sides = parent.padding ? parent.padding.map(px) : [undefined, undefined, undefined, undefined];
|
|
129
|
+
const all = spacingValue(node.paddingAll, profile, true);
|
|
130
|
+
if (all !== undefined)
|
|
131
|
+
sides.fill(all);
|
|
132
|
+
const sideKeys = ['paddingTop', 'paddingEnd', 'paddingBottom', 'paddingStart'];
|
|
133
|
+
const sideProperties = ['padding-block-start', 'padding-inline-end', 'padding-block-end', 'padding-inline-start'];
|
|
134
|
+
sideKeys.forEach((key, i) => {
|
|
135
|
+
const value = spacingValue(node[key], profile, true);
|
|
136
|
+
if (value !== undefined)
|
|
137
|
+
sides[i] = value;
|
|
138
|
+
});
|
|
139
|
+
sides.forEach((value, i) => {
|
|
140
|
+
if (value !== undefined)
|
|
141
|
+
v.style.set(sideProperties[i], value);
|
|
142
|
+
});
|
|
143
|
+
const backgroundColor = parseColor(node.backgroundColor);
|
|
144
|
+
if (backgroundColor)
|
|
145
|
+
v.style.set('background-color', backgroundColor);
|
|
146
|
+
const gradient = linearGradient(node.background);
|
|
147
|
+
if (gradient)
|
|
148
|
+
v.style.set('background-image', gradient);
|
|
149
|
+
const borderColor = parseColor(node.borderColor);
|
|
150
|
+
if (borderColor)
|
|
151
|
+
v.style.set('border-color', borderColor);
|
|
152
|
+
const borderKeyword = lookup(profile.borderWidth, node.borderWidth);
|
|
153
|
+
const borderWidth = borderKeyword !== undefined ? px(borderKeyword) : spacingLength(node.borderWidth);
|
|
154
|
+
if (borderWidth !== undefined)
|
|
155
|
+
v.style.set('border-width', borderWidth);
|
|
156
|
+
const radiusKeyword = lookup(profile.cornerRadius, node.cornerRadius);
|
|
157
|
+
const radius = radiusKeyword !== undefined ? px(radiusKeyword) : spacingLength(node.cornerRadius);
|
|
158
|
+
if (radius !== undefined)
|
|
159
|
+
v.style.set('border-radius', radius);
|
|
160
|
+
if (width)
|
|
161
|
+
v.style.set('width', cssLength(width));
|
|
162
|
+
if (height)
|
|
163
|
+
v.style.set('height', cssLength(height));
|
|
164
|
+
const maxWidth = parseLength(node.maxWidth, { percent: true });
|
|
165
|
+
if (maxWidth)
|
|
166
|
+
v.style.set('max-width', cssLength(maxWidth));
|
|
167
|
+
const maxHeight = parseLength(node.maxHeight, { percent: true });
|
|
168
|
+
if (maxHeight)
|
|
169
|
+
v.style.set('max-height', cssLength(maxHeight));
|
|
170
|
+
if (typeof node.justifyContent === 'string' && JUSTIFY.has(node.justifyContent)) {
|
|
171
|
+
v.style.set('justify-content', node.justifyContent);
|
|
172
|
+
}
|
|
173
|
+
if (typeof node.alignItems === 'string' && ALIGN.has(node.alignItems)) {
|
|
174
|
+
v.style.set('align-items', node.alignItems);
|
|
175
|
+
}
|
|
176
|
+
const spacingKeyword = lookup(profile.spacing, node.spacing);
|
|
177
|
+
const spacing = spacingKeyword !== undefined ? (spacingKeyword > 0 ? px(spacingKeyword) : undefined) : spacingLength(node.spacing);
|
|
178
|
+
const childCtx = absolute && !ctx.inAbsolute ? { ...ctx, inAbsolute: true } : ctx;
|
|
179
|
+
const contents = Array.isArray(node.contents) ? node.contents : [];
|
|
180
|
+
contents.forEach((child, index) => {
|
|
181
|
+
const previous = index > 0 && isObject(contents[index - 1]) ? contents[index - 1].type : undefined;
|
|
182
|
+
const childParent = { layout, block: false, spacing, spacingKeyword: spacingKeyword !== undefined, index, previous };
|
|
183
|
+
const rendered = component(child, childParent, `${path}/contents/${index}`, childCtx);
|
|
184
|
+
if (rendered)
|
|
185
|
+
v.children.push(rendered);
|
|
186
|
+
});
|
|
187
|
+
return v;
|
|
188
|
+
}
|
|
189
|
+
/** A non-negative px length. */
|
|
190
|
+
function spacingLength(value) {
|
|
191
|
+
const length = parseLength(value);
|
|
192
|
+
return length && length.n >= 0 ? cssLength(length) : undefined;
|
|
193
|
+
}
|
|
194
|
+
function linearGradient(background) {
|
|
195
|
+
if (!isObject(background) || background.type !== 'linearGradient')
|
|
196
|
+
return undefined;
|
|
197
|
+
const angle = parseAngle(background.angle);
|
|
198
|
+
const start = parseColor(background.startColor);
|
|
199
|
+
const end = parseColor(background.endColor);
|
|
200
|
+
if (angle === undefined || !start || !end)
|
|
201
|
+
return undefined;
|
|
202
|
+
const stops = [`${start} 0%`];
|
|
203
|
+
if (background.centerColor !== undefined) {
|
|
204
|
+
const center = parseColor(background.centerColor);
|
|
205
|
+
const position = parseLength(background.centerPosition ?? '50%', { percent: true });
|
|
206
|
+
if (!center || !position || position.unit !== '%')
|
|
207
|
+
return undefined;
|
|
208
|
+
stops.push(`${center} ${cssLength(position)}`);
|
|
209
|
+
}
|
|
210
|
+
stops.push(`${end} 100%`);
|
|
211
|
+
return `linear-gradient(${angle}deg, ${stops.join(', ')})`;
|
|
212
|
+
}
|
|
213
|
+
function span(node, profile) {
|
|
214
|
+
if (!isObject(node) || node.type !== 'span')
|
|
215
|
+
return undefined;
|
|
216
|
+
const v = h('span', ['span'], [typeof node.text === 'string' ? node.text : '']);
|
|
217
|
+
applyTextStyle(v, node, profile);
|
|
218
|
+
return v;
|
|
219
|
+
}
|
|
220
|
+
function text(node, parent, path, ctx) {
|
|
221
|
+
const { profile } = ctx;
|
|
222
|
+
const wrap = node.wrap === true;
|
|
223
|
+
const v = h('div', ['c', 'text']);
|
|
224
|
+
if (wrap)
|
|
225
|
+
v.classes.push('wrap');
|
|
226
|
+
applyCommon(v, node, parent, ctx);
|
|
227
|
+
applyTextStyle(v, node, profile);
|
|
228
|
+
setAction(v, node, path);
|
|
229
|
+
if (node.align === 'center')
|
|
230
|
+
v.style.set('text-align', 'center');
|
|
231
|
+
if (node.align === 'end')
|
|
232
|
+
v.style.set('text-align', 'end');
|
|
233
|
+
const decorate = (p) => {
|
|
234
|
+
if (!wrap)
|
|
235
|
+
return p;
|
|
236
|
+
if (profile.maxLines && isNonNegativeInteger(node.maxLines) && node.maxLines > 0) {
|
|
237
|
+
p.classes.push('clamp');
|
|
238
|
+
p.style.set('-webkit-line-clamp', String(node.maxLines));
|
|
239
|
+
}
|
|
240
|
+
if (profile.lineSpacing) {
|
|
241
|
+
const spacing = parseLength(node.lineSpacing);
|
|
242
|
+
if (spacing && spacing.n > 0) {
|
|
243
|
+
// Extra space between lines only: every line box grows, and the half
|
|
244
|
+
// that lands above the first and below the last line is pulled back in.
|
|
245
|
+
p.style.set('--ls', cssLength(spacing));
|
|
246
|
+
p.style.set('margin-block', `${spacing.n / -2}px`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return p;
|
|
250
|
+
};
|
|
251
|
+
const spans = Array.isArray(node.contents) ? node.contents.map((s) => span(s, profile)).filter((s) => s !== undefined) : [];
|
|
252
|
+
if (spans.length > 0) {
|
|
253
|
+
// With spans present the component's own `text` is not shown.
|
|
254
|
+
v.children.push(decorate(h('p', [], spans)));
|
|
255
|
+
return v;
|
|
256
|
+
}
|
|
257
|
+
const lines = (typeof node.text === 'string' ? node.text : '').split('\n');
|
|
258
|
+
if (!wrap) {
|
|
259
|
+
// Unwrapped: each line is cut off on its own.
|
|
260
|
+
for (const line of lines)
|
|
261
|
+
v.children.push(h('p', [], line === '' ? [] : [line]));
|
|
262
|
+
return v;
|
|
263
|
+
}
|
|
264
|
+
// Wrapped: one paragraph, so that maxLines counts across explicit line breaks.
|
|
265
|
+
const children = [];
|
|
266
|
+
lines.forEach((line, i) => {
|
|
267
|
+
if (i > 0)
|
|
268
|
+
children.push(h('br', []));
|
|
269
|
+
if (line !== '')
|
|
270
|
+
children.push(line);
|
|
271
|
+
else if (i === lines.length - 1 && i > 0)
|
|
272
|
+
children.push(ZWSP);
|
|
273
|
+
});
|
|
274
|
+
v.children.push(decorate(h('p', [], children)));
|
|
275
|
+
return v;
|
|
276
|
+
}
|
|
277
|
+
function image(node, parent, path, ctx) {
|
|
278
|
+
const { profile } = ctx;
|
|
279
|
+
const v = h('div', ['c', 'image']);
|
|
280
|
+
applyCommon(v, node, parent, ctx);
|
|
281
|
+
setAction(v, node, path);
|
|
282
|
+
if (node.align === 'start')
|
|
283
|
+
v.style.set('align-items', 'flex-start');
|
|
284
|
+
if (node.align === 'end')
|
|
285
|
+
v.style.set('align-items', 'flex-end');
|
|
286
|
+
const frame = h('div', ['frame']);
|
|
287
|
+
const size = node.size ?? 'md';
|
|
288
|
+
const full = size === 'full';
|
|
289
|
+
const keyword = lookup(profile.imageSize, size);
|
|
290
|
+
const length = parseLength(size, { percent: true });
|
|
291
|
+
if (full)
|
|
292
|
+
frame.style.set('width', '100%');
|
|
293
|
+
else if (keyword !== undefined)
|
|
294
|
+
frame.style.set('width', px(keyword));
|
|
295
|
+
else if (length && length.n >= 0)
|
|
296
|
+
frame.style.set('width', cssLength(length));
|
|
297
|
+
else
|
|
298
|
+
frame.style.set('width', px(profile.imageSize.md));
|
|
299
|
+
if (full && (isAbsolute(node) || (parent.layout !== 'vertical' && node.flex === 0)))
|
|
300
|
+
v.style.set('width', '100%');
|
|
301
|
+
const [w, hgt] = parseAspectRatio(node.aspectRatio) ?? [1, 1];
|
|
302
|
+
frame.style.set('aspect-ratio', `${w} / ${hgt}`);
|
|
303
|
+
const backgroundColor = parseColor(node.backgroundColor);
|
|
304
|
+
if (backgroundColor)
|
|
305
|
+
frame.style.set('background-color', backgroundColor);
|
|
306
|
+
const url = parseHttpsUrl(node.url);
|
|
307
|
+
if (url) {
|
|
308
|
+
const img = h('img', [node.aspectMode === 'cover' ? 'cover' : 'fit']);
|
|
309
|
+
img.attrs = { src: url, alt: '', loading: 'lazy', decoding: 'async', referrerpolicy: 'no-referrer', draggable: 'false' };
|
|
310
|
+
frame.children.push(img);
|
|
311
|
+
}
|
|
312
|
+
v.children.push(frame);
|
|
313
|
+
return v;
|
|
314
|
+
}
|
|
315
|
+
function button(node, parent, path, ctx) {
|
|
316
|
+
const v = h('div', ['c', 'button']);
|
|
317
|
+
applyCommon(v, node, parent, ctx);
|
|
318
|
+
setAction(v, node, path);
|
|
319
|
+
const style = node.style === 'primary' || node.style === 'secondary' ? node.style : 'link';
|
|
320
|
+
const face = h('div', ['btn', style]);
|
|
321
|
+
if (node.height === 'sm')
|
|
322
|
+
face.classes.push('sm');
|
|
323
|
+
const color = parseColor(node.color);
|
|
324
|
+
if (color)
|
|
325
|
+
face.style.set(style === 'link' ? 'color' : 'background-color', color);
|
|
326
|
+
const label = isObject(node.action) && typeof node.action.label === 'string' ? node.action.label : '';
|
|
327
|
+
face.children.push(h('div', ['label'], label === '' ? [] : [label]));
|
|
328
|
+
v.children.push(face);
|
|
329
|
+
return v;
|
|
330
|
+
}
|
|
331
|
+
function icon(node, parent, ctx) {
|
|
332
|
+
const v = h('div', ['c', 'icon']);
|
|
333
|
+
applyCommon(v, node, parent, ctx, { flex: 'none' });
|
|
334
|
+
const size = fontSizeValue(node.size, ctx.profile);
|
|
335
|
+
if (size !== undefined)
|
|
336
|
+
v.style.set('font-size', size);
|
|
337
|
+
const [w, hgt] = parseAspectRatio(node.aspectRatio) ?? [1, 1];
|
|
338
|
+
const url = parseHttpsUrl(node.url);
|
|
339
|
+
const glyph = h(url ? 'img' : 'span', ['ico']);
|
|
340
|
+
if (url)
|
|
341
|
+
glyph.attrs = { src: url, alt: '', decoding: 'async', referrerpolicy: 'no-referrer', draggable: 'false' };
|
|
342
|
+
glyph.style.set('width', `${w / hgt}em`);
|
|
343
|
+
v.children.push(h('div', [], [glyph]));
|
|
344
|
+
return v;
|
|
345
|
+
}
|
|
346
|
+
function separator(node, parent, ctx) {
|
|
347
|
+
const v = h('div', ['c', 'sep']);
|
|
348
|
+
applyCommon(v, { type: 'separator', margin: node.margin }, parent, ctx, { flex: 'none' });
|
|
349
|
+
const color = parseColor(node.color);
|
|
350
|
+
if (color)
|
|
351
|
+
v.style.set('color', color);
|
|
352
|
+
return v;
|
|
353
|
+
}
|
|
354
|
+
function filler(node, parent, ctx) {
|
|
355
|
+
const v = h('div', ['c', 'filler']);
|
|
356
|
+
applyCommon(v, { type: 'filler', flex: node.flex }, parent, ctx);
|
|
357
|
+
return v;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* A fixed gap. LINE no longer documents this component but still accepts it;
|
|
361
|
+
* the sizes are the spacing keywords, `md` when none is given.
|
|
362
|
+
*/
|
|
363
|
+
function spacer(node, parent, ctx) {
|
|
364
|
+
const v = h('div', ['c', 'spacer']);
|
|
365
|
+
applyCommon(v, { type: 'spacer' }, parent, ctx, { flex: 'none' });
|
|
366
|
+
const size = px(lookup(ctx.profile.spacing, node.size) ?? ctx.profile.spacing.md);
|
|
367
|
+
const vertical = parent.layout === 'vertical';
|
|
368
|
+
v.style.set('width', vertical ? '1px' : size);
|
|
369
|
+
v.style.set('height', vertical ? size : '1px');
|
|
370
|
+
return v;
|
|
371
|
+
}
|
|
372
|
+
function component(node, parent, path, ctx) {
|
|
373
|
+
if (!isObject(node))
|
|
374
|
+
return undefined;
|
|
375
|
+
let v;
|
|
376
|
+
switch (node.type) {
|
|
377
|
+
case 'box':
|
|
378
|
+
v = box(node, parent, path, ctx);
|
|
379
|
+
break;
|
|
380
|
+
case 'text':
|
|
381
|
+
v = text(node, parent, path, ctx);
|
|
382
|
+
break;
|
|
383
|
+
case 'image':
|
|
384
|
+
v = image(node, parent, path, ctx);
|
|
385
|
+
break;
|
|
386
|
+
case 'button':
|
|
387
|
+
v = button(node, parent, path, ctx);
|
|
388
|
+
break;
|
|
389
|
+
case 'icon':
|
|
390
|
+
v = icon(node, parent, ctx);
|
|
391
|
+
break;
|
|
392
|
+
case 'separator':
|
|
393
|
+
v = separator(node, parent, ctx);
|
|
394
|
+
break;
|
|
395
|
+
case 'filler':
|
|
396
|
+
v = filler(node, parent, ctx);
|
|
397
|
+
break;
|
|
398
|
+
case 'spacer':
|
|
399
|
+
v = spacer(node, parent, ctx);
|
|
400
|
+
break;
|
|
401
|
+
case 'video':
|
|
402
|
+
// No playback in a preview: the alternative content stands in for the
|
|
403
|
+
// video, and is addressed by the video's own path.
|
|
404
|
+
v = component(node.altContent, parent, path, ctx);
|
|
405
|
+
if (!v)
|
|
406
|
+
return undefined;
|
|
407
|
+
setAction(v, node, path);
|
|
408
|
+
break;
|
|
409
|
+
default:
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
if (ctx.annotate)
|
|
413
|
+
v.attrs['data-path'] = path;
|
|
414
|
+
return v;
|
|
415
|
+
}
|
|
416
|
+
const BLOCKS = ['header', 'hero', 'body', 'footer'];
|
|
417
|
+
function bubble(node, path, ctx) {
|
|
418
|
+
const { profile } = ctx;
|
|
419
|
+
const metrics = lookup(profile.bubble, node.size) ?? profile.bubble.mega;
|
|
420
|
+
const rtl = node.direction === 'rtl';
|
|
421
|
+
const styles = isObject(node.styles) ? node.styles : {};
|
|
422
|
+
const card = h('div', ['card']);
|
|
423
|
+
card.attrs.dir = rtl ? 'rtl' : 'ltr';
|
|
424
|
+
card.style.set('border-radius', px(metrics.cornerRadius));
|
|
425
|
+
if (metrics.inset > 0)
|
|
426
|
+
card.style.set('margin-inline', px(metrics.inset));
|
|
427
|
+
setAction(card, node, path);
|
|
428
|
+
const hasFooter = isObject(node.footer);
|
|
429
|
+
for (const name of BLOCKS) {
|
|
430
|
+
const root = node[name];
|
|
431
|
+
if (!isObject(root))
|
|
432
|
+
continue;
|
|
433
|
+
const style = isObject(styles[name]) ? styles[name] : {};
|
|
434
|
+
if (style.separator === true && card.children.length > 0) {
|
|
435
|
+
const line = h('div', ['sep']);
|
|
436
|
+
const color = parseColor(style.separatorColor);
|
|
437
|
+
if (color)
|
|
438
|
+
line.style.set('color', color);
|
|
439
|
+
card.children.push(line);
|
|
440
|
+
}
|
|
441
|
+
const block = h('div', ['block', name]);
|
|
442
|
+
const background = parseColor(style.backgroundColor);
|
|
443
|
+
if (background)
|
|
444
|
+
block.style.set('background-color', background);
|
|
445
|
+
let padding;
|
|
446
|
+
if (name === 'header')
|
|
447
|
+
padding = metrics.headerPadding;
|
|
448
|
+
if (name === 'footer')
|
|
449
|
+
padding = metrics.footerPadding;
|
|
450
|
+
if (name === 'body') {
|
|
451
|
+
const [top, end, bottom, start] = metrics.bodyPadding;
|
|
452
|
+
padding = [top, end, hasFooter ? metrics.bodyPaddingBottomWithFooter : bottom, start];
|
|
453
|
+
}
|
|
454
|
+
const rendered = component(root, { layout: 'vertical', block: true, index: 0, padding }, `${path}/${name}`, ctx);
|
|
455
|
+
if (rendered)
|
|
456
|
+
block.children.push(rendered);
|
|
457
|
+
card.children.push(block);
|
|
458
|
+
}
|
|
459
|
+
const v = h('div', ['bubble'], [card]);
|
|
460
|
+
v.style.set('max-width', px(metrics.maxWidth));
|
|
461
|
+
if (ctx.annotate)
|
|
462
|
+
v.attrs['data-path'] = path;
|
|
463
|
+
return v;
|
|
464
|
+
}
|
|
465
|
+
function carousel(node, ctx) {
|
|
466
|
+
const { carousel: metrics } = ctx.profile;
|
|
467
|
+
const track = h('div', ['track']);
|
|
468
|
+
const contents = Array.isArray(node.contents) ? node.contents : [];
|
|
469
|
+
contents.forEach((item, index) => {
|
|
470
|
+
if (!isObject(item) || item.type !== 'bubble')
|
|
471
|
+
return;
|
|
472
|
+
const v = bubble(item, `/contents/${index}`, ctx);
|
|
473
|
+
v.style.set('width', `${metrics.itemWidthPercent}%`);
|
|
474
|
+
v.style.set('margin-inline-start', px(track.children.length === 0 ? metrics.leadingGap : metrics.gap));
|
|
475
|
+
track.children.push(v);
|
|
476
|
+
});
|
|
477
|
+
const tail = h('div', ['tail']);
|
|
478
|
+
tail.style.set('width', px(metrics.trailingGap));
|
|
479
|
+
track.children.push(tail);
|
|
480
|
+
return h('div', ['carousel'], [track]);
|
|
481
|
+
}
|
|
482
|
+
/** Accepts a Flex message (`{ type: "flex", contents }`) or a bare container. */
|
|
483
|
+
export function containerOf(input) {
|
|
484
|
+
if (!isObject(input))
|
|
485
|
+
return undefined;
|
|
486
|
+
if (input.type === 'flex')
|
|
487
|
+
return isObject(input.contents) ? input.contents : undefined;
|
|
488
|
+
return input;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Builds the element tree for a message. Returns an empty root when the input
|
|
492
|
+
* is not a bubble or a carousel.
|
|
493
|
+
*/
|
|
494
|
+
export function buildTree(input, options) {
|
|
495
|
+
const ctx = { profile: options.profile, annotate: options.annotate === true, inAbsolute: false };
|
|
496
|
+
const root = h('div', ['root']);
|
|
497
|
+
const container = containerOf(input);
|
|
498
|
+
if (container?.type === 'bubble')
|
|
499
|
+
root.children.push(bubble(container, '', ctx));
|
|
500
|
+
else if (container?.type === 'carousel')
|
|
501
|
+
root.children.push(carousel(container, ctx));
|
|
502
|
+
return root;
|
|
503
|
+
}
|