mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 09:21:24 +08:00
fix: handle root block when indent
This commit is contained in:
@@ -173,7 +173,7 @@ export const BulletView = ({ block, editor }: CreateView) => {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return tabBlock(block, isShiftKey);
|
return tabBlock(editor, block, isShiftKey);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ export const NumberedView = ({ block, editor }: CreateView) => {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
tabBlock(block, isShiftKey);
|
tabBlock(editor, block, isShiftKey);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ export const TextView = ({
|
|||||||
block.firstCreateFlag = true;
|
block.firstCreateFlag = true;
|
||||||
};
|
};
|
||||||
const onTab: TextProps['handleTab'] = async ({ isShiftKey }) => {
|
const onTab: TextProps['handleTab'] = async ({ isShiftKey }) => {
|
||||||
await tabBlock(block, isShiftKey);
|
await tabBlock(editor, block, isShiftKey);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ export const TodoView = ({ block, editor }: CreateView) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const on_tab: TextProps['handleTab'] = async ({ isShiftKey }) => {
|
const on_tab: TextProps['handleTab'] = async ({ isShiftKey }) => {
|
||||||
await tabBlock(block, isShiftKey);
|
await tabBlock(editor, block, isShiftKey);
|
||||||
return true;
|
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 { Protocol } from '@toeverything/datasource/db-service';
|
||||||
import { AsyncBlock } from '@toeverything/framework/virgo';
|
import { AsyncBlock } from '@toeverything/framework/virgo';
|
||||||
import type { TodoAsyncBlock } from '../blocks/todo/types';
|
import type { TodoAsyncBlock } from '../blocks/todo/types';
|
||||||
@@ -6,14 +9,19 @@ import type { TodoAsyncBlock } from '../blocks/todo/types';
|
|||||||
/**
|
/**
|
||||||
* Is the block in top level
|
* Is the block in top level
|
||||||
*/
|
*/
|
||||||
export const isTopLevelBlock = (parentBlock: AsyncBlock): boolean => {
|
export const isTopLevelBlock = (
|
||||||
|
editor: BlockEditor,
|
||||||
|
block: AsyncBlock
|
||||||
|
): boolean => {
|
||||||
return (
|
return (
|
||||||
parentBlock.type === Protocol.Block.Type.group ||
|
editor.getRootBlockId() === block.id ||
|
||||||
parentBlock.type === Protocol.Block.Type.page
|
block.type === Protocol.Block.Type.group ||
|
||||||
|
block.type === Protocol.Block.Type.page
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Move down
|
||||||
* @returns true if indent is success
|
* @returns true if indent is success
|
||||||
* @example
|
* @example
|
||||||
* ```
|
* ```
|
||||||
@@ -31,7 +39,6 @@ export const isTopLevelBlock = (parentBlock: AsyncBlock): boolean => {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
const indentBlock = async (block: TodoAsyncBlock) => {
|
const indentBlock = async (block: TodoAsyncBlock) => {
|
||||||
// Move down
|
|
||||||
const previousBlock = await block.previousSibling();
|
const previousBlock = await block.previousSibling();
|
||||||
|
|
||||||
if (!previousBlock || !supportChildren(previousBlock)) {
|
if (!previousBlock || !supportChildren(previousBlock)) {
|
||||||
@@ -57,6 +64,7 @@ const indentBlock = async (block: TodoAsyncBlock) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Move up
|
||||||
* @returns true if dedent is success
|
* @returns true if dedent is success
|
||||||
* @example
|
* @example
|
||||||
* ```
|
* ```
|
||||||
@@ -73,13 +81,15 @@ const indentBlock = async (block: TodoAsyncBlock) => {
|
|||||||
* └─ [ ]
|
* └─ [ ]
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
const dedentBlock = async (block: AsyncBlock) => {
|
const dedentBlock = async (editor: BlockEditor, block: AsyncBlock) => {
|
||||||
// Move up
|
if (editor.getRootBlockId() === block.id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let parentBlock = await block.parent();
|
let parentBlock = await block.parent();
|
||||||
if (!parentBlock) {
|
if (!parentBlock) {
|
||||||
throw new Error('Failed to dedent block! Parent block not found!');
|
throw new Error('Failed to dedent block! Parent block not found!');
|
||||||
}
|
}
|
||||||
if (isTopLevelBlock(parentBlock)) {
|
if (isTopLevelBlock(editor, parentBlock)) {
|
||||||
// Top, do nothing
|
// Top, do nothing
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -111,9 +121,13 @@ const dedentBlock = async (block: AsyncBlock) => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const tabBlock = async (block: AsyncBlock, isShiftKey: boolean) => {
|
export const tabBlock = async (
|
||||||
|
editor: BlockEditor,
|
||||||
|
block: AsyncBlock,
|
||||||
|
isShiftKey: boolean
|
||||||
|
) => {
|
||||||
if (isShiftKey) {
|
if (isShiftKey) {
|
||||||
return await dedentBlock(block);
|
return await dedentBlock(editor, block);
|
||||||
} else {
|
} else {
|
||||||
return await indentBlock(block);
|
return await indentBlock(block);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user