UNPKG

3.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createSpanningCellManager = void 0;
4const alignSpanningCell_1 = require("./alignSpanningCell");
5const calculateSpanningCellWidth_1 = require("./calculateSpanningCellWidth");
6const makeRangeConfig_1 = require("./makeRangeConfig");
7const utils_1 = require("./utils");
8const findRangeConfig = (cell, rangeConfigs) => {
9 return rangeConfigs.find((rangeCoordinate) => {
10 return (0, utils_1.isCellInRange)(cell, rangeCoordinate);
11 });
12};
13const 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};
41const 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};
49const hashRange = (range) => {
50 const { row, col } = range.topLeft;
51 return `${row}/${col}`;
52};
53const 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};
85exports.createSpanningCellManager = createSpanningCellManager;
86//# sourceMappingURL=spanningCellManager.js.map
\No newline at end of file