@@ -58,73 +58,48 @@ export class DocCRUD {
5858 ) ;
5959 }
6060
61+ const hasBlock = this . _yBlocks . has ( id ) ;
62+ if ( hasBlock ) {
63+ throw new BlockSuiteError (
64+ ErrorCode . ModelCRUDError ,
65+ `Should not add existing block: ${ id } `
66+ ) ;
67+ }
68+
6169 const parentFlavour = parent
6270 ? this . _yBlocks . get ( parent ) ?. get ( 'sys:flavour' )
6371 : undefined ;
6472
6573 this . _schema . validate ( flavour , parentFlavour as string ) ;
6674
67- const hasBlock = this . _yBlocks . has ( id ) ;
75+ const yBlock = new Y . Map ( ) as YBlock ;
76+ this . _yBlocks . set ( id , yBlock ) ;
6877
69- if ( hasBlock ) {
70- const yBlock = this . _yBlocks . get ( id ) ;
71- const existedParent = this . getParent ( id ) ;
72- if ( yBlock && existedParent ) {
73- const yParent = this . _yBlocks . get ( existedParent ) as YBlock ;
74- const yParentChildren = yParent . get ( 'sys:children' ) as Y . Array < string > ;
75- const index = yParentChildren . toArray ( ) . indexOf ( id ) ;
76- yParentChildren . delete ( index , 1 ) ;
77- if (
78- parentIndex != null &&
79- index != null &&
80- existedParent === parent &&
81- index < parentIndex
82- ) {
83- parentIndex -- ;
84- }
85- const props = {
86- ...initialProps ,
87- } ;
88- delete props . id ;
89- delete props . flavour ;
90- delete props . children ;
78+ const version = schema . version ;
79+ const children = (
80+ initialProps . children as undefined | ( string | BlockModel ) [ ]
81+ ) ?. map ( child => ( typeof child === 'string' ? child : child . id ) ) ;
9182
92- Object . entries ( props ) . forEach ( ( [ key , value ] ) => {
93- if ( value === undefined ) return ;
83+ yBlock . set ( 'sys:id' , id ) ;
84+ yBlock . set ( 'sys:flavour' , flavour ) ;
85+ yBlock . set ( 'sys:version' , version ) ;
86+ yBlock . set ( 'sys:children' , Y . Array . from ( children ?? [ ] ) ) ;
9487
95- yBlock . set ( `prop:${ key } ` , native2Y ( value ) ) ;
96- } ) ;
97- }
98- } else {
99- const yBlock = new Y . Map ( ) as YBlock ;
100- this . _yBlocks . set ( id , yBlock ) ;
101-
102- const version = schema . version ;
103- const children = (
104- initialProps . children as undefined | ( string | BlockModel ) [ ]
105- ) ?. map ( child => ( typeof child === 'string' ? child : child . id ) ) ;
106-
107- yBlock . set ( 'sys:id' , id ) ;
108- yBlock . set ( 'sys:flavour' , flavour ) ;
109- yBlock . set ( 'sys:version' , version ) ;
110- yBlock . set ( 'sys:children' , Y . Array . from ( children ?? [ ] ) ) ;
111-
112- const defaultProps = schema . model . props ?.( internalPrimitives ) ?? { } ;
113- const props = {
114- ...defaultProps ,
115- ...initialProps ,
116- } ;
117-
118- delete props . id ;
119- delete props . flavour ;
120- delete props . children ;
121-
122- Object . entries ( props ) . forEach ( ( [ key , value ] ) => {
123- if ( value === undefined ) return ;
124-
125- yBlock . set ( `prop:${ key } ` , native2Y ( value ) ) ;
126- } ) ;
127- }
88+ const defaultProps = schema . model . props ?.( internalPrimitives ) ?? { } ;
89+ const props = {
90+ ...defaultProps ,
91+ ...initialProps ,
92+ } ;
93+
94+ delete props . id ;
95+ delete props . flavour ;
96+ delete props . children ;
97+
98+ Object . entries ( props ) . forEach ( ( [ key , value ] ) => {
99+ if ( value === undefined ) return ;
100+
101+ yBlock . set ( `prop:${ key } ` , native2Y ( value ) ) ;
102+ } ) ;
128103
129104 const parentId =
130105 parent ?? ( schema . model . role === 'root' ? null : this . root ) ;
@@ -355,14 +330,12 @@ export class DocCRUD {
355330 return ;
356331 }
357332
358- const targetIndex = targetParentChildren
333+ let targetIndex = targetParentChildren
359334 . toArray ( )
360335 . findIndex ( id => id === targetSibling ) ;
361336 if ( targetIndex === - 1 ) {
362- throw new BlockSuiteError (
363- ErrorCode . ModelCRUDError ,
364- 'Target sibling not found'
365- ) ;
337+ console . error ( 'Target sibling not found, just insert to the end' ) ;
338+ targetIndex = targetParentChildren . length ;
366339 }
367340 insertIndex = shouldInsertBeforeSibling
368341 ? targetIndex
0 commit comments