oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/commands.mjs — the editor's structural commands, one place
2// for the surfaces that offer them: the Slide and Insert menus in the
3// bar (and their shortcuts), the menu on a box (box-menu.mjs, and its
4// keys), the inspector card's buttons (the delete question's, a box's
5// phone reset), Deck settings' accent reset, the bar's title, and
6// the strip's item menu (Skip in the short version and Park among
7// them). Each is a batch of ops through commit() — the same vocabulary
8// the API takes — and nothing here touches the stage: the entry's
9// observers re-fetch what the ops changed once they are saved.
10//
11// ctx is read live (its fields are functions) because a resync
12// replaces the document, the stage and the undo manager under a
13// running editor.
14import { slideIDs, blockIDs } from './stage.mjs'
15import { newID } from './ids.mjs'
16import { layoutOps, layoutAfter } from './slide-layouts.mjs'
17import { moveSlideOp, moveBlockOp, zOps, deckOp, addBlockOp, talkOp } from './inspector-ops.mjs'
18import { resetPhone } from './switcher.mjs'
19import { view } from './ops.mjs'
20import { insertProps } from './scene-card.mjs'
21
22/**
23 * ctx: {ydoc(), commit(ops) (a promise of whether the batch was applied:
24 * it may wait for a font first, editor-entry.js), flush(), slideId(), selected(), slideJSON(id),
25 * blockJSON(slideId, id), undo(), redo(), present(), select(id), clearSelection(),
26 * showSlideOptions() (the card back on the slide's options, where the
27 * delete question is), showFormat() (the card shown, focus on the
28 * selected box's first option), openPlace() (the card shown, its
29 * Position and size group open, focus on its first field), confirmEl} —
30 * returns {run(name), strip(name, slideId), setTitle(title), addBlock(kind), …}.
31 */
32export function createCommands (ctx) {
33 // A new slide right after `after` (the current one by default), made
34 // from `layout` (slide-layouts.mjs) or, by default, the layout that
35 // usually follows the slide it comes after; then the stage moves to
36 // it once it exists on the server.
37 async function newSlide (after = ctx.slideId(), layout = '') {
38 const id = newID()
39 if (!layout) layout = layoutAfter(after ? ctx.slideJSON(after)?.template : '')
40 // commit resolves false when the batch was refused (a font it
41 // needed could not be loaded): there is no slide to move to.
42 if (await ctx.commit(layoutOps(id, layout, after || undefined)) === false) return
43 // The stage shows the server's HTML for the new slide, so it moves
44 // there once the server has it: flush resolves when the save is
45 // acknowledged (through a retry, if one is needed), false when it
46 // was refused, and then there is nothing to move to.
47 if (await ctx.flush() === false) return
48 location.hash = '#' + id
49 }
50 async function duplicateSlide (from = ctx.slideId()) {
51 if (!from) return
52 const id = newID()
53 if (await ctx.commit([{ op: 'duplicate_slide', id: from, new_id: id, new_block_ids: blockIDs(ctx.ydoc(), from).map(() => newID()) }]) === false) return
54 if (await ctx.flush() === false) return
55 location.hash = '#' + id
56 }
57 function moveSlide (id, dir) {
58 const op = moveSlideOp(slideIDs(ctx.ydoc()), id, dir)
59 if (op) ctx.commit([op])
60 }
61
62 // Delete asks first, in the callout at the top of the slide's options, and
63 // remembers which slide was asked about: the strip's menu can ask
64 // about a slide that is not yet on the stage. The card goes back to the
65 // slide's options BEFORE the stage is asked to move: clearing a box
66 // selected on this slide writes this slide's hash, and done after the
67 // move it wrote it over the other slide's, so the stage stayed here
68 // while the question was about the other one.
69 const confirmEl = ctx.confirmEl
70 let deleteTarget = ''
71 function askDelete (id) {
72 deleteTarget = id || ctx.slideId()
73 if (!deleteTarget) return
74 if (ctx.showSlideOptions) ctx.showSlideOptions()
75 if (deleteTarget !== ctx.slideId()) location.hash = '#' + deleteTarget
76 confirmEl.hidden = false
77 confirmEl.querySelector('[data-command="slide-delete"]').focus()
78 }
79 function deleteSlide () {
80 confirmEl.hidden = true
81 const id = deleteTarget
82 deleteTarget = ''
83 if (id && slideIDs(ctx.ydoc()).includes(id)) ctx.commit([{ op: 'remove_slide', id }])
84 }
85
86 // Skip in the short version and Park (talkOp): the Slide menu's are
87 // the current slide's, the strip item's menu names its own.
88 const TALK = { 'slide-talk-full': 'full', 'slide-talk-parked': 'parked' }
89 function setTalk (id, talk) {
90 const slide = id ? ctx.slideJSON(id) : null
91 if (slide) ctx.commit([talkOp(slide, talk)])
92 }
93
94 // A block command runs on the selected block of the current slide.
95 const withBlock = fn => () => {
96 const slideId = ctx.slideId()
97 const block = ctx.blockJSON(slideId, ctx.selected())
98 if (block) fn(slideId, block, ctx.slideJSON(slideId))
99 }
100 const moveBlock = dir => withBlock((s, b) => {
101 const op = moveBlockOp(s, blockIDs(ctx.ydoc(), s), b.id, dir)
102 if (op) ctx.commit([op])
103 })
104 const layer = dir => withBlock((s, b, slide) => {
105 const ops = zOps(s, slide.blocks, b.id, dir)
106 if (ops.length) ctx.commit(ops)
107 })
108
109 // A box of this kind on the current slide, after the selected one (or
110 // where its kind goes), then selected. commit may wait for the box's
111 // font (a code box's, say) and then run, or refuse: it is selected
112 // once it exists.
113 //
114 // A follow-along code asks for its audience link first (ctx.qrLink,
115 // POST /d/{sqid}/qr-link) and is added with the follow address in
116 // it, so the box and its link are one undo step. With no link to be
117 // had it is added empty, and its card's button tries again.
118 async function addBlock (kind) {
119 let slide = ctx.slideJSON(ctx.slideId())
120 if (!slide) return
121 // A scene block shows one of the deck's scenes (scene-card.mjs); a
122 // clip plays on a press until its card says otherwise, and its
123 // video is picked at once (clip-card.mjs).
124 const props = kind === 'scene' ? insertProps(view(ctx.ydoc()).scenes) : kind === 'clip' ? { play: 'press' } : {}
125 if (!props) return
126 const url = kind === 'qr' ? await audienceLink() : null
127 if (kind === 'qr') {
128 // The request took a moment: place the box against the slide as
129 // it is now, or not at all if it went.
130 slide = ctx.slideJSON(slide.id)
131 if (!slide) return
132 }
133 const id = newID()
134 const op = addBlockOp(slide, kind, id, ctx.selected(), props)
135 if (url) op.props.url = url
136 if (await ctx.commit([op]) === false) return
137 ctx.select(id)
138 if (kind === 'clip' && ctx.pickClip) ctx.pickClip()
139 }
140 async function audienceLink () {
141 if (!ctx.qrLink) return null
142 try {
143 return (await ctx.qrLink()) || null
144 } catch {
145 return null
146 }
147 }
148 // "Make an audience link" in a follow-along code's card: the selected
149 // code pointed at a new link. The old one keeps working until it is
150 // revoked on the share page.
151 async function remakeLink () {
152 const slideId = ctx.slideId()
153 const before = ctx.blockJSON(slideId, ctx.selected())
154 if (!before || before.type !== 'qr') return
155 const url = await audienceLink()
156 // The box may have gone while the link was being made.
157 if (!url || !ctx.blockJSON(slideId, before.id)) return
158 await ctx.commit([{ op: 'set_block', slide: slideId, id: before.id, props: { url } }])
159 }
160
161 const commands = {
162 'new-slide': () => newSlide(),
163 'slide-add-after': () => newSlide(),
164 'duplicate-slide': () => duplicateSlide(),
165 'slide-move-up': () => moveSlide(ctx.slideId(), 'up'),
166 'slide-move-down': () => moveSlide(ctx.slideId(), 'down'),
167 'slide-delete-ask': () => askDelete(''),
168 'slide-delete': deleteSlide,
169 'slide-delete-cancel': () => { confirmEl.hidden = true; deleteTarget = '' },
170 'slide-talk-full': () => setTalk(ctx.slideId(), 'full'),
171 'slide-talk-parked': () => setTalk(ctx.slideId(), 'parked'),
172 'accent-reset': () => ctx.commit([deckOp('accent-reset')]),
173 'block-move-up': moveBlock('up'),
174 'block-move-down': moveBlock('down'),
175 'block-forward': layer('forward'),
176 'block-back': layer('back'),
177 'block-delete': withBlock((s, b) => { ctx.clearSelection(); ctx.commit([{ op: 'remove_block', slide: s, id: b.id }]) }),
178 // The box's menu into the card (R12, spec §4.3).
179 'block-format': withBlock(() => ctx.showFormat && ctx.showFormat()),
180 'block-place': withBlock(() => ctx.openPlace && ctx.openPlace()),
181 'phone-reset': withBlock((s, b) => ctx.commit([resetPhone(s, b)])),
182 'qr-link': remakeLink,
183 undo: () => ctx.undo(),
184 redo: () => ctx.redo(),
185 present: () => ctx.present()
186 }
187
188 return {
189 has: name => name in commands,
190 // A command's own promise, for one that waits (qr-link).
191 run (name) {
192 if (name in commands) return commands[name]()
193 },
194 // The bar's title field (head.mjs): the deck's title, one set_meta.
195 setTitle: title => ctx.commit([deckOp('title', title)]),
196 // The Insert menu's rows name a kind (data-insert-block, head.mjs).
197 addBlock,
198 // The layout picker names its layout (data-layout-choice).
199 newSlide: layout => newSlide(ctx.slideId(), layout),
200 // The strip's item menu names a slide that may not be the current one.
201 strip (name, id) {
202 if (name === 'add-after') newSlide(id)
203 else if (name === 'duplicate') duplicateSlide(id)
204 else if (name === 'move-up' || name === 'move-down') moveSlide(id, name.slice(5))
205 else if (name === 'delete') askDelete(id)
206 else if (name in TALK) setTalk(id, TALK[name])
207 },
208 moveSlide
209 }
210}
211