-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcalculateRowHeights.ts
54 lines (44 loc) · 1.79 KB
/
calculateRowHeights.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {
calculateCellHeight,
} from './calculateCellHeight';
import type {
BaseConfig,
Row,
} from './types/internal';
import {
sequence,
sumArray,
} from './utils';
/**
* Produces an array of values that describe the largest value length (height) in every row.
*/
export const calculateRowHeights = (rows: Row[], config: BaseConfig): number[] => {
const rowHeights: number[] = [];
for (const [rowIndex, row] of rows.entries()) {
let rowHeight = 1;
row.forEach((cell, cellIndex) => {
const containingRange = config.spanningCellManager?.getContainingRange({col: cellIndex,
row: rowIndex});
if (!containingRange) {
const cellHeight = calculateCellHeight(cell, config.columns[cellIndex].width, config.columns[cellIndex].wrapWord);
rowHeight = Math.max(rowHeight, cellHeight);
return;
}
const {topLeft, bottomRight, height} = containingRange;
// bottom-most cell of a range needs to contain all remain lines of spanning cells
if (rowIndex === bottomRight.row) {
const totalOccupiedSpanningCellHeight = sumArray(rowHeights.slice(topLeft.row));
const totalHorizontalBorderHeight = bottomRight.row - topLeft.row;
const totalHiddenHorizontalBorderHeight = sequence(topLeft.row + 1, bottomRight.row).filter((horizontalBorderIndex) => {
/* istanbul ignore next */
return !config.drawHorizontalLine?.(horizontalBorderIndex, rows.length);
}).length;
const cellHeight = height - totalOccupiedSpanningCellHeight - totalHorizontalBorderHeight + totalHiddenHorizontalBorderHeight;
rowHeight = Math.max(rowHeight, cellHeight);
}
// otherwise, just depend on other sibling cell heights in the row
});
rowHeights.push(rowHeight);
}
return rowHeights;
};