@@ -5,7 +5,8 @@ import { JSONSchema4 } from 'json-schema';
55import { get as _get , isEqual as _isEqual , isObject as _isObject } from 'lodash' ;
66import { isRefNode } from '../utils/guards' ;
77import { getNodeMetadata , metadataStore } from './metadata' ;
8- import { populateTree } from './populateTree' ;
8+ import { canStepIn } from './utils/canStepIn' ;
9+ import { populateTree } from './utils/populateTree' ;
910
1011export type SchemaTreeRefDereferenceFn = ( path : JsonPath , schema : JSONSchema4 ) => Optional < JSONSchema4 > ;
1112
@@ -36,7 +37,7 @@ export class SchemaTree extends Tree {
3637 const expanded = { } ;
3738 populateTree ( this . schema , this . root , 0 , [ ] , {
3839 mergeAllOf : this . mergeAllOf ,
39- onNode : ( node , parentTreeNode , level ) : boolean => {
40+ onNode : ( fragment , node , parentTreeNode , level ) : boolean => {
4041 if ( isRefNode ( node ) && node . $ref !== null && isLocalRef ( node . $ref ) ) {
4142 expanded [ node . id ] = false ;
4243 }
@@ -51,27 +52,34 @@ export class SchemaTree extends Tree {
5152 this . invalidate ( ) ;
5253 }
5354
54- public populateTreeFragment ( parent : TreeListParentNode , schema : JSONSchema4 , path : JsonPath ) {
55+ public populateTreeFragment ( parent : TreeListParentNode , schema : JSONSchema4 , path : JsonPath , stepIn : boolean ) {
5556 const initialLevel = Tree . getLevel ( parent ) ;
5657 const artificialRoot = Tree . createArtificialRoot ( ) ;
5758 populateTree ( schema , artificialRoot , initialLevel , path , {
5859 mergeAllOf : this . mergeAllOf ,
59- onNode : ( node , parentTreeNode , level ) => level <= this . expandedDepth + 1 || level <= initialLevel + 1 ,
60+ onNode : ( fragment , node , parentTreeNode , level ) => {
61+ if ( level <= this . expandedDepth || level <= initialLevel ) return true ;
62+ return stepIn && level <= initialLevel + 1 && canStepIn ( getNodeMetadata ( parentTreeNode ) . schema ) ;
63+ } ,
6064 } ) ;
6165
6266 if ( artificialRoot . children . length === 0 ) {
6367 throw new Error ( `Could not expand node ${ path . join ( '.' ) } ` ) ;
6468 }
6569
66- // todo: improve walk, i.e. add stepIn so that this is not required
70+ this . insertTreeFragment ( stepIn ? this . stepIn ( artificialRoot , parent ) : artificialRoot . children , parent ) ;
71+ }
72+
73+ protected stepIn ( root : TreeListParentNode , parent : TreeListParentNode ) {
6774 if (
68- 'children' in artificialRoot . children [ 0 ] &&
69- _isEqual ( getNodeMetadata ( parent ) . path , getNodeMetadata ( artificialRoot . children [ 0 ] ) . path )
75+ root . children . length > 0 &&
76+ 'children' in root . children [ 0 ] &&
77+ _isEqual ( getNodeMetadata ( parent ) . path , getNodeMetadata ( root . children [ 0 ] ) . path )
7078 ) {
71- this . insertTreeFragment ( artificialRoot . children [ 0 ] . children , parent ) ;
72- } else {
73- this . insertTreeFragment ( artificialRoot . children , parent ) ;
79+ return root . children [ 0 ] . children ;
7480 }
81+
82+ return root . children ;
7583 }
7684
7785 public unwrap ( node : TreeListParentNode ) {
@@ -82,16 +90,15 @@ export class SchemaTree extends Tree {
8290 const metadata = getNodeMetadata ( node ) ;
8391 const { path, schemaNode, schema } = metadata ;
8492 if ( ! isRefNode ( schemaNode ) ) {
85- this . populateTreeFragment ( node , schema , path ) ;
93+ this . populateTreeFragment ( node , schema , path , true ) ;
8694 } else if ( schemaNode . $ref !== null ) {
8795 const refPath = pointerToPath ( schemaNode . $ref ) ;
8896 const schemaFragment = this . resolveRef ? this . resolveRef ( refPath , this . schema ) : _get ( this . schema , refPath ) ;
8997 if ( ! _isObject ( schemaFragment ) ) {
9098 throw new ReferenceError ( `Could not dereference ${ refPath . join ( '.' ) } ` ) ;
9199 }
92100
93- this . populateTreeFragment ( node , schemaFragment , path ) ;
94- metadata . schema = schemaFragment ;
101+ this . populateTreeFragment ( node , schemaFragment , path , false ) ;
95102 } else {
96103 throw new Error ( `I do know not how not expand node ${ path . join ( '.' ) } ` ) ;
97104 }
0 commit comments