diff --git a/blocksuite/affine/blocks/paragraph/src/utils/merge-with-prev.ts b/blocksuite/affine/blocks/paragraph/src/utils/merge-with-prev.ts index e9b463408..4e6daf46a 100644 --- a/blocksuite/affine/blocks/paragraph/src/utils/merge-with-prev.ts +++ b/blocksuite/affine/blocks/paragraph/src/utils/merge-with-prev.ts @@ -8,6 +8,7 @@ import { EdgelessTextBlockModel, ImageBlockModel, ListBlockModel, + NoteBlockModel, ParagraphBlockModel, type RootBlockModel, } from '@blocksuite/affine-model'; @@ -19,7 +20,6 @@ import { EMBED_BLOCK_MODEL_LIST } from '@blocksuite/affine-shared/consts'; import type { ExtendedModel } from '@blocksuite/affine-shared/types'; import { focusTitle, - getDocTitleInlineEditor, getPrevContentBlock, matchModels, } from '@blocksuite/affine-shared/utils'; @@ -122,41 +122,39 @@ function handleNoPreviousSibling(editorHost: EditorHost, model: ExtendedModel) { const text = model.text; const parent = doc.getParent(model); if (!parent) return false; - const titleEditor = getDocTitleInlineEditor(editorHost); - // Probably no title, e.g. in edgeless mode - if (!titleEditor) { - if ( - matchModels(parent, [EdgelessTextBlockModel]) || - model.children.length > 0 - ) { + + if (matchModels(parent, [NoteBlockModel]) && parent.isPageBlock()) { + const rootModel = model.store.root as RootBlockModel; + const title = rootModel.props.title; + + doc.captureSync(); + let textLength = 0; + if (text) { + textLength = text.length; + title.join(text); + } + + // Preserve at least one block to be able to focus on container click + if (doc.getNext(model) || model.children.length > 0) { doc.deleteBlock(model, { bringChildrenTo: parent, }); - return true; + } else { + text?.clear(); } - return false; + focusTitle(editorHost, title.length - textLength); + return true; } - const rootModel = model.store.root as RootBlockModel; - const title = rootModel.props.title; - - doc.captureSync(); - let textLength = 0; - if (text) { - textLength = text.length; - title.join(text); - } - - // Preserve at least one block to be able to focus on container click - if (doc.getNext(model) || model.children.length > 0) { - const parent = doc.getParent(model); - if (!parent) return false; + if ( + matchModels(parent, [EdgelessTextBlockModel]) || + model.children.length > 0 + ) { doc.deleteBlock(model, { bringChildrenTo: parent, }); - } else { - text?.clear(); + return true; } - focusTitle(editorHost, title.length - textLength); - return true; + + return false; } diff --git a/tests/affine-local/e2e/blocksuite/edgeless/note.spec.ts b/tests/affine-local/e2e/blocksuite/edgeless/note.spec.ts index d0038ed31..398335d98 100644 --- a/tests/affine-local/e2e/blocksuite/edgeless/note.spec.ts +++ b/tests/affine-local/e2e/blocksuite/edgeless/note.spec.ts @@ -7,6 +7,7 @@ import { getEdgelessSelectedIds, getPageMode, getSelectedXYWH, + isDocTitleFocused, locateEditorContainer, locateModeSwitchButton, locateToolbar, @@ -427,6 +428,14 @@ test.describe('note block rendering', () => { 'should show collapsed content when dragging is finished' ).toHaveCSS('overflow-y', 'visible'); }); + + test('cursor should not jump to page block title from note block', async ({ + page, + }) => { + await createEdgelessNoteBlock(page, [50, 50]); + await pressBackspace(page); + expect(await isDocTitleFocused(page)).toBeFalsy(); + }); }); test('should convert note block to linked doc when clicking turn into linked doc button', async ({ diff --git a/tests/kit/src/utils/editor.ts b/tests/kit/src/utils/editor.ts index dcabf2b4f..1116f9d0d 100644 --- a/tests/kit/src/utils/editor.ts +++ b/tests/kit/src/utils/editor.ts @@ -69,6 +69,14 @@ export function locateDocTitle(page: Page, editorIndex = 0) { return locateEditorContainer(page, editorIndex).locator('doc-title'); } +export function isDocTitleFocused(page: Page, editorIndex = 0) { + return locateDocTitle(page, editorIndex) + .locator('.inline-editor') + .evaluate(inlineEditor => { + return document.activeElement === inlineEditor; + }); +} + export async function focusDocTitle(page: Page, editorIndex = 0) { await locateDocTitle(page, editorIndex).locator('.inline-editor').focus(); }