Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

feat(tables): spreadsheet-style Enter navigation in tables (#2982)

Abrahamf11a70e92d09
diff --git a/.changeset/table-last-row-enter.md b/.changeset/table-last-row-enter.md
new file mode 100644
index 000000000..e40a8b50d
--- /dev/null
+++ b/.changeset/table-last-row-enter.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": minor
+---
+
+Enter inside a table now works the way it does in Notion and Obsidian: it moves the cursor to the same column of the row below, and from the last row it creates a new row — so you can fill a table top to bottom without touching the mouse. Enter no longer splits a cell into multiple paragraphs (which markdown tables can't represent and used to resolve unpredictably); Shift+Enter is the way to get a line break inside a cell.
diff --git a/packages/app/src/editor/extensions/shared.ts b/packages/app/src/editor/extensions/shared.ts
index f4365c5ca..f2e6334fd 100644
--- a/packages/app/src/editor/extensions/shared.ts
+++ b/packages/app/src/editor/extensions/shared.ts
@@ -41,6 +41,7 @@ import { SlashCommand } from './slash-command';
import { SourceDirtyObserver } from './source-dirty-observer';
import { TabFocusTrap } from './tab-focus-trap';
import { TableInsertControls } from './table-insert-controls';
+import { TableRowEnter } from './table-row-enter';
import { TagClickPlugin } from './tag-click-plugin';
import { Tag } from './tag-view';
import { WikiLink } from './wiki-link';
@@ -156,6 +157,10 @@ export const sharedExtensions = [
// `view.dom.parentElement` for the same Activity-flip reason as
// BlockDragHandle; keyboard parity lives in the TableCellHandles dropdowns.
TableInsertControls,
+ // TableRowEnter — spreadsheet-style Enter in tables: always moves the caret
+ // to the same column of the row below (appending a row from the last row),
+ // never splitting the cell; Shift+Enter stays the in-cell line break.
+ TableRowEnter,
SourceDirtyObserver,
// Typed-URL autolink. Origin-guarded (never converts CRDT-sync-tagged
// peer/agent/disk edits) and active-editor-gated (never converts in a pooled
diff --git a/packages/app/src/editor/extensions/table-row-enter.test.ts b/packages/app/src/editor/extensions/table-row-enter.test.ts
new file mode 100644
index 000000000..16a78c988
--- /dev/null
+++ b/packages/app/src/editor/extensions/table-row-enter.test.ts
@@ -0,0 +1,172 @@
+import { sharedExtensions } from '@inkeep/open-knowledge-core';
+import { getSchema } from '@tiptap/core';
+import type { Node as PmNode } from '@tiptap/pm/model';
+import { EditorState, TextSelection } from '@tiptap/pm/state';
+import { CellSelection, cellAround, TableMap } from '@tiptap/pm/tables';
+import { describe, expect, test } from 'vitest';
+import { tableEnterDown } from './table-row-enter';
+
+// Schema from core's shared extensions rather than `@tiptap/extension-*`
+// directly — those are only transitive deps here and importing them trips
+// knip (same setup as table-insert-commands.test.ts).
+const schema = getSchema(sharedExtensions);
+
+/**
+ * Build a `rows × cols` table doc; row 0 is header cells, the rest body
+ * cells. With `withText`, each cell holds `R{r}C{c}` so caret-offset
+ * assertions can tell the end of a cell's text from its start.
+ */
+function makeTableDoc(rows: number, cols: number, withText = false): PmNode {
+ const cell = (r: number, c: number) =>
+ (r === 0 ? schema.nodes.tableHeader : schema.nodes.tableCell).createChecked(
+ null,
+ schema.nodes.paragraph.create(null, withText ? schema.text(`R${r}C${c}`) : undefined),
+ );
+ const tableRows: PmNode[] = [];
+ for (let r = 0; r < rows; r++) {
+ const cells: PmNode[] = [];
+ for (let c = 0; c < cols; c++) cells.push(cell(r, c));
+ tableRows.push(schema.nodes.tableRow.createChecked(null, cells));
+ }
+ const table = schema.nodes.table.createChecked(null, tableRows);
+ return schema.nodes.doc.create(null, table);
+}
+
+/** State with the caret inside the cell at (row, col). The table sits at pos 0. */
+function stateWithCaretInCell(doc: PmNode, row: number, col: number): EditorState {
+ const table = doc.nodeAt(0);
+ if (!table) throw new Error('no table at pos 0');
+ const tableStart = 1;
+ const cellPos = tableStart + TableMap.get(table).positionAt(row, col, table);
+ return EditorState.create({
+ schema,
+ doc,
+ selection: TextSelection.near(doc.resolve(cellPos + 1)),
+ });
+}
+
+/** (row, col) of the cell the selection caret sits in. */
+function caretCellRect(state: EditorState) {
+ const $cell = cellAround(state.selection.$from);
+ if (!$cell) throw new Error('caret not in a cell');
+ const table = $cell.node(-1);
+ return TableMap.get(table).findCell($cell.pos - $cell.start(-1));
+}
+
+describe('tableEnterDown', () => {
+ test('caret in a last-row cell appends a row and moves the caret to it', () => {
+ const state = stateWithCaretInCell(makeTableDoc(3, 3), 2, 1);
+
+ const tr = tableEnterDown(state);
+ expect(tr).not.toBeNull();
+ const next = state.apply(tr as NonNullable<typeof tr>);
+
+ const table = next.doc.nodeAt(0);
+ const map = TableMap.get(table as PmNode);
+ expect(map.height).toBe(4);
+ expect(map.width).toBe(3);
+
+ // caret landed in the new row, same column
+ const rect = caretCellRect(next);
+ expect(rect.top).toBe(3);
+ expect(rect.left).toBe(1);
+
+ // the appended row is body cells, not header cells
+ const lastRow = (table as PmNode).child(3);
+ for (let c = 0; c < lastRow.childCount; c++) {
+ expect(lastRow.child(c).type.name).toBe('tableCell');
+ }
+ });
+
+ test('header row of a header-only table counts as the last row', () => {
+ const state = stateWithCaretInCell(makeTableDoc(1, 2), 0, 0);
+
+ const tr = tableEnterDown(state);
+ expect(tr).not.toBeNull();
+ const next = state.apply(tr as NonNullable<typeof tr>);
+
+ expect(TableMap.get(next.doc.nodeAt(0) as PmNode).height).toBe(2);
+ const rect = caretCellRect(next);
+ expect(rect.top).toBe(1);
+ expect(rect.left).toBe(0);
+ });
+
+ test('caret in a non-last row navigates to the END of the cell below', () => {
+ const state = stateWithCaretInCell(makeTableDoc(3, 3, true), 1, 2);
+
+ const tr = tableEnterDown(state);
+ expect(tr).not.toBeNull();
+ const next = state.apply(tr as NonNullable<typeof tr>);
+
+ // pure navigation — same doc, no new rows, no split cell
+ expect(next.doc.eq(state.doc)).toBe(true);
+ const rect = caretCellRect(next);
+ expect(rect.top).toBe(2);
+ expect(rect.left).toBe(2);
+
+ // caret sits AFTER the existing text ("R2C2"), not before it
+ expect(next.selection.empty).toBe(true);
+ expect(next.selection.$from.parent.textContent).toBe('R2C2');
+ expect(next.selection.$from.parentOffset).toBe('R2C2'.length);
+ });
+
+ test('caret in a header cell of a multi-row table navigates to the first body row', () => {
+ const state = stateWithCaretInCell(makeTableDoc(3, 2, true), 0, 1);
+
+ const tr = tableEnterDown(state);
+ expect(tr).not.toBeNull();
+ const next = state.apply(tr as NonNullable<typeof tr>);
+
+ expect(next.doc.eq(state.doc)).toBe(true);
+ const rect = caretCellRect(next);
+ expect(rect.top).toBe(1);
+ expect(rect.left).toBe(1);
+ expect(next.selection.$from.parent.textContent).toBe('R1C1');
+ expect(next.selection.$from.parentOffset).toBe('R1C1'.length);
+ });
+
+ test('a non-empty in-cell selection is intercepted (moves down, no cell split)', () => {
+ const doc = makeTableDoc(3, 2);
+ const table = doc.nodeAt(0) as PmNode;
+ const cellPos = 1 + TableMap.get(table).positionAt(1, 0, table);
+ const state = EditorState.create({
+ schema,
+ doc,
+ // empty cell: anchor at paragraph-open, focus at paragraph-close — a non-collapsed TextSelection
+ selection: TextSelection.create(doc, cellPos + 1, cellPos + 2),
+ });
+
+ const tr = tableEnterDown(state);
+ expect(tr).not.toBeNull();
+ const next = state.apply(tr as NonNullable<typeof tr>);
+
+ expect(next.doc.eq(state.doc)).toBe(true);
+ const rect = caretCellRect(next);
+ expect(rect.top).toBe(2);
+ expect(rect.left).toBe(0);
+ });
+
+ test('a CellSelection (whole-cell drag) falls through to default handling', () => {
+ const doc = makeTableDoc(3, 2, true);
+ const table = doc.nodeAt(0) as PmNode;
+ const cellPos = 1 + TableMap.get(table).positionAt(1, 0, table);
+ const state = EditorState.create({
+ schema,
+ doc,
+ // CellSelection's $from resolves inside a cell, so only the
+ // instanceof TextSelection guard keeps this out of the down-navigation.
+ selection: CellSelection.create(doc, cellPos),
+ });
+ expect(tableEnterDown(state)).toBeNull();
+ });
+
+ test('caret outside any table falls through', () => {
+ const doc = schema.nodes.doc.create(null, schema.nodes.paragraph.create());
+ const state = EditorState.create({
+ schema,
+ doc,
+ selection: TextSelection.near(doc.resolve(1)),
+ });
+ expect(tableEnterDown(state)).toBeNull();
+ });
+});
diff --git a/packages/app/src/editor/extensions/table-row-enter.ts b/packages/app/src/editor/extensions/table-row-enter.ts
new file mode 100644
index 000000000..0b5991520
--- /dev/null
+++ b/packages/app/src/editor/extensions/table-row-enter.ts
@@ -0,0 +1,82 @@
+/**
+ * Spreadsheet-style Enter inside tables (the Obsidian/Notion model): Enter
+ * always moves the caret to the same column of the row below, creating a new
+ * row when the caret is already in the last row. Enter NEVER falls through to
+ * the default paragraph split inside a cell — a multi-paragraph cell is
+ * unrepresentable in GFM, so the split survives only until server
+ * canonicalization restructures it unpredictably. Shift+Enter remains the
+ * in-cell line break (the one newline GFM cells can represent).
+ *
+ * Kept as a selection-driven transaction builder (view-free) so it's
+ * unit-testable against a bare `EditorState`, mirroring
+ * `table-insert-commands.ts`.
+ */
+
+import { Extension } from '@tiptap/core';
+import type { EditorState, Transaction } from '@tiptap/pm/state';
+import { TextSelection } from '@tiptap/pm/state';
+import { addRow, cellAround, TableMap } from '@tiptap/pm/tables';
+
+/**
+ * Transaction moving the caret one row down (same column), appending a row
+ * first when the caret sits in the last row; `null` when the selection's head
+ * is not inside a table cell (caller falls through to the default Enter).
+ */
+export function tableEnterDown(state: EditorState): Transaction | null {
+ const { selection } = state;
+ // Cover non-empty in-cell selections too: the default Enter would delete
+ // them and split the cell, recreating the unrepresentable multi-paragraph
+ // shape. CellSelection (whole-cell drag) is not a TextSelection and keeps
+ // its default handling.
+ if (!(selection instanceof TextSelection)) return null;
+ const $cell = cellAround(selection.$from);
+ if (!$cell) return null;
+
+ const table = $cell.node(-1);
+ const tableStart = $cell.start(-1);
+ const map = TableMap.get(table);
+ const rect = map.findCell($cell.pos - tableStart);
+
+ // GFM tables are rectangular (no rowspan), so bottom === height exactly
+ // identifies the last row — including a header-only table, where Enter in
+ // the header grows the first body row.
+ if (rect.bottom === map.height) {
+ const tr = addRow(
+ state.tr,
+ { map, tableStart, table, left: 0, top: 0, right: map.width, bottom: map.height },
+ map.height,
+ );
+ const tableAfter = tr.doc.nodeAt(tableStart - 1);
+ if (!tableAfter) return null;
+ const mapAfter = TableMap.get(tableAfter);
+ const newCellPos = tableStart + mapAfter.positionAt(map.height, rect.left, tableAfter);
+ tr.setSelection(TextSelection.near(tr.doc.resolve(newCellPos + 1)));
+ return tr.scrollIntoView();
+ }
+
+ // Not the last row: navigate to the cell below, same column, caret at the
+ // END of its content — that's the continue-typing position when the row
+ // already holds text.
+ const belowOffset = map.positionAt(rect.bottom, rect.left, table);
+ const belowCell = table.nodeAt(belowOffset);
+ if (!belowCell) return null;
+ const belowPos = tableStart + belowOffset;
+ const tr = state.tr;
+ tr.setSelection(TextSelection.near(tr.doc.resolve(belowPos + belowCell.nodeSize - 1), -1));
+ return tr.scrollIntoView();
+}
+
+export const TableRowEnter = Extension.create({
+ name: 'tableRowEnter',
+
+ addKeyboardShortcuts() {
+ return {
+ Enter: ({ editor }) => {
+ const tr = tableEnterDown(editor.state);
+ if (!tr) return false;
+ editor.view.dispatch(tr);
+ return true;
+ },
+ };
+ },
+});