Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions packages/core/src/blocks/Table/TableExtension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { callOrReturn, Extension, getExtensionField } from "@tiptap/core";
import { columnResizing, goToNextCell, tableEditing } from "prosemirror-tables";
import { TextSelection } from "prosemirror-state";
import {
columnResizing,
goToNextCell,
isInTable,
moveCellForward,
nextCell,
selectionCell,
tableEditing,
} from "prosemirror-tables";

export const RESIZE_MIN_WIDTH = 35;
export const EMPTY_CELL_WIDTH = 120;
Expand All @@ -24,19 +33,39 @@ export const TableExtension = Extension.create({

addKeyboardShortcuts() {
return {
// Makes enter create a new line within the cell.
// Moves the selection to the cell below.
Enter: () => {
if (
this.editor.state.selection.empty &&
this.editor.state.selection.$head.parent.type.name ===
"tableParagraph"
this.editor.state.selection.$head.parent.type.name !==
"tableParagraph"
) {
this.editor.commands.insertContent({ type: "hardBreak" });

return true;
return false;
}

return false;
return this.editor.commands.command(({ state, dispatch }) => {
if (!isInTable(state)) {
return false;
}

const $cell = selectionCell(state);
const $nextCell = nextCell($cell, "vert", 1);

if (!$nextCell) {
return false;
}

if (dispatch) {
dispatch(
state.tr
.setSelection(
TextSelection.between($nextCell, moveCellForward($nextCell)),
)
.scrollIntoView(),
);
}

return true;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
},
// Ensures that backspace won't delete the table if the text cursor is at
// the start of a cell and the selection is empty.
Expand Down
18 changes: 18 additions & 0 deletions tests/src/end-to-end/tables/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ test.describe("Check Table interactions", () => {

await compareDocToSnapshot(page, "arrowKeyCells.json");
});
test("Enter should move to cell below", async ({ page }) => {
await focusOnEditor(page);
await executeSlashCommand(page, "table");
await page.keyboard.type("Top");
await page.keyboard.press("Enter");
await page.keyboard.type("Bottom");

await compareDocToSnapshot(page, "enterMovesToCellBelow.json");
});
test("Shift+Enter should create a new line within cell", async ({ page }) => {
await focusOnEditor(page);
await executeSlashCommand(page, "table");
await page.keyboard.type("Line 1");
await page.keyboard.press("Shift+Enter");
await page.keyboard.type("Line 2");

await compareDocToSnapshot(page, "shiftEnterNewLineInCell.json");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"type": "doc",
"content": [
{
"type": "blockGroup",
"content": [
{
"type": "blockContainer",
"attrs": {
"id": "0"
},
"content": [
{
"type": "table",
"attrs": {
"textColor": "default"
},
"content": [
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph",
"content": [
{
"type": "text",
"text": "Top"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph"
}
]
},
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph"
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph",
"content": [
{
"type": "text",
"text": "Bottom"
}
]
}
]
},
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph"
}
]
},
{
"type": "tableCell",
"attrs": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"colspan": 1,
"rowspan": 1,
"colwidth": null
},
"content": [
{
"type": "tableParagraph"
}
]
}
]
}
]
}
]
},
{
"type": "blockContainer",
"attrs": {
"id": "1"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
}
}
]
}
]
}
]
}
Loading
Loading