From c27feedfd5e2cc20528cc1900572e1fdd15c436d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 3 May 2025 23:56:49 -0400 Subject: [PATCH] fix: Improve behavior when hitting backspace/delete with table cell selections --- shared/editor/commands/table.ts | 59 +++++++++++++++++++++++++++++++-- shared/editor/nodes/Math.ts | 13 +------- shared/editor/nodes/Table.ts | 12 ++++++- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/shared/editor/commands/table.ts b/shared/editor/commands/table.ts index 0222d2d248..0192bdc271 100644 --- a/shared/editor/commands/table.ts +++ b/shared/editor/commands/table.ts @@ -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; +} diff --git a/shared/editor/nodes/Math.ts b/shared/editor/nodes/Math.ts index 83c0911e57..1679c6aa6b 100644 --- a/shared/editor/nodes/Math.ts +++ b/shared/editor/nodes/Math.ts @@ -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, }; } diff --git a/shared/editor/nodes/Table.ts b/shared/editor/nodes/Table.ts index 708c794385..aefbe753ae 100644 --- a/shared/editor/nodes/Table.ts +++ b/shared/editor/nodes/Table.ts @@ -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),