decklight 0.8.1 → 0.10.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/PLATFORM.md +707 -0
- package/README.md +226 -150
- package/SPEC.md +803 -888
- package/cli/agent-review.mjs +246 -0
- package/cli/agents.mjs +35 -9
- package/cli/associate.mjs +12 -12
- package/cli/audit.mjs +42 -10
- package/cli/banner.mjs +41 -17
- package/cli/bundle-audio.mjs +190 -0
- package/cli/bundle-module.mjs +95 -0
- package/cli/bundle.mjs +439 -35
- package/cli/cast.mjs +48 -12
- package/cli/check-fixes.mjs +186 -0
- package/cli/check.mjs +988 -0
- package/cli/clone-deck.mjs +179 -0
- package/cli/commands.mjs +345 -0
- package/cli/comments.mjs +64 -18
- package/cli/commit-flow.mjs +51 -22
- package/cli/commit-message.mjs +51 -74
- package/cli/commit-split.mjs +601 -0
- package/cli/deck-refs.mjs +64 -0
- package/cli/deck-routes.mjs +67 -0
- package/cli/deckfile.mjs +3 -5
- package/cli/decklight.mjs +68 -289
- package/cli/design-system-deps.mjs +209 -0
- package/cli/design-system-edit.mjs +194 -0
- package/cli/design-system-refs.mjs +666 -0
- package/cli/design-system.mjs +497 -0
- package/cli/doctor.mjs +115 -0
- package/cli/edit-git.mjs +353 -0
- package/cli/edit-handover.mjs +551 -0
- package/cli/edit-look.mjs +631 -0
- package/cli/edit-slides.mjs +683 -0
- package/cli/edit.mjs +2585 -855
- package/cli/enhance.mjs +485 -0
- package/cli/extension.mjs +10 -10
- package/cli/font-refs.mjs +273 -0
- package/cli/font.mjs +246 -0
- package/cli/git-tree.mjs +1 -1
- package/cli/git.mjs +78 -11
- package/cli/import.mjs +56 -10
- package/cli/init.mjs +407 -236
- package/cli/library.mjs +127 -0
- package/cli/loader.mjs +3 -1
- package/cli/marketplace-autocheck.mjs +200 -0
- package/cli/marketplace.mjs +460 -29
- package/cli/narrate.mjs +251 -0
- package/cli/open-browser.mjs +63 -0
- package/cli/open.mjs +961 -0
- package/cli/pdf.mjs +64 -17
- package/cli/pkg.mjs +44 -2
- package/cli/plugin.mjs +43 -41
- package/cli/port-conflict.mjs +53 -23
- package/cli/pptx-export.mjs +63 -27
- package/cli/publish.mjs +8 -4
- package/cli/qr.mjs +1 -1
- package/cli/record.mjs +85 -23
- package/cli/remote.mjs +43 -17
- package/cli/restore.mjs +11 -4
- package/cli/review-ref.mjs +125 -0
- package/cli/review-remote.mjs +92 -48
- package/cli/review-routes.mjs +254 -0
- package/cli/review-store.mjs +47 -7
- package/cli/review-submit.mjs +47 -57
- package/cli/runtime-link.mjs +560 -0
- package/cli/serve.mjs +236 -45
- package/cli/setup.mjs +112 -0
- package/cli/sign.mjs +1 -1
- package/cli/skill-content.mjs +229 -35
- package/cli/skills.mjs +353 -26
- package/cli/start.mjs +110 -0
- package/cli/supervise.mjs +6 -6
- package/cli/theme-refs.mjs +527 -0
- package/cli/theme.mjs +181 -137
- package/cli/trust.mjs +83 -0
- package/cli/units.mjs +112 -47
- package/cli/update-check.mjs +1 -5
- package/cli/upgrade.mjs +194 -28
- package/cli/upstream.mjs +4 -3
- package/cli/util.mjs +4 -4
- package/cli/wizard.mjs +6 -6
- package/dist/build-info.json +1 -1
- package/dist/decklight.css +1533 -59
- package/dist/decklight.js +258 -115
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +141 -0
- package/docs/architecture.svg +17 -17
- package/package.json +4 -2
- package/themes/README.md +9 -8
- package/themes/reveal-solarized.css +10 -0
- package/tools/args.mjs +20 -0
- package/tools/atomic-write.mjs +48 -0
- package/tools/brand-palette.mjs +717 -0
- package/tools/bridge.mjs +1 -1
- package/tools/build-groups.mjs +42 -0
- package/tools/color.mjs +48 -0
- package/tools/deck-html.mjs +396 -11
- package/tools/design-system-format.mjs +778 -0
- package/tools/design-system-migrations.mjs +207 -0
- package/tools/elevenlabs-tts.mjs +110 -17
- package/tools/exec.mjs +11 -3
- package/tools/extension-check.mjs +31 -7
- package/tools/font-format.mjs +190 -0
- package/tools/font-stacks.mjs +24 -0
- package/tools/gemini-tts.mjs +3 -0
- package/tools/lipsync-config.mjs +103 -0
- package/tools/lipsync-engines.mjs +205 -2
- package/tools/lipsync-server.mjs +264 -41
- package/tools/lipsync.mjs +37 -18
- package/tools/local-voice.mjs +32 -42
- package/tools/narration-manifest.mjs +270 -0
- package/tools/narration-synth.mjs +416 -0
- package/tools/pptx.mjs +776 -19
- package/tools/publish-targets.mjs +1 -1
- package/tools/publish-voices.mjs +3 -3
- package/tools/recording-impact.mjs +7 -3
- package/tools/render-theme.mjs +33 -0
- package/tools/review-anchor.mjs +28 -12
- package/tools/semver.mjs +40 -0
- package/tools/sentences.mjs +180 -0
- package/tools/shot.mjs +14 -7
- package/tools/site-clip.mjs +114 -0
- package/tools/site-voices.mjs +232 -0
- package/tools/template-theme.mjs +1 -1
- package/tools/test-impact.mjs +17 -10
- package/tools/theme-check.mjs +30 -3
- package/tools/tts-cache.mjs +4 -3
- package/tools/tts-engines.mjs +38 -20
- package/tools/tts-setup.mjs +7 -7
- package/tools/video-options.mjs +34 -0
- package/tools/video.mjs +530 -54
- package/tools/voiceover-server.mjs +185 -104
- package/tools/voiceover.mjs +119 -261
- package/tools/zip.mjs +1 -1
- package/cli/dev.mjs +0 -623
- package/cli/present.mjs +0 -853
- package/cli/review.mjs +0 -361
- package/tools/publish-site-voices.mjs +0 -75
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// The routes that CHANGE A SLIDE: notes, sources, rehearsed timings, layout,
|
|
5
|
+
// hidden, the four element-edit ones, whole slides (new, duplicate, delete,
|
|
6
|
+
// reorder), a dropped image and the `<img>` that points at it, and the
|
|
7
|
+
// narration track the recorder points the deck at (SPEC PRESENTING author
|
|
8
|
+
// mode).
|
|
9
|
+
//
|
|
10
|
+
// They came out of cli/edit.mjs because they are the one group in that server
|
|
11
|
+
// that needs almost nothing FROM it. Each is the same three steps — read the
|
|
12
|
+
// deck, run one pure `(html, …) → html` transform over it, put the result
|
|
13
|
+
// through `applyEdit` so `Z` takes it back — and between them they want four
|
|
14
|
+
// bindings, not the fifty editMain has in scope. Naming those four in a
|
|
15
|
+
// parameter list is what lets a handler be called from a test with a temp file
|
|
16
|
+
// and no socket at all, which is how the ones here are covered.
|
|
17
|
+
//
|
|
18
|
+
// `/deck/edit/asset` is the one that is not a text transform at all — it writes an
|
|
19
|
+
// image file beside the deck — and it sits here because what an author does
|
|
20
|
+
// next is put that image on a slide, and the two halves of one drop should not
|
|
21
|
+
// be two files apart.
|
|
22
|
+
//
|
|
23
|
+
// The transforms themselves stay in edit.mjs: they are the deck-rewriting
|
|
24
|
+
// vocabulary the whole command shares (and what test/edit.test.mjs cites), so
|
|
25
|
+
// this file imports them rather than owning them — which makes the two modules
|
|
26
|
+
// a cycle, deliberately and STATICALLY. Not dynamically: edit.mjs ends in a
|
|
27
|
+
// top-level `await` (its isMain boot), so an `await import('./edit-slides.mjs')`
|
|
28
|
+
// from inside editMain would be waiting on an evaluation that is waiting on it,
|
|
29
|
+
// and the edit server would come up to the agents line and hang there — which
|
|
30
|
+
// is exactly what it did. A static cycle has no such moment: every binding on
|
|
31
|
+
// both sides is a hoisted function declaration, and nothing is read until a
|
|
32
|
+
// request arrives.
|
|
33
|
+
|
|
34
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
35
|
+
import { basename, resolve, sep } from 'node:path';
|
|
36
|
+
|
|
37
|
+
import {
|
|
38
|
+
notesTextToAside, setSlideNotes,
|
|
39
|
+
sourcesToAside, setSlideSources,
|
|
40
|
+
setSlideTiming, setSlideLayout, setSlideHidden,
|
|
41
|
+
upsertNarrationTrack,
|
|
42
|
+
locateElement, removeSlideElement, setSlideElementHtml, setSlideElementBuild, setElementStyles, setElementAttr, setElementText, removeNestedElement } from './edit.mjs';
|
|
43
|
+
import { oneline } from './git.mjs';
|
|
44
|
+
import { slotWriteProblem, applySlideTemplate, insertSlideTemplateSlide, describeTemplateChange } from './design-system-edit.mjs';
|
|
45
|
+
import { designSystemRefs, resolveDesignSystemRef, packageVerdict } from './design-system-refs.mjs';
|
|
46
|
+
import { markedSources } from './theme-refs.mjs';
|
|
47
|
+
import { parseTemplateRef } from '../tools/design-system-format.mjs';
|
|
48
|
+
import { setDeckConcepts } from './runtime-link.mjs';
|
|
49
|
+
// The whole-slide and image transforms are NOT in edit.mjs with the rest: they
|
|
50
|
+
// are string surgery on a deck's sections, which is what tools/deck-html.mjs
|
|
51
|
+
// is, and nothing about them wants the server.
|
|
52
|
+
import {
|
|
53
|
+
sectionBodies, insertBlankSlide, duplicateSlide, deleteSlide, swapSlides, insertImage,
|
|
54
|
+
} from '../tools/deck-html.mjs';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Put the slide-mutation routes into `routes`, the table cli/edit.mjs
|
|
58
|
+
* dispatches on, and hand it back.
|
|
59
|
+
*
|
|
60
|
+
* `ctx` is the whole of what these handlers may touch, written out rather than
|
|
61
|
+
* closed over: `readDeck()` for the current file, `applyEdit()` for the one
|
|
62
|
+
* door every mutation goes through (snapshot, then write), `history` for the
|
|
63
|
+
* undo/redo counts every one of them reports back, and `deckPath` — the only
|
|
64
|
+
* one that is not about the deck's TEXT — because a dropped image is saved
|
|
65
|
+
* beside the deck and nowhere else.
|
|
66
|
+
*/
|
|
67
|
+
export function registerSlideRoutes(routes, { readDeck, applyEdit, history, deckPath, quiet = () => {}, broadcast = () => {} }) {
|
|
68
|
+
function notesRoute({ body, json }) {
|
|
69
|
+
const { slide, text, from } = JSON.parse(body);
|
|
70
|
+
if (!Number.isInteger(slide) || slide < 1 || typeof text !== 'string') throw new Error('bad payload');
|
|
71
|
+
// `changed` says whether a byte was written: an edit that normalises to
|
|
72
|
+
// the same notes writes nothing, so no reload follows — the page must
|
|
73
|
+
// not sit waiting for one (#646)
|
|
74
|
+
const aside = notesTextToAside(text);
|
|
75
|
+
const changed = applyEdit((html) => setSlideNotes(html, slide, aside));
|
|
76
|
+
if (changed) {
|
|
77
|
+
// Saved IN PLACE, never by a reload: the notes came from the page, so
|
|
78
|
+
// the page already shows them. The write is quiet (the watcher skips
|
|
79
|
+
// its reload), and every open view of the deck (another tab, the
|
|
80
|
+
// speaker view's deck) is sent the slide's new notes instead, so none
|
|
81
|
+
// keeps the old ones. Anything else that changes the file still reloads.
|
|
82
|
+
quiet(readDeck());
|
|
83
|
+
// `from` is the saving page's own id, echoed so that page leaves its
|
|
84
|
+
// box as the author typed it rather than as the file normalised it
|
|
85
|
+
broadcast('notes', { slide, aside, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
86
|
+
console.log(` notes saved: slide ${slide} (${text.length} chars), updated in place`);
|
|
87
|
+
}
|
|
88
|
+
return json(200, { ok: true, changed: !!changed, inPlace: !!changed, ...history.counts() });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Where a slide got what it says, written back (SLIDE_SOURCES). One
|
|
92
|
+
// applyEdit, so `Z` takes the whole card back the way it takes a note.
|
|
93
|
+
function sourcesRoute({ body, json }) {
|
|
94
|
+
const { slide, facts, links } = JSON.parse(body);
|
|
95
|
+
if (!Number.isInteger(slide) || slide < 1) throw new Error('bad payload');
|
|
96
|
+
const dropped = [];
|
|
97
|
+
const inner = sourcesToAside({
|
|
98
|
+
facts: Array.isArray(facts) ? facts : [],
|
|
99
|
+
links: Array.isArray(links) ? links : [],
|
|
100
|
+
}, dropped);
|
|
101
|
+
const changed = applyEdit((html) => setSlideSources(html, slide, inner));
|
|
102
|
+
if (changed) {
|
|
103
|
+
console.log(` sources saved: slide ${slide}`
|
|
104
|
+
+ (inner ? ` (${(facts ?? []).length} fact(s), ${(links ?? []).length} link(s))` : ' — cleared')
|
|
105
|
+
+ (dropped.length ? ` — refused ${dropped.join(', ')}` : ''));
|
|
106
|
+
}
|
|
107
|
+
return json(200, { ok: true, changed, dropped, ...history.counts() });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function timingsRoute({ body, json }) {
|
|
111
|
+
// every slide's rehearsed time in ONE edit — one undo entry, one commit
|
|
112
|
+
const { timings } = JSON.parse(body);
|
|
113
|
+
if (!Array.isArray(timings) || !timings.every((t) => Number.isInteger(t?.slide) && t.slide >= 1 && Number.isFinite(t?.seconds))) throw new Error('bad payload');
|
|
114
|
+
const changed = applyEdit((html) => timings.reduce((h, t) => setSlideTiming(h, t.slide, t.seconds), html));
|
|
115
|
+
if (changed) console.log(` rehearsal timings saved: ${timings.length} slides`);
|
|
116
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function layoutRoute({ body, json }) {
|
|
120
|
+
const { slide, layout, quiet: inPlace, from } = JSON.parse(body);
|
|
121
|
+
if (!Number.isInteger(slide) || slide < 1 || typeof layout !== 'string') throw new Error('bad payload');
|
|
122
|
+
const changed = applyEdit((html) => setSlideLayout(html, slide, layout));
|
|
123
|
+
if (changed) {
|
|
124
|
+
console.log(` layout saved: slide ${slide} → ${layout}`);
|
|
125
|
+
// The page that asked already shows the pick: an L press applies it
|
|
126
|
+
// before the write. Asked for IN PLACE, the write is quiet (the
|
|
127
|
+
// watcher skips its reload) and every page is sent the pick to put on
|
|
128
|
+
// the slide itself — the notes save's shape, for the same reason.
|
|
129
|
+
if (inPlace === true) {
|
|
130
|
+
quiet(readDeck());
|
|
131
|
+
broadcast('layout', { slide, layout, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return json(200, { ok: true, changed, inPlace: changed && inPlace === true, ...history.counts() });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A layout of one of THIS deck's design systems, as the server resolves it
|
|
139
|
+
* (SPEC DESIGN_SYSTEMS) — `{ ref, id, slots }`, or a sentence saying why not.
|
|
140
|
+
* Read from the package on disk through the same resolver and check every
|
|
141
|
+
* server uses, never from the page that asked.
|
|
142
|
+
*/
|
|
143
|
+
function deckTemplate(html, value) {
|
|
144
|
+
const want = parseTemplateRef(value);
|
|
145
|
+
if (!want) return { error: `"${value}" is not a slide template (name/template)` };
|
|
146
|
+
const ref = designSystemRefs(html).find((r) => r.name === want.system);
|
|
147
|
+
if (!ref) return { error: `the deck does not use a design system called "${want.system}" — Design systems… adds one` };
|
|
148
|
+
const hit = resolveDesignSystemRef(ref, undefined, { source: markedSources(html)[ref.marketplace] ?? null });
|
|
149
|
+
if (!hit.dir) return { error: `${ref.ref} — ${hit.missing}` };
|
|
150
|
+
const verdict = packageVerdict(hit.dir);
|
|
151
|
+
if (!verdict.ok) return { error: `${ref.ref} — ${verdict.why}` };
|
|
152
|
+
const layout = verdict.summary.templates.find((l) => l.id === want.template);
|
|
153
|
+
if (!layout) return { error: `${ref.ref} has no template "${want.template}" — it has ${verdict.summary.templates.map((l) => l.id).join(', ')}` };
|
|
154
|
+
return { layout: { ref: `${want.system}/${want.template}`, id: layout.id, slots: layout.slots } };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Put a slide into a slide template, move it to another, take it out,
|
|
159
|
+
* or insert a new slide in one (SPEC DESIGN_SYSTEMS) — one edit, one undo,
|
|
160
|
+
* and what went where, said back.
|
|
161
|
+
* { slide, layout: "acme/divider" } convert or switch slide N
|
|
162
|
+
* { slide, layout: null } remove: back to a plain slide
|
|
163
|
+
* { slide, layout: "acme/divider", insert: true } a new slide after slide N
|
|
164
|
+
*/
|
|
165
|
+
function slideTemplateRoute({ body, json }) {
|
|
166
|
+
const { slide, template, insert } = JSON.parse(body || '{}');
|
|
167
|
+
if (!Number.isInteger(slide) || slide < 1 || !(template === null || typeof template === 'string')) throw new Error('bad payload');
|
|
168
|
+
const html = readDeck();
|
|
169
|
+
let def = null;
|
|
170
|
+
if (template !== null) {
|
|
171
|
+
const r = deckTemplate(html, template);
|
|
172
|
+
if (r.error) return json(409, { ok: false, error: r.error });
|
|
173
|
+
def = r.layout;
|
|
174
|
+
} else if (insert) return json(400, { ok: false, error: 'insert needs a template' });
|
|
175
|
+
let report = null;
|
|
176
|
+
try {
|
|
177
|
+
const changed = applyEdit((deck) => {
|
|
178
|
+
if (insert) return insertSlideTemplateSlide(deck, slide, def);
|
|
179
|
+
const out = applySlideTemplate(deck, slide, def);
|
|
180
|
+
report = out;
|
|
181
|
+
return out.html;
|
|
182
|
+
});
|
|
183
|
+
const said = insert ? `a new ${def.ref} slide after slide ${slide}` : describeTemplateChange(report, def?.ref ?? '');
|
|
184
|
+
if (changed) console.log(` slide template: slide ${slide} — ${said}`);
|
|
185
|
+
return json(200, {
|
|
186
|
+
ok: true, changed, said, ...(insert ? { inserted: slide + 1 } : { mode: report.mode, moved: report.moved, lacking: report.lacking }),
|
|
187
|
+
...history.counts(),
|
|
188
|
+
});
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return json(404, { ok: false, error: oneline(e) });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function hiddenRoute({ body, json }) {
|
|
195
|
+
const { slide, hidden } = JSON.parse(body);
|
|
196
|
+
if (!Number.isInteger(slide) || slide < 1 || typeof hidden !== 'boolean') throw new Error('bad payload');
|
|
197
|
+
const changed = applyEdit((html) => setSlideHidden(html, slide, hidden));
|
|
198
|
+
if (changed) console.log(` slide ${slide} ${hidden ? 'hidden' : 'shown again'}`);
|
|
199
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function narrationRoute({ body, json }) {
|
|
203
|
+
const { files, ext, segments } = JSON.parse(body || '{}');
|
|
204
|
+
// The same three shapes /deck/edit/narration/record refuses for a folder, refused
|
|
205
|
+
// again here: this one is written INTO the deck, where a bad value is
|
|
206
|
+
// not a failed request but a deck that no longer plays.
|
|
207
|
+
if (typeof files !== 'string' || !files.trim() || files.length > 200
|
|
208
|
+
|| /^[/\\]/.test(files) || /^[a-zA-Z]:/.test(files)
|
|
209
|
+
|| files.split(/[/\\]/).includes('..')) {
|
|
210
|
+
return json(400, { ok: false, error: 'bad narration folder' });
|
|
211
|
+
}
|
|
212
|
+
if (ext !== undefined && !/^[a-z0-9]{1,5}$/.test(String(ext))) {
|
|
213
|
+
return json(400, { ok: false, error: 'bad ext' });
|
|
214
|
+
}
|
|
215
|
+
if (segments !== undefined && typeof segments !== 'boolean') {
|
|
216
|
+
return json(400, { ok: false, error: 'bad segments' });
|
|
217
|
+
}
|
|
218
|
+
const { label } = JSON.parse(body || '{}');
|
|
219
|
+
if (label !== undefined && (typeof label !== 'string' || label.length > 120)) {
|
|
220
|
+
return json(400, { ok: false, error: 'bad label' });
|
|
221
|
+
}
|
|
222
|
+
// upsert, never replace: a deck carries as many tracks as you have
|
|
223
|
+
// voices, and writing one must not throw the others away. Read here rather
|
|
224
|
+
// than inside applyEdit because `null` is an ANSWER, not an edit — it has
|
|
225
|
+
// to be looked at before anything is written.
|
|
226
|
+
const before = readDeck();
|
|
227
|
+
const next = upsertNarrationTrack(before, {
|
|
228
|
+
label: label || files.trim(),
|
|
229
|
+
dir: files.trim(),
|
|
230
|
+
...(ext === undefined ? {} : { ext }),
|
|
231
|
+
...(segments === undefined ? {} : { segments }),
|
|
232
|
+
});
|
|
233
|
+
if (next === null) {
|
|
234
|
+
// Not a failure of this server — a deck whose config is built
|
|
235
|
+
// somewhere else. Say which, so the answer is "paste this line",
|
|
236
|
+
// not "it did not work".
|
|
237
|
+
return json(409, { ok: false,
|
|
238
|
+
error: 'this deck builds its config outside the Decklight.init(…) call, so there is no literal here to edit' });
|
|
239
|
+
}
|
|
240
|
+
const changed = applyEdit(next, before);
|
|
241
|
+
if (changed) console.log(` narration: ${files} in the deck's config`);
|
|
242
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ── element edit mode (E, right-click a slide element) — #112 ─────
|
|
246
|
+
// Reads the element's outerHTML fresh from the FILE, never the live
|
|
247
|
+
// DOM: the engine mutates elements in place (pinned-title classes,
|
|
248
|
+
// namespaced SVG ids, chart/code/math subtree replacement), so the DOM
|
|
249
|
+
// the player sees is not what a Save should write back over.
|
|
250
|
+
function elementSourceRoute({ url, json }) {
|
|
251
|
+
const slide = Number(url.searchParams.get('slide'));
|
|
252
|
+
const index = Number(url.searchParams.get('index'));
|
|
253
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0) {
|
|
254
|
+
return json(400, { ok: false, error: 'bad payload' });
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
const { parts, idx, r } = locateElement(readDeck(), slide, index);
|
|
258
|
+
return json(200, { ok: true, html: parts[idx].slice(r.start, r.end) });
|
|
259
|
+
} catch (e) {
|
|
260
|
+
return json(404, { ok: false, error: oneline(e) });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// The three that WRITE an element. Same door as layout and notes: a pure
|
|
265
|
+
// (html, slide, index, …) → html transform, through applyEdit, onto the ONE
|
|
266
|
+
// undo/redo stack. A content or label save asked `quiet` is written in
|
|
267
|
+
// place: the page that saved already shows it, so the watcher skips its
|
|
268
|
+
// reload and every other page is sent a `content` event instead.
|
|
269
|
+
// `path` (a node inside the element, the selection inside a diagram) removes
|
|
270
|
+
// that node alone; without it the whole element goes
|
|
271
|
+
function elementRemoveRoute({ body, json }) {
|
|
272
|
+
const { slide, index, path = null, tag = null } = JSON.parse(body);
|
|
273
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0) throw new Error('bad payload');
|
|
274
|
+
let changed;
|
|
275
|
+
try {
|
|
276
|
+
changed = applyEdit((html) => (path ? removeNestedElement(html, slide, index, { path, tag }) : removeSlideElement(html, slide, index)));
|
|
277
|
+
} catch (e) { if (e.code !== 'STALE') throw e; return json(409, { ok: false, error: oneline(e) }); }
|
|
278
|
+
if (changed) console.log(` element removed: slide ${slide} #${index}${path ? ` › ${path.join('.')}` : ''}`);
|
|
279
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function elementContentRoute({ body, json }) {
|
|
283
|
+
const { slide, index, html, quiet: inPlace = false, from = null } = JSON.parse(body);
|
|
284
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0 || typeof html !== 'string') {
|
|
285
|
+
throw new Error('bad payload');
|
|
286
|
+
}
|
|
287
|
+
// on a design-system slide an edit changes what an element says, never
|
|
288
|
+
// the slot it fills (SPEC DESIGN_SYSTEMS)
|
|
289
|
+
const dropped = slotWriteProblem(readDeck(), slide, index, html);
|
|
290
|
+
if (dropped) return json(409, { ok: false, error: dropped });
|
|
291
|
+
const changed = applyEdit((deck) => setSlideElementHtml(deck, slide, index, html));
|
|
292
|
+
if (changed) console.log(` element content saved: slide ${slide} #${index}`);
|
|
293
|
+
if (changed && inPlace === true) {
|
|
294
|
+
quiet(readDeck());
|
|
295
|
+
broadcast('content', { slide, index, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
296
|
+
}
|
|
297
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function elementEffectRoute({ body, json }) {
|
|
301
|
+
const { slide, index, effect } = JSON.parse(body);
|
|
302
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0
|
|
303
|
+
|| (effect !== null && typeof effect !== 'string')) {
|
|
304
|
+
throw new Error('bad payload');
|
|
305
|
+
}
|
|
306
|
+
const changed = applyEdit((html) => setSlideElementBuild(html, slide, index, effect));
|
|
307
|
+
if (changed) console.log(` element effect saved: slide ${slide} #${index} → ${effect ?? '(removed)'}`);
|
|
308
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* `POST /deck/edit/element/style` — `{ slide, index, edits, concept? }`: the
|
|
313
|
+
* colour picker's save (element edit mode → Colors…). One applyEdit for a
|
|
314
|
+
* shape's fill and its text together, so `Z` takes the pair back in one
|
|
315
|
+
* press. A pasted style (Paste style) can carry the shape's concept too,
|
|
316
|
+
* `concept: { path, tag, value }` (value null takes it off), written in the
|
|
317
|
+
* same edit: a style that is a concept is that concept, not its colour.
|
|
318
|
+
*/
|
|
319
|
+
function elementStyleRoute({ body, json }) {
|
|
320
|
+
const { slide, index, edits = [], concept = null, quiet: inPlace, from } = JSON.parse(body);
|
|
321
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0) throw new Error('bad payload');
|
|
322
|
+
if (concept !== null && (typeof concept !== 'object' || !(concept.value === null || typeof concept.value === 'string'))) throw new Error('bad payload');
|
|
323
|
+
if (!concept && (!Array.isArray(edits) || !edits.length)) throw new Error('bad edits');
|
|
324
|
+
let changed;
|
|
325
|
+
try {
|
|
326
|
+
changed = applyEdit((html) => {
|
|
327
|
+
const styled = Array.isArray(edits) && edits.length ? setElementStyles(html, slide, index, edits) : html;
|
|
328
|
+
return concept ? setElementAttr(styled, slide, index,
|
|
329
|
+
{ path: concept.path ?? [], tag: concept.tag ?? null, name: 'data-concept', value: concept.value }) : styled;
|
|
330
|
+
});
|
|
331
|
+
} catch (e) { if (e.code !== 'STALE') throw e; return json(409, { ok: false, error: oneline(e) }); }
|
|
332
|
+
// Asked for IN PLACE (the inspector's Colors and Type tabs), the write is
|
|
333
|
+
// quiet: the page that picked already shows it, and a reload would close
|
|
334
|
+
// the panel it was picked in. Every other page is told, and reloads.
|
|
335
|
+
if (changed && inPlace === true) {
|
|
336
|
+
quiet(readDeck());
|
|
337
|
+
broadcast('styles', { slide, index, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
338
|
+
}
|
|
339
|
+
if (changed) {
|
|
340
|
+
const said = [...(edits ?? []).map((e) => `${e.prop} ${e.value}`), ...(concept ? [`concept ${concept.value ?? '(detached)'}`] : [])];
|
|
341
|
+
console.log(` element colours saved: slide ${slide} #${index} → ${said.join(', ')}`);
|
|
342
|
+
}
|
|
343
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* `POST /deck/edit/element/text` — `{ slide, index, path, tag, was, text }`:
|
|
348
|
+
* a diagram label edited in place (SVG_DIAGRAMS). The words between one
|
|
349
|
+
* `<text>`/`<tspan>`'s tags change and nothing else in the diagram does; a
|
|
350
|
+
* label the file no longer says is a 409.
|
|
351
|
+
*/
|
|
352
|
+
function elementTextRoute({ body, json }) {
|
|
353
|
+
const { slide, index, path = [], tag = null, was, text, quiet: inPlace = false, from = null } = JSON.parse(body);
|
|
354
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0) throw new Error('bad payload');
|
|
355
|
+
let changed;
|
|
356
|
+
try { changed = applyEdit((html) => setElementText(html, slide, index, { path, tag, was, text })); }
|
|
357
|
+
catch (e) { if (e.code !== 'STALE') throw e; return json(409, { ok: false, error: oneline(e) }); }
|
|
358
|
+
if (changed) console.log(` diagram label saved: slide ${slide} #${index} → ${JSON.stringify(text).slice(0, 60)}`);
|
|
359
|
+
if (changed && inPlace === true) {
|
|
360
|
+
quiet(readDeck());
|
|
361
|
+
broadcast('content', { slide, index, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
362
|
+
}
|
|
363
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* `POST /deck/edit/element/concept` — `{ slide, index, path, tag, concept }`:
|
|
368
|
+
* the shape's `data-concept` (SVG_DIAGRAMS, #718), set from the right-click
|
|
369
|
+
* menu or taken off by the colour card's "Detach from concept". The engine
|
|
370
|
+
* recolours on the reload this write brings.
|
|
371
|
+
*/
|
|
372
|
+
function elementConceptRoute({ body, json }) {
|
|
373
|
+
const { slide, index, path = [], tag = null, concept } = JSON.parse(body);
|
|
374
|
+
if (!Number.isInteger(slide) || slide < 1 || !Number.isInteger(index) || index < 0 || !(concept === null || typeof concept === 'string')) throw new Error('bad payload');
|
|
375
|
+
let changed;
|
|
376
|
+
try { changed = applyEdit((html) => setElementAttr(html, slide, index, { path, tag, name: 'data-concept', value: concept })); }
|
|
377
|
+
catch (e) { if (e.code !== 'STALE') throw e; return json(409, { ok: false, error: oneline(e) }); }
|
|
378
|
+
if (changed) console.log(` element concept saved: slide ${slide} #${index} → ${concept ?? '(detached)'}`);
|
|
379
|
+
return json(200, { ok: true, changed, ...history.counts() });
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* `POST /deck/edit/element/concepts` — `{ items: [{ slide, index, path, tag }],
|
|
384
|
+
* concept, quiet?, from? }`: many shapes into one concept (or out of theirs,
|
|
385
|
+
* `concept: null`) in ONE applyEdit, so `Z` takes the whole batch back in
|
|
386
|
+
* one press: the look-alikes offered after an attach, a pick-mode click, a
|
|
387
|
+
* Check deck… suggestion accepted. Asked for in place (`quiet`), the page
|
|
388
|
+
* that asked already shows it; every other page is sent `styles` and
|
|
389
|
+
* reloads. Every item is checked before any is written: one the file no
|
|
390
|
+
* longer has is a 409 and nothing changes.
|
|
391
|
+
*/
|
|
392
|
+
function elementConceptsRoute({ body, json }) {
|
|
393
|
+
const { items, concept, quiet: inPlace = false, from = null } = JSON.parse(body || '{}');
|
|
394
|
+
if (!Array.isArray(items) || !items.length || items.length > 500 || !(concept === null || typeof concept === 'string')
|
|
395
|
+
|| items.some((t) => !t || !Number.isInteger(t.slide) || t.slide < 1 || !Number.isInteger(t.index) || t.index < 0)) {
|
|
396
|
+
throw new Error('bad payload');
|
|
397
|
+
}
|
|
398
|
+
let changed;
|
|
399
|
+
try {
|
|
400
|
+
changed = applyEdit((html) => items.reduce((out, t) => setElementAttr(out, t.slide, t.index,
|
|
401
|
+
{ path: t.path ?? [], tag: t.tag ?? null, name: 'data-concept', value: concept }), html));
|
|
402
|
+
} catch (e) { if (e.code !== 'STALE') throw e; return json(409, { ok: false, error: oneline(e) }); }
|
|
403
|
+
if (changed) {
|
|
404
|
+
const slides = [...new Set(items.map((t) => t.slide))].sort((a, b) => a - b);
|
|
405
|
+
console.log(` element concepts saved: ${items.length} shape${items.length === 1 ? '' : 's'} on slide${slides.length === 1 ? '' : 's'} ${slides.join(', ')} → ${concept ?? '(detached)'}`);
|
|
406
|
+
if (inPlace === true) {
|
|
407
|
+
quiet(readDeck());
|
|
408
|
+
broadcast('styles', { slide: slides[0], index: items[0].index, from: typeof from === 'string' ? from.slice(0, 64) : null });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return json(200, { ok: true, changed, count: items.length, inPlace: changed && inPlace === true, ...history.counts() });
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* `POST /deck/edit/concepts` — `{ concepts, quiet?, from? }`: the deck's
|
|
416
|
+
* concept map, `{ name: slot | colour }`, written the way the deck carries
|
|
417
|
+
* its configuration (`setDeckConcepts`). Quiet and in place like a theme
|
|
418
|
+
* pick: the page that chose already shows it, every other page is sent a
|
|
419
|
+
* `concepts` event and recolours without a reload.
|
|
420
|
+
*/
|
|
421
|
+
function conceptsRoute({ body, json }) {
|
|
422
|
+
const { concepts, quiet: inPlace, from } = JSON.parse(body || '{}');
|
|
423
|
+
if (!concepts || typeof concepts !== 'object' || Array.isArray(concepts)) throw new Error('bad payload');
|
|
424
|
+
let unwritable = false;
|
|
425
|
+
const changed = applyEdit((html) => { const next = setDeckConcepts(html, concepts); if (next === null) unwritable = true; return next ?? html; });
|
|
426
|
+
if (unwritable) return json(409, { ok: false, error: 'the deck carries its configuration in a way this server cannot write — a configuration block, or a Decklight.init call that is data' });
|
|
427
|
+
if (changed) {
|
|
428
|
+
console.log(` concepts saved: ${Object.entries(concepts).map(([k, v]) => `${k}=${v}`).join(', ') || '(none)'}`);
|
|
429
|
+
if (inPlace === true) { quiet(readDeck()); broadcast('concepts', { concepts, from: typeof from === 'string' ? from.slice(0, 64) : null }); }
|
|
430
|
+
}
|
|
431
|
+
return json(200, { ok: true, changed, inPlace: changed && inPlace === true, ...history.counts() });
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ── whole slides: the slide bar's new · duplicate · delete · reorder ──────
|
|
435
|
+
|
|
436
|
+
const SLIDE_OPS = new Set(['new', 'duplicate', 'delete', 'up', 'down', 'move']);
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Slide `from` moved so that it ends up at position `to` — what a drag in
|
|
440
|
+
* the overview asks for. One swap per step, each the same reindenting swap
|
|
441
|
+
* `up` and `down` make, so the moved section and every one it passes keep
|
|
442
|
+
* their indentation; the whole thing is still ONE applyEdit for the caller.
|
|
443
|
+
*/
|
|
444
|
+
function moveSlide(html, from, to) {
|
|
445
|
+
let out = html;
|
|
446
|
+
for (let at = from; at < to; at++) out = swapSlides(out, at, at + 1);
|
|
447
|
+
for (let at = from; at > to; at--) out = swapSlides(out, at - 1, at);
|
|
448
|
+
return out;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* `POST /deck/edit/slide` — `{ op, slide }` (`{ op: 'move', slide, to }` for a
|
|
453
|
+
* drag), one section moved, copied, made or taken away.
|
|
454
|
+
*
|
|
455
|
+
* Every op is ONE applyEdit, so `Z` takes the whole thing back in one press:
|
|
456
|
+
* a reorder is two sections changing places, and an undo that put one of
|
|
457
|
+
* them back would be a deck in a state nobody typed.
|
|
458
|
+
*
|
|
459
|
+
* The answer carries `slide` — where the stage should BE afterwards — rather
|
|
460
|
+
* than leaving the browser to work it out: the number moves differently for
|
|
461
|
+
* every op (a new slide is the one you are now on, a delete at the end
|
|
462
|
+
* leaves you on the new end), and two implementations of that arithmetic is
|
|
463
|
+
* one too many.
|
|
464
|
+
*
|
|
465
|
+
* Numbering is by SOURCE ORDER, hidden slides counted (DECK_ANATOMY), which
|
|
466
|
+
* is how comments, review anchors and history already number them.
|
|
467
|
+
*/
|
|
468
|
+
function slideRoute({ body, json }) {
|
|
469
|
+
const { op, slide, to } = JSON.parse(body || '{}');
|
|
470
|
+
if (!SLIDE_OPS.has(op) || !Number.isInteger(slide) || slide < 1) {
|
|
471
|
+
return json(400, { ok: false, error: 'bad payload' });
|
|
472
|
+
}
|
|
473
|
+
if (op === 'move' && (!Number.isInteger(to) || to < 1)) return json(400, { ok: false, error: 'bad payload' });
|
|
474
|
+
const before = readDeck();
|
|
475
|
+
const total = sectionBodies(before).length;
|
|
476
|
+
if (slide > total) return json(404, { ok: false, error: `no slide ${slide} (deck has ${total})` });
|
|
477
|
+
// The three refusals, said as sentences: a deck with no slides is not a
|
|
478
|
+
// deck, and the ends of the list are the ends of the list.
|
|
479
|
+
if (op === 'delete' && total === 1) return json(409, { ok: false, error: 'a deck needs at least one slide' });
|
|
480
|
+
if (op === 'up' && slide === 1) return json(409, { ok: false, error: 'already the first slide' });
|
|
481
|
+
if (op === 'down' && slide === total) return json(409, { ok: false, error: 'already the last slide' });
|
|
482
|
+
if (op === 'move' && to > total) return json(404, { ok: false, error: `no position ${to} (deck has ${total})` });
|
|
483
|
+
if (op === 'move' && to === slide) return json(409, { ok: false, error: `slide ${slide} is already there` });
|
|
484
|
+
|
|
485
|
+
const after = op === 'delete' ? total - 1 : (op === 'up' || op === 'down' || op === 'move') ? total : total + 1;
|
|
486
|
+
const show = {
|
|
487
|
+
new: slide + 1,
|
|
488
|
+
duplicate: slide + 1,
|
|
489
|
+
delete: Math.min(slide, after), // deleting the last slide leaves you on the new last
|
|
490
|
+
up: slide - 1,
|
|
491
|
+
down: slide + 1,
|
|
492
|
+
move: to,
|
|
493
|
+
}[op];
|
|
494
|
+
applyEdit((html) => {
|
|
495
|
+
switch (op) {
|
|
496
|
+
case 'new': return insertBlankSlide(html, slide);
|
|
497
|
+
case 'duplicate': return duplicateSlide(html, slide);
|
|
498
|
+
case 'delete': return deleteSlide(html, slide);
|
|
499
|
+
case 'up': return swapSlides(html, slide - 1, slide);
|
|
500
|
+
case 'move': return moveSlide(html, slide, to);
|
|
501
|
+
default: return swapSlides(html, slide, slide + 1);
|
|
502
|
+
}
|
|
503
|
+
}, before);
|
|
504
|
+
console.log(` slide ${op}: ${slide}${op === 'move' ? ` → ${to}` : ''} → ${after} slide${after === 1 ? '' : 's'}`);
|
|
505
|
+
return json(200, { ok: true, slide: show, total: after, ...history.counts() });
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ── an image dropped on the stage ────────────────────────────────────────
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* What an author may drop, and the extension it is SAVED under. The
|
|
512
|
+
* extension comes from the content type and never from the name the browser
|
|
513
|
+
* sent: the name is the author's to choose, what the bytes ARE is not.
|
|
514
|
+
*/
|
|
515
|
+
const IMAGE_TYPES = new Map([
|
|
516
|
+
['image/png', 'png'], ['image/jpeg', 'jpg'], ['image/gif', 'gif'],
|
|
517
|
+
['image/webp', 'webp'], ['image/svg+xml', 'svg'], ['image/avif', 'avif'],
|
|
518
|
+
]);
|
|
519
|
+
const MAX_ASSET_BYTES = 25e6;
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* The basename an upload is saved under: lower-case, `[a-z0-9._-]`, no
|
|
523
|
+
* directory part and no extension of its own.
|
|
524
|
+
*
|
|
525
|
+
* Every character that could make this a PATH rather than a name is gone
|
|
526
|
+
* before `resolve` ever sees it — a separator, a drive letter, a `..`, a
|
|
527
|
+
* leading dot that would hide the file from the no-trust mode's dotfile rule. The
|
|
528
|
+
* containment check at the write is the second lock on the same door, not
|
|
529
|
+
* the first.
|
|
530
|
+
*/
|
|
531
|
+
function safeStem(name) {
|
|
532
|
+
const leaf = String(name ?? '').split(/[/\\]/).pop();
|
|
533
|
+
const stem = leaf.replace(/\.[a-z0-9]{1,8}$/i, '');
|
|
534
|
+
const safe = stem.toLowerCase()
|
|
535
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
536
|
+
.replace(/^[-._]+|[-._]+$/g, '')
|
|
537
|
+
.slice(0, 60);
|
|
538
|
+
return safe || 'image';
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* `POST /deck/edit/asset` — the raw bytes of an image dropped onto the stage,
|
|
543
|
+
* saved into `assets/` beside the deck. Answers the `src` the caller then
|
|
544
|
+
* hands to `POST /deck/edit/element/image`.
|
|
545
|
+
*
|
|
546
|
+
* Ahead of the shared body read (`BEFORE_BODY` in cli/edit.mjs) for the same
|
|
547
|
+
* reason `/deck/edit/narration/record` is: the body is BINARY and megabytes of it, so the
|
|
548
|
+
* string concat and its 1 MB ceiling are both wrong. The size limit is
|
|
549
|
+
* enforced on the way IN — `content-length` first when the browser declared
|
|
550
|
+
* one, then the stream itself — because a limit that buffers 2 GB before
|
|
551
|
+
* announcing it is a limit that costs more than it saves.
|
|
552
|
+
*
|
|
553
|
+
* SVG is accepted and NOTHING is stripped from it. A saved file is the
|
|
554
|
+
* author's bytes, and an editor that silently rewrote them would be lying
|
|
555
|
+
* about what is on the slide; if that SVG is ever inlined into the deck,
|
|
556
|
+
* `decklight --no-trust --audit` names its scripts like any other block, which
|
|
557
|
+
* is the mechanism that exists for this (SPEC PRESENTING, the ingredients
|
|
558
|
+
* label). What is refused is an SVG that arrives with a script already in
|
|
559
|
+
* it — that is not an author's picture, and saving it would be putting a
|
|
560
|
+
* payload one `<object>` away from the deck under the author's own hand.
|
|
561
|
+
*/
|
|
562
|
+
async function assetRoute({ req, json }) {
|
|
563
|
+
const type = String(req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase();
|
|
564
|
+
const ext = IMAGE_TYPES.get(type);
|
|
565
|
+
if (!ext) return json(415, { ok: false, error: `${type || 'that'} is not an image a slide can carry` });
|
|
566
|
+
const declared = Number(req.headers['content-length']);
|
|
567
|
+
if (Number.isFinite(declared) && declared > MAX_ASSET_BYTES) {
|
|
568
|
+
return json(413, { ok: false, error: 'image too large' });
|
|
569
|
+
}
|
|
570
|
+
const chunks = [];
|
|
571
|
+
let size = 0;
|
|
572
|
+
for await (const chunk of req) {
|
|
573
|
+
size += chunk.length;
|
|
574
|
+
if (size > MAX_ASSET_BYTES) return json(413, { ok: false, error: 'image too large' });
|
|
575
|
+
chunks.push(chunk);
|
|
576
|
+
}
|
|
577
|
+
if (!size) return json(400, { ok: false, error: 'no image in the request body' });
|
|
578
|
+
const buf = Buffer.concat(chunks);
|
|
579
|
+
// latin1, not utf8: the needle is ASCII, and decoding byte-for-byte cannot
|
|
580
|
+
// turn a stray sequence into a replacement character that hides it.
|
|
581
|
+
if (ext === 'svg' && /<script/i.test(buf.toString('latin1'))) {
|
|
582
|
+
return json(400, { ok: false, error: 'an SVG with a script is not an image' });
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const dir = resolve(deckPath, '..', 'assets');
|
|
586
|
+
const stem = safeStem(req.headers['x-decklight-name']);
|
|
587
|
+
let name = `${stem}.${ext}`;
|
|
588
|
+
try {
|
|
589
|
+
mkdirSync(dir, { recursive: true });
|
|
590
|
+
// De-duplicate by NAME, not by content: two pictures called `logo` are
|
|
591
|
+
// two pictures, and overwriting the first would change a slide the
|
|
592
|
+
// author was not editing. `wx` is what decides it — an existsSync ahead
|
|
593
|
+
// of the write is a check that can go stale between the two lines.
|
|
594
|
+
for (let i = 2; ; i++) {
|
|
595
|
+
const at = resolve(dir, name);
|
|
596
|
+
if (at !== resolve(dir, basename(at)) || !at.startsWith(dir + sep)) {
|
|
597
|
+
return json(400, { ok: false, error: 'bad image name' });
|
|
598
|
+
}
|
|
599
|
+
try { writeFileSync(at, buf, { flag: 'wx' }); break; }
|
|
600
|
+
catch (e) {
|
|
601
|
+
if (e.code !== 'EEXIST') throw e;
|
|
602
|
+
if (i > 999) return json(409, { ok: false, error: `too many images called ${stem}` });
|
|
603
|
+
name = `${stem}-${i}.${ext}`;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
} catch (e) { return json(500, { ok: false, error: oneline(e) }); }
|
|
607
|
+
console.log(` image saved: assets/${name} (${Math.round(size / 1024)} kB)`);
|
|
608
|
+
return json(200, { ok: true, src: `assets/${name}`, bytes: size });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Is `src` a path this deck can carry? Relative, and only relative.
|
|
613
|
+
*
|
|
614
|
+
* A scheme is somebody else's server (or, for `javascript:` and `data:`,
|
|
615
|
+
* somebody else's code); a leading slash is the serving root rather than the
|
|
616
|
+
* folder the deck lives in, so it breaks the moment the deck is opened over
|
|
617
|
+
* `file://` or published under a path; and `..` points outside what travels
|
|
618
|
+
* with the file. A deck is one HTML file and what sits beside it
|
|
619
|
+
* (DECK_ANATOMY), and an `<img>` the author cannot send anybody is worse
|
|
620
|
+
* than a refusal.
|
|
621
|
+
*/
|
|
622
|
+
const relativeSrc = (s) => typeof s === 'string' && !!s.trim()
|
|
623
|
+
&& !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(s)
|
|
624
|
+
&& !/^[/\\]/.test(s)
|
|
625
|
+
&& !s.split(/[/\\]/).includes('..');
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* `POST /deck/edit/element/image` — `{ slide, index, src, alt }`, an `<img>` onto a
|
|
629
|
+
* slide, addressed by the same top-level child index every element route
|
|
630
|
+
* uses (`sectionChildRanges`).
|
|
631
|
+
*
|
|
632
|
+
* `index: null` means "wherever it belongs on this slide": after the last
|
|
633
|
+
* content child and before the asides, so a picture dropped on a slide that
|
|
634
|
+
* has speaker notes lands on the slide instead of inside the notes, where
|
|
635
|
+
* the audience would never see it.
|
|
636
|
+
*
|
|
637
|
+
* The answer names the new element's index, because everything after the
|
|
638
|
+
* insertion has just shifted and the caller's next act is usually to open
|
|
639
|
+
* element edit mode on what it just made.
|
|
640
|
+
*/
|
|
641
|
+
function imageRoute({ body, json }) {
|
|
642
|
+
const { slide, index = null, src, alt } = JSON.parse(body || '{}');
|
|
643
|
+
const text = alt == null ? '' : alt;
|
|
644
|
+
if (!Number.isInteger(slide) || slide < 1 || typeof text !== 'string'
|
|
645
|
+
|| (index !== null && (!Number.isInteger(index) || index < 0))) {
|
|
646
|
+
return json(400, { ok: false, error: 'bad payload' });
|
|
647
|
+
}
|
|
648
|
+
if (!relativeSrc(src)) {
|
|
649
|
+
return json(400, { ok: false, error: 'an image src must be a relative path beside the deck' });
|
|
650
|
+
}
|
|
651
|
+
let at = null;
|
|
652
|
+
try {
|
|
653
|
+
applyEdit((html) => {
|
|
654
|
+
const out = insertImage(html, slide, index, { src, alt: text });
|
|
655
|
+
at = out.index;
|
|
656
|
+
return out.html;
|
|
657
|
+
});
|
|
658
|
+
} catch (e) { return json(404, { ok: false, error: oneline(e) }); }
|
|
659
|
+
console.log(` image added: slide ${slide} #${at} → ${src}`);
|
|
660
|
+
return json(200, { ok: true, index: at, ...history.counts() });
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
routes.set('POST /deck/edit/slide/notes', notesRoute);
|
|
664
|
+
routes.set('POST /deck/edit/slide/sources', sourcesRoute);
|
|
665
|
+
routes.set('POST /deck/edit/timings', timingsRoute);
|
|
666
|
+
routes.set('POST /deck/edit/slide/layout', layoutRoute);
|
|
667
|
+
routes.set('POST /deck/edit/slide/template', slideTemplateRoute);
|
|
668
|
+
routes.set('POST /deck/edit/slide/hidden', hiddenRoute);
|
|
669
|
+
routes.set('POST /deck/edit/narration', narrationRoute);
|
|
670
|
+
routes.set('GET /deck/edit/element/source', elementSourceRoute);
|
|
671
|
+
routes.set('POST /deck/edit/element/remove', elementRemoveRoute);
|
|
672
|
+
routes.set('POST /deck/edit/element/content', elementContentRoute);
|
|
673
|
+
routes.set('POST /deck/edit/element/effect', elementEffectRoute);
|
|
674
|
+
routes.set('POST /deck/edit/element/style', elementStyleRoute);
|
|
675
|
+
routes.set('POST /deck/edit/element/concept', elementConceptRoute);
|
|
676
|
+
routes.set('POST /deck/edit/element/concepts', elementConceptsRoute);
|
|
677
|
+
routes.set('POST /deck/edit/element/text', elementTextRoute);
|
|
678
|
+
routes.set('POST /deck/edit/concepts', conceptsRoute);
|
|
679
|
+
routes.set('POST /deck/edit/slide', slideRoute);
|
|
680
|
+
routes.set('POST /deck/edit/asset', assetRoute);
|
|
681
|
+
routes.set('POST /deck/edit/element/image', imageRoute);
|
|
682
|
+
return routes;
|
|
683
|
+
}
|