@@ -506,13 +506,42 @@ export function useActions(
506
506
}
507
507
}
508
508
509
- const removeNodes : Actions [ 'removeNodes' ] = ( nodes , removeConnectedEdges = true ) => {
509
+ const removeNodes : Actions [ 'removeNodes' ] = ( nodes , removeConnectedEdges = true , removeChildren = false ) => {
510
510
let nodesToRemove = nodes instanceof Function ? nodes ( state . nodes ) : nodes
511
511
nodesToRemove = Array . isArray ( nodesToRemove ) ? nodesToRemove : [ nodesToRemove ]
512
512
513
513
const nodeChanges : NodeRemoveChange [ ] = [ ]
514
514
const edgeChanges : EdgeRemoveChange [ ] = [ ]
515
515
516
+ function createEdgeRemovalChanges ( nodes : Node [ ] ) {
517
+ const connections = getConnectedEdges ( nodes , state . edges ) . filter ( ( edge ) => {
518
+ if ( isDef ( edge . deletable ) ) {
519
+ return edge . deletable
520
+ }
521
+ return true
522
+ } )
523
+
524
+ edgeChanges . push ( ...connections . map ( ( connection ) => createRemoveChange ( connection . id ) ) )
525
+ }
526
+
527
+ // recursively get all children and if the child is a parent, get those children as well until all nodes have been removed that are children of the current node
528
+ function createChildrenRemovalChanges ( id : string ) {
529
+ const children = state . nodes . filter ( ( n ) => n . parentNode === id )
530
+
531
+ if ( children . length ) {
532
+ const childIds = children . map ( ( n ) => n . id )
533
+ nodeChanges . push ( ...childIds . map ( ( id ) => createRemoveChange ( id ) ) )
534
+
535
+ if ( removeConnectedEdges ) {
536
+ createEdgeRemovalChanges ( children )
537
+ }
538
+
539
+ children . forEach ( ( child ) => {
540
+ createChildrenRemovalChanges ( child . id )
541
+ } )
542
+ }
543
+ }
544
+
516
545
nodesToRemove . forEach ( ( item ) => {
517
546
const currNode = typeof item === 'string' ? findNode ( item ) : item
518
547
@@ -527,14 +556,11 @@ export function useActions(
527
556
nodeChanges . push ( createRemoveChange ( currNode . id ) )
528
557
529
558
if ( removeConnectedEdges ) {
530
- const connections = getConnectedEdges ( [ currNode ] , state . edges ) . filter ( ( edge ) => {
531
- if ( isDef ( edge . deletable ) ) {
532
- return edge . deletable
533
- }
534
- return true
535
- } )
559
+ createEdgeRemovalChanges ( [ currNode ] )
560
+ }
536
561
537
- edgeChanges . push ( ...connections . map ( ( connection ) => createRemoveChange ( connection . id ) ) )
562
+ if ( removeChildren ) {
563
+ createChildrenRemovalChanges ( currNode . id )
538
564
}
539
565
} )
540
566
0 commit comments