Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/tables: add a command to reset the merged cells
Adorian Doran2f283306c01b
diff --git a/apps/client/src/menus/table_context_menu.spec.ts b/apps/client/src/menus/table_context_menu.spec.tsindex 869bbeb171..843b52ee6b 100644--- a/apps/client/src/menus/table_context_menu.spec.ts+++ b/apps/client/src/menus/table_context_menu.spec.ts@@ -43,7 +43,8 @@ describe("buildTableMenuItems", () => {const submenu = findItem(items, "table_context_menu.split_cells")?.items ?? [];expect(titles(submenu)).toEqual(["table_context_menu.split_vertically",- "table_context_menu.split_horizontally"+ "table_context_menu.split_horizontally",+ "table_context_menu.split_reset"]);});@@ -57,6 +58,7 @@ describe("buildTableMenuItems", () => {}run(submenu, "table_context_menu.split_vertically");run(submenu, "table_context_menu.split_horizontally");+ run(submenu, "table_context_menu.split_reset");expect(executed).toEqual(["triliumInsertTableRowsAbove",@@ -69,7 +71,8 @@ describe("buildTableMenuItems", () => {"triliumDistributeTableColumns","mergeTableCells","splitTableCellVertically",- "splitTableCellHorizontally"+ "splitTableCellHorizontally",+ "triliumResetTableCellSpans"]);expect(focus).toHaveBeenCalledTimes(executed.length);});@@ -91,16 +94,23 @@ describe("buildTableMenuItems", () => {expect(findItem(items, "table_context_menu.insert_rows_above")?.enabled).toBe(true);});- it("enables the split submenu while either split direction is enabled", () => {+ it("enables the split submenu while any of its rows is enabled", () => {+ const SPLIT_COMMANDS = [+ "splitTableCellVertically",+ "splitTableCellHorizontally",+ "triliumResetTableCellSpans"+ ];const splitRow = (disabled: string[]) => {const { editor } = stubEditor(disabled);const items = buildTableMenuItems(editor, cellElement()) ?? [];return findItem(items, "table_context_menu.split_cells")?.enabled;};- expect(splitRow(["splitTableCellVertically"])).toBe(true);- expect(splitRow(["splitTableCellHorizontally"])).toBe(true);- expect(splitRow(["splitTableCellVertically", "splitTableCellHorizontally"])).toBe(false);+ for (const enabled of SPLIT_COMMANDS) {+ const disabled = SPLIT_COMMANDS.filter((command) => command !== enabled);+ expect(splitRow(disabled), enabled).toBe(true);+ }+ expect(splitRow(SPLIT_COMMANDS)).toBe(false);});it("returns null without the plugin or a table cell at the target", () => {diff --git a/apps/client/src/menus/table_context_menu.ts b/apps/client/src/menus/table_context_menu.tsindex 16dd7de016..623eceb1ed 100644--- a/apps/client/src/menus/table_context_menu.ts+++ b/apps/client/src/menus/table_context_menu.ts@@ -47,6 +47,15 @@ export function buildTableMenuItems(}});+ const splitItems = [+ commandItem(t("table_context_menu.split_vertically"), "bx bx-empty",+ "splitTableCellVertically"),+ commandItem(t("table_context_menu.split_horizontally"), "bx bx-empty",+ "splitTableCellHorizontally"),+ commandItem(t("table_context_menu.split_reset"), "bx bx-reset",+ "triliumResetTableCellSpans")+ ];+return [commandItem(t("table_context_menu.insert_rows_above"),"bx bx-horizontal-left bx-rotate-90", "triliumInsertTableRowsAbove"),@@ -69,14 +78,8 @@ export function buildTableMenuItems({title: t("table_context_menu.split_cells"),uiIcon: "bx bx-border-inner",- enabled: editor.commands.get("splitTableCellVertically")?.isEnabled === true- || editor.commands.get("splitTableCellHorizontally")?.isEnabled === true,- items: [- commandItem(t("table_context_menu.split_vertically"), "bx bx-empty",- "splitTableCellVertically"),- commandItem(t("table_context_menu.split_horizontally"), "bx bx-empty",- "splitTableCellHorizontally")- ]+ enabled: splitItems.some((item) => item.enabled),+ items: splitItems}];}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 51c5cc5454..6baed9fba4 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3514,7 +3514,8 @@"merge_cells": "Merge cells","split_cells": "Split cells","split_vertically": "Vertically",- "split_horizontally": "Horizontally"+ "split_horizontally": "Horizontally",+ "split_reset": "Reset"},"board_view": {"delete-note": "Delete note...",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.htmlindex 3ea175c819..f9e4e95287 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@@ -30,7 +30,10 @@ src="3_Tables_image.png" width="384" height="100">columns; a merged cell counts for every column it covers.</li><li><em>Merge cells</em> merges the selected cells into one. The selected cellsmust form a rectangle.</li>- <li><em>Split cells</em> splits each selected cell in two, either <em>Vertically</em> or <em>Horizontally</em>.</li>+ <li><em>Split cells</em> splits each selected cell in two, either <em>Vertically</em> or <em>Horizontally</em>. <em>Reset</em> turns+ each merged cell in the selection back into the single cells it covers,+ keeping its content in the top-left one; cells that are not merged stay+ as they are.</li><li><em>Cut</em>, <em>Copy</em> and <em>Copy as Markdown</em>, below the tablesection, act on the selected cells:<ul>@@ -135,7 +138,8 @@ src="3_Tables_image.png" width="384" height="100">a cell into multiple cells (can also be used to undo a merge).</li></ul><p>Merging and splitting are also available by right-clicking the selected- cells, via the <a href="#context-menu">context menu</a>.</p>+ cells, via the <a href="#context-menu">context menu</a>. To undo merges+ entirely, use <em>Split cells</em> → <em>Reset</em> from the same menu.</p><h2>Table properties</h2><figure class="image image-style-align-right"><img style="aspect-ratio:312/311;" src="Tables_image.png" width="312"diff --git a/docs/User Guide/User Guide/Note Types/Text/Tables.md b/docs/User Guide/User Guide/Note Types/Text/Tables.mdindex bcf6e970ed..3f6ba9fa6c 100644--- a/docs/User Guide/User Guide/Note Types/Text/Tables.md+++ b/docs/User Guide/User Guide/Note Types/Text/Tables.md@@ -21,7 +21,7 @@ Since v0.106.0, right-clicking anywhere inside a table opens a context menu with* _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_.+* _Split cells_ splits each selected cell in two, either _Vertically_ or _Horizontally_. _Reset_ turns each merged cell in the selection back into the single cells it covers, keeping its content in the top-left one; cells that are not merged stay as they are.* _Cut_, _Copy_ and _Copy as Markdown_, below the table section, act on the selected cells:* Cutting or copying produces a smaller table holding just the selected cells. Pasting it over a cell selection replaces those cells; pasting elsewhere inserts it as a table of its own. Cutting clears the cells without removing rows or columns.* _Copy as Markdown_ converts the selected cells to a Markdown table. A selected header row becomes the Markdown header; without one, an empty header row is emitted, since Markdown tables require one.@@ -79,7 +79,7 @@ More options are available by pressing the arrow next to it:* Click on a single cell and select Merge cell up/down/right/left to merge with an adjacent cell.* Select _Split cell vertically_ or _horizontally_, to split a cell into multiple cells (can also be used to undo a merge).-Merging and splitting are also available by right-clicking the selected cells, via the [context menu](#context-menu).+Merging and splitting are also available by right-clicking the selected cells, via the [context menu](#context-menu). To undo merges entirely, use _Split cells_ → _Reset_ from the same menu.## Table propertiesdiff --git a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts b/packages/ckeditor5/src/plugins/table_context_menu.spec.tsindex f8c0eb8690..8bb3e15b1e 100644--- a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts+++ b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts@@ -3,6 +3,7 @@ import type { ClassicEditor, ModelElement } from "ckeditor5";import { beforeEach, describe, expect, it } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";+import { modelTable as spannedTable } from "../../test/table-kit.js";import TableContextMenu from "./table_context_menu.js";const INSERT_COMMANDS = [@@ -142,6 +143,77 @@ describe("TableContextMenu", () => {});});+ describe("reset cell spans", () => {+ const COMMAND = "triliumResetTableCellSpans";++ it("is enabled only when the selection holds a merged cell", () => {+ setModelData(editor.model, "<paragraph>fo[]o</paragraph>");+ expect(editor.commands.get(COMMAND)?.isEnabled).toBe(false);++ setModelData(editor.model,+ spannedTable([[{ contents: "a", colspan: 2 }], ["b", "c[]"]]));+ expect(editor.commands.get(COMMAND)?.isEnabled).toBe(false);++ selectCells(editor, [0, 0], [1, 1]);+ expect(editor.commands.get(COMMAND)?.isEnabled).toBe(true);++ setModelData(editor.model,+ spannedTable([[{ contents: "a[]", rowspan: 2 }, "b"], ["c"]]));+ expect(editor.commands.get(COMMAND)?.isEnabled).toBe(true);+ });++ it("splits a merged cell back into single cells, keeping its content in the first", () => {+ setModelData(editor.model,+ spannedTable([[{ contents: "a[]", colspan: 2 }], ["b", "c"]]));+ editor.execute(COMMAND);+ expect(tableData(editor)).toBe(modelTable([["a", ""], ["b", "c"]]));++ setModelData(editor.model,+ spannedTable([[{ contents: "a[]", rowspan: 2 }, "b"], ["c"]]));+ editor.execute(COMMAND);+ expect(tableData(editor)).toBe(modelTable([["a", "b"], ["", "c"]]));++ setModelData(editor.model, spannedTable([+ [{ contents: "a[]", colspan: 2, rowspan: 2 }, "b"],+ ["c"],+ ["d", "e", "f"]+ ]));+ editor.execute(COMMAND);+ expect(tableData(editor))+ .toBe(modelTable([["a", "", "b"], ["", "", "c"], ["d", "e", "f"]]));+ });++ it("resets every merged cell of a multi-cell selection and ignores the rest", () => {+ setModelData(editor.model, spannedTable([+ [{ contents: "a", colspan: 2 }, "b"],+ ["c", "d", { contents: "e", rowspan: 2 }],+ ["f", "g"]+ ]));+ selectCells(editor, [0, 0], [1, 2]);+ editor.execute(COMMAND);++ expect(tableData(editor)).toBe(modelTable([+ ["a", "", "b"],+ ["c", "d", "e"],+ ["f", "g", ""]+ ]));+ });++ it("undoes in one step", () => {+ const merged = spannedTable([+ [{ contents: "a", colspan: 2 }, { contents: "b", rowspan: 2 }],+ ["c", "d"]+ ]);+ setModelData(editor.model, merged);+ selectCells(editor, [0, 0], [1, 1]);++ editor.execute(COMMAND);+ editor.execute("undo");++ expect(tableData(editor)).toBe(merged);+ });+ });+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.tsindex b2fadfc452..ab3ea788ff 100644--- a/packages/ckeditor5/src/plugins/table_context_menu.ts+++ b/packages/ckeditor5/src/plugins/table_context_menu.ts@@ -45,6 +45,7 @@ export default class TableContextMenu extends Plugin {editor.commands.add("triliumDistributeTableColumns",new DistributeTableColumnsCommand(editor));editor.commands.add("triliumDeleteTable", new DeleteTableCommand(editor));+ editor.commands.add("triliumResetTableCellSpans", new ResetTableCellSpansCommand(editor));const view = editor.editing.view;view.addObserver(MouseObserver);@@ -181,6 +182,58 @@ export class DeleteTableCommand extends Command {}+/**+ * Splits every merged cell touched by the selection back into single cells, the reverse of a+ * merge. The content stays in the top-left cell; the others are created empty. Cells that are not+ * merged are left alone.+ */+export class ResetTableCellSpansCommand extends Command {++ refresh() {+ this.isEnabled = this.findMergedCells().length > 0;+ }++ execute() {+ const tableUtils = this.editor.plugins.get(TableUtils);+ const mergedCells = this.findMergedCells();++ // Splitting a cell into as many cells as it spans only redistributes the span, so the grid+ // keeps its size and the other cells stay where they are.+ this.editor.model.change(() => {+ for (const cell of mergedCells) {+ const colspan = getSpan(cell, "colspan");+ const rowspan = getSpan(cell, "rowspan");+ const rowCells = [cell];++ if (colspan > 1) {+ tableUtils.splitCellVertically(cell, colspan);+ let next = cell.nextSibling;+ while (rowCells.length < colspan && next?.is("element", "tableCell")) {+ rowCells.push(next);+ next = next.nextSibling;+ }+ }+ if (rowspan > 1) {+ for (const rowCell of rowCells) {+ tableUtils.splitCellHorizontally(rowCell, rowspan);+ }+ }+ }+ });+ }++ private findMergedCells(): ModelElement[] {+ const tableUtils = this.editor.plugins.get(TableUtils);+ return tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection)+ .filter((cell) => getSpan(cell, "colspan") > 1 || getSpan(cell, "rowspan") > 1);+ }++}++function getSpan(cell: ModelElement, attribute: "colspan" | "rowspan"): number {+ return Number(cell.getAttribute(attribute) ?? 1);+}+/** 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;@@ -210,5 +263,6 @@ declare module "ckeditor5" {triliumInsertTableColumnsRight: TableMultiInsertCommand;triliumDistributeTableColumns: DistributeTableColumnsCommand;triliumDeleteTable: DeleteTableCommand;+ triliumResetTableCellSpans: ResetTableCellSpansCommand;}}