| 1 | // columns.mjs — making, changing and undoing column layouts in the |
| 2 | // editor (columns and icons spec §1.7). A layout is a columns node of two |
| 3 | // to four column nodes; each column holds ordinary blocks, never another |
| 4 | // layout. Every command here is one ordinary transaction, so ⌘Z undoes |
| 5 | // it and autosave writes it as an edit. |
| 6 | |
| 7 | import { TextSelection, Plugin } from 'prosemirror-state' |
| 8 | import { Fragment } from 'prosemirror-model' |
| 9 | |
| 10 | // layoutAround is the layout and column the selection's head is in: |
| 11 | // { layout, layoutPos, column, columnPos, index }, or null. |
| 12 | export function layoutAround ($pos) { |
| 13 | for (let depth = $pos.depth; depth > 0; depth--) { |
| 14 | if ($pos.node(depth).type.name === 'column') { |
| 15 | return { |
| 16 | layout: $pos.node(depth - 1), |
| 17 | layoutPos: $pos.before(depth - 1), |
| 18 | column: $pos.node(depth), |
| 19 | columnPos: $pos.before(depth), |
| 20 | index: $pos.index(depth - 1) |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | return null |
| 25 | } |
| 26 | |
| 27 | function emptyColumn (schema) { |
| 28 | return schema.nodes.column.create(null, schema.nodes.paragraph.create()) |
| 29 | } |
| 30 | |
| 31 | // topRange is the run of top-level blocks the selection touches. |
| 32 | function topRange (state) { |
| 33 | const { $from, $to } = state.selection |
| 34 | if ($from.depth < 1) return null |
| 35 | return { from: $from.before(1), to: $to.after(1) } |
| 36 | } |
| 37 | |
| 38 | // makeColumns turns the blocks the selection touches into column 1 of a |
| 39 | // layout of count columns, the rest empty, with the caret in column 2 |
| 40 | // when column 1 took text, or in column 1 when the block was empty. |
| 41 | // Not inside a layout: layouts do not nest. |
| 42 | export function makeColumns (count) { |
| 43 | return (state, dispatch) => { |
| 44 | const schema = state.schema |
| 45 | if (layoutAround(state.selection.$from) || layoutAround(state.selection.$to)) return false |
| 46 | const range = topRange(state) |
| 47 | if (!range) return false |
| 48 | if (dispatch) { |
| 49 | // A layout the selection holds gives up its columns' blocks: |
| 50 | // layouts never nest (Fable on the build, H1). |
| 51 | const moved = withoutLayouts(state.doc.slice(range.from, range.to).content) |
| 52 | const filled = moved.textBetween(0, moved.size, '', '').trim() !== '' |
| 53 | const first = schema.nodes.column.createChecked(null, |
| 54 | moved.size ? moved : schema.nodes.paragraph.create()) |
| 55 | const columns = [first] |
| 56 | for (let at = 1; at < count; at++) columns.push(emptyColumn(schema)) |
| 57 | const layout = schema.nodes.columns.createChecked(null, columns) |
| 58 | const tr = state.tr.replaceWith(range.from, range.to, layout) |
| 59 | const target = filled ? range.from + 1 + first.nodeSize + 1 : range.from + 2 |
| 60 | tr.setSelection(TextSelection.near(tr.doc.resolve(target))) |
| 61 | dispatch(tr.scrollIntoView()) |
| 62 | } |
| 63 | return true |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // addColumn adds an empty column after the caret's, up to four. |
| 68 | export function addColumn (state, dispatch) { |
| 69 | const around = layoutAround(state.selection.$head) |
| 70 | if (!around || around.layout.childCount >= 4) return false |
| 71 | if (dispatch) { |
| 72 | const at = around.columnPos + around.column.nodeSize |
| 73 | const tr = state.tr.insert(at, emptyColumn(state.schema)) |
| 74 | tr.setSelection(TextSelection.near(tr.doc.resolve(at + 2))) |
| 75 | dispatch(tr.scrollIntoView()) |
| 76 | } |
| 77 | return true |
| 78 | } |
| 79 | |
| 80 | // removeColumn moves the caret's column's blocks to the end of the |
| 81 | // column before it (or the start of the next, for the first), and takes |
| 82 | // the column out. It never deletes text. From two columns it undoes the |
| 83 | // layout instead. |
| 84 | export function removeColumn (state, dispatch) { |
| 85 | const around = layoutAround(state.selection.$head) |
| 86 | if (!around) return false |
| 87 | if (around.layout.childCount <= 2) return undoColumns(state, dispatch) |
| 88 | if (dispatch) { |
| 89 | const kept = written(around.column.content) |
| 90 | const tr = state.tr.delete(around.columnPos, around.columnPos + around.column.nodeSize) |
| 91 | const neighbour = around.index > 0 ? around.index - 1 : 0 |
| 92 | let pos = around.layoutPos + 1 |
| 93 | for (let at = 0; at < neighbour; at++) pos += tr.doc.nodeAt(around.layoutPos).child(at).nodeSize |
| 94 | const column = tr.doc.nodeAt(pos) |
| 95 | const into = around.index > 0 ? pos + column.nodeSize - 1 : pos + 1 |
| 96 | if (kept.size) tr.insert(into, kept) |
| 97 | tr.setSelection(TextSelection.near(tr.doc.resolve(into))) |
| 98 | dispatch(tr.scrollIntoView()) |
| 99 | } |
| 100 | return true |
| 101 | } |
| 102 | |
| 103 | // undoColumns puts every column's blocks at the top level, in reading |
| 104 | // order, in the layout's place. |
| 105 | export function undoColumns (state, dispatch) { |
| 106 | const around = layoutAround(state.selection.$head) |
| 107 | if (!around) return false |
| 108 | if (dispatch) { |
| 109 | const blocks = [] |
| 110 | around.layout.forEach(column => column.forEach(child => blocks.push(child))) |
| 111 | const content = written(Fragment.from(blocks)) |
| 112 | const tr = state.tr.replaceWith(around.layoutPos, |
| 113 | around.layoutPos + around.layout.nodeSize, |
| 114 | content.size ? content : state.schema.nodes.paragraph.create()) |
| 115 | tr.setSelection(TextSelection.near(tr.doc.resolve(around.layoutPos + 1))) |
| 116 | dispatch(tr.scrollIntoView()) |
| 117 | } |
| 118 | return true |
| 119 | } |
| 120 | |
| 121 | // written is content less the empty paragraphs, which write nothing. |
| 122 | function written (content) { |
| 123 | const kept = [] |
| 124 | content.forEach(node => { |
| 125 | if (!(node.type.name === 'paragraph' && node.content.size === 0)) kept.push(node) |
| 126 | }) |
| 127 | return Fragment.from(kept) |
| 128 | } |
| 129 | |
| 130 | // emptyColumnBackspace is Backspace at the start of an empty column that |
| 131 | // holds only its empty paragraph: the column goes, by removeColumn's rule. |
| 132 | export function emptyColumnBackspace (state, dispatch) { |
| 133 | const { $head, empty } = state.selection |
| 134 | const around = layoutAround($head) |
| 135 | if (!empty || !around) return false |
| 136 | const only = around.column.childCount === 1 && |
| 137 | around.column.firstChild.type.name === 'paragraph' && |
| 138 | around.column.firstChild.content.size === 0 |
| 139 | if (!only) return false |
| 140 | return removeColumn(state, dispatch) |
| 141 | } |
| 142 | |
| 143 | // toColumn moves the caret to the start of the next (step 1) or the |
| 144 | // previous (step -1) column: ⌥⌘→ and ⌥⌘←. |
| 145 | export function toColumn (step) { |
| 146 | return (state, dispatch) => { |
| 147 | const around = layoutAround(state.selection.$head) |
| 148 | if (!around) return false |
| 149 | const target = around.index + step |
| 150 | if (target < 0 || target >= around.layout.childCount) return false |
| 151 | if (dispatch) { |
| 152 | let pos = around.layoutPos + 1 |
| 153 | for (let at = 0; at < target; at++) pos += around.layout.child(at).nodeSize |
| 154 | dispatch(state.tr.setSelection(TextSelection.near(state.doc.resolve(pos + 1))).scrollIntoView()) |
| 155 | } |
| 156 | return true |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // columnCommands are the Insert menu's and the / list's, by name. |
| 161 | export function columnCommands () { |
| 162 | return { |
| 163 | 'insert-columns-2': makeColumns(2), |
| 164 | 'insert-columns-3': makeColumns(3), |
| 165 | 'insert-columns-4': makeColumns(4), |
| 166 | 'add-column': addColumn, |
| 167 | 'remove-column': removeColumn, |
| 168 | 'undo-columns': undoColumns |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // withoutLayouts is content with each column layout replaced by its |
| 173 | // columns' blocks, in reading order: what a layout's place may hold. |
| 174 | export function withoutLayouts (content) { |
| 175 | const out = [] |
| 176 | content.forEach(node => { |
| 177 | if (node.type.name === 'columns') node.forEach(column => column.forEach(child => out.push(child))) |
| 178 | else out.push(node) |
| 179 | }) |
| 180 | return Fragment.from(out) |
| 181 | } |
| 182 | |
| 183 | // inColumn is whether a position is inside a column. |
| 184 | export function inColumn ($pos) { return layoutAround($pos) !== null } |
| 185 | |
| 186 | // crossColumns is a selection with its ends in different columns, or one |
| 187 | // end in a layout and the other outside it. |
| 188 | export function crossColumns (state) { |
| 189 | const { $from, $to, empty } = state.selection |
| 190 | if (empty) return false |
| 191 | const one = layoutAround($from) |
| 192 | const other = layoutAround($to) |
| 193 | if (!one && !other) return false |
| 194 | return !one || !other || one.columnPos !== other.columnPos |
| 195 | } |
| 196 | |
| 197 | // deleteInColumns deletes a selection that crosses columns without ever |
| 198 | // deleting a column or moving text between columns (§1.7): each column |
| 199 | // loses its selected part, a column left empty keeps an empty paragraph, |
| 200 | // and blocks outside a layout go as usual. |
| 201 | export function deleteInColumns (state, dispatch) { |
| 202 | if (!crossColumns(state)) return false |
| 203 | if (dispatch) { |
| 204 | const { from, to } = state.selection |
| 205 | const cuts = [] |
| 206 | state.doc.nodesBetween(from, to, (node, pos, parent) => { |
| 207 | if (node.type.name === 'columns') return true |
| 208 | if (node.type.name === 'column') { |
| 209 | const start = Math.max(from, pos + 1) |
| 210 | const end = Math.min(to, pos + node.nodeSize - 1) |
| 211 | if (end > start) cuts.push({ from: start, to: end, column: pos }) |
| 212 | return false |
| 213 | } |
| 214 | if (parent === state.doc) { |
| 215 | const start = Math.max(from, pos) |
| 216 | const end = Math.min(to, pos + node.nodeSize) |
| 217 | if (end > start) cuts.push({ from: start, to: end }) |
| 218 | } |
| 219 | return false |
| 220 | }) |
| 221 | const tr = state.tr |
| 222 | for (const cut of cuts.reverse()) { |
| 223 | tr.delete(tr.mapping.map(cut.from), tr.mapping.map(cut.to)) |
| 224 | if (cut.column !== undefined) { |
| 225 | const at = tr.mapping.map(cut.column) |
| 226 | const column = tr.doc.nodeAt(at) |
| 227 | if (column && column.content.size === 0) { |
| 228 | tr.insert(at + 1, state.schema.nodes.paragraph.create()) |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | tr.setSelection(TextSelection.near(tr.doc.resolve(tr.mapping.map(from)))) |
| 233 | dispatch(tr.scrollIntoView()) |
| 234 | } |
| 235 | return true |
| 236 | } |
| 237 | |
| 238 | // layoutEdge is Backspace at the start of the block after a layout, or |
| 239 | // Delete at the end of the block before one: the caret goes into the |
| 240 | // layout's nearest column, and nothing joins (Fable on the build, M4). |
| 241 | export function layoutEdge (backward) { |
| 242 | return (state, dispatch) => { |
| 243 | const { $cursor } = state.selection |
| 244 | if (!$cursor || $cursor.depth < 1) return false |
| 245 | const atEdge = backward ? $cursor.parentOffset === 0 |
| 246 | : $cursor.parentOffset === $cursor.parent.content.size |
| 247 | if (!atEdge) return false |
| 248 | const depth = $cursor.depth |
| 249 | const index = $cursor.index(depth - 1) |
| 250 | const parent = $cursor.node(depth - 1) |
| 251 | const neighbour = parent.maybeChild(backward ? index - 1 : index + 1) |
| 252 | if (!neighbour || neighbour.type.name !== 'columns') return false |
| 253 | if (dispatch) { |
| 254 | const at = backward ? $cursor.before(depth) - 1 : $cursor.after(depth) + 1 |
| 255 | const target = backward ? TextSelection.near(state.doc.resolve(at), -1) |
| 256 | : TextSelection.near(state.doc.resolve(at), 1) |
| 257 | dispatch(state.tr.setSelection(target).scrollIntoView()) |
| 258 | } |
| 259 | return true |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // columnsPlugin keeps typing and pasting over a selection that crosses |
| 264 | // columns from deleting a column: the selection is deleted column by |
| 265 | // column first (§1.7). |
| 266 | export function columnsPlugin () { |
| 267 | return new Plugin({ |
| 268 | props: { |
| 269 | handleTextInput (view, from, to, text) { |
| 270 | if (!crossColumns(view.state)) return false |
| 271 | deleteInColumns(view.state, view.dispatch) |
| 272 | view.dispatch(view.state.tr.insertText(text).scrollIntoView()) |
| 273 | return true |
| 274 | }, |
| 275 | handlePaste (view) { |
| 276 | if (crossColumns(view.state)) deleteInColumns(view.state, view.dispatch) |
| 277 | return false |
| 278 | } |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | |