Skip to content

Commit 17bf418

Browse files
fix(tables): tab causing indent in last table cell (BLO-1211) (#2831)
1 parent 1c0a53c commit 17bf418

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
@@ -73,14 +73,28 @@ export const TableExtension = Extension.create({
7373
},
7474
// Enables navigating cells using the tab key.
7575
Tab: () => {
76-
return this.editor.commands.command(({ state, dispatch, view }) =>
77-
goToNextCell(1)(state, dispatch, view),
78-
);
76+
return this.editor.commands.command(({ state, dispatch, view }) => {
77+
if (!isInTable(state)) {
78+
return false;
79+
}
80+
81+
goToNextCell(1)(state, dispatch, view);
82+
83+
// Always return true to avoid accidental indents.
84+
return true;
85+
});
7986
},
8087
"Shift-Tab": () => {
81-
return this.editor.commands.command(({ state, dispatch, view }) =>
82-
goToNextCell(-1)(state, dispatch, view),
83-
);
88+
return this.editor.commands.command(({ state, dispatch, view }) => {
89+
if (!isInTable(state)) {
90+
return false;
91+
}
92+
93+
// Always return true to avoid accidental un-indents.
94+
goToNextCell(-1)(state, dispatch, view);
95+
96+
return true;
97+
});
8498
},
8599
};
86100
},

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../../utils/editor.js";
1111
import { mouseSequence, moveMouseOverElement } from "../../utils/mouse.js";
1212
import { executeSlashCommand } from "../../utils/slashmenu.js";
13+
import { insertParagraph } from "../../utils/copypaste.js";
1314

1415
beforeEach(async () => {
1516
await render(<App />);
@@ -40,6 +41,32 @@ describe("Check Table interactions", () => {
4041

4142
await compareDocToSnapshot("tabCells");
4243
});
44+
test("Tab in last cell should be a no-op", async () => {
45+
await focusOnEditor();
46+
47+
await insertParagraph();
48+
await executeSlashCommand("table");
49+
50+
for (let i = 0; i < 6; i++) {
51+
await userEvent.keyboard("{Tab}");
52+
}
53+
54+
// Only top level block group should exist.
55+
expect(document.querySelectorAll(".bn-block-group").length).toBe(1);
56+
});
57+
test("Shift+Tab in first should be a no-op", async () => {
58+
await focusOnEditor();
59+
60+
await insertParagraph();
61+
await userEvent.keyboard("{Enter}");
62+
await userEvent.keyboard("{Tab}");
63+
await executeSlashCommand("table");
64+
65+
await userEvent.keyboard("{Shift>}{Tab}{/Shift}");
66+
67+
// Block group containing table should exist as well as top level group.
68+
expect(document.querySelectorAll(".bn-block-group").length).toBe(2);
69+
});
4370
test("Arrow keys should move cells", async () => {
4471
await focusOnEditor();
4572
await executeSlashCommand("table");

0 commit comments

Comments
 (0)