diff --git a/src/commands.ts b/src/commands.ts index f253541f..ed910bab 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,6 +1,12 @@ // This file defines a number of table-related commands. -import { Fragment, Node, NodeType, ResolvedPos } from 'prosemirror-model'; +import { + Fragment, + Node, + NodeType, + ResolvedPos, + Slice, +} from 'prosemirror-model'; import { Command, EditorState, @@ -857,3 +863,31 @@ export function deleteTable( } return false; } + +/** + * Deletes the content of the selected cells, if they are not empty. + * + * @public + */ +export function deleteCellSelection( + state: EditorState, + dispatch?: (tr: Transaction) => void, +): boolean { + const sel = state.selection; + if (!(sel instanceof CellSelection)) return false; + if (dispatch) { + const tr = state.tr; + const baseContent = tableNodeTypes(state.schema).cell.createAndFill()! + .content; + sel.forEachCell((cell, pos) => { + if (!cell.content.eq(baseContent)) + tr.replace( + tr.mapping.map(pos + 1), + tr.mapping.map(pos + cell.nodeSize - 1), + new Slice(baseContent, 0, 0), + ); + }); + if (tr.docChanged) dispatch(tr); + } + return true; +} diff --git a/src/index.ts b/src/index.ts index db21e4cb..367a28d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,6 +47,7 @@ export { TableView, updateColumnsOnResize } from './tableview'; export { addColSpan, cellAround, + cellNear, colCount, columnIsHeader, findCell, diff --git a/src/input.ts b/src/input.ts index 3c23708c..f8d54839 100644 --- a/src/input.ts +++ b/src/input.ts @@ -1,6 +1,7 @@ // This file defines a number of helpers for wiring up user input to // table-related functionality. +import { keydownHandler } from 'prosemirror-keymap'; import { Fragment, ResolvedPos, Slice } from 'prosemirror-model'; import { Command, @@ -9,21 +10,21 @@ import { TextSelection, Transaction, } from 'prosemirror-state'; -import { keydownHandler } from 'prosemirror-keymap'; +import { EditorView } from 'prosemirror-view'; +import { CellSelection } from './cellselection'; +import { deleteCellSelection } from './commands'; +import { clipCells, fitSlice, insertCells, pastedCells } from './copypaste'; +import { tableNodeTypes } from './schema'; +import { TableMap } from './tablemap'; import { cellAround, inSameTable, isInTable, - tableEditingKey, nextCell, selectionCell, + tableEditingKey, } from './util'; -import { CellSelection } from './cellselection'; -import { TableMap } from './tablemap'; -import { clipCells, fitSlice, insertCells, pastedCells } from './copypaste'; -import { tableNodeTypes } from './schema'; -import { EditorView } from 'prosemirror-view'; type Axis = 'horiz' | 'vert'; @@ -118,29 +119,6 @@ function shiftArrow(axis: Axis, dir: Direction): Command { }; } -function deleteCellSelection( - state: EditorState, - dispatch?: (tr: Transaction) => void, -): boolean { - const sel = state.selection; - if (!(sel instanceof CellSelection)) return false; - if (dispatch) { - const tr = state.tr; - const baseContent = tableNodeTypes(state.schema).cell.createAndFill()! - .content; - sel.forEachCell((cell, pos) => { - if (!cell.content.eq(baseContent)) - tr.replace( - tr.mapping.map(pos + 1), - tr.mapping.map(pos + cell.nodeSize - 1), - new Slice(baseContent, 0, 0), - ); - }); - if (tr.docChanged) dispatch(tr); - } - return true; -} - export function handleTripleClick(view: EditorView, pos: number): boolean { const doc = view.state.doc, $cell = cellAround(doc.resolve(pos)); diff --git a/src/util.ts b/src/util.ts index 70fd658f..bb4bfbb5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -78,7 +78,10 @@ export function selectionCell(state: EditorState): ResolvedPos { throw new RangeError(`No cell found around position ${sel.head}`); } -function cellNear($pos: ResolvedPos): ResolvedPos | undefined { +/** + * @public + */ +export function cellNear($pos: ResolvedPos): ResolvedPos | undefined { for ( let after = $pos.nodeAfter, pos = $pos.pos; after;