alignSpanningCell.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.alignVerticalRangeContent = exports.wrapRangeContent = void 0;
  4. const alignString_1 = require("./alignString");
  5. const mapDataUsingRowHeights_1 = require("./mapDataUsingRowHeights");
  6. const padTableData_1 = require("./padTableData");
  7. const truncateTableData_1 = require("./truncateTableData");
  8. const utils_1 = require("./utils");
  9. const wrapCell_1 = require("./wrapCell");
  10. /**
  11. * Fill content into all cells in range in order to calculate total height
  12. */
  13. const wrapRangeContent = (rangeConfig, rangeWidth, context) => {
  14. const { topLeft, paddingRight, paddingLeft, truncate, wrapWord, alignment } = rangeConfig;
  15. const originalContent = context.rows[topLeft.row][topLeft.col];
  16. const contentWidth = rangeWidth - paddingLeft - paddingRight;
  17. return (0, wrapCell_1.wrapCell)((0, truncateTableData_1.truncateString)(originalContent, truncate), contentWidth, wrapWord).map((line) => {
  18. const alignedLine = (0, alignString_1.alignString)(line, contentWidth, alignment);
  19. return (0, padTableData_1.padString)(alignedLine, paddingLeft, paddingRight);
  20. });
  21. };
  22. exports.wrapRangeContent = wrapRangeContent;
  23. const alignVerticalRangeContent = (range, content, context) => {
  24. const { rows, drawHorizontalLine, rowHeights } = context;
  25. const { topLeft, bottomRight, verticalAlignment } = range;
  26. // They are empty before calculateRowHeights function run
  27. if (rowHeights.length === 0) {
  28. return [];
  29. }
  30. const totalCellHeight = (0, utils_1.sumArray)(rowHeights.slice(topLeft.row, bottomRight.row + 1));
  31. const totalBorderHeight = bottomRight.row - topLeft.row;
  32. const hiddenHorizontalBorderCount = (0, utils_1.sequence)(topLeft.row + 1, bottomRight.row).filter((horizontalBorderIndex) => {
  33. return !drawHorizontalLine(horizontalBorderIndex, rows.length);
  34. }).length;
  35. const availableRangeHeight = totalCellHeight + totalBorderHeight - hiddenHorizontalBorderCount;
  36. return (0, mapDataUsingRowHeights_1.padCellVertically)(content, availableRangeHeight, verticalAlignment).map((line) => {
  37. if (line.length === 0) {
  38. return ' '.repeat(content[0].length);
  39. }
  40. return line;
  41. });
  42. };
  43. exports.alignVerticalRangeContent = alignVerticalRangeContent;
  44. //# sourceMappingURL=alignSpanningCell.js.map