decklight 0.10.0 → 0.11.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/NOTICE +2 -0
- package/PLATFORM.md +17 -0
- package/README.md +53 -25
- package/SPEC.md +1239 -67
- package/cli/agents.mjs +139 -15
- package/cli/bundle.mjs +68 -2
- package/cli/cast.mjs +45 -14
- package/cli/check-fixes.mjs +12 -4
- package/cli/check-watch.mjs +151 -0
- package/cli/check.mjs +460 -71
- package/cli/code-editor.mjs +287 -0
- package/cli/commands.mjs +13 -2
- package/cli/commit-message.mjs +34 -7
- package/cli/commit-split.mjs +123 -23
- package/cli/decklight.mjs +11 -1
- package/cli/design-system-edit.mjs +37 -1
- package/cli/edit-address.mjs +184 -0
- package/cli/edit-git.mjs +21 -4
- package/cli/edit-handover.mjs +136 -25
- package/cli/edit-look.mjs +45 -1
- package/cli/edit-open.mjs +143 -0
- package/cli/edit-slides.mjs +1043 -67
- package/cli/edit-translate.mjs +243 -0
- package/cli/edit.mjs +3182 -267
- package/cli/editor-support.mjs +286 -0
- package/cli/enhance.mjs +148 -20
- package/cli/find-replace.mjs +142 -0
- package/cli/fix-review.mjs +368 -0
- package/cli/init.mjs +93 -19
- package/cli/keep-awake.mjs +68 -0
- package/cli/migrate-writing.mjs +214 -0
- package/cli/narrate.mjs +51 -10
- package/cli/open.mjs +29 -9
- package/cli/outside-edit.mjs +257 -0
- package/cli/paste.mjs +92 -0
- package/cli/pdf.mjs +61 -2
- package/cli/pptx-export.mjs +3 -1
- package/cli/publish.mjs +24 -3
- package/cli/record.mjs +19 -8
- package/cli/rewrite.mjs +265 -0
- package/cli/runtime-link.mjs +218 -17
- package/cli/serve.mjs +7 -4
- package/cli/setup.mjs +2 -2
- package/cli/skill-content.mjs +212 -8
- package/cli/skills/slide-writing.md +333 -0
- package/cli/skills/voiceover.md +423 -0
- package/cli/skills.mjs +36 -13
- package/cli/starter-full.mjs +353 -0
- package/cli/supervise.mjs +14 -0
- package/cli/theme.mjs +3 -2
- package/cli/translate-core.mjs +466 -0
- package/cli/translate-files.mjs +149 -0
- package/cli/translate.mjs +458 -0
- package/cli/util.mjs +25 -0
- package/cli/vocabulary-docs.mjs +138 -0
- package/dist/decklight.css +2087 -91
- package/dist/decklight.js +367 -203
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +9 -2
- package/docs/WRITING_RESEARCH.md +327 -0
- package/package.json +5 -3
- package/themes/coastal.css +3 -0
- package/themes/metropolis.css +2 -0
- package/themes/reveal-blood.css +3 -0
- package/themes/reveal-league.css +2 -0
- package/themes/reveal-moon.css +2 -0
- package/themes/reveal-serif.css +1 -0
- package/themes/reveal-sky.css +1 -0
- package/themes/reveal-solarized.css +4 -0
- package/tools/brand-palette.mjs +6 -1
- package/tools/build-groups.mjs +28 -8
- package/tools/cdp.mjs +176 -0
- package/tools/chart-spec.mjs +242 -0
- package/tools/click-match.mjs +70 -0
- package/tools/code-options.mjs +174 -0
- package/tools/conversation.mjs +49 -0
- package/tools/deck-family.mjs +101 -0
- package/tools/deck-html.mjs +212 -15
- package/tools/design-system-format.mjs +15 -1
- package/tools/diagram-shapes.mjs +221 -0
- package/tools/elevenlabs-tts.mjs +62 -53
- package/tools/face-clip.mjs +96 -0
- package/tools/face-layout.mjs +94 -0
- package/tools/gemini-tts.mjs +1 -1
- package/tools/invisible-in-print.mjs +27 -0
- package/tools/lipsync-auto.mjs +197 -0
- package/tools/lipsync-config.mjs +16 -0
- package/tools/lipsync-engines.mjs +22 -4
- package/tools/lipsync-server.mjs +8 -5
- package/tools/lipsync.mjs +122 -30
- package/tools/local-voice.mjs +1 -1
- package/tools/mouth-file.mjs +26 -0
- package/tools/mouth.mjs +320 -0
- package/tools/narration-manifest.mjs +24 -4
- package/tools/narration-synth.mjs +96 -76
- package/tools/recording-impact.mjs +16 -1
- package/tools/sentences.mjs +7 -1
- package/tools/starters.mjs +402 -0
- package/tools/steps.mjs +118 -0
- package/tools/take-check.mjs +146 -0
- package/tools/template-theme.mjs +3 -1
- package/tools/test-impact.mjs +36 -12
- package/tools/theme-check.mjs +196 -1
- package/tools/tone-cue.mjs +108 -0
- package/tools/tts-cache.mjs +9 -3
- package/tools/tts-engines.mjs +100 -11
- package/tools/video-cache.mjs +93 -0
- package/tools/video-live.mjs +349 -0
- package/tools/video-options.mjs +11 -0
- package/tools/video.mjs +772 -127
- package/tools/viseme-fit.mjs +27 -0
- package/tools/viseme-match.mjs +77 -0
- package/tools/vocabulary.mjs +397 -0
- package/tools/voice-renumber.mjs +605 -0
- package/tools/voiceover-server.mjs +42 -11
- package/tools/voiceover.mjs +48 -20
- package/dist/build-info.json +0 -1
package/tools/build-groups.mjs
CHANGED
|
@@ -11,22 +11,38 @@
|
|
|
11
11
|
* to the DOM (#526): cli/ and tools/ never import src/, which the package
|
|
12
12
|
* does not ship, while src/ may import a tool.
|
|
13
13
|
*
|
|
14
|
-
* `
|
|
15
|
-
*
|
|
14
|
+
* A step marked `with` (`data-build-with`, and no explicit key of its own)
|
|
15
|
+
* arrives on the click of the step emitted just before it: it takes that
|
|
16
|
+
* step's key and joins its group, wherever the order puts it.
|
|
17
|
+
*
|
|
18
|
+
* `items` = [{ key, explicit, with? }] in document/emission order; returns
|
|
19
|
+
* groups of item indices, one group per click.
|
|
16
20
|
*/
|
|
17
21
|
export function computeGroups(items) {
|
|
18
|
-
const indexed =
|
|
19
|
-
|
|
22
|
+
const indexed = [];
|
|
23
|
+
items.forEach((it, i) => {
|
|
24
|
+
const prev = indexed[i - 1];
|
|
25
|
+
indexed.push(it.with && !it.explicit && prev ? { key: prev.key, explicit: prev.explicit, i, joins: prev.joins ?? prev.i } : { ...it, i, joins: undefined });
|
|
26
|
+
});
|
|
27
|
+
const sorted = [...indexed].sort((a, b) => a.key - b.key || (a.joins ?? a.i) - (b.joins ?? b.i) || a.i - b.i);
|
|
20
28
|
const groups = [];
|
|
29
|
+
const groupOf = new Map();
|
|
21
30
|
let cur = null;
|
|
22
31
|
let curKey = null;
|
|
23
32
|
let curExplicit = false;
|
|
24
|
-
for (const it of
|
|
33
|
+
for (const it of sorted) {
|
|
34
|
+
if (it.joins !== undefined && groupOf.has(it.joins)) {
|
|
35
|
+
groupOf.get(it.joins).push(it.i);
|
|
36
|
+
groupOf.set(it.i, groupOf.get(it.joins));
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
25
39
|
if (cur && it.explicit && curExplicit && it.key === curKey) {
|
|
26
40
|
cur.push(it.i);
|
|
41
|
+
groupOf.set(it.i, cur);
|
|
27
42
|
continue;
|
|
28
43
|
}
|
|
29
44
|
cur = [it.i];
|
|
45
|
+
groupOf.set(it.i, cur);
|
|
30
46
|
curKey = it.key;
|
|
31
47
|
curExplicit = it.explicit;
|
|
32
48
|
groups.push(cur);
|
|
@@ -34,9 +50,13 @@ export function computeGroups(items) {
|
|
|
34
50
|
return groups;
|
|
35
51
|
}
|
|
36
52
|
|
|
37
|
-
/**
|
|
38
|
-
|
|
53
|
+
/**
|
|
54
|
+
* The order item for a step: `orderAttr` explicit when present, else the auto
|
|
55
|
+
* position `auto`; `withPrev` (its `data-build-with`) to arrive with the step
|
|
56
|
+
* before it, when it has no explicit key.
|
|
57
|
+
*/
|
|
58
|
+
export function orderItem(orderAttr, auto, withPrev = false) {
|
|
39
59
|
const explicit = orderAttr != null && orderAttr !== '';
|
|
40
60
|
const key = explicit ? parseInt(orderAttr, 10) : auto;
|
|
41
|
-
return { key: Number.isFinite(key) ? key : auto, explicit };
|
|
61
|
+
return { key: Number.isFinite(key) ? key : auto, explicit, ...(withPrev && !explicit ? { with: true } : {}) };
|
|
42
62
|
}
|
package/tools/cdp.mjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// A minimal Chrome DevTools Protocol page driver — the one thing `decklight
|
|
5
|
+
// video` needs that a one-shot `--screenshot` cannot do: hold ONE page open,
|
|
6
|
+
// move its clock by hand, and photograph it at each tick.
|
|
7
|
+
//
|
|
8
|
+
// No dependency: Node ≥22 ships a global WebSocket client, and Chrome says
|
|
9
|
+
// where it listens on stderr (`--remote-debugging-port=0`). The page is still
|
|
10
|
+
// whatever URL the caller gives it — the video tool passes the deck served over
|
|
11
|
+
// http://127.0.0.1 under the `--no-trust` CSP (#229), so this adds no file://
|
|
12
|
+
// access: the debugging port is bound to loopback and lives only as long as the
|
|
13
|
+
// browser, which is killed in `close()`.
|
|
14
|
+
|
|
15
|
+
import { spawn } from 'node:child_process';
|
|
16
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
17
|
+
import { tmpdir } from 'node:os';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
20
|
+
import { chromeArgs } from './chrome.mjs';
|
|
21
|
+
|
|
22
|
+
const LISTENING = /DevTools listening on (ws:\/\/\S+)/;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Start a headless Chrome and open one page of `w`×`h` CSS pixels.
|
|
26
|
+
* `args` are extra Chrome flags (`decklight check --watch` reads a file://
|
|
27
|
+
* deck, so it needs `--allow-file-access-from-files`).
|
|
28
|
+
* @returns {Promise<{ send, once, evaluate, navigate, screenshot, close }>}
|
|
29
|
+
*/
|
|
30
|
+
export async function openPage(bin, { w, h, signal, startupMs = 15000, args = [] } = {}) {
|
|
31
|
+
if (typeof WebSocket === 'undefined') throw new Error('animated capture needs Node 22 or newer (no WebSocket client here)');
|
|
32
|
+
signal?.throwIfAborted();
|
|
33
|
+
const profile = mkdtempSync(join(tmpdir(), 'decklight-cdp-'));
|
|
34
|
+
const child = spawn(bin, chromeArgs(
|
|
35
|
+
'--hide-scrollbars', '--autoplay-policy=no-user-gesture-required',
|
|
36
|
+
`--window-size=${w},${h}`, '--remote-debugging-port=0', `--user-data-dir=${profile}`, ...args, 'about:blank',
|
|
37
|
+
), { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
38
|
+
child.on('error', () => {}); // an abort kills the child; the rejection below says why
|
|
39
|
+
const exited = new Promise((ok) => child.once('close', ok));
|
|
40
|
+
let stopBrowser = () => child.kill();
|
|
41
|
+
const abort = () => stopBrowser();
|
|
42
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
43
|
+
// the profile goes only once Chrome has let go of it — it writes on the way out
|
|
44
|
+
const cleanup = async () => {
|
|
45
|
+
signal?.removeEventListener('abort', abort);
|
|
46
|
+
if (child.exitCode === null && child.signalCode === null) child.kill();
|
|
47
|
+
await exited;
|
|
48
|
+
try {
|
|
49
|
+
rmSync(profile, { recursive: true, force: true, maxRetries: 5 });
|
|
50
|
+
} catch (error) {
|
|
51
|
+
// Windows can hold a profile lock briefly after the browser exits.
|
|
52
|
+
if (process.platform !== 'win32' || !['EPERM', 'EBUSY', 'ENOTEMPTY'].includes(error.code)) throw error;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
let endpoint;
|
|
57
|
+
try {
|
|
58
|
+
endpoint = await new Promise((ok, no) => {
|
|
59
|
+
let seen = '';
|
|
60
|
+
const timer = setTimeout(() => no(new Error('Chrome did not open its debugging port')), startupMs);
|
|
61
|
+
const readEndpoint = (d) => {
|
|
62
|
+
seen += d;
|
|
63
|
+
const m = LISTENING.exec(seen);
|
|
64
|
+
if (m) {
|
|
65
|
+
child.stderr.off('data', readEndpoint);
|
|
66
|
+
child.stderr.resume();
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
ok(m[1]);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
child.stderr.on('data', readEndpoint);
|
|
72
|
+
child.on('exit', () => { clearTimeout(timer); no(new Error('Chrome exited before it was ready')); });
|
|
73
|
+
child.once('error', (error) => { clearTimeout(timer); no(error); });
|
|
74
|
+
});
|
|
75
|
+
} catch (e) { await cleanup(); throw e; }
|
|
76
|
+
|
|
77
|
+
const ws = new WebSocket(endpoint);
|
|
78
|
+
try {
|
|
79
|
+
await new Promise((ok, no) => {
|
|
80
|
+
const fail = () => { clearTimeout(timer); no(new Error('could not reach Chrome\'s debugging port')); };
|
|
81
|
+
const timer = setTimeout(fail, startupMs);
|
|
82
|
+
ws.onopen = () => { clearTimeout(timer); ok(); };
|
|
83
|
+
ws.onerror = fail;
|
|
84
|
+
exited.then(fail);
|
|
85
|
+
});
|
|
86
|
+
} catch (e) { await cleanup(); throw e; }
|
|
87
|
+
|
|
88
|
+
let id = 0;
|
|
89
|
+
const pending = new Map();
|
|
90
|
+
const waiting = []; // { method, sessionId, ok, no }
|
|
91
|
+
const listeners = new Set();
|
|
92
|
+
// Chrome gone mid-capture: every call still waiting on it fails, rather than
|
|
93
|
+
// the render hanging on an answer that will never come
|
|
94
|
+
ws.onclose = () => {
|
|
95
|
+
const gone = new Error('Chrome closed its debugging connection');
|
|
96
|
+
for (const p of pending.values()) p.no(gone);
|
|
97
|
+
for (const x of waiting.splice(0)) x.no(gone);
|
|
98
|
+
pending.clear();
|
|
99
|
+
};
|
|
100
|
+
ws.onmessage = ({ data }) => {
|
|
101
|
+
const msg = JSON.parse(data);
|
|
102
|
+
if (msg.id) {
|
|
103
|
+
const p = pending.get(msg.id);
|
|
104
|
+
pending.delete(msg.id);
|
|
105
|
+
if (msg.error) p?.no(new Error(`${p.method}: ${msg.error.message}`)); else p?.ok(msg.result);
|
|
106
|
+
} else {
|
|
107
|
+
const i = waiting.findIndex((x) => x.method === msg.method && x.sessionId === msg.sessionId);
|
|
108
|
+
if (i >= 0) waiting.splice(i, 1)[0].ok(msg.params);
|
|
109
|
+
for (const listener of listeners) listener(msg);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const call = (method, params = {}, sessionId) => new Promise((ok, no) => {
|
|
113
|
+
const n = ++id;
|
|
114
|
+
pending.set(n, { ok, no, method });
|
|
115
|
+
ws.send(JSON.stringify({ id: n, method, params, ...(sessionId ? { sessionId } : {}) }));
|
|
116
|
+
});
|
|
117
|
+
stopBrowser = () => {
|
|
118
|
+
if (ws.readyState !== WebSocket.OPEN) return child.kill();
|
|
119
|
+
call('Browser.close').catch(() => {});
|
|
120
|
+
const timer = setTimeout(() => child.kill(), 1000);
|
|
121
|
+
exited.then(() => clearTimeout(timer));
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
signal?.throwIfAborted();
|
|
126
|
+
const { targetId } = await call('Target.createTarget', { url: 'about:blank' });
|
|
127
|
+
const { sessionId } = await call('Target.attachToTarget', { targetId, flatten: true });
|
|
128
|
+
const send = (method, params) => call(method, params, sessionId);
|
|
129
|
+
const once = (method) => new Promise((ok, no) => waiting.push({ method, sessionId, ok, no }));
|
|
130
|
+
await send('Page.enable');
|
|
131
|
+
await send('Runtime.enable');
|
|
132
|
+
await send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: false });
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
send,
|
|
136
|
+
/** The page's next `method` event, its params. */
|
|
137
|
+
once,
|
|
138
|
+
on(method, callback) {
|
|
139
|
+
const listener = (msg) => {
|
|
140
|
+
if (msg.method === method && msg.sessionId === sessionId) callback(msg.params);
|
|
141
|
+
};
|
|
142
|
+
listeners.add(listener);
|
|
143
|
+
return () => listeners.delete(listener);
|
|
144
|
+
},
|
|
145
|
+
/** Run an expression in the page; its (awaited) value comes back. */
|
|
146
|
+
async evaluate(expression) {
|
|
147
|
+
const r = await send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true });
|
|
148
|
+
if (r.exceptionDetails) throw new Error(r.exceptionDetails.exception?.description ?? r.exceptionDetails.text);
|
|
149
|
+
return r.result.value;
|
|
150
|
+
},
|
|
151
|
+
/** Load `url` and resolve once the page's load event has fired. */
|
|
152
|
+
async navigate(url) {
|
|
153
|
+
const loaded = once('Page.loadEventFired');
|
|
154
|
+
loaded.catch(() => {}); // a failed navigate below is the error that gets reported
|
|
155
|
+
const { errorText } = await send('Page.navigate', { url });
|
|
156
|
+
if (errorText) throw new Error(`could not load ${url}: ${errorText}`);
|
|
157
|
+
await loaded;
|
|
158
|
+
},
|
|
159
|
+
/** A PNG of the viewport, as a Buffer. */
|
|
160
|
+
async screenshot(format = 'png') {
|
|
161
|
+
const { data } = await send('Page.captureScreenshot', { format, optimizeForSpeed: true });
|
|
162
|
+
return Buffer.from(data, 'base64');
|
|
163
|
+
},
|
|
164
|
+
async close() {
|
|
165
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
166
|
+
// Browser.close lets Chrome stop its renderer processes and release
|
|
167
|
+
// the profile before Windows tries to remove it.
|
|
168
|
+
call('Browser.close').catch(() => {});
|
|
169
|
+
await Promise.race([exited, delay(1500, undefined, { ref: false })]);
|
|
170
|
+
}
|
|
171
|
+
try { ws.close(); } catch { /* already gone */ }
|
|
172
|
+
await cleanup();
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
} catch (e) { try { ws.close(); } catch { /* gone */ } await cleanup(); throw e; }
|
|
176
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// A chart's JSON, read (SPEC CHARTS): what `data-chart` declares, normalized
|
|
5
|
+
// and checked, with no DOM and no drawing. Pure and shared, so the runtime
|
|
6
|
+
// (src/core/charts.js, which draws what this returns), the Data tab
|
|
7
|
+
// (src/core/chartdata.js) and `decklight check` (cli/check.mjs, which reports
|
|
8
|
+
// what this refuses and what it notes) hold one reading of a chart.
|
|
9
|
+
|
|
10
|
+
const VB_W = 640;
|
|
11
|
+
const VB_H = 360;
|
|
12
|
+
export const CHART_TYPES = ['bar', 'line', 'area', 'pie', 'donut', 'scatter'];
|
|
13
|
+
/** The one type whose data is x/y pairs rather than a value per category. */
|
|
14
|
+
const isXY = (type) => type === 'scatter';
|
|
15
|
+
/** The types a series can dash, carry a band or mark a point on: a value per label, joined by a line. */
|
|
16
|
+
const isLine = (type) => type === 'line' || type === 'area';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Cell text → the JSON it writes. `''` is a gap (`null`, which the chart
|
|
20
|
+
* skips); a number is kept as typed when it is already JSON (`1.50` stays
|
|
21
|
+
* `1.50`), and put in JSON's spelling when it is a plain decimal JSON does
|
|
22
|
+
* not spell that way (`+3`, `.5`, `007`). Anything else (`12a`, `1,5`, `0x10`,
|
|
23
|
+
* `Infinity`) is refused: `{ ok: false }`.
|
|
24
|
+
*/
|
|
25
|
+
export function cellNumber(input) {
|
|
26
|
+
const t = String(input ?? '').trim();
|
|
27
|
+
if (t === '') return { ok: true, raw: 'null', value: null };
|
|
28
|
+
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(t)) {
|
|
29
|
+
const v = Number(t);
|
|
30
|
+
return Number.isFinite(v) ? { ok: true, raw: t, value: v } : { ok: false };
|
|
31
|
+
}
|
|
32
|
+
if (/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(t)) {
|
|
33
|
+
const v = Number(t);
|
|
34
|
+
return Number.isFinite(v) ? { ok: true, raw: String(v), value: v } : { ok: false };
|
|
35
|
+
}
|
|
36
|
+
return { ok: false };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One value of a series, as the chart plots it: a finite JSON number, or a
|
|
41
|
+
* string `cellNumber` reads as one; anything else (`null`, `''`, a boolean,
|
|
42
|
+
* a list, an object, a word) is a gap, `null`, never a zero (#1125).
|
|
43
|
+
*/
|
|
44
|
+
export function chartValue(v) {
|
|
45
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : null;
|
|
46
|
+
if (typeof v === 'string') { const n = cellNumber(v); return n.ok ? n.value : null; }
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The row a reference names (`dashFrom`, `now`, a marker's `at`): a label,
|
|
52
|
+
* as written, or else a position counted from 0. -1 when it names neither.
|
|
53
|
+
* The label wins, so a chart over the years 2023…2026 says `"now": 2025`.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveAt(labels, at) {
|
|
56
|
+
if (typeof at !== 'string' && typeof at !== 'number') return -1;
|
|
57
|
+
const byLabel = labels.indexOf(String(at));
|
|
58
|
+
if (byLabel !== -1) return byLabel;
|
|
59
|
+
if (typeof at === 'number' && Number.isInteger(at) && at >= 0 && at < labels.length) return at;
|
|
60
|
+
return -1;
|
|
61
|
+
}
|
|
62
|
+
/** How a reference reads in a sentence. */
|
|
63
|
+
const said = (at) => (typeof at === 'string' ? `"${at}"` : String(at));
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Normalize and validate a chart declaration. `source` is the JSON text (or
|
|
67
|
+
* an already-parsed object); `attrs` carries the wrapper's data-* attributes,
|
|
68
|
+
* which win over the same keys in the JSON (the fence form has no attribute
|
|
69
|
+
* surface, so the JSON may carry type/title/aspect/build itself).
|
|
70
|
+
* Throws with a message meant to be READ: it lands in the error box, and
|
|
71
|
+
* `decklight check` says it. What is drawn without being refused (a band
|
|
72
|
+
* shorter than the labels, a `now` that names no label) is said in `notes`,
|
|
73
|
+
* which only `check` reads.
|
|
74
|
+
*/
|
|
75
|
+
export function parseChart(source, attrs = {}) {
|
|
76
|
+
let json;
|
|
77
|
+
if (typeof source === 'string') {
|
|
78
|
+
try { json = JSON.parse(source); } catch (e) {
|
|
79
|
+
throw new Error(`invalid JSON — ${e.message}`);
|
|
80
|
+
}
|
|
81
|
+
} else if (source && typeof source === 'object') {
|
|
82
|
+
json = source;
|
|
83
|
+
} else {
|
|
84
|
+
throw new Error('no data — expected a <script type="application/json"> child (or the fence body)');
|
|
85
|
+
}
|
|
86
|
+
if (!json || typeof json !== 'object' || Array.isArray(json)) throw new Error('the JSON must be an object');
|
|
87
|
+
|
|
88
|
+
const type = String(attrs.type || json.type || '').toLowerCase();
|
|
89
|
+
if (!CHART_TYPES.includes(type)) {
|
|
90
|
+
throw new Error(`unknown type "${type}" — use ${CHART_TYPES.join(' | ')}`);
|
|
91
|
+
}
|
|
92
|
+
const notes = [];
|
|
93
|
+
|
|
94
|
+
// A scatter has no categories: its x is a number, not a label, which is the
|
|
95
|
+
// whole difference between "sales by region" and "latency against load".
|
|
96
|
+
if (!isXY(type) && (!Array.isArray(json.labels) || json.labels.length === 0)) {
|
|
97
|
+
throw new Error('"labels" must be a non-empty array');
|
|
98
|
+
}
|
|
99
|
+
if (!Array.isArray(json.series) || json.series.length === 0) {
|
|
100
|
+
throw new Error('"series" must be a non-empty array');
|
|
101
|
+
}
|
|
102
|
+
const labels = isXY(type) ? [] : json.labels.map(String);
|
|
103
|
+
// pad/trim to the label count; a value that is not a number is a gap
|
|
104
|
+
const row = (list) => labels.map((_, k) => chartValue(list[k]));
|
|
105
|
+
const series = json.series.map((s, i) => {
|
|
106
|
+
const name = s && s.name != null ? String(s.name) : `series ${i + 1}`;
|
|
107
|
+
const concept = s && s.concept != null ? String(s.concept) : null;
|
|
108
|
+
if (isXY(type)) {
|
|
109
|
+
if (!s || !Array.isArray(s.points)) {
|
|
110
|
+
throw new Error(`series ${i + 1}: "points" must be an array of [x, y] pairs`);
|
|
111
|
+
}
|
|
112
|
+
const points = s.points
|
|
113
|
+
.map((pt) => (Array.isArray(pt) ? [chartValue(pt[0]), chartValue(pt[1])] : [null, null]))
|
|
114
|
+
.filter(([x, y]) => x != null && y != null);
|
|
115
|
+
if (!points.length) throw new Error(`series ${i + 1}: no usable [x, y] pairs`);
|
|
116
|
+
if (s.band !== undefined || s.dashFrom !== undefined || s.markers !== undefined) {
|
|
117
|
+
notes.push(`series ${i + 1}: a scatter draws no band, dashes or markers — only a line or area chart does`);
|
|
118
|
+
}
|
|
119
|
+
return { name, concept, kind: 'line', points, data: points.map(([, y]) => y), band: null, dashFrom: null, markers: [] };
|
|
120
|
+
}
|
|
121
|
+
if (!s || typeof s !== 'object') throw new Error(`series ${i + 1}: must be an object with "data"`);
|
|
122
|
+
// a series is a line (its values, the default) or a band (a range, no line)
|
|
123
|
+
const kind = s.type == null ? 'line' : String(s.type).toLowerCase();
|
|
124
|
+
if (kind !== 'line' && kind !== 'band') {
|
|
125
|
+
throw new Error(`series ${i + 1}: "type" is "band" (a range drawn as an area) or left out`);
|
|
126
|
+
}
|
|
127
|
+
if (kind === 'band' && !isLine(type)) {
|
|
128
|
+
throw new Error(`series ${i + 1} is a band: a band needs a line or area chart`);
|
|
129
|
+
}
|
|
130
|
+
if (kind === 'line' && !Array.isArray(s.data)) {
|
|
131
|
+
throw new Error(`series ${i + 1}: "data" must be an array of numbers`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// a band: the range round a series, or the whole of a band series
|
|
135
|
+
let band = null;
|
|
136
|
+
if (s.band != null) {
|
|
137
|
+
const b = s.band;
|
|
138
|
+
if (typeof b !== 'object' || Array.isArray(b) || !Array.isArray(b.low) || !Array.isArray(b.high)) {
|
|
139
|
+
throw new Error(`series ${i + 1}: "band" must be { "low": […], "high": […] }`);
|
|
140
|
+
}
|
|
141
|
+
for (const edge of ['low', 'high']) {
|
|
142
|
+
if (b[edge].length !== labels.length) {
|
|
143
|
+
notes.push(`series ${i + 1}: its band's "${edge}" has ${b[edge].length} value${b[edge].length === 1 ? '' : 's'} for ${labels.length} label${labels.length === 1 ? '' : 's'} — ${b[edge].length < labels.length ? 'the rest is a gap' : 'the extra values are not drawn'}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
band = { low: row(b.low), high: row(b.high) };
|
|
147
|
+
const crossed = labels.findIndex((_, k) => band.low[k] != null && band.high[k] != null && band.low[k] > band.high[k]);
|
|
148
|
+
if (crossed !== -1) notes.push(`series ${i + 1}: its band's low is above its high at "${labels[crossed]}"`);
|
|
149
|
+
if (!isLine(type)) notes.push(`series ${i + 1}: a ${type} chart draws no band — only a line or area chart does`);
|
|
150
|
+
} else if (kind === 'band') {
|
|
151
|
+
throw new Error(`series ${i + 1} is a band: it needs "band": { "low": […], "high": […] }`);
|
|
152
|
+
}
|
|
153
|
+
const data = kind === 'band' ? labels.map(() => null) : row(s.data);
|
|
154
|
+
|
|
155
|
+
// where the line turns dashed: solid up to that point, dashed after it
|
|
156
|
+
let dashFrom = null;
|
|
157
|
+
if (s.dashFrom != null) {
|
|
158
|
+
if (typeof s.dashFrom !== 'string' && typeof s.dashFrom !== 'number') {
|
|
159
|
+
throw new Error(`series ${i + 1}: "dashFrom" is a label or a position`);
|
|
160
|
+
}
|
|
161
|
+
const k = resolveAt(labels, s.dashFrom);
|
|
162
|
+
if (k === -1) notes.push(`series ${i + 1}: "dashFrom": ${said(s.dashFrom)} names no label (nor a position from 0): the line is drawn solid`);
|
|
163
|
+
else if (!isLine(type) || kind === 'band') notes.push(`series ${i + 1}: "dashFrom" dashes a line, and ${kind === 'band' ? 'a band series has none' : `a ${type} chart draws none`}`);
|
|
164
|
+
else dashFrom = k;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// emphasised points, each with its words
|
|
168
|
+
const markers = [];
|
|
169
|
+
if (s.markers != null) {
|
|
170
|
+
if (!Array.isArray(s.markers)) throw new Error(`series ${i + 1}: "markers" must be a list of { "at": …, "label": … }`);
|
|
171
|
+
s.markers.forEach((m, j) => {
|
|
172
|
+
if (!m || typeof m !== 'object' || Array.isArray(m) || (typeof m.at !== 'string' && typeof m.at !== 'number')) {
|
|
173
|
+
throw new Error(`series ${i + 1}: marker ${j + 1} must be { "at": <label or position>, "label": "…" }`);
|
|
174
|
+
}
|
|
175
|
+
const k = resolveAt(labels, m.at);
|
|
176
|
+
if (k === -1) { notes.push(`series ${i + 1}: marker ${j + 1} is at ${said(m.at)}, which names no label (nor a position from 0): it is not drawn`); return; }
|
|
177
|
+
if (!isLine(type)) { notes.push(`series ${i + 1}: a ${type} chart draws no markers — only a line or area chart does`); return; }
|
|
178
|
+
if (data[k] == null) { notes.push(`series ${i + 1}: marker ${j + 1} is at "${labels[k]}", where the series has no value: it is not drawn`); return; }
|
|
179
|
+
markers.push({ at: k, label: m.label != null ? String(m.label) : '' });
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return { name, concept, kind, data, band, dashFrom, markers };
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const isPie = type === 'pie' || type === 'donut';
|
|
186
|
+
if (isPie && series.length > 1) {
|
|
187
|
+
throw new Error('pie takes exactly one series (labels are the slices)');
|
|
188
|
+
}
|
|
189
|
+
if (isPie && !series[0].data.some((v) => v > 0)) {
|
|
190
|
+
throw new Error('pie needs at least one positive value');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let height = VB_H;
|
|
194
|
+
const aspect = attrs.aspect ?? json.aspect ?? null;
|
|
195
|
+
if (aspect != null) {
|
|
196
|
+
const m = String(aspect).match(/^\s*(\d+(?:\.\d+)?)\s*[:x]\s*(\d+(?:\.\d+)?)\s*$/i);
|
|
197
|
+
if (!m || +m[1] === 0 || +m[2] === 0) throw new Error(`bad aspect "${aspect}" — use "w:h", e.g. "4:3"`);
|
|
198
|
+
height = Math.round((VB_W * +m[2]) / +m[1]);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// the chart's own rules: a level to read the line against, and the moment it is now
|
|
202
|
+
const axes = !isPie && !isXY(type);
|
|
203
|
+
let threshold = null;
|
|
204
|
+
if (json.threshold != null) {
|
|
205
|
+
const t = json.threshold;
|
|
206
|
+
const y = t && typeof t === 'object' && !Array.isArray(t) ? chartValue(t.y) : null;
|
|
207
|
+
if (y == null) throw new Error('"threshold" must be { "y": <number>, "label": "…" }');
|
|
208
|
+
if (axes) threshold = { y, label: t.label != null ? String(t.label) : '' };
|
|
209
|
+
else notes.push(`"threshold": a ${type} chart has no value axis to draw it on`);
|
|
210
|
+
}
|
|
211
|
+
let now = null;
|
|
212
|
+
if (json.now != null) {
|
|
213
|
+
// a label or a position, or { "at": …, "label": "today" } for words of its own
|
|
214
|
+
const n = json.now;
|
|
215
|
+
const obj = typeof n === 'object' && !Array.isArray(n);
|
|
216
|
+
const at = obj ? n.at : n;
|
|
217
|
+
if (typeof at !== 'string' && typeof at !== 'number') throw new Error('"now" is a label or a position (or { "at": …, "label": "…" })');
|
|
218
|
+
const k = resolveAt(labels, at);
|
|
219
|
+
if (!axes) notes.push(`"now": a ${type} chart has no category axis to draw it on`);
|
|
220
|
+
else if (k === -1) notes.push(`"now": ${said(at)} names no label (nor a position from 0): it is not drawn`);
|
|
221
|
+
else now = { at: k, label: obj && n.label != null ? String(n.label) : 'now' };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
type: isPie ? 'pie' : type,
|
|
226
|
+
donut: type === 'donut' || json.donut === true,
|
|
227
|
+
title: attrs.title ?? (json.title != null ? String(json.title) : null),
|
|
228
|
+
// What the two axes MEAN. Only a scatter asks for them: a category chart's
|
|
229
|
+
// x axis is already labelled, one value at a time.
|
|
230
|
+
xTitle: json.x != null ? String(json.x) : null,
|
|
231
|
+
yTitle: json.y != null ? String(json.y) : null,
|
|
232
|
+
labels,
|
|
233
|
+
series,
|
|
234
|
+
threshold,
|
|
235
|
+
now,
|
|
236
|
+
legend: json.legend, // true forces, false suppresses, undefined = auto
|
|
237
|
+
width: VB_W,
|
|
238
|
+
height,
|
|
239
|
+
build: json.build, // fence form only; the div form authors data-build
|
|
240
|
+
notes,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Do a slide's [click] markers match its build clicks (SPEC BUILD_SEMANTICS)?
|
|
6
|
+
* One rule and one sentence for everything that asks: `decklight check` from
|
|
7
|
+
* the file or from a rendered page, the Check deck… panel, and the recorder
|
|
8
|
+
* before a take (#929). Pure, and here rather than in src/ for the reason
|
|
9
|
+
* tools/build-groups.mjs gives: cli/ never imports src/, src/ may import a tool.
|
|
10
|
+
*
|
|
11
|
+
* The notes want one [click] per build click: the words before the first
|
|
12
|
+
* marker are said on arrival, and each marker reveals the next build. Notes
|
|
13
|
+
* with no marker at all make no claim about the builds and never disagree; a
|
|
14
|
+
* marker on a slide with no builds does, since it waits for a build that never
|
|
15
|
+
* comes.
|
|
16
|
+
*
|
|
17
|
+
* `cause` is the likely reason, when the counting saw one: an EMPTY BUILD
|
|
18
|
+
* GROUP (a build left behind when its content was removed, so a click reveals
|
|
19
|
+
* nothing) or a LEAD CLICK (a marker with nothing said before it, so the first
|
|
20
|
+
* beat is empty). Those two are what put three slides of a real deck out of step.
|
|
21
|
+
*/
|
|
22
|
+
export const CLICK_CAUSES = ['empty-build', 'lead-click'];
|
|
23
|
+
|
|
24
|
+
/** `{ markers, clicks, cause }` when the two counts disagree, null when they agree or the notes make no claim. */
|
|
25
|
+
export function clickMismatch({ markers = 0, clicks = 0, cause = null } = {}) {
|
|
26
|
+
if (!(markers > 0) || markers === clicks) return null;
|
|
27
|
+
return { markers, clicks, cause: CLICK_CAUSES.includes(cause) ? cause : null };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
31
|
+
|
|
32
|
+
/** `5 [click] markers, 6 build clicks`: the two numbers, by the names the author knows them by. */
|
|
33
|
+
export function clickCounts(m) {
|
|
34
|
+
return `${plural(m.markers, '[click] marker')}, ${plural(m.clicks, 'build click')}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `2 [click], 4 builds`: the same, short enough for a slide row's status. */
|
|
38
|
+
export function clickCountsShort(m) {
|
|
39
|
+
return `${m.markers} [click], ${plural(m.clicks, 'build')}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The likely cause, as a phrase, or ''. */
|
|
43
|
+
export function clickCause(m) {
|
|
44
|
+
if (m?.cause === 'empty-build') return 'an empty build group (a data-build with nothing visible in it)';
|
|
45
|
+
if (m?.cause === 'lead-click') return 'a [click] with nothing before it, which makes an empty first beat';
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The whole finding: the counts, the tie when `steps` differs from the clicks,
|
|
51
|
+
* what the notes want, and the likely cause.
|
|
52
|
+
*/
|
|
53
|
+
export function clickMismatchText(m, { steps = null } = {}) {
|
|
54
|
+
const tied = steps != null && steps !== m.clicks ? ` (${plural(steps, 'build step')}, tied by data-build-order)` : '';
|
|
55
|
+
const want = m.clicks
|
|
56
|
+
? `the notes want ${plural(m.clicks, '[click]')}, one per build click`
|
|
57
|
+
: 'there is no build for a [click] to reveal';
|
|
58
|
+
const cause = clickCause(m);
|
|
59
|
+
return `${clickCounts(m)}${tied}: ${want}, so the narration and the builds drift apart here`
|
|
60
|
+
+ (cause ? `; likely ${cause}` : '');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `5 6 empty-build`: the verdict as the print view stamps it on a section, for `check` to read back. */
|
|
64
|
+
export const stampClickMismatch = (m) => `${m.markers} ${m.clicks}${m.cause ? ` ${m.cause}` : ''}`;
|
|
65
|
+
|
|
66
|
+
/** The stamp back into a verdict, or null. */
|
|
67
|
+
export function readClickMismatch(stamp) {
|
|
68
|
+
const [markers, clicks, cause] = String(stamp ?? '').trim().split(/\s+/);
|
|
69
|
+
return clickMismatch({ markers: Number(markers), clicks: Number(clicks), cause });
|
|
70
|
+
}
|