alignSpanningCell.js 2.5 KB

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