// writearea.mjs — the page's empty writing area takes a click (build // note 84). The body fills the column to the page's foot (frame.css), so // most clicks land in the editor itself; this places the caret for the // rest, as a word processor would: // - below the last block, at the end of the document. The document // always ends in a paragraph (§6.2), so below a fence, a heading or a // list that is the empty one the editor already keeps: nothing is // written until the person types; // - beside the text column, in the page's margins, at the nearest // position on that line, from posAtCoords with x held inside the // column (above the first line, the first line). // Only a press on the empty area itself is taken: the page around it // (main, the column), or the body below its last block. Anything drawn // on it (the title, links, chips, forms, the panels) is its own target // and keeps doing its own thing. A drag from there selects from where it // started, and Shift extends the selection, as in the text. A touch // scroll fires no mousedown, so only a tap focuses; the focus happens // inside that gesture, which iOS needs to bring up the keyboard. import { Plugin, Selection, TextSelection } from 'prosemirror-state' export function writeAreaPlugin () { return new Plugin({ props: { handleDOMEvents: { mousedown: (view, event) => event.target === view.dom && below(view, event.clientY) && press(view, event) } }, view (view) { const column = view.dom.parentElement const main = view.dom.closest('[mapa-main]') if (!main) return {} const down = event => { if (event.target === main || event.target === column) press(view, event) } main.addEventListener('mousedown', down) return { destroy: () => main.removeEventListener('mousedown', down) } } }) } // press puts the caret where the press says, focuses the editor, and // follows a drag until the button is let go. // Mid-composition (an IME, dictation) the press is the browser's: it // ends the composition, which ProseMirror then reads in (§6.11). function press (view, event) { if (view.composing) return false if (!view.editable || event.button !== 0 || event.ctrlKey || event.metaKey || event.altKey) return false event.preventDefault() const anchor = event.shiftKey ? view.state.selection.anchor : spot(view, event.clientX, event.clientY) select(view, anchor, spot(view, event.clientX, event.clientY)) view.focus() follow(view, anchor, event) return true } function follow (view, anchor, start) { const move = event => { if (!(event.buttons & 1)) return stop() if (Math.hypot(event.clientX - start.clientX, event.clientY - start.clientY) < 4) return select(view, anchor, spot(view, event.clientX, event.clientY)) } const stop = () => { window.removeEventListener('mousemove', move) window.removeEventListener('mouseup', stop) } window.addEventListener('mousemove', move) window.addEventListener('mouseup', stop) } // select selects from anchor to head. The anchor was taken when the // press began; a teammate's block merged in mid-drag may have shortened // the document since, so both are held inside it. export function select (view, anchor, head) { const doc = view.state.doc const inside = pos => clamp(pos, 0, doc.content.size) const selection = TextSelection.between(doc.resolve(inside(anchor)), doc.resolve(inside(head))) if (!selection.eq(view.state.selection)) { view.dispatch(view.state.tr.setSelection(selection)) } } // edges are the top of the first block and the bottom of the last. function edges (view) { const doc = view.state.doc const first = view.nodeDOM(0) const last = view.nodeDOM(doc.content.size - doc.lastChild.nodeSize) const box = view.dom.getBoundingClientRect() return { box, top: first?.getBoundingClientRect?.().top ?? box.top, bottom: last?.getBoundingClientRect?.().bottom ?? box.bottom } } function below (view, y) { return y >= edges(view).bottom } // spot is the document position a press at (x, y) stands for. function spot (view, x, y) { const end = Selection.atEnd(view.state.doc).head const { box, top, bottom } = edges(view) if (y >= bottom) return end const found = view.posAtCoords({ left: clamp(x, box.left + 1, box.right - 1), top: clamp(y, top + 1, bottom - 1) }) return found ? found.pos : end } function clamp (value, low, high) { return Math.min(Math.max(value, low), high) }