-
Notifications
You must be signed in to change notification settings - Fork 821
/
row_heights.ts
339 lines (289 loc) Β· 8.98 KB
/
row_heights.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import {
useEffect,
useState,
useMemo,
useCallback,
useContext,
CSSProperties,
} from 'react';
import type { VariableSizeGrid as Grid } from 'react-window';
import { isObject, isNumber } from '../../../services/predicate';
import {
EuiDataGridStyleCellPaddings,
EuiDataGridStyle,
EuiDataGridRowHeightOption,
EuiDataGridRowHeightsOptions,
EuiDataGridColumn,
} from '../data_grid_types';
import { DataGridSortingContext } from './sorting';
// TODO: Once JS variables are available, use them here instead of hard-coded maps
export const cellPaddingsMap: Record<EuiDataGridStyleCellPaddings, number> = {
s: 4,
m: 6,
l: 8,
};
export const AUTO_HEIGHT = 'auto';
export const DEFAULT_ROW_HEIGHT = 34;
export class RowHeightUtils {
getRowHeightOption(
rowIndex: number,
rowHeightsOptions?: EuiDataGridRowHeightsOptions
): EuiDataGridRowHeightOption | undefined {
return (
rowHeightsOptions?.rowHeights?.[rowIndex] ??
rowHeightsOptions?.defaultHeight
);
}
isRowHeightOverride(
rowIndex: number,
rowHeightsOptions?: EuiDataGridRowHeightsOptions
): boolean {
return rowHeightsOptions?.rowHeights?.[rowIndex] != null;
}
getCalculatedHeight(
heightOption: EuiDataGridRowHeightOption,
defaultHeight: number,
rowIndex?: number,
isRowHeightOverride?: boolean
) {
if (isObject(heightOption) && heightOption.height) {
return Math.max(heightOption.height, defaultHeight);
}
if (heightOption && isNumber(heightOption)) {
return Math.max(heightOption, defaultHeight);
}
if (isObject(heightOption) && heightOption.lineCount) {
if (isRowHeightOverride) {
return this.getRowHeight(rowIndex!) || defaultHeight; // lineCount overrides are stored in the heights cache
} else {
return defaultHeight; // default lineCount height is set in minRowHeight state in grid_row_body
}
}
if (heightOption === AUTO_HEIGHT && rowIndex != null) {
return this.getRowHeight(rowIndex);
}
return defaultHeight;
}
/**
* Styles utils
*/
private styles: {
paddingTop: number;
paddingBottom: number;
} = {
paddingTop: 0,
paddingBottom: 0,
};
cacheStyles(gridStyles: EuiDataGridStyle) {
this.styles = {
paddingTop: cellPaddingsMap[gridStyles.cellPadding!],
paddingBottom: cellPaddingsMap[gridStyles.cellPadding!],
};
}
getStylesForCell = (
rowHeightsOptions: EuiDataGridRowHeightsOptions,
rowIndex: number
): CSSProperties => {
const height = this.getRowHeightOption(rowIndex, rowHeightsOptions);
if (height === AUTO_HEIGHT) {
return {};
}
const lineCount = this.getLineCount(height);
if (lineCount) {
return {
WebkitLineClamp: lineCount,
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
height: '100%',
overflow: 'hidden',
};
}
return {
height: '100%',
overflow: 'hidden',
};
};
/**
* Line count utils
*/
getLineCount(option?: EuiDataGridRowHeightOption) {
return isObject(option) ? option.lineCount : undefined;
}
calculateHeightForLineCount(
cellRef: HTMLElement,
lineCount: number,
excludePadding?: boolean
) {
const computedStyles = window.getComputedStyle(cellRef, null);
const lineHeight = parseInt(computedStyles.lineHeight, 10);
const contentHeight = Math.ceil(lineCount * lineHeight);
return excludePadding
? contentHeight
: contentHeight + this.styles.paddingTop + this.styles.paddingBottom;
}
/**
* Auto height utils
*/
private heightsCache = new Map<number, Map<string, number>>();
private timerId?: number;
private grid?: Grid;
private lastUpdatedRow: number = Infinity;
private rerenderGridBody: Function = () => {};
isAutoHeight(
rowIndex: number,
rowHeightsOptions?: EuiDataGridRowHeightsOptions
) {
const height = this.getRowHeightOption(rowIndex, rowHeightsOptions);
if (height === AUTO_HEIGHT) {
return true;
}
return false;
}
getRowHeight(rowIndex: number) {
const rowHeights = this.heightsCache.get(rowIndex);
if (rowHeights == null) return 0;
const rowHeightValues = Array.from(rowHeights.values());
if (!rowHeightValues.length) return 0;
return Math.max(...rowHeightValues);
}
setRowHeight(
rowIndex: number,
colId: string,
height: number = DEFAULT_ROW_HEIGHT,
visibleRowIndex: number
) {
const rowHeights =
this.heightsCache.get(rowIndex) || new Map<string, number>();
const adaptedHeight = Math.ceil(
height + this.styles.paddingTop + this.styles.paddingBottom
);
if (rowHeights.get(colId) === adaptedHeight) {
return;
}
rowHeights.set(colId, adaptedHeight);
this.heightsCache.set(rowIndex, rowHeights);
this.resetRow(visibleRowIndex);
this.rerenderGridBody();
}
pruneHiddenColumnHeights(visibleColumns: EuiDataGridColumn[]) {
const visibleColumnIds = new Set(visibleColumns.map(({ id }) => id));
let didModify = false;
this.heightsCache.forEach((rowHeights) => {
const existingColumnIds = Array.from(rowHeights.keys());
existingColumnIds.forEach((existingColumnId) => {
if (visibleColumnIds.has(existingColumnId) === false) {
didModify = true;
rowHeights.delete(existingColumnId);
}
});
});
if (didModify) {
this.resetRow(0);
}
}
resetRow(visibleRowIndex: number) {
// save the first row index of batch, reassigning it only
// if this visible row index less than lastUpdatedRow
this.lastUpdatedRow = Math.min(this.lastUpdatedRow, visibleRowIndex);
clearTimeout(this.timerId);
this.timerId = window.setTimeout(() => this.resetGrid(), 0);
}
resetGrid() {
this.grid?.resetAfterRowIndex(this.lastUpdatedRow);
this.lastUpdatedRow = Infinity;
}
setGrid(grid: Grid) {
this.grid = grid;
}
setRerenderGridBody(rerenderGridBody: Function) {
this.rerenderGridBody = rerenderGridBody;
}
}
/**
* Hook for instantiating RowHeightUtils, and also updating
* internal vars from outside props via useEffects
*/
export const useRowHeightUtils = ({
gridRef,
gridStyles,
columns,
}: {
gridRef: Grid | null;
gridStyles: EuiDataGridStyle;
columns: EuiDataGridColumn[];
}) => {
const rowHeightUtils = useMemo(() => new RowHeightUtils(), []);
// Update rowHeightUtils with grid ref
useEffect(() => {
if (gridRef) rowHeightUtils.setGrid(gridRef);
}, [gridRef, rowHeightUtils]);
// Re-cache styles whenever grid density changes
useEffect(() => {
rowHeightUtils.cacheStyles({
cellPadding: gridStyles.cellPadding,
});
}, [gridStyles.cellPadding, rowHeightUtils]);
// Update row heights map to remove hidden columns whenever orderedVisibleColumns change
useEffect(() => {
rowHeightUtils.pruneHiddenColumnHeights(columns);
}, [rowHeightUtils, columns]);
return rowHeightUtils;
};
export const useDefaultRowHeight = ({
rowHeightsOptions,
rowHeightUtils,
}: {
rowHeightsOptions?: EuiDataGridRowHeightsOptions;
rowHeightUtils: RowHeightUtils;
}) => {
const { getCorrectRowIndex } = useContext(DataGridSortingContext);
// `minRowHeight` is primarily used by undefined & lineCount heights
// and ignored by auto & static heights (unless the static height is < the min)
const [minRowHeight, setRowHeight] = useState(DEFAULT_ROW_HEIGHT);
// Default/fallback height for all rows
const defaultRowHeight = useMemo(() => {
return rowHeightsOptions?.defaultHeight
? rowHeightUtils.getCalculatedHeight(
rowHeightsOptions.defaultHeight,
minRowHeight
)
: minRowHeight;
}, [rowHeightsOptions, minRowHeight, rowHeightUtils]);
// Used by react-window's Grid component to determine actual row heights
const getRowHeight = useCallback(
(rowIndex: number) => {
const correctRowIndex = getCorrectRowIndex(rowIndex);
let rowHeight;
// Account for row-specific height overrides
const rowHeightOption = rowHeightUtils.getRowHeightOption(
correctRowIndex,
rowHeightsOptions
);
if (rowHeightOption) {
rowHeight = rowHeightUtils.getCalculatedHeight(
rowHeightOption,
minRowHeight,
correctRowIndex,
rowHeightUtils.isRowHeightOverride(correctRowIndex, rowHeightsOptions)
);
}
// Use the row-specific height if it exists, if not, fall back to the default
return rowHeight || defaultRowHeight;
},
[
minRowHeight,
rowHeightsOptions,
getCorrectRowIndex,
rowHeightUtils,
defaultRowHeight,
]
);
return { defaultRowHeight, setRowHeight, getRowHeight };
};