@@ -202,11 +202,12 @@ export default class TowerModel {
202
202
// Create a new tower for the detached subtree
203
203
const newTower = new TowerModel ( uuid ( ) , node . brick , { ...node . position } ) ;
204
204
205
- // Helper to recursively gather all descendants
206
- const gatherDescendants = ( n : ITowerNode , collection : Map < string , ITowerNode > ) => {
205
+ // Helper to recursively gather all descendants, respecting isNested
206
+ const gatherDescendants = ( n : ITowerNode , collection : Map < string , ITowerNode > ) : void => {
207
207
collection . set ( n . brick . uuid , cloneDeep ( n ) ) ;
208
+ // Find all children (nested, args, or stacked) based on parent reference
208
209
this . nodes . forEach ( childNode => {
209
- if ( childNode . parent && childNode . parent . brick . uuid === n . brick . uuid ) {
210
+ if ( childNode . parent ? .brick . uuid === n . brick . uuid ) {
210
211
gatherDescendants ( childNode , collection ) ;
211
212
}
212
213
} ) ;
@@ -219,8 +220,7 @@ export default class TowerModel {
219
220
if ( node . parent ) {
220
221
// Remove connections involving the detached subtree
221
222
this . connections = this . connections . filter (
222
- ( conn ) => {
223
- // Keep connections that don't involve any node in the detached subtree
223
+ conn => {
224
224
const fromInSubtree = newNodes . has ( conn . from ) ;
225
225
const toInSubtree = newNodes . has ( conn . to ) ;
226
226
return ! ( fromInSubtree || toInSubtree ) ;
@@ -233,14 +233,23 @@ export default class TowerModel {
233
233
this . nodes . delete ( key ) ;
234
234
}
235
235
236
- // The new tower's nodes are the collected descendants
236
+ // Set up the new tower's nodes, adjusting parents
237
237
newTower . nodes . clear ( ) ; // Clear the root created by the constructor
238
238
newNodes . forEach ( ( n , id ) => {
239
+ const newNode = cloneDeep ( n ) ;
239
240
// Reset parent for the root of the new tower
240
241
if ( id === brickId ) {
241
- n . parent = null ;
242
+ newNode . parent = null ;
243
+ } else {
244
+ // Re-link parent if it exists in the new tower
245
+ if ( n . parent ) {
246
+ const parentNode = newNodes . get ( n . parent . brick . uuid ) ;
247
+ newNode . parent = parentNode || null ; // Safe assignment with type guard
248
+ } else {
249
+ newNode . parent = null ; // Explicitly handle null parent
250
+ }
242
251
}
243
- newTower . nodes . set ( id , n ) ;
252
+ newTower . nodes . set ( id , newNode ) ;
244
253
} ) ;
245
254
246
255
// Copy relevant connections to the new tower
0 commit comments