Render: a slide can be drawn at a press, and nothing else changes
Options.Reveal draws a slide as it stands after n presses: boxes and each-list items that arrive on a press get data-press, and data-waiting while later. The section says data-arrive when the deck has an effect. Nil Reveal and the editor's stage write none of it. slides.css hides waiting things with visibility and defines the 300ms effect, off under reduced motion.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
7 files changed,
+414
−3
internal/slides/render/render.go+18 −3internal/slides/render/reveal.go+50 −0internal/slides/render/reveal_test.go+174 −0internal/slides/render/testdata/steps-at0-desktop.golden.html+21 −0internal/slides/render/testdata/steps-at1-desktop.golden.html+21 −0internal/slides/static/slides.css+30 −0internal/slidestest/browser_reveal_test.go+100 −0
diff --git a/internal/slides/render/render.go b/internal/slides/render/render.go| index c099c40..525b5e5 100644 |
| --- a/internal/slides/render/render.go |
| +++ b/internal/slides/render/render.go |
| @@ -117,6 +117,12 @@ type Options struct { |
| // only while the block is empty. It is the catalog's copy, handed |
| // in by the caller, because this package has no locale. |
| Placeholders map[string]string |
| + // Reveal draws the slide as it stands after that many presses: a box |
| + // or list item that arrives on a later press is marked data-waiting, |
| + // and one that arrives on any press data-press. nil is the finished |
| + // slide with none of those attributes, and so is the editor's stage |
| + // whatever this says. See reveal.go. |
| + Reveal *int |
| } |
| // DefaultAssetBase is what a caller that passed no Options gets. It is |
| @@ -159,7 +165,7 @@ func SlideWith(d *deck.Deck, index int, vp Viewport, opt Options) (template.HTML |
| if base == "" { |
| base = DefaultAssetBase |
| } |
| - c := &renderer{deck: d, canvas: layout == "canvas", phone: vp == Phone, assetBase: base, late: map[string]bool{}, editing: opt.Editing, placeholders: opt.Placeholders} |
| + c := &renderer{deck: d, canvas: layout == "canvas", phone: vp == Phone, assetBase: base, late: map[string]bool{}, editing: opt.Editing, placeholders: opt.Placeholders, reveal: newReveal(s, opt)} |
| attrs := []attr{ |
| {name: "slides-slide", bare: true}, |
| @@ -182,6 +188,8 @@ func SlideWith(d *deck.Deck, index int, vp Viewport, opt Options) (template.HTML |
| attrs = append(attrs, attr{name: "data-reflowed", value: "canvas"}) |
| } |
| + attrs = append(attrs, c.reveal.sectionAttrs(d.Meta.Arrive)...) |
| + |
| children := c.body(s, reflowed) |
| if !c.canvas { |
| // A flow slide's regions sit on a frame inside the section |
| @@ -272,6 +280,8 @@ type renderer struct { |
| // attrs can mark them (data-unreached) and the stylesheet can |
| // show them for what they are. |
| late map[string]bool |
| + // reveal is Options.Reveal worked out; nil draws the finished slide. |
| + reveal *reveal |
| } |
| // assetRef resolves an asset id — a block's props.asset, a slide |
| @@ -659,6 +669,7 @@ func (c *renderer) attrs(b deck.Block, extra ...attr) []attr { |
| {name: "data-id", value: b.ID}, |
| } |
| out = append(out, extra...) |
| + out = append(out, c.reveal.attrs(b.ID, -1)...) |
| if c.editing && c.placeholders[b.Type] != "" { |
| out = append(out, attr{name: "data-placeholder", value: c.placeholders[b.Type]}) |
| } |
| @@ -789,8 +800,12 @@ func (c *renderer) listBlock(b deck.Block) template.HTML { |
| return elInline(tag, c.attrs(b, extra...), c.style(b), editingParagraphs("li", b.Text)) |
| } |
| items := make([]template.HTML, 0, len(b.Text)) |
| - for _, p := range b.Text { |
| - items = append(items, elInline("li", nil, "", inline(p))) |
| + for i, p := range b.Text { |
| + var li []attr |
| + if b.Items == "each" { |
| + li = c.reveal.attrs(b.ID, i) |
| + } |
| + items = append(items, elInline("li", li, "", inline(p))) |
| } |
| return elBlock(tag, c.attrs(b, extra...), c.style(b), items) |
| } |
diff --git a/internal/slides/render/reveal.go b/internal/slides/render/reveal.go| new file mode 100644 |
| index 0000000..5fbb779 |
| --- /dev/null |
| +++ b/internal/slides/render/reveal.go |
| @@ -0,0 +1,50 @@ |
| +package render |
| + |
| +import ( |
| + "strconv" |
| + |
| + "amadan.net/oficina/slides/internal/slides/deck" |
| +) |
| + |
| +// reveal is a slide drawn at a press: which presses its boxes and list |
| +// items arrive on, and which press this is. A renderer without one |
| +// (Options.Reveal nil, or the editor's stage) draws the finished slide |
| +// and writes none of what is here. |
| +type reveal struct { |
| + at int |
| + arrival map[deck.Reveal]int |
| +} |
| + |
| +// newReveal is nil when the slide is not being drawn at a press. |
| +func newReveal(s deck.Slide, opt Options) *reveal { |
| + if opt.Reveal == nil || opt.Editing { |
| + return nil |
| + } |
| + return &reveal{at: *opt.Reveal, arrival: deck.ArrivalOf(s)} |
| +} |
| + |
| +// attrs is data-press for a box (item -1) or a list item that arrives |
| +// on a press, and data-waiting too while that press has not come. |
| +func (r *reveal) attrs(id string, item int) []attr { |
| + if r == nil { |
| + return nil |
| + } |
| + n, ok := r.arrival[deck.Reveal{Block: id, Item: item}] |
| + if !ok { |
| + return nil |
| + } |
| + out := []attr{{name: "data-press", value: strconv.Itoa(n)}} |
| + if n > r.at { |
| + out = append(out, attr{name: "data-waiting", bare: true}) |
| + } |
| + return out |
| +} |
| + |
| +// sectionAttrs is data-arrive: the deck's effect, said only when the |
| +// slide is drawn at a press and the deck has one. |
| +func (r *reveal) sectionAttrs(arrive string) []attr { |
| + if r == nil || (arrive != "fade" && arrive != "rise") { |
| + return nil |
| + } |
| + return []attr{{name: "data-arrive", value: arrive}} |
| +} |
diff --git a/internal/slides/render/reveal_test.go b/internal/slides/render/reveal_test.go| new file mode 100644 |
| index 0000000..3d37cb9 |
| --- /dev/null |
| +++ b/internal/slides/render/reveal_test.go |
| @@ -0,0 +1,174 @@ |
| +package render_test |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/slides/internal/slides/deck" |
| + "amadan.net/oficina/slides/internal/slides/render" |
| +) |
| + |
| +// stepsDeck is one title-body slide whose body list comes in an item |
| +// per press from press 1 (the blank paragraph takes none), a note box |
| +// that arrives at step 2, which is press 4 because the list takes |
| +// presses 1 to 3 first, and a footer that is there from the start. |
| +func stepsDeck(arrive string) *deck.Deck { |
| + return &deck.Deck{ |
| + Meta: deck.Meta{Title: "Steps", Theme: "paper", Aspect: "16:9", Arrive: arrive}, |
| + Slides: []deck.Slide{{ |
| + ID: "slide0001", Template: "title-body", Layout: "flow", |
| + Blocks: []deck.Block{ |
| + {ID: "blk0001", Type: "heading", Region: "title", Text: txt("Steps")}, |
| + {ID: "blk0002", Type: "list", Region: "body", Items: "each", Step: 1, |
| + Text: paras("one", " ", "two", "three")}, |
| + {ID: "blk0003", Type: "text", Region: "body", Step: 2, Text: txt("note")}, |
| + {ID: "blk0004", Type: "text", Region: "body", Text: txt("footer")}, |
| + }, |
| + }}, |
| + } |
| +} |
| + |
| +func renderAt(t *testing.T, d *deck.Deck, press *int, editing bool) string { |
| + t.Helper() |
| + got, err := render.SlideWith(d, 0, render.Desktop, render.Options{Reveal: press, Editing: editing}) |
| + if err != nil { |
| + t.Fatalf("SlideWith: %v", err) |
| + } |
| + return string(got) |
| +} |
| + |
| +// markOf is the opening tag of the element with the given data-id. |
| +func markOf(out, id string) string { |
| + i := strings.Index(out, `data-id="`+id+`"`) |
| + if i < 0 { |
| + return "" |
| + } |
| + start := strings.LastIndex(out[:i], "<") |
| + end := strings.Index(out[i:], ">") |
| + return out[start : i+end] |
| +} |
| + |
| +// TestRevealNilChangesNothing: with no Reveal, every existing golden |
| +// still matches, and a slide that has steps carries none of the new |
| +// attributes. |
| +func TestRevealNilChangesNothing(t *testing.T) { |
| + d := fixture() |
| + for _, g := range goldenNames { |
| + for _, v := range viewports { |
| + name := g.name + "-" + v.name |
| + got, err := render.SlideWith(d, g.index, v.vp, render.Options{Reveal: nil}) |
| + if err != nil { |
| + t.Fatalf("%s: %v", name, err) |
| + } |
| + want, err := os.ReadFile(filepath.Join("testdata", name+".golden.html")) |
| + if err != nil { |
| + t.Fatalf("%s: %v", name, err) |
| + } |
| + if string(got)+"\n" != string(want) { |
| + t.Errorf("%s: a nil Reveal changed the markup", name) |
| + } |
| + } |
| + } |
| + for _, a := range []string{"", "fade", "rise"} { |
| + out := renderAt(t, stepsDeck(a), nil, false) |
| + for _, bad := range []string{"data-press", "data-waiting", "data-arrive", "data-arriving"} { |
| + if strings.Contains(out, bad) { |
| + t.Errorf("arrive %q, nil Reveal: markup has %s", a, bad) |
| + } |
| + } |
| + } |
| +} |
| + |
| +func TestRevealMarksWaitingBoxes(t *testing.T) { |
| + for _, tc := range []struct { |
| + press int |
| + waiting bool |
| + }{{0, true}, {3, true}, {4, false}} { |
| + p := tc.press |
| + out := renderAt(t, stepsDeck(""), &p, false) |
| + note := markOf(out, "blk0003") |
| + if !strings.Contains(note, `data-press="4"`) { |
| + t.Errorf("press %d: note box is %s, want data-press=\"4\"", tc.press, note) |
| + } |
| + if got := strings.Contains(note, "data-waiting"); got != tc.waiting { |
| + t.Errorf("press %d: note waiting = %v, want %v (%s)", tc.press, got, tc.waiting, note) |
| + } |
| + if strings.Contains(out, "data-arrive=") { |
| + t.Errorf("press %d: a deck that appears has data-arrive", tc.press) |
| + } |
| + // The footer arrives with the slide, and the each list's own |
| + // element is not a press (its items are). |
| + for _, id := range []string{"blk0001", "blk0002", "blk0004"} { |
| + if m := markOf(out, id); strings.Contains(m, "data-press") || strings.Contains(m, "data-waiting") { |
| + t.Errorf("press %d: %s is marked as arriving: %s", tc.press, id, m) |
| + } |
| + } |
| + } |
| + for _, a := range []string{"fade", "rise"} { |
| + p := 0 |
| + if out := renderAt(t, stepsDeck(a), &p, false); !strings.Contains(markOf(out, "slide0001"), `data-arrive="`+a+`"`) { |
| + t.Errorf("arrive %s: section lacks data-arrive in %s", a, out) |
| + } |
| + } |
| + // The editor's stage always shows the finished slide. |
| + p := 0 |
| + out := renderAt(t, stepsDeck("fade"), &p, true) |
| + for _, bad := range []string{"data-press", "data-waiting", "data-arrive"} { |
| + if strings.Contains(out, bad) { |
| + t.Errorf("editing render has %s", bad) |
| + } |
| + } |
| +} |
| + |
| +func TestRevealEachListMarksItems(t *testing.T) { |
| + for _, tc := range []struct { |
| + press int |
| + want []string // the start of each <li>, per paragraph |
| + }{ |
| + {0, []string{`<li data-press="1" data-waiting>`, `<li>`, `<li data-press="2" data-waiting>`, `<li data-press="3" data-waiting>`}}, |
| + {2, []string{`<li data-press="1">`, `<li>`, `<li data-press="2">`, `<li data-press="3" data-waiting>`}}, |
| + {3, []string{`<li data-press="1">`, `<li>`, `<li data-press="2">`, `<li data-press="3">`}}, |
| + } { |
| + p := tc.press |
| + out := renderAt(t, stepsDeck(""), &p, false) |
| + at := 0 |
| + for i, w := range tc.want { |
| + j := strings.Index(out[at:], "<li") |
| + if j < 0 { |
| + t.Fatalf("press %d: item %d missing in %s", tc.press, i, out) |
| + } |
| + rest := out[at+j:] |
| + if !strings.HasPrefix(rest, w) { |
| + t.Errorf("press %d: item %d is %.40q, want prefix %q", tc.press, i, rest, w) |
| + } |
| + at += j + 3 |
| + } |
| + } |
| +} |
| + |
| +// TestRevealGoldens: the steps slide at press 0 and at press 1. |
| +func TestRevealGoldens(t *testing.T) { |
| + for _, g := range []struct { |
| + name string |
| + press int |
| + }{{"steps-at0", 0}, {"steps-at1", 1}} { |
| + p := g.press |
| + body := renderAt(t, stepsDeck("fade"), &p, false) + "\n" |
| + path := filepath.Join("testdata", g.name+"-desktop.golden.html") |
| + if *update { |
| + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + continue |
| + } |
| + want, err := os.ReadFile(path) |
| + if err != nil { |
| + t.Fatalf("read golden (run with -update to create it): %v", err) |
| + } |
| + if body != string(want) { |
| + t.Errorf("%s does not match the golden.\n got: %s\nwant: %s", g.name, body, want) |
| + } |
| + } |
| +} |
diff --git a/internal/slides/render/testdata/steps-at0-desktop.golden.html b/internal/slides/render/testdata/steps-at0-desktop.golden.html| new file mode 100644 |
| index 0000000..79261d8 |
| --- /dev/null |
| +++ b/internal/slides/render/testdata/steps-at0-desktop.golden.html |
| @@ -0,0 +1,21 @@ |
| +<section slides-slide data-index="0" data-id="slide0001" data-layout="flow" data-template="title-body" data-theme="paper" data-arrive="fade" style="--slides-bg:#faf7f0;--slides-fg:#1c1a17;--slides-accent:#8a5410;--slides-accent-2:#46617a;--slides-font-heading:Liberation Serif, Georgia, serif;--slides-font-body:Liberation Sans, system-ui, -apple-system, Segoe UI, Roboto, sans-serif"> |
| +<div slides-frame> |
| +<div slides-region data-region="body"> |
| +<ul slides-block data-block="list" data-id="blk0002" data-style="bullet" data-size="m" data-region="body"> |
| +<li data-press="1" data-waiting>one</li> |
| +<li> </li> |
| +<li data-press="2" data-waiting>two</li> |
| +<li data-press="3" data-waiting>three</li> |
| +</ul> |
| +<div slides-block data-block="text" data-id="blk0003" data-size="m" data-press="4" data-waiting data-region="body"> |
| +<p>note</p> |
| +</div> |
| +<div slides-block data-block="text" data-id="blk0004" data-size="m" data-region="body"> |
| +<p>footer</p> |
| +</div> |
| +</div> |
| +<div slides-region data-region="title"> |
| +<h1 slides-block data-block="heading" data-id="blk0001" data-level="1" data-region="title">Steps</h1> |
| +</div> |
| +</div> |
| +</section> |
diff --git a/internal/slides/render/testdata/steps-at1-desktop.golden.html b/internal/slides/render/testdata/steps-at1-desktop.golden.html| new file mode 100644 |
| index 0000000..34b723b |
| --- /dev/null |
| +++ b/internal/slides/render/testdata/steps-at1-desktop.golden.html |
| @@ -0,0 +1,21 @@ |
| +<section slides-slide data-index="0" data-id="slide0001" data-layout="flow" data-template="title-body" data-theme="paper" data-arrive="fade" style="--slides-bg:#faf7f0;--slides-fg:#1c1a17;--slides-accent:#8a5410;--slides-accent-2:#46617a;--slides-font-heading:Liberation Serif, Georgia, serif;--slides-font-body:Liberation Sans, system-ui, -apple-system, Segoe UI, Roboto, sans-serif"> |
| +<div slides-frame> |
| +<div slides-region data-region="body"> |
| +<ul slides-block data-block="list" data-id="blk0002" data-style="bullet" data-size="m" data-region="body"> |
| +<li data-press="1">one</li> |
| +<li> </li> |
| +<li data-press="2" data-waiting>two</li> |
| +<li data-press="3" data-waiting>three</li> |
| +</ul> |
| +<div slides-block data-block="text" data-id="blk0003" data-size="m" data-press="4" data-waiting data-region="body"> |
| +<p>note</p> |
| +</div> |
| +<div slides-block data-block="text" data-id="blk0004" data-size="m" data-region="body"> |
| +<p>footer</p> |
| +</div> |
| +</div> |
| +<div slides-region data-region="title"> |
| +<h1 slides-block data-block="heading" data-id="blk0001" data-level="1" data-region="title">Steps</h1> |
| +</div> |
| +</div> |
| +</section> |
diff --git a/internal/slides/static/slides.css b/internal/slides/static/slides.css| index 86a5bbb..58ddff1 100644 |
| --- a/internal/slides/static/slides.css |
| +++ b/internal/slides/static/slides.css |
| @@ -694,3 +694,33 @@ |
| overflow: visible; |
| } |
| } |
| + |
| +/* A slide drawn at a press (render.Options.Reveal). What has not yet |
| + arrived is still laid out, so nothing moves when it does, but it is |
| + neither seen, read out, nor reachable with Tab: visibility does all |
| + three. */ |
| +[data-waiting] { |
| + visibility: hidden; |
| +} |
| + |
| +/* The arriving effect. A script sets data-arriving on what a press has |
| + just shown (the server never does) and takes it off when the effect |
| + is done; the deck's setting is on the section as data-arrive. With |
| + no setting, or with reduced motion asked for, a thing just appears. */ |
| +@keyframes slides-arrive-fade { |
| + from { opacity: 0; } |
| +} |
| + |
| +@keyframes slides-arrive-rise { |
| + from { opacity: 0; transform: translateY(0.6em); } |
| +} |
| + |
| +@media (prefers-reduced-motion: no-preference) { |
| + [data-arrive="fade"] [data-arriving] { |
| + animation: slides-arrive-fade 300ms ease-out; |
| + } |
| + |
| + [data-arrive="rise"] [data-arriving] { |
| + animation: slides-arrive-rise 300ms ease-out; |
| + } |
| +} |
diff --git a/internal/slidestest/browser_reveal_test.go b/internal/slidestest/browser_reveal_test.go| new file mode 100644 |
| index 0000000..a14533e |
| --- /dev/null |
| +++ b/internal/slidestest/browser_reveal_test.go |
| @@ -0,0 +1,100 @@ |
| +//go:build browser |
| + |
| +// A slide drawn at a press, held in a real Chromium: what only layout, |
| +// computed style and the accessibility tree can show. The page is the |
| +// test's own: render's output and slides.css, no app route. |
| +package slidestest |
| + |
| +import ( |
| + "encoding/json" |
| + "fmt" |
| + "testing" |
| + |
| + "amadan.net/oficina/slides/internal/slides/deck" |
| + "amadan.net/oficina/slides/internal/slides/render" |
| + "github.com/chromedp/chromedp" |
| +) |
| + |
| +func TestWaitingBoxIsHiddenAndKeepsItsSpace(t *testing.T) { |
| + rig, _ := newRig(t) |
| + link := func(word, href string) deck.Text { return deck.Text{{{T: word, Href: href}}} } |
| + d := &deck.Deck{ |
| + Meta: deck.Meta{Title: "Steps", Theme: "paper", Aspect: "16:9", Arrive: "fade"}, |
| + Slides: []deck.Slide{{ |
| + ID: "slide0001", Template: "title-body", Layout: "flow", |
| + Blocks: []deck.Block{ |
| + {ID: "blk0001", Type: "heading", Region: "title", Text: deck.Text{{{T: "Steps"}}}}, |
| + {ID: "blk0002", Type: "text", Region: "body", Step: 1, Text: link("later", "https://example.com/later")}, |
| + {ID: "blk0003", Type: "text", Region: "body", Text: link("now", "https://example.com/now")}, |
| + }, |
| + }}, |
| + } |
| + at := func(press int, id string) string { |
| + out, err := render.SlideWith(d, 0, render.Desktop, render.Options{Reveal: &press}) |
| + if err != nil { |
| + t.Fatalf("render at press %d: %v", press, err) |
| + } |
| + return fmt.Sprintf(`<div id=%q style="width:1280px;position:absolute;left:0;top:%s">%s</div>`, id, map[string]string{"p0": "0", "p1": "1200px"}[id], out) |
| + } |
| + markup, _ := json.Marshal(at(0, "p0") + at(1, "p1")) |
| + |
| + var raw string |
| + rig.Run( |
| + chromedp.EmulateViewport(1280, 900), |
| + chromedp.Navigate(rig.Origin+"/signin"), |
| + chromedp.Evaluate(`(() => { |
| + const link = document.createElement('link'); |
| + link.rel = 'stylesheet'; |
| + link.href = '/static/slides.css'; |
| + document.head.appendChild(link); |
| + document.body.insertAdjacentHTML('beforeend', `+string(markup)+`); |
| + return new Promise(res => link.addEventListener('load', () => res('ok'))); |
| + })()`, nil, awaitPromise), |
| + chromedp.Evaluate(`(() => { |
| + const boxes = id => [...document.querySelectorAll('#'+id+' [data-id]')].map(e => { |
| + const r = e.getBoundingClientRect(), p = e.closest('[slides-slide]').getBoundingClientRect(); |
| + return [e.dataset.id, r.x-p.x, r.y-p.y, r.width, r.height]; |
| + }); |
| + const later = document.querySelector('#p0 [data-id=blk0002]'); |
| + const shown = document.querySelector('#p1 [data-id=blk0002]'); |
| + later.querySelector('a').focus(); |
| + const focusedLater = document.activeElement === later.querySelector('a'); |
| + shown.querySelector('a').focus(); |
| + return JSON.stringify({p0: boxes('p0'), p1: boxes('p1'), |
| + laterVis: getComputedStyle(later).visibility, shownVis: getComputedStyle(shown).visibility, |
| + focusedLater}); |
| + })()`, &raw), |
| + ) |
| + var got struct { |
| + P0, P1 [][]any |
| + LaterVis, ShownVis string |
| + FocusedLater bool |
| + } |
| + if err := json.Unmarshal([]byte(raw), &got); err != nil { |
| + t.Fatalf("read the page: %v\n%s", err, raw) |
| + } |
| + if len(got.P0) != 4 { |
| + t.Fatalf("press 0 has %d boxes, want 4 (the slide and its blocks): %v", len(got.P0), got.P0) |
| + } |
| + if fmt.Sprint(got.P0) != fmt.Sprint(got.P1) { |
| + t.Errorf("the boxes moved between press 0 and press 1:\n p0 %v\n p1 %v", got.P0, got.P1) |
| + } |
| + if got.LaterVis != "hidden" { |
| + t.Errorf("a waiting box has visibility %q, want hidden", got.LaterVis) |
| + } |
| + if got.ShownVis != "visible" { |
| + t.Errorf("an arrived box has visibility %q, want visible", got.ShownVis) |
| + } |
| + if got.FocusedLater { |
| + t.Error("a link in a waiting box took focus") |
| + } |
| + if roles := axNamed(t, rig, "#p0 [data-id=blk0002]", "later"); len(roles) != 0 { |
| + t.Errorf("a waiting box is in the accessibility tree: %v", roles) |
| + } |
| + if roles := axNamed(t, rig, "#p0 [data-id=blk0003]", "now"); len(roles) == 0 { |
| + t.Error("the control, a visible link, is not in the accessibility tree: the check proves nothing") |
| + } |
| + if roles := axNamed(t, rig, "#p1 [data-id=blk0002]", "later"); len(roles) == 0 { |
| + t.Error("a box that has arrived is not in the accessibility tree") |
| + } |
| +} |