spanningCellManager.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createSpanningCellManager = void 0;
  4. const alignSpanningCell_1 = require("./alignSpanningCell");
  5. const calculateSpanningCellWidth_1 = require("./calculateSpanningCellWidth");
  6. const makeRangeConfig_1 = require("./makeRangeConfig");
  7. const utils_1 = require("./utils");
  8. const findRangeConfig = (cell, rangeConfigs) => {
  9. return rangeConfigs.find((rangeCoordinate) => {
  10. return (0, utils_1.isCellInRange)(cell, rangeCoordinate);
  11. });
  12. };
  13. const getContainingRange = (rangeConfig, context) => {
  14. const width = (0, calculateSpanningCellWidth_1.calculateSpanningCellWidth)(rangeConfig, context);
  15. const wrappedContent = (0, alignSpanningCell_1.wrapRangeContent)(rangeConfig, width, context);
  16. const alignedContent = (0, alignSpanningCell_1.alignVerticalRangeContent)(rangeConfig, wrappedContent, context);
  17. const getCellContent = (rowIndex) => {
  18. const { topLeft } = rangeConfig;
  19. const { drawHorizontalLine, rowHeights } = context;
  20. const totalWithinHorizontalBorderHeight = rowIndex - topLeft.row;
  21. const totalHiddenHorizontalBorderHeight = (0, utils_1.sequence)(topLeft.row + 1, rowIndex).filter((index) => {
  22. /* istanbul ignore next */
  23. return !(drawHorizontalLine === null || drawHorizontalLine === void 0 ? void 0 : drawHorizontalLine(index, rowHeights.length));
  24. }).length;
  25. const offset = (0, utils_1.sumArray)(rowHeights.slice(topLeft.row, rowIndex)) + totalWithinHorizontalBorderHeight - totalHiddenHorizontalBorderHeight;
  26. return alignedContent.slice(offset, offset + rowHeights[rowIndex]);
  27. };
  28. const getBorderContent = (borderIndex) => {
  29. const { topLeft } = rangeConfig;
  30. const offset = (0, utils_1.sumArray)(context.rowHeights.slice(topLeft.row, borderIndex)) + (borderIndex - topLeft.row - 1);
  31. return alignedContent[offset];
  32. };
  33. return {
  34. ...rangeConfig,
  35. extractBorderContent: getBorderContent,
  36. extractCellContent: getCellContent,
  37. height: wrappedContent.length,
  38. width,
  39. };
  40. };
  41. const inSameRange = (cell1, cell2, ranges) => {
  42. const range1 = findRangeConfig(cell1, ranges);
  43. const range2 = findRangeConfig(cell2, ranges);
  44. if (range1 && range2) {
  45. return (0, utils_1.areCellEqual)(range1.topLeft, range2.topLeft);
  46. }
  47. return false;
  48. };
  49. const hashRange = (range) => {
  50. const { row, col } = range.topLeft;
  51. return `${row}/${col}`;
  52. };
  53. const createSpanningCellManager = (parameters) => {
  54. const { spanningCellConfigs, columnsConfig } = parameters;
  55. const ranges = spanningCellConfigs.map((config) => {
  56. return (0, makeRangeConfig_1.makeRangeConfig)(config, columnsConfig);
  57. });
  58. const rangeCache = {};
  59. let rowHeights = [];
  60. return { getContainingRange: (cell, options) => {
  61. var _a;
  62. const originalRow = (options === null || options === void 0 ? void 0 : options.mapped) ? (0, utils_1.findOriginalRowIndex)(rowHeights, cell.row) : cell.row;
  63. const range = findRangeConfig({ ...cell,
  64. row: originalRow }, ranges);
  65. if (!range) {
  66. return undefined;
  67. }
  68. if (rowHeights.length === 0) {
  69. return getContainingRange(range, { ...parameters,
  70. rowHeights });
  71. }
  72. const hash = hashRange(range);
  73. (_a = rangeCache[hash]) !== null && _a !== void 0 ? _a : (rangeCache[hash] = getContainingRange(range, { ...parameters,
  74. rowHeights }));
  75. return rangeCache[hash];
  76. },
  77. inSameRange: (cell1, cell2) => {
  78. return inSameRange(cell1, cell2, ranges);
  79. },
  80. rowHeights,
  81. setRowHeights: (_rowHeights) => {
  82. rowHeights = _rowHeights;
  83. } };
  84. };
  85. exports.createSpanningCellManager = createSpanningCellManager;
  86. //# sourceMappingURL=spanningCellManager.js.map