fix(editor): behavior when clicking in blank page and backspacing on last group

This commit is contained in:
austaras
2022-08-05 16:07:21 +08:00
committed by Austaras
parent cb86ea8ae5
commit e12deb302f
4 changed files with 34 additions and 19 deletions

View File

@@ -137,7 +137,7 @@ export const TextView: FC<CreateTextView> = ({
!parentChild.length !parentChild.length
) { ) {
await editor.selectionManager.setSelectedNodesIds( await editor.selectionManager.setSelectedNodesIds(
[preParent.id] [preParent?.id ?? editor.getRootBlockId()]
); );
} }
} }

View File

@@ -9,7 +9,7 @@ import {
services, services,
type ReturnUnobserve, type ReturnUnobserve,
} from '@toeverything/datasource/db-service'; } from '@toeverything/datasource/db-service';
import { addNewGroup } from './recast-block'; import { addNewGroup, appendNewGroup } from './recast-block';
import { useIsOnDrag } from './hooks'; import { useIsOnDrag } from './hooks';
interface RenderRootProps { interface RenderRootProps {
@@ -199,24 +199,32 @@ function ScrollBlank({ editor }: { editor: BlockEditor }) {
mouseMoved.current = false; mouseMoved.current = false;
return; 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 // If last block is not a group
// create a group with a empty text // create a group with a empty text
if (lastGroupBlock.type !== 'group') { if (lastRootChildren == null) {
addNewGroup(editor, lastBlock, true); appendNewGroup(editor, rootBlock, true);
return; return;
} }
if (lastGroupBlock.childrenIds.length > 1) { if (
addNewGroup(editor, lastBlock, true); lastRootChildren.type !== Protocol.Block.Type.group ||
lastRootChildren.childrenIds.length > 1
) {
addNewGroup(editor, lastRootChildren, true);
return; return;
} }
// If the **only** block in the group is text and is empty // If the **only** block in the group is text and is empty
// active the text block // active the text block
const theGroupChildBlock = await lastGroupBlock.firstChild(); const theGroupChildBlock = await lastRootChildren.firstChild();
if ( if (
theGroupChildBlock && theGroupChildBlock &&
@@ -229,7 +237,7 @@ function ScrollBlank({ editor }: { editor: BlockEditor }) {
return; return;
} }
// else create a new group // else create a new group
addNewGroup(editor, lastBlock, true); addNewGroup(editor, lastRootChildren, true);
}, },
[editor] [editor]
); );

View File

@@ -367,15 +367,6 @@ export class Editor implements Virgo {
return blockList; 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()) { async getLastBlock(rootBlockId = this.getRootBlockId()) {
const rootBlock = await this.getBlockById(rootBlockId); const rootBlock = await this.getBlockById(rootBlockId);
if (!rootBlock) { if (!rootBlock) {

View File

@@ -185,6 +185,22 @@ export const splitGroup = async (
return newGroupBlock; 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 ( export const addNewGroup = async (
editor: BlockEditor, editor: BlockEditor,
previousBlock: AsyncBlock, previousBlock: AsyncBlock,