oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// sample.go builds the deck a person meets on their first sign-in.
2//
3// It is documentation that runs. Every flow template, both layout
4// modes, the phone layer, and the block types worth knowing about are
5// exercised once each, and each slide's speaker notes say what it is
6// showing — so the first answer to "what can this thing do" is a deck
7// rather than a page of prose nobody reads.
8//
9// The ops are Go values, not a JSON blob in a string constant, for the
10// same reason: this file is meant to be read.
11package slides
12
13import (
14 "context"
15 "fmt"
16
17 "gorm.io/gorm"
18)
19
20// sampleDeckTitle is the deck's name. Not a catalog key: it is the
21// title of a document, which is content, and translating it would mean
22// translating the eight slides inside it too. When a locale wants its
23// own sample deck, this whole builder is what gets a second version.
24const sampleDeckTitle = "Welcome to Slides"
25
26// sampleTheme is the theme the sample wears — the default, so the deck
27// shows what an untouched deck looks like.
28const sampleTheme = defaultTheme
29
30// seedSampleDeck builds sampleDeckTitle for u. Called once, from
31// ensureUser's caller, the moment a person's users row is created.
32//
33// One row, then one op batch: the whole deck is a single DeckUpdate, so
34// the log a later request replays is one entry rather than forty.
35func (a *app) seedSampleDeck(ctx context.Context, u User) error {
36 // Making a deck rests on standing in team mode, the sample one too:
37 // u carries the Access it was admitted on (team_home_only.go).
38 g := a.memberGuard(ctx, u)
39 d, err := a.createDeckRow(ctx, u, sampleDeckTitle, sampleTheme, g)
40 if err != nil {
41 return err
42 }
43 ops, err := sampleOps()
44 if err != nil {
45 return err
46 }
47 encoded, err := opsJSON(ops)
48 if err != nil {
49 return err
50 }
51 return a.editDeck(ctx, &d, actorFor(u), encoded, nil, g)
52}
53
54// slideIDs mints the ids one slide's ops need: the slide's own and n
55// blocks'. Every id is minted here because the core draws no entropy of
56// its own (core/src/ops.rs).
57func slideIDs(n int) (string, []string, error) {
58 slide, err := newID()
59 if err != nil {
60 return "", nil, err
61 }
62 blocks := make([]string, n)
63 for i := range blocks {
64 if blocks[i], err = newID(); err != nil {
65 return "", nil, err
66 }
67 }
68 return slide, blocks, nil
69}
70
71// sampleOps is the whole deck as one batch.
72//
73// Eight slides. Six are flow slides, one per template that has anything
74// to show — title, title-body, two-column, section, image-full,
75// quote — and the last two are canvas slides, which carry the seventh
76// template (`blank`) because a canvas slide ignores its template
77// anyway: that is the honest place for `blank` to appear, rather than a
78// seventh flow slide with nothing on it.
79func sampleOps() ([]op, error) {
80 ops := []op{{"op": "set_meta", "title": sampleDeckTitle, "theme": sampleTheme}}
81 for _, build := range []func() ([]op, error){
82 sampleTitleSlide,
83 sampleTitleBodySlide,
84 sampleTwoColumnSlide,
85 sampleSectionSlide,
86 sampleImageFullSlide,
87 sampleQuoteSlide,
88 sampleCanvasSlide,
89 samplePhoneReflowSlide,
90 } {
91 part, err := build()
92 if err != nil {
93 return nil, err
94 }
95 ops = append(ops, part...)
96 }
97 return ops, nil
98}
99
100// addSlide is the op that starts a flow slide.
101func addSlide(id, template string) op {
102 return op{"op": "add_slide", "id": id, "layout": "flow", "template": template}
103}
104
105// addCanvas is the op that starts a canvas slide. Its template is
106// `blank` and it is ignored — see sampleOps.
107func addCanvas(id string) op {
108 return op{"op": "add_slide", "id": id, "layout": "canvas", "template": "blank"}
109}
110
111// addBlock places a block in a flow slide's region.
112func addBlock(slide, id, kind, region, markdown string, props map[string]any) op {
113 o := op{"op": "add_block", "slide": slide, "id": id, "type": kind, "region": region}
114 if props != nil {
115 o["props"] = props
116 }
117 if markdown != "" {
118 o["markdown"] = markdown
119 }
120 return o
121}
122
123// addBoxed places a block on a canvas slide: a box in fractions of the
124// 16:9 frame, and a z. Region is meaningless here and is not sent.
125func addBoxed(slide, id, kind string, x, y, w, h float64, z int, markdown string, props map[string]any) op {
126 o := op{
127 "op": "add_block", "slide": slide, "id": id, "type": kind,
128 "box": map[string]any{"x": x, "y": y, "w": w, "h": h},
129 "z": z,
130 }
131 if props != nil {
132 o["props"] = props
133 }
134 if markdown != "" {
135 o["markdown"] = markdown
136 }
137 return o
138}
139
140// notes attaches speaker notes. Every slide in this deck has them:
141// they are what makes the sample teach rather than merely exist.
142func notes(slide, markdown string) op {
143 return op{"op": "set_notes", "id": slide, "markdown": markdown}
144}
145
146// 1. title — the opening slide: a heading and one line under it.
147func sampleTitleSlide() ([]op, error) {
148 s, b, err := slideIDs(2)
149 if err != nil {
150 return nil, err
151 }
152 return []op{
153 addSlide(s, "title"),
154 addBlock(s, b[0], "heading", "heading", sampleDeckTitle, map[string]any{"level": 1}),
155 addBlock(s, b[1], "text", "body", "Eight slides that show what a deck is made of. Open the notes on each one.", map[string]any{"size": "l"}),
156 notes(s, "The **title** template: one heading and one supporting line, centred. It is what a new deck opens on."),
157 }, nil
158}
159
160// 2. title-body — the workhorse: a heading, prose with every inline
161// mark, and a list.
162func sampleTitleBodySlide() ([]op, error) {
163 s, b, err := slideIDs(3)
164 if err != nil {
165 return nil, err
166 }
167 return []op{
168 addSlide(s, "title-body"),
169 addBlock(s, b[0], "heading", "heading", "Text and marks", map[string]any{"level": 1}),
170 addBlock(s, b[1], "text", "body",
171 "Text carries **bold**, *italic*, `code`, ==highlight== and [links](https://amadan.net). Nothing else — a heading is a block, not a mark.",
172 map[string]any{"size": "m"}),
173 addBlock(s, b[2], "list", "body", "- One item per paragraph\n- Bullets or numbers\n- Marks work here too, like **this**",
174 map[string]any{"style": "bullet", "size": "m"}),
175 notes(s, "The **title-body** template. Text arrives as a small Markdown subset: bold, italic, code, highlight and links. A list block is one paragraph per item."),
176 }, nil
177}
178
179// 3. two-column — the heading, body and aside regions together.
180func sampleTwoColumnSlide() ([]op, error) {
181 s, b, err := slideIDs(3)
182 if err != nil {
183 return nil, err
184 }
185 return []op{
186 addSlide(s, "two-column"),
187 addBlock(s, b[0], "heading", "heading", "Regions, not coordinates", map[string]any{"level": 1}),
188 addBlock(s, b[1], "text", "body", "A flow slide names regions — heading, body, aside, media — and blocks stack inside the one they are in.", nil),
189 addBlock(s, b[2], "text", "aside", "The aside is the second column. On a phone the grid collapses to one column, so reflow is CSS rather than a second layout.", nil),
190 notes(s, "The **two-column** template puts `body` and `aside` side by side. Nothing here is positioned by hand; the template decides where a region sits."),
191 }, nil
192}
193
194// 4. section — the divider slide, one heading and nothing else.
195func sampleSectionSlide() ([]op, error) {
196 s, b, err := slideIDs(1)
197 if err != nil {
198 return nil, err
199 }
200 return []op{
201 addSlide(s, "section"),
202 addBlock(s, b[0], "heading", "heading", "Part two", map[string]any{"level": 1}),
203 notes(s, "The **section** template: a chapter break. One heading, large, and deliberately nothing else on the slide."),
204 }, nil
205}
206
207// 5. image-full — the media region, standing in for a picture.
208//
209// There is no image here, because there is no way to upload one yet:
210// assets arrive browser-to-bucket in a later slice (spec §2.7). A
211// `shape` rectangle fills the media region instead, and the slide's own
212// notes say so rather than leaving a reader to wonder why the sample
213// deck's picture slide has no picture.
214func sampleImageFullSlide() ([]op, error) {
215 s, b, err := slideIDs(2)
216 if err != nil {
217 return nil, err
218 }
219 return []op{
220 addSlide(s, "image-full"),
221 addBlock(s, b[0], "heading", "heading", "A picture would go here", map[string]any{"level": 2}),
222 addBlock(s, b[1], "shape", "media", "Image placeholder",
223 map[string]any{"kind": "rect", "fill": "#d8d4cc", "stroke": "#9a948a", "strokeWidth": 2, "radius": 8}),
224 notes(s, "The **image-full** template gives the `media` region the whole frame. This deck has no image in it: uploads land in a later slice, so a `shape` rectangle is standing in for one."),
225 }, nil
226}
227
228// 6. quote — a pull quote with its attribution.
229func sampleQuoteSlide() ([]op, error) {
230 s, b, err := slideIDs(1)
231 if err != nil {
232 return nil, err
233 }
234 return []op{
235 addSlide(s, "quote"),
236 addBlock(s, b[0], "quote", "body", "A deck is a document, not a drawing. Everything here is text you can search, export and read aloud.",
237 map[string]any{"cite": "The design spec"}),
238 notes(s, "The **quote** template. A `quote` block carries its attribution in `cite` rather than as a second block of text."),
239 }, nil
240}
241
242// 7. canvas — free placement: a heading, two shapes and a text block,
243// each with its own box and z.
244func sampleCanvasSlide() ([]op, error) {
245 s, b, err := slideIDs(4)
246 if err != nil {
247 return nil, err
248 }
249 return []op{
250 addCanvas(s),
251 addBoxed(s, b[0], "heading", 0.06, 0.06, 0.88, 0.28, 3, "Canvas: put it where you want it", map[string]any{"level": 1}),
252 addBoxed(s, b[1], "shape", 0.08, 0.34, 0.36, 0.44, 1, "",
253 map[string]any{"kind": "rect", "fill": "#e8e2d6", "stroke": "#8a7f6a", "strokeWidth": 2, "radius": 12}),
254 addBoxed(s, b[2], "shape", 0.26, 0.46, 0.30, 0.30, 2, "",
255 map[string]any{"kind": "ellipse", "fill": "#cfd9e8", "stroke": "#5a6f8a", "strokeWidth": 2}),
256 addBoxed(s, b[3], "text", 0.58, 0.38, 0.36, 0.54, 4,
257 "Every box is a fraction of the 16:9 frame, so a canvas slide scales to any screen without reflowing. `z` decides what sits on top — the ellipse overlaps the rectangle because its z is higher.", map[string]any{"size": "s"}),
258 notes(s, "A **canvas** slide. Blocks carry `box` (x, y, w, h as fractions of the frame) and `z` instead of a region. The template is ignored here."),
259 }, nil
260}
261
262// 8. canvas with phone: reflow — the same freedom, plus the phone
263// layer. The slide reflows to a single column on a phone, and one block
264// is hidden there outright.
265func samplePhoneReflowSlide() ([]op, error) {
266 s, b, err := slideIDs(3)
267 if err != nil {
268 return nil, err
269 }
270 return []op{
271 addCanvas(s),
272 op{"op": "set_slide", "id": s, "phone": "reflow", "transition": "fade"},
273 addBoxed(s, b[0], "heading", 0.06, 0.06, 0.88, 0.28, 1, "On a phone this one reflows", map[string]any{"level": 1}),
274 addBoxed(s, b[1], "text", 0.06, 0.38, 0.52, 0.54, 2,
275 "A canvas slide normally scales to fit a phone, text and all. Setting the slide's `phone` to `reflow` makes the renderer lay its blocks out as a single column instead.", nil),
276 addBoxed(s, b[2], "shape", 0.64, 0.38, 0.30, 0.44, 3, "Desktop only",
277 map[string]any{"kind": "rect", "fill": "#e0d7e8", "stroke": "#75608a", "strokeWidth": 2, "radius": 12}),
278 op{"op": "set_phone", "slide": s, "id": b[2], "phone": map[string]any{"hidden": true}},
279 notes(s, "The same canvas slide with `phone: reflow`, and a phone override that hides the shape on a small screen. The phone layer only ever adds to the desktop layout; it never replaces it."),
280 }, nil
281}
282
283// seedSampleDeckFor is what currentUser calls on a first sign-in. A
284// failure is logged and swallowed on purpose: a sample deck is a
285// welcome, and refusing to sign somebody in because their welcome could
286// not be built would be the wrong trade by a distance.
287func (a *app) seedSampleDeckFor(ctx context.Context, u User) {
288 if err := a.seedSampleDeck(ctx, u); err != nil {
289 a.logger.Error("slides: seed the sample deck", "user", u.ID, "err", err)
290 }
291}
292
293// hasDeck reports whether u owns any deck at all — the belt to
294// ensureUser's braces. ensureUser only reports "created" for the
295// request that actually inserted the row, so this should always be
296// false when seeding runs; it costs one indexed count and it is what
297// stops a race, a restore or a future second caller from handing
298// somebody two welcome decks.
299func hasDeck(ctx context.Context, g *gorm.DB, u User) (bool, error) {
300 var n int64
301 if err := g.WithContext(ctx).Model(&Deck{}).Where("owner_id = ?", u.ID).Count(&n).Error; err != nil {
302 return false, fmt.Errorf("slides: count decks for user %d: %w", u.ID, err)
303 }
304 return n > 0, nil
305}
306