Skip to content

Commit 7317c64

Browse files
committed
Made slash menu items insert empty paragraph if no blocks with inline content are found after the inserted block
1 parent ec9c151 commit 7317c64

21 files changed

Lines changed: 110 additions & 7 deletions

packages/core/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import { DefaultSuggestionItem } from "./DefaultSuggestionItem.js";
1414
import { SuggestionMenu } from "./SuggestionMenu.js";
1515

1616
// Sets the editor's text cursor position to the next content editable block,
17-
// so either a block with inline content or a table. The last block is always a
18-
// paragraph, so this function won't try to set the cursor position past the
19-
// last block.
17+
// so either a block with inline content or a table. If no such block exists
18+
// after the current one, an empty paragraph is appended to the end of the
19+
// document and the cursor is moved to it.
2020
function setSelectionToNextContentEditableBlock<
2121
BSchema extends BlockSchema,
2222
I extends InlineContentSchema,
@@ -29,6 +29,16 @@ function setSelectionToNextContentEditableBlock<
2929
while (contentType === "none") {
3030
block = editor.getTextCursorPosition().nextBlock;
3131
if (block === undefined) {
32+
// No content editable block exists after the current one, so we append
33+
// an empty paragraph to the end of the document and move the cursor to
34+
// it.
35+
const lastBlock = editor.document[editor.document.length - 1];
36+
const newBlock = editor.insertBlocks(
37+
[{ type: "paragraph" }],
38+
lastBlock,
39+
"after",
40+
)[0];
41+
editor.setTextCursorPosition(newBlock, "end");
3242
return;
3343
}
3444
contentType = editor.schema.blockSchema[block.type].content as
2.5 KB
Loading
2.29 KB
Loading
2.54 KB
Loading

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ describe("Check Block Dragging Functionality", () => {
102102
await focusOnEditor();
103103
await executeSlashCommand("image");
104104
await userEvent.keyboard("{Escape}");
105-
await userEvent.click(await waitForSelector(".bn-trailing-block"));
105+
const paragraphs = document.querySelectorAll(PARAGRAPH_SELECTOR);
106+
await userEvent.click(paragraphs[paragraphs.length - 1]);
106107
await insertHeading(1);
107108

108109
await dragAndDropBlock(IMAGE_SELECTOR, H_ONE_BLOCK_SELECTOR, false);
@@ -119,7 +120,8 @@ describe("Check Block Dragging Functionality", () => {
119120
await focusOnEditor();
120121
await executeSlashCommand("image");
121122
await userEvent.keyboard("{Escape}");
122-
await userEvent.click(await waitForSelector(".bn-trailing-block"));
123+
const paragraphs = document.querySelectorAll(PARAGRAPH_SELECTOR);
124+
await userEvent.click(paragraphs[paragraphs.length - 1]);
123125
await insertHeading(1);
124126

125127
await dragAndDropBlock(IMAGE_SELECTOR, H_ONE_BLOCK_SELECTOR, false);
929 Bytes
Loading
644 Bytes
Loading
957 Bytes
Loading

tests/src/end-to-end/images/__snapshots__/createImage.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@
2222
}
2323
}
2424
]
25+
},
26+
{
27+
"type": "blockContainer",
28+
"attrs": {
29+
"id": "1"
30+
},
31+
"content": [
32+
{
33+
"type": "paragraph",
34+
"attrs": {
35+
"backgroundColor": "default",
36+
"textColor": "default",
37+
"textAlignment": "left"
38+
}
39+
}
40+
]
2541
}
2642
]
2743
}

tests/src/end-to-end/images/__snapshots__/embedImage.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@
2222
}
2323
}
2424
]
25+
},
26+
{
27+
"type": "blockContainer",
28+
"attrs": {
29+
"id": "1"
30+
},
31+
"content": [
32+
{
33+
"type": "paragraph",
34+
"attrs": {
35+
"backgroundColor": "default",
36+
"textColor": "default",
37+
"textAlignment": "left"
38+
}
39+
}
40+
]
2541
}
2642
]
2743
}

0 commit comments

Comments
 (0)