|
| 1 | +import { |
| 2 | + alignString, |
| 3 | +} from './alignString'; |
| 4 | +import { |
| 5 | + padCellVertically, |
| 6 | +} from './mapDataUsingRowHeights'; |
| 7 | +import { |
| 8 | + padString, |
| 9 | +} from './padTableData'; |
| 10 | +import type { |
| 11 | + SpanningCellContext, |
| 12 | +} from './spanningCellManager'; |
| 13 | +import { |
| 14 | + truncateString, |
| 15 | +} from './truncateTableData'; |
| 16 | +import type { |
| 17 | + RangeConfig, |
| 18 | +} from './types/internal'; |
| 19 | +import { |
| 20 | + sequence, sumArray, |
| 21 | +} from './utils'; |
| 22 | +import { |
| 23 | + wrapCell, |
| 24 | +} from './wrapCell'; |
| 25 | + |
| 26 | +export const wrapContentRange = (rangeConfig: RangeConfig, rangeWidth: number, context: SpanningCellContext): string[] => { |
| 27 | + const {topLeft, paddingRight, paddingLeft, truncate, wrapWord, alignment} = rangeConfig; |
| 28 | + |
| 29 | + const originalContent = context.rows[topLeft.row][topLeft.col]; |
| 30 | + const contentWidth = rangeWidth - paddingLeft - paddingRight; |
| 31 | + |
| 32 | + return wrapCell(truncateString(originalContent, truncate), contentWidth, wrapWord).map((line) => { |
| 33 | + const alignedLine = alignString(line, contentWidth, alignment); |
| 34 | + |
| 35 | + return padString(alignedLine, paddingLeft, paddingRight); |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +export const alignVerticalContentRange = (range: RangeConfig, content: string[], context: SpanningCellContext) => { |
| 40 | + const {rows, drawHorizontalLine, rowHeights} = context; |
| 41 | + const {topLeft, bottomRight, verticalAlignment} = range; |
| 42 | + |
| 43 | + if (rowHeights.length === 0) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + const totalCellHeight = sumArray(rowHeights.slice(topLeft.row, bottomRight.row + 1)); |
| 48 | + const totalBorderHeight = bottomRight.row - topLeft.row; |
| 49 | + const hiddenHorizontalBorderCount = sequence(topLeft.row + 1, bottomRight.row).filter((horizontalBorderIndex) => { |
| 50 | + return !drawHorizontalLine(horizontalBorderIndex, rows.length); |
| 51 | + }).length; |
| 52 | + |
| 53 | + const availableRangeHeight = totalCellHeight + totalBorderHeight - hiddenHorizontalBorderCount; |
| 54 | + |
| 55 | + return padCellVertically(content, availableRangeHeight, verticalAlignment).map((line) => { |
| 56 | + if (line.length === 0) { |
| 57 | + return ' '.repeat(content[0].length); |
| 58 | + } |
| 59 | + |
| 60 | + return line; |
| 61 | + }); |
| 62 | +}; |
0 commit comments