From 1d3a285681ad70d7eadb7fa7fe2e35231baafa7c Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Wed, 17 Aug 2022 19:05:59 +0800 Subject: [PATCH] refactor: clean text enter --- .../src/blocks/text/TextView.tsx | 38 ++++++++++--------- .../editor-blocks/src/utils/indent.ts | 4 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/libs/components/editor-blocks/src/blocks/text/TextView.tsx b/libs/components/editor-blocks/src/blocks/text/TextView.tsx index 633595c904..5edf231217 100644 --- a/libs/components/editor-blocks/src/blocks/text/TextView.tsx +++ b/libs/components/editor-blocks/src/blocks/text/TextView.tsx @@ -14,7 +14,7 @@ import { CreateView } from '@toeverything/framework/virgo'; import { BlockContainer } from '../../components/BlockContainer'; import { IndentWrapper } from '../../components/IndentWrapper'; import { TextManage } from '../../components/text-manage'; -import { tabBlock } from '../../utils/indent'; +import { dedentBlock, tabBlock } from '../../utils/indent'; interface CreateTextView extends CreateView { // TODO: need to optimize containerClassName?: string; @@ -115,7 +115,19 @@ export const TextView = ({ 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 ( Protocol.Block.Type.group === parentBlock.type || editor.getRootBlockId() === parentBlock.id @@ -130,10 +142,7 @@ export const TextView = ({ block.id, 'end' ); - if ( - block.getProperty('text').value[0] && - block.getProperty('text').value[0]?.text !== '' - ) { + if (!block.blockProvider?.isEmpty()) { const value = [ ...preNode.getProperty('text').value, ...block.getProperty('text').value, @@ -145,12 +154,13 @@ export const TextView = ({ await preNode.append(...children); await block.remove(); } else { + // If not pre node, block should be merged to the parent block // TODO: point does not clear await editor.selectionManager.activePreviousNode( block.id, 'start' ); - if (block.blockProvider.isEmpty()) { + if (block.blockProvider?.isEmpty()) { await block.remove(); const parentChild = await parentBlock.children(); if ( @@ -158,6 +168,8 @@ export const TextView = ({ Protocol.Block.Type.group && !parentChild.length ) { + const preParent = + await parentBlock.previousSibling(); await editor.selectionManager.setSelectedNodesIds( [preParent?.id ?? editor.getRootBlockId()] ); @@ -198,17 +210,9 @@ export const TextView = ({ await parentBlock.remove(); } 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; } ); diff --git a/libs/components/editor-blocks/src/utils/indent.ts b/libs/components/editor-blocks/src/utils/indent.ts index 7f0a2bad08..2c3eee6719 100644 --- a/libs/components/editor-blocks/src/utils/indent.ts +++ b/libs/components/editor-blocks/src/utils/indent.ts @@ -1,6 +1,6 @@ import { - type BlockEditor, supportChildren, + type BlockEditor, } from '@toeverything/components/editor-core'; import { Protocol } from '@toeverything/datasource/db-service'; 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) { return false; }