fix: handle root block when indent

This commit is contained in:
lawvs
2022-08-12 16:58:54 +08:00
parent bdc61ed02e
commit 42f02aed5c
5 changed files with 28 additions and 14 deletions
@@ -173,7 +173,7 @@ export const BulletView = ({ block, editor }: CreateView) => {
}
return true;
} else {
return tabBlock(block, isShiftKey);
return tabBlock(editor, block, isShiftKey);
}
};
@@ -167,7 +167,7 @@ export const NumberedView = ({ block, editor }: CreateView) => {
}
return true;
} else {
tabBlock(block, isShiftKey);
tabBlock(editor, block, isShiftKey);
return false;
}
};
@@ -211,7 +211,7 @@ export const TextView = ({
block.firstCreateFlag = true;
};
const onTab: TextProps['handleTab'] = async ({ isShiftKey }) => {
await tabBlock(block, isShiftKey);
await tabBlock(editor, block, isShiftKey);
return true;
};
@@ -100,7 +100,7 @@ export const TodoView = ({ block, editor }: CreateView) => {
};
const on_tab: TextProps['handleTab'] = async ({ isShiftKey }) => {
await tabBlock(block, isShiftKey);
await tabBlock(editor, block, isShiftKey);
return true;
};
@@ -1,4 +1,7 @@
import { supportChildren } from '@toeverything/components/editor-core';
import {
type BlockEditor,
supportChildren,
} from '@toeverything/components/editor-core';
import { Protocol } from '@toeverything/datasource/db-service';
import { AsyncBlock } from '@toeverything/framework/virgo';
import type { TodoAsyncBlock } from '../blocks/todo/types';
@@ -6,14 +9,19 @@ import type { TodoAsyncBlock } from '../blocks/todo/types';
/**
* Is the block in top level
*/
export const isTopLevelBlock = (parentBlock: AsyncBlock): boolean => {
export const isTopLevelBlock = (
editor: BlockEditor,
block: AsyncBlock
): boolean => {
return (
parentBlock.type === Protocol.Block.Type.group ||
parentBlock.type === Protocol.Block.Type.page
editor.getRootBlockId() === block.id ||
block.type === Protocol.Block.Type.group ||
block.type === Protocol.Block.Type.page
);
};
/**
* Move down
* @returns true if indent is success
* @example
* ```
@@ -31,7 +39,6 @@ export const isTopLevelBlock = (parentBlock: AsyncBlock): boolean => {
* ```
*/
const indentBlock = async (block: TodoAsyncBlock) => {
// Move down
const previousBlock = await block.previousSibling();
if (!previousBlock || !supportChildren(previousBlock)) {
@@ -57,6 +64,7 @@ const indentBlock = async (block: TodoAsyncBlock) => {
};
/**
* Move up
* @returns true if dedent is success
* @example
* ```
@@ -73,13 +81,15 @@ const indentBlock = async (block: TodoAsyncBlock) => {
* [ ]
* ```
*/
const dedentBlock = async (block: AsyncBlock) => {
// Move up
const dedentBlock = async (editor: BlockEditor, block: AsyncBlock) => {
if (editor.getRootBlockId() === block.id) {
return false;
}
let parentBlock = await block.parent();
if (!parentBlock) {
throw new Error('Failed to dedent block! Parent block not found!');
}
if (isTopLevelBlock(parentBlock)) {
if (isTopLevelBlock(editor, parentBlock)) {
// Top, do nothing
return false;
}
@@ -111,9 +121,13 @@ const dedentBlock = async (block: AsyncBlock) => {
return true;
};
export const tabBlock = async (block: AsyncBlock, isShiftKey: boolean) => {
export const tabBlock = async (
editor: BlockEditor,
block: AsyncBlock,
isShiftKey: boolean
) => {
if (isShiftKey) {
return await dedentBlock(block);
return await dedentBlock(editor, block);
} else {
return await indentBlock(block);
}