@@ -160,24 +160,22 @@ export default class BrickTreeManager {
160160
161161 if ( ! fromBrickNode || ! toBrickNode ) return null ;
162162
163- console . log ( `Connection points - fromPoint:` , fromPoint , `toPoint:` , toPoint ) ;
164- console . log ( `From brick connection points:` , fromBrickNode . brick . connectionPoints ) ;
165- console . log ( `To brick connection points:` , toBrickNode . brick . connectionPoints ) ;
163+ // Validate that both bricks exist and can be connected
164+ // The connection points represent the specific notches where bricks will connect
166165
167166 const fromNotchId = this . findNotchId ( fromBrickNode . brick , fromPoint ) ;
168167 const toNotchId = this . findNotchId ( toBrickNode . brick , toPoint ) ;
169168
170- console . log (
171- `Connecting ${ fromBrickId } to ${ toBrickId } : fromNotchId=${ fromNotchId } , toNotchId=${ toNotchId } ` ,
172- ) ;
169+ // Find the specific notch IDs for both bricks based on their connection points
170+ // These IDs are used to track which notches are occupied
173171
174172 if ( ! fromNotchId || ! toNotchId ) {
175173 console . error ( 'Could not determine notch IDs for connection' ) ;
176174 return null ;
177175 }
178176
179- console . log ( `From brick connected notches:` , Array . from ( fromBrickNode . connectedNotches ) ) ;
180- console . log ( `To brick connected notches:` , Array . from ( toBrickNode . connectedNotches ) ) ;
177+ // Check if the notches are already connected to other bricks
178+ // A notch can only be connected to one other notch at a time
181179
182180 if (
183181 fromBrickNode . connectedNotches . has ( fromNotchId ) ||
@@ -187,6 +185,7 @@ export default class BrickTreeManager {
187185 return null ;
188186 }
189187
188+ // Mark both notches as connected to prevent future connections
190189 fromBrickNode . connectedNotches . add ( fromNotchId ) ;
191190 toBrickNode . connectedNotches . add ( toNotchId ) ;
192191
@@ -204,15 +203,15 @@ export default class BrickTreeManager {
204203 } ;
205204
206205 if ( fromTree . id === toTree . id ) {
206+ // Both bricks are already in the same tree, just add the new connection
207207 fromTree . connections . push ( connection ) ;
208208 this . updateParentChildRelationships ( fromBrickId , toBrickId , connection . type ) ;
209- console . log ( `Added connection to existing tree ${ fromTree . id } ` ) ;
210209 return fromTree . id ;
211210 }
212211
212+ // Bricks are in different trees, merge them into a single tree
213213 const mergedTree = this . mergeTrees ( fromTree , toTree , connection ) ;
214214 this . updateParentChildRelationships ( fromBrickId , toBrickId , connection . type ) ;
215- console . log ( `Merged trees into ${ mergedTree . id } ` ) ;
216215 return mergedTree . id ;
217216 }
218217
@@ -263,10 +262,8 @@ export default class BrickTreeManager {
263262 const originalTree = this . findTreeByBrickId ( brickId ) ;
264263 if ( ! originalTree ) return { removedConnections : [ ] , newTreeIds : [ ] } ;
265264
266- console . log ( `Disconnecting brick ${ brickId } from tree ${ originalTree . id } ` ) ;
267- console . log ( `Original tree connections:` , originalTree . connections ) ;
268-
269- // Collect all descendant nodes of the disconnected brick (including the brick itself)
265+ // Step 1: Collect all descendant nodes that will move with the disconnected brick
266+ // This includes the brick itself and all its children (hierarchical behavior)
270267 const nodesToMove = new Map < string , TTreeNode > ( ) ;
271268 const stack : TTreeNode [ ] = [ brickNode ] ;
272269 const visited = new Set < string > ( [ brickNode . brick . uuid ] ) ;
@@ -275,6 +272,7 @@ export default class BrickTreeManager {
275272 const currentNode = stack . pop ( ) ! ;
276273 nodesToMove . set ( currentNode . brick . uuid , currentNode ) ;
277274
275+ // Add all children of the current node to the stack for processing
278276 this . getBrickChildren ( currentNode . brick . uuid ) . forEach ( ( child ) => {
279277 if ( ! visited . has ( child . brick . uuid ) ) {
280278 visited . add ( child . brick . uuid ) ;
@@ -283,10 +281,10 @@ export default class BrickTreeManager {
283281 } ) ;
284282 }
285283
286- console . log ( `Nodes to move:` , Array . from ( nodesToMove . keys ( ) ) ) ;
287-
288- // Find all connections that need to be removed from the original tree
289- // This includes connections between nodes being moved and connections to/from external nodes
284+ // Step 2: Identify connections that need to be removed from the original tree
285+ // Remove connections where:
286+ // - Both nodes are moving to the new tree (internal connections)
287+ // - One node is moving and the other stays (cross-tree connections)
290288 const connectionsToRemove = originalTree . connections . filter ( ( conn ) => {
291289 const fromInNewTree = nodesToMove . has ( conn . from ) ;
292290 const toInNewTree = nodesToMove . has ( conn . to ) ;
@@ -298,43 +296,35 @@ export default class BrickTreeManager {
298296 ( fromInNewTree && ! toInNewTree ) ||
299297 ( ! fromInNewTree && toInNewTree ) ;
300298
301- console . log (
302- `Connection ${ conn . from } -> ${ conn . to } : fromInNewTree=${ fromInNewTree } , toInNewTree=${ toInNewTree } , shouldRemove=${ shouldRemove } ` ,
303- ) ;
304-
305299 return shouldRemove ;
306300 } ) ;
307301
308- console . log ( `Connections to remove:` , connectionsToRemove ) ;
309-
310302 if ( connectionsToRemove . length === 0 ) return { removedConnections : [ ] , newTreeIds : [ ] } ;
311303
312- // Remove connections from original tree
304+ // Step 3: Remove connections and nodes from the original tree
313305 this . removeConnections ( originalTree , connectionsToRemove ) ;
314-
315- // Remove nodes from original tree
316306 nodesToMove . forEach ( ( node , brickId ) => {
317307 originalTree . nodes . delete ( brickId ) ;
318308 } ) ;
319309
320- // Create new tree with the disconnected brick as root
310+ // Step 4: Create a new tree with the disconnected brick as root
321311 const newTree = this . createTree ( brickNode . brick , brickNode . position ) ;
322312
323- // Add all descendant nodes to the new tree
313+ // Step 5: Add all descendant nodes to the new tree
324314 nodesToMove . forEach ( ( node , brickId ) => {
325315 if ( brickId !== brickNode . brick . uuid ) {
326316 // Don't add the root twice
327317 newTree . nodes . set ( brickId , node ) ;
328318 }
329319 } ) ;
330320
331- // Move connections between nodes in the new tree to the new tree
321+ // Step 6: Move internal connections to the new tree
332322 const connectionsToMove = connectionsToRemove . filter (
333323 ( conn ) => nodesToMove . has ( conn . from ) && nodesToMove . has ( conn . to ) ,
334324 ) ;
335325 newTree . connections = connectionsToMove ;
336326
337- // Update parent relationships for the new tree
327+ // Step 7: Update parent relationships for the new tree
338328 // The disconnected brick becomes the root (no parent)
339329 brickNode . parent = null ;
340330
@@ -349,12 +339,11 @@ export default class BrickTreeManager {
349339 }
350340 } ) ;
351341
352- // Clean up original tree if it's empty
342+ // Step 8: Clean up original tree if it's empty
353343 if ( originalTree . nodes . size === 0 ) {
354344 this . trees = this . trees . filter ( ( t ) => t . id !== originalTree . id ) ;
355345 }
356346
357- console . log ( `Returning ${ connectionsToRemove . length } removed connections` ) ;
358347 return { removedConnections : connectionsToRemove , newTreeIds : [ newTree . id ] } ;
359348 }
360349
0 commit comments