-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathalignSpanningCell.ts
67 lines (57 loc) · 2.09 KB
/
alignSpanningCell.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
55
56
57
58
59
60
61
62
63
64
65
66
67
import stringWidth from 'string-width';
import {
alignString,
} from './alignString';
import {
padCellVertically,
} from './mapDataUsingRowHeights';
import {
padString,
} from './padTableData';
import type {
SpanningCellContext,
} from './spanningCellManager';
import {
truncateString,
} from './truncateTableData';
import type {
RangeConfig,
} from './types/internal';
import {
sequence, sumArray,
} from './utils';
import {
wrapCell,
} from './wrapCell';
/**
* Fill content into all cells in range in order to calculate total height
*/
export const wrapRangeContent = (rangeConfig: RangeConfig, rangeWidth: number, context: SpanningCellContext): string[] => {
const {topLeft, paddingRight, paddingLeft, truncate, wrapWord, alignment} = rangeConfig;
const originalContent = context.rows[topLeft.row][topLeft.col];
const contentWidth = rangeWidth - paddingLeft - paddingRight;
return wrapCell(truncateString(originalContent, truncate), contentWidth, wrapWord).map((line) => {
const alignedLine = alignString(line, contentWidth, alignment);
return padString(alignedLine, paddingLeft, paddingRight);
});
};
export const alignVerticalRangeContent = (range: RangeConfig, content: string[], context: SpanningCellContext) => {
const {rows, drawHorizontalLine, rowHeights} = context;
const {topLeft, bottomRight, verticalAlignment} = range;
// They are empty before calculateRowHeights function run
if (rowHeights.length === 0) {
return [];
}
const totalCellHeight = sumArray(rowHeights.slice(topLeft.row, bottomRight.row + 1));
const totalBorderHeight = bottomRight.row - topLeft.row;
const hiddenHorizontalBorderCount = sequence(topLeft.row + 1, bottomRight.row).filter((horizontalBorderIndex) => {
return !drawHorizontalLine(horizontalBorderIndex, rows.length);
}).length;
const availableRangeHeight = totalCellHeight + totalBorderHeight - hiddenHorizontalBorderCount;
return padCellVertically(content, availableRangeHeight, verticalAlignment).map((line) => {
if (line.length === 0) {
return ' '.repeat(stringWidth(content[0]));
}
return line;
});
};