Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: add support for indenting tables

Adorian Dorand7bed18525a6
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index d3314ba02a..8d1f9db070 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -46,6 +46,7 @@ import LinkEmbedToolbar from "./plugins/link_embed/link_embed_toolbar.js";
import TodoListMultistate from "./plugins/todo_list_multistate/todo_list_multistate.js";
import TodoListUncheckOnEnter from "./plugins/todo_list_uncheck_on_enter.js";
import CollapsibleListItems from "./plugins/collapsible_list_items.js";
+import TableIndent from "./plugins/table_indent.js";
/**
* Plugins that are specific to Trilium and not part of the CKEditor 5 core, included in both text editors but not in the attribute editor.
@@ -78,6 +79,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
LinkEmbedToolbar,
TodoListMultistate,
CollapsibleListItems,
+ TableIndent,
CopyAnchorLinkButton,
CopyLinkUrlButton,
ImageActions,
diff --git a/packages/ckeditor5/src/plugins/table_indent.spec.ts b/packages/ckeditor5/src/plugins/table_indent.spec.ts
new file mode 100644
index 0000000000..6044138072
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/table_indent.spec.ts
@@ -0,0 +1,64 @@
+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, Indent, IndentBlock, Paragraph, Table, TableEditing } from "ckeditor5";
+import { beforeEach, describe, expect, it } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import TableIndent from "./table_indent.js";
+
+const TABLE = "<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>";
+
+describe("TableIndent", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ editor = await createTestEditor([Essentials, Paragraph, Indent, IndentBlock, Table, TableEditing, TableIndent]);
+ });
+
+ it("loads the plugin", () => {
+ expect(editor.plugins.get(TableIndent)).toBeInstanceOf(TableIndent);
+ });
+
+ it("allows the blockIndent attribute on the table element", () => {
+ expect(editor.model.schema.checkAttribute(["$root", "table"], "blockIndent")).toBe(true);
+ });
+
+ it("enables indentBlock when a table is selected", () => {
+ setModelData(editor.model, `[${TABLE}]`);
+
+ expect(editor.commands.get("indentBlock")?.isEnabled).toBe(true);
+ });
+
+ it("indents and outdents a selected table by the configured offset", () => {
+ setModelData(editor.model, `[${TABLE}]`);
+
+ editor.execute("indentBlock");
+ expect(getModelData(editor.model)).toContain('blockIndent="40px"');
+
+ editor.execute("outdentBlock");
+ expect(getModelData(editor.model)).not.toContain("blockIndent");
+ });
+
+ it("round-trips the indentation as a margin-left on the table figure", () => {
+ setModelData(editor.model, `[${TABLE}]`);
+ editor.execute("indentBlock");
+
+ const data = editor.getData();
+ expect(data).toContain("margin-left:40px");
+
+ editor.setData(data);
+ expect(getModelData(editor.model)).toContain('blockIndent="40px"');
+ });
+
+ it("caps the table width by the indent so it stays within its parent", () => {
+ setModelData(editor.model, `[${TABLE}]`);
+ editor.execute("indentBlock");
+
+ // margin-left + max-width land on the same element, so the indented table cannot overflow.
+ const data = editor.getData();
+ expect(data).toContain("margin-left:40px");
+ expect(data).toContain("max-width:calc(100% - 40px)");
+
+ // Outdenting back to zero removes both the offset and the width cap.
+ editor.execute("outdentBlock");
+ expect(editor.getData()).not.toContain("max-width");
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/table_indent.ts b/packages/ckeditor5/src/plugins/table_indent.ts
new file mode 100644
index 0000000000..e0456a2bed
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/table_indent.ts
@@ -0,0 +1,65 @@
+import { IndentBlock, Plugin, Table } from "ckeditor5";
+import type { DowncastAttributeEvent, DowncastDispatcher } from "ckeditor5";
+
+/**
+ * Lets tables participate in block indentation.
+ *
+ * CKEditor's {@link IndentBlock} feature only allows the `blockIndent` attribute on paragraphs and
+ * headings (its `DEFAULT_ELEMENTS`), so tables cannot be indented out of the box. Here we widen the
+ * schema to allow `blockIndent` on the `table` element as well. The attribute is downcast generically
+ * by `IndentBlock` to a `margin-left` style on the table's `<figure>` wrapper, so the existing
+ * `indentBlock`/`outdentBlock` commands and the margin-left round-trip apply to the table once the
+ * schema permits it.
+ *
+ * With a table widget selected, the indent/outdent toolbar buttons then shift the whole table by the
+ * configured indentation offset. (Tab inside a table stays reserved for cell navigation, see
+ * {@link IndentBlockShortcutPlugin}.)
+ *
+ * Inserted tables are full width (`TableColumnResize` sets the figure to `width: 100%`), so the raw
+ * `margin-left` would push the table past the content edge. We therefore also cap the figure width by
+ * the same offset — `max-width: calc(100% - <indent>)` — so an indented table shrinks to stay within
+ * its parent instead of overflowing. Narrower (resized) tables are below that cap and are unaffected.
+ */
+export default class TableIndent extends Plugin {
+
+ static get requires() {
+ return [IndentBlock, Table] as const;
+ }
+
+ static get pluginName() {
+ return "TableIndent" as const;
+ }
+
+ init() {
+ const editor = this.editor;
+ editor.model.schema.extend("table", { allowAttributes: "blockIndent" });
+ editor.conversion.for("downcast").add(keepIndentedTableWithinParent);
+ }
+
+}
+
+/**
+ * Downcast helper: mirrors `IndentBlock`'s `margin-left` on an indented table with a matching
+ * `max-width: calc(100% - <indent>)` so the table stays within its parent. Runs at low priority so
+ * the table's `<figure>` is already mapped, and does not consume the attribute — `IndentBlock` keeps
+ * ownership of `margin-left`.
+ */
+function keepIndentedTableWithinParent(dispatcher: DowncastDispatcher) {
+ dispatcher.on<DowncastAttributeEvent>("attribute:blockIndent:table", (evt, data, conversionApi) => {
+ if (!data.item.is("element", "table")) {
+ return;
+ }
+
+ const figure = conversionApi.mapper.toViewElement(data.item);
+ if (!figure) {
+ return;
+ }
+
+ const viewWriter = conversionApi.writer;
+ if (data.attributeNewValue) {
+ viewWriter.setStyle("max-width", `calc(100% - ${data.attributeNewValue})`, figure);
+ } else {
+ viewWriter.removeStyle("max-width", figure);
+ }
+ }, { priority: "low" });
+}