Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(core): editor blur unexpectedly when clicking blank area (#10501)

L-Sun90b37ce65c7a
diff --git a/blocksuite/affine/block-note/src/commands/focus-block-end.ts b/blocksuite/affine/block-note/src/commands/focus-block-end.ts
index 8a6b1946a2..a8e5906c71 100644
--- a/blocksuite/affine/block-note/src/commands/focus-block-end.ts
+++ b/blocksuite/affine/block-note/src/commands/focus-block-end.ts
@@ -4,14 +4,23 @@ import {
TextSelection,
} from '@blocksuite/block-std';
+/**
+ * Focus the end of the block
+ * @param focusBlock - The block to focus
+ * @param force - If set to true, the selection will be cleared.
+ * It is useful when the selection is same.
+ */
export const focusBlockEnd: Command<{
focusBlock?: BlockComponent;
+ force?: boolean;
}> = (ctx, next) => {
- const { focusBlock, std } = ctx;
+ const { focusBlock, force, std } = ctx;
if (!focusBlock || !focusBlock.model.text) return;
const { selection } = std;
+ if (force) selection.clear();
+
selection.setGroup('note', [
selection.create(TextSelection, {
from: {
diff --git a/packages/frontend/core/src/blocksuite/block-suite-editor/blocksuite-editor-container.tsx b/packages/frontend/core/src/blocksuite/block-suite-editor/blocksuite-editor-container.tsx
index 2eacc05a4a..ed29ed1c64 100644
--- a/packages/frontend/core/src/blocksuite/block-suite-editor/blocksuite-editor-container.tsx
+++ b/packages/frontend/core/src/blocksuite/block-suite-editor/blocksuite-editor-container.tsx
@@ -151,6 +151,7 @@ export const BlocksuiteEditorContainer = forwardRef<
const focusBlock = std.view.getBlock(lastBlock.id) ?? undefined;
std.command.exec(focusBlockEnd, {
focusBlock,
+ force: true,
});
return;
}
diff --git a/tests/affine-local/e2e/blocksuite/editor.spec.ts b/tests/affine-local/e2e/blocksuite/editor.spec.ts
index dc958d8c82..753adba2ed 100644
--- a/tests/affine-local/e2e/blocksuite/editor.spec.ts
+++ b/tests/affine-local/e2e/blocksuite/editor.spec.ts
@@ -1,4 +1,5 @@
import { test } from '@affine-test/kit/playwright';
+import { locateEditorContainer } from '@affine-test/kit/utils/editor';
import { openHomePage } from '@affine-test/kit/utils/load-page';
import {
addDatabase,
@@ -82,7 +83,15 @@ test('append paragraph when click editor gap', async ({ page }) => {
await page.locator('[data-testid=page-editor-blank]').click();
expect(await paragraph.count()).toBe(numParagraphs + 1);
- // click the gap again, should not append another paragraph
await page.locator('[data-testid=page-editor-blank]').click();
- expect(await paragraph.count()).toBe(numParagraphs + 1);
+ expect(
+ await paragraph.count(),
+ 'click the gap again, should not append another paragraph'
+ ).toBe(numParagraphs + 1);
+
+ const editorContainer = locateEditorContainer(page);
+ expect(
+ await editorContainer.evaluate(el => el.contains(document.activeElement)),
+ 'editor should should keep being focused'
+ ).toBe(true);
});