@@ -114,6 +114,7 @@ function History(props: Record<string, unknown>): JSX.Element {
114
114
if ( ! delta ) return 'No state changes' ;
115
115
116
116
const changedState = findStateChangeObj ( delta ) ;
117
+ console . log ( 'changed state' , formatters . html . format ( changedState [ 0 ] ) ) ;
117
118
return changedState . length > 0 ? formatters . html . format ( changedState [ 0 ] ) : 'No state changes' ;
118
119
} catch ( error ) {
119
120
console . error ( 'Error in findDiff:' , error ) ;
@@ -129,9 +130,20 @@ function History(props: Record<string, unknown>): JSX.Element {
129
130
const svg = d3 . select ( svgRef . current ) ;
130
131
svg . selectAll ( '*' ) . remove ( ) ;
131
132
132
- const treeLayout = d3 . tree ( ) . nodeSize ( [ FIXED_NODE_WIDTH , FIXED_NODE_HEIGHT ] ) ;
133
- const d3root = treeLayout ( d3 . hierarchy ( root ) ) ;
133
+ // Create tree layout with fixed node size
134
+ const treeLayout = d3
135
+ . tree ( )
136
+ . nodeSize ( [ FIXED_NODE_WIDTH , FIXED_NODE_HEIGHT ] )
137
+ . separation ( ( a , b ) => {
138
+ // Increase separation between unrelated subtrees
139
+ return a . parent === b . parent ? 1.2 : 2 ;
140
+ } ) ;
141
+
142
+ // Create hierarchy and compute initial layout
143
+ const d3root = d3 . hierarchy ( root ) ;
144
+ treeLayout ( d3root ) ;
134
145
146
+ // Calculate the bounds of the tree
135
147
let minX = Infinity ;
136
148
let maxX = - Infinity ;
137
149
let minY = Infinity ;
@@ -144,15 +156,24 @@ function History(props: Record<string, unknown>): JSX.Element {
144
156
maxY = Math . max ( maxY , d . y ) ;
145
157
} ) ;
146
158
159
+ // Calculate the actual dimensions needed
147
160
const actualWidth = maxX - minX + FIXED_NODE_WIDTH ;
148
161
const actualHeight = maxY - minY + FIXED_NODE_HEIGHT ;
149
162
163
+ // Set SVG dimensions to accommodate the tree
164
+ const svgWidth = Math . max ( actualWidth + margin . left + margin . right , totalWidth ) ;
150
165
svg
151
- . attr ( 'width' , Math . max ( actualWidth + margin . left + margin . right , totalWidth ) )
166
+ . attr ( 'width' , svgWidth )
152
167
. attr ( 'height' , Math . max ( actualHeight + margin . top + margin . bottom , totalHeight ) ) ;
153
168
154
- const centerOffset = totalWidth / 2 - ( maxX - minX ) / 2 ;
155
- const g = svg . append ( 'g' ) . attr ( 'transform' , `translate(${ centerOffset } ,${ margin . top } )` ) ;
169
+ // Calculate center offset to keep root centered
170
+ const rootOffset = - d3root . x ;
171
+ const horizontalCenter = svgWidth / 2 ;
172
+
173
+ // Create container group and apply transforms
174
+ const g = svg
175
+ . append ( 'g' )
176
+ . attr ( 'transform' , `translate(${ horizontalCenter + rootOffset } ,${ margin . top } )` ) ;
156
177
157
178
// Draw links
158
179
const link = g
@@ -163,13 +184,12 @@ function History(props: Record<string, unknown>): JSX.Element {
163
184
. attr ( 'class' , ( d ) => {
164
185
return d . data . index === currLocation . index ? 'link current-link' : 'link' ;
165
186
} )
166
- . attr (
167
- 'd' ,
168
- ( d ) =>
169
- `M${ d . x } ,${ d . y } C${ d . x } ,${ ( d . y + d . parent . y ) / 2 } ${ d . parent . x } ,${
170
- ( d . y + d . parent . y ) / 2
171
- } ${ d . parent . x } ,${ d . parent . y } `,
172
- ) ;
187
+ . attr ( 'd' , ( d ) => {
188
+ return `M${ d . x } ,${ d . y }
189
+ C${ d . x } ,${ ( d . y + d . parent . y ) / 2 }
190
+ ${ d . parent . x } ,${ ( d . y + d . parent . y ) / 2 }
191
+ ${ d . parent . x } ,${ d . parent . y } ` ;
192
+ } ) ;
173
193
174
194
// Create node groups
175
195
const node = g
@@ -189,7 +209,7 @@ function History(props: Record<string, unknown>): JSX.Element {
189
209
dispatch ( changeSlider ( d . data . index ) ) ;
190
210
} ) ;
191
211
192
- // Add rectangles for nodes
212
+ // Add rectangles for nodes with consistent size
193
213
node
194
214
. append ( 'rect' )
195
215
. attr ( 'width' , 180 )
@@ -199,7 +219,7 @@ function History(props: Record<string, unknown>): JSX.Element {
199
219
. attr ( 'rx' , 8 )
200
220
. attr ( 'ry' , 8 ) ;
201
221
202
- // Add snapshot title
222
+ // Add snapshot titles
203
223
node
204
224
. append ( 'text' )
205
225
. attr ( 'dy' , '-25' )
@@ -216,7 +236,7 @@ function History(props: Record<string, unknown>): JSX.Element {
216
236
. attr ( 'height' , 65 )
217
237
. append ( 'xhtml:div' )
218
238
. style ( 'font-size' , '12px' )
219
- . style ( 'overflow' , 'hidden ' )
239
+ . style ( 'overflow-y ' , 'auto ' )
220
240
. style ( 'text-align' , 'center' )
221
241
. html ( ( d ) => findDiff ( d . data . index ) ) ;
222
242
0 commit comments