Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export cellNear and deleteCellSelection #239

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading