oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/layout-pick.mjs — the card at rest (spec §3.1): the slide's
2// layout as eight pictures, the slide's seven templates and then Free
3// placement, exactly one lit; and the two quiet rows under them,
4// Background and Transition, each opening a popover that holds the
5// field the card used to show.
6//
7// The markup is the server's (templates/editor.html), drawn with the
8// painted slide's picture already lit, so the first paint is final;
9// here the pictures are lit for whichever slide is current, and a
10// choice is turned into the ops the Template and Layout fields wrote.
11//
12// A template chosen on a canvas slide drops every box's position, so it
13// asks first, in the card ([slides-confirm-layout], the way Delete
14// slide asks). While it asks, the lit (checked) picture is still the
15// slide's true one, Free placement; the picture asked about is marked
16// apart from it (data-asked, a dashed outline, and aria-describedby
17// naming the question), never as chosen. Use the layout commits both
18// ops in one commit, one undo step; Keep free placement commits
19// nothing. The question is about one slide as it was when asked: if
20// that slide stops being canvas (a colleague, an undo) or the card
21// moves to another slide, the question withdraws and nothing is
22// committed (Review Focus 2).
23//
24// Escape inside a row's popover closes it and puts focus back on its
25// row, and goes no further, as the Outline's does (outline.mjs): the
26// popover is in the top layer, but in the DOM it is inside the slide
27// form, so the form's change listener (inspector.mjs) still hears its
28// fields.
29import { slideOp } from './inspector-ops.mjs'
30
31export const FREE = 'free'
32/** The pictures' values, in the card's order: the templates, then Free placement. */
33export const PICTURES = ['title', 'title-body', 'two-column', 'section', 'image-full', 'quote', 'blank', FREE]
34
35/** The picture lit for slide: Free placement on a canvas slide, else its template. */
36export function litFor (slide) {
37 if (!slide) return ''
38 return slide.layout === 'canvas' ? FREE : slide.template
39}
40
41/**
42 * What choosing value on slide writes, and whether it asks first: a
43 * template on a flow slide is the template; Free placement is the
44 * canvas layout; a template on a canvas slide is the flow layout and
45 * then the template, asked first; the lit picture again is nothing.
46 */
47export function pickOps (slide, value) {
48 if (value === litFor(slide)) return { ops: [], ask: false }
49 if (value === FREE) return { ops: [slideOp(slide, 'layout', 'canvas')], ask: false }
50 if (slide.layout === 'canvas') {
51 return { ops: [slideOp(slide, 'layout', 'flow'), slideOp(slide, 'template', value)], ask: true }
52 }
53 return { ops: [slideOp(slide, 'template', value)], ask: false }
54}
55
56/**
57 * Mounts the pictures, the question and the rows' popovers on form,
58 * the card's slide form. getSlide() is the current slide's JSON (null
59 * with none); commit(ops) applies one batch. Returns {fill, withdraw,
60 * destroy}: fill() after every document change, withdraw() to drop an
61 * open question.
62 */
63export function mountLayoutPick ({ form, getSlide, commit }) {
64 const radios = () => [...form.querySelectorAll('input[name="slides-layout-pick"]')]
65 const confirm = form.querySelector('[slides-confirm-layout]')
66 const action = confirm && confirm.querySelector('[data-command="layout-leave-free"]')
67 const question = confirm && confirm.querySelector('[slides-confirm-layout-text]')
68 // The question being asked: which slide, which picture, what it writes.
69 let asked = null
70
71 // The picture asked about, marked as asked about and described by the
72 // question; '' takes the mark off every picture.
73 function markAsked (value) {
74 for (const r of radios()) {
75 const item = r.closest('[slides-layout-pick-item]')
76 if (r.value === value) {
77 if (item) item.setAttribute('data-asked', '')
78 if (question) r.setAttribute('aria-describedby', question.getAttribute('id'))
79 } else {
80 if (item && item.hasAttribute('data-asked')) item.removeAttribute('data-asked')
81 if (r.hasAttribute('aria-describedby')) r.removeAttribute('aria-describedby')
82 }
83 }
84 }
85
86 function light (value) {
87 for (const r of radios()) {
88 const on = r.value === value
89 if (r.checked !== on) r.checked = on
90 }
91 }
92 const radioFor = value => radios().find(r => r.value === value) || null
93
94 function withdraw () {
95 asked = null
96 if (confirm) confirm.hidden = true
97 markAsked('')
98 light(litFor(getSlide()))
99 }
100
101 function fill () {
102 const slide = getSlide()
103 if (asked && (!slide || slide.id !== asked.id || slide.layout !== 'canvas')) withdraw()
104 else light(litFor(slide))
105 }
106
107 function choose (value) {
108 const slide = getSlide()
109 if (!slide) return
110 const { ops, ask } = pickOps(slide, value)
111 if (ask) {
112 asked = { id: slide.id, value, ops }
113 light(litFor(slide))
114 markAsked(value)
115 confirm.hidden = false
116 if (action) action.focus()
117 return
118 }
119 if (asked) withdraw()
120 if (ops.length) commit(ops)
121 }
122
123 function leave () {
124 const slide = getSlide()
125 const q = asked
126 if (!q || !slide || slide.id !== q.id || slide.layout !== 'canvas') {
127 withdraw()
128 return
129 }
130 asked = null
131 confirm.hidden = true
132 markAsked('')
133 light(q.value)
134 const r = radioFor(q.value)
135 if (r) r.focus()
136 commit(q.ops)
137 }
138
139 function cancel () {
140 withdraw()
141 const r = radioFor(litFor(getSlide()))
142 if (r) r.focus()
143 }
144
145 const abort = new AbortController()
146 const { signal } = abort
147 form.addEventListener('change', e => {
148 const t = e.target
149 if (t && t.name === 'slides-layout-pick' && t.checked !== false) choose(t.value)
150 }, { signal })
151 form.addEventListener('click', e => {
152 const btn = e.target && e.target.closest ? e.target.closest('[data-command]') : null
153 if (!btn) return
154 const name = btn.dataset.command
155 if (name === 'layout-leave-free') leave()
156 else if (name === 'layout-leave-free-cancel') cancel()
157 }, { signal })
158 form.addEventListener('keydown', e => {
159 if (e.key !== 'Escape' || e.isComposing) return
160 const pop = e.target && e.target.closest ? e.target.closest('[popover]') : null
161 if (!pop || !form.contains(pop)) return
162 e.preventDefault()
163 e.stopPropagation()
164 if (pop.matches(':popover-open')) pop.hidePopover()
165 const row = form.querySelector(`[popovertarget="${pop.getAttribute('id')}"]`)
166 if (row) row.focus()
167 }, { signal })
168
169 return { fill, withdraw, destroy () { abort.abort() } }
170}
171