@@ -24,10 +24,12 @@ import { setStationsBulk } from './action';
2424 *
2525 * Implementation Details:
2626 * 1. Diverge Point (D):
27- * - Swap `children` nodes (Main <-> Branch).
27+ * - Swap `children` nodes, to ensure the index of the branch start node
28+ * which decides drawing details unchanged.
2829 * - Update `branch.right` to point to the *new* branch start node (old Main).
2930 * 2. Converge Point (G):
30- * - Swap `parents` nodes.
31+ * - Swap `parents` nodes, to ensure the index of the branch start node
32+ * which decides drawing details unchanged.
3133 * - Update `branch.left` to point to the *new* branch end node (old Main).
3234 *
3335 * Assumption: Strict 2-way split/merge at D/G.
@@ -61,24 +63,26 @@ export const swapBranch = (branchIndex: number) => {
6163
6264 let modified = false ;
6365
64- // Swap children of divergePoint, then assign the new children[0] to branch.right[1]
66+ // Swap children of divergePoint, then assigned the child preserving index in children to branch.right[1]
6567 if ( divergePoint . branch && divergePoint . branch . right ) {
68+ const indexOfRightInChildren = divergePoint . children . indexOf ( divergePoint . branch . right [ 1 ] ) ;
6669 const [ child1 , child2 ] = divergePoint . children ;
6770 divergePoint . children = [ child2 , child1 ] ;
6871 divergePoint . branch = {
6972 ...divergePoint . branch ,
70- right : [ divergePoint . branch . right [ 0 ] , divergePoint . children [ 0 ] ] ,
73+ right : [ divergePoint . branch . right [ 0 ] , divergePoint . children [ indexOfRightInChildren ] ] ,
7174 } ;
7275
7376 modified = true ;
7477 }
75- // Swap parents of convergePoint, then assign the new parents[0] to branch.left[1]
78+ // Swap parents of convergePoint, then assigned the parent preserving index in parents to branch.left[1]
7679 if ( convergePoint . branch && convergePoint . branch . left ) {
80+ const indexOfLeftInParents = convergePoint . parents . indexOf ( convergePoint . branch . left [ 1 ] ) ;
7781 const [ parent1 , parent2 ] = convergePoint . parents ;
7882 convergePoint . parents = [ parent2 , parent1 ] ;
7983 convergePoint . branch = {
8084 ...convergePoint . branch ,
81- left : [ convergePoint . branch . left [ 0 ] , convergePoint . parents [ 0 ] ] ,
85+ left : [ convergePoint . branch . left [ 0 ] , convergePoint . parents [ indexOfLeftInParents ] ] ,
8286 } ;
8387
8488 modified = true ;
0 commit comments