From 7a32271992abe43f3faa2a65540205d06df8566b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 4 Jun 2024 21:59:22 -0400 Subject: [PATCH] fix: Allow delete table row and column with mod+backspace --- app/editor/index.tsx | 2 +- shared/editor/commands/table.ts | 36 +++++++++++++++++++++++++++++++++ shared/editor/nodes/Table.ts | 6 ++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 5f7bed11d..d8a848c33 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -402,8 +402,8 @@ export class Editor extends React.PureComponent< schema: this.schema, doc, plugins: [ - ...this.plugins, ...this.keymaps, + ...this.plugins, dropCursor({ color: this.props.theme.cursor, }), diff --git a/shared/editor/commands/table.ts b/shared/editor/commands/table.ts index 89869b384..c83592a39 100644 --- a/shared/editor/commands/table.ts +++ b/shared/editor/commands/table.ts @@ -8,6 +8,8 @@ import { tableNodeTypes, toggleHeader, addColumn, + deleteRow, + deleteColumn, } from "prosemirror-tables"; import { chainTransactions } from "../lib/chainTransactions"; import { getCellsInColumn, isHeaderEnabled } from "../queries/table"; @@ -210,6 +212,40 @@ export function addRowBefore({ index }: { index?: number }): Command { }; } +/** + * A command that deletes the current selected row, if any. + * + * @returns The command + */ +export function deleteRowSelection(): Command { + return (state, dispatch) => { + if ( + state.selection instanceof CellSelection && + state.selection.isRowSelection() + ) { + return deleteRow(state, dispatch); + } + return false; + }; +} + +/** + * A command that deletes the current selected column, if any. + * + * @returns The command + */ +export function deleteColSelection(): Command { + return (state, dispatch) => { + if ( + state.selection instanceof CellSelection && + state.selection.isColSelection() + ) { + return deleteColumn(state, dispatch); + } + return false; + }; +} + /** * A command that safely adds a column taking into account any existing heading column on the far * left of the table, and preventing it moving "into" the table. diff --git a/shared/editor/nodes/Table.ts b/shared/editor/nodes/Table.ts index 4d05b6024..af9bdcc3b 100644 --- a/shared/editor/nodes/Table.ts +++ b/shared/editor/nodes/Table.ts @@ -19,6 +19,8 @@ import { createTable, sortTable, setTableAttr, + deleteColSelection, + deleteRowSelection, } from "../commands/table"; import { MarkdownSerializerState } from "../lib/markdown/serializer"; import tablesRule from "../rules/tables"; @@ -86,6 +88,10 @@ export default class Table extends Node { Tab: chainCommands(goToNextCell(1), addRowAndMoveSelection()), "Shift-Tab": goToNextCell(-1), "Mod-Enter": addRowAndMoveSelection(), + "Mod-Backspace": chainCommands( + deleteColSelection(), + deleteRowSelection() + ), }; }