// columns.mjs — making, changing and undoing column layouts in the // editor (columns and icons spec §1.7). A layout is a columns node of two // to four column nodes; each column holds ordinary blocks, never another // layout. Every command here is one ordinary transaction, so ⌘Z undoes // it and autosave writes it as an edit. import { TextSelection, Plugin } from 'prosemirror-state' import { Fragment } from 'prosemirror-model' // layoutAround is the layout and column the selection's head is in: // { layout, layoutPos, column, columnPos, index }, or null. export function layoutAround ($pos) { for (let depth = $pos.depth; depth > 0; depth--) { if ($pos.node(depth).type.name === 'column') { return { layout: $pos.node(depth - 1), layoutPos: $pos.before(depth - 1), column: $pos.node(depth), columnPos: $pos.before(depth), index: $pos.index(depth - 1) } } } return null } function emptyColumn (schema) { return schema.nodes.column.create(null, schema.nodes.paragraph.create()) } // topRange is the run of top-level blocks the selection touches. function topRange (state) { const { $from, $to } = state.selection if ($from.depth < 1) return null return { from: $from.before(1), to: $to.after(1) } } // makeColumns turns the blocks the selection touches into column 1 of a // layout of count columns, the rest empty, with the caret in column 2 // when column 1 took text, or in column 1 when the block was empty. // Not inside a layout: layouts do not nest. export function makeColumns (count) { return (state, dispatch) => { const schema = state.schema if (layoutAround(state.selection.$from) || layoutAround(state.selection.$to)) return false const range = topRange(state) if (!range) return false if (dispatch) { // A layout the selection holds gives up its columns' blocks: // layouts never nest (Fable on the build, H1). const moved = withoutLayouts(state.doc.slice(range.from, range.to).content) const filled = moved.textBetween(0, moved.size, '', '').trim() !== '' const first = schema.nodes.column.createChecked(null, moved.size ? moved : schema.nodes.paragraph.create()) const columns = [first] for (let at = 1; at < count; at++) columns.push(emptyColumn(schema)) const layout = schema.nodes.columns.createChecked(null, columns) const tr = state.tr.replaceWith(range.from, range.to, layout) const target = filled ? range.from + 1 + first.nodeSize + 1 : range.from + 2 tr.setSelection(TextSelection.near(tr.doc.resolve(target))) dispatch(tr.scrollIntoView()) } return true } } // addColumn adds an empty column after the caret's, up to four. export function addColumn (state, dispatch) { const around = layoutAround(state.selection.$head) if (!around || around.layout.childCount >= 4) return false if (dispatch) { const at = around.columnPos + around.column.nodeSize const tr = state.tr.insert(at, emptyColumn(state.schema)) tr.setSelection(TextSelection.near(tr.doc.resolve(at + 2))) dispatch(tr.scrollIntoView()) } return true } // removeColumn moves the caret's column's blocks to the end of the // column before it (or the start of the next, for the first), and takes // the column out. It never deletes text. From two columns it undoes the // layout instead. export function removeColumn (state, dispatch) { const around = layoutAround(state.selection.$head) if (!around) return false if (around.layout.childCount <= 2) return undoColumns(state, dispatch) if (dispatch) { const kept = written(around.column.content) const tr = state.tr.delete(around.columnPos, around.columnPos + around.column.nodeSize) const neighbour = around.index > 0 ? around.index - 1 : 0 let pos = around.layoutPos + 1 for (let at = 0; at < neighbour; at++) pos += tr.doc.nodeAt(around.layoutPos).child(at).nodeSize const column = tr.doc.nodeAt(pos) const into = around.index > 0 ? pos + column.nodeSize - 1 : pos + 1 if (kept.size) tr.insert(into, kept) tr.setSelection(TextSelection.near(tr.doc.resolve(into))) dispatch(tr.scrollIntoView()) } return true } // undoColumns puts every column's blocks at the top level, in reading // order, in the layout's place. export function undoColumns (state, dispatch) { const around = layoutAround(state.selection.$head) if (!around) return false if (dispatch) { const blocks = [] around.layout.forEach(column => column.forEach(child => blocks.push(child))) const content = written(Fragment.from(blocks)) const tr = state.tr.replaceWith(around.layoutPos, around.layoutPos + around.layout.nodeSize, content.size ? content : state.schema.nodes.paragraph.create()) tr.setSelection(TextSelection.near(tr.doc.resolve(around.layoutPos + 1))) dispatch(tr.scrollIntoView()) } return true } // written is content less the empty paragraphs, which write nothing. function written (content) { const kept = [] content.forEach(node => { if (!(node.type.name === 'paragraph' && node.content.size === 0)) kept.push(node) }) return Fragment.from(kept) } // emptyColumnBackspace is Backspace at the start of an empty column that // holds only its empty paragraph: the column goes, by removeColumn's rule. export function emptyColumnBackspace (state, dispatch) { const { $head, empty } = state.selection const around = layoutAround($head) if (!empty || !around) return false const only = around.column.childCount === 1 && around.column.firstChild.type.name === 'paragraph' && around.column.firstChild.content.size === 0 if (!only) return false return removeColumn(state, dispatch) } // toColumn moves the caret to the start of the next (step 1) or the // previous (step -1) column: ⌥⌘→ and ⌥⌘←. export function toColumn (step) { return (state, dispatch) => { const around = layoutAround(state.selection.$head) if (!around) return false const target = around.index + step if (target < 0 || target >= around.layout.childCount) return false if (dispatch) { let pos = around.layoutPos + 1 for (let at = 0; at < target; at++) pos += around.layout.child(at).nodeSize dispatch(state.tr.setSelection(TextSelection.near(state.doc.resolve(pos + 1))).scrollIntoView()) } return true } } // columnCommands are the Insert menu's and the / list's, by name. export function columnCommands () { return { 'insert-columns-2': makeColumns(2), 'insert-columns-3': makeColumns(3), 'insert-columns-4': makeColumns(4), 'add-column': addColumn, 'remove-column': removeColumn, 'undo-columns': undoColumns } } // withoutLayouts is content with each column layout replaced by its // columns' blocks, in reading order: what a layout's place may hold. export function withoutLayouts (content) { const out = [] content.forEach(node => { if (node.type.name === 'columns') node.forEach(column => column.forEach(child => out.push(child))) else out.push(node) }) return Fragment.from(out) } // inColumn is whether a position is inside a column. export function inColumn ($pos) { return layoutAround($pos) !== null } // crossColumns is a selection with its ends in different columns, or one // end in a layout and the other outside it. export function crossColumns (state) { const { $from, $to, empty } = state.selection if (empty) return false const one = layoutAround($from) const other = layoutAround($to) if (!one && !other) return false return !one || !other || one.columnPos !== other.columnPos } // deleteInColumns deletes a selection that crosses columns without ever // deleting a column or moving text between columns (§1.7): each column // loses its selected part, a column left empty keeps an empty paragraph, // and blocks outside a layout go as usual. export function deleteInColumns (state, dispatch) { if (!crossColumns(state)) return false if (dispatch) { const { from, to } = state.selection const cuts = [] state.doc.nodesBetween(from, to, (node, pos, parent) => { if (node.type.name === 'columns') return true if (node.type.name === 'column') { const start = Math.max(from, pos + 1) const end = Math.min(to, pos + node.nodeSize - 1) if (end > start) cuts.push({ from: start, to: end, column: pos }) return false } if (parent === state.doc) { const start = Math.max(from, pos) const end = Math.min(to, pos + node.nodeSize) if (end > start) cuts.push({ from: start, to: end }) } return false }) const tr = state.tr for (const cut of cuts.reverse()) { tr.delete(tr.mapping.map(cut.from), tr.mapping.map(cut.to)) if (cut.column !== undefined) { const at = tr.mapping.map(cut.column) const column = tr.doc.nodeAt(at) if (column && column.content.size === 0) { tr.insert(at + 1, state.schema.nodes.paragraph.create()) } } } tr.setSelection(TextSelection.near(tr.doc.resolve(tr.mapping.map(from)))) dispatch(tr.scrollIntoView()) } return true } // layoutEdge is Backspace at the start of the block after a layout, or // Delete at the end of the block before one: the caret goes into the // layout's nearest column, and nothing joins (Fable on the build, M4). export function layoutEdge (backward) { return (state, dispatch) => { const { $cursor } = state.selection if (!$cursor || $cursor.depth < 1) return false const atEdge = backward ? $cursor.parentOffset === 0 : $cursor.parentOffset === $cursor.parent.content.size if (!atEdge) return false const depth = $cursor.depth const index = $cursor.index(depth - 1) const parent = $cursor.node(depth - 1) const neighbour = parent.maybeChild(backward ? index - 1 : index + 1) if (!neighbour || neighbour.type.name !== 'columns') return false if (dispatch) { const at = backward ? $cursor.before(depth) - 1 : $cursor.after(depth) + 1 const target = backward ? TextSelection.near(state.doc.resolve(at), -1) : TextSelection.near(state.doc.resolve(at), 1) dispatch(state.tr.setSelection(target).scrollIntoView()) } return true } } // columnsPlugin keeps typing and pasting over a selection that crosses // columns from deleting a column: the selection is deleted column by // column first (§1.7). export function columnsPlugin () { return new Plugin({ props: { handleTextInput (view, from, to, text) { if (!crossColumns(view.state)) return false deleteInColumns(view.state, view.dispatch) view.dispatch(view.state.tr.insertText(text).scrollIntoView()) return true }, handlePaste (view) { if (crossColumns(view.state)) deleteInColumns(view.state, view.dispatch) return false } } }) }