diff --git a/tests/src/end-to-end/keyboardhandlers/keyboardhandlers.test.ts b/tests/src/end-to-end/keyboardhandlers/keyboardhandlers.test.ts index 3213e2fd65..3bfe88970c 100644 --- a/tests/src/end-to-end/keyboardhandlers/keyboardhandlers.test.ts +++ b/tests/src/end-to-end/keyboardhandlers/keyboardhandlers.test.ts @@ -5,7 +5,12 @@ import { ITALIC_BUTTON_SELECTOR, } from "../../utils/const.js"; import { insertHeading, insertParagraph } from "../../utils/copypaste.js"; -import { compareDocToSnapshot, focusOnEditor } from "../../utils/editor.js"; +import { + compareDocToSnapshot, + focusOnEditor, + moveCursorToBlockEnd, + moveCursorToBlockStart, +} from "../../utils/editor.js"; import { executeSlashCommand } from "../../utils/slashmenu.js"; test.describe.configure({ mode: "serial" }); @@ -96,7 +101,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await focusOnEditor(page); await insertHeading(page, 1); - await page.keyboard.press("Control+ArrowLeft"); + await moveCursorToBlockStart(page); await page.keyboard.press("Backspace"); await compareDocToSnapshot(page, "backspaceStartOfBlock.json"); @@ -143,7 +148,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await page.keyboard.press("ArrowUp"); } - await page.keyboard.press("Control+ArrowLeft"); + await moveCursorToBlockStart(page); await page.keyboard.press("Backspace"); await compareDocToSnapshot(page, "backspacePreservesNestedBlocks.json"); @@ -283,6 +288,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await page.keyboard.press("ArrowUp"); await page.keyboard.press("ArrowUp"); + await moveCursorToBlockEnd(page); await page.keyboard.press("Delete"); await compareDocToSnapshot(page, "deleteMultipleChildren.json"); @@ -299,6 +305,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await page.keyboard.press("ArrowUp"); await page.keyboard.press("ArrowUp"); + await moveCursorToBlockEnd(page); await page.keyboard.press("Delete"); await compareDocToSnapshot(page, "deleteNestedChildren.json"); @@ -314,6 +321,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await insertParagraph(page); await page.keyboard.press("ArrowUp"); + await moveCursorToBlockEnd(page); await page.keyboard.press("Delete"); await compareDocToSnapshot(page, "deleteShallowerBlock.json"); @@ -335,8 +343,7 @@ test.describe("Check Keyboard Handlers' Behaviour", () => { await page.keyboard.press("ArrowUp"); await page.keyboard.press("ArrowUp"); - await page.keyboard.press("ControlOrMeta+ArrowLeft"); - await page.keyboard.press("ControlOrMeta+ArrowRight"); + await moveCursorToBlockEnd(page); await page.keyboard.press("Delete"); await compareDocToSnapshot(page, "deleteShallowerBlockWithChildren.json"); diff --git a/tests/src/utils/editor.ts b/tests/src/utils/editor.ts index 24bdb3c53b..c8859f3f8e 100644 --- a/tests/src/utils/editor.ts +++ b/tests/src/utils/editor.ts @@ -46,3 +46,32 @@ export async function compareDocToSnapshot(page: Page, name: string) { const doc = JSON.stringify(await getDoc(page), null, 2); expect(doc).toMatchSnapshot(`${name}.json`); } + +/** + * Programmatically move cursor to end of the current block content. + * This avoids relying on keyboard navigation (ArrowUp/End) which can + * position the cursor incorrectly in WebKit when crossing blocks with + * different indentation levels. + */ +export async function moveCursorToBlockEnd(page: Page) { + await page.evaluate(() => { + const tiptap = (window as any).ProseMirror; + const bnEditor = tiptap.schema.cached.blockNoteEditor; + const block = bnEditor.getTextCursorPosition().block; + bnEditor.setTextCursorPosition(block, "end"); + }); +} + +/** + * Programmatically move cursor to start of the current block content. + * This avoids relying on keyboard navigation which can be unreliable + * in WebKit. + */ +export async function moveCursorToBlockStart(page: Page) { + await page.evaluate(() => { + const tiptap = (window as any).ProseMirror; + const bnEditor = tiptap.schema.cached.blockNoteEditor; + const block = bnEditor.getTextCursorPosition().block; + bnEditor.setTextCursorPosition(block, "start"); + }); +}