@@ -8,24 +8,24 @@ const RowManager = (function() {
88 }
99
1010 resizeAll ( ) {
11- _rows . forEach ( ( row , i ) => {
12- row . calculateMaxSlot ( ) ;
13- row . calculateMinSlot ( ) ;
14- if ( i > 0 ) {
15- row . paddingTop = _rows [ i - 1 ] . minSlot ;
16- }
17- let dy = row . minHeight - row . rh ;
18- if ( dy > 0 ) { this . resizeRow ( row . idx , dy ) ; }
11+ _rows . forEach ( row => {
12+ this . recalculateRowSlots ( row ) ;
13+ this . resizeRow ( row . idx ) ;
1914 } ) ;
2015 }
2116
22- resizeRow ( i , dy ) {
17+ resizeRow ( i , dy = 0 ) {
2318 let row = _rows [ i ] ;
2419 dy = Math . max ( - row . rh + row . minHeight , dy ) ;
2520 row . height ( row . rh + dy ) ;
2621 row . words . forEach ( word => word . redrawLinks ( ) ) ;
2722 for ( let j = i + 1 ; j < _rows . length ; ++ j ) {
28- _rows [ j ] . dy ( dy ) ;
23+ if ( _rows [ j - 1 ] . ry2 + ROW_PADDING > _rows [ j ] . ry + dy ) {
24+ _rows [ j ] . move ( _rows [ j - 1 ] . ry2 + ROW_PADDING ) ;
25+ }
26+ else {
27+ _rows [ j ] . dy ( dy ) ;
28+ }
2929 _rows [ j ] . words . forEach ( word => word . redrawLinks ( ) ) ;
3030 }
3131 _svg . height ( this . lastRow . ry2 + ROW_PADDING + 20 ) ;
@@ -60,15 +60,23 @@ const RowManager = (function() {
6060
6161 addWordToRow ( word , row , i , ignorePosition ) {
6262 if ( isNaN ( i ) ) { i = row . words . length ; }
63- if ( word . row && word . row !== row ) { word . row . calculateMaxSlot ( ) ; }
63+
64+ // get word slots
65+ let slots = this . getSlotRange ( [ 0 , 0 ] , word ) ;
66+ if ( word . row && word . row !== row &&
67+ ( slots [ 0 ] === word . row . minSlot || word . row . maxSlot === slots [ 1 ] ) ) {
68+ this . recalculateRowSlots ( word . row ) ;
69+ }
70+ if ( row . minSlot > slots [ 0 ] || row . maxSlot < slots [ 1 ] ) {
71+ if ( row . minSlot > slots [ 0 ] ) { row . minSlot = slots [ 0 ] ; }
72+ if ( row . maxSlot < slots [ 1 ] ) { row . maxSlot = slots [ 1 ] ; }
73+ this . resizeRow ( row . idx ) ;
74+ }
6475
6576 let overflow = row . addWord ( word , i , ignorePosition ) ;
6677 while ( overflow < row . words . length ) {
6778 this . moveWordDownARow ( row . idx ) ;
6879 }
69- row . calculateMaxSlot ( ) ;
70- let dy = row . minHeight - row . rh ;
71- if ( dy > 0 ) { this . resizeRow ( row . idx , dy ) ; }
7280 }
7381
7482 moveWordOnRow ( word , dx ) {
@@ -183,6 +191,19 @@ const RowManager = (function() {
183191 this . addWordToRow ( _rows [ index ] . removeLastWord ( ) , nextRow , 0 ) ;
184192 }
185193
194+ getSlotRange ( acc , anchor ) {
195+ if ( anchor . links . length === 0 ) {
196+ return [ Math . min ( acc [ 0 ] , anchor . slot ) , Math . max ( acc [ 1 ] , anchor . slot ) ] ;
197+ }
198+ let a = anchor . links . reduce ( ( acc , val ) => this . getSlotRange ( acc , val ) , [ 0 , 0 ] ) ;
199+ return [ Math . min ( acc [ 0 ] , a [ 0 ] ) , Math . max ( acc [ 1 ] , a [ 1 ] ) ] ;
200+ }
201+
202+ recalculateRowSlots ( row ) {
203+ [ row . minSlot , row . maxSlot ] = row . words
204+ . reduce ( ( acc , val ) => this . getSlotRange ( acc , val ) , [ 0 , 0 ] ) ;
205+ }
206+
186207 get lastRow ( ) { return _rows [ _rows . length - 1 ] ; }
187208 get rows ( ) { return _rows ; }
188209 }
0 commit comments