| 1 | // fixtures/inspector-block.mjs — the inspector's Block form: the |
| 2 | // selected block's props (each field marked data-types with the types |
| 3 | // that have it), its placement (a region on a flow slide, a box and a |
| 4 | // layer on a canvas one), and its phone layer (§2.5). fillBlock puts |
| 5 | // the block's values in the controls and shows the right ones; |
| 6 | // blockChange turns a changed control into its op. |
| 7 | // |
| 8 | // The controls are typed by attribute rather than by name, so a new |
| 9 | // prop is a new field in editor.html and nothing here (data-kinds |
| 10 | // narrows a shape's field to some kinds of shape): |
| 11 | // |
| 12 | // data-prop="level" data-kind="int" a prop → set_block props |
| 13 | // data-text="label" a shape's label → set_text |
| 14 | // data-place="region" the region → set_block region |
| 15 | // data-box="x" a box field → set_block box, or |
| 16 | // set_phone box in the phone view |
| 17 | // data-phone="hidden|order|size" the phone layer → set_phone |
| 18 | // name="rst-block-appears" how the box arrives → set_block |
| 19 | // step and items (inspector-steps.mjs) |
| 20 | // |
| 21 | // size is view-dependent (switcher.mjs): the desktop Size field goes |
| 22 | // to the phone map in the phone view, which is what the switcher |
| 23 | // promises. The phone fieldset's own Size always goes to the map. |
| 24 | import { propOp, propValue, regionOp, labelOp, boxFieldOp } from './inspector-ops.mjs' |
| 25 | import { routeEdit, hasOverride } from './switcher.mjs' |
| 26 | import { defaultRegion } from './layout.mjs' |
| 27 | import { appearsOps, fillAppears } from './inspector-steps.mjs' |
| 28 | |
| 29 | // An undo or a redo is the person's own act, so the refill that shows |
| 30 | // it (inspector.fill({ force: true }), run at once) writes the focused |
| 31 | // control too: a field left showing the value just taken back looks like |
| 32 | // an undo that did nothing. Every other refill, a colleague's change |
| 33 | // included, still leaves the control in use alone. |
| 34 | let forcing = false |
| 35 | export function forceWrites (fn) { |
| 36 | const was = forcing |
| 37 | forcing = true |
| 38 | try { return fn() } finally { forcing = was } |
| 39 | } |
| 40 | |
| 41 | /** Sets a control's value without disturbing the one the person is typing in. */ |
| 42 | export function setValue (el, value) { |
| 43 | if (!el) return |
| 44 | if (el.type === 'checkbox') { |
| 45 | el.checked = !!value |
| 46 | return |
| 47 | } |
| 48 | // A segmented choice is one radio per value: the one whose value it |
| 49 | // is is checked (a prop's every radio carries its data-prop). |
| 50 | if (el.type === 'radio') { |
| 51 | el.checked = el.value === String(value ?? '') |
| 52 | return |
| 53 | } |
| 54 | // Never write into the control somebody is using: a value replaced |
| 55 | // under a caret loses what was half-typed, and a <select> or a colour |
| 56 | // picker being changed by a peer's edit mid-choice is the same |
| 57 | // surprise by another route. |
| 58 | if (!forcing && el === document.activeElement && |
| 59 | (el.tagName === 'SELECT' || (el.tagName === 'INPUT' && ['text', 'number', 'url', 'color'].includes(el.type)))) return |
| 60 | const v = value === null || value === undefined ? '' : String(value) |
| 61 | if (el.value !== v) el.value = v |
| 62 | } |
| 63 | |
| 64 | /** Shows the elements under root whose attr names a value in wanted, hides the rest. */ |
| 65 | export function showWhere (root, attr, test) { |
| 66 | for (const el of root.querySelectorAll(`[${attr}]`)) { |
| 67 | el.hidden = !test(el.getAttribute(attr)) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // The renderer's defaults, so an unset prop shows as what the slide |
| 72 | // shows rather than as the first option. |
| 73 | const DEFAULTS = { |
| 74 | level: 1, size: 'm', align: 'start', style: 'bullet', fit: 'cover', count: 2, |
| 75 | kind: 'rect', radius: 0, strokeWidth: 1, header: false, rows: 2, cols: 2, decorative: false, |
| 76 | // A clip with no play plays once (render.go's clipBlock). |
| 77 | play: 'once' |
| 78 | } |
| 79 | const SPACER_SIZE = 'l' |
| 80 | |
| 81 | /** The block's text as one plain string (a shape's label). */ |
| 82 | export function plainText (block) { |
| 83 | const paras = Array.isArray(block.text) ? block.text : [] |
| 84 | return paras.map(p => p.map(r => r.t || '').join('')).join('\n') |
| 85 | } |
| 86 | |
| 87 | /** The box the fields show: the phone override's in the phone view when there is one. */ |
| 88 | export function shownBox (block, view) { |
| 89 | if (view === 'phone' && block.phone && block.phone.box) return block.phone.box |
| 90 | return block.box || null |
| 91 | } |
| 92 | |
| 93 | const pctText = f => (Math.round(f * 1000) / 10).toFixed(1) |
| 94 | |
| 95 | export function fillBlock ({ form, slide, block, view, ink = '#000000', scenes = null }) { |
| 96 | // The box's type, for the one group its markup cannot mark by |
| 97 | // data-types: the Text group shows for a box with text, and also for |
| 98 | // the notes whatever box is selected, so editor.css decides it. |
| 99 | form.dataset.blockType = block.type |
| 100 | showWhere(form, 'data-types', v => v.split(' ').includes(block.type)) |
| 101 | // A shape's field for some kinds alone (corner radius: a rectangle's, |
| 102 | // spec §3.2), among those its type already shows. |
| 103 | const kind = (block.props && block.props.kind) || DEFAULTS.kind |
| 104 | for (const el of form.querySelectorAll('[data-kinds]')) { |
| 105 | if (!el.hidden && block.type === 'shape') el.hidden = !el.getAttribute('data-kinds').split(' ').includes(kind) |
| 106 | } |
| 107 | showWhere(form, 'data-layout', v => v === slide.layout) |
| 108 | form.querySelector('[slides-phone-badge]').hidden = !hasOverride(block) |
| 109 | fillAppears(form, slide, block, scenes) |
| 110 | |
| 111 | const props = block.props || {} |
| 112 | for (const el of form.querySelectorAll('[data-prop]')) { |
| 113 | const key = el.dataset.prop |
| 114 | let v = props[key] |
| 115 | if (v === undefined || v === null) v = key === 'size' && block.type === 'spacer' && el.name === 'rst-block-spacer-size' ? SPACER_SIZE : DEFAULTS[key] |
| 116 | setValue(el, v === undefined ? '' : v) |
| 117 | } |
| 118 | // A colour the box does not set shows as the slide draws it |
| 119 | // (render.go): no fill is none (data-none, a struck-through swatch), |
| 120 | // no stroke is the theme's ink. Nothing is written until one is picked. |
| 121 | for (const el of form.querySelectorAll('[slides-swatch][data-prop]')) { |
| 122 | const unset = !props[el.dataset.prop] |
| 123 | if (unset && el.dataset.prop === 'stroke') setValue(el, ink) |
| 124 | if (unset && el.dataset.prop === 'fill') el.setAttribute('data-none', '') |
| 125 | else el.removeAttribute('data-none') |
| 126 | } |
| 127 | for (const el of form.querySelectorAll('[data-text]')) setValue(el, plainText(block)) |
| 128 | for (const el of form.querySelectorAll('[data-place="region"]')) setValue(el, block.region || defaultRegion(block.type)) |
| 129 | const box = shownBox(block, view) |
| 130 | for (const el of form.querySelectorAll('[data-box]')) setValue(el, box ? pctText(box[el.dataset.box]) : '') |
| 131 | const phone = block.phone || {} |
| 132 | for (const el of form.querySelectorAll('[data-phone]')) { |
| 133 | const key = el.dataset.phone |
| 134 | if (key === 'hidden') setValue(el, phone.hidden === true) |
| 135 | else if (key === 'order') setValue(el, Number.isInteger(phone.order) ? phone.order : '') |
| 136 | else setValue(el, phone.size || '') |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** The ops for a changed control in the Block section (usually one; [] when it is not a control this file knows). */ |
| 141 | export function blockChange ({ el, slide, block, view }) { |
| 142 | const slideId = slide.id |
| 143 | if (el.name === 'rst-block-appears') return appearsOps(slide, block, el.value) |
| 144 | if (el.dataset.prop) { |
| 145 | const key = el.dataset.prop |
| 146 | const value = el.type === 'checkbox' ? el.checked : el.value |
| 147 | if (key === 'size' && block.type !== 'spacer') return [routeEdit({ view, slide: slideId, block, field: 'size', value })] |
| 148 | return [propOp(slideId, block, key, propValue(value, el.dataset.kind || ''))] |
| 149 | } |
| 150 | if (el.dataset.text) return [labelOp(slideId, block, el.value.trim())] |
| 151 | if (el.dataset.place === 'region') return [regionOp(slideId, block, el.value)] |
| 152 | if (el.dataset.box) { |
| 153 | const op = boxFieldOp({ view, slideId, block, key: el.dataset.box, percent: el.value }) |
| 154 | return op ? [op] : [] |
| 155 | } |
| 156 | if (el.dataset.phone) { |
| 157 | const key = el.dataset.phone |
| 158 | let value |
| 159 | if (key === 'hidden') value = el.checked |
| 160 | else if (key === 'order') value = el.value === '' ? null : propValue(el.value, 'int') |
| 161 | else value = el.value |
| 162 | return [routeEdit({ view: 'phone', slide: slideId, block, field: key, value })] |
| 163 | } |
| 164 | return [] |
| 165 | } |
| 166 | |