oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/selection.mjs — which block is selected on the stage, and
2// how that is said: an outline and aria-selected on the block's
3// element, the block's id in the URL hash after the slide's
4// (#<slide>/<block>), and the inspector's Block section following it.
5//
6// Selecting is a click on a block, focus on it (every box is a tab
7// stop, stage-focus.mjs), or a row of View → Outline; Escape clears. A refresh
8// replaces the stage's DOM, so the selection is re-applied by id to
9// the new element after every load — and cleared when the block has
10// gone, which is how a deleted block leaves the inspector.
11//
12// The hash is written with replaceState: a block selection is not a
13// place to go back to, and the entry's hashchange handler must not
14// reload the slide for it. The pure parts (the hash's two halves) are
15// tested in selection.test.mjs.
16
17/** '#<slide>/<block>' → {slide, block}; a bare '#<slide>' has block ''. Never throws. */
18export function parseHash (hash) {
19 let raw = (hash || '').replace(/^#/, '')
20 try { raw = decodeURIComponent(raw) } catch { /* a stray %: read as typed */ }
21 const at = raw.indexOf('/')
22 if (at < 0) return { slide: raw, block: '' }
23 return { slide: raw.slice(0, at), block: raw.slice(at + 1) }
24}
25
26/** The hash for a slide and, optionally, a block on it. */
27export function formatHash (slide, block) {
28 if (!slide) return ''
29 return '#' + slide + (block ? '/' + block : '')
30}
31
32/** The block element on the stage with this id, or null. */
33export function blockElement (stageEl, id) {
34 if (!stageEl || !id) return null
35 return stageEl.querySelector(`[slides-block][data-id="${CSS.escape(id)}"]`)
36}
37
38/**
39 * Mounts the selection over the stage element. getSlideId() is the
40 * stage's current slide; onChange(id, how) fires with the selected block's
41 * id, or '' on clear; how is { kept: true } when a refresh only
42 * re-marked the same block. Returns {select, clear, reapply, current}.
43 */
44export function mountSelection ({ stageEl, getSlideId, onChange = () => {} }) {
45 let current = ''
46 // Every listener goes on one signal, so destroy() takes all of them
47 // off in one line — the document-level Escape included. A resync
48 // mounts a second selection over the SAME stage element (it is the
49 // page's, not the document's), and without this the first one would
50 // go on marking blocks for a Y.Doc that has been thrown away.
51 const abort = new AbortController()
52 const { signal } = abort
53
54 function mark (id, on) {
55 const el = blockElement(stageEl, id)
56 if (!el) return false
57 if (on) {
58 el.setAttribute('data-selected', 'true')
59 el.setAttribute('aria-selected', 'true')
60 } else {
61 el.removeAttribute('data-selected')
62 el.removeAttribute('aria-selected')
63 }
64 return true
65 }
66
67 function writeHash () {
68 const slide = getSlideId()
69 if (!slide) return
70 const want = formatHash(slide, current)
71 if (location.hash !== want) history.replaceState(null, '', want)
72 }
73
74 // A block just added is selected before the stage has re-fetched
75 // it: the id is kept and the element marked on the next reapply().
76 // Choosing the block that is already selected changes nothing here,
77 // but is still heard: it is this person going back to that box (from
78 // the notes, say), and the card shows its options again (inspect.mjs).
79 function select (id) {
80 if (id && id === current) {
81 onChange(current)
82 return
83 }
84 if (current) mark(current, false)
85 current = id || ''
86 mark(current, true)
87 writeHash()
88 onChange(current)
89 }
90
91 function clear () {
92 if (!current) return
93 mark(current, false)
94 current = ''
95 writeHash()
96 onChange('')
97 }
98
99 // After a refresh the element is new: mark it again, or let the
100 // selection go with the block. Kept, it is said with { kept: true }:
101 // the same box, re-marked, and not this person choosing it again.
102 function reapply () {
103 if (!current) return
104 if (!mark(current, true)) {
105 current = ''
106 writeHash()
107 onChange('')
108 } else {
109 onChange(current, { kept: true })
110 }
111 }
112
113 stageEl.addEventListener('click', e => {
114 const el = e.target.closest('[slides-block][data-id]')
115 if (el && stageEl.contains(el)) select(el.getAttribute('data-id'))
116 }, { signal })
117 // Escape clears, from anywhere but a dialog, a menu or a popover (the
118 // Outline, the layouts, the box's menu: Escape closes them first, and
119 // the box's menu gives focus back to the box), and one press does
120 // it from a box's text too. The
121 // text editor marks every Escape handled: prosemirror-view's own
122 // keydown capture (captureKeyDown, keyCode 27) prevents the browser's
123 // default and runs no command, so nothing a person sees has taken
124 // that press. An Escape that ends an input method's composition is
125 // the composition's.
126 document.addEventListener('keydown', e => {
127 if (e.key !== 'Escape' || e.isComposing || !current) return
128 const target = e.target instanceof Element ? e.target : null
129 const inStageText = !!(target && stageEl.contains(target) && target.closest('.ProseMirror'))
130 if (e.defaultPrevented && !inStageText) return
131 if (target && target.closest('dialog, [rst-dropdown], [popover], [slides-box-menu]')) return
132 e.preventDefault()
133 clear()
134 if (target instanceof HTMLElement) target.blur()
135 }, { signal })
136
137 return {
138 select,
139 clear,
140 reapply,
141 destroy () { abort.abort() },
142 get current () { return current }
143 }
144}
145