diff --git a/packages/core/src/extensions/Blocks/nodes/BlockContainer.ts b/packages/core/src/extensions/Blocks/nodes/BlockContainer.ts index 3e4b0ffb29..3120ba8253 100644 --- a/packages/core/src/extensions/Blocks/nodes/BlockContainer.ts +++ b/packages/core/src/extensions/Blocks/nodes/BlockContainer.ts @@ -281,10 +281,19 @@ export const BlockContainer = Node.create({ } // Deletes next block and adds its text content to the nearest previous block. - // TODO: Use slices. + if (dispatch) { - state.tr.deleteRange(startPos, startPos + contentNode.nodeSize); - state.tr.insertText(contentNode.textContent, prevBlockEndPos - 1); + dispatch( + state.tr + .deleteRange(startPos, startPos + contentNode.nodeSize) + .replace( + prevBlockEndPos - 1, + startPos, + new Slice(contentNode.content, 0, 0) + ) + .scrollIntoView() + ); + state.tr.setSelection( new TextSelection(state.doc.resolve(prevBlockEndPos - 1)) ); diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts index 13a9c086db..0a03efd59e 100644 --- a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts @@ -6,7 +6,7 @@ import { H_TWO_BLOCK_SELECTOR, } from "../../utils/const"; import { compareDocToSnapshot, focusOnEditor } from "../../utils/editor"; -import { insertHeading } from "../../utils/copypaste"; +import { insertHeading, insertParagraph } from "../../utils/copypaste"; test.describe.configure({ mode: "serial" }); @@ -72,4 +72,58 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await compareDocToSnapshot(page, "enterPreservesNestedBlocks.json"); }); + test("Check Backspace at the start of a block", async ({ page }) => { + await focusOnEditor(page); + await insertHeading(page, 1); + + await page.keyboard.press("ArrowUp"); + await page.keyboard.press("Control+ArrowLeft"); + await page.keyboard.press("Backspace"); + + await compareDocToSnapshot(page, "backspaceStartOfBlock.json"); + }); + test("Check Backspace preserves marks", async ({ page }) => { + await focusOnEditor(page); + await insertParagraph(page); + await insertParagraph(page); + + await page.keyboard.press("ArrowUp"); + await page.keyboard.press("Control+ArrowLeft"); + + for (let i = 0; i < 2; i++) { + await page.keyboard.press("ArrowRight"); + } + + for (let i = 0; i < 5; i++) { + await page.keyboard.press("Shift+ArrowRight"); + } + + await page.locator(ITALIC_BUTTON_SELECTOR).click(); + await page.waitForTimeout(500); + + await page.keyboard.press("ArrowLeft"); + await page.keyboard.press("Control+ArrowLeft"); + await page.keyboard.press("Backspace"); + + await compareDocToSnapshot(page, "backspacePreservesMarks.json"); + }); + test("Check Backspace preserves nested blocks", async ({ page }) => { + await focusOnEditor(page); + await insertParagraph(page); + await insertParagraph(page); + 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("Control+ArrowLeft"); + await page.keyboard.press("Backspace"); + + await compareDocToSnapshot(page, "backspacePreservesNestedBlocks.json"); + }); }); diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-chromium-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-chromium-linux.json new file mode 100644 index 0000000000..242f82807d --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-chromium-linux.json @@ -0,0 +1,61 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphPa" + }, + { + "type": "text", + "marks": [ + { + "type": "italic" + } + ], + "text": "ragra" + }, + { + "type": "text", + "text": "ph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-firefox-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-firefox-linux.json new file mode 100644 index 0000000000..242f82807d --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-firefox-linux.json @@ -0,0 +1,61 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphPa" + }, + { + "type": "text", + "marks": [ + { + "type": "italic" + } + ], + "text": "ragra" + }, + { + "type": "text", + "text": "ph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-webkit-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-webkit-linux.json new file mode 100644 index 0000000000..242f82807d --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesMarks-json-webkit-linux.json @@ -0,0 +1,61 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphPa" + }, + { + "type": "text", + "marks": [ + { + "type": "italic" + } + ], + "text": "ragra" + }, + { + "type": "text", + "text": "ph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-chromium-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-chromium-linux.json new file mode 100644 index 0000000000..98f36be4ea --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-chromium-linux.json @@ -0,0 +1,97 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphParagraph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + }, + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "3", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "4", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-firefox-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-firefox-linux.json new file mode 100644 index 0000000000..98f36be4ea --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-firefox-linux.json @@ -0,0 +1,97 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphParagraph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + }, + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "3", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "4", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-webkit-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-webkit-linux.json new file mode 100644 index 0000000000..98f36be4ea --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspacePreservesNestedBlocks-json-webkit-linux.json @@ -0,0 +1,97 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "ParagraphParagraph" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "2", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + }, + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "3", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Paragraph" + } + ] + } + ] + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "4", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-chromium-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-chromium-linux.json new file mode 100644 index 0000000000..f5a1348948 --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-chromium-linux.json @@ -0,0 +1,48 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Heading" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "1", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-firefox-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-firefox-linux.json new file mode 100644 index 0000000000..f5a1348948 --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-firefox-linux.json @@ -0,0 +1,48 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Heading" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "1", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-webkit-linux.json b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-webkit-linux.json new file mode 100644 index 0000000000..f5a1348948 --- /dev/null +++ b/tests/end-to-end/keyboardhandlers/keyboardhandlers.test.ts-snapshots/backspaceStartOfBlock-json-webkit-linux.json @@ -0,0 +1,48 @@ +{ + "type": "doc", + "content": [ + { + "type": "blockGroup", + "content": [ + { + "type": "blockContainer", + "attrs": { + "id": "0", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + }, + "content": [ + { + "type": "text", + "text": "Heading" + } + ] + } + ] + }, + { + "type": "blockContainer", + "attrs": { + "id": "1", + "textColor": "default", + "backgroundColor": "default" + }, + "content": [ + { + "type": "paragraph", + "attrs": { + "textAlignment": "left" + } + } + ] + } + ] + } + ] +} \ No newline at end of file