diff --git a/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts b/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts index 57ec776d3a..654fbfdeba 100644 --- a/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +++ b/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest"; import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js"; import { setupTestEnv } from "../../setupTestEnv.js"; -import { mergeBlocksCommand } from "./mergeBlocks.js"; +import { getParentBlockInfo, mergeBlocksCommand } from "./mergeBlocks.js"; const getEditor = setupTestEnv(); @@ -77,6 +77,20 @@ describe("Test mergeBlocks", () => { expect(anchorIsAtOldFirstBlockEndPos).toBeTruthy(); }); + it("getParentBlockInfo returns undefined for top-level block", () => { + getEditor().setTextCursorPosition("paragraph-0"); + + const beforePos = getPosBeforeSelectedBlock(); + const doc = getEditor()._tiptapEditor.state.doc; + const $pos = doc.resolve(beforePos); + + expect($pos.depth - 1).toBeLessThan(1); + + const result = getParentBlockInfo(doc, beforePos); + + expect(result).toBeUndefined(); + }); + // We expect a no-op for each of the remaining tests as merging should only // happen for blocks which both have inline content. We also expect // `mergeBlocks` to return false as TipTap commands should do that instead of diff --git a/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts b/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts index 2e9a58d70f..ce1a9455db 100644 --- a/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +++ b/packages/core/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts @@ -16,6 +16,11 @@ export const getParentBlockInfo = ( ): BlockInfo | undefined => { const $pos = doc.resolve(beforePos); const depth = $pos.depth - 1; + + if (depth < 1) { + return undefined; + } + const parentBeforePos = $pos.before(depth); const parentNode = doc.resolve(parentBeforePos).nodeAfter;