@@ -601,14 +601,6 @@ function addNodeAtDepthAndIndex({
601601 getNodeKey ( { node : n , treeIndex : currentIndex } ) ,
602602 ] ) ;
603603
604- // If the potential parent node is at the targetDepth, it isn't eligible
605- if ( currentDepth === targetDepth ) {
606- return {
607- node,
608- nextIndex : currentIndex + 1 + getDescendantCount ( { node, ignoreCollapsed } ) ,
609- } ;
610- }
611-
612604 // If the current position is the only possible place to add, add it
613605 if ( currentIndex >= minimumTreeIndex - 1
614606 || ( isLastChild && ! ( node . children && node . children . length ) ) ) {
@@ -632,7 +624,9 @@ function addNodeAtDepthAndIndex({
632624 }
633625 }
634626
635- if ( currentDepth === targetDepth - 1 ) {
627+ // If this is the target depth for the insertion,
628+ // i.e., where the newNode can be added to the current node's children
629+ if ( currentDepth >= targetDepth - 1 ) {
636630 // Skip over nodes with no children or hidden children
637631 if ( ! node . children ||
638632 typeof node . children === 'function' ||
@@ -641,28 +635,38 @@ function addNodeAtDepthAndIndex({
641635 return { node, nextIndex : currentIndex + 1 } ;
642636 }
643637
638+ // Scan over the children to see if there's a place among them that fulfills
639+ // the minimumTreeIndex requirement
644640 let childIndex = currentIndex + 1 ;
645641 let insertedTreeIndex = null ;
646642 let insertIndex = null ;
647643 for ( let i = 0 ; i < node . children . length ; i ++ ) {
644+ // If a valid location is found, mark it as the insertion location and
645+ // break out of the loop
648646 if ( childIndex >= minimumTreeIndex ) {
649647 insertedTreeIndex = childIndex ;
650648 insertIndex = i ;
651649 break ;
652650 }
653651
652+ // Increment the index by the child itself plus the number of descendants it has
654653 childIndex += 1 + getDescendantCount ( { node : node . children [ i ] , ignoreCollapsed } ) ;
655654 }
656655
656+ // If no valid indices to add the node were found
657657 if ( insertIndex === null ) {
658+ // If the last position in this node's children is less than the minimum index
659+ // and there are more children on the level of this node, return without insertion
658660 if ( childIndex < minimumTreeIndex && ! isLastChild ) {
659661 return { node, nextIndex : childIndex } ;
660662 }
661663
664+ // Use the last position in the children array to insert the newNode
662665 insertedTreeIndex = childIndex ;
663666 insertIndex = node . children . length ;
664667 }
665668
669+ // Insert the newNode at the insertIndex
666670 const nextNode = {
667671 ...node ,
668672 children : [
@@ -672,6 +676,7 @@ function addNodeAtDepthAndIndex({
672676 ] ,
673677 } ;
674678
679+ // Return node with successful insert result
675680 return {
676681 node : nextNode ,
677682 nextIndex : childIndex ,
@@ -748,7 +753,6 @@ function addNodeAtDepthAndIndex({
748753 * @param {boolean= } expandParent - If true, expands the parent of the inserted node
749754 * @param {!function } getNodeKey - Function to get the key from the nodeData and tree index
750755 *
751-
752756 * @return {Object } result
753757 * @return {Object[] } result.treeData - The tree data with the node added
754758 * @return {number } result.treeIndex - The tree index at which the node was inserted
0 commit comments