decklight 0.8.1 → 0.9.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.
Files changed (133) hide show
  1. package/PLATFORM.md +666 -0
  2. package/README.md +224 -150
  3. package/SPEC.md +732 -886
  4. package/cli/agent-review.mjs +245 -0
  5. package/cli/agents.mjs +25 -5
  6. package/cli/associate.mjs +12 -12
  7. package/cli/audit.mjs +42 -10
  8. package/cli/banner.mjs +41 -17
  9. package/cli/bundle-audio.mjs +183 -0
  10. package/cli/bundle.mjs +383 -31
  11. package/cli/cast.mjs +7 -7
  12. package/cli/check.mjs +961 -0
  13. package/cli/clone-deck.mjs +179 -0
  14. package/cli/commands.mjs +345 -0
  15. package/cli/comments.mjs +64 -18
  16. package/cli/commit-flow.mjs +44 -19
  17. package/cli/commit-message.mjs +51 -74
  18. package/cli/commit-split.mjs +601 -0
  19. package/cli/deck-refs.mjs +59 -0
  20. package/cli/deck-routes.mjs +67 -0
  21. package/cli/deckfile.mjs +3 -5
  22. package/cli/decklight.mjs +68 -289
  23. package/cli/design-system-deps.mjs +209 -0
  24. package/cli/design-system-edit.mjs +194 -0
  25. package/cli/design-system-refs.mjs +409 -0
  26. package/cli/design-system.mjs +429 -0
  27. package/cli/doctor.mjs +115 -0
  28. package/cli/edit-git.mjs +353 -0
  29. package/cli/edit-handover.mjs +445 -0
  30. package/cli/edit-look.mjs +388 -0
  31. package/cli/edit-slides.mjs +683 -0
  32. package/cli/edit.mjs +2126 -842
  33. package/cli/enhance.mjs +483 -0
  34. package/cli/extension.mjs +10 -10
  35. package/cli/font-refs.mjs +273 -0
  36. package/cli/font.mjs +246 -0
  37. package/cli/git-tree.mjs +1 -1
  38. package/cli/git.mjs +65 -11
  39. package/cli/import.mjs +56 -10
  40. package/cli/init.mjs +400 -234
  41. package/cli/library.mjs +127 -0
  42. package/cli/loader.mjs +3 -1
  43. package/cli/marketplace.mjs +215 -21
  44. package/cli/narrate.mjs +176 -0
  45. package/cli/open-browser.mjs +63 -0
  46. package/cli/open.mjs +943 -0
  47. package/cli/pdf.mjs +41 -13
  48. package/cli/pkg.mjs +44 -2
  49. package/cli/plugin.mjs +43 -41
  50. package/cli/port-conflict.mjs +53 -23
  51. package/cli/pptx-export.mjs +57 -24
  52. package/cli/publish.mjs +8 -4
  53. package/cli/qr.mjs +1 -1
  54. package/cli/record.mjs +14 -12
  55. package/cli/remote.mjs +11 -11
  56. package/cli/restore.mjs +11 -4
  57. package/cli/review-ref.mjs +125 -0
  58. package/cli/review-remote.mjs +92 -48
  59. package/cli/review-routes.mjs +254 -0
  60. package/cli/review-store.mjs +47 -7
  61. package/cli/review-submit.mjs +47 -57
  62. package/cli/runtime-link.mjs +560 -0
  63. package/cli/serve.mjs +235 -44
  64. package/cli/setup.mjs +112 -0
  65. package/cli/sign.mjs +1 -1
  66. package/cli/skill-content.mjs +190 -33
  67. package/cli/skills.mjs +107 -24
  68. package/cli/start.mjs +110 -0
  69. package/cli/supervise.mjs +6 -6
  70. package/cli/theme-refs.mjs +515 -0
  71. package/cli/theme.mjs +181 -137
  72. package/cli/trust.mjs +83 -0
  73. package/cli/units.mjs +39 -42
  74. package/cli/update-check.mjs +1 -5
  75. package/cli/upgrade.mjs +194 -28
  76. package/cli/upstream.mjs +4 -3
  77. package/cli/util.mjs +4 -4
  78. package/cli/wizard.mjs +6 -6
  79. package/dist/build-info.json +1 -1
  80. package/dist/decklight.css +1126 -49
  81. package/dist/decklight.js +212 -115
  82. package/dist/decklight.js.map +4 -4
  83. package/docs/GLOSSARY.md +141 -0
  84. package/docs/architecture.svg +17 -17
  85. package/package.json +4 -2
  86. package/themes/README.md +9 -8
  87. package/themes/reveal-solarized.css +10 -0
  88. package/tools/args.mjs +20 -0
  89. package/tools/atomic-write.mjs +48 -0
  90. package/tools/brand-palette.mjs +717 -0
  91. package/tools/bridge.mjs +1 -1
  92. package/tools/build-groups.mjs +42 -0
  93. package/tools/color.mjs +48 -0
  94. package/tools/deck-html.mjs +396 -11
  95. package/tools/design-system-format.mjs +753 -0
  96. package/tools/elevenlabs-tts.mjs +110 -17
  97. package/tools/exec.mjs +1 -1
  98. package/tools/extension-check.mjs +31 -7
  99. package/tools/font-format.mjs +190 -0
  100. package/tools/font-stacks.mjs +24 -0
  101. package/tools/gemini-tts.mjs +3 -0
  102. package/tools/lipsync-config.mjs +103 -0
  103. package/tools/lipsync-engines.mjs +205 -2
  104. package/tools/lipsync-server.mjs +264 -41
  105. package/tools/lipsync.mjs +37 -18
  106. package/tools/local-voice.mjs +32 -42
  107. package/tools/narration-manifest.mjs +196 -0
  108. package/tools/narration-synth.mjs +416 -0
  109. package/tools/pptx.mjs +776 -19
  110. package/tools/publish-targets.mjs +1 -1
  111. package/tools/publish-voices.mjs +3 -3
  112. package/tools/recording-impact.mjs +7 -3
  113. package/tools/render-theme.mjs +33 -0
  114. package/tools/review-anchor.mjs +28 -12
  115. package/tools/semver.mjs +40 -0
  116. package/tools/sentences.mjs +180 -0
  117. package/tools/shot.mjs +14 -7
  118. package/tools/site-voices.mjs +232 -0
  119. package/tools/template-theme.mjs +1 -1
  120. package/tools/test-impact.mjs +17 -10
  121. package/tools/theme-check.mjs +30 -3
  122. package/tools/tts-cache.mjs +4 -3
  123. package/tools/tts-engines.mjs +38 -20
  124. package/tools/tts-setup.mjs +7 -7
  125. package/tools/video-options.mjs +34 -0
  126. package/tools/video.mjs +461 -48
  127. package/tools/voiceover-server.mjs +185 -104
  128. package/tools/voiceover.mjs +119 -261
  129. package/tools/zip.mjs +1 -1
  130. package/cli/dev.mjs +0 -623
  131. package/cli/present.mjs +0 -853
  132. package/cli/review.mjs +0 -361
  133. package/tools/publish-site-voices.mjs +0 -75
