fix: Improve behavior when hitting backspace/delete with table cell selections

This commit is contained in:
Tom Moor committed 2025-05-03 23:56:49 -04:00
1 parent ceac57bd64
commit c27feedfd5
3 files changed
+68 -16

No files matched your search

+56 -3
View File
@@ -1,6 +1,11 @@
import { GapCursor } from "prosemirror-gapcursor";
import { Node, NodeType } from "prosemirror-model";
import { Command, EditorState, TextSelection } from "prosemirror-state";
import { Node, NodeType, Slice } from "prosemirror-model";
import {
Command,
EditorState,
TextSelection,
Transaction,
} from "prosemirror-state";
import {
CellSelection,
addRow,
@@ -11,11 +16,16 @@ import {
addColumn,
deleteRow,
deleteColumn,
deleteTable,
} from "prosemirror-tables";
import { ProsemirrorHelper } from "../../utils/ProsemirrorHelper";
import { CSVHelper } from "../../utils/csv";
import { chainTransactions } from "../lib/chainTransactions";
import { getCellsInColumn, isHeaderEnabled } from "../queries/table";
import {
getCellsInColumn,
isHeaderEnabled,
isTableSelected,
} from "../queries/table";
import { TableLayout } from "../types";
import { collapseSelection } from "./collapseSelection";
@@ -544,3 +554,46 @@ export function moveOutOfTable(direction: 1 | -1): Command {
return false;
};
}
/**
* A command that deletes the entire table if all cells are selected.
*
* @returns The command
*/
export function deleteTableIfSelected(): Command {
return (state, dispatch): boolean => {
if (isTableSelected(state)) {
return deleteTable(state, dispatch);
}
return false;
};
}
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;
}
}
return false;
}
+1 -12
View File
@@ -5,12 +5,6 @@ import {
mathSchemaSpec,
} from "@benrbray/prosemirror-math";
import { PluginSimple } from "markdown-it";
import {
chainCommands,
deleteSelection,
selectNodeBackward,
joinBackward,
} from "prosemirror-commands";
import {
NodeSpec,
NodeType,
@@ -51,12 +45,7 @@ export default class Math extends Node {
keys({ type }: { type: NodeType }) {
return {
"Mod-Space": insertMathCmd(type),
Backspace: chainCommands(
deleteSelection,
mathBackspaceCmd,
joinBackward,
selectNodeBackward
),
Backspace: mathBackspaceCmd,
};
}
+11 -1
View File
@@ -24,8 +24,10 @@ import {
setTableAttr,
deleteColSelection,
deleteRowSelection,
deleteCellSelection,
moveOutOfTable,
createTableInner,
deleteTableIfSelected,
} from "../commands/table";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import { FixTablesPlugin } from "../plugins/FixTables";
@@ -96,8 +98,16 @@ export default class Table extends Node {
"Shift-Tab": goToNextCell(-1),
"Mod-Enter": addRowAndMoveSelection(),
"Mod-Backspace": chainCommands(
deleteCellSelection,
deleteColSelection(),
deleteRowSelection()
deleteRowSelection(),
deleteTableIfSelected()
),
Backspace: chainCommands(
deleteCellSelection,
deleteColSelection(),
deleteRowSelection(),
deleteTableIfSelected()
),
ArrowDown: moveOutOfTable(1),
ArrowUp: moveOutOfTable(-1),