fix: Allow delete table row and column with mod+backspace

This commit is contained in:
Tom Moor
2024-06-04 21:59:22 -04:00
parent dd4c8c5546
commit 7a32271992
3 changed files with 43 additions and 1 deletions

View File

@@ -402,8 +402,8 @@ export class Editor extends React.PureComponent<
schema: this.schema, schema: this.schema,
doc, doc,
plugins: [ plugins: [
...this.plugins,
...this.keymaps, ...this.keymaps,
...this.plugins,
dropCursor({ dropCursor({
color: this.props.theme.cursor, color: this.props.theme.cursor,
}), }),

View File

@@ -8,6 +8,8 @@ import {
tableNodeTypes, tableNodeTypes,
toggleHeader, toggleHeader,
addColumn, addColumn,
deleteRow,
deleteColumn,
} from "prosemirror-tables"; } from "prosemirror-tables";
import { chainTransactions } from "../lib/chainTransactions"; import { chainTransactions } from "../lib/chainTransactions";
import { getCellsInColumn, isHeaderEnabled } from "../queries/table"; 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 * 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. * left of the table, and preventing it moving "into" the table.

View File

@@ -19,6 +19,8 @@ import {
createTable, createTable,
sortTable, sortTable,
setTableAttr, setTableAttr,
deleteColSelection,
deleteRowSelection,
} from "../commands/table"; } from "../commands/table";
import { MarkdownSerializerState } from "../lib/markdown/serializer"; import { MarkdownSerializerState } from "../lib/markdown/serializer";
import tablesRule from "../rules/tables"; import tablesRule from "../rules/tables";
@@ -86,6 +88,10 @@ export default class Table extends Node {
Tab: chainCommands(goToNextCell(1), addRowAndMoveSelection()), Tab: chainCommands(goToNextCell(1), addRowAndMoveSelection()),
"Shift-Tab": goToNextCell(-1), "Shift-Tab": goToNextCell(-1),
"Mod-Enter": addRowAndMoveSelection(), "Mod-Enter": addRowAndMoveSelection(),
"Mod-Backspace": chainCommands(
deleteColSelection(),
deleteRowSelection()
),
}; };
} }