oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package deck
2
3// Mode is which version of the talk is being given: core leaves out the
4// slides marked full only, full has them.
5type Mode string
6
7const (
8 ModeCore Mode = "core"
9 ModeFull Mode = "full"
10)
11
12// Position is where a talk is, absolutely: the slide by id (so an edit
13// that moves slides about cannot shift it), the press on that slide, the
14// mode, and whether the screen is black. A Slide of "" is nowhere, which
15// is where an empty talk is.
16type Position struct {
17 Slide string `json:"slide"` // slide id
18 Step int `json:"step"`
19 Mode Mode `json:"mode"`
20 Black bool `json:"black"`
21}
22
23// Move is one press of the presenter's: next, back, first, last, goto
24// (with Slide), mode or black.
25type Move struct {
26 Op string `json:"op"`
27 Slide string `json:"slide,omitempty"`
28}
29
30// TalkOrder is the deck indices of the slides in the talk, in deck
31// order: parked slides never, full-only slides only in full mode.
32func TalkOrder(d *Deck, m Mode) []int {
33 var out []int
34 for i, s := range d.Slides {
35 if s.Talk == "parked" || (s.Talk == "full" && m != ModeFull) {
36 continue
37 }
38 out = append(out, i)
39 }
40 return out
41}
42
43// Start is where a talk begins: the first slide of the core talk.
44func Start(d *Deck) Position {
45 if order := TalkOrder(d, ModeCore); len(order) > 0 {
46 return Position{Slide: d.Slides[order[0]].ID, Mode: ModeCore}
47 }
48 return Position{Mode: ModeCore}
49}
50
51// Advance is where a move takes the talk from p. fixtures/moves.mjs is
52// the same function for the browser, and fixtures/vectors/moves.json
53// holds the two to one answer.
54//
55// next and back step through the slide's presses, then leave it for the
56// neighbouring slide of the talk (back lands fully built); at either end
57// they stay. The neighbour is found by the slide's place in the deck, so
58// a position on a slide outside the order (full only in core, or parked)
59// still moves sensibly. first, last and goto land at step 0. Each of
60// these lifts black. goto a full-only slide in core switches to full;
61// goto a parked or missing slide changes nothing at all. mode flips the
62// mode and keeps the rest; black toggles. Any other op changes nothing.
63func Advance(d *Deck, p Position, mv Move) Position {
64 order := TalkOrder(d, p.Mode)
65 switch mv.Op {
66 case "black":
67 p.Black = !p.Black
68 return p
69 case "mode":
70 if p.Mode == ModeFull {
71 p.Mode = ModeCore
72 } else {
73 p.Mode = ModeFull
74 }
75 return p
76 case "goto":
77 i := d.slideIndex(mv.Slide)
78 if mv.Slide == "" || i < 0 || d.Slides[i].Talk == "parked" {
79 return p
80 }
81 if d.Slides[i].Talk == "full" {
82 p.Mode = ModeFull
83 }
84 return Position{Slide: mv.Slide, Mode: p.Mode}
85 case "first", "last":
86 if len(order) == 0 {
87 return Position{Mode: p.Mode}
88 }
89 at := order[0]
90 if mv.Op == "last" {
91 at = order[len(order)-1]
92 }
93 return Position{Slide: d.Slides[at].ID, Mode: p.Mode}
94 case "next", "back":
95 return step(d, p, mv.Op == "next", order)
96 }
97 return p
98}
99
100// step is next (forward) or back from p.
101func step(d *Deck, p Position, forward bool, order []int) Position {
102 if len(order) == 0 {
103 return Position{Mode: p.Mode}
104 }
105 cur := d.slideIndex(p.Slide)
106 if cur < 0 {
107 p = Settle(d, p)
108 p.Black = false
109 return p
110 }
111 count := len(Presses(d.Slides[cur], d.Scenes))
112 k := min(max(p.Step, 0), count)
113 p.Black = false
114 switch {
115 case forward && k < count:
116 p.Step = k + 1
117 case !forward && k > 0:
118 p.Step = k - 1
119 case forward:
120 p.Step = k
121 for _, i := range order {
122 if i > cur {
123 return Position{Slide: d.Slides[i].ID, Mode: p.Mode}
124 }
125 }
126 default:
127 p.Step = k
128 for j := len(order) - 1; j >= 0; j-- {
129 if i := order[j]; i < cur {
130 s := d.Slides[i]
131 return Position{Slide: s.ID, Step: len(Presses(s, d.Scenes)), Mode: p.Mode}
132 }
133 }
134 }
135 return p
136}
137
138// Settle is SettleNear with no place known for a missing slide.
139func Settle(d *Deck, p Position) Position {
140 return SettleNear(d, p, -1)
141}
142
143// SettleNear puts p back on its feet after the deck changed under it.
144// near is the index p's slide had in the old deck's full talk order, or
145// -1 when that is unknown; the server passes its stored slide number
146// less one, the presenter window the index from the version it held.
147//
148// A missing slide with near known becomes the slide that followed it:
149// the entry at near in the new full order, or the first after that which
150// is in p's mode, at step 0. With near unknown, or nothing following, it
151// becomes the order's last. A parked slide becomes the next slide of the
152// order after its place in the deck at step 0, or the order's last. No
153// slide at all becomes the first. A step outside the slide's presses
154// clamps into them. A full-only slide in core mode is a place the mode
155// switch left it, and stays. An empty talk gives the mode alone.
156func SettleNear(d *Deck, p Position, near int) Position {
157 order := TalkOrder(d, p.Mode)
158 if len(order) == 0 {
159 return Position{Mode: p.Mode}
160 }
161 landing := func(at int) Position {
162 return Position{Slide: d.Slides[at].ID, Mode: p.Mode, Black: p.Black}
163 }
164 if p.Slide == "" {
165 return landing(order[0])
166 }
167 inOrder := map[int]bool{}
168 for _, i := range order {
169 inOrder[i] = true
170 }
171 cur := d.slideIndex(p.Slide)
172 switch {
173 case cur < 0 && near >= 0:
174 full := TalkOrder(d, ModeFull)
175 for _, i := range full[min(near, len(full)):] {
176 if inOrder[i] {
177 return landing(i)
178 }
179 }
180 return landing(order[len(order)-1])
181 case cur < 0:
182 return landing(order[len(order)-1])
183 case d.Slides[cur].Talk == "parked":
184 for _, i := range order {
185 if i > cur {
186 return landing(i)
187 }
188 }
189 return landing(order[len(order)-1])
190 }
191 p.Step = min(max(p.Step, 0), len(Presses(d.Slides[cur], d.Scenes)))
192 return p
193}
194
195// ForReaders is the deck as anybody holding only a share or follow link
196// may see it: a copy without the parked slides. Meta and the slides
197// themselves are copied by value; the assets and scenes maps are shared,
198// as nothing downstream writes to them. The scenes stay whole: a reader's
199// scene blocks are drawn from them, and their presses counted by them.
200func (d *Deck) ForReaders() *Deck {
201 out := &Deck{Meta: d.Meta, Assets: d.Assets, Scenes: d.Scenes, Slides: make([]Slide, 0, len(d.Slides))}
202 for _, s := range d.Slides {
203 if s.Talk != "parked" {
204 out.Slides = append(out.Slides, s)
205 }
206 }
207 return out
208}
209
210// slideIndex is the deck index of the slide with this id, or -1.
211func (d *Deck) slideIndex(id string) int {
212 for i, s := range d.Slides {
213 if s.ID == id {
214 return i
215 }
216 }
217 return -1
218}
219