@@ -460,20 +460,46 @@ export class Mirror<S extends SchemaType> {
460460 string ,
461461 unknown
462462 > ;
463+ const rootSchema =
464+ this . schema && this . schema . type === "schema"
465+ ? ( this . schema as RootSchemaType <
466+ Record < string , ContainerSchemaType >
467+ > )
468+ : undefined ;
463469 for ( const [ key , value ] of Object . entries ( init ) ) {
464- let container : Container | null = null ;
465- if ( Array . isArray ( value ) ) {
466- container = this . doc . getList ( key ) ;
467- } else if ( typeof value === "string" ) {
468- container = this . doc . getText ( key ) ;
469- } else if ( isObject ( value ) ) {
470- container = this . doc . getMap ( key ) ;
471- }
472- if ( container ) {
473- this . rootPathById . set ( container . id , [ key ] ) ;
474- this . registerContainerWithRegistry ( container . id , undefined ) ;
475- }
470+ const fieldSchema = rootSchema ?. definition [ key ] ;
471+ const containerType =
472+ ( fieldSchema
473+ ? schemaToContainerType ( fieldSchema )
474+ : undefined ) ??
475+ this . inferRootContainerTypeFromInitialValue ( value ) ;
476+ if ( ! containerType ) continue ;
477+
478+ const container = getRootContainerByType (
479+ this . doc ,
480+ key ,
481+ containerType ,
482+ ) ;
483+ this . rootPathById . set ( container . id , [ key ] ) ;
484+ this . registerContainerWithRegistry ( container . id , undefined ) ;
485+ }
486+ }
487+
488+ private inferRootContainerTypeFromInitialValue (
489+ value : unknown ,
490+ ) : ContainerType | undefined {
491+ if ( Array . isArray ( value ) ) {
492+ return this . options . inferOptions ?. defaultMovableList
493+ ? "MovableList"
494+ : "List" ;
495+ }
496+ if ( typeof value === "string" ) {
497+ return "Text" ;
498+ }
499+ if ( isObject ( value ) ) {
500+ return "Map" ;
476501 }
502+ return undefined ;
477503 }
478504
479505 /**
0 commit comments