@@ -17,6 +17,7 @@ export interface UnipikaFlattenTreeItem {
1717 level : number ;
1818 open ?: BlockType ;
1919 close ?: BlockType ;
20+ depth ?: number ;
2021
2122 path ?: string ; // if present the block is collapsible/expandable
2223
@@ -123,6 +124,10 @@ function flattenUnipikaJsonImpl(value: UnipikaValue, level = 0, ctx: FlatContext
123124
124125 if ( isCollapsed ) {
125126 handleCollapsedValue ( value , level , ctx ) ;
127+ // Get container size even for collapsed items
128+ if ( isContainerType ) {
129+ containerSize = value . $value . length ;
130+ }
126131 } else {
127132 let jsonOpenLevel = NaN ;
128133 let valueLevel = level ;
@@ -155,6 +160,9 @@ function flattenUnipikaJsonImpl(value: UnipikaValue, level = 0, ctx: FlatContext
155160 }
156161
157162 if ( isContainerType ) {
163+ if ( containerSize ) {
164+ ctx . dst [ itemPathIndex ] . depth = containerSize ;
165+ }
158166 if ( hasAttributes || containerSize ) {
159167 handlePath ( ctx , itemPathIndex ) ; // handle 'array item'/'object field' path
160168 }
@@ -170,7 +178,7 @@ function handleJsonAttributes(
170178
171179 const isCollapsed = isPathCollapsed ( ctx ) ;
172180 if ( isCollapsed ) {
173- handleCollapsedBlock ( 'attributes' , valueLevel , ctx ) ;
181+ handleCollapsedBlock ( 'attributes' , valueLevel , ctx , $attributes . length ) ;
174182 } else {
175183 const attrsLevelInfo = openBlock ( 'attributes' , valueLevel , ctx , $attributes . length ) ;
176184 handlePath ( ctx , ctx . dst . length - 1 ) ;
@@ -198,6 +206,8 @@ function handleValueBlock(
198206 const isValueCollapsed = isContainerType && isPathCollapsed ( ctx ) ;
199207 if ( isValueCollapsed ) {
200208 handleCollapsedValue ( value , valueLevel , ctx ) ;
209+ // Get container size even for collapsed $value
210+ containerSize = value . $type === 'map' || value . $type === 'list' ? value . $value . length : 0 ;
201211 } else {
202212 switch ( value . $type ) {
203213 case 'map' :
@@ -263,6 +273,10 @@ function flattenUnipikaYsonImpl(value: UnipikaValue, level = 0, ctx: FlatContext
263273 }
264274 }
265275
276+ if ( isContainerType && containerSize ) {
277+ ctx . dst [ itemPathIndex ] . depth = containerSize ;
278+ }
279+
266280 if (
267281 ( containerSize && ! hasAttributes ) ||
268282 ( hasAttributes && ( parentType === 'object' || parentType === 'attributes' ) )
@@ -274,11 +288,11 @@ function flattenUnipikaYsonImpl(value: UnipikaValue, level = 0, ctx: FlatContext
274288function handleCollapsedValue ( value : UnipikaValue , level : number , ctx : FlatContext ) {
275289 switch ( value . $type ) {
276290 case 'map' : {
277- handleCollapsedBlock ( 'object' , level , ctx ) ;
291+ handleCollapsedBlock ( 'object' , level , ctx , value . $value . length ) ;
278292 break ;
279293 }
280294 case 'list' : {
281- handleCollapsedBlock ( 'array' , level , ctx ) ;
295+ handleCollapsedBlock ( 'array' , level , ctx , value . $value . length ) ;
282296 break ;
283297 }
284298 default : {
@@ -287,8 +301,8 @@ function handleCollapsedValue(value: UnipikaValue, level: number, ctx: FlatConte
287301 }
288302}
289303
290- function handleCollapsedBlock ( type : BlockType , level : number , ctx : FlatContext ) {
291- openBlock ( type , level , ctx , 0 ) ;
304+ function handleCollapsedBlock ( type : BlockType , level : number , ctx : FlatContext , depth ?: number ) {
305+ openBlock ( type , level , ctx , depth || 0 ) ;
292306 const item = ctx . dst [ ctx . dst . length - 1 ] ;
293307 item . collapsed = true ;
294308 handlePath ( ctx , ctx . dst . length - 1 ) ;
@@ -326,8 +340,15 @@ function openBlock(type: BlockType, level: number, ctx: FlatContext, length: num
326340 // for attributes level should be upper than level of key or parent array
327341 if ( last ?. key && last . level === level ) {
328342 last . open = type ;
343+ if ( length > 0 ) {
344+ last . depth = length ;
345+ }
329346 } else {
330- dst . push ( { level, open : type } ) ;
347+ const item : UnipikaFlattenTreeItem = { level, open : type } ;
348+ if ( length > 0 ) {
349+ item . depth = length ;
350+ }
351+ dst . push ( item ) ;
331352 }
332353 const levelInfo = { type, length, currentIndex : 0 } ;
333354 ctx . levels . push ( levelInfo ) ;
0 commit comments