Skip to content

Commit

Permalink
feat: export cellNear helper and deleteCellSelection command (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue authored Aug 22, 2024
1 parent ed739ff commit fb7345b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
36 changes: 35 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export { TableView, updateColumnsOnResize } from './tableview';
export {
addColSpan,
cellAround,
cellNear,
colCount,
columnIsHeader,
findCell,
Expand Down
38 changes: 8 additions & 30 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fb7345b

Please sign in to comment.