// accessory.mjs — the phone's row over the keyboard while writing (spec // §6.11, D6, Página's): [[ (Link to a page), /, B, I, a list, a checklist, an image // (images §4.1), and Make team page on a private note. It shows on a coarse pointer under 800px // while the page's text has focus, and visualViewport keeps it on top // of the keyboard. While the keyboard is up carries // data-mapa-keyboard, which hides the Links sheet's peek (§4.3). import { Plugin } from 'prosemirror-state' import { toggleMark } from 'prosemirror-commands' import { blockCommands, insertText } from './commands.mjs' function element (tag, attrs = {}, text = '') { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) if (text) node.textContent = text return node } // accessoryPlugin keeps the row in step with the editor: each update // (a caret moved, focus came) places it again. export function accessoryPlugin (options) { return new Plugin({ view (view) { const { place, row } = accessory(view, options) return { update: place, destroy: () => row.remove() } } }) } export function accessory (view, { makeTeamPage = null, linkPage = null, image = null } = {}) { const schema = view.state.schema const blocks = blockCommands(schema) const row = element('div', { 'mapa-accessory': '', role: 'toolbar', 'aria-label': 'Writing' }) row.hidden = true const add = (label, text, command) => { const button = element('button', { type: 'button', 'aria-label': label }, text) button.addEventListener('mousedown', event => event.preventDefault()) button.addEventListener('click', () => { command(view.state, view.dispatch, view) // Link to a page over a selection moves focus to the bubble's // field; anything else goes back to the text. if (!document.activeElement?.closest?.('[mapa-bubble]')) view.focus() }) row.append(button) } // [[ is ⌘L's command (build note 81): over a selection it links the // words to a page; with none it types [[. add('Link to a page', '[[', linkPage ?? insertText('[[')) add('Blocks', '/', insertText('/')) add('Bold', 'B', toggleMark(schema.marks.strong)) add('Italic', 'I', toggleMark(schema.marks.em)) add('List', '•', blocks['insert-list']) add('Checklist', '☐', blocks['insert-check']) // The image button opens the file picker (images §4.1). if (image) add('Image', 'Image', image) if (makeTeamPage) add('Make team page', 'Team page', (state, dispatch, view) => makeTeamPage(view)) document.body.append(row) const phone = window.matchMedia('(pointer: coarse) and (max-width: 799px)') const viewport = window.visualViewport const place = () => { const writing = phone.matches && view.hasFocus() row.hidden = !writing const covered = viewport ? window.innerHeight - viewport.height - viewport.offsetTop : 0 document.documentElement.toggleAttribute('data-mapa-keyboard', writing && covered > 80) if (writing) row.style.setProperty('bottom', `${Math.max(covered, 0)}px`) } view.dom.addEventListener('focus', place) view.dom.addEventListener('blur', () => setTimeout(place, 0)) viewport?.addEventListener('resize', place) viewport?.addEventListener('scroll', place) phone.addEventListener?.('change', place) return { place, row } }