// box-menu.mjs: the menu on a selected box (the quieter editor, spec // §4). Which rows a box has, whose a right-click is, which keys are the // box's, where the round button and the menu go, and the menu itself // against the DOM stub: it draws only the rows that apply, a row runs // its command and closes, a region row sets the region, Esc closes and // gives focus back to the box, and an empty selection closes it. import test from 'node:test' import assert from 'node:assert/strict' import { el as stub, withDocument } from './dom-stub.mjs' import { rowsFor, canOwnContextMenu, boxKey, mountBoxMenu } from './box-menu.mjs' test('rowsFor: a canvas box is arranged in layers and placed; a flow box by region', () => { assert.deepEqual(rowsFor({ layout: 'canvas', type: 'shape' }), ['block-format', 'block-forward', 'block-back', 'block-move-up', 'block-move-down', 'block-place', 'block-delete']) assert.deepEqual(rowsFor({ layout: 'flow', type: 'heading' }), ['block-format', 'block-move-up', 'block-move-down', 'region', 'block-delete']) // Every kind has the same rows: what decides is the slide. assert.deepEqual(rowsFor({ layout: 'flow', type: 'image' }), rowsFor({ layout: 'flow', type: 'heading' })) // A copy each time: a caller cannot change the next answer. rowsFor({ layout: 'flow', type: 'text' }).pop() assert.equal(rowsFor({ layout: 'flow', type: 'text' }).length, 5) }) test('canOwnContextMenu: never in text being edited, anywhere else on a box', () => { const ctx = withDocument() try { const text = stub('span') const editable = stub('h1', { contenteditable: 'true' }, [text]) const shape = stub('div', { 'slides-block': '' }) ctx.document.append(editable, shape) ctx.document.activeElement = editable assert.equal(canOwnContextMenu(text), false, 'the caret is in this text: the browser\'s menu, spelling and paste') assert.equal(canOwnContextMenu(editable), false) assert.equal(canOwnContextMenu(shape), true) ctx.document.activeElement = null assert.equal(canOwnContextMenu(text), true, 'text nobody is editing: the box\'s menu') assert.equal(canOwnContextMenu(null), false) } finally { ctx.restore() } }) test('boxKey: the box\'s keys, and Delete and the arrows only from the box itself', () => { const k = (key, mods = {}, code = '') => ({ key, code, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, ...mods }) for (const onBox of [true, false]) { assert.equal(boxKey(k(']', { ctrlKey: true }), { onBox }), 'block-forward') assert.equal(boxKey(k('[', { metaKey: true }), { onBox }), 'block-back') assert.equal(boxKey(k('F10', { shiftKey: true }), { onBox }), 'menu') } // By the key's place when the layout gives it another character. assert.equal(boxKey(k('ü', { ctrlKey: true }, 'BracketLeft'), { onBox: true }), 'block-back') // Mod+Shift+[ is the browser's (the tab before), and Alt is not ours. assert.equal(boxKey(k('{', { metaKey: true, shiftKey: true }, 'BracketLeft'), { onBox: true }), null) assert.equal(boxKey(k(']', { ctrlKey: true, altKey: true }), { onBox: true }), null) assert.equal(boxKey(k('ArrowUp', { ctrlKey: true, shiftKey: true }), { onBox: true }), 'block-move-up') assert.equal(boxKey(k('ArrowDown', { metaKey: true, shiftKey: true }), { onBox: true }), 'block-move-down') assert.equal(boxKey(k('Delete'), { onBox: true }), 'block-delete') assert.equal(boxKey(k('Backspace'), { onBox: true }), 'block-delete') // In text these select and delete text. assert.equal(boxKey(k('ArrowUp', { ctrlKey: true, shiftKey: true }), { onBox: false }), null) assert.equal(boxKey(k('Delete'), { onBox: false }), null) assert.equal(boxKey(k('Backspace'), { onBox: false }), null) // The overlay's own: the arrows nudge, Shift further, Alt resizes. for (const mods of [{}, { shiftKey: true }, { altKey: true }]) assert.equal(boxKey(k('ArrowUp', mods), { onBox: true }), null) assert.equal(boxKey(k('Delete', { ctrlKey: true }), { onBox: true }), null) assert.equal(boxKey(k('F10'), { onBox: true }), null) }) // The menu as editor.html draws it: every row, a group of region rows. function menuMarkup () { const row = (id, extra = {}) => stub('button', { 'estilo-menu-item': '', 'data-row': id, 'data-command': id, ...extra }) const regions = ['heading', 'body', 'aside', 'media'].map(r => stub('button', { 'estilo-menu-item': '', 'data-region': r, 'aria-checked': 'false' })) const menu = stub('div', { 'slides-box-menu': '' }, [ row('block-format'), stub('hr'), row('block-forward'), row('block-back'), row('block-move-up'), row('block-move-down'), stub('hr'), row('block-place'), stub('div', { 'data-row': 'region' }, regions), stub('hr'), row('block-delete') ]) menu.showPopover = () => { menu.popoverOpen = true } menu.hidePopover = () => { menu.popoverOpen = false } return menu } function rig ({ layout = 'canvas', type = 'shape', region } = {}) { const ctx = withDocument() const box = stub('div', { 'slides-block': '', 'data-id': 'b1' }) const stageEl = stub('div', { 'slides-stage': '' }, [box]) // The stage's frame: nothing outside Phone view, the phone's screen in it. const frame = stub('div', { 'slides-stage-frame': '' }, [stageEl]) const button = stub('button', { 'slides-box-more': '' }) const menu = menuMarkup() const root = stub('div', { 'slides-editor': '' }, [frame, button, menu]) ctx.document.append(root) const calls = [] const state = { selected: 'b1', layout } const block = { id: 'b1', type, region } const m = mountBoxMenu({ root, stageEl, getSelected: () => state.selected, getSlide: () => ({ id: 's1', layout: state.layout, blocks: [block] }), run: name => calls.push(['run', name]), setRegion: (id, r) => calls.push(['region', id, r]), focusBox: id => calls.push(['focus', id]), select: id => { state.selected = id }, after: (name, id) => calls.push(['after', name, id]) }) const shown = () => menu.querySelectorAll('[estilo-menu-item]').filter(i => !i.hidden && !i.closest('[data-row]').hidden) return { ctx, m, root, menu, button, box, stageEl, frame, calls, state, shown, block } } test('open draws only the rows that apply, and focuses the first', () => { const canvas = rig() try { assert.equal(canvas.m.open('button'), true) assert.equal(canvas.m.isOpen(), true) assert.equal(canvas.menu.popoverOpen, true) assert.deepEqual(canvas.shown().map(i => i.dataset.row), rowsFor({ layout: 'canvas' })) assert.equal(canvas.ctx.document.activeElement, canvas.shown()[0]) assert.equal(canvas.button.getAttribute('aria-expanded'), 'true') } finally { canvas.m.destroy() canvas.ctx.restore() } const flow = rig({ layout: 'flow', type: 'image' }) try { flow.m.open('button') const rows = flow.shown().map(i => i.dataset.row || 'region:' + i.dataset.region) assert.deepEqual(rows, ['block-format', 'block-move-up', 'block-move-down', 'region:heading', 'region:body', 'region:aside', 'region:media', 'block-delete']) // The box's region is checked: an image's, where nobody said, is media. assert.deepEqual(flow.menu.querySelectorAll('[data-region]').map(b => b.getAttribute('aria-checked')), ['false', 'false', 'false', 'true']) } finally { flow.m.destroy() flow.ctx.restore() } }) test('a row runs its command and closes; a region row sets the region', () => { const r = rig({ layout: 'flow', type: 'text', region: 'aside' }) try { r.m.open('button') const row = r.menu.querySelector('[data-row="block-move-down"]') row.dispatchEvent({ type: 'click', target: row }) assert.equal(r.m.isOpen(), false) assert.equal(r.menu.popoverOpen, false) assert.equal(r.button.getAttribute('aria-expanded'), 'false') assert.deepEqual(r.calls, [['focus', 'b1'], ['run', 'block-move-down'], ['after', 'block-move-down', 'b1']]) r.calls.length = 0 r.m.open('button') const body = r.menu.querySelector('[data-region="body"]') body.dispatchEvent({ type: 'click', target: body }) assert.equal(r.m.isOpen(), false) assert.deepEqual(r.calls, [['focus', 'b1'], ['region', 'b1', 'body'], ['after', 'region', 'b1']]) // Delete: no focus back to the box, which is going; where focus goes // next is after()'s (stage-focus.mjs). r.calls.length = 0 r.m.open('button') const del = r.menu.querySelector('[data-row="block-delete"]') del.dispatchEvent({ type: 'click', target: del }) assert.deepEqual(r.calls, [['run', 'block-delete'], ['after', 'block-delete', 'b1']]) } finally { r.m.destroy() r.ctx.restore() } }) test('the arrows move through the rows drawn; Esc closes and focuses the box, and goes no further', () => { const r = rig({ layout: 'flow', type: 'heading' }) try { r.m.open('button') const rows = r.shown() const key = k => { const ev = { type: 'keydown', key: k, target: r.ctx.document.activeElement }; r.ctx.document.activeElement.dispatchEvent(ev); return ev } key('ArrowDown') assert.equal(r.ctx.document.activeElement, rows[1]) key('ArrowUp'); key('ArrowUp') assert.equal(r.ctx.document.activeElement, rows[rows.length - 1], 'up from the first is the last') key('Home') assert.equal(r.ctx.document.activeElement, rows[0]) const esc = key('Escape') assert.equal(esc.defaultPrevented, true) assert.equal(esc.stopped, true, 'the selection\'s own Escape never hears it') assert.equal(r.m.isOpen(), false) assert.deepEqual(r.calls, [['focus', 'b1']]) } finally { r.m.destroy() r.ctx.restore() } }) test('place: the round button follows the selection, and an empty selection closes the menu', () => { const r = rig() try { r.m.place() assert.equal(r.button.hidden, false) r.m.open('button') r.state.selected = '' r.m.place() assert.equal(r.m.isOpen(), false, 'the box it was for is not selected now') assert.equal(r.button.hidden, true) // A box that has gone from the stage is the same. r.state.selected = 'b1' r.m.place() r.m.open('button') r.box.remove() r.m.place() assert.equal(r.m.isOpen(), false) assert.equal(r.button.hidden, true) assert.equal(r.m.open('button'), true, 'the slide still has the box: its menu still opens') r.state.selected = '' assert.equal(r.m.close() ?? true, true) assert.equal(r.m.open('button'), false, 'nothing selected: no menu') } finally { r.m.destroy() r.ctx.restore() } }) test('a right-click on a box opens the menu; in text being edited it is the browser\'s', () => { const r = rig({ layout: 'flow', type: 'heading' }) try { const word = stub('span') const editable = stub('h1', { 'slides-block': '', 'data-id': 'b1', contenteditable: 'true' }, [word]) r.box.remove() r.stageEl.append(editable) r.ctx.document.activeElement = editable const inText = { type: 'contextmenu', target: word, clientX: 40, clientY: 40 } word.dispatchEvent(inText) assert.equal(inText.defaultPrevented, undefined) assert.equal(r.m.isOpen(), false) r.ctx.document.activeElement = null const onBox = { type: 'contextmenu', target: word, clientX: 40, clientY: 40 } word.dispatchEvent(onBox) assert.equal(onBox.defaultPrevented, true) assert.equal(r.m.isOpen(), true) } finally { r.m.destroy() r.ctx.restore() } }) test('the keys: from the box, from its text, and never from outside the stage', () => { const r = rig({ layout: 'flow', type: 'shape' }) try { const press = (target, key, mods = {}) => { r.ctx.document.activeElement = target const ev = { type: 'keydown', key, target, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, ...mods } target.dispatchEvent(ev) return ev } // The box itself has focus: Delete deletes it. assert.equal(press(r.box, 'Delete').defaultPrevented, true) assert.deepEqual(r.calls, [['run', 'block-delete'], ['after', 'block-delete', 'b1']]) // Bring forward on a flow slide: held from the browser, nothing done. r.calls.length = 0 assert.equal(press(r.box, ']', { ctrlKey: true }).defaultPrevented, true) assert.deepEqual(r.calls, []) // Outside the stage nothing is the box's: the strip's Delete, the bar's. const strip = stub('a') r.ctx.document.append(strip) assert.equal(press(strip, 'Delete').defaultPrevented, undefined) assert.equal(press(strip, '[', { metaKey: true }).defaultPrevented, undefined) // Nothing selected: nothing. r.state.selected = '' assert.equal(press(r.box, 'Delete').defaultPrevented, undefined) assert.deepEqual(r.calls, []) // Shift+F10 opens the menu. r.state.selected = 'b1' press(r.box, 'F10', { shiftKey: true }) assert.equal(r.m.isOpen(), true) } finally { r.m.destroy() r.ctx.restore() } }) // A colleague changes the slide under an open menu (the review's A3): // its rows were for the slide as it was. test('sync: the menu closes when the slide\'s layout changes, and its region checks follow the box', () => { const r = rig({ layout: 'canvas', type: 'shape' }) try { r.m.open('button') r.calls.length = 0 r.m.sync() assert.equal(r.m.isOpen(), true, 'nothing changed: it stays open') r.state.layout = 'flow' r.m.sync() assert.equal(r.m.isOpen(), false, 'the slide is flow now: the canvas rows were the old slide\'s') assert.deepEqual(r.calls, [['focus', 'b1']], 'focus back on the box, which is still there') } finally { r.m.destroy() r.ctx.restore() } const f = rig({ layout: 'flow', type: 'text', region: 'body' }) try { f.m.open('button') f.block.region = 'aside' f.m.sync() assert.equal(f.m.isOpen(), true) assert.deepEqual(f.menu.querySelectorAll('[data-region]').map(b => b.getAttribute('aria-checked')), ['false', 'false', 'true', 'false']) } finally { f.m.destroy() f.ctx.restore() } }) test('a row that no longer applies to the box does nothing, and closes the menu', () => { const r = rig({ layout: 'canvas', type: 'shape' }) try { r.m.open('button') r.state.layout = 'flow' r.calls.length = 0 const place = r.menu.querySelector('[data-row="block-place"]') place.dispatchEvent({ type: 'click', target: place }) assert.equal(r.m.isOpen(), false) assert.deepEqual(r.calls.filter(c => c[0] === 'run'), [], 'Position and size is not a flow slide\'s') // The box gone from the slide: nothing either. r.state.layout = 'canvas' r.m.open('button') r.block.id = 'gone' r.calls.length = 0 const del = r.menu.querySelector('[data-row="block-delete"]') del.dispatchEvent({ type: 'click', target: del }) assert.deepEqual(r.calls.filter(c => c[0] === 'run'), []) } finally { r.m.destroy() r.ctx.restore() } }) test('destroy takes every listener off the page\'s elements', () => { const r = rig() r.m.destroy() for (const [el, type] of [[r.menu, 'click'], [r.menu, 'keydown'], [r.button, 'click'], [r.stageEl, 'contextmenu'], [r.stageEl, 'scroll'], [r.frame, 'scroll'], [r.ctx.document, 'keydown'], [r.ctx.document, 'pointerdown']]) { assert.equal(el.countListeners(type), 0, type) } r.ctx.restore() }) // Fix round 2: where no place on the stage is clear of the handles (a // stage smaller than the button), the round button docks in the stage // bar beside the view switch, off the slide; on a stage with room it // is placed on the box again. test('place: a stage with no room docks the round button beside the view switch', () => { const r = rig() try { const rectOf = (left, top, width, height) => () => ({ left, top, width, height, right: left + width, bottom: top + height }) const switcher = stub('fieldset', { 'slides-switcher': '' }) switcher.getBoundingClientRect = rectOf(500, 10, 100, 28) r.root.append(switcher) r.root.getBoundingClientRect = rectOf(0, 0, 1000, 800) r.button.style = {} r.button.offsetWidth = 28 r.stageEl.getBoundingClientRect = rectOf(100, 100, 20, 20) r.box.getBoundingClientRect = rectOf(100, 100, 20, 20) r.m.place() assert.equal(r.button.hidden, false) assert.equal(r.button.hasAttribute('data-docked'), true) assert.deepEqual([r.button.style.left, r.button.style.top], ['608px', '10px']) r.stageEl.getBoundingClientRect = rectOf(100, 100, 600, 340) r.box.getBoundingClientRect = rectOf(200, 200, 200, 100) r.m.place() assert.equal(r.button.hasAttribute('data-docked'), false) assert.deepEqual([r.button.style.left, r.button.style.top], ['372px', '156px'], 'above the top end corner') } finally { r.m.destroy() r.ctx.restore() } }) // Phone view scrolls the phone's screen (the stage's frame), not the // stage (the review's A4): the round button follows that scroll, kept // inside what the screen shows, and is not drawn while its box is // scrolled out of it. test('place: the round button follows the phone screen\'s scroll, and goes while its box is out of sight', () => { const r = rig() try { const rectOf = (left, top, width, height) => () => ({ left, top, width, height, right: left + width, bottom: top + height }) r.root.getBoundingClientRect = rectOf(0, 0, 1000, 800) r.button.style = {} r.button.offsetWidth = 28 r.frame.getBoundingClientRect = rectOf(100, 100, 390, 400) const at = top => { r.stageEl.getBoundingClientRect = rectOf(100, top - 100, 390, 1200) r.box.getBoundingClientRect = rectOf(150, top, 200, 100) } at(200) r.m.place() assert.equal(r.button.hidden, false) assert.deepEqual([r.button.style.left, r.button.style.top], ['322px', '156px'], 'above the top end corner') // Scrolled until the box is above the screen: no button. at(-150) r.frame.dispatchEvent({ type: 'scroll', target: r.frame }) assert.equal(r.button.hidden, true) // Back until the box's top is near the screen's: the button is in // the screen, beside the box rather than above the screen. at(150) r.frame.dispatchEvent({ type: 'scroll', target: r.frame }) assert.equal(r.button.hidden, false) assert.ok(parseFloat(r.button.style.top) >= 100, `the button's top ${r.button.style.top} is inside the screen`) // Outside Phone view the frame is nothing (display: contents, no box). r.frame.getBoundingClientRect = rectOf(0, 0, 0, 0) at(200) r.stageEl.dispatchEvent({ type: 'scroll', target: r.stageEl }) assert.deepEqual([r.button.style.left, r.button.style.top], ['322px', '156px']) } finally { r.m.destroy() r.ctx.restore() } })