Merge pull request #265 from toeverything/fix/text-with-root-id

Fix/text with root id
This commit is contained in:
DarkSky
2022-08-16 18:55:19 +08:00
committed by GitHub
2 changed files with 87 additions and 47 deletions
@@ -2,13 +2,11 @@ import { useState } from 'react';
import { CustomText, TextProps } from '@toeverything/components/common';
import {
mergeGroup,
BlockPendantProvider,
RenderBlockChildren,
splitGroup,
supportChildren,
unwrapGroup,
useOnSelect,
BlockPendantProvider,
} from '@toeverything/components/editor-core';
import { styled } from '@toeverything/components/ui';
import { Protocol } from '@toeverything/datasource/db-service';
@@ -69,24 +67,36 @@ export const TextView = ({
const { contentBeforeSelection, contentAfterSelection } = splitContents;
const before = [...contentBeforeSelection.content];
const after = [...contentAfterSelection.content];
const _nextBlockChildren = await block.children();
const _nextBlock = await editor.createBlock('text');
await _nextBlock.setProperty('text', {
const nextBlockChildren = await block.children();
const nextBlock = await editor.createBlock('text');
if (!nextBlock) {
throw new Error('Failed to create text block');
}
await nextBlock.setProperty('text', {
value: after as CustomText[],
});
_nextBlock.append(..._nextBlockChildren);
block.removeChildren();
await block.setProperty('text', {
value: before as CustomText[],
});
await block.after(_nextBlock);
editor.selectionManager.activeNodeByNodeId(_nextBlock.id);
if (editor.getRootBlockId() === block.id) {
// If the block is the root block,
// new block can not append as next sibling,
// all new blocks should be append as children.
await block.insert(0, [nextBlock]);
editor.selectionManager.activeNodeByNodeId(nextBlock.id);
return true;
}
await nextBlock.append(...nextBlockChildren);
await block.removeChildren();
await block.after(nextBlock);
editor.selectionManager.activeNodeByNodeId(nextBlock.id);
return true;
};
const onBackspace: TextProps['handleBackSpace'] = async props => {
return await editor.withSuspend(async () => {
const onBackspace: TextProps['handleBackSpace'] = editor.withBatch(
async props => {
const { isCollAndStart } = props;
if (!isCollAndStart) {
return false;
@@ -95,20 +105,27 @@ export const TextView = ({
await block.setType('text');
return true;
}
if (editor.getRootBlockId() === block.id) {
// Can not delete
return false;
}
const parentBlock = await block.parent();
if (!parentBlock) {
return false;
}
const preParent = await parentBlock.previousSibling();
if (Protocol.Block.Type.group === parentBlock.type) {
if (
Protocol.Block.Type.group === parentBlock.type ||
editor.getRootBlockId() === parentBlock.id
) {
const children = await block.children();
const preNode = await block.physicallyPerviousSibling();
// FIXME support children do not means has textBlock
// TODO: abstract this part of code
if (preNode) {
if (supportChildren(preNode)) {
editor.suspend(true);
await editor.selectionManager.activePreviousNode(
block.id,
'end'
@@ -127,7 +144,6 @@ export const TextView = ({
}
await preNode.append(...children);
await block.remove();
editor.suspend(false);
} else {
// TODO: point does not clear
await editor.selectionManager.activePreviousNode(
@@ -194,8 +210,9 @@ export const TextView = ({
);
}
return true;
});
};
}
);
const handleConvert = async (
toType: string,
options?: Record<string, unknown>