@skeudo/html 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 +59 -2
- package/dist/code/enhance.d.ts +2 -0
- package/dist/code/enhance.js +175 -0
- package/dist/code/links.d.ts +20 -0
- package/dist/code/links.js +45 -0
- package/dist/code/render.d.ts +21 -0
- package/dist/code/render.js +235 -0
- package/dist/data/enhance.d.ts +1 -0
- package/dist/data/enhance.js +355 -0
- package/dist/data/render.d.ts +4 -0
- package/dist/data/render.js +274 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +53 -0
- package/dist/shared/hast.d.ts +10 -0
- package/dist/shared/hast.js +48 -0
- package/dist/shared/popover.d.ts +23 -0
- package/dist/shared/popover.js +76 -0
- package/dist/shared/width.d.ts +4 -0
- package/dist/shared/width.js +37 -0
- package/dist/skeudo.css +904 -0
- package/package.json +47 -3
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Interaction for rendered data sketches, wired by enhance() in index.ts:
|
|
3
|
+
* - Stepping: the story plays in frames, one for each substep and one for each step without substeps.
|
|
4
|
+
* Clicking a step or substep, or the arrows below the list, shows its frame; a step with substeps shows
|
|
5
|
+
* its first. The arrow keys, Home and End step through the frames while the sketch has focus, since a
|
|
6
|
+
* page can hold several.
|
|
7
|
+
* - Reads: a line goes from the current step or substep to each row its reads find. The lines are drawn
|
|
8
|
+
* again when the sketch changes size or a table scrolls sideways.
|
|
9
|
+
* - References: hovering over one highlights the row it points at, and hovering over a row highlights
|
|
10
|
+
* the references to it.
|
|
11
|
+
* - Explanations: resting on a step, a substep or a table's name shows its explanation in the shared
|
|
12
|
+
* popover. Clicking its "i" mark keeps it open until Escape, a click elsewhere, or another mark.
|
|
13
|
+
*/
|
|
14
|
+
import { Popover, POPOVER_DELAY } from '../shared/popover.js';
|
|
15
|
+
const SKETCH = '[data-skd]';
|
|
16
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
17
|
+
export function enhanceData(root) {
|
|
18
|
+
const off = [stepping(root), highlightReferences(root), showDocs(root)];
|
|
19
|
+
return () => off.forEach((remove) => remove());
|
|
20
|
+
}
|
|
21
|
+
/** The frame a sketch shows. */
|
|
22
|
+
const current = (sketch) => Number(sketch.getAttribute('data-skd-frame'));
|
|
23
|
+
function stepping(root) {
|
|
24
|
+
/**
|
|
25
|
+
* The frame each sketch showed, by its position under root. A preview that renders the page again on
|
|
26
|
+
* each edit, as VS Code's does, then keeps the reader on their step. It is forgotten when the page's
|
|
27
|
+
* address changes, since a site that swaps pages without reloading, as docs sites do, puts other
|
|
28
|
+
* sketches at the same positions.
|
|
29
|
+
*/
|
|
30
|
+
const remembered = new Map();
|
|
31
|
+
const sketches = () => [...root.querySelectorAll(SKETCH)];
|
|
32
|
+
const win = (root instanceof Document ? root : root.ownerDocument).defaultView;
|
|
33
|
+
const address = () => (win ? win.location.pathname + win.location.search : '');
|
|
34
|
+
let page = address();
|
|
35
|
+
// The lines follow the rows when the sketch changes size, as when the page is resized or fonts load.
|
|
36
|
+
const resized = win?.ResizeObserver ? new win.ResizeObserver((entries) => entries.forEach((e) => drawLines(e.target))) : undefined;
|
|
37
|
+
const watched = new Set();
|
|
38
|
+
function go(sketch, frame) {
|
|
39
|
+
const count = Number(sketch.getAttribute('data-skd-frames'));
|
|
40
|
+
if (!(frame >= 0 && frame < count))
|
|
41
|
+
return;
|
|
42
|
+
sketch.setAttribute('data-skd-frame', String(frame));
|
|
43
|
+
for (const body of sketch.querySelectorAll('tbody[data-skd-at]')) {
|
|
44
|
+
body.hidden = Number(body.getAttribute('data-skd-at')) !== frame;
|
|
45
|
+
}
|
|
46
|
+
for (const item of sketch.querySelectorAll('li[data-skd-at]')) {
|
|
47
|
+
const at = Number(item.getAttribute('data-skd-at'));
|
|
48
|
+
item.classList.toggle('skd-cur', at === frame);
|
|
49
|
+
item.classList.toggle('skd-past', at < frame);
|
|
50
|
+
const button = item.querySelector(':scope > .skd-go');
|
|
51
|
+
if (at === frame)
|
|
52
|
+
button?.setAttribute('aria-current', 'step');
|
|
53
|
+
else
|
|
54
|
+
button?.removeAttribute('aria-current');
|
|
55
|
+
}
|
|
56
|
+
// A step with substeps is current while one of them is.
|
|
57
|
+
for (const item of sketch.querySelectorAll('li[data-skd-first]')) {
|
|
58
|
+
const first = Number(item.getAttribute('data-skd-first'));
|
|
59
|
+
const last = Number(item.getAttribute('data-skd-last'));
|
|
60
|
+
item.classList.toggle('skd-cur', frame >= first && frame <= last);
|
|
61
|
+
item.classList.toggle('skd-past', frame > last);
|
|
62
|
+
}
|
|
63
|
+
for (const el of sketch.querySelectorAll('[data-skd-on]')) {
|
|
64
|
+
el.classList.toggle('skd-on', el.getAttribute('data-skd-on').split(' ').includes(String(frame)));
|
|
65
|
+
}
|
|
66
|
+
const prev = sketch.querySelector('.skd-prev');
|
|
67
|
+
const next = sketch.querySelector('.skd-next');
|
|
68
|
+
if (prev)
|
|
69
|
+
prev.disabled = frame === 0;
|
|
70
|
+
if (next)
|
|
71
|
+
next.disabled = frame === count - 1;
|
|
72
|
+
remembered.set(sketches().indexOf(sketch), frame);
|
|
73
|
+
drawLines(sketch);
|
|
74
|
+
}
|
|
75
|
+
/** Starts each sketch not seen yet at its first frame, or the frame it showed before being rendered again. */
|
|
76
|
+
function start() {
|
|
77
|
+
if (address() !== page) {
|
|
78
|
+
remembered.clear();
|
|
79
|
+
page = address();
|
|
80
|
+
}
|
|
81
|
+
for (const sketch of watched) {
|
|
82
|
+
if (sketch.isConnected)
|
|
83
|
+
continue;
|
|
84
|
+
resized?.unobserve(sketch);
|
|
85
|
+
watched.delete(sketch);
|
|
86
|
+
}
|
|
87
|
+
sketches().forEach((sketch, i) => {
|
|
88
|
+
if (sketch.hasAttribute('data-skd-live') || !Number(sketch.getAttribute('data-skd-frames')))
|
|
89
|
+
return;
|
|
90
|
+
sketch.setAttribute('data-skd-live', '');
|
|
91
|
+
go(sketch, Math.min(remembered.get(i) ?? 0, Number(sketch.getAttribute('data-skd-frames')) - 1));
|
|
92
|
+
resized?.observe(sketch);
|
|
93
|
+
watched.add(sketch);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
const click = (e) => {
|
|
97
|
+
const el = e.target;
|
|
98
|
+
const sketch = el?.closest?.(SKETCH);
|
|
99
|
+
if (!sketch?.hasAttribute('data-skd-live') || el.closest('.skd-mark'))
|
|
100
|
+
return;
|
|
101
|
+
const item = el.closest('li[data-skd-at]');
|
|
102
|
+
const step = el.closest('li[data-skd-first]');
|
|
103
|
+
if (item)
|
|
104
|
+
go(sketch, Number(item.getAttribute('data-skd-at')));
|
|
105
|
+
else if (step)
|
|
106
|
+
go(sketch, Number(step.getAttribute('data-skd-first')));
|
|
107
|
+
else if (el.closest('.skd-prev'))
|
|
108
|
+
go(sketch, current(sketch) - 1);
|
|
109
|
+
else if (el.closest('.skd-next'))
|
|
110
|
+
go(sketch, current(sketch) + 1);
|
|
111
|
+
};
|
|
112
|
+
const key = (e) => {
|
|
113
|
+
const event = e;
|
|
114
|
+
const sketch = event.target?.closest?.(SKETCH);
|
|
115
|
+
if (!sketch?.hasAttribute('data-skd-live') || event.altKey || event.ctrlKey || event.metaKey)
|
|
116
|
+
return;
|
|
117
|
+
const count = Number(sketch.getAttribute('data-skd-frames'));
|
|
118
|
+
const frame = {
|
|
119
|
+
ArrowRight: current(sketch) + 1,
|
|
120
|
+
ArrowDown: current(sketch) + 1,
|
|
121
|
+
ArrowLeft: current(sketch) - 1,
|
|
122
|
+
ArrowUp: current(sketch) - 1,
|
|
123
|
+
Home: 0,
|
|
124
|
+
End: count - 1,
|
|
125
|
+
}[event.key];
|
|
126
|
+
if (frame === undefined)
|
|
127
|
+
return;
|
|
128
|
+
event.preventDefault();
|
|
129
|
+
go(sketch, frame);
|
|
130
|
+
// Focus follows the current step or substep, so a screen reader reads it out.
|
|
131
|
+
sketch.querySelector(`li[data-skd-at="${current(sketch)}"] > .skd-go`)?.focus({ preventScroll: true });
|
|
132
|
+
};
|
|
133
|
+
// A table that scrolls sideways moves its rows under the lines. Scrolling doesn't bubble, so this listens while it is captured.
|
|
134
|
+
const scroll = (e) => {
|
|
135
|
+
const el = e.target;
|
|
136
|
+
if (el?.closest?.('.skd-table, .skd-files'))
|
|
137
|
+
drawLines(el.closest(SKETCH));
|
|
138
|
+
};
|
|
139
|
+
start();
|
|
140
|
+
const observer = new MutationObserver(start);
|
|
141
|
+
observer.observe(root instanceof Document ? root.documentElement : root, { childList: true, subtree: true });
|
|
142
|
+
root.addEventListener('click', click);
|
|
143
|
+
root.addEventListener('keydown', key);
|
|
144
|
+
root.addEventListener('scroll', scroll, true);
|
|
145
|
+
return () => {
|
|
146
|
+
observer.disconnect();
|
|
147
|
+
resized?.disconnect();
|
|
148
|
+
root.removeEventListener('click', click);
|
|
149
|
+
root.removeEventListener('keydown', key);
|
|
150
|
+
root.removeEventListener('scroll', scroll, true);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Draws a line from the current step or substep to each row its reads find, or to the column names of a
|
|
155
|
+
* table where a read finds nothing. The lines are an SVG over the sketch, and start at the right edge of
|
|
156
|
+
* the step, level with the step or substep. Where the list is above the tables, they would cross it, so
|
|
157
|
+
* there are none.
|
|
158
|
+
*/
|
|
159
|
+
function drawLines(sketch) {
|
|
160
|
+
let svg = sketch.querySelector(':scope > svg.skd-lines');
|
|
161
|
+
const item = sketch.querySelector(`li[data-skd-at="${current(sketch)}"]`);
|
|
162
|
+
const story = sketch.querySelector('.skd-story');
|
|
163
|
+
const board = sketch.querySelector('.skd-board');
|
|
164
|
+
const targets = new Set();
|
|
165
|
+
for (const row of sketch.querySelectorAll('tbody:not([hidden]) tr.skd-read'))
|
|
166
|
+
targets.add(row.cells[0]);
|
|
167
|
+
for (const badge of sketch.querySelectorAll('.skd-nomatch.skd-on')) {
|
|
168
|
+
const names = badge.closest('[data-skd-table]')?.querySelector('thead th');
|
|
169
|
+
if (names)
|
|
170
|
+
targets.add(names);
|
|
171
|
+
}
|
|
172
|
+
svg?.replaceChildren();
|
|
173
|
+
if (!item || !story || !board || !targets.size)
|
|
174
|
+
return;
|
|
175
|
+
const beside = board.getBoundingClientRect().left >= story.getBoundingClientRect().right;
|
|
176
|
+
if (!beside)
|
|
177
|
+
return;
|
|
178
|
+
if (!svg) {
|
|
179
|
+
svg = sketch.ownerDocument.createElementNS(SVG, 'svg');
|
|
180
|
+
svg.setAttribute('class', 'skd-lines');
|
|
181
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
182
|
+
sketch.append(svg);
|
|
183
|
+
}
|
|
184
|
+
// In the sketch's coordinates: absolute positions start inside its border.
|
|
185
|
+
const box = sketch.getBoundingClientRect();
|
|
186
|
+
const left = box.left + sketch.clientLeft;
|
|
187
|
+
const top = box.top + sketch.clientTop;
|
|
188
|
+
const from = item.querySelector(':scope > .skd-go').getBoundingClientRect();
|
|
189
|
+
const sx = item.closest('.skd-step').getBoundingClientRect().right - left + 2;
|
|
190
|
+
const sy = from.top + from.height / 2 - top;
|
|
191
|
+
let lines = '';
|
|
192
|
+
let arrows = '';
|
|
193
|
+
for (const target of targets) {
|
|
194
|
+
const r = target.getBoundingClientRect();
|
|
195
|
+
// A row scrolled out of its table's view: the line stops at the table's edge.
|
|
196
|
+
const view = target.closest('.skd-table, .skd-files')?.getBoundingClientRect();
|
|
197
|
+
const ex = Math.max(r.left, view?.left ?? r.left) - left - 3;
|
|
198
|
+
const ey = r.top + r.height / 2 - top;
|
|
199
|
+
// Leaves and arrives level. Handles any longer than half the way would loop where the rows are close.
|
|
200
|
+
const dx = Math.max(0, ex - sx) / 2;
|
|
201
|
+
lines += `M${sx},${sy}C${sx + dx},${sy} ${ex - dx},${ey} ${ex},${ey}`;
|
|
202
|
+
arrows += `M${ex - 7},${ey - 3.5}L${ex},${ey}L${ex - 7},${ey + 3.5}Z`;
|
|
203
|
+
}
|
|
204
|
+
for (const [cls, d] of [
|
|
205
|
+
['skd-line', lines],
|
|
206
|
+
['skd-arrow', arrows],
|
|
207
|
+
]) {
|
|
208
|
+
const path = sketch.ownerDocument.createElementNS(SVG, 'path');
|
|
209
|
+
path.setAttribute('class', cls);
|
|
210
|
+
path.setAttribute('d', d);
|
|
211
|
+
svg.append(path);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function highlightReferences(root) {
|
|
215
|
+
const lit = [];
|
|
216
|
+
const clear = () => lit.splice(0).forEach((el) => el.classList.remove('skd-hl'));
|
|
217
|
+
const light = (els) => {
|
|
218
|
+
for (const el of els) {
|
|
219
|
+
el.classList.add('skd-hl');
|
|
220
|
+
lit.push(el);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const over = (e) => {
|
|
224
|
+
clear();
|
|
225
|
+
const el = e.target;
|
|
226
|
+
// Only the rows of the step that shows.
|
|
227
|
+
const shown = el?.closest?.(SKETCH)?.querySelectorAll('tbody:not([hidden])');
|
|
228
|
+
if (!shown)
|
|
229
|
+
return;
|
|
230
|
+
const within = (selector) => [...shown].flatMap((body) => [...body.querySelectorAll(selector)]);
|
|
231
|
+
const ref = el.closest('[data-skd-ref]');
|
|
232
|
+
const row = el.closest('tr[data-skd-row]');
|
|
233
|
+
if (ref)
|
|
234
|
+
light([ref, ...within(`tr[data-skd-row="${CSS.escape(ref.getAttribute('data-skd-ref'))}"]`)]);
|
|
235
|
+
else if (row)
|
|
236
|
+
light(within(`[data-skd-ref="${CSS.escape(row.getAttribute('data-skd-row'))}"]`));
|
|
237
|
+
};
|
|
238
|
+
const out = (e) => {
|
|
239
|
+
const to = e.relatedTarget;
|
|
240
|
+
if (!to || !e.target.closest?.(SKETCH)?.contains(to))
|
|
241
|
+
clear();
|
|
242
|
+
};
|
|
243
|
+
root.addEventListener('mouseover', over);
|
|
244
|
+
root.addEventListener('mouseout', out);
|
|
245
|
+
return () => {
|
|
246
|
+
clear();
|
|
247
|
+
root.removeEventListener('mouseover', over);
|
|
248
|
+
root.removeEventListener('mouseout', out);
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function showDocs(root) {
|
|
252
|
+
const doc = root instanceof Document ? root : root.ownerDocument;
|
|
253
|
+
const win = doc.defaultView;
|
|
254
|
+
const pop = new Popover(doc);
|
|
255
|
+
/** The step or table name whose explanation shows, or is about to. */
|
|
256
|
+
let anchor;
|
|
257
|
+
/** Whether a click on the anchor's mark keeps the explanation open. */
|
|
258
|
+
let pinned = false;
|
|
259
|
+
let timer;
|
|
260
|
+
const owner = (e) => e.target?.closest?.('[data-skd-doc]') ?? null;
|
|
261
|
+
function show() {
|
|
262
|
+
clearTimeout(timer);
|
|
263
|
+
timer = undefined;
|
|
264
|
+
if (!anchor)
|
|
265
|
+
return;
|
|
266
|
+
pop.show((anchor.getAttribute('data-skd-doc') ?? '').split('\n\n').map((paragraph) => {
|
|
267
|
+
const p = doc.createElement('p');
|
|
268
|
+
p.textContent = paragraph;
|
|
269
|
+
return p;
|
|
270
|
+
}));
|
|
271
|
+
// Beside a step or substep, so the list stays readable; under a table's name.
|
|
272
|
+
const box = anchor.getBoundingClientRect();
|
|
273
|
+
if (anchor.closest('.skd-steps'))
|
|
274
|
+
pop.beside(box);
|
|
275
|
+
else
|
|
276
|
+
pop.below(box, box.left);
|
|
277
|
+
anchor.setAttribute('aria-describedby', pop.id);
|
|
278
|
+
anchor.classList.add('skd-doc-on');
|
|
279
|
+
}
|
|
280
|
+
function hide() {
|
|
281
|
+
clearTimeout(timer);
|
|
282
|
+
timer = undefined;
|
|
283
|
+
anchor?.removeAttribute('aria-describedby');
|
|
284
|
+
anchor?.classList.remove('skd-doc-on');
|
|
285
|
+
anchor = undefined;
|
|
286
|
+
pinned = false;
|
|
287
|
+
pop.hide();
|
|
288
|
+
}
|
|
289
|
+
function start(el, delay) {
|
|
290
|
+
if (el === anchor)
|
|
291
|
+
return;
|
|
292
|
+
// Moving from one explanation to the next doesn't wait again.
|
|
293
|
+
const showing = !!anchor && timer === undefined;
|
|
294
|
+
hide();
|
|
295
|
+
anchor = el;
|
|
296
|
+
if (delay && !showing)
|
|
297
|
+
timer = setTimeout(show, delay);
|
|
298
|
+
else
|
|
299
|
+
show();
|
|
300
|
+
}
|
|
301
|
+
const over = (e) => {
|
|
302
|
+
if (pinned)
|
|
303
|
+
return;
|
|
304
|
+
const el = owner(e);
|
|
305
|
+
if (el)
|
|
306
|
+
start(el, POPOVER_DELAY);
|
|
307
|
+
else if (anchor && !anchor.isConnected)
|
|
308
|
+
hide();
|
|
309
|
+
};
|
|
310
|
+
const out = (e) => {
|
|
311
|
+
if (!pinned && anchor && !anchor.contains(e.relatedTarget))
|
|
312
|
+
hide();
|
|
313
|
+
};
|
|
314
|
+
const click = (e) => {
|
|
315
|
+
const mark = e.target?.closest?.('.skd-mark');
|
|
316
|
+
const el = mark?.closest('[data-skd-doc]');
|
|
317
|
+
if (el && !(pinned && el === anchor)) {
|
|
318
|
+
if (el !== anchor) {
|
|
319
|
+
hide();
|
|
320
|
+
anchor = el;
|
|
321
|
+
}
|
|
322
|
+
show();
|
|
323
|
+
pinned = true;
|
|
324
|
+
}
|
|
325
|
+
else
|
|
326
|
+
hide();
|
|
327
|
+
};
|
|
328
|
+
// A click elsewhere on the page, outside root, also closes a pinned explanation.
|
|
329
|
+
const away = (e) => {
|
|
330
|
+
if (pinned && !(root instanceof Document) && !root.contains(e.target))
|
|
331
|
+
hide();
|
|
332
|
+
};
|
|
333
|
+
const key = (e) => {
|
|
334
|
+
if (e.key === 'Escape')
|
|
335
|
+
hide();
|
|
336
|
+
};
|
|
337
|
+
root.addEventListener('mouseover', over);
|
|
338
|
+
root.addEventListener('mouseout', out);
|
|
339
|
+
root.addEventListener('click', click);
|
|
340
|
+
root.addEventListener('keydown', key);
|
|
341
|
+
doc.addEventListener('click', away);
|
|
342
|
+
win?.addEventListener('scroll', hide, true);
|
|
343
|
+
win?.addEventListener('resize', hide);
|
|
344
|
+
return () => {
|
|
345
|
+
hide();
|
|
346
|
+
pop.remove();
|
|
347
|
+
root.removeEventListener('mouseover', over);
|
|
348
|
+
root.removeEventListener('mouseout', out);
|
|
349
|
+
root.removeEventListener('click', click);
|
|
350
|
+
root.removeEventListener('keydown', key);
|
|
351
|
+
doc.removeEventListener('click', away);
|
|
352
|
+
win?.removeEventListener('scroll', hide, true);
|
|
353
|
+
win?.removeEventListener('resize', hide);
|
|
354
|
+
};
|
|
355
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Renders a data sketch: the steps of its story beside its tables. The story plays in frames: one for
|
|
3
|
+
* each substep, and one for each step without substeps. Every frame's rows are in the markup, one table
|
|
4
|
+
* body per frame, and all but one are hidden. Without a script the last frame shows; enhance()
|
|
5
|
+
* (data/enhance.ts) starts at the first and switches frames by toggling which body shows. What else
|
|
6
|
+
* shows at some frames only, such as a "no match" badge, lists them in `data-skd-on`, and has the class
|
|
7
|
+
* `skd-on` while one of them shows.
|
|
8
|
+
*
|
|
9
|
+
* Nothing may move between steps, and a renderer can't measure the page. So each table reserves room
|
|
10
|
+
* for the most rows it ever holds, and each column and table gets a width estimated generously in `ch`
|
|
11
|
+
* from the widest values across all steps. A column whose values are wider than estimated still grows
|
|
12
|
+
* rather than cut them off. Only values longer than the cap are cut, with the whole value in the cell's
|
|
13
|
+
* tooltip.
|
|
14
|
+
*
|
|
15
|
+
* A folder is drawn like a table, with its name in a tab and a path for each row's id. A group is a frame
|
|
16
|
+
* around its tables, drawn where its first table is declared.
|
|
17
|
+
*/
|
|
18
|
+
import { replayData, } from '@skeudo/core';
|
|
19
|
+
import { h, text } from '../shared/hast.js';
|
|
20
|
+
import { proportionalWidth, textWidth } from '../shared/width.js';
|
|
21
|
+
/** The longest value shown whole, in `ch`. data.css cuts longer ones at the same width. */
|
|
22
|
+
const MAX_WIDTH = 24;
|
|
23
|
+
/** Horizontal padding of a cell, in pixels: data.css sets 8px on each side. */
|
|
24
|
+
const CELL_PADDING = 16;
|
|
25
|
+
/** The width a reference's chip adds to its text, in `ch`: its border and padding. */
|
|
26
|
+
const CHIP = 1.8;
|
|
27
|
+
/**
|
|
28
|
+
* The width of a character of the monospace ids and references, in `ch` of the values' font. They are
|
|
29
|
+
* smaller, but a monospace character is wider than a proportional font's `0` in some fonts.
|
|
30
|
+
*/
|
|
31
|
+
const MONO = 1.2;
|
|
32
|
+
/** What an empty value shows. */
|
|
33
|
+
const EMPTY = '–';
|
|
34
|
+
/** Builds a data sketch as a hast tree: one `div.skd` element. */
|
|
35
|
+
export function dataToHast(sketch) {
|
|
36
|
+
const frames = replayData(sketch);
|
|
37
|
+
const shown = frames.length - 1;
|
|
38
|
+
const reads = frames.some((f) => f.reads.length);
|
|
39
|
+
const story = frames.length
|
|
40
|
+
? [h('div', { className: ['skd-story'] }, [stepsHast(sketch, frames, shown), navHast(), legendHast(reads)])]
|
|
41
|
+
: [];
|
|
42
|
+
const folders = new Set(sketch.tables.filter((t) => t.kind === 'folder').map((t) => t.name));
|
|
43
|
+
const groupOf = new Map(sketch.groups.flatMap((g) => g.tables.map((name) => [name, g])));
|
|
44
|
+
const items = [];
|
|
45
|
+
const drawn = new Set();
|
|
46
|
+
for (const table of sketch.tables) {
|
|
47
|
+
const group = groupOf.get(table.name);
|
|
48
|
+
if (!group)
|
|
49
|
+
items.push(tableHast(table, frames, shown, folders));
|
|
50
|
+
else if (!drawn.has(group)) {
|
|
51
|
+
drawn.add(group);
|
|
52
|
+
const tables = sketch.tables.filter((t) => groupOf.get(t.name) === group);
|
|
53
|
+
items.push(groupHast(group, tables.map((t) => tableHast(t, frames, shown, folders))));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const board = h('div', { className: ['skd-board'] }, items);
|
|
57
|
+
const warnings = sketch.diagnostics.length
|
|
58
|
+
? [h('ul', { className: ['skd-warnings'] }, sketch.diagnostics.map((d) => h('li', {}, text(`Line ${d.line}: ${d.message}`))))]
|
|
59
|
+
: [];
|
|
60
|
+
return h('div', {
|
|
61
|
+
className: ['skd'],
|
|
62
|
+
dataSkd: true,
|
|
63
|
+
dataSkdFrames: frames.length,
|
|
64
|
+
...(frames.length ? { dataSkdFrame: shown, tabIndex: -1 } : {}),
|
|
65
|
+
}, [h('div', { className: story.length ? ['skd-body'] : ['skd-body', 'skd-no-story'] }, [...story, board]), ...warnings]);
|
|
66
|
+
}
|
|
67
|
+
/** A group's frame, with its label on top like a table's name. */
|
|
68
|
+
function groupHast(group, tables) {
|
|
69
|
+
const long = proportionalWidth(group.label) > MAX_WIDTH;
|
|
70
|
+
return h('section', { className: ['skd-group'], dataSkdGroup: group.label }, [
|
|
71
|
+
h('div', { className: ['skd-group-name'], ...docProps(group.doc) }, [
|
|
72
|
+
h('span', long ? { title: group.label } : {}, text(group.label)),
|
|
73
|
+
...markHast(group.doc),
|
|
74
|
+
]),
|
|
75
|
+
h('div', { className: ['skd-group-body'] }, tables),
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The story. A step without substeps is one item, which carries its frame in `data-skd-at`. A step with
|
|
80
|
+
* substeps lists them under its title, each with its frame, and carries the range of its frames in
|
|
81
|
+
* `data-skd-first` and `data-skd-last`, so it stays current while one of them shows.
|
|
82
|
+
*/
|
|
83
|
+
function stepsHast(sketch, frames, shown) {
|
|
84
|
+
const state = (first, last) => (shown >= first && shown <= last ? ['skd-cur'] : shown > last ? ['skd-past'] : []);
|
|
85
|
+
const current = (at) => (at === shown ? { ariaCurrent: 'step' } : {});
|
|
86
|
+
return h('ol', { className: ['skd-steps'] }, sketch.steps.map((step, i) => {
|
|
87
|
+
const first = frames.findIndex((f) => f.step === i);
|
|
88
|
+
const title = (props) => h('button', { type: 'button', className: ['skd-go'], ...props }, [
|
|
89
|
+
h('span', { className: ['skd-n'] }, text(String(i + 1))),
|
|
90
|
+
h('span', { className: ['skd-title'] }, text(step.title)),
|
|
91
|
+
]);
|
|
92
|
+
if (!step.substeps.length) {
|
|
93
|
+
return h('li', { className: ['skd-step', ...state(first, first)], dataSkdAt: first, ...docProps(step.doc) }, [
|
|
94
|
+
title(current(first)),
|
|
95
|
+
...markHast(step.doc),
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
const last = first + step.substeps.length - 1;
|
|
99
|
+
return h('li', { className: ['skd-step', 'skd-multi', ...state(first, last)], dataSkdFirst: first, dataSkdLast: last }, [
|
|
100
|
+
h('div', { className: ['skd-head'], ...docProps(step.doc) }, [title({}), ...markHast(step.doc)]),
|
|
101
|
+
h('ol', { className: ['skd-subs'] }, step.substeps.map((sub, k) => h('li', { className: ['skd-sub', ...state(first + k, first + k)], dataSkdAt: first + k, ...docProps(sub.doc) }, [
|
|
102
|
+
h('button', { type: 'button', className: ['skd-go'], ...current(first + k) }, [h('span', { className: ['skd-title'] }, text(sub.title))]),
|
|
103
|
+
...markHast(sub.doc),
|
|
104
|
+
]))),
|
|
105
|
+
]);
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
function navHast() {
|
|
109
|
+
return h('div', { className: ['skd-nav'] }, [
|
|
110
|
+
h('button', { type: 'button', className: ['skd-prev'], ariaLabel: 'Previous step' }, text('◀')),
|
|
111
|
+
h('button', { type: 'button', className: ['skd-next'], ariaLabel: 'Next step' }, text('▶')),
|
|
112
|
+
h('span', { className: ['skd-keys'] }, text('or ← →')),
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
/** The colors of changes, and the ring of a row read when the sketch has reads. */
|
|
116
|
+
function legendHast(reads) {
|
|
117
|
+
const item = (cls, label) => h('span', { className: [cls] }, text(label));
|
|
118
|
+
return h('div', { className: ['skd-legend'] }, [
|
|
119
|
+
item('skd-l-new', 'new'),
|
|
120
|
+
item('skd-l-chg', 'changed'),
|
|
121
|
+
item('skd-l-del', 'deleted'),
|
|
122
|
+
...(reads ? [item('skd-l-read', 'read')] : []),
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
125
|
+
/** The properties of an element that shows at some frames only, with the class it starts with. */
|
|
126
|
+
function onProps(at, shown, cls = []) {
|
|
127
|
+
const all = [...cls, ...(at.includes(shown) ? ['skd-on'] : [])];
|
|
128
|
+
return { ...(all.length ? { className: all } : {}), ...(at.length ? { dataSkdOn: at.join(' ') } : {}) };
|
|
129
|
+
}
|
|
130
|
+
/** A table or step's explanation, for the popover: its paragraphs, separated by a blank line. */
|
|
131
|
+
function docProps(doc) {
|
|
132
|
+
return doc.length ? { dataSkdDoc: doc.join('\n\n') } : {};
|
|
133
|
+
}
|
|
134
|
+
/** The mark that pins an explanation. Its "i" is drawn by data.css, so copying the page leaves it out. */
|
|
135
|
+
function markHast(doc) {
|
|
136
|
+
return doc.length ? [h('button', { type: 'button', className: ['skd-mark'], ariaLabel: 'Explanation' })] : [];
|
|
137
|
+
}
|
|
138
|
+
/** The last part of a path, which a reference to a file shows. */
|
|
139
|
+
const fileName = (path) => path.slice(path.lastIndexOf('/') + 1);
|
|
140
|
+
function tableHast(table, frames, shown, folders) {
|
|
141
|
+
const folder = table.kind === 'folder';
|
|
142
|
+
const states = frames.map((f) => f.state);
|
|
143
|
+
/** The reads of this table at each frame. */
|
|
144
|
+
const reads = frames.map((f) => f.reads.filter((r) => r.table === table.name));
|
|
145
|
+
/** The frames where the predicate holds for one of this table's reads. */
|
|
146
|
+
const when = (test) => reads.flatMap((list, i) => (list.some(test) ? [i] : []));
|
|
147
|
+
// A read that finds nothing says so beside the table's name. A lookup's columns are underlined.
|
|
148
|
+
const missed = when((r) => !r.found.length);
|
|
149
|
+
const badge = missed.length ? [h('span', onProps(missed, shown, ['skd-nomatch']), text('no match'))] : [];
|
|
150
|
+
// Rounded first, so the table's width is exactly the sum of its columns' and no width is left over to share out.
|
|
151
|
+
const widths = columnWidths(table, states, folders).map((w) => Math.round(w * 10) / 10);
|
|
152
|
+
const slots = Math.max(1, ...states.map((s) => s.get(table.name)?.length ?? 0));
|
|
153
|
+
const width = (w, columns = 1) => `calc(${Math.round(w * 10) / 10}ch + ${CELL_PADDING * columns}px)`;
|
|
154
|
+
// The table's own width keeps a narrow space from squeezing its columns, which it would do differently
|
|
155
|
+
// at each step. The table scrolls instead.
|
|
156
|
+
const total = width(widths.reduce((a, b) => a + b, 0), widths.length);
|
|
157
|
+
// A table with no story still draws its header and an empty body.
|
|
158
|
+
const bodies = states.length ? states : [new Map()];
|
|
159
|
+
const grid = h('table', { className: ['skd-grid'], style: `width:${total}` }, [
|
|
160
|
+
h('colgroup', {}, widths.map((w) => h('col', { style: `width:${width(w)}` }))),
|
|
161
|
+
h('thead', {}, [
|
|
162
|
+
h('tr', {}, [h('th', {}), ...table.columns.map((c) => h('th', onProps(when((r) => !!r.match && c.name in r.match), shown), text(c.name)))]),
|
|
163
|
+
]),
|
|
164
|
+
...bodies.map((state, i) => {
|
|
165
|
+
const rows = state.get(table.name) ?? [];
|
|
166
|
+
const filler = Array.from({ length: slots - rows.length }, (_, k) => slotHast(table, k === 0 && !rows.length));
|
|
167
|
+
const cells = rows.map((r, k) => rowHast(table, r, state, folders, folder ? rows[k - 1]?.id : undefined, reads[i] ?? []));
|
|
168
|
+
return h('tbody', { dataSkdAt: i, hidden: i !== bodies.length - 1 }, [...cells, ...filler]);
|
|
169
|
+
}),
|
|
170
|
+
]);
|
|
171
|
+
if (!folder) {
|
|
172
|
+
return h('section', { className: ['skd-table'], dataSkdTable: table.name }, [
|
|
173
|
+
h('div', { className: ['skd-name'], ...docProps(table.doc) }, [h('span', {}, text(table.name)), ...markHast(table.doc), ...badge]),
|
|
174
|
+
grid,
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
// A folder's name ends with a slash, in a tab on top of its files.
|
|
178
|
+
return h('section', { className: ['skd-folder'], dataSkdTable: table.name }, [
|
|
179
|
+
h('div', { className: ['skd-name'], ...docProps(table.doc) }, [
|
|
180
|
+
h('span', {}, [...text(table.name), h('span', { className: ['skd-slash'] }, text('/'))]),
|
|
181
|
+
...markHast(table.doc),
|
|
182
|
+
...badge,
|
|
183
|
+
]),
|
|
184
|
+
h('div', { className: ['skd-files'] }, [grid]),
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A row. In a folder, `above` is the path on the line above, whose folders this row's path dims. A row
|
|
189
|
+
* one of `reads` finds is ringed, and what the read matched on is marked: its id, its path's prefix, or
|
|
190
|
+
* the cells of the lookup's columns.
|
|
191
|
+
*/
|
|
192
|
+
function rowHast(table, row, state, folders, above, reads) {
|
|
193
|
+
const hits = reads.filter((r) => r.found.includes(row.id));
|
|
194
|
+
const cls = [...(row.status === 'new' ? ['skd-new'] : row.status === 'removed' ? ['skd-removed'] : []), ...(hits.length ? ['skd-read'] : [])];
|
|
195
|
+
const byId = hits.some((r) => r.id !== undefined);
|
|
196
|
+
const prefix = hits.reduce((longest, r) => (r.prefix && r.prefix.length > longest.length ? r.prefix : longest), '');
|
|
197
|
+
const looked = new Set(hits.flatMap((r) => Object.keys(r.match ?? {})));
|
|
198
|
+
return h('tr', { ...(cls.length ? { className: cls } : {}), dataSkdRow: `${table.name}:${row.id}` }, [
|
|
199
|
+
table.kind === 'folder' ? pathHast(row.id, above, prefix, byId) : h('td', { className: ['skd-id', ...(byId ? ['skd-read-key'] : [])] }, text(row.id)),
|
|
200
|
+
...table.columns.map((c) => cellHast(c, row, state, folders, looked.has(c.name))),
|
|
201
|
+
]);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* A file's path, with the folders it shares with the path above dimmed, so the layout of the keys stands
|
|
205
|
+
* out. A prefix a read listed is marked instead, and so is the whole path when a read named it.
|
|
206
|
+
*/
|
|
207
|
+
function pathHast(path, above, prefix, read) {
|
|
208
|
+
const parts = path.split('/');
|
|
209
|
+
const before = above?.split('/') ?? [];
|
|
210
|
+
let shared = 0;
|
|
211
|
+
// The file's own name is never dimmed.
|
|
212
|
+
while (shared < Math.min(parts.length, before.length) - 1 && parts[shared] === before[shared])
|
|
213
|
+
shared++;
|
|
214
|
+
const head = parts.slice(0, shared).map((p) => `${p}/`).join('');
|
|
215
|
+
return h('td', { className: ['skd-id', 'skd-path', ...(read ? ['skd-read-key'] : [])] }, [
|
|
216
|
+
...(prefix ? [h('span', { className: ['skd-read-key'] }, text(prefix))] : []),
|
|
217
|
+
...(head.length > prefix.length ? [h('span', { className: ['skd-same'] }, text(head.slice(prefix.length)))] : []),
|
|
218
|
+
...text(path.slice(Math.max(head.length, prefix.length))),
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
/** A cell. `read` marks the cell of a column a read looked the row up by. */
|
|
222
|
+
function cellHast(column, row, state, folders, read) {
|
|
223
|
+
const value = row.values[column.name] ?? null;
|
|
224
|
+
const changed = column.name in row.was;
|
|
225
|
+
// A reference to a file shows the file's name, with the whole path in the tooltip.
|
|
226
|
+
const file = value !== null && column.ref !== undefined && folders.has(column.ref) && value.includes('/');
|
|
227
|
+
const long = value !== null && !file && proportionalWidth(value) > MAX_WIDTH;
|
|
228
|
+
const title = [long || file ? value : '', changed ? `was ${row.was[column.name] ?? EMPTY}` : ''].filter(Boolean).join('\n');
|
|
229
|
+
const cls = [...(changed ? ['skd-chg'] : []), ...(read ? ['skd-read-key'] : [])];
|
|
230
|
+
const props = { ...(cls.length ? { className: cls } : {}), ...(title ? { title } : {}) };
|
|
231
|
+
return h('td', props, valueHast(column, value, state, long, file));
|
|
232
|
+
}
|
|
233
|
+
function valueHast(column, value, state, long, file) {
|
|
234
|
+
if (value === null)
|
|
235
|
+
return [h('span', { className: ['skd-null'] }, text(EMPTY))];
|
|
236
|
+
if (long)
|
|
237
|
+
return [h('span', { className: ['skd-long'] }, text(value))];
|
|
238
|
+
if (column.ref === undefined || value === '')
|
|
239
|
+
return text(value);
|
|
240
|
+
const target = state.get(column.ref);
|
|
241
|
+
// A reference to a row that isn't there at this step is drawn broken; the warnings say why.
|
|
242
|
+
const broken = !target?.some((r) => r.id === value && r.status !== 'removed');
|
|
243
|
+
const shown = file ? fileName(value) : value;
|
|
244
|
+
return [h('span', { className: broken ? ['skd-ref', 'skd-broken'] : ['skd-ref'], dataSkdRef: `${column.ref}:${value}` }, text(shown))];
|
|
245
|
+
}
|
|
246
|
+
/** A reserved row, so the table keeps its height. The first one says the table is empty when it is. */
|
|
247
|
+
function slotHast(table, empty) {
|
|
248
|
+
return h('tr', { className: ['skd-slot'] }, [
|
|
249
|
+
h('td', { className: ['skd-id'] }, text(empty ? 'empty' : '')),
|
|
250
|
+
...table.columns.map(() => h('td', {})),
|
|
251
|
+
]);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* The minimum width of each column, the ids first, in `ch` of the values' font: the widest it gets in
|
|
255
|
+
* any step.
|
|
256
|
+
*/
|
|
257
|
+
function columnWidths(table, states, folders) {
|
|
258
|
+
const rows = states.flatMap((s) => s.get(table.name) ?? []);
|
|
259
|
+
const id = Math.max(textWidth('empty'), ...rows.map((r) => textWidth(r.id))) * MONO;
|
|
260
|
+
const columns = table.columns.map((c) => {
|
|
261
|
+
const files = c.ref !== undefined && folders.has(c.ref);
|
|
262
|
+
const values = rows.map((r) => {
|
|
263
|
+
const v = r.values[c.name] ?? null;
|
|
264
|
+
if (v === null)
|
|
265
|
+
return 1;
|
|
266
|
+
if (c.ref === undefined || v === '')
|
|
267
|
+
return Math.min(MAX_WIDTH, proportionalWidth(v));
|
|
268
|
+
return textWidth(files ? fileName(v) : v) * MONO + CHIP;
|
|
269
|
+
});
|
|
270
|
+
// The header's font is smaller, so the values' estimate is enough for it.
|
|
271
|
+
return Math.max(proportionalWidth(c.name), ...values);
|
|
272
|
+
});
|
|
273
|
+
return [id, ...columns];
|
|
274
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Sketch } from '@skeudo/core';
|
|
2
|
+
import type { Element as HastElement } from 'hast';
|
|
3
|
+
import { type CodeHtmlOptions } from './code/render.ts';
|
|
4
|
+
export { escapeHtml } from './shared/hast.ts';
|
|
5
|
+
export { tagHast, tagHtml } from './code/render.ts';
|
|
6
|
+
export { repoHref, srcBaseHref, type GitHost, type SrcLink } from './code/links.ts';
|
|
7
|
+
/** Options for rendering sketches. Each view reads the options it knows; the data view has none. */
|
|
8
|
+
export interface HtmlOptions extends CodeHtmlOptions {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds a sketch (source text or an already parsed sketch) as a hast tree: one element, the same
|
|
12
|
+
* markup toHtml returns.
|
|
13
|
+
*/
|
|
14
|
+
export declare function toHast(input: string | Sketch, opts?: HtmlOptions): HastElement;
|
|
15
|
+
/** Renders a sketch (source text or an already parsed sketch) to an HTML string. */
|
|
16
|
+
export declare function toHtml(input: string | Sketch, opts?: HtmlOptions): string;
|
|
17
|
+
/**
|
|
18
|
+
* Makes rendered sketches interactive.
|
|
19
|
+
*
|
|
20
|
+
* In code sketches:
|
|
21
|
+
* - Hovering a binding, parameter or labeled name highlights all its uses within the same sketch;
|
|
22
|
+
* clicking pins the highlight.
|
|
23
|
+
* - Hovering or focusing a call or a line with a `::doc` shows its explanation in a popover. On a
|
|
24
|
+
* block header, the block is highlighted with it.
|
|
25
|
+
*
|
|
26
|
+
* In data sketches:
|
|
27
|
+
* - The story starts at its first step. Clicking a step shows its rows, and the arrow keys step through
|
|
28
|
+
* the story while the sketch has focus.
|
|
29
|
+
* - Hovering a reference highlights the row it points at, and hovering a row the references to it.
|
|
30
|
+
* - Resting on a step or a table's name shows its explanation; clicking its mark keeps it open.
|
|
31
|
+
*
|
|
32
|
+
* Listeners sit on `root`, so sketches rendered under it later work too. Returns a function that
|
|
33
|
+
* removes them.
|
|
34
|
+
*/
|
|
35
|
+
export declare function enhance(root?: HTMLElement | Document): () => void;
|