oficina / slides Public

Deck: settle a deleted slide near its old place; columns wait

SettleNear(d, p, near) and settleNear(view, pos, near) land a position
whose slide was deleted on the slide that followed it, from its index
in the old full talk order; Settle is SettleNear with near -1. A
columns box that would arrive as an empty frame now waits for the
earliest of its children that shows. New vectors on both sides: the
deletion cases and three columns cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 4178ef89a5f53887630db835d325dde66f21478c parent f78e415
8 files changed, +144 −44
  • fixtures/moves.mjs +24 −12
  • fixtures/moves.test.mjs +7 −5
  • fixtures/presses.mjs +17 −2
  • fixtures/vectors/moves.json +14 −2
  • fixtures/vectors/presses.json +4 −1
  • internal/slides/deck/presses.go +28 −2
  • internal/slides/deck/talk.go +42 −18
  • internal/slides/deck/talk_test.go +8 −2
diff --git a/fixtures/moves.mjs b/fixtures/moves.mjs
index cba23ff..c8bc85e 100644
--- a/fixtures/moves.mjs
+++ b/fixtures/moves.mjs
@@ -81,24 +81,36 @@ function step (view, pos, forward, order) {
return { ...pos, step: k, black: false }
}
+/** settleNear with no place known for a missing slide. */
+export function settle (view, pos) {
+ return settleNear(view, pos, -1)
+}
+
/**
- * pos put back on its feet after the deck changed under it. A missing or
- * parked slide becomes the next slide of the order after its place in
- * the deck at step 0, or the order's last (a missing slide has no place,
- * so it is always the last). No slide at all becomes the first. A step
- * outside the slide's presses clamps into them. An empty talk gives the
- * mode alone.
+ * pos put back on its feet after the deck changed under it. near is the
+ * index pos's slide had in the old deck's full talk order, or -1 when
+ * that is unknown. A missing slide with near known becomes the slide
+ * that followed it: the entry at near in the new full order, or the
+ * first after that which is in pos's mode, at step 0. With near
+ * unknown, or nothing following, it becomes the order's last. A parked
+ * slide becomes the next slide of the order after its place in the deck
+ * at step 0, or the order's last. No slide at all becomes the first. A
+ * step outside the slide's presses clamps into them. An empty talk gives
+ * the mode alone.
*/
-export function settle (view, pos) {
+export function settleNear (view, pos, near) {
const slides = slidesOf(view)
const order = talkOrder(view, pos.mode)
if (!order.length) return nowhere(pos.mode)
- const keep = slide => ({ ...at(slide, pos.mode), black: pos.black })
- if (pos.slide === '') return keep(slides[order[0]])
+ const landing = i => ({ ...at(slides[i], pos.mode), black: pos.black })
+ const last = order[order.length - 1]
+ if (pos.slide === '') return landing(order[0])
const cur = indexOf(slides, pos.slide)
- if (cur < 0 || slides[cur].talk === 'parked') {
- const next = cur < 0 ? undefined : order.find(i => i > cur)
- return keep(slides[next ?? order[order.length - 1]])
+ if (cur < 0 && near >= 0) {
+ const next = talkOrder(view, 'full').slice(near).find(i => order.includes(i))
+ return landing(next ?? last)
}
+ if (cur < 0) return landing(last)
+ if (slides[cur].talk === 'parked') return landing(order.find(i => i > cur) ?? last)
return { ...pos, step: clamp(pos.step, count(slides[cur])) }
}
diff --git a/fixtures/moves.test.mjs b/fixtures/moves.test.mjs
index 3072713..a7a6601 100644
--- a/fixtures/moves.test.mjs
+++ b/fixtures/moves.test.mjs
@@ -1,11 +1,12 @@
// moves.test.mjs — the browser's moves against the server's, through the
// shared vectors (vectors/moves.json, which
// internal/slides/deck/talk_test.go reads too). Two ops there are not
-// moves: "start" is start(view) and "settle" is settle(view, from).
+// moves: "start" is start(view) and "settle" is settle(view, from), or
+// settleNear(view, from, near) when the case has a near.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
-import { talkOrder, start, advance, settle } from './moves.mjs'
+import { talkOrder, start, advance, settle, settleNear } from './moves.mjs'
const vectors = JSON.parse(readFileSync(new URL('./vectors/moves.json', import.meta.url)))
@@ -13,9 +14,10 @@ test('moves vectors', async t => {
assert.ok(vectors.length >= 20)
for (const c of vectors) {
await t.test(c.name, () => {
- const got = c.move.op === 'start'
- ? start(c.deck)
- : c.move.op === 'settle' ? settle(c.deck, c.from) : advance(c.deck, c.from, c.move)
+ let got
+ if (c.move.op === 'start') got = start(c.deck)
+ else if (c.move.op !== 'settle') got = advance(c.deck, c.from, c.move)
+ else got = 'near' in c ? settleNear(c.deck, c.from, c.near) : settle(c.deck, c.from)
assert.deepEqual(got, c.to)
})
}
diff --git a/fixtures/presses.mjs b/fixtures/presses.mjs
index a4022b4..355cff0 100644
--- a/fixtures/presses.mjs
+++ b/fixtures/presses.mjs
@@ -16,7 +16,8 @@
// on; everything else at v comes with the first of them, lists tied at
// v pair up by index, and the next value starts after the longest.
// - A box that shows nothing is in no press, so a value with only such
-// boxes makes none.
+// boxes makes none. A columns box shows nothing until one of its
+// children does, so it arrives with the earliest of them.
// Go's unicode.IsSpace, spelled out: JS's \s differs at U+0085 and U+FEFF.
const BLANK = /^[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]*$/
@@ -83,6 +84,20 @@ function shows (sl, b, on) {
return true
}
+/** The value a box first shows at: a columns box waits for its earliest child that shows. */
+function arrival (sl, b, on) {
+ const v = value(sl, b)
+ if (b.type !== 'columns') return v
+ on.add(b.id)
+ let first = Infinity
+ for (const id of children(b)) {
+ const c = sl.byID.get(id)
+ if (!c || on.has(id) || !shows(sl, c, new Set())) continue
+ first = Math.min(first, arrival(sl, c, on))
+ }
+ return first > v && first !== Infinity ? first : v
+}
+
export function presses (slide) {
const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : []
const sl = slideBoxes(blocks)
@@ -90,7 +105,7 @@ export function presses (slide) {
const values = []
for (const b of blocks) {
if (ownStep(b) === 0 || !shows(sl, b, new Set())) continue
- const a = { id: b.id, value: value(sl, b), items: null }
+ const a = { id: b.id, value: arrival(sl, b, new Set()), items: null }
if (b.type === 'list' && b.items === 'each') a.items = filledItems(b.text)
all.push(a)
if (!values.includes(a.value)) values.push(a.value)
diff --git a/fixtures/vectors/moves.json b/fixtures/vectors/moves.json
index 7134251..961d2de 100644
--- a/fixtures/vectors/moves.json
+++ b/fixtures/vectors/moves.json
@@ -56,8 +56,20 @@
{"name":"settle: a step below 0 clamps to 0","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"s2","step":-1,"mode":"core","black":false},"move":{"op":"settle"},"to":{"slide":"s2","step":0,"mode":"core","black":false}},
{"name":"settle: a parked slide becomes the next slide of the order after it","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"p4","step":1,"mode":"full","black":true},"move":{"op":"settle"},"to":{"slide":"s5","step":0,"mode":"full","black":true}},
{"name":"settle: a parked slide with nothing after it becomes the last","deck":{"slides":[{"id":"f1","talk":"full","blocks":[]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]}]},{"id":"p3","talk":"parked","blocks":[]}]},"from":{"slide":"p3","step":0,"mode":"full","black":false},"move":{"op":"settle"},"to":{"slide":"s2","step":0,"mode":"full","black":false}},
- {"name":"settle: a missing slide becomes the last","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"gone","step":2,"mode":"core","black":false},"move":{"op":"settle"},"to":{"slide":"s5","step":0,"mode":"core","black":false}},
+ {"name":"settle: a missing slide with no place known becomes the last","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"gone","step":2,"mode":"core","black":false},"move":{"op":"settle"},"to":{"slide":"s5","step":0,"mode":"core","black":false}},
{"name":"settle: a full only slide in core stays, its step clamped","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"f3","step":4,"mode":"core","black":false},"move":{"op":"settle"},"to":{"slide":"f3","step":1,"mode":"core","black":false}},
{"name":"settle: no slide yet becomes the first","deck":{"slides":[{"id":"s1","blocks":[{"id":"h1","type":"heading","text":[[{"t":"Hello"}]]}]},{"id":"s2","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]},{"id":"b","type":"text","step":2,"text":[[{"t":"b"}]]}]},{"id":"f3","talk":"full","blocks":[{"id":"c","type":"text","step":1,"text":[[{"t":"c"}]]}]},{"id":"p4","talk":"parked","blocks":[{"id":"d","type":"text","step":1,"text":[[{"t":"d"}]]}]},{"id":"s5","blocks":[]}]},"from":{"slide":"","step":0,"mode":"full","black":false},"move":{"op":"settle"},"to":{"slide":"s1","step":0,"mode":"full","black":false}},
- {"name":"settle: an empty talk gives only the mode","deck":{"slides":[{"id":"p1","talk":"parked","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]}]}]},"from":{"slide":"p1","step":1,"mode":"full","black":true},"move":{"op":"settle"},"to":{"slide":"","step":0,"mode":"full","black":false}}
+ {"name":"settle: an empty talk gives only the mode","deck":{"slides":[{"id":"p1","talk":"parked","blocks":[{"id":"a","type":"text","step":1,"text":[[{"t":"a"}]]}]}]},"from":{"slide":"p1","step":1,"mode":"full","black":true},"move":{"op":"settle"},"to":{"slide":"","step":0,"mode":"full","black":false}},
+ {"name":"settle near: deleting B from A-B-C-D lands on C","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]},{"id":"D","blocks":[{"id":"d1","type":"text","text":[[{"t":"d1"}]],"step":1}]}]},"from":{"slide":"B","step":1,"mode":"core","black":true},"move":{"op":"settle"},"near":1,"to":{"slide":"C","step":0,"mode":"core","black":true}},
+ {"name":"settle near: deleting the last slide lands on the new last","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"B","blocks":[{"id":"b1","type":"text","text":[[{"t":"b1"}]],"step":1}]},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"D","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":3,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: deleting the only full-only slide while in core","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"F","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":1,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: a full-only follower is skipped in core","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"F","blocks":[{"id":"f1","type":"text","text":[[{"t":"f1"}]],"step":1}],"talk":"full"},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"B","step":0,"mode":"core","black":false},"move":{"op":"settle"},"near":1,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: a full-only follower is kept in full","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"F","blocks":[{"id":"f1","type":"text","text":[[{"t":"f1"}]],"step":1}],"talk":"full"},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"B","step":0,"mode":"full","black":false},"move":{"op":"settle"},"near":1,"to":{"slide":"F","step":0,"mode":"full","black":false}},
+ {"name":"settle near: nothing in the mode after it gives the last","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"F","blocks":[{"id":"f1","type":"text","text":[[{"t":"f1"}]],"step":1}],"talk":"full"}]},"from":{"slide":"B","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":1,"to":{"slide":"A","step":0,"mode":"core","black":false}},
+ {"name":"settle near: deleting all but one","deck":{"slides":[{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"A","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":0,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: deleting all but one, from the middle","deck":{"slides":[{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"B","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":1,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: -1 is no place known, the last","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]},{"id":"D","blocks":[{"id":"d1","type":"text","text":[[{"t":"d1"}]],"step":1}]}]},"from":{"slide":"B","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":-1,"to":{"slide":"D","step":0,"mode":"core","black":false}},
+ {"name":"settle near: a parked slide goes by its own place, not near","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"P","blocks":[{"id":"p1","type":"text","text":[[{"t":"p1"}]],"step":1}],"talk":"parked"},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]},{"id":"D","blocks":[{"id":"d1","type":"text","text":[[{"t":"d1"}]],"step":1}]}]},"from":{"slide":"P","step":1,"mode":"core","black":false},"move":{"op":"settle"},"near":0,"to":{"slide":"C","step":0,"mode":"core","black":false}},
+ {"name":"settle near: a slide still there clamps its step as settle does","deck":{"slides":[{"id":"A","blocks":[{"id":"a1","type":"text","text":[[{"t":"a1"}]],"step":1}]},{"id":"C","blocks":[{"id":"c1","type":"text","text":[[{"t":"c1"}]],"step":1}]}]},"from":{"slide":"C","step":5,"mode":"core","black":false},"move":{"op":"settle"},"near":0,"to":{"slide":"C","step":1,"mode":"core","black":false}},
+ {"name":"settle near: an empty talk gives only the mode","deck":{"slides":[{"id":"P","blocks":[{"id":"p1","type":"text","text":[[{"t":"p1"}]],"step":1}],"talk":"parked"}]},"from":{"slide":"B","step":1,"mode":"full","black":true},"move":{"op":"settle"},"near":0,"to":{"slide":"","step":0,"mode":"full","black":false}}
]
diff --git a/fixtures/vectors/presses.json b/fixtures/vectors/presses.json
index 040bad8..fb972e5 100644
--- a/fixtures/vectors/presses.json
+++ b/fixtures/vectors/presses.json
@@ -196,5 +196,8 @@
"presses": [
[{"block": "a", "item": -1}, {"block": "b", "item": -1}]
]
- }
+ },
+ {"name": "columns whose children all come later arrive with the first of them", "slide": {"id": "s", "blocks": [{"id": "c", "type": "columns", "step": 1, "props": {"children": ["y", "z"]}}, {"id": "y", "type": "text", "text": [[{"t": "y"}]], "step": 2}, {"id": "z", "type": "text", "text": [[{"t": "z"}]], "step": 3}, {"id": "r", "type": "text", "text": [[{"t": "r"}]], "step": 1}]}, "presses": [[{"block": "r", "item": -1}], [{"block": "c", "item": -1}, {"block": "y", "item": -1}], [{"block": "z", "item": -1}]]},
+ {"name": "a blank child does not hold a columns box at its own value", "slide": {"id": "s", "blocks": [{"id": "c", "type": "columns", "step": 1, "props": {"children": ["b", "y"]}}, {"id": "b", "type": "text", "text": [[{"t": " "}]]}, {"id": "y", "type": "text", "text": [[{"t": "y"}]], "step": 2}]}, "presses": [[{"block": "c", "item": -1}, {"block": "y", "item": -1}]]},
+ {"name": "an outer columns box waits through an inner one for its first child", "slide": {"id": "s", "blocks": [{"id": "o", "type": "columns", "step": 1, "props": {"children": ["i"]}}, {"id": "i", "type": "columns", "props": {"children": ["w"]}}, {"id": "w", "type": "text", "text": [[{"t": "w"}]], "step": 3}]}, "presses": [[{"block": "o", "item": -1}, {"block": "w", "item": -1}]]}
]
diff --git a/internal/slides/deck/presses.go b/internal/slides/deck/presses.go
index a52503f..2c5f988 100644
--- a/internal/slides/deck/presses.go
+++ b/internal/slides/deck/presses.go
@@ -1,6 +1,7 @@
package deck
import (
+ "math"
"sort"
"strings"
)
@@ -31,7 +32,8 @@ type Press []Reveal
// v on; everything else at v comes with the first of them, lists tied
// at v pair up by index, and the next value starts after the longest.
// - A box that shows nothing is in no press, so a value with only such
-// boxes makes none.
+// boxes makes none. A columns box shows nothing until one of its
+// children does, so it arrives with the earliest of them.
func Presses(s Slide) []Press {
sl := newSlideBoxes(s)
type arriving struct {
@@ -47,7 +49,7 @@ func Presses(s Slide) []Press {
if ownStep(b) == 0 || !sl.shows(b, map[string]bool{}) {
continue
}
- a := arriving{b: b, value: sl.value(b)}
+ a := arriving{b: b, value: sl.arrival(b, map[string]bool{})}
if b.Type == "list" && b.Items == "each" {
a.items = filledItems(b.Text)
}
@@ -139,6 +141,30 @@ func (sl slideBoxes) value(b *Block) float64 {
return v
}
+// arrival is the value a box first shows at: its value, except that a
+// columns box waits for the earliest of its children that show, since
+// until then it is an empty frame. on is the columns boxes already being
+// looked into, so a cycle ends.
+func (sl slideBoxes) arrival(b *Block, on map[string]bool) float64 {
+ v := sl.value(b)
+ if b.Type != "columns" {
+ return v
+ }
+ on[b.ID] = true
+ first := math.Inf(1)
+ for _, id := range children(*b) {
+ c, ok := sl.byID[id]
+ if !ok || on[id] || !sl.shows(c, map[string]bool{}) {
+ continue
+ }
+ first = min(first, sl.arrival(c, on))
+ }
+ if first > v && !math.IsInf(first, 1) {
+ return first
+ }
+ return v
+}
+
// shows is whether a box puts anything on the slide: a list with a
// non-blank item, a heading, text or quote with non-blank text, a
// columns box with a child that shows. Every other type does. on is the
diff --git a/internal/slides/deck/talk.go b/internal/slides/deck/talk.go
index 7e13240..89d6092 100644
--- a/internal/slides/deck/talk.go
+++ b/internal/slides/deck/talk.go
@@ -135,34 +135,58 @@ func step(d *Deck, p Position, forward bool, order []int) Position {
return p
}
-// Settle puts p back on its feet after the deck changed under it. A
-// slide that is missing or parked becomes the next slide of the order
-// after its place in the deck at step 0, or the order's last when
-// nothing follows (a missing slide has no place, so it is always the
-// last). No slide at all becomes the first. A step outside the slide's
-// presses clamps into them. A full-only slide in core mode is a place
-// the mode switch left it, and stays. An empty talk gives the mode
-// alone.
+// Settle is SettleNear with no place known for a missing slide.
func Settle(d *Deck, p Position) Position {
+ return SettleNear(d, p, -1)
+}
+
+// SettleNear puts p back on its feet after the deck changed under it.
+// near is the index p's slide had in the old deck's full talk order, or
+// -1 when that is unknown; the server passes its stored slide number
+// less one, the presenter window the index from the version it held.
+//
+// A missing slide with near known becomes the slide that followed it:
+// the entry at near in the new full order, or the first after that which
+// is in p's mode, at step 0. With near unknown, or nothing following, it
+// becomes the order's last. A parked slide becomes the next slide of the
+// order after its place in the deck at step 0, or the order's last. No
+// slide at all becomes the first. A step outside the slide's presses
+// clamps into them. A full-only slide in core mode is a place the mode
+// switch left it, and stays. An empty talk gives the mode alone.
+func SettleNear(d *Deck, p Position, near int) Position {
order := TalkOrder(d, p.Mode)
if len(order) == 0 {
return Position{Mode: p.Mode}
}
+ landing := func(at int) Position {
+ return Position{Slide: d.Slides[at].ID, Mode: p.Mode, Black: p.Black}
+ }
if p.Slide == "" {
- return Position{Slide: d.Slides[order[0]].ID, Mode: p.Mode, Black: p.Black}
+ return landing(order[0])
+ }
+ inOrder := map[int]bool{}
+ for _, i := range order {
+ inOrder[i] = true
}
cur := d.slideIndex(p.Slide)
- if cur < 0 || d.Slides[cur].Talk == "parked" {
- at := order[len(order)-1]
- if cur >= 0 {
- for _, i := range order {
- if i > cur {
- at = i
- break
- }
+ switch {
+ case cur < 0 && near >= 0:
+ full := TalkOrder(d, ModeFull)
+ for _, i := range full[min(near, len(full)):] {
+ if inOrder[i] {
+ return landing(i)
}
}
- return Position{Slide: d.Slides[at].ID, Mode: p.Mode, Black: p.Black}
+ return landing(order[len(order)-1])
+ case cur < 0:
+ return landing(order[len(order)-1])
+ case d.Slides[cur].Talk == "parked":
+ for _, i := range order {
+ if i > cur {
+ return landing(i)
+ }
+ }
+ return landing(order[len(order)-1])
}
p.Step = min(max(p.Step, 0), len(Presses(d.Slides[cur])))
return p
diff --git a/internal/slides/deck/talk_test.go b/internal/slides/deck/talk_test.go
index a79c0ae..cfc9265 100644
--- a/internal/slides/deck/talk_test.go
+++ b/internal/slides/deck/talk_test.go
@@ -9,13 +9,15 @@ import (
// TestMovesVectors runs fixtures/vectors/moves.json, which
// fixtures/moves.test.mjs runs too. Two ops there are not moves: "start"
-// is Start(deck) and "settle" is Settle(deck, from).
+// is Start(deck) and "settle" is Settle(deck, from), or
+// SettleNear(deck, from, near) when the case has a near.
func TestMovesVectors(t *testing.T) {
var cases []struct {
Name string `json:"name"`
Deck deck.Deck `json:"deck"`
From deck.Position `json:"from"`
Move deck.Move `json:"move"`
+ Near *int `json:"near"`
To deck.Position `json:"to"`
}
readVectors(t, "moves.json", &cases)
@@ -28,7 +30,11 @@ func TestMovesVectors(t *testing.T) {
case "start":
got = deck.Start(&c.Deck)
case "settle":
- got = deck.Settle(&c.Deck, c.From)
+ if c.Near != nil {
+ got = deck.SettleNear(&c.Deck, c.From, *c.Near)
+ } else {
+ got = deck.Settle(&c.Deck, c.From)
+ }
default:
got = deck.Advance(&c.Deck, c.From, c.Move)
}