oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// viewer.go is the paged viewer and the notes page: the screens that
2// show a deck to somebody who is reading it rather than writing it.
3//
4// One surface, three audiences (spec §5). The owner, a team member and
5// a stranger holding a share link all get the same page from the same
6// template — mode is a field on the view data, never a second template
7// and never a second handler, because the day the three drift is the
8// day one of them leaks the speaker notes.
9//
10// It is the scriptless floor (§8) as a whole screen: one slide per
11// page, previous and next as ordinary links, a filmstrip of ordinary
12// links, notes in a <details>. Nothing here needs JavaScript to work,
13// and task 6's live presenter is an addition to it rather than a
14// replacement for it.
15package slides
16
17import (
18 "fmt"
19 "html/template"
20 "net/http"
21 "strconv"
22 "strings"
23
24 "amadan.net/oficina/estilo.git/chrome"
25 estilomenu "amadan.net/oficina/estilo.git/menu"
26 "amadan.net/rastrillo/rastrillo"
27
28 "amadan.net/oficina/slides/internal/slides/deck"
29 // Aliased: this package already has a render — render.go's, which
30 // executes a page template — and the two would collide on the
31 // name. slidehtml says which of the two a call means at the call
32 // site, which is the more useful thing for a reader anyway.
33 slidehtml "amadan.net/oficina/slides/internal/slides/render"
34)
35
36// The three audiences a viewer is rendered for. Owner and member are
37// both signed in and both see the notes; link is a share-link reader,
38// who sees the slides and nothing else about the deck.
39const (
40 modeOwner = "owner"
41 modeMember = "member"
42 modeLink = "link"
43)
44
45// vpPhone is the ?vp= value that asks for the phone rendering. Anything
46// else is the desktop one — an unknown viewport is not an error, it is
47// simply not the phone.
48const vpPhone = "phone"
49
50// filmItem is one slide in the filmstrip: where it is, what it is
51// called, and whether it is the one being shown.
52type filmItem struct {
53 Href string
54 Label string
55 Current bool
56 // Thumb is the server's picture of the slide (thumbs.go), from this
57 // surface's own capability, so a share link's filmstrip is fetched
58 // over the share link.
59 Thumb string
60}
61
62// viewerPage is the whole viewer screen. Mode is what the template
63// branches on; every href is built in Go so the query string a link
64// carries (the viewport, the presenting flag) is decided in one place.
65type viewerPage struct {
66 shell
67 Mode string
68 Present bool
69 // VP is "desktop" or "phone" — the wrapper's data-vp, so the page
70 // itself says which shape it was rendered for and not only the
71 // section inside it.
72 VP string
73
74 Title string
75 Slide template.HTML
76 // Scenes is what the slide's scene blocks run (scene_pages.go), nil
77 // when it shows none.
78 Scenes map[string]pageScene
79
80 // HasSlides is false for a deck with nothing in it. It is a real
81 // state rather than a 404: a deck you own that renders as "not
82 // found" reads as data loss.
83 HasSlides bool
84 EmptyTitle string
85 EmptyBody string
86
87 Counter string
88 // Announce is what the page's live region says when the viewer has
89 // changed to this slide in place (fixtures/viewer-nav.mjs): "Slide 2
90 // of 5: Revenue". On the swapped root, read by the script.
91 Announce string
92 PagerLabel string
93 PrevHref string
94 PrevLabel string
95 NextHref string
96 NextLabel string
97 FilmLabel string
98 Film []filmItem
99
100 HasNotes bool
101 NotesLabel string
102 Notes template.HTML
103
104 ToolsLabel string
105 // EditHref opens the editor (editor.go). Owner and member alike:
106 // the editor is deckFor's audience, the same people the live-sync
107 // endpoints behind it let write.
108 EditHref string
109 EditLabel string
110 // The overflow (MoreLabel): the deck's other reading screens, kept
111 // out of the row so the row is what a reader reaches for — Edit and
112 // Present. Share… is in the bar (Docs' place for it), and the deck's
113 // settings are the editor's inspector and the share page's.
114 MoreLabel string
115 NotesHref string
116 NotesPageLabel string
117 // ExportHref opens the export screen (exports.go). Owner and
118 // member alike, like the editor and presenting: anybody who can
119 // read the deck can take a copy of it away.
120 ExportHref string
121 ExportLabel string
122 // PresentAction is the form that opens a presentation session
123 // (presentation.go). A form and not a link, because pressing it
124 // WRITES: it mints a session and a remote capability.
125 PresentAction string
126 PresentHref string
127 PresentLabel string
128
129 // FollowHref is offered on a share link while somebody is
130 // presenting this deck — spec §3.4's "follow the presenter". Empty
131 // the rest of the time, which is nearly always.
132 FollowHref string
133 FollowLabel string
134}
135
136// --- GET /d/{sqid} ---
137
138// showDeck serves the signed-in viewer, or opens the editor.
139//
140// A deck's own address opens it to be worked on, as a document's does
141// in Docs (Paul, 2026-11-11: he opened a deck and landed on a read-only
142// page): everybody deckFor lets in may edit (editor.go), so the bare
143// address is a 303 to /edit. The viewer is the same address with a
144// query — ?slide=N, which is what every link inside it already is — so
145// it stays reachable for somebody with scripts off (the editor's
146// <noscript> links it), for the File menu's "Open in the viewer", and
147// for the in-place paging that fetches its pages. The redirect comes
148// after deckFor, so a stranger still gets the same 404 either way.
149//
150// Which of the two signed-in modes it renders is the ownership
151// question and nothing else: a member reached this deck through
152// visibleDecks, so they read the notes, and only the owner is offered
153// the screens that change it.
154func (a *app) showDeck(w http.ResponseWriter, r *http.Request) {
155 u, ok := a.viewer(w, r)
156 if !ok {
157 return
158 }
159 d, ok := a.deckFor(w, r, u)
160 if !ok {
161 return
162 }
163 if r.URL.RawQuery == "" {
164 http.Redirect(w, r, editorHref(d.Sqid), http.StatusSeeOther)
165 return
166 }
167 mode := modeMember
168 f := frame{parts: []string{d.Title}, context: d.Title}
169 if a.mayShareDeck(r.Context(), u, d) {
170 mode = modeOwner
171 // Share… in the bar's actions, where Docs puts it on a
172 // document (Docs' handlers.go, docs.share.action).
173 f.actions = []chrome.Action{{Item: estilomenu.Item{Href: "/d/" + d.Sqid + "/share", Label: rastrillo.T(r, "slides.share.action")}}}
174 }
175 a.renderViewer(w, r, d, mode, "/d/"+d.Sqid, a.shellFor(r, u, f))
176}
177
178// editorHref is a deck's editor: where a deck opens.
179func editorHref(sqid string) string { return "/d/" + sqid + "/edit" }
180
181// viewerStart is the viewer's first slide: the deck's address with the
182// query that keeps it the viewer (showDeck).
183func viewerStart(sqid string) string { return viewerHref("/d/"+sqid, 1, "desktop", false) }
184
185// renderViewer is the one viewer, for all three audiences. base is the
186// URL every link on the page hangs off — "/d/{sqid}" signed in,
187// "/s/{token}" over a share link — so the page never has to know which
188// of the two it is serving to build a next link.
189func (a *app) renderViewer(w http.ResponseWriter, r *http.Request, d Deck, mode, base string, sh shell) {
190 h, view, err := a.openDeck(r.Context(), d)
191 if err != nil {
192 a.fail(w, r, "open deck", err)
193 return
194 }
195 defer h.Close(r.Context())
196 view = view.ForReaders()
197
198 q := r.URL.Query()
199 vp := q.Get("vp")
200 if vp != vpPhone {
201 vp = "desktop"
202 }
203 present := q.Get("present") == "1"
204
205 // The font gate for the whole deck, not this one slide: a family a
206 // later slide uses is waited for here, so paging on never meets a
207 // face that has not loaded (fontgate.go).
208 sh.Fonts = newFontGate(r, view, base)
209 page := viewerPage{
210 shell: sh,
211 Mode: mode,
212 Present: present,
213 VP: vp,
214 Title: view.Meta.Title,
215 }
216
217 // The buttons, before anything about which slide is being shown:
218 // they are about the deck, not about the slide, so a deck whose
219 // document has come back empty still has a way into the editor.
220 // modeLink's present link is the exception and waits for the index,
221 // because it is a link to a slide.
222 page.ToolsLabel = rastrillo.T(r, "slides.viewer.tools")
223 switch mode {
224 case modeOwner, modeMember:
225 page.EditHref = base + "/edit"
226 page.EditLabel = rastrillo.T(r, "slides.viewer.edit")
227 page.MoreLabel = rastrillo.T(r, "slides.viewer.more")
228 page.NotesHref = base + "/notes"
229 page.NotesPageLabel = rastrillo.T(r, "slides.viewer.notes.page")
230 page.ExportHref = base + "/export"
231 page.ExportLabel = rastrillo.T(r, "slides.viewer.export")
232 // A member presents too: reading a deck to a room is reading
233 // it (presentation.go's startPresenting).
234 page.PresentAction = base + "/present"
235 page.PresentLabel = rastrillo.T(r, "slides.viewer.present")
236 case modeLink:
237 // Spec §3.4: a share link opened while somebody is presenting
238 // offers to follow along. The lookup is here and only here,
239 // because this is the one mode that can be reached by an
240 // audience — a signed-in reader has the deck's own screens.
241 if _, live := a.liveSessionFor(r, d.ID); live {
242 page.FollowHref = base + "?follow=1"
243 page.FollowLabel = rastrillo.T(r, "slides.follow.offer")
244 }
245 }
246
247 if len(view.Slides) == 0 {
248 page.EmptyTitle = rastrillo.T(r, "slides.viewer.empty.title")
249 // Somebody who can edit is told how to fill it; a stranger
250 // holding a share link only that it is empty (the audit found
251 // this line telling people to read docs/api.md).
252 page.EmptyBody = rastrillo.T(r, "slides.viewer.empty.body.link")
253 if page.EditHref != "" {
254 page.EmptyBody = rastrillo.T(r, "slides.viewer.empty.body")
255 }
256 render(w, viewerTemplate(mode), page)
257 return
258 }
259
260 index, ok := slideIndex(q.Get("slide"), len(view.Slides))
261 if !ok {
262 // Off the end of the deck is a page that does not exist, not a
263 // clamp to the last slide: a link to slide 40 of an 8-slide
264 // deck is wrong, and quietly showing slide 8 hides that.
265 a.notFound(w, r)
266 return
267 }
268
269 // The images go under this page's own base — "/d/{sqid}/assets"
270 // signed in, "/s/{token}/assets" over a share link — because the
271 // reader looking at the page is the reader whose request has to
272 // fetch them (assets.go).
273 html, err := slidehtml.SlideWith(view, index, viewport(vp),
274 slidehtml.Options{AssetBase: assetBaseFor(base), QRAlt: qrAlt(r), QR: a.followCodes(r.Context(), d, view, base)})
275 if err != nil {
276 a.fail(w, r, "render slide", err)
277 return
278 }
279 s := view.Slides[index]
280
281 page.HasSlides = true
282 page.Slide = html
283 page.Scenes = scenesShown(view, base, index)
284 page.Counter = rastrillo.Tf(r, "slides.viewer.counter",
285 "n", index+1, "total", len(view.Slides))
286 page.Announce = rastrillo.Tf(r, "slides.slide.announce",
287 "n", index+1, "total", len(view.Slides), "title", slideLabel(r, view.Slides[index], index+1))
288 page.PagerLabel = rastrillo.T(r, "slides.viewer.pager")
289 page.PrevLabel = rastrillo.T(r, "slides.viewer.prev")
290 page.NextLabel = rastrillo.T(r, "slides.viewer.next")
291 if index > 0 {
292 page.PrevHref = viewerHref(base, index, vp, present)
293 }
294 if index+1 < len(view.Slides) {
295 page.NextHref = viewerHref(base, index+2, vp, present)
296 }
297
298 // The presenting surface is the slide and the two links, and
299 // nothing else: no filmstrip, no notes, no buttons. Task 6 replaces
300 // it with the real fullscreen surface; what it must not do is
301 // arrive before then as a page that leaks the notes.
302 if present {
303 render(w, viewerTemplate(mode), page)
304 return
305 }
306
307 page.FilmLabel = rastrillo.T(r, "slides.viewer.film")
308 page.Film = make([]filmItem, 0, len(view.Slides))
309 for i, sl := range view.Slides {
310 page.Film = append(page.Film, filmItem{
311 Href: viewerHref(base, i+1, vp, false),
312 Label: slideLabel(r, sl, i+1),
313 Current: i == index,
314 Thumb: fmt.Sprintf("%s/slides/%s.svg?seq=%d", base, sl.ID, d.Seq),
315 })
316 }
317
318 // The notes are the deck's own working notes, so they belong to
319 // the people who reached the deck rather than to whoever holds a
320 // link to it (spec §5: a share link is read-only, and read-only
321 // means the slides).
322 if mode != modeLink && len(s.Notes) > 0 {
323 page.HasNotes = true
324 page.NotesLabel = rastrillo.T(r, "slides.viewer.notes")
325 page.Notes = slidehtml.Notes(s.Notes)
326 }
327
328 // Spec §5: the present link is this same capability with
329 // ?present=1, so a link reader gets a working one rather than the
330 // placeholder the signed-in screens carry until task 6.
331 if mode == modeLink {
332 page.PresentHref = viewerHref(base, index+1, vp, true)
333 page.PresentLabel = rastrillo.T(r, "slides.viewer.present")
334 }
335
336 render(w, viewerTemplate(mode), page)
337}
338
339// viewerTemplate picks which build of viewer.html to execute. Two
340// builds, one file (render.go): the signed-in one carries the account
341// menu, and the share-link one carries a sign-in link instead, because
342// nobody reading over a link has an account to show.
343func viewerTemplate(mode string) string {
344 if mode == modeLink {
345 return "viewer_link"
346 }
347 return "viewer"
348}
349
350// viewport maps the ?vp= value onto the renderer's own two.
351func viewport(vp string) slidehtml.Viewport {
352 if vp == vpPhone {
353 return slidehtml.Phone
354 }
355 return slidehtml.Desktop
356}
357
358// slideIndex turns the ?slide= parameter into a 0-based index. Empty is
359// the first slide; anything else must be a whole number naming a slide
360// this deck actually has.
361func slideIndex(raw string, count int) (int, bool) {
362 if raw == "" {
363 return 0, count > 0
364 }
365 n, err := strconv.Atoi(raw)
366 if err != nil || n < 1 || n > count {
367 return 0, false
368 }
369 return n - 1, true
370}
371
372// viewerHref builds a link to slide n (1-based), carrying whatever the
373// current request was rendered with. The viewport travels because
374// paging out of a phone preview back into the desktop one is a trap;
375// the presenting flag travels for the same reason.
376//
377// Built by hand rather than with url.Values so the order is fixed —
378// slide, then vp, then present — which is what makes these URLs stable
379// enough to assert on and to read in a log.
380func viewerHref(base string, n int, vp string, present bool) string {
381 var b strings.Builder
382 b.WriteString(base)
383 b.WriteString("?slide=")
384 b.WriteString(strconv.Itoa(n))
385 if vp == vpPhone {
386 b.WriteString("&vp=phone")
387 }
388 if present {
389 b.WriteString("&present=1")
390 }
391 return b.String()
392}
393
394// slideLabel is what the filmstrip and the notes page call a slide: its
395// own first heading, or its number when it has none. The heading is the
396// better label by a distance — "Regions, not coordinates" is findable
397// in a list of eight and "Slide 3" is not — but a slide need not have
398// one, so the number is always there to fall back to.
399func slideLabel(r *http.Request, s deck.Slide, n int) string {
400 for _, b := range s.Blocks {
401 if b.Type != "heading" {
402 continue
403 }
404 if text := strings.TrimSpace(b.Text.Plain()); text != "" {
405 return text
406 }
407 }
408 return rastrillo.Tf(r, "slides.deck.slide", "n", n)
409}
410
411// --- GET /d/{sqid}/notes ---
412
413type notesRow struct {
414 Label string
415 Href string
416 Notes template.HTML
417 Empty string
418}
419
420type notesPage struct {
421 shell
422 Title string
423 DeckTitle string
424 Body string
425 BackHref string
426 BackLabel string
427 Slides []notesRow
428}
429
430// deckNotes serves GET /d/{sqid}/notes: every slide's label and its
431// notes, in order, on one page.
432//
433// It is spec §8's "read the presenter notes page" — the thing a person
434// prints, or keeps open on a second screen, when the live presenter
435// view is not available to them. Signed in only, and behind the same
436// visibility seam as the deck itself: notes are not part of what a
437// share link carries.
438func (a *app) deckNotes(w http.ResponseWriter, r *http.Request) {
439 u, ok := a.viewer(w, r)
440 if !ok {
441 return
442 }
443 d, ok := a.deckFor(w, r, u)
444 if !ok {
445 return
446 }
447
448 h, view, err := a.openDeck(r.Context(), d)
449 if err != nil {
450 a.fail(w, r, "open deck", err)
451 return
452 }
453 defer h.Close(r.Context())
454 view = view.ForReaders()
455
456 base := "/d/" + d.Sqid
457 page := notesPage{
458 shell: a.shellFor(r, u, onDeck(rastrillo.T(r, "slides.notes.title"), view.Meta.Title)),
459 Title: rastrillo.T(r, "slides.notes.title"),
460 DeckTitle: view.Meta.Title,
461 Body: rastrillo.T(r, "slides.notes.body"),
462 BackHref: base,
463 BackLabel: rastrillo.T(r, "slides.deck.back"),
464 Slides: make([]notesRow, 0, len(view.Slides)),
465 }
466 empty := rastrillo.T(r, "slides.notes.none")
467 for i, s := range view.Slides {
468 row := notesRow{
469 Label: slideLabel(r, s, i+1),
470 Href: viewerHref(base, i+1, "desktop", false),
471 }
472 if len(s.Notes) > 0 {
473 row.Notes = slidehtml.Notes(s.Notes)
474 } else {
475 // A slide with no notes says so rather than rendering as a
476 // heading over nothing: on a page being read while
477 // presenting, a gap is indistinguishable from a bug.
478 row.Empty = empty
479 }
480 page.Slides = append(page.Slides, row)
481 }
482 render(w, "deck_notes", page)
483}
484