| 1 | // writearea.mjs — the page's empty writing area takes a click (build |
| 2 | // note 84). The body fills the column to the page's foot (frame.css), so |
| 3 | // most clicks land in the editor itself; this places the caret for the |
| 4 | // rest, as a word processor would: |
| 5 | // - below the last block, at the end of the document. The document |
| 6 | // always ends in a paragraph (§6.2), so below a fence, a heading or a |
| 7 | // list that is the empty one the editor already keeps: nothing is |
| 8 | // written until the person types; |
| 9 | // - beside the text column, in the page's margins, at the nearest |
| 10 | // position on that line, from posAtCoords with x held inside the |
| 11 | // column (above the first line, the first line). |
| 12 | // Only a press on the empty area itself is taken: the page around it |
| 13 | // (main, the column), or the body below its last block. Anything drawn |
| 14 | // on it (the title, links, chips, forms, the panels) is its own target |
| 15 | // and keeps doing its own thing. A drag from there selects from where it |
| 16 | // started, and Shift extends the selection, as in the text. A touch |
| 17 | // scroll fires no mousedown, so only a tap focuses; the focus happens |
| 18 | // inside that gesture, which iOS needs to bring up the keyboard. |
| 19 | |
| 20 | import { Plugin, Selection, TextSelection } from 'prosemirror-state' |
| 21 | |
| 22 | export function writeAreaPlugin () { |
| 23 | return new Plugin({ |
| 24 | props: { |
| 25 | handleDOMEvents: { |
| 26 | mousedown: (view, event) => |
| 27 | event.target === view.dom && below(view, event.clientY) && |
| 28 | press(view, event) |
| 29 | } |
| 30 | }, |
| 31 | view (view) { |
| 32 | const column = view.dom.parentElement |
| 33 | const main = view.dom.closest('[mapa-main]') |
| 34 | if (!main) return {} |
| 35 | const down = event => { |
| 36 | if (event.target === main || event.target === column) press(view, event) |
| 37 | } |
| 38 | main.addEventListener('mousedown', down) |
| 39 | return { destroy: () => main.removeEventListener('mousedown', down) } |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | // press puts the caret where the press says, focuses the editor, and |
| 45 | // follows a drag until the button is let go. |
| 46 | // Mid-composition (an IME, dictation) the press is the browser's: it |
| 47 | // ends the composition, which ProseMirror then reads in (§6.11). |
| 48 | function press (view, event) { |
| 49 | if (view.composing) return false |
| 50 | if (!view.editable || event.button !== 0 || event.ctrlKey || |
| 51 | event.metaKey || event.altKey) return false |
| 52 | event.preventDefault() |
| 53 | const anchor = event.shiftKey |
| 54 | ? view.state.selection.anchor |
| 55 | : spot(view, event.clientX, event.clientY) |
| 56 | select(view, anchor, spot(view, event.clientX, event.clientY)) |
| 57 | view.focus() |
| 58 | follow(view, anchor, event) |
| 59 | return true |
| 60 | } |
| 61 | |
| 62 | function follow (view, anchor, start) { |
| 63 | const move = event => { |
| 64 | if (!(event.buttons & 1)) return stop() |
| 65 | if (Math.hypot(event.clientX - start.clientX, |
| 66 | event.clientY - start.clientY) < 4) return |
| 67 | select(view, anchor, spot(view, event.clientX, event.clientY)) |
| 68 | } |
| 69 | const stop = () => { |
| 70 | window.removeEventListener('mousemove', move) |
| 71 | window.removeEventListener('mouseup', stop) |
| 72 | } |
| 73 | window.addEventListener('mousemove', move) |
| 74 | window.addEventListener('mouseup', stop) |
| 75 | } |
| 76 | |
| 77 | // select selects from anchor to head. The anchor was taken when the |
| 78 | // press began; a teammate's block merged in mid-drag may have shortened |
| 79 | // the document since, so both are held inside it. |
| 80 | export function select (view, anchor, head) { |
| 81 | const doc = view.state.doc |
| 82 | const inside = pos => clamp(pos, 0, doc.content.size) |
| 83 | const selection = TextSelection.between(doc.resolve(inside(anchor)), |
| 84 | doc.resolve(inside(head))) |
| 85 | if (!selection.eq(view.state.selection)) { |
| 86 | view.dispatch(view.state.tr.setSelection(selection)) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // edges are the top of the first block and the bottom of the last. |
| 91 | function edges (view) { |
| 92 | const doc = view.state.doc |
| 93 | const first = view.nodeDOM(0) |
| 94 | const last = view.nodeDOM(doc.content.size - doc.lastChild.nodeSize) |
| 95 | const box = view.dom.getBoundingClientRect() |
| 96 | return { |
| 97 | box, |
| 98 | top: first?.getBoundingClientRect?.().top ?? box.top, |
| 99 | bottom: last?.getBoundingClientRect?.().bottom ?? box.bottom |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | function below (view, y) { |
| 104 | return y >= edges(view).bottom |
| 105 | } |
| 106 | |
| 107 | // spot is the document position a press at (x, y) stands for. |
| 108 | function spot (view, x, y) { |
| 109 | const end = Selection.atEnd(view.state.doc).head |
| 110 | const { box, top, bottom } = edges(view) |
| 111 | if (y >= bottom) return end |
| 112 | const found = view.posAtCoords({ |
| 113 | left: clamp(x, box.left + 1, box.right - 1), |
| 114 | top: clamp(y, top + 1, bottom - 1) |
| 115 | }) |
| 116 | return found ? found.pos : end |
| 117 | } |
| 118 | |
| 119 | function clamp (value, low, high) { |
| 120 | return Math.min(Math.max(value, low), high) |
| 121 | } |
| 122 | |