| 1 | // accessory.mjs — the phone's row over the keyboard while writing (spec |
| 2 | // §6.11, D6, Página's): [[ (Link to a page), /, B, I, a list, a checklist, an image |
| 3 | // (images §4.1), and Make team page on a private note. It shows on a coarse pointer under 800px |
| 4 | // while the page's text has focus, and visualViewport keeps it on top |
| 5 | // of the keyboard. While the keyboard is up <html> carries |
| 6 | // data-mapa-keyboard, which hides the Links sheet's peek (§4.3). |
| 7 | |
| 8 | import { Plugin } from 'prosemirror-state' |
| 9 | import { toggleMark } from 'prosemirror-commands' |
| 10 | import { blockCommands, insertText } from './commands.mjs' |
| 11 | |
| 12 | function element (tag, attrs = {}, text = '') { |
| 13 | const node = document.createElement(tag) |
| 14 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 15 | if (text) node.textContent = text |
| 16 | return node |
| 17 | } |
| 18 | |
| 19 | // accessoryPlugin keeps the row in step with the editor: each update |
| 20 | // (a caret moved, focus came) places it again. |
| 21 | export function accessoryPlugin (options) { |
| 22 | return new Plugin({ |
| 23 | view (view) { |
| 24 | const { place, row } = accessory(view, options) |
| 25 | return { update: place, destroy: () => row.remove() } |
| 26 | } |
| 27 | }) |
| 28 | } |
| 29 | |
| 30 | export function accessory (view, { makeTeamPage = null, linkPage = null, image = null } = {}) { |
| 31 | const schema = view.state.schema |
| 32 | const blocks = blockCommands(schema) |
| 33 | const row = element('div', { 'mapa-accessory': '', role: 'toolbar', 'aria-label': 'Writing' }) |
| 34 | row.hidden = true |
| 35 | const add = (label, text, command) => { |
| 36 | const button = element('button', { type: 'button', 'aria-label': label }, text) |
| 37 | button.addEventListener('mousedown', event => event.preventDefault()) |
| 38 | button.addEventListener('click', () => { |
| 39 | command(view.state, view.dispatch, view) |
| 40 | // Link to a page over a selection moves focus to the bubble's |
| 41 | // field; anything else goes back to the text. |
| 42 | if (!document.activeElement?.closest?.('[mapa-bubble]')) view.focus() |
| 43 | }) |
| 44 | row.append(button) |
| 45 | } |
| 46 | // [[ is ⌘L's command (build note 81): over a selection it links the |
| 47 | // words to a page; with none it types [[. |
| 48 | add('Link to a page', '[[', linkPage ?? insertText('[[')) |
| 49 | add('Blocks', '/', insertText('/')) |
| 50 | add('Bold', 'B', toggleMark(schema.marks.strong)) |
| 51 | add('Italic', 'I', toggleMark(schema.marks.em)) |
| 52 | add('List', '•', blocks['insert-list']) |
| 53 | add('Checklist', '☐', blocks['insert-check']) |
| 54 | // The image button opens the file picker (images §4.1). |
| 55 | if (image) add('Image', 'Image', image) |
| 56 | if (makeTeamPage) add('Make team page', 'Team page', (state, dispatch, view) => makeTeamPage(view)) |
| 57 | document.body.append(row) |
| 58 | |
| 59 | const phone = window.matchMedia('(pointer: coarse) and (max-width: 799px)') |
| 60 | const viewport = window.visualViewport |
| 61 | const place = () => { |
| 62 | const writing = phone.matches && view.hasFocus() |
| 63 | row.hidden = !writing |
| 64 | const covered = viewport ? window.innerHeight - viewport.height - viewport.offsetTop : 0 |
| 65 | document.documentElement.toggleAttribute('data-mapa-keyboard', writing && covered > 80) |
| 66 | if (writing) row.style.setProperty('bottom', `${Math.max(covered, 0)}px`) |
| 67 | } |
| 68 | view.dom.addEventListener('focus', place) |
| 69 | view.dom.addEventListener('blur', () => setTimeout(place, 0)) |
| 70 | viewport?.addEventListener('resize', place) |
| 71 | viewport?.addEventListener('scroll', place) |
| 72 | phone.addEventListener?.('change', place) |
| 73 | return { place, row } |
| 74 | } |
| 75 | |