@@ -0,0 +1,388 @@
1
+ // Copyright 2026 Gilles Philippart
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // The routes that change how the deck LOOKS rather than what it says: the
5
+ // theme it opens in and whether the picker holds it to its brand's themes,
6
+ // and the three things a marketplace hands a deck to wear (themes, design
7
+ // systems, fonts), each listed from the cache and marked or dropped in the
8
+ // deck's config block (SPEC THEME_DISTRIBUTION, DESIGN_SYSTEMS, FONTS).
9
+ //
10
+ // They came out of cli/edit.mjs because they are a family with one shape and
11
+ // one set of needs. Every one either lists what the cached catalogs offer
12
+ // against what the deck already references, or writes one entry into the
13
+ // config block as one undo entry; and between them they want six of editMain's
14
+ // bindings, none of them about git, agents, bridges or the export machinery
15
+ // that make up the rest of that server. The marketplace and reference modules
16
+ // they lean on are imported per request, as they always were, so the server
17
+ // still pays for none of them until somebody opens a picker.
18
+ //
19
+ // The import back to edit.mjs is avoided rather than repeated: nothing here
20
+ // needs edit.mjs's own transforms, so this module imports only leaf modules,
21
+ // and edit.mjs reaches it with a STATIC import for the reason
22
+ // cli/edit-slides.mjs gives (a dynamic one from inside editMain would wait on
23
+ // edit.mjs's top-level `await`).
24
+
25
+ import { readFileSync } from 'node:fs';
26
+
27
+ import { oneline } from './git.mjs';
28
+ import { setDeckTheme, setDeckBrandThemesOnly } from './runtime-link.mjs';
29
+
30
+ /**
31
+ * Put the look routes into `routes`, the table cli/edit.mjs dispatches on, and
32
+ * hand it back.
33
+ *
34
+ * `ctx` is the whole of what these handlers may touch: `readDeck()` for the
35
+ * current file; `applyEdit()` for the routes that are one text transform;
36
+ * `writeDeck()` and `history` for the marks, which compute the next file
37
+ * themselves (a mark may fetch, validate and record an install first) and so
38
+ * record the undo entry and write it in two steps of their own; `broadcast`
39
+ * for the in-place theme and brand-theme picks; and `quiet(html)`, which
40
+ * tells the deck watcher that the next write of exactly these bytes updates
41
+ * the page itself and must not reload it. `quiet` is a setter, not the
42
+ * variable, because the watcher in editMain reads and clears it.
43
+ */
44
+ export function registerLookRoutes(routes, { readDeck, applyEdit, writeDeck, history, broadcast = () => {}, quiet = () => {} }) {
45
+ // The deck's theme (PRESENTING): in write mode a pick is the deck's, not
46
+ // the viewer's — written the way this deck carries it (setDeckTheme), quiet
47
+ // and in place like a layout pick, with a `theme` event for every other
48
+ // page. One undo entry, so Z takes it back.
49
+ function themeRoute({ body, json }) {
50
+ const { theme, quiet: inPlace, from } = JSON.parse(body || '{}');
51
+ if (typeof theme !== 'string' || !/^[\w-]+$/.test(theme)) return json(400, { ok: false, error: 'theme is a name' });
52
+ let unwritable = false;
53
+ const changed = applyEdit((html) => { const next = setDeckTheme(html, theme); if (next === null) unwritable = true; return next ?? html; });
54
+ if (unwritable) return json(409, { ok: false, error: `this deck does not carry a theme the server can set to ${theme} — a bundle holds only the themes it embeds` });
55
+ if (changed) {
56
+ console.log(` theme saved: ${theme}`);
57
+ if (inPlace === true) {
58
+ quiet(readDeck());
59
+ broadcast('theme', { theme, from: typeof from === 'string' ? from.slice(0, 64) : null });
60
+ }
61
+ }
62
+ return json(200, { ok: true, changed, inPlace: changed && inPlace === true, ...history.counts() });
63
+ }
64
+ /**
65
+ * `POST /deck/edit/brand-themes` `{ only, quiet?, from? }`: whether the
66
+ * picker holds this deck to its design systems' own themes (SPEC
67
+ * BRAND_PALETTE) — the picker's *Show every theme* and *Only <brand>'s
68
+ * themes* rows. Quiet and in place like a theme pick; every other page reloads.
69
+ */
70
+ function brandThemesRoute({ body, json }) {
71
+ const { only, quiet: inPlace, from } = JSON.parse(body || '{}');
72
+ if (typeof only !== 'boolean') return json(400, { ok: false, error: 'only is true or false' });
73
+ let unwritable = false;
74
+ const changed = applyEdit((html) => { const next = setDeckBrandThemesOnly(html, only); if (next === null) unwritable = true; return next ?? html; });
75
+ if (unwritable) return json(409, { ok: false, error: 'this deck carries no configuration block to record it in — decklight upgrade --link <deck.html> gives it one' });
76
+ if (changed) {
77
+ console.log(` brand themes only: ${only}`);
78
+ if (inPlace === true) {
79
+ quiet(readDeck());
80
+ broadcast('styles', { slide: 1, index: 0, from: typeof from === 'string' ? from.slice(0, 64) : null });
81
+ }
82
+ }
83
+ return json(200, { ok: true, changed, inPlace: changed && inPlace === true, ...history.counts() });
84
+ }
85
+
86
+ // ── marketplace themes, and marking (THEME_BROWSE#UI) ─────────────────
87
+ // What the overlay lists while authoring: every theme of every registered
88
+ // marketplace, each saying whether this deck marks it. Cache-only by
89
+ // construction — the catalogs `marketplace update` already fetched — so a
90
+ // deck on a plane lists what it has and names what it could not read.
91
+ async function themeBrowseRoute({ json }) {
92
+ const { marketplaceThemes, markedRefs, markedSources, resolveThemeRef } = await import('./theme-refs.mjs');
93
+ const { themes, stale } = marketplaceThemes();
94
+ // what the deck marks, as THIS machine names it — the deck may call the
95
+ // same catalog something else (its recorded source says which it means)
96
+ const html = readDeck();
97
+ const sources = markedSources(html);
98
+ const marked = new Set(markedRefs(html).map((r) => {
99
+ const hit = resolveThemeRef(r, undefined, { source: sources[r.marketplace] ?? null });
100
+ return `${r.name}@${hit.local ?? r.marketplace}`;
101
+ }));
102
+ const out = themes.map((t) => ({
103
+ ...t, marked: marked.has(t.qualified),
104
+ ...(resolveThemeRef(t.qualified).remote ? { remote: true } : {}),
105
+ }));
106
+ return json(200, { ok: true, themes: out, stale, cacheOnly: true });
107
+ }
108
+
109
+ /**
110
+ * Mark a theme for this deck, or unmark it (SPEC THEME_DISTRIBUTION). The
111
+ * deck gains or loses one reference in its config block — never CSS — as
112
+ * one undo entry; the watcher's reload brings every browser back with the
113
+ * list as the file now says it is.
114
+ *
115
+ * Marking runs the theme through `theme add`'s own validator first, so a
116
+ * theme refused on the command line is refused here, by the same code, and
117
+ * a deck cannot be made to carry what the shipped set could not contain.
118
+ * An entry whose bytes live at a URL is read now — this is the explicit act
119
+ * — and kept, so no server ever has to reach for it again.
120
+ */
121
+ async function themeMarkRoute({ body, json }) {
122
+ const req = JSON.parse(body || '{}');
123
+ const ref = typeof req.ref === 'string' ? req.ref.trim() : '';
124
+ const on = req.marked !== false;
125
+ const { parseRef, parseShipped, resolveThemeRef, setMarked, cacheThemeCss, refForDeck } = await import('./theme-refs.mjs');
126
+ const { MarketplaceError, configHome } = await import('./marketplace.mjs');
127
+ const shipped = parseShipped(ref);
128
+ if (!shipped && !parseRef(ref)) return json(400, { ok: false, error: 'which theme? — a shipped name, or name@marketplace' });
129
+ const before = readDeck();
130
+ let deckRef = ref, source = null;
131
+ // A shipped theme needs no resolving and no check — it is decklight's own,
132
+ // and passed the contract when it shipped. Marking it says only that the
133
+ // deck carries it when it travels.
134
+ if (on && !shipped) {
135
+ const r = resolveThemeRef(ref);
136
+ let css;
137
+ if (r.file) css = readFileSync(r.file, 'utf8');
138
+ else if (r.remote) {
139
+ const { fetchTheme } = await import('./theme.mjs');
140
+ try { css = await fetchTheme(r.remote); }
141
+ catch (e) { return json(502, { ok: false, error: `could not read ${r.remote}: ${oneline(e)}` }); }
142
+ } else {
143
+ // An entry this machine knows but whose files are not here is a state
144
+ // `marketplace update` fixes (409); one it has never heard of is 404.
145
+ const status = r.entry && r.entry.type !== 'theme' ? 400 : r.entry ? 409 : 404;
146
+ return json(status, { ok: false, error: r.missing });
147
+ }
148
+ const { validateTheme } = await import('../tools/theme-check.mjs');
149
+ const check = validateTheme(css);
150
+ if (!check.ok) {
151
+ // The deck is left byte-for-byte unchanged.
152
+ return json(400, { ok: false, error: `${r.name} fails the theme contract`, problems: check.errors ?? [] });
153
+ }
154
+ if (r.remote) cacheThemeCss(configHome(), r.marketplace, r.name, css);
155
+ source = r.source ?? null;
156
+ deckRef = refForDeck(before, r.name, r.local, source);
157
+ }
158
+ let out;
159
+ try { out = setMarked(before, deckRef, on, { source }); }
160
+ catch (e) {
161
+ if (e instanceof MarketplaceError) return json(409, { ok: false, error: e.message });
162
+ throw e;
163
+ }
164
+ if (out.changed) {
165
+ history.record(before); // Z takes a mark back like any other edit
166
+ // The picker asks for `quiet`: it shows the new mark itself and stays
167
+ // open. Everyone else — the export card's "mark and carry on", which
168
+ // resumes across the reload — gets the reload.
169
+ if (req.quiet === true) quiet(out.html);
170
+ writeDeck(out.html);
171
+ console.log(` theme: ${on ? 'marked' : 'unmarked'} ${deckRef}`);
172
+ }
173
+ return json(200, { ok: true, ref: deckRef, marked: on, changed: out.changed, ...history.counts() });
174
+ }
175
+ // Every design system every registered marketplace offers, each saying
176
+ // whether this deck references it (SPEC DESIGN_SYSTEMS) — the palette's
177
+ // "Design systems…" list. Cache-only, like the theme browse above.
178
+ async function designSystemBrowseRoute({ json }) {
179
+ const { marketplaceDesignSystems, designSystemRefs, resolveDesignSystemRef } = await import('./design-system-refs.mjs');
180
+ const { markedSources } = await import('./theme-refs.mjs');
181
+ const { systems, stale, unfetched } = marketplaceDesignSystems();
182
+ const html = readDeck();
183
+ const sources = markedSources(html);
184
+ // what the deck references, as THIS machine names the catalog
185
+ const used = new Set(designSystemRefs(html).map((r) => {
186
+ const hit = resolveDesignSystemRef(r, undefined, { source: sources[r.marketplace] ?? null });
187
+ return `${r.name}@${hit.local ?? r.marketplace}`;
188
+ }));
189
+ return json(200, { ok: true, systems: systems.map((s) => ({ ...s, used: used.has(s.qualified) })), stale, unfetched, cacheOnly: true });
190
+ }
191
+
192
+ /**
193
+ * Reference a design system from this deck, or drop it (SPEC
194
+ * DESIGN_SYSTEMS) — the twin of the theme mark below it. The package runs
195
+ * through `decklight design-system gate` first, so the command line and
196
+ * this route refuse by the same code; the deck gains or loses one entry in
197
+ * its config block as one undo entry.
198
+ */
199
+ async function designSystemMarkRoute({ body, json }) {
200
+ const req = JSON.parse(body || '{}');
201
+ const ref = typeof req.ref === 'string' ? req.ref.trim() : '';
202
+ const on = req.used !== false && req.marked !== false;
203
+ const { parseRef, refForDeck } = await import('./theme-refs.mjs');
204
+ const { resolveDesignSystemRef, setDesignSystem, designSystemRefs } = await import('./design-system-refs.mjs');
205
+ const { checkDir } = await import('./design-system.mjs');
206
+ const { MarketplaceError, recordInstall, loadRegistry, configHome } = await import('./marketplace.mjs');
207
+ if (!parseRef(ref)) return json(400, { ok: false, error: 'which design system? — name@marketplace' });
208
+ const before = readDeck();
209
+ let deckRef = ref, source = null, manifest = null, local = null;
210
+ if (on) {
211
+ const r = resolveDesignSystemRef(ref);
212
+ if (!r.dir) {
213
+ const status = r.entry && r.entry.type !== 'design-system' ? 400 : r.entry ? 409 : 404;
214
+ return json(status, { ok: false, error: r.missing });
215
+ }
216
+ const verdict = checkDir(r.dir);
217
+ if (!verdict.ok) {
218
+ // the deck is left byte-for-byte unchanged
219
+ return json(400, { ok: false, error: `${r.ref} fails the design-system check`, problems: verdict.problems.map((p) => `${p.file}${p.line ? ` line ${p.line}` : ''}: ${p.msg}`) });
220
+ }
221
+ source = r.source ?? null;
222
+ manifest = verdict.manifest;
223
+ local = r.local;
224
+ deckRef = refForDeck(before, r.name, r.local, source);
225
+ if (!designSystemRefs(before).some((x) => x.ref === deckRef)) {
226
+ recordInstall({ type: 'design-system', name: r.name, marketplace: r.local, version: r.entry.version ?? null,
227
+ commit: loadRegistry(configHome()).marketplaces?.[r.local]?.commit ?? null });
228
+ }
229
+ } else {
230
+ // the deck's own spelling, whatever this machine calls the catalog
231
+ const p = parseRef(ref);
232
+ deckRef = designSystemRefs(before).find((x) => x.ref === ref || x.name === p.name)?.ref ?? ref;
233
+ }
234
+ let out;
235
+ try { out = setDesignSystem(before, deckRef, on, { source }); }
236
+ catch (e) {
237
+ if (e instanceof MarketplaceError) return json(409, { ok: false, error: e.message });
238
+ throw e;
239
+ }
240
+ // Its themes and fonts come with it (SPEC DESIGN_SYSTEMS, #642) — in the
241
+ // same write, so the whole thing is ONE undo — unless the author held ⇧
242
+ // (recommended: false). The look is offered, and applied only when asked.
243
+ let html = out.html, plan = null, look = null;
244
+ if (on && manifest) {
245
+ const deps = await import('./design-system-deps.mjs');
246
+ plan = deps.planRecommended(html, { manifest, local });
247
+ if (req.recommended !== false) {
248
+ html = deps.writePlan(html, plan);
249
+ for (const inst of deps.plannedInstalls(plan)) recordInstall(inst);
250
+ }
251
+ look = deps.lookOf(html, req.recommended !== false ? plan
252
+ : { ...plan, items: plan.items.filter((it) => it.status === 'already' || it.status === 'stack') });
253
+ if (req.apply === true && look.differs) html = deps.applyLook(html, look);
254
+ look.phrase = deps.lookPhrase(look, plan);
255
+ }
256
+ const changed = html !== before;
257
+ if (changed) {
258
+ history.record(before); // Z takes it back like any other edit
259
+ if (req.quiet === true) quiet(html);
260
+ writeDeck(html);
261
+ console.log(` design system: ${on ? 'referenced' : 'dropped'} ${deckRef}`);
262
+ }
263
+ const pulled = plan && req.recommended !== false ? plan.items.map(({ kind, rec, ref: r2, status, why, cmd, family }) => ({ kind, rec, ref: r2, status, why, cmd, family })) : [];
264
+ return json(200, { ok: true, ref: deckRef, used: on, changed, pulled, look, ...history.counts() });
265
+ }
266
+
267
+ /**
268
+ * Give the deck the look a design system it uses was drawn for (SPEC
269
+ * DESIGN_SYSTEMS, #642): its first recommended theme and font, bringing any
270
+ * that are missing — one edit, one undo, so Z puts the old look back and
271
+ * keeps the design system. The twin of `decklight design-system apply`.
272
+ */
273
+ async function designSystemApplyRoute({ body, json }) {
274
+ const req = JSON.parse(body || '{}');
275
+ const { designSystemRefs, resolveDesignSystemRef } = await import('./design-system-refs.mjs');
276
+ const { markedSources, parseRef } = await import('./theme-refs.mjs');
277
+ const { checkDir } = await import('./design-system.mjs');
278
+ const { recordInstall } = await import('./marketplace.mjs');
279
+ const deps = await import('./design-system-deps.mjs');
280
+ const before = readDeck();
281
+ const p = parseRef(String(req.ref ?? ''));
282
+ const hit = designSystemRefs(before).find((x) => x.ref === req.ref || x.name === (p?.name ?? req.ref));
283
+ if (!hit) return json(404, { ok: false, error: `this deck does not use ${req.ref}` });
284
+ const r = resolveDesignSystemRef(hit, undefined, { source: markedSources(before)[hit.marketplace] ?? null });
285
+ if (!r.dir) return json(409, { ok: false, error: r.missing });
286
+ const verdict = checkDir(r.dir);
287
+ if (!verdict.ok) return json(409, { ok: false, error: `${hit.ref} no longer passes its check` });
288
+ const plan = deps.planRecommended(before, { manifest: verdict.manifest, local: r.local });
289
+ let html = deps.writePlan(before, plan);
290
+ const look = deps.lookOf(html, plan);
291
+ if (look.differs) html = deps.applyLook(html, look);
292
+ look.phrase = deps.lookPhrase(look, plan);
293
+ // recorded even when the deck is unchanged: a dependency already there
294
+ // takes the catalog's version now, as `design-system apply` does (#653)
295
+ for (const inst of deps.plannedInstalls(plan)) recordInstall(inst);
296
+ if (html !== before) {
297
+ history.record(before);
298
+ writeDeck(html);
299
+ console.log(` design system: applied ${look.title}'s look — ${look.phrase || 'already worn'}`);
300
+ }
301
+ return json(200, { ok: true, ref: hit.ref, changed: html !== before, look, ...history.counts() });
302
+ }
303
+
304
+ // Every font every registered marketplace offers (SPEC FONTS), each saying
305
+ // whether this deck references it, with the faces a preview needs — the
306
+ // font picker's marketplace rows. Cache-only, like the theme browse.
307
+ async function fontBrowseRoute({ json }) {
308
+ const { marketplaceFonts, fontRefs, resolveFontRef } = await import('./font-refs.mjs');
309
+ const { markedSources } = await import('./theme-refs.mjs');
310
+ const { fonts, stale, unfetched } = marketplaceFonts();
311
+ const html = readDeck();
312
+ const sources = markedSources(html);
313
+ const used = new Set(fontRefs(html).map((r) => {
314
+ const hit = resolveFontRef(r, undefined, { source: sources[r.marketplace] ?? null });
315
+ return `${r.name}@${hit.local ?? r.marketplace}`;
316
+ }));
317
+ return json(200, { ok: true, fonts: fonts.map((f) => ({ ...f, used: used.has(f.qualified) })), stale, unfetched, cacheOnly: true });
318
+ }
319
+
320
+ /**
321
+ * Reference a font from this deck, or drop it (SPEC FONTS) — the twin of
322
+ * the design-system mark above: `font gate` first, one config entry, one
323
+ * undo. `use: true` also makes it the deck's default font. `quiet` writes
324
+ * without the reload: the picker previews the face itself, and stays open.
325
+ */
326
+ async function fontMarkRoute({ body, json }) {
327
+ const req = JSON.parse(body || '{}');
328
+ const ref = typeof req.ref === 'string' ? req.ref.trim() : '';
329
+ const on = req.used !== false && req.marked !== false;
330
+ const { parseRef, refForDeck } = await import('./theme-refs.mjs');
331
+ const { resolveFontRef, setFont, fontRefs } = await import('./font-refs.mjs');
332
+ const { checkFontDir } = await import('./font.mjs');
333
+ const { MarketplaceError, recordInstall, loadRegistry, configHome } = await import('./marketplace.mjs');
334
+ if (!parseRef(ref)) return json(400, { ok: false, error: 'which font? — name@marketplace' });
335
+ const before = readDeck();
336
+ let deckRef = ref, source = null;
337
+ if (on) {
338
+ const r = resolveFontRef(ref);
339
+ if (!r.dir) {
340
+ const status = r.entry && r.entry.type !== 'font' ? 400 : r.entry ? 409 : 404;
341
+ return json(status, { ok: false, error: r.missing });
342
+ }
343
+ const verdict = checkFontDir(r.dir);
344
+ if (!verdict.ok) {
345
+ return json(400, { ok: false, error: `${r.ref} fails the font check`, problems: verdict.problems.map((p) => `${p.file}${p.line ? ` line ${p.line}` : ''}: ${p.msg}`) });
346
+ }
347
+ source = r.source ?? null;
348
+ deckRef = refForDeck(before, r.name, r.local, source);
349
+ if (!fontRefs(before).some((x) => x.ref === deckRef)) {
350
+ recordInstall({ type: 'font', name: r.name, marketplace: r.local, version: r.entry.version ?? null,
351
+ commit: loadRegistry(configHome()).marketplaces?.[r.local]?.commit ?? null });
352
+ }
353
+ } else {
354
+ const p = parseRef(ref);
355
+ deckRef = fontRefs(before).find((x) => x.ref === ref || x.name === p.name)?.ref ?? ref;
356
+ }
357
+ let out;
358
+ try { out = setFont(before, deckRef, on, { source, use: req.use === true }); }
359
+ catch (e) {
360
+ if (e instanceof MarketplaceError) return json(409, { ok: false, error: e.message });
361
+ throw e;
362
+ }
363
+ if (out.changed) {
364
+ history.record(before);
365
+ if (req.quiet === true) quiet(out.html);
366
+ writeDeck(out.html);
367
+ console.log(` font: ${on ? 'referenced' : 'dropped'} ${deckRef}${on && req.use === true ? ' (the deck\'s default)' : ''}`);
368
+ }
369
+ return json(200, { ok: true, ref: deckRef, used: on, changed: out.changed, ...history.counts() });
370
+ }
371
+
372
+ // `/deck/edit/theme/add` was 0.9.0's name for installing from Browse. A deck that
373
+ // carries its OWN copy of the runtime still asks for it; it means "mark".
374
+ const themeAddRoute = ({ body, json }) =>
375
+ themeMarkRoute({ body: JSON.stringify({ ref: JSON.parse(body || '{}').ref, marked: true }), json });
376
+
377
+ routes.set('POST /deck/edit/theme', themeRoute);
378
+ routes.set('POST /deck/edit/brand-themes', brandThemesRoute);
379
+ routes.set('GET /deck/edit/theme/browse', themeBrowseRoute);
380
+ routes.set('POST /deck/edit/theme/add', themeAddRoute);
381
+ routes.set('POST /deck/edit/theme/mark', themeMarkRoute);
382
+ routes.set('GET /deck/edit/design-system/browse', designSystemBrowseRoute);
383
+ routes.set('POST /deck/edit/design-system/mark', designSystemMarkRoute);
384
+ routes.set('POST /deck/edit/design-system/apply', designSystemApplyRoute);
385
+ routes.set('GET /deck/edit/font/browse', fontBrowseRoute);
386
+ routes.set('POST /deck/edit/font/mark', fontMarkRoute);
387
+ return routes;
388
+ }