Skip to content

Commit 04d6025

Browse files
committed
Made pressing Tab/Shift+Tab at the end/start cell of a table no-op
1 parent 118d8dc commit 04d6025

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

packages/core/src/blocks/Table/TableExtension.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,28 @@ export const TableExtension = Extension.create({
8484
},
8585
// Enables navigating cells using the tab key.
8686
Tab: () => {
87-
return this.editor.commands.command(({ state, dispatch, view }) =>
88-
goToNextCell(1)(state, dispatch, view),
89-
);
87+
return this.editor.commands.command(({ state, dispatch, view }) => {
88+
if (!isInTable(state)) {
89+
return false;
90+
}
91+
92+
goToNextCell(1)(state, dispatch, view);
93+
94+
// Always return true to avoid accidental indents.
95+
return true;
96+
});
9097
},
9198
"Shift-Tab": () => {
92-
return this.editor.commands.command(({ state, dispatch, view }) =>
93-
goToNextCell(-1)(state, dispatch, view),
94-
);
99+
return this.editor.commands.command(({ state, dispatch, view }) => {
100+
if (!isInTable(state)) {
101+
return false;
102+
}
103+
104+
// Always return true to avoid accidental un-indents.
105+
goToNextCell(-1)(state, dispatch, view);
106+
107+
return true;
108+
});
95109
},
96110
};
97111
},

tests/src/end-to-end/tables/tables.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { test } from "../../setup/setupScript.js";
33
import { BASE_URL, TABLE_SELECTOR } from "../../utils/const.js";
44
import { compareDocToSnapshot, focusOnEditor } from "../../utils/editor.js";
55
import { executeSlashCommand } from "../../utils/slashmenu.js";
6+
import { insertParagraph } from "../../utils/copypaste.js";
67

78
test.beforeEach(async ({ page }) => {
89
await page.goto(BASE_URL);
@@ -32,6 +33,32 @@ test.describe("Check Table interactions", () => {
3233

3334
await compareDocToSnapshot(page, "tabCells.json");
3435
});
36+
test("Tab in last cell should be a no-op", async ({ page }) => {
37+
await focusOnEditor(page);
38+
39+
await insertParagraph(page);
40+
await executeSlashCommand(page, "table");
41+
42+
for (let i = 0; i < 6; i++) {
43+
await page.keyboard.press("Tab");
44+
}
45+
46+
// Only top level block group should exist.
47+
await expect(page.locator(".bn-block-group")).toHaveCount(1);
48+
});
49+
test("Shift+Tab in first should be a no-op", async ({ page }) => {
50+
await focusOnEditor(page);
51+
52+
await insertParagraph(page);
53+
await page.keyboard.press("Enter");
54+
await page.keyboard.press("Tab");
55+
await executeSlashCommand(page, "table");
56+
57+
await page.keyboard.press("Shift+Tab");
58+
59+
// Block group containing table should exist as well as top level group.
60+
await expect(page.locator(".bn-block-group")).toHaveCount(2);
61+
});
3562
test("Arrow keys should move cells", async ({ page }) => {
3663
await focusOnEditor(page);
3764
await executeSlashCommand(page, "table");

0 commit comments

Comments
 (0)