refactor: clean text enter

This commit is contained in:
lawvs
2022-08-17 19:05:59 +08:00
parent f269990f9b
commit 1d3a285681
2 changed files with 23 additions and 19 deletions
@@ -14,7 +14,7 @@ import { CreateView } from '@toeverything/framework/virgo';
import { BlockContainer } from '../../components/BlockContainer'; import { BlockContainer } from '../../components/BlockContainer';
import { IndentWrapper } from '../../components/IndentWrapper'; import { IndentWrapper } from '../../components/IndentWrapper';
import { TextManage } from '../../components/text-manage'; import { TextManage } from '../../components/text-manage';
import { tabBlock } from '../../utils/indent'; import { dedentBlock, tabBlock } from '../../utils/indent';
interface CreateTextView extends CreateView { interface CreateTextView extends CreateView {
// TODO: need to optimize // TODO: need to optimize
containerClassName?: string; containerClassName?: string;
@@ -115,7 +115,19 @@ export const TextView = ({
return false; return false;
} }
const preParent = await parentBlock.previousSibling(); // The parent block is group block or is the root block.
// Merge block to previous sibling.
//
// - group/root <- parent block
// - text1 <- preNode
// - text2 <- press backspace before target block
// - children
//
// ---
//
// - group/root
// - text1text2 <- merge block to previous sibling
// - children <- children should switch parent block
if ( if (
Protocol.Block.Type.group === parentBlock.type || Protocol.Block.Type.group === parentBlock.type ||
editor.getRootBlockId() === parentBlock.id editor.getRootBlockId() === parentBlock.id
@@ -130,10 +142,7 @@ export const TextView = ({
block.id, block.id,
'end' 'end'
); );
if ( if (!block.blockProvider?.isEmpty()) {
block.getProperty('text').value[0] &&
block.getProperty('text').value[0]?.text !== ''
) {
const value = [ const value = [
...preNode.getProperty('text').value, ...preNode.getProperty('text').value,
...block.getProperty('text').value, ...block.getProperty('text').value,
@@ -145,12 +154,13 @@ export const TextView = ({
await preNode.append(...children); await preNode.append(...children);
await block.remove(); await block.remove();
} else { } else {
// If not pre node, block should be merged to the parent block
// TODO: point does not clear // TODO: point does not clear
await editor.selectionManager.activePreviousNode( await editor.selectionManager.activePreviousNode(
block.id, block.id,
'start' 'start'
); );
if (block.blockProvider.isEmpty()) { if (block.blockProvider?.isEmpty()) {
await block.remove(); await block.remove();
const parentChild = await parentBlock.children(); const parentChild = await parentBlock.children();
if ( if (
@@ -158,6 +168,8 @@ export const TextView = ({
Protocol.Block.Type.group && Protocol.Block.Type.group &&
!parentChild.length !parentChild.length
) { ) {
const preParent =
await parentBlock.previousSibling();
await editor.selectionManager.setSelectedNodesIds( await editor.selectionManager.setSelectedNodesIds(
[preParent?.id ?? editor.getRootBlockId()] [preParent?.id ?? editor.getRootBlockId()]
); );
@@ -198,17 +210,9 @@ export const TextView = ({
await parentBlock.remove(); await parentBlock.remove();
} }
return true; return true;
} else {
const nextNodes = await block.nextSiblings();
for (const nextNode of nextNodes) {
await nextNode.remove();
}
block.append(...nextNodes);
editor.commands.blockCommands.moveBlockAfter(
block.id,
parentBlock.id
);
} }
dedentBlock(editor, block);
return true; return true;
} }
); );
@@ -1,6 +1,6 @@
import { import {
type BlockEditor,
supportChildren, supportChildren,
type BlockEditor,
} from '@toeverything/components/editor-core'; } 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';
@@ -81,7 +81,7 @@ const indentBlock = async (block: TodoAsyncBlock) => {
* └─ [ ] * └─ [ ]
* ``` * ```
*/ */
const dedentBlock = async (editor: BlockEditor, block: AsyncBlock) => { export const dedentBlock = async (editor: BlockEditor, block: AsyncBlock) => {
if (editor.getRootBlockId() === block.id) { if (editor.getRootBlockId() === block.id) {
return false; return false;
} }