Skip to content

Commit ec293d3

Browse files
committed
#269 - Refactored table layouts
1 parent cd29be8 commit ec293d3

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/layout-manager.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@ const Cell = require('./cell');
22
const { ColSpanCell, RowSpanCell } = Cell;
33

44
(function () {
5+
function next(alloc, col) {
6+
if (alloc[col] > 0) {
7+
return next(alloc, col + 1);
8+
}
9+
return col;
10+
}
11+
512
function layoutTable(table) {
13+
let alloc = {};
614
table.forEach(function (row, rowIndex) {
7-
let prevCell = null;
8-
row.forEach(function (cell, columnIndex) {
15+
let col = 0;
16+
row.forEach(function (cell) {
917
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;
1925
}
20-
prevCell = cell;
2126
}
27+
col = cell.x + colSpan;
28+
});
29+
Object.keys(alloc).forEach((idx) => {
30+
alloc[idx]--;
31+
if (alloc[idx] < 1) delete alloc[idx];
2232
});
2333
});
2434
}

0 commit comments

Comments
 (0)