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(editor): behavior when clicking in blank page and backspacing on last group

austarase12deb302f74
diff --git a/libs/components/editor-blocks/src/blocks/text/TextView.tsx b/libs/components/editor-blocks/src/blocks/text/TextView.tsx
index d9af8fb3f1..cb408cb189 100644
--- a/libs/components/editor-blocks/src/blocks/text/TextView.tsx
+++ b/libs/components/editor-blocks/src/blocks/text/TextView.tsx
@@ -137,7 +137,7 @@ export const TextView: FC<CreateTextView> = ({
!parentChild.length
) {
await editor.selectionManager.setSelectedNodesIds(
- [preParent.id]
+ [preParent?.id ?? editor.getRootBlockId()]
);
}
}
diff --git a/libs/components/editor-core/src/RenderRoot.tsx b/libs/components/editor-core/src/RenderRoot.tsx
index aed9ebea50..1d08db0b16 100644
--- a/libs/components/editor-core/src/RenderRoot.tsx
+++ b/libs/components/editor-core/src/RenderRoot.tsx
@@ -9,7 +9,7 @@ import {
services,
type ReturnUnobserve,
} from '@toeverything/datasource/db-service';
-import { addNewGroup } from './recast-block';
+import { addNewGroup, appendNewGroup } from './recast-block';
import { useIsOnDrag } from './hooks';
interface RenderRootProps {
@@ -199,24 +199,32 @@ function ScrollBlank({ editor }: { editor: BlockEditor }) {
mouseMoved.current = false;
return;
}
- const lastBlock = await editor.getRootLastChildrenBlock();
+ const rootBlock = await editor.getBlockById(
+ editor.getRootBlockId()
+ );
+ if (!rootBlock) {
+ throw new Error('root block is not found');
+ }
- const lastGroupBlock = await editor.getRootLastChildrenBlock();
+ const lastRootChildren = await rootBlock.lastChild();
// If last block is not a group
// create a group with a empty text
- if (lastGroupBlock.type !== 'group') {
- addNewGroup(editor, lastBlock, true);
+ if (lastRootChildren == null) {
+ appendNewGroup(editor, rootBlock, true);
return;
}
- if (lastGroupBlock.childrenIds.length > 1) {
- addNewGroup(editor, lastBlock, true);
+ if (
+ lastRootChildren.type !== Protocol.Block.Type.group ||
+ lastRootChildren.childrenIds.length > 1
+ ) {
+ addNewGroup(editor, lastRootChildren, true);
return;
}
// If the **only** block in the group is text and is empty
// active the text block
- const theGroupChildBlock = await lastGroupBlock.firstChild();
+ const theGroupChildBlock = await lastRootChildren.firstChild();
if (
theGroupChildBlock &&
@@ -229,7 +237,7 @@ function ScrollBlank({ editor }: { editor: BlockEditor }) {
return;
}
// else create a new group
- addNewGroup(editor, lastBlock, true);
+ addNewGroup(editor, lastRootChildren, true);
},
[editor]
);
diff --git a/libs/components/editor-core/src/editor/editor.ts b/libs/components/editor-core/src/editor/editor.ts
index 8750a566c7..208c4e9ef1 100644
--- a/libs/components/editor-core/src/editor/editor.ts
+++ b/libs/components/editor-core/src/editor/editor.ts
@@ -367,15 +367,6 @@ export class Editor implements Virgo {
return blockList;
}
- async getRootLastChildrenBlock(rootBlockId = this.getRootBlockId()) {
- const rootBlock = await this.getBlockById(rootBlockId);
- if (!rootBlock) {
- throw new Error('root block is not found');
- }
- const lastChildren = await rootBlock.lastChild();
- return lastChildren ?? rootBlock;
- }
-
async getLastBlock(rootBlockId = this.getRootBlockId()) {
const rootBlock = await this.getBlockById(rootBlockId);
if (!rootBlock) {
diff --git a/libs/components/editor-core/src/recast-block/group.ts b/libs/components/editor-core/src/recast-block/group.ts
index b8b25cce01..ae846fc238 100644
--- a/libs/components/editor-core/src/recast-block/group.ts
+++ b/libs/components/editor-core/src/recast-block/group.ts
@@ -185,6 +185,22 @@ export const splitGroup = async (
return newGroupBlock;
};
+export const appendNewGroup = async (
+ editor: BlockEditor,
+ parentBlock: AsyncBlock,
+ active = false
+) => {
+ const newGroupBlock = await createGroupWithEmptyText(editor);
+ await parentBlock.append(newGroupBlock);
+ if (active) {
+ // Active text block
+ await editor.selectionManager.activeNodeByNodeId(
+ newGroupBlock.childrenIds[0]
+ );
+ }
+ return newGroupBlock;
+};
+
export const addNewGroup = async (
editor: BlockEditor,
previousBlock: AsyncBlock,