@wirelith/core 0.0.0 → 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/NOTICE +48 -0
- package/README.md +49 -1
- package/dist/ai-spec.d.ts +5 -0
- package/dist/ai-spec.js +7 -0
- package/dist/ai-spec.js.map +1 -0
- package/dist/api.d.ts +54 -0
- package/dist/api.js +236 -0
- package/dist/api.js.map +1 -0
- package/dist/components.d.ts +43 -0
- package/dist/components.js +159 -0
- package/dist/components.js.map +1 -0
- package/dist/data/ai-spec.d.ts +2 -0
- package/dist/data/ai-spec.js +5 -0
- package/dist/data/ai-spec.js.map +1 -0
- package/dist/data/fonts.d.ts +2 -0
- package/dist/data/fonts.js +5 -0
- package/dist/data/fonts.js.map +1 -0
- package/dist/data/icons.d.ts +4 -0
- package/dist/data/icons.js +7 -0
- package/dist/data/icons.js.map +1 -0
- package/dist/format.d.ts +7 -0
- package/dist/format.js +189 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +156 -0
- package/dist/layout.js +1118 -0
- package/dist/layout.js.map +1 -0
- package/dist/measure.d.ts +27 -0
- package/dist/measure.js +105 -0
- package/dist/measure.js.map +1 -0
- package/dist/overview.d.ts +37 -0
- package/dist/overview.js +523 -0
- package/dist/overview.js.map +1 -0
- package/dist/parse.d.ts +57 -0
- package/dist/parse.js +609 -0
- package/dist/parse.js.map +1 -0
- package/dist/render.d.ts +12 -0
- package/dist/render.js +980 -0
- package/dist/render.js.map +1 -0
- package/dist/svg.d.ts +18 -0
- package/dist/svg.js +97 -0
- package/dist/svg.js.map +1 -0
- package/dist/theme.d.ts +42 -0
- package/dist/theme.js +220 -0
- package/dist/theme.js.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +35 -0
- package/dist/validate.js +450 -0
- package/dist/validate.js.map +1 -0
- package/package.json +38 -5
package/dist/render.js
ADDED
|
@@ -0,0 +1,980 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// The renderer (spec section 12 and Appendix C): draws the overview as one SVG document.
|
|
3
|
+
import { ICON_DRAWINGS, ICON_NAMES } from './data/icons.js';
|
|
4
|
+
import { CARD_TITLE } from './layout.js';
|
|
5
|
+
import { baseline, measure, r, truncate } from './measure.js';
|
|
6
|
+
import { SPEC_VERSION } from './parse.js';
|
|
7
|
+
import { el, fmt4, join } from './svg.js';
|
|
8
|
+
import { COLOR_TOKENS } from './theme.js';
|
|
9
|
+
import { textOf } from './validate.js';
|
|
10
|
+
const FAMILY = {
|
|
11
|
+
sans: 'Inter, sans-serif',
|
|
12
|
+
serif: "'Source Serif 4', serif",
|
|
13
|
+
mono: "'JetBrains Mono', monospace",
|
|
14
|
+
};
|
|
15
|
+
const has = (n, f) => n.flags?.includes(f) ?? false;
|
|
16
|
+
class Painter {
|
|
17
|
+
input;
|
|
18
|
+
ids;
|
|
19
|
+
t;
|
|
20
|
+
L;
|
|
21
|
+
u;
|
|
22
|
+
S;
|
|
23
|
+
R;
|
|
24
|
+
defs = [];
|
|
25
|
+
/** The id prefix for elements in the frame being drawn. */
|
|
26
|
+
scope = '';
|
|
27
|
+
constructor(input, ids) {
|
|
28
|
+
this.input = input;
|
|
29
|
+
this.ids = ids;
|
|
30
|
+
this.t = input.theme;
|
|
31
|
+
this.L = input.overview.layout;
|
|
32
|
+
this.u = this.L.u;
|
|
33
|
+
this.S = this.t.stroke;
|
|
34
|
+
this.R = this.t.radius;
|
|
35
|
+
}
|
|
36
|
+
get c() {
|
|
37
|
+
return this.t.color;
|
|
38
|
+
}
|
|
39
|
+
// ---- primitives ---------------------------------------------------------------------------
|
|
40
|
+
/** One line of text whose line box starts at `top`. */
|
|
41
|
+
text(s, x, top, f, lh, fill) {
|
|
42
|
+
return el('text', {
|
|
43
|
+
x,
|
|
44
|
+
y: r(top + baseline(f, lh)),
|
|
45
|
+
'font-family': FAMILY[f.font],
|
|
46
|
+
'font-size': f.size,
|
|
47
|
+
'font-weight': f.weight,
|
|
48
|
+
fill,
|
|
49
|
+
}, s);
|
|
50
|
+
}
|
|
51
|
+
/** Text centred in a box, truncated to fit. */
|
|
52
|
+
centred(s, box, f, lh, fill, pad = 0) {
|
|
53
|
+
const shown = truncate(s, f, box.w - 2 * pad);
|
|
54
|
+
if (shown === '')
|
|
55
|
+
return undefined;
|
|
56
|
+
const w = measure(shown, f);
|
|
57
|
+
return this.text(shown, r(box.x + (box.w - w) / 2), r(box.y + (box.h - lh) / 2), f, lh, fill);
|
|
58
|
+
}
|
|
59
|
+
outlined(b, fill, stroke = this.c.border) {
|
|
60
|
+
return el('rect', {
|
|
61
|
+
x: b.x,
|
|
62
|
+
y: b.y,
|
|
63
|
+
width: b.w,
|
|
64
|
+
height: b.h,
|
|
65
|
+
rx: this.R,
|
|
66
|
+
fill,
|
|
67
|
+
stroke,
|
|
68
|
+
'stroke-width': this.S,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
bar(x, y, w) {
|
|
72
|
+
return el('rect', {
|
|
73
|
+
x,
|
|
74
|
+
y,
|
|
75
|
+
width: Math.max(0, w),
|
|
76
|
+
height: 8,
|
|
77
|
+
rx: 4,
|
|
78
|
+
fill: this.c.border,
|
|
79
|
+
'fill-opacity': '0.35',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
hline(x1, x2, y, stroke = this.c.border) {
|
|
83
|
+
return el('line', { x1, y1: y, x2, y2: y, stroke, 'stroke-width': this.S });
|
|
84
|
+
}
|
|
85
|
+
vline(x, y1, y2, stroke = this.c.border) {
|
|
86
|
+
return el('line', { x1: x, y1, x2: x, y2, stroke, 'stroke-width': this.S });
|
|
87
|
+
}
|
|
88
|
+
/** An icon at (x, y), `size` pixels square (Appendix C.1). */
|
|
89
|
+
icon(name, x, y, size, color) {
|
|
90
|
+
const k = size / 24;
|
|
91
|
+
const drawing = Object.hasOwn(ICON_NAMES, name) ? ICON_DRAWINGS[ICON_NAMES[name]] : undefined;
|
|
92
|
+
const parts = [];
|
|
93
|
+
if (!drawing) {
|
|
94
|
+
parts.push(el('rect', { x: 4, y: 4, width: 16, height: 16 }));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
for (const [tag, a] of drawing) {
|
|
98
|
+
const attrs = {};
|
|
99
|
+
for (const [key, v] of Object.entries(a))
|
|
100
|
+
attrs[key] = key === 'fill' && v === 'currentColor' ? color : v;
|
|
101
|
+
parts.push(el(tag, attrs));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return el('g', {
|
|
105
|
+
transform: `translate(${num(x)} ${num(y)}) scale(${fmt4(k)})`,
|
|
106
|
+
fill: 'none',
|
|
107
|
+
stroke: color,
|
|
108
|
+
'stroke-width': fmt4(this.S / k),
|
|
109
|
+
'stroke-linecap': 'round',
|
|
110
|
+
'stroke-linejoin': 'round',
|
|
111
|
+
'aria-hidden': 'true',
|
|
112
|
+
}, ...parts);
|
|
113
|
+
}
|
|
114
|
+
/** A group for an element: its id and classes. */
|
|
115
|
+
group(n, parts, titleText) {
|
|
116
|
+
const cls = [`wl-${n.type}`, ...(n.flags ?? []).map((f) => `wl-${f}`)].join(' ');
|
|
117
|
+
return el('g', { id: this.ids(n), class: cls }, titleText !== undefined ? el('title', {}, titleText) : undefined, ...parts);
|
|
118
|
+
}
|
|
119
|
+
// ---- components ---------------------------------------------------------------------------
|
|
120
|
+
draw(l) {
|
|
121
|
+
const n = l.node;
|
|
122
|
+
const b = l.box;
|
|
123
|
+
const u = this.u;
|
|
124
|
+
const c = this.c;
|
|
125
|
+
const L = this.L;
|
|
126
|
+
const label = n.label ?? '';
|
|
127
|
+
const kids = () => l.children.map((k) => this.draw(k));
|
|
128
|
+
switch (n.type) {
|
|
129
|
+
case 'h1':
|
|
130
|
+
case 'h2':
|
|
131
|
+
case 'h3':
|
|
132
|
+
case 'text':
|
|
133
|
+
case 'link': {
|
|
134
|
+
const { f, lh } = L.textStyle(n.type);
|
|
135
|
+
if (n.type === 'text' && n.label === undefined) {
|
|
136
|
+
const count = Math.round(b.h / lh);
|
|
137
|
+
const bars = [];
|
|
138
|
+
for (let i = 0; i < count; i++) {
|
|
139
|
+
bars.push(this.bar(b.x, r(b.y + i * lh + (lh - 8) / 2), i === count - 1 && count > 1 ? r(b.w * 0.6) : b.w));
|
|
140
|
+
}
|
|
141
|
+
return this.group(n, [el('g', { 'aria-hidden': 'true' }, ...bars)]);
|
|
142
|
+
}
|
|
143
|
+
const disabled = has(n, 'disabled');
|
|
144
|
+
const fill = n.type === 'link'
|
|
145
|
+
? disabled
|
|
146
|
+
? c.muted
|
|
147
|
+
: c.primary
|
|
148
|
+
: n.type === 'text' && has(n, 'muted')
|
|
149
|
+
? c.muted
|
|
150
|
+
: c.text;
|
|
151
|
+
const parts = [];
|
|
152
|
+
(l.lines ?? []).forEach((line, i) => {
|
|
153
|
+
const w = measure(line, f);
|
|
154
|
+
const x = this.alignX(n, b, w);
|
|
155
|
+
const top = r(b.y + i * lh);
|
|
156
|
+
parts.push(this.text(line, x, top, f, lh, fill));
|
|
157
|
+
if (n.type === 'link' && !disabled && line !== '') {
|
|
158
|
+
const y = r(top + baseline(f, lh) + 2);
|
|
159
|
+
parts.push(el('line', { x1: x, y1: y, x2: r(x + w), y2: y, stroke: c.primary, 'stroke-width': 1 }));
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return this.group(n, parts);
|
|
163
|
+
}
|
|
164
|
+
case 'label': {
|
|
165
|
+
const f = L.body(13, 500);
|
|
166
|
+
const shown = truncate(label, f, b.w);
|
|
167
|
+
return this.group(n, [
|
|
168
|
+
shown ? this.text(shown, b.x, b.y, f, 18, has(n, 'muted') ? c.muted : c.text) : undefined,
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
case 'button':
|
|
172
|
+
return this.group(n, this.button(n, b));
|
|
173
|
+
case 'input':
|
|
174
|
+
case 'select':
|
|
175
|
+
case 'textarea':
|
|
176
|
+
return this.group(n, this.field(n, b));
|
|
177
|
+
case 'checkbox':
|
|
178
|
+
case 'toggle':
|
|
179
|
+
return this.group(n, this.check(n, b));
|
|
180
|
+
case 'radio':
|
|
181
|
+
return this.group(n, this.radio(n, b));
|
|
182
|
+
case 'icon': {
|
|
183
|
+
const s = L.iconSize(n);
|
|
184
|
+
return this.group(n, [this.icon(label, b.x, b.y, s, c.text)], label);
|
|
185
|
+
}
|
|
186
|
+
case 'avatar': {
|
|
187
|
+
const s = L.avatarSize(n);
|
|
188
|
+
const words = label.split(' ').filter((w) => w !== '');
|
|
189
|
+
const firstChar = (w) => (w ? (Array.from(w)[0] ?? '') : '');
|
|
190
|
+
const initials = (firstChar(words[0]) + (words.length > 1 ? firstChar(words[words.length - 1]) : '')).replace(/[a-z]/g, (ch) => ch.toUpperCase());
|
|
191
|
+
const f = L.body(s === 24 ? 10 : s === 48 ? 18 : 13, 600);
|
|
192
|
+
const box = { x: b.x, y: b.y, w: s, h: s };
|
|
193
|
+
return this.group(n, [
|
|
194
|
+
el('circle', {
|
|
195
|
+
cx: r(b.x + s / 2),
|
|
196
|
+
cy: r(b.y + s / 2),
|
|
197
|
+
r: r(s / 2),
|
|
198
|
+
fill: c.bg,
|
|
199
|
+
stroke: c.border,
|
|
200
|
+
'stroke-width': this.S,
|
|
201
|
+
}),
|
|
202
|
+
initials ? this.centred(initials, box, f, s, c.text) : undefined,
|
|
203
|
+
], label);
|
|
204
|
+
}
|
|
205
|
+
case 'badge':
|
|
206
|
+
return this.group(n, this.badge(n, label, b.x, b.y));
|
|
207
|
+
case 'image': {
|
|
208
|
+
const f = L.body(13, 400);
|
|
209
|
+
const shown = truncate(label, f, b.w - 4 * u);
|
|
210
|
+
const parts = [
|
|
211
|
+
this.outlined(b, c.bg),
|
|
212
|
+
el('g', { 'aria-hidden': 'true' }, el('line', {
|
|
213
|
+
x1: b.x,
|
|
214
|
+
y1: b.y,
|
|
215
|
+
x2: b.x + b.w,
|
|
216
|
+
y2: b.y + b.h,
|
|
217
|
+
stroke: c.border,
|
|
218
|
+
'stroke-width': this.S,
|
|
219
|
+
}), el('line', {
|
|
220
|
+
x1: b.x,
|
|
221
|
+
y1: b.y + b.h,
|
|
222
|
+
x2: b.x + b.w,
|
|
223
|
+
y2: b.y,
|
|
224
|
+
stroke: c.border,
|
|
225
|
+
'stroke-width': this.S,
|
|
226
|
+
})),
|
|
227
|
+
];
|
|
228
|
+
if (shown) {
|
|
229
|
+
const w = measure(shown, f);
|
|
230
|
+
const bx = r(b.x + (b.w - w - 2 * u) / 2);
|
|
231
|
+
const by = r(b.y + (b.h - 18) / 2);
|
|
232
|
+
parts.push(el('rect', { x: bx, y: by, width: w + 2 * u, height: 18, fill: c.bg }));
|
|
233
|
+
parts.push(this.text(shown, r(bx + u), by, f, 18, c.muted));
|
|
234
|
+
}
|
|
235
|
+
return this.group(n, parts, label);
|
|
236
|
+
}
|
|
237
|
+
case 'spacer':
|
|
238
|
+
return this.group(n, []);
|
|
239
|
+
case 'divider': {
|
|
240
|
+
const inRow = b.w <= this.S && b.h > this.S;
|
|
241
|
+
const line = inRow
|
|
242
|
+
? this.vline(r(b.x + b.w / 2), b.y, b.y + b.h)
|
|
243
|
+
: this.hline(b.x, b.x + b.w, r(b.y + b.h / 2));
|
|
244
|
+
return this.group(n, [el('g', { 'aria-hidden': 'true' }, line)]);
|
|
245
|
+
}
|
|
246
|
+
case 'item':
|
|
247
|
+
return this.group(n, this.item(n, b));
|
|
248
|
+
case 'nav':
|
|
249
|
+
return this.group(n, this.nav(l));
|
|
250
|
+
case 'tabs':
|
|
251
|
+
return this.group(n, [...this.tabs(l), ...kids()]);
|
|
252
|
+
case 'breadcrumbs':
|
|
253
|
+
return this.group(n, this.crumbs(n, b));
|
|
254
|
+
case 'list':
|
|
255
|
+
return this.group(n, l.entries ? this.listEntries(l) : kids());
|
|
256
|
+
case 'table':
|
|
257
|
+
return this.group(n, this.table(l));
|
|
258
|
+
case 'card': {
|
|
259
|
+
const p = L.pad(n);
|
|
260
|
+
const parts = [this.outlined(b, c.surface)];
|
|
261
|
+
// With a leading header, the title is drawn in the header instead (Appendix C.7).
|
|
262
|
+
if (n.label !== undefined && L.leadHeader(n) === undefined) {
|
|
263
|
+
const f = L.heading(15, 600);
|
|
264
|
+
const shown = truncate(label, f, b.w - 2 * p);
|
|
265
|
+
if (shown)
|
|
266
|
+
parts.push(this.text(shown, b.x + p, b.y + p, f, 22, c.text));
|
|
267
|
+
}
|
|
268
|
+
return this.group(n, [...parts, ...kids()]);
|
|
269
|
+
}
|
|
270
|
+
case 'empty':
|
|
271
|
+
case 'error':
|
|
272
|
+
case 'loading':
|
|
273
|
+
return this.group(n, [...this.state(l), ...kids()]);
|
|
274
|
+
case 'header':
|
|
275
|
+
case 'footer': {
|
|
276
|
+
const y = n.type === 'header' ? b.y + b.h : b.y;
|
|
277
|
+
const entries = l.children.map((k) => {
|
|
278
|
+
if (k.node.type !== CARD_TITLE)
|
|
279
|
+
return this.draw(k);
|
|
280
|
+
const f = L.heading(15, 600);
|
|
281
|
+
const shown = truncate(k.node.label ?? '', f, k.box.w);
|
|
282
|
+
return shown ? this.text(shown, k.box.x, k.box.y, f, 22, c.text) : undefined;
|
|
283
|
+
});
|
|
284
|
+
return this.group(n, [el('g', { 'aria-hidden': 'true' }, this.hline(b.x, b.x + b.w, y)), ...entries]);
|
|
285
|
+
}
|
|
286
|
+
case 'sidebar': {
|
|
287
|
+
const parts = [el('rect', { x: b.x, y: b.y, width: b.w, height: b.h, fill: c.bg })];
|
|
288
|
+
const right = has(n, 'right');
|
|
289
|
+
parts.push(el('g', { 'aria-hidden': 'true' }, this.vline(right ? b.x : b.x + b.w, b.y, b.y + b.h)));
|
|
290
|
+
return this.group(n, [...parts, ...kids()]);
|
|
291
|
+
}
|
|
292
|
+
case 'modal':
|
|
293
|
+
case 'drawer':
|
|
294
|
+
return this.group(n, this.overlay(l));
|
|
295
|
+
case 'swatches':
|
|
296
|
+
return this.group(n, this.swatches(b));
|
|
297
|
+
default:
|
|
298
|
+
return this.group(n, kids());
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
alignX(n, b, w) {
|
|
302
|
+
const a = n.attrs?.align;
|
|
303
|
+
if (a === 'center')
|
|
304
|
+
return r(b.x + (b.w - w) / 2);
|
|
305
|
+
if (a === 'end')
|
|
306
|
+
return r(b.x + b.w - w);
|
|
307
|
+
return b.x;
|
|
308
|
+
}
|
|
309
|
+
button(n, b) {
|
|
310
|
+
const u = this.u;
|
|
311
|
+
const c = this.c;
|
|
312
|
+
const h = this.L.buttonHeight(n);
|
|
313
|
+
const box = { x: b.x, y: b.y, w: b.w, h };
|
|
314
|
+
const disabled = has(n, 'disabled');
|
|
315
|
+
let fill = c.surface;
|
|
316
|
+
let stroke = c.border;
|
|
317
|
+
let ink = c.text;
|
|
318
|
+
if (disabled) {
|
|
319
|
+
fill = c.bg;
|
|
320
|
+
ink = c.muted;
|
|
321
|
+
}
|
|
322
|
+
else if (has(n, 'primary')) {
|
|
323
|
+
fill = c.primary;
|
|
324
|
+
stroke = c.primary;
|
|
325
|
+
ink = c.onPrimary;
|
|
326
|
+
}
|
|
327
|
+
else if (has(n, 'danger')) {
|
|
328
|
+
fill = c.danger;
|
|
329
|
+
stroke = c.danger;
|
|
330
|
+
ink = c.onPrimary;
|
|
331
|
+
}
|
|
332
|
+
const parts = [this.outlined(box, fill, stroke)];
|
|
333
|
+
const icon = textOf(n.attrs?.icon);
|
|
334
|
+
const iconSize = has(n, 'lg') ? 20 : 16;
|
|
335
|
+
const f = this.L.body(15, 600);
|
|
336
|
+
if (icon !== undefined && n.label === undefined) {
|
|
337
|
+
parts.push(this.icon(icon, r(b.x + (b.w - iconSize) / 2), r(b.y + (h - iconSize) / 2), iconSize, ink));
|
|
338
|
+
return parts;
|
|
339
|
+
}
|
|
340
|
+
const iconW = icon !== undefined ? iconSize + 2 * u : 0;
|
|
341
|
+
const shown = truncate(n.label ?? '', f, b.w - 8 * u - iconW);
|
|
342
|
+
const w = iconW + measure(shown, f);
|
|
343
|
+
const x0 = r(b.x + (b.w - w) / 2);
|
|
344
|
+
if (icon !== undefined)
|
|
345
|
+
parts.push(this.icon(icon, x0, r(b.y + (h - iconSize) / 2), iconSize, ink));
|
|
346
|
+
if (shown)
|
|
347
|
+
parts.push(this.text(shown, r(x0 + iconW), r(b.y + (h - 22) / 2), f, 22, ink));
|
|
348
|
+
return parts;
|
|
349
|
+
}
|
|
350
|
+
fieldLabel(n, b) {
|
|
351
|
+
if (n.label === undefined)
|
|
352
|
+
return [];
|
|
353
|
+
const f = this.L.body(13, 500);
|
|
354
|
+
const req = has(n, 'required');
|
|
355
|
+
const shown = truncate(n.label, f, b.w - (req ? measure(' *', f) : 0));
|
|
356
|
+
const top = b.y;
|
|
357
|
+
const y = r(top + baseline(f, 18));
|
|
358
|
+
return [
|
|
359
|
+
el('text', {
|
|
360
|
+
x: b.x,
|
|
361
|
+
y,
|
|
362
|
+
'font-family': FAMILY[f.font],
|
|
363
|
+
'font-size': f.size,
|
|
364
|
+
'font-weight': f.weight,
|
|
365
|
+
fill: this.c.text,
|
|
366
|
+
}, shown, req ? el('tspan', { fill: this.c.danger }, ' *') : undefined),
|
|
367
|
+
];
|
|
368
|
+
}
|
|
369
|
+
field(n, b) {
|
|
370
|
+
const u = this.u;
|
|
371
|
+
const c = this.c;
|
|
372
|
+
const parts = this.fieldLabel(n, b);
|
|
373
|
+
const top = n.label !== undefined ? b.y + 18 + u : b.y;
|
|
374
|
+
const rows = typeof n.attrs?.rows === 'number' &&
|
|
375
|
+
Number.isInteger(n.attrs.rows) &&
|
|
376
|
+
n.attrs.rows >= 1 &&
|
|
377
|
+
n.attrs.rows <= 100
|
|
378
|
+
? n.attrs.rows
|
|
379
|
+
: 3;
|
|
380
|
+
const h = n.type === 'textarea' ? rows * 22 + 4 * u : 10 * u;
|
|
381
|
+
const box = { x: b.x, y: r(top), w: b.w, h };
|
|
382
|
+
const disabled = has(n, 'disabled');
|
|
383
|
+
const error = textOf(n.attrs?.error);
|
|
384
|
+
parts.push(this.outlined(box, disabled ? c.bg : c.surface, error !== undefined ? c.danger : c.border));
|
|
385
|
+
const type = textOf(n.attrs?.type);
|
|
386
|
+
let left = box.x + 3 * u;
|
|
387
|
+
let right = box.x + box.w - 3 * u;
|
|
388
|
+
const iconY = r(box.y + (Math.min(h, 10 * u) - 16) / 2);
|
|
389
|
+
if (n.type === 'input' && type === 'search') {
|
|
390
|
+
parts.push(this.icon('search', left, iconY, 16, c.muted));
|
|
391
|
+
left += 16 + 2 * u;
|
|
392
|
+
}
|
|
393
|
+
else if (n.type === 'input' && type === 'date') {
|
|
394
|
+
parts.push(this.icon('calendar', right - 16, iconY, 16, c.muted));
|
|
395
|
+
right -= 16 + 2 * u;
|
|
396
|
+
}
|
|
397
|
+
else if (n.type === 'select') {
|
|
398
|
+
parts.push(this.icon('chevron-down', right - 16, iconY, 16, c.muted));
|
|
399
|
+
right -= 16 + 2 * u;
|
|
400
|
+
}
|
|
401
|
+
const value = textOf(n.attrs?.value);
|
|
402
|
+
const placeholder = textOf(n.attrs?.placeholder);
|
|
403
|
+
let shown;
|
|
404
|
+
let ink = disabled ? c.muted : c.text;
|
|
405
|
+
if (value !== undefined)
|
|
406
|
+
shown = n.type === 'input' && type === 'password' ? '•'.repeat(Array.from(value).length) : value;
|
|
407
|
+
else if (placeholder !== undefined) {
|
|
408
|
+
shown = placeholder;
|
|
409
|
+
ink = c.muted;
|
|
410
|
+
}
|
|
411
|
+
const f = this.L.body(15, 400);
|
|
412
|
+
if (shown !== undefined) {
|
|
413
|
+
if (n.type === 'textarea') {
|
|
414
|
+
let y = box.y + 2 * u;
|
|
415
|
+
for (const line of this.L.wrapText(shown, f, right - left)) {
|
|
416
|
+
if (y + 22 > box.y + box.h)
|
|
417
|
+
break;
|
|
418
|
+
parts.push(this.text(line, r(left), r(y), f, 22, ink));
|
|
419
|
+
y += 22;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
const cut = truncate(shown, f, right - left);
|
|
424
|
+
if (cut)
|
|
425
|
+
parts.push(this.text(cut, r(left), r(box.y + (h - 22) / 2), f, 22, ink));
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (error !== undefined) {
|
|
429
|
+
const ef = this.L.body(13, 400);
|
|
430
|
+
const cut = truncate(error, ef, b.w);
|
|
431
|
+
if (cut)
|
|
432
|
+
parts.push(this.text(cut, b.x, r(box.y + h + u), ef, 18, c.danger));
|
|
433
|
+
}
|
|
434
|
+
return parts;
|
|
435
|
+
}
|
|
436
|
+
check(n, b) {
|
|
437
|
+
const u = this.u;
|
|
438
|
+
const c = this.c;
|
|
439
|
+
const h = 6 * u;
|
|
440
|
+
const disabled = has(n, 'disabled');
|
|
441
|
+
const parts = [];
|
|
442
|
+
let markW = 16;
|
|
443
|
+
if (n.type === 'checkbox') {
|
|
444
|
+
// `checked` and `on` mean the same on a checkbox and on a toggle (section 5.5).
|
|
445
|
+
const checked = has(n, 'checked') || has(n, 'on');
|
|
446
|
+
const y = r(b.y + (h - 16) / 2);
|
|
447
|
+
parts.push(el('rect', {
|
|
448
|
+
x: b.x,
|
|
449
|
+
y,
|
|
450
|
+
width: 16,
|
|
451
|
+
height: 16,
|
|
452
|
+
rx: Math.min(this.R, 4),
|
|
453
|
+
fill: checked ? c.primary : disabled ? c.bg : c.surface,
|
|
454
|
+
stroke: checked ? c.primary : c.border,
|
|
455
|
+
'stroke-width': this.S,
|
|
456
|
+
}));
|
|
457
|
+
if (checked)
|
|
458
|
+
parts.push(this.icon('check', b.x, y, 16, c.onPrimary));
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
markW = 36;
|
|
462
|
+
const on = has(n, 'on') || has(n, 'checked');
|
|
463
|
+
const y = r(b.y + (h - 20) / 2);
|
|
464
|
+
parts.push(el('rect', {
|
|
465
|
+
x: b.x,
|
|
466
|
+
y,
|
|
467
|
+
width: 36,
|
|
468
|
+
height: 20,
|
|
469
|
+
rx: 10,
|
|
470
|
+
fill: on ? c.primary : c.bg,
|
|
471
|
+
stroke: on ? c.primary : c.border,
|
|
472
|
+
'stroke-width': this.S,
|
|
473
|
+
}));
|
|
474
|
+
parts.push(on
|
|
475
|
+
? el('circle', { cx: b.x + 26, cy: r(y + 10), r: 8, fill: c.onPrimary })
|
|
476
|
+
: el('circle', {
|
|
477
|
+
cx: b.x + 10,
|
|
478
|
+
cy: r(y + 10),
|
|
479
|
+
r: 8,
|
|
480
|
+
fill: c.surface,
|
|
481
|
+
stroke: c.border,
|
|
482
|
+
'stroke-width': this.S,
|
|
483
|
+
}));
|
|
484
|
+
}
|
|
485
|
+
const f = this.L.body(15, 400);
|
|
486
|
+
const x = b.x + markW + 2 * u;
|
|
487
|
+
const shown = truncate(n.label ?? '', f, Math.max(0, b.w - markW - 2 * u));
|
|
488
|
+
if (shown)
|
|
489
|
+
parts.push(this.text(shown, x, r(b.y + (h - 22) / 2), f, 22, disabled ? c.muted : c.text));
|
|
490
|
+
return parts;
|
|
491
|
+
}
|
|
492
|
+
radio(n, b) {
|
|
493
|
+
const u = this.u;
|
|
494
|
+
const c = this.c;
|
|
495
|
+
const parts = this.fieldLabel(n, b);
|
|
496
|
+
let y = n.label !== undefined ? b.y + 18 + u : b.y;
|
|
497
|
+
const value = textOf(n.attrs?.value);
|
|
498
|
+
const disabled = has(n, 'disabled');
|
|
499
|
+
const f = this.L.body(15, 400);
|
|
500
|
+
for (const opt of n.items ?? []) {
|
|
501
|
+
const cy = r(y + 3 * u);
|
|
502
|
+
parts.push(el('circle', {
|
|
503
|
+
cx: b.x + 8,
|
|
504
|
+
cy,
|
|
505
|
+
r: 8,
|
|
506
|
+
fill: disabled ? c.bg : c.surface,
|
|
507
|
+
stroke: c.border,
|
|
508
|
+
'stroke-width': this.S,
|
|
509
|
+
}));
|
|
510
|
+
if (opt === value)
|
|
511
|
+
parts.push(el('circle', { cx: b.x + 8, cy, r: 4, fill: c.primary }));
|
|
512
|
+
const shown = truncate(opt, f, Math.max(0, b.w - 16 - 2 * u));
|
|
513
|
+
if (shown)
|
|
514
|
+
parts.push(this.text(shown, b.x + 16 + 2 * u, r(y + (6 * u - 22) / 2), f, 22, disabled ? c.muted : c.text));
|
|
515
|
+
y += 6 * u;
|
|
516
|
+
}
|
|
517
|
+
return parts;
|
|
518
|
+
}
|
|
519
|
+
badge(n, label, x, y) {
|
|
520
|
+
const u = this.u;
|
|
521
|
+
const c = this.c;
|
|
522
|
+
const f = this.L.body(13, 500);
|
|
523
|
+
const w = measure(label, f) + 4 * u;
|
|
524
|
+
let fill = c.bg;
|
|
525
|
+
let stroke = c.border;
|
|
526
|
+
let ink = c.text;
|
|
527
|
+
for (const k of ['primary', 'success', 'danger']) {
|
|
528
|
+
if (n && has(n, k)) {
|
|
529
|
+
fill = k === 'primary' ? c.primary : k === 'success' ? c.success : c.danger;
|
|
530
|
+
stroke = undefined;
|
|
531
|
+
ink = c.onPrimary;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const parts = [
|
|
535
|
+
el('rect', {
|
|
536
|
+
x,
|
|
537
|
+
y,
|
|
538
|
+
width: w,
|
|
539
|
+
height: 20,
|
|
540
|
+
rx: 10,
|
|
541
|
+
fill,
|
|
542
|
+
stroke,
|
|
543
|
+
'stroke-width': stroke ? this.S : undefined,
|
|
544
|
+
}),
|
|
545
|
+
];
|
|
546
|
+
if (label !== '')
|
|
547
|
+
parts.push(this.text(label, r(x + 2 * u), r(y + 1), f, 18, ink));
|
|
548
|
+
return parts;
|
|
549
|
+
}
|
|
550
|
+
item(n, b) {
|
|
551
|
+
const u = this.u;
|
|
552
|
+
const c = this.c;
|
|
553
|
+
const active = has(n, 'active');
|
|
554
|
+
const parts = [];
|
|
555
|
+
if (active)
|
|
556
|
+
parts.push(el('rect', { x: b.x, y: b.y, width: b.w, height: b.h, rx: this.R, fill: c.bg }));
|
|
557
|
+
let x = b.x + 3 * u;
|
|
558
|
+
const icon = textOf(n.attrs?.icon);
|
|
559
|
+
if (icon !== undefined) {
|
|
560
|
+
parts.push(this.icon(icon, x, r(b.y + (b.h - 16) / 2), 16, c.text));
|
|
561
|
+
x += 16 + 2 * u;
|
|
562
|
+
}
|
|
563
|
+
const badge = textOf(n.attrs?.badge);
|
|
564
|
+
const badgeW = badge !== undefined ? measure(badge, this.L.body(13, 500)) + 4 * u : 0;
|
|
565
|
+
const right = b.x + b.w - 3 * u - (badge !== undefined ? badgeW + 2 * u : 0);
|
|
566
|
+
const f = this.L.body(15, active ? 600 : 400);
|
|
567
|
+
const shown = truncate(n.label ?? '', f, Math.max(0, right - x));
|
|
568
|
+
if (shown)
|
|
569
|
+
parts.push(this.text(shown, r(x), r(b.y + (b.h - 22) / 2), f, 22, c.text));
|
|
570
|
+
if (badge !== undefined)
|
|
571
|
+
parts.push(...this.badge(undefined, badge, r(b.x + b.w - 3 * u - badgeW), r(b.y + (b.h - 20) / 2)));
|
|
572
|
+
return parts;
|
|
573
|
+
}
|
|
574
|
+
nav(l) {
|
|
575
|
+
const u = this.u;
|
|
576
|
+
const c = this.c;
|
|
577
|
+
const vertical = has(l.node, 'vertical');
|
|
578
|
+
const parts = [];
|
|
579
|
+
for (const e of l.entries ?? []) {
|
|
580
|
+
const f = this.L.body(15, e.active ? 600 : 400);
|
|
581
|
+
const eb = e.box;
|
|
582
|
+
if (vertical && e.active)
|
|
583
|
+
parts.push(el('rect', { x: eb.x, y: eb.y, width: eb.w, height: eb.h, rx: this.R, fill: c.bg }));
|
|
584
|
+
let x = vertical ? eb.x + 3 * u : eb.x;
|
|
585
|
+
if (e.icon !== undefined) {
|
|
586
|
+
parts.push(this.icon(e.icon, x, r(eb.y + (eb.h - 16) / 2), 16, c.text));
|
|
587
|
+
x += 16 + 2 * u;
|
|
588
|
+
}
|
|
589
|
+
const room = vertical ? eb.x + eb.w - 3 * u - x : eb.x + eb.w - x;
|
|
590
|
+
const shown = truncate(e.text, f, Math.max(0, room));
|
|
591
|
+
if (shown)
|
|
592
|
+
parts.push(this.text(shown, r(x), r(eb.y + (eb.h - 22) / 2), f, 22, c.text));
|
|
593
|
+
if (!vertical && e.active) {
|
|
594
|
+
parts.push(el('rect', {
|
|
595
|
+
x: r(x),
|
|
596
|
+
y: r(eb.y + eb.h - 2),
|
|
597
|
+
width: measure(shown, f),
|
|
598
|
+
height: 2,
|
|
599
|
+
fill: c.primary,
|
|
600
|
+
}));
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
for (const k of l.children)
|
|
604
|
+
parts.push(this.group(k.node, []));
|
|
605
|
+
return parts;
|
|
606
|
+
}
|
|
607
|
+
tabs(l) {
|
|
608
|
+
const u = this.u;
|
|
609
|
+
const c = this.c;
|
|
610
|
+
const b = l.box;
|
|
611
|
+
const strip = 10 * u;
|
|
612
|
+
const parts = [
|
|
613
|
+
el('g', { 'aria-hidden': 'true' }, this.hline(b.x, b.x + b.w, b.y + strip)),
|
|
614
|
+
];
|
|
615
|
+
for (const e of l.entries ?? []) {
|
|
616
|
+
const f = this.L.body(15, e.active ? 600 : 400);
|
|
617
|
+
parts.push(this.centred(e.text, { x: e.box.x, y: e.box.y, w: e.box.w, h: strip }, f, 22, e.active ? c.text : c.muted, 4 * u));
|
|
618
|
+
if (e.active)
|
|
619
|
+
parts.push(el('rect', { x: e.box.x, y: r(b.y + strip - 2), width: e.box.w, height: 2, fill: c.primary }));
|
|
620
|
+
}
|
|
621
|
+
return parts;
|
|
622
|
+
}
|
|
623
|
+
crumbs(n, b) {
|
|
624
|
+
const u = this.u;
|
|
625
|
+
const c = this.c;
|
|
626
|
+
const items = n.items ?? [];
|
|
627
|
+
const parts = [];
|
|
628
|
+
let x = b.x;
|
|
629
|
+
items.forEach((it, i) => {
|
|
630
|
+
const last = i === items.length - 1;
|
|
631
|
+
if (i > 0) {
|
|
632
|
+
parts.push(this.icon('chevron-right', r(x + u), r(b.y + (b.h - 16) / 2), 16, c.muted));
|
|
633
|
+
x += 16 + 2 * u;
|
|
634
|
+
}
|
|
635
|
+
const f = this.L.body(15, last ? 500 : 400);
|
|
636
|
+
const shown = truncate(it, f, Math.max(0, b.x + b.w - x));
|
|
637
|
+
if (shown)
|
|
638
|
+
parts.push(this.text(shown, r(x), r(b.y + (b.h - 22) / 2), f, 22, last ? c.text : c.muted));
|
|
639
|
+
x += measure(shown, f);
|
|
640
|
+
});
|
|
641
|
+
return parts;
|
|
642
|
+
}
|
|
643
|
+
listEntries(l) {
|
|
644
|
+
const u = this.u;
|
|
645
|
+
const c = this.c;
|
|
646
|
+
const ordered = has(l.node, 'ordered');
|
|
647
|
+
const f = this.L.body(15, 400);
|
|
648
|
+
const parts = [];
|
|
649
|
+
(l.entries ?? []).forEach((e, i) => {
|
|
650
|
+
const top = r(e.box.y + (e.box.h - 22) / 2);
|
|
651
|
+
if (ordered)
|
|
652
|
+
parts.push(this.text(`${i + 1}.`, e.box.x, top, f, 22, c.muted));
|
|
653
|
+
else
|
|
654
|
+
parts.push(el('circle', { cx: r(e.box.x + 2 * u), cy: r(e.box.y + e.box.h / 2), r: 2.5, fill: c.text }));
|
|
655
|
+
const shown = truncate(e.text, f, Math.max(0, e.box.w - 5 * u));
|
|
656
|
+
if (shown)
|
|
657
|
+
parts.push(this.text(shown, r(e.box.x + 5 * u), top, f, 22, c.text));
|
|
658
|
+
});
|
|
659
|
+
return parts;
|
|
660
|
+
}
|
|
661
|
+
table(l) {
|
|
662
|
+
const u = this.u;
|
|
663
|
+
const c = this.c;
|
|
664
|
+
const n = l.node;
|
|
665
|
+
const b = l.box;
|
|
666
|
+
const parts = [];
|
|
667
|
+
if (n.label !== undefined) {
|
|
668
|
+
const f = this.L.heading(15, 600);
|
|
669
|
+
const shown = truncate(n.label, f, b.w);
|
|
670
|
+
if (shown)
|
|
671
|
+
parts.push(this.text(shown, b.x, b.y, f, 22, c.text));
|
|
672
|
+
}
|
|
673
|
+
const top = l.tableTop ?? b.y;
|
|
674
|
+
const box = { x: b.x, y: top, w: b.w, h: r(b.y + b.h - top) };
|
|
675
|
+
const clipId = `${this.ids(n)}-clip`;
|
|
676
|
+
this.defs.push(el('clipPath', { id: clipId }, el('rect', { x: box.x, y: box.y, width: box.w, height: box.h, rx: this.R })));
|
|
677
|
+
const inner = [
|
|
678
|
+
el('rect', { x: box.x, y: box.y, width: box.w, height: box.h, fill: c.surface }),
|
|
679
|
+
];
|
|
680
|
+
const rowH = 10 * u;
|
|
681
|
+
inner.push(el('rect', { x: box.x, y: box.y, width: box.w, height: rowH, fill: c.bg }));
|
|
682
|
+
const cols = l.columns ?? [0];
|
|
683
|
+
const colW = (i) => (cols[i + 1] ?? box.w) - (cols[i] ?? 0);
|
|
684
|
+
const cell = (text, i, y, f, ink) => {
|
|
685
|
+
const shown = truncate(text, f, Math.max(0, colW(i) - 6 * u));
|
|
686
|
+
return shown
|
|
687
|
+
? this.text(shown, r(box.x + (cols[i] ?? 0) + 3 * u), r(y + (rowH - 22) / 2), f, 22, ink)
|
|
688
|
+
: undefined;
|
|
689
|
+
};
|
|
690
|
+
for (const [i, h] of (n.items ?? []).entries())
|
|
691
|
+
inner.push(cell(h, i, box.y, this.L.body(14, 600), c.text));
|
|
692
|
+
const rows = Math.round(box.h / rowH) - 1;
|
|
693
|
+
const rowNodes = l.children;
|
|
694
|
+
for (let k = 0; k < rows; k++) {
|
|
695
|
+
const y = r(box.y + rowH * (k + 1));
|
|
696
|
+
inner.push(this.hline(box.x, box.x + box.w, y));
|
|
697
|
+
const rn = rowNodes[k];
|
|
698
|
+
if (rn) {
|
|
699
|
+
const cells = [];
|
|
700
|
+
if (has(rn.node, 'selected')) {
|
|
701
|
+
cells.push(el('rect', { x: box.x, y, width: box.w, height: rowH, fill: c.bg }));
|
|
702
|
+
cells.push(el('rect', { x: box.x, y, width: 3, height: rowH, fill: c.primary }));
|
|
703
|
+
}
|
|
704
|
+
for (let i = 0; i < cols.length; i++) {
|
|
705
|
+
cells.push(cell(rn.node.items?.[i] ?? '', i, y, this.L.body(14, 400), c.text));
|
|
706
|
+
}
|
|
707
|
+
inner.push(this.group(rn.node, cells));
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
for (let i = 0; i < cols.length; i++) {
|
|
711
|
+
inner.push(this.bar(r(box.x + (cols[i] ?? 0) + 3 * u), r(y + (rowH - 8) / 2), r((colW(i) - 6 * u) * 0.6)));
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
parts.push(el('g', { 'clip-path': `url(#${clipId})` }, ...inner));
|
|
716
|
+
parts.push(el('rect', {
|
|
717
|
+
x: box.x,
|
|
718
|
+
y: box.y,
|
|
719
|
+
width: box.w,
|
|
720
|
+
height: box.h,
|
|
721
|
+
rx: this.R,
|
|
722
|
+
fill: 'none',
|
|
723
|
+
stroke: c.border,
|
|
724
|
+
'stroke-width': this.S,
|
|
725
|
+
}));
|
|
726
|
+
return parts;
|
|
727
|
+
}
|
|
728
|
+
state(l) {
|
|
729
|
+
const u = this.u;
|
|
730
|
+
const c = this.c;
|
|
731
|
+
const n = l.node;
|
|
732
|
+
const b = l.box;
|
|
733
|
+
const parts = [];
|
|
734
|
+
let y = b.y + 8 * u;
|
|
735
|
+
const cx = b.x + b.w / 2;
|
|
736
|
+
if (n.type === 'loading') {
|
|
737
|
+
const cy = r(y + 12);
|
|
738
|
+
parts.push(el('g', { 'aria-hidden': 'true' }, el('circle', { cx: r(cx), cy, r: 12, fill: 'none', stroke: c.border, 'stroke-width': this.S }), el('path', {
|
|
739
|
+
d: `M${num(r(cx))} ${num(cy - 12)}A12 12 0 0 1 ${num(r(cx + 12))} ${num(cy)}`,
|
|
740
|
+
fill: 'none',
|
|
741
|
+
stroke: c.primary,
|
|
742
|
+
'stroke-width': this.S,
|
|
743
|
+
'stroke-linecap': 'round',
|
|
744
|
+
})));
|
|
745
|
+
y += 24;
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
parts.push(this.icon(n.type === 'error' ? 'triangle-alert' : 'inbox', r(cx - 16), r(y), 32, n.type === 'error' ? c.danger : c.muted));
|
|
749
|
+
y += 32;
|
|
750
|
+
}
|
|
751
|
+
if (l.lines) {
|
|
752
|
+
y += 2 * u;
|
|
753
|
+
const f = this.L.body(15, 400);
|
|
754
|
+
for (const line of l.lines) {
|
|
755
|
+
const w = measure(line, f);
|
|
756
|
+
parts.push(this.text(line, r(cx - w / 2), r(y), f, 22, n.type === 'error' ? c.text : c.muted));
|
|
757
|
+
y += 22;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return parts;
|
|
761
|
+
}
|
|
762
|
+
overlay(l) {
|
|
763
|
+
const u = this.u;
|
|
764
|
+
const c = this.c;
|
|
765
|
+
const n = l.node;
|
|
766
|
+
const b = l.box;
|
|
767
|
+
const p = 6 * u;
|
|
768
|
+
const parts = [];
|
|
769
|
+
if (n.type === 'modal')
|
|
770
|
+
parts.push(this.outlined(b, c.surface));
|
|
771
|
+
else {
|
|
772
|
+
parts.push(el('rect', { x: b.x, y: b.y, width: b.w, height: b.h, fill: c.surface }));
|
|
773
|
+
const bottom = has(n, 'bottom');
|
|
774
|
+
const left = has(n, 'left') && !has(n, 'right');
|
|
775
|
+
const edge = bottom
|
|
776
|
+
? this.hline(b.x, b.x + b.w, b.y)
|
|
777
|
+
: this.vline(left ? b.x + b.w : b.x, b.y, b.y + b.h);
|
|
778
|
+
parts.push(el('g', { 'aria-hidden': 'true' }, edge));
|
|
779
|
+
}
|
|
780
|
+
const f = this.L.heading(18, 600);
|
|
781
|
+
const shown = truncate(n.label ?? '', f, b.w - 2 * p - 20 - 2 * u);
|
|
782
|
+
if (shown)
|
|
783
|
+
parts.push(this.text(shown, b.x + p, b.y + p, f, 24, c.text));
|
|
784
|
+
parts.push(this.icon('x', r(b.x + b.w - p - 20), r(b.y + p + 2), 20, c.muted));
|
|
785
|
+
for (const k of l.children)
|
|
786
|
+
parts.push(this.draw(k));
|
|
787
|
+
return parts;
|
|
788
|
+
}
|
|
789
|
+
swatches(b) {
|
|
790
|
+
const u = this.u;
|
|
791
|
+
const c = this.c;
|
|
792
|
+
const gap = 4 * u;
|
|
793
|
+
const cols = 3;
|
|
794
|
+
const colW = (b.w - gap * (cols - 1)) / cols;
|
|
795
|
+
const values = [c.bg, c.surface, c.border, c.text, c.muted, c.primary, c.onPrimary, c.danger, c.success];
|
|
796
|
+
const parts = [];
|
|
797
|
+
COLOR_TOKENS.forEach((token, i) => {
|
|
798
|
+
const x = r(b.x + (i % cols) * (colW + gap));
|
|
799
|
+
const y = r(b.y + Math.floor(i / cols) * (36 + gap));
|
|
800
|
+
const value = values[i] ?? '#000000';
|
|
801
|
+
parts.push(el('rect', {
|
|
802
|
+
x,
|
|
803
|
+
y: r(y + 6),
|
|
804
|
+
width: 40,
|
|
805
|
+
height: 24,
|
|
806
|
+
rx: this.R,
|
|
807
|
+
fill: value,
|
|
808
|
+
stroke: c.border,
|
|
809
|
+
'stroke-width': this.S,
|
|
810
|
+
}));
|
|
811
|
+
parts.push(this.text(token, x + 40 + 2 * u, y, this.L.body(13, 500), 18, c.text));
|
|
812
|
+
parts.push(this.text(value, x + 40 + 2 * u, y + 18, this.L.body(13, 400), 18, c.muted));
|
|
813
|
+
});
|
|
814
|
+
return parts;
|
|
815
|
+
}
|
|
816
|
+
// ---- frames ---------------------------------------------------------------------------------
|
|
817
|
+
frame(f, alone) {
|
|
818
|
+
const c = this.c;
|
|
819
|
+
const frameId = `${this.input.idPrefix}-${f.key}`;
|
|
820
|
+
const isExtra = f.overlay !== undefined;
|
|
821
|
+
this.scope = isExtra ? `${f.key}-` : '';
|
|
822
|
+
const clipId = `${frameId}-clip`;
|
|
823
|
+
this.defs.push(el('clipPath', { id: clipId }, el('rect', { x: 0, y: 0, width: f.w, height: f.h, rx: this.R })));
|
|
824
|
+
const inner = [];
|
|
825
|
+
const s = f.screen;
|
|
826
|
+
// Source order among the screen's direct children; overlays last.
|
|
827
|
+
const byNode = new Map();
|
|
828
|
+
if (f.header)
|
|
829
|
+
byNode.set(f.header.node, f.header);
|
|
830
|
+
if (f.footer)
|
|
831
|
+
byNode.set(f.footer.node, f.footer);
|
|
832
|
+
for (const l of f.sidebars)
|
|
833
|
+
byNode.set(l.node, l);
|
|
834
|
+
for (const l of f.body)
|
|
835
|
+
byNode.set(l.node, l);
|
|
836
|
+
for (const k of s.children ?? []) {
|
|
837
|
+
const l = byNode.get(k);
|
|
838
|
+
if (l)
|
|
839
|
+
inner.push(this.draw(l));
|
|
840
|
+
}
|
|
841
|
+
if (f.open.length > 0) {
|
|
842
|
+
inner.push(el('rect', { x: 0, y: 0, width: f.w, height: f.h, rx: this.R, fill: c.text, 'fill-opacity': '0.4' }));
|
|
843
|
+
for (const o of f.open)
|
|
844
|
+
inner.push(this.draw(o));
|
|
845
|
+
}
|
|
846
|
+
const parts = [el('title', {}, f.title)];
|
|
847
|
+
if (!alone) {
|
|
848
|
+
const tf = this.L.heading(15, 600);
|
|
849
|
+
const shown = truncate(f.title, tf, f.w);
|
|
850
|
+
if (shown)
|
|
851
|
+
parts.push(this.text(shown, 0, -32, tf, 24, c.text));
|
|
852
|
+
}
|
|
853
|
+
parts.push(el('rect', { x: 0, y: 0, width: f.w, height: f.h, rx: this.R, fill: c.surface }));
|
|
854
|
+
parts.push(el('g', { 'clip-path': `url(#${clipId})` }, ...inner));
|
|
855
|
+
parts.push(el('rect', {
|
|
856
|
+
x: 0,
|
|
857
|
+
y: 0,
|
|
858
|
+
width: f.w,
|
|
859
|
+
height: f.h,
|
|
860
|
+
rx: this.R,
|
|
861
|
+
fill: 'none',
|
|
862
|
+
stroke: c.border,
|
|
863
|
+
'stroke-width': this.S,
|
|
864
|
+
}));
|
|
865
|
+
for (const m of f.markers)
|
|
866
|
+
parts.push(this.marker(m.number, m.x, m.y));
|
|
867
|
+
const g = el('g', { id: frameId, class: 'wl-screen', transform: `translate(${num(f.x)} ${num(f.y)})` }, ...parts);
|
|
868
|
+
this.scope = '';
|
|
869
|
+
return g;
|
|
870
|
+
}
|
|
871
|
+
marker(number, x, y) {
|
|
872
|
+
const box = { x: r(x - 9), y: r(y - 9), w: 18, h: 18 };
|
|
873
|
+
return el('g', { class: 'wl-marker', 'aria-hidden': 'true' }, el('circle', { cx: r(x), cy: r(y), r: 9, fill: this.c.primary }), this.centred(String(number), box, this.L.body(12, 600), 18, this.c.onPrimary));
|
|
874
|
+
}
|
|
875
|
+
notes(f) {
|
|
876
|
+
if (f.notesList.length === 0)
|
|
877
|
+
return undefined;
|
|
878
|
+
const u = this.u;
|
|
879
|
+
const c = this.c;
|
|
880
|
+
const x = f.x;
|
|
881
|
+
let y = f.y + f.h + 24;
|
|
882
|
+
const parts = [this.text('Notes', x, y, this.L.body(13, 600), 18, c.muted)];
|
|
883
|
+
y += 18 + 2 * u;
|
|
884
|
+
const font = this.L.body(13, 400);
|
|
885
|
+
for (const n of f.notesList) {
|
|
886
|
+
parts.push(this.marker(n.number, x + 9, y + 9));
|
|
887
|
+
for (const [i, line] of n.lines.entries())
|
|
888
|
+
parts.push(this.text(line, x + 24, y + i * 18, font, 18, c.text));
|
|
889
|
+
y += n.lines.length * 18 + 2 * u;
|
|
890
|
+
}
|
|
891
|
+
return el('g', { id: `${this.input.idPrefix}-${f.key}-notes`, class: 'wl-notes' }, ...parts);
|
|
892
|
+
}
|
|
893
|
+
/** An arrow's line. Its label is drawn separately, after every line (see `arrowLabel`). */
|
|
894
|
+
arrow(a, markerId) {
|
|
895
|
+
const c = this.c;
|
|
896
|
+
const d = a.points.map((p, i) => `${i === 0 ? 'M' : 'L'}${num(p[0])} ${num(p[1])}`).join('');
|
|
897
|
+
const parts = [
|
|
898
|
+
el('path', {
|
|
899
|
+
d,
|
|
900
|
+
fill: 'none',
|
|
901
|
+
stroke: c.muted,
|
|
902
|
+
'stroke-width': this.S,
|
|
903
|
+
'stroke-linejoin': 'round',
|
|
904
|
+
'marker-end': `url(#${markerId})`,
|
|
905
|
+
}),
|
|
906
|
+
];
|
|
907
|
+
return el('g', { class: 'wl-arrow' }, ...parts);
|
|
908
|
+
}
|
|
909
|
+
/** An arrow's label, or nothing. Labels are drawn after every arrow, so no line crosses one. */
|
|
910
|
+
arrowLabel(a) {
|
|
911
|
+
if (a.label === undefined || a.label === '')
|
|
912
|
+
return undefined;
|
|
913
|
+
const f = this.L.body(13, 500);
|
|
914
|
+
const label = a.label.replace(/\n/g, ' ');
|
|
915
|
+
const w = measure(label, f) + 2 * this.u;
|
|
916
|
+
const x = r(a.mid[0] - w / 2);
|
|
917
|
+
const y = r(a.mid[1] - 9);
|
|
918
|
+
return el('g', { class: 'wl-arrow-label' }, el('rect', { x, y, width: w, height: 18, rx: 3, fill: this.c.bg }), this.text(label, r(x + this.u), y, f, 18, this.c.text));
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
function num(n) {
|
|
922
|
+
const v = Math.round(n * 10) / 10;
|
|
923
|
+
if (v === 0)
|
|
924
|
+
return '0';
|
|
925
|
+
return Number.isInteger(v) ? String(v) : v.toFixed(1);
|
|
926
|
+
}
|
|
927
|
+
/** Draws the overview as an SVG document. */
|
|
928
|
+
export function paint(input) {
|
|
929
|
+
const { overview: ov, idPrefix } = input;
|
|
930
|
+
const L = ov.layout;
|
|
931
|
+
const painter = new Painter(input, (n) => `${idPrefix}-${painter.scope}${n.id ?? `n${L.index.get(n) ?? 0}`}`);
|
|
932
|
+
const body = [];
|
|
933
|
+
body.push(el('rect', { x: 0, y: 0, width: ov.width, height: ov.height, fill: input.theme.color.bg }));
|
|
934
|
+
for (const f of ov.frames)
|
|
935
|
+
body.push(painter.frame(f, ov.alone));
|
|
936
|
+
for (const f of ov.frames) {
|
|
937
|
+
const notes = painter.notes(f);
|
|
938
|
+
if (notes)
|
|
939
|
+
body.push(notes);
|
|
940
|
+
}
|
|
941
|
+
const markerId = `${idPrefix}-arrow`;
|
|
942
|
+
if (ov.arrows.length > 0) {
|
|
943
|
+
painter.defs.push(el('marker', {
|
|
944
|
+
id: markerId,
|
|
945
|
+
markerWidth: 8,
|
|
946
|
+
markerHeight: 8,
|
|
947
|
+
refX: 7,
|
|
948
|
+
refY: 4,
|
|
949
|
+
orient: 'auto',
|
|
950
|
+
markerUnits: 'userSpaceOnUse',
|
|
951
|
+
}, el('path', { d: 'M0 0L8 4L0 8Z', fill: input.theme.color.muted })));
|
|
952
|
+
for (const a of ov.arrows)
|
|
953
|
+
body.push(painter.arrow(a, markerId));
|
|
954
|
+
for (const a of ov.arrows) {
|
|
955
|
+
const label = painter.arrowLabel(a);
|
|
956
|
+
if (label)
|
|
957
|
+
body.push(label);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
const head = [
|
|
961
|
+
el('title', { id: `${idPrefix}-title` }, input.title),
|
|
962
|
+
el('desc', { id: `${idPrefix}-desc` }, input.desc),
|
|
963
|
+
];
|
|
964
|
+
if (input.source !== undefined) {
|
|
965
|
+
head.push(el('metadata', {}, el('wl:source', { 'xmlns:wl': 'urn:wirelith:source', version: SPEC_VERSION }, input.source)));
|
|
966
|
+
}
|
|
967
|
+
if (painter.defs.length > 0)
|
|
968
|
+
head.push(el('defs', {}, ...painter.defs));
|
|
969
|
+
const svg = el('svg', {
|
|
970
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
971
|
+
width: ov.width,
|
|
972
|
+
height: ov.height,
|
|
973
|
+
viewBox: `0 0 ${num(ov.width)} ${num(ov.height)}`,
|
|
974
|
+
role: 'img',
|
|
975
|
+
'aria-labelledby': `${idPrefix}-title ${idPrefix}-desc`,
|
|
976
|
+
'xml:space': 'preserve',
|
|
977
|
+
}, ...head, join(body));
|
|
978
|
+
return `${svg.value}\n`;
|
|
979
|
+
}
|
|
980
|
+
//# sourceMappingURL=render.js.map
|