Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: add "Delete table" context menu item

Adorian Doran2fc845657a80
diff --git a/apps/client/src/menus/table_context_menu.spec.ts b/apps/client/src/menus/table_context_menu.spec.ts
index 0b0f2a3eef..869bbeb171 100644
--- a/apps/client/src/menus/table_context_menu.spec.ts
+++ b/apps/client/src/menus/table_context_menu.spec.ts
@@ -22,6 +22,7 @@ const SECTION_TITLES = [
"---",
"table_context_menu.delete_rows",
"table_context_menu.delete_columns",
+ "table_context_menu.delete_table",
"---",
"table_context_menu.distribute_columns",
"---",
@@ -64,6 +65,7 @@ describe("buildTableMenuItems", () => {
"triliumInsertTableColumnsRight",
"removeTableRow",
"removeTableColumn",
+ "triliumDeleteTable",
"triliumDistributeTableColumns",
"mergeTableCells",
"splitTableCellVertically",
diff --git a/apps/client/src/menus/table_context_menu.ts b/apps/client/src/menus/table_context_menu.ts
index 4946595888..16dd7de016 100644
--- a/apps/client/src/menus/table_context_menu.ts
+++ b/apps/client/src/menus/table_context_menu.ts
@@ -7,8 +7,8 @@ import { getTextEditorContaining } from "./text_editor_context_menu.js";
/**
* The table section of the text editor's right-click menu: row and column insertion sized by the
- * selection, deletion of every spanned row or column, equal widths for the spanned columns, and
- * merging or splitting of the selected cells.
+ * selection, deletion of every spanned row or column or of the whole table, equal widths for the
+ * spanned columns, and merging or splitting of the selected cells.
*
* Returns `null` when the click is not on a table cell of the active text editor.
*/
@@ -60,6 +60,7 @@ export function buildTableMenuItems(
{ kind: "separator" },
commandItem(t("table_context_menu.delete_rows"), "bx bx-trash", "removeTableRow"),
commandItem(t("table_context_menu.delete_columns"), "bx bx-trash", "removeTableColumn"),
+ commandItem(t("table_context_menu.delete_table"), "bx bx-trash", "triliumDeleteTable"),
{ kind: "separator" },
commandItem(t("table_context_menu.distribute_columns"), "bx bx-move-horizontal",
"triliumDistributeTableColumns"),
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a3eec6c709..51c5cc5454 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3509,6 +3509,7 @@
"insert_columns_right": "Insert column to the right",
"delete_rows": "Delete rows",
"delete_columns": "Delete columns",
+ "delete_table": "Delete table",
"distribute_columns": "Distribute columns evenly",
"merge_cells": "Merge cells",
"split_cells": "Split cells",
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
index 5e908e99d7..3ea175c819 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
@@ -21,6 +21,9 @@ src="3_Tables_image.png" width="384" height="100">
the same way for columns.</li>
<li><em>Delete rows</em> and <em>Delete columns</em> remove every row or column
the selection touches, even when only some of their cells are selected.</li>
+ <li><em>Delete table</em> removes the whole table and leaves an empty paragraph
+ in its place. In a table nested inside another, only the inner table is
+ removed.</li>
<li><em>Distribute columns evenly</em> splits the combined width of the columns
the selection touches equally between them. The other columns and the width
of the table stay as they are. It needs a selection spanning at least two
diff --git a/docs/User Guide/User Guide/Note Types/Text/Tables.md b/docs/User Guide/User Guide/Note Types/Text/Tables.md
index 417bcb512a..bcf6e970ed 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Tables.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Tables.md
@@ -18,6 +18,7 @@ Since v0.106.0, right-clicking anywhere inside a table opens a context menu with
* _Insert row above_ and _Insert row below_ insert a blank row next to the row of the current cell. When the selection spans multiple rows, the same number of rows is inserted.
* _Insert column to the left_ and _Insert column to the right_ work the same way for columns.
* _Delete rows_ and _Delete columns_ remove every row or column the selection touches, even when only some of their cells are selected.
+* _Delete table_ removes the whole table and leaves an empty paragraph in its place. In a table nested inside another, only the inner table is removed.
* _Distribute columns evenly_ splits the combined width of the columns the selection touches equally between them. The other columns and the width of the table stay as they are. It needs a selection spanning at least two columns; a merged cell counts for every column it covers.
* _Merge cells_ merges the selected cells into one. The selected cells must form a rectangle.
* _Split cells_ splits each selected cell in two, either _Vertically_ or _Horizontally_.
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
index 7a7b6bd256..f8c0eb8690 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts
@@ -96,6 +96,52 @@ describe("TableContextMenu", () => {
});
});
+ describe("delete table", () => {
+ it("is enabled only inside a table", () => {
+ setModelData(editor.model, "<paragraph>fo[]o</paragraph>");
+ expect(editor.commands.get("triliumDeleteTable")?.isEnabled).toBe(false);
+
+ setModelData(editor.model, modelTable([["1[]1"]]));
+ expect(editor.commands.get("triliumDeleteTable")?.isEnabled).toBe(true);
+
+ editor.enableReadOnlyMode("spec");
+ expect(editor.commands.get("triliumDeleteTable")?.isEnabled).toBe(false);
+ });
+
+ it("replaces the table with an empty paragraph holding the caret", () => {
+ setModelData(editor.model,
+ `<paragraph>a</paragraph>${modelTable([["1[]1", "12"]])}<paragraph>b</paragraph>`);
+ editor.execute("triliumDeleteTable");
+ expect(getModelData(editor.model))
+ .toBe("<paragraph>a</paragraph><paragraph>[]</paragraph><paragraph>b</paragraph>");
+
+ setModelData(editor.model, modelTable([["11", "12"], ["21", "22"]]));
+ selectCells(editor, [0, 0], [1, 1]);
+ editor.execute("triliumDeleteTable");
+ expect(getModelData(editor.model)).toBe("<paragraph>[]</paragraph>");
+ });
+
+ it("deletes only the innermost table around the selection", () => {
+ const inner = "<table><tableRow><tableCell><paragraph>in[]ner</paragraph></tableCell>"
+ + "</tableRow></table>";
+ setModelData(editor.model,
+ `<table><tableRow><tableCell>${inner}</tableCell></tableRow></table>`);
+
+ editor.execute("triliumDeleteTable");
+
+ expect(getModelData(editor.model)).toBe(
+ "<table><tableRow><tableCell><paragraph>[]</paragraph></tableCell></tableRow></table>"
+ );
+ });
+
+ it("undoes in one step", () => {
+ setModelData(editor.model, modelTable([["1[]1", "12"]]));
+ editor.execute("triliumDeleteTable");
+ editor.execute("undo");
+ expect(tableData(editor)).toBe(modelTable([["11", "12"]]));
+ });
+ });
+
describe("syncSelectionToDomTarget", () => {
it("moves the selection into an unselected cell", () => {
setModelData(editor.model, modelTable([["1[]1", "12"], ["21", "22"]]));
diff --git a/packages/ckeditor5/src/plugins/table_context_menu.ts b/packages/ckeditor5/src/plugins/table_context_menu.ts
index 31b1e1fda7..b2fadfc452 100644
--- a/packages/ckeditor5/src/plugins/table_context_menu.ts
+++ b/packages/ckeditor5/src/plugins/table_context_menu.ts
@@ -44,6 +44,7 @@ export default class TableContextMenu extends Plugin {
new TableMultiInsertCommand(editor, "right"));
editor.commands.add("triliumDistributeTableColumns",
new DistributeTableColumnsCommand(editor));
+ editor.commands.add("triliumDeleteTable", new DeleteTableCommand(editor));
const view = editor.editing.view;
view.addObserver(MouseObserver);
@@ -146,6 +147,40 @@ export class TableMultiInsertCommand extends Command {
}
+/**
+ * Deletes the table the selection is in, the innermost one when tables are nested. The table is
+ * replaced by an empty paragraph holding the caret, as when a selected table is deleted with the
+ * Delete key.
+ */
+export class DeleteTableCommand extends Command {
+
+ refresh() {
+ this.isEnabled = this.findTable() !== null;
+ }
+
+ execute() {
+ const table = this.findTable();
+ /* v8 ignore next 3 -- defensive: disabled whenever the selection is outside a table */
+ if (!table) {
+ return;
+ }
+
+ const model = this.editor.model;
+ model.change((writer) => {
+ const selection = writer.createSelection(table, "on");
+ model.deleteContent(selection);
+ writer.setSelection(selection);
+ });
+ }
+
+ private findTable(): ModelElement | null {
+ const tableUtils = this.editor.plugins.get(TableUtils);
+ const selection = this.editor.model.document.selection;
+ return tableUtils.getSelectionAffectedTableCells(selection)[0]?.findAncestor("table") ?? null;
+ }
+
+}
+
/** The model `tableCell` whose rendered cell contains `domTarget`, or `null` when there is none. */
function resolveModelCell(editor: Editor, domTarget: Node): ModelElement | null {
const domElement = domTarget instanceof Element ? domTarget : domTarget.parentElement;
@@ -174,5 +209,6 @@ declare module "ckeditor5" {
triliumInsertTableColumnsLeft: TableMultiInsertCommand;
triliumInsertTableColumnsRight: TableMultiInsertCommand;
triliumDistributeTableColumns: DistributeTableColumnsCommand;
+ triliumDeleteTable: DeleteTableCommand;
}
}