Fix/issue 271 (#371)

* fix: issue #271

* chore: clean
This commit is contained in:
Whitewater
2022-09-06 16:01:26 +08:00
committed by GitHub
parent 2867973ee6
commit b48d2c1393
3 changed files with 32 additions and 17 deletions

View File

@@ -132,25 +132,31 @@ export const NumberedView = ({ block, editor }: CreateView) => {
// Move children to new block // Move children to new block
const children = await block.children(); const children = await block.children();
await block.removeChildren();
const next_node = await editor.createBlock( const nextBlock = await editor.createBlock(
Protocol.Block.Type.numbered Protocol.Block.Type.numbered
); );
if (!next_node) { if (!nextBlock) {
throw new Error('Failed to create todo block'); throw new Error('Failed to create todo block');
} }
await next_node.append(...children); if (editor.getRootBlockId() === block.id) {
await next_node.setProperties({ // 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 block.removeChildren();
await nextBlock.append(...children);
await nextBlock.setProperties({
text: { value: after } as ContentColumnValue, text: { value: after } as ContentColumnValue,
}); });
await block.setProperties({ await block.setProperties({
text: { value: before } as ContentColumnValue, text: { value: before } as ContentColumnValue,
}); });
await block.after(next_node); await block.after(nextBlock);
editor.selectionManager.activeNodeByNodeId(nextBlock.id);
editor.selectionManager.activeNodeByNodeId(next_node.id);
return true; return true;
}; };
const on_tab: TextProps['handleTab'] = async ({ isShiftKey }) => { const on_tab: TextProps['handleTab'] = async ({ isShiftKey }) => {

View File

@@ -88,13 +88,21 @@ export const TodoView = ({ block, editor }: CreateView) => {
Boolean Boolean
) as AsyncBlock[]; ) as AsyncBlock[];
const next_node = await editor.createBlock(Protocol.Block.Type.todo); const nextBlock = await editor.createBlock(Protocol.Block.Type.todo);
if (!next_node) { if (!nextBlock) {
throw new Error('Failed to create todo block'); throw new Error('Failed to create todo block');
} }
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 block.removeChildren(); await block.removeChildren();
await next_node.append(...children); await nextBlock.append(...children);
await next_node.setProperties({ await nextBlock.setProperties({
text: { value: after } as ContentColumnValue, text: { value: after } as ContentColumnValue,
}); });
@@ -102,9 +110,9 @@ export const TodoView = ({ block, editor }: CreateView) => {
text: { value: before } as ContentColumnValue, text: { value: before } as ContentColumnValue,
collapsed: { value: false }, collapsed: { value: false },
}); });
await block.after(next_node); await block.after(nextBlock);
editor.selectionManager.activeNodeByNodeId(next_node.id); editor.selectionManager.activeNodeByNodeId(nextBlock.id);
return true; return true;
}; };

View File

@@ -1,9 +1,10 @@
import { genErrorObj } from '@toeverything/utils'; import { genErrorObj } from '@toeverything/utils';
import { createContext, PropsWithChildren, useContext } from 'react'; import type { ComponentType, PropsWithChildren } from 'react';
import { createContext, useContext } from 'react';
import { RenderBlockProps } from './RenderBlock'; import { RenderBlockProps } from './RenderBlock';
type BlockRenderProps = { type BlockRenderProps = {
blockRender: (args: RenderBlockProps) => JSX.Element; blockRender: ComponentType<RenderBlockProps>;
}; };
export const BlockRenderContext = createContext<BlockRenderProps>( export const BlockRenderContext = createContext<BlockRenderProps>(