Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ export const KeyboardShortcutsExtension = Extension.create<{

let chainedCommands = chain();

if (blockInfo.childContainer) {
chainedCommands.insertContentAt(
blockInfo.bnBlock.afterPos,
blockInfo.childContainer?.node.content,
);
}

if (
prevBlockInfo.blockContent.node.type.spec.content ===
"tableRow+"
Expand All @@ -209,8 +216,7 @@ export const KeyboardShortcutsExtension = Extension.create<{
);
} else {
const blockContentStartPos =
prevBlockInfo.blockContent.afterPos -
prevBlockInfo.blockContent.node.nodeSize;
prevBlockInfo.blockContent.afterPos - 1;

chainedCommands =
chainedCommands.setTextSelection(blockContentStartPos);
Expand Down Expand Up @@ -413,7 +419,7 @@ export const KeyboardShortcutsExtension = Extension.create<{
// Creates a new block and moves the selection to it if the current one is empty, while the selection is also
// empty & at the start of the block.
() =>
commands.command(({ state, dispatch }) => {
commands.command(({ state, dispatch, tr }) => {
const blockInfo = getBlockInfoFromSelection(state);
if (!blockInfo.isBlockContainer) {
return false;
Expand All @@ -431,15 +437,29 @@ export const KeyboardShortcutsExtension = Extension.create<{
const newBlockContentPos = newBlockInsertionPos + 2;

if (dispatch) {
const newBlock =
state.schema.nodes["blockContainer"].createAndFill()!;

state.tr
.insert(newBlockInsertionPos, newBlock)
const newBlock = state.schema.nodes[
"blockContainer"
].createAndFill(
undefined,
[
state.schema.nodes["paragraph"].createAndFill() ||
undefined,
blockInfo.childContainer?.node,
].filter((node) => node !== undefined),
)!;

tr.insert(newBlockInsertionPos, newBlock)
.setSelection(
new TextSelection(tr.doc.resolve(newBlockContentPos)),
)
.scrollIntoView();
state.tr.setSelection(
new TextSelection(state.doc.resolve(newBlockContentPos)),
);

if (blockInfo.childContainer) {
tr.delete(
blockInfo.childContainer.beforePos,
blockInfo.childContainer.afterPos,
);
}
}

return true;
Expand Down
44 changes: 43 additions & 1 deletion tests/src/end-to-end/keyboardhandlers/keyboardhandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,32 @@ test.describe("Check Keyboard Handlers' Behaviour", () => {
await page.keyboard.press("ArrowUp");
await page.keyboard.press("ArrowUp");
await page.keyboard.press("ArrowUp");
await page.keyboard.press("ArrowUp");
await page.keyboard.press("Control+ArrowLeft");
await page.keyboard.press("ArrowRight");
await page.keyboard.press("Enter");

await compareDocToSnapshot(page, "enterPreservesNestedBlocks.json");
});
test("Check Enter preserves nested blocks for empty block", async ({
page,
}) => {
await focusOnEditor(page);
await page.keyboard.press("#");
await page.keyboard.press(" ");
await page.keyboard.press("ArrowDown", { delay: 10 });
await page.keyboard.press("Tab");
await insertHeading(page, 2);
await page.keyboard.press("Tab");
await insertHeading(page, 3);

await page.waitForTimeout(500);
await page.keyboard.press("ArrowUp");
await page.keyboard.press("ArrowUp");
await page.keyboard.press("ArrowUp");
await page.keyboard.press("Enter");

await compareDocToSnapshot(page, "enterPreservesNestedBlocksEmpty.json");
});
test("Check Backspace at the start of a block", async ({ page }) => {
await focusOnEditor(page);
await insertHeading(page, 1);
Expand Down Expand Up @@ -129,6 +148,29 @@ test.describe("Check Keyboard Handlers' Behaviour", () => {

await compareDocToSnapshot(page, "backspacePreservesNestedBlocks.json");
});
test("Check Backspace preserves nested blocks for empty block", async ({
page,
}) => {
await focusOnEditor(page);
await insertParagraph(page);
await page.keyboard.press("Enter", { delay: 10 });
await page.keyboard.press("Tab");
await insertParagraph(page);
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await insertParagraph(page);

for (let i = 0; i < 3; i++) {
await page.keyboard.press("ArrowUp");
}

await page.keyboard.press("Backspace");

await compareDocToSnapshot(
page,
"backspacePreservesNestedBlocksEmpty.json",
);
});
test("Check heading 1 shortcut", async ({ page }) => {
await focusOnEditor(page);
await page.keyboard.type("Paragraph");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"type": "doc",
"content": [
{
"type": "blockGroup",
"content": [
{
"type": "blockContainer",
"attrs": {
"id": "0"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
}
]
},
{
"type": "blockContainer",
"attrs": {
"id": "2"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
},
{
"type": "blockGroup",
"content": [
{
"type": "blockContainer",
"attrs": {
"id": "3"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
}
]
}
]
}
]
},
{
"type": "blockContainer",
"attrs": {
"id": "4"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
}
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"type": "doc",
"content": [
{
"type": "blockGroup",
"content": [
{
"type": "blockContainer",
"attrs": {
"id": "0"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
}
]
},
{
"type": "blockContainer",
"attrs": {
"id": "2"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
},
{
"type": "blockGroup",
"content": [
{
"type": "blockContainer",
"attrs": {
"id": "3"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Paragraph"
}
]
}
]
}
]
}
]
},
{
"type": "blockContainer",
"attrs": {
"id": "4"
},
"content": [
{
"type": "paragraph",
"attrs": {
"backgroundColor": "default",
"textColor": "default",
"textAlignment": "left"
}
}
]
}
]
}
]
}
Loading
Loading