-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathspanningCellManager.ts
156 lines (129 loc) · 4.56 KB
/
spanningCellManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
wrapRangeContent, alignVerticalRangeContent,
} from './alignSpanningCell';
import {
calculateSpanningCellWidth,
} from './calculateSpanningCellWidth';
import {
makeRangeConfig,
} from './makeRangeConfig';
import type {
DrawHorizontalLine,
DrawVerticalLine,
SpanningCellConfig,
} from './types/api';
import type {
CellCoordinates,
ColumnConfig,
RangeConfig,
ResolvedRangeConfig,
Row,
} from './types/internal';
import {
areCellEqual,
flatten,
isCellInRange, sequence, sumArray,
} from './utils';
export type SpanningCellManager = {
getContainingRange: (cell: CellCoordinates, options?: {mapped: true, }) => ResolvedRangeConfig | undefined,
inSameRange: (cell1: CellCoordinates, cell2: CellCoordinates) => boolean,
rowHeights: number[],
setRowHeights: (rowHeights: number[]) => void,
rowIndexMapping: number[],
setRowIndexMapping: (mappedRowHeights: number[]) => void,
};
export type SpanningCellParameters = {
spanningCellConfigs: SpanningCellConfig[],
rows: Row[],
columnsConfig: ColumnConfig[],
drawVerticalLine: DrawVerticalLine,
drawHorizontalLine: DrawHorizontalLine,
};
export type SpanningCellContext = SpanningCellParameters & {
rowHeights: number[],
};
const findRangeConfig = (cell: CellCoordinates, rangeConfigs: RangeConfig[]): RangeConfig | undefined => {
return rangeConfigs.find((rangeCoordinate) => {
return isCellInRange(cell, rangeCoordinate);
});
};
const getContainingRange = (rangeConfig: RangeConfig, context: SpanningCellContext): ResolvedRangeConfig | undefined => {
const width = calculateSpanningCellWidth(rangeConfig, context);
const wrappedContent = wrapRangeContent(rangeConfig, width, context);
const alignedContent = alignVerticalRangeContent(rangeConfig, wrappedContent, context);
const getCellContent = (rowIndex: number) => {
const {topLeft} = rangeConfig;
const {drawHorizontalLine, rowHeights} = context;
const totalWithinHorizontalBorderHeight = rowIndex - topLeft.row;
const totalHiddenHorizontalBorderHeight = sequence(topLeft.row + 1, rowIndex).filter((index) => {
/* istanbul ignore next */
return !drawHorizontalLine?.(index, rowHeights.length);
}).length;
const offset = sumArray(rowHeights.slice(topLeft.row, rowIndex)) + totalWithinHorizontalBorderHeight - totalHiddenHorizontalBorderHeight;
return alignedContent.slice(offset, offset + rowHeights[rowIndex]);
};
const getBorderContent = (borderIndex: number) => {
const {topLeft} = rangeConfig;
const offset = sumArray(context.rowHeights.slice(topLeft.row, borderIndex)) + (borderIndex - topLeft.row - 1);
return alignedContent[offset];
};
return {
...rangeConfig,
extractBorderContent: getBorderContent,
extractCellContent: getCellContent,
height: wrappedContent.length,
width,
};
};
const inSameRange = (cell1: CellCoordinates, cell2: CellCoordinates, ranges: RangeConfig[]): boolean => {
const range1 = findRangeConfig(cell1, ranges);
const range2 = findRangeConfig(cell2, ranges);
if (range1 && range2) {
return areCellEqual(range1.topLeft, range2.topLeft);
}
return false;
};
const hashRange = (range: RangeConfig): string => {
const {row, col} = range.topLeft;
return `${row}/${col}`;
};
export const createSpanningCellManager = (parameters: SpanningCellParameters): SpanningCellManager => {
const {spanningCellConfigs, columnsConfig} = parameters;
const ranges = spanningCellConfigs.map((config) => {
return makeRangeConfig(config, columnsConfig);
});
const rangeCache: Record<string, ResolvedRangeConfig | undefined> = {};
let rowHeights: number[] = [];
let rowIndexMapping: number[] = [];
return {getContainingRange: (cell, options) => {
const originalRow = options?.mapped ? rowIndexMapping[cell.row] : cell.row;
const range = findRangeConfig({...cell,
row: originalRow}, ranges);
if (!range) {
return undefined;
}
if (rowHeights.length === 0) {
return getContainingRange(range, {...parameters,
rowHeights});
}
const hash = hashRange(range);
rangeCache[hash] ??= getContainingRange(range, {...parameters,
rowHeights});
return rangeCache[hash];
},
inSameRange: (cell1, cell2) => {
return inSameRange(cell1, cell2, ranges);
},
rowHeights,
rowIndexMapping,
setRowHeights: (_rowHeights: number[]) => {
rowHeights = _rowHeights;
},
setRowIndexMapping: (mappedRowHeights: number[]) => {
rowIndexMapping = flatten(mappedRowHeights.map((height, index) => {
return Array.from({length: height}, () => {
return index;
});
}));
}};
};