File tree 1 file changed +22
-12
lines changed
1 file changed +22
-12
lines changed Original file line number Diff line number Diff line change @@ -2,23 +2,33 @@ const Cell = require('./cell');
2
2
const { ColSpanCell, RowSpanCell } = Cell ;
3
3
4
4
( function ( ) {
5
+ function next ( alloc , col ) {
6
+ if ( alloc [ col ] > 0 ) {
7
+ return next ( alloc , col + 1 ) ;
8
+ }
9
+ return col ;
10
+ }
11
+
5
12
function layoutTable ( table ) {
13
+ let alloc = { } ;
6
14
table . forEach ( function ( row , rowIndex ) {
7
- let prevCell = null ;
8
- row . forEach ( function ( cell , columnIndex ) {
15
+ let col = 0 ;
16
+ row . forEach ( function ( cell ) {
9
17
cell . y = rowIndex ;
10
- cell . x = prevCell ? prevCell . x + 1 : columnIndex ;
11
- for ( let y = rowIndex ; y >= 0 ; y -- ) {
12
- let row2 = table [ y ] ;
13
- let xMax = y === rowIndex ? columnIndex : row2 . length ;
14
- for ( let x = 0 ; x < xMax ; x ++ ) {
15
- let cell2 = row2 [ x ] ;
16
- while ( cellsConflict ( cell , cell2 ) ) {
17
- cell . x ++ ;
18
- }
18
+ // Avoid erroneous call to next() on first row
19
+ cell . x = rowIndex ? next ( alloc , col ) : col ;
20
+ const rowSpan = cell . rowSpan || 1 ;
21
+ const colSpan = cell . colSpan || 1 ;
22
+ if ( rowSpan > 1 ) {
23
+ for ( let cs = 0 ; cs < colSpan ; cs ++ ) {
24
+ alloc [ cell . x + cs ] = rowSpan ;
19
25
}
20
- prevCell = cell ;
21
26
}
27
+ col = cell . x + colSpan ;
28
+ } ) ;
29
+ Object . keys ( alloc ) . forEach ( ( idx ) => {
30
+ alloc [ idx ] -- ;
31
+ if ( alloc [ idx ] < 1 ) delete alloc [ idx ] ;
22
32
} ) ;
23
33
} ) ;
24
34
}
You can’t perform that action at this time.
0 commit comments