| 1 | // fixtures/outline.mjs — View → Outline (spec §4.4): a popover listing |
| 2 | // the slide's boxes by kind and first words. Choosing one selects that |
| 3 | // box, closes the Outline and puts focus on the box. It is the list the |
| 4 | // card held (the Boxes group) until the boxes became tab stops |
| 5 | // (stage-focus.mjs), moved out of the card and opened by name. |
| 6 | // |
| 7 | // The popover is the template's (#slides-outline, in the top layer); |
| 8 | // every word in it is the catalog's. Escape closes it and gives focus |
| 9 | // back to where it was opened from (the View menu's title), and goes no |
| 10 | // further: the selection.mjs Escape that gives a box up is not this |
| 11 | // one. A click outside closes it too (the browser's light dismiss). |
| 12 | // |
| 13 | // mountOutline({popover, getBlocks, getSelected, select, labels}) → |
| 14 | // {fill(), open({returnTo}), close(), isOpen(), destroy()}. getBlocks() is the |
| 15 | // slide's blocks in order ([] with no slide), select(id) selects a box |
| 16 | // and focuses it, labels.types names each kind. |
| 17 | import { blockLabel } from './inspector-ops.mjs' |
| 18 | |
| 19 | /** |
| 20 | * The list, reconciled by id rather than rebuilt: fill() runs on EVERY |
| 21 | * document change while the Outline is open, a colleague's keystrokes |
| 22 | * included, and a rebuild would take the focused row out of the |
| 23 | * document under somebody's finger. Same shape as strip.plan: keep |
| 24 | * what is wanted, drop what is not, and walk the order once. |
| 25 | * |
| 26 | * list is the rows' container, none the "no boxes yet" line, blocks |
| 27 | * the slide's blocks in order, selected the selected block's id, |
| 28 | * label(b) the words on a row, and make() a new row: a button whose |
| 29 | * [estilo-menu-label], or itself, takes the words. |
| 30 | */ |
| 31 | export function fillBlocks ({ list, none, blocks, selected, label, make = plainRow }) { |
| 32 | if (none) none.hidden = blocks.length > 0 |
| 33 | const have = new Map() |
| 34 | for (const el of list.querySelectorAll('[data-select-block]')) have.set(el.dataset.selectBlock, el) |
| 35 | |
| 36 | const want = [] |
| 37 | for (const b of blocks) { |
| 38 | let btn = have.get(b.id) |
| 39 | if (btn) have.delete(b.id) |
| 40 | else { |
| 41 | btn = make() |
| 42 | btn.dataset.selectBlock = b.id |
| 43 | } |
| 44 | // Written only when it differs: assigning the same text replaces |
| 45 | // the text node, which is enough to lose a selection inside it. |
| 46 | const words = label(b) |
| 47 | const text = btn.querySelector('[estilo-menu-label]') || btn |
| 48 | if (text.textContent !== words) text.textContent = words |
| 49 | const pressed = b.id === selected ? 'true' : 'false' |
| 50 | if (btn.getAttribute('aria-pressed') !== pressed) btn.setAttribute('aria-pressed', pressed) |
| 51 | want.push(btn) |
| 52 | } |
| 53 | for (const el of have.values()) el.remove() |
| 54 | |
| 55 | // insertBefore only where the order actually differs. Re-appending a |
| 56 | // node that is already in place is still a remove and an insert, and |
| 57 | // a focused button would be blurred by it. |
| 58 | let at = list.firstElementChild |
| 59 | for (const btn of want) { |
| 60 | if (at === btn) { |
| 61 | at = btn.nextElementSibling |
| 62 | continue |
| 63 | } |
| 64 | list.insertBefore(btn, at) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // How far the panel keeps from the window's edges, in CSS px. |
| 69 | const EDGE = 8 |
| 70 | |
| 71 | function plainRow () { |
| 72 | const btn = document.createElement('button') |
| 73 | btn.type = 'button' |
| 74 | return btn |
| 75 | } |
| 76 | |
| 77 | export function mountOutline ({ popover, getBlocks, getSelected = () => '', select, labels = { types: {} } }) { |
| 78 | const list = popover.querySelector('[slides-outline-list]') |
| 79 | const none = popover.querySelector('[slides-outline-none]') |
| 80 | // A row as the template draws it (estilo's menu row, with its check). |
| 81 | const tmpl = popover.querySelector('[slides-outline-row-template]') |
| 82 | const make = tmpl && tmpl.content ? () => tmpl.content.firstElementChild.cloneNode(true) : plainRow |
| 83 | const abort = new AbortController() |
| 84 | const { signal } = abort |
| 85 | let shown = false |
| 86 | let returnTo = null |
| 87 | |
| 88 | function fill () { |
| 89 | if (!shown) return |
| 90 | fillBlocks({ |
| 91 | list, |
| 92 | none, |
| 93 | blocks: getBlocks(), |
| 94 | selected: getSelected(), |
| 95 | label: b => blockLabel(b, labels.types[b.type] || b.type), |
| 96 | make |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | // Under what opened it (the View menu's title), and focus on the |
| 101 | // selected box's row, or the first; on the popover with no rows. |
| 102 | function open ({ returnTo: back = null } = {}) { |
| 103 | returnTo = back |
| 104 | shown = true |
| 105 | fill() |
| 106 | const r = back && typeof back.getBoundingClientRect === 'function' ? back.getBoundingClientRect() : null |
| 107 | if (r && popover.style) { |
| 108 | popover.style.left = `${Math.round(r.left)}px` |
| 109 | popover.style.top = `${Math.round(r.bottom)}px` |
| 110 | } |
| 111 | popover.showPopover() |
| 112 | if (r && popover.style) keepInWindow(r) |
| 113 | const rows = [...list.querySelectorAll('[data-select-block]')] |
| 114 | const row = rows.find(b => b.getAttribute('aria-pressed') === 'true') || rows[0] || popover |
| 115 | row.focus() |
| 116 | } |
| 117 | |
| 118 | // The panel inside the window on every side, EDGE short of it: as |
| 119 | // tall as the room under its title allows (it scrolls inside itself |
| 120 | // past that), and moved back from the end edge when its title is |
| 121 | // nearer that edge than the panel is wide (a narrow window). |
| 122 | function keepInWindow (r) { |
| 123 | const w = globalThis.innerWidth |
| 124 | const h = globalThis.innerHeight |
| 125 | if (typeof w !== 'number' || typeof popover.getBoundingClientRect !== 'function') return |
| 126 | const top = Math.min(Math.round(r.bottom), Math.max(EDGE, h - EDGE - 64)) |
| 127 | popover.style.top = `${top}px` |
| 128 | popover.style.maxBlockSize = `${Math.max(0, h - top - EDGE)}px` |
| 129 | const width = popover.getBoundingClientRect().width |
| 130 | popover.style.left = `${Math.round(Math.max(EDGE, Math.min(r.left, w - width - EDGE)))}px` |
| 131 | } |
| 132 | |
| 133 | // restore: focus back to what opened it (Escape); not when a row was |
| 134 | // chosen, which puts it on the box. |
| 135 | function close ({ restore = true } = {}) { |
| 136 | if (shown) { |
| 137 | shown = false |
| 138 | popover.hidePopover() |
| 139 | } |
| 140 | const back = returnTo |
| 141 | returnTo = null |
| 142 | if (restore && back && back.isConnected !== false && typeof back.focus === 'function') back.focus() |
| 143 | } |
| 144 | |
| 145 | // A click outside, or the browser closing it any other way. |
| 146 | popover.addEventListener('toggle', e => { if (e.newState === 'closed') shown = false }, { signal }) |
| 147 | popover.addEventListener('click', e => { |
| 148 | const row = e.target.closest('[data-select-block]') |
| 149 | if (!row) return |
| 150 | const id = row.dataset.selectBlock |
| 151 | close({ restore: false }) |
| 152 | select(id) |
| 153 | }, { signal }) |
| 154 | popover.addEventListener('keydown', e => { |
| 155 | if (e.key !== 'Escape' || e.isComposing) return |
| 156 | e.preventDefault() |
| 157 | e.stopPropagation() |
| 158 | close() |
| 159 | }, { signal }) |
| 160 | |
| 161 | return { fill, open, close, isOpen: () => shown, destroy () { abort.abort() } } |
| 162 | } |
| 163 | |