Skip to content

Commit 36a6ea8

Browse files
committed
Support probing row height pre-render
1 parent 613a272 commit 36a6ea8

2 files changed

Lines changed: 59 additions & 9 deletions

File tree

src/ts/regular-table.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ import {
2020
import { get_draw_fps } from "./utils";
2121
import { METADATA_MAP } from "./view_model";
2222

23-
type VirtualMode = "both" | "horizontal" | "vertical" | "none";
23+
export type VirtualMode = "both" | "horizontal" | "vertical" | "none";
2424

2525
const VIRTUAL_MODES: VirtualMode[] = ["both", "horizontal", "vertical", "none"];
2626

27+
export type ResetAutoSizeOptions = {
28+
auto: boolean;
29+
override: boolean;
30+
indices: boolean;
31+
row_height: boolean;
32+
};
33+
2734
/**
2835
* The `<regular-table>` custom element.
2936
*
@@ -64,10 +71,22 @@ export class RegularTableElement extends RegularViewEventModel {
6471
* Reset column autosizing, such that column sizes will be recalculated
6572
* on the next draw() call.
6673
*/
67-
resetAutoSize() {
68-
this._column_sizes.auto = [];
69-
this._column_sizes.override = {};
70-
this._column_sizes.indices = [];
74+
resetAutoSize(options?: ResetAutoSizeOptions) {
75+
if (!options || options.auto) {
76+
this._column_sizes.auto = [];
77+
}
78+
79+
if (!options || options.override) {
80+
this._column_sizes.override = {};
81+
}
82+
83+
if (!options || options.indices) {
84+
this._column_sizes.indices = [];
85+
}
86+
87+
if (!options || options.row_height) {
88+
this._column_sizes.row_height = undefined;
89+
}
7190
}
7291

7392
/**
@@ -271,7 +290,7 @@ export class RegularTableElement extends RegularViewEventModel {
271290
return;
272291
}
273292

274-
const viewport_row_height = this._column_sizes.row_height || 19;
293+
const viewport_row_height = this._column_sizes.row_height || 0;
275294
this.scrollTop = Math.ceil(viewport_row_height * y);
276295
let scroll_left = 0;
277296
while (x > 0) {

src/ts/scroll_panel.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class RegularVirtualTableViewModel extends HTMLElement {
9595
protected _is_styling?: boolean;
9696
protected table_model!: RegularTableViewModel;
9797
protected _style_callbacks!: Array<StyleCallback>;
98+
private _probe_element?: [HTMLElement, HTMLElement];
9899

99100
/**
100101
* Draws this virtual panel, given an object of render options that allow
@@ -123,6 +124,30 @@ export class RegularVirtualTableViewModel extends HTMLElement {
123124
await flush_tag(this);
124125
}
125126

127+
/**
128+
* Probe the shadow DOM to measure the default row height from CSS.
129+
*/
130+
protected _probe_row_height() {
131+
if (!this._probe_element) {
132+
this._probe_element = [
133+
document.createElement("table"),
134+
document.createElement("td"),
135+
];
136+
this._probe_element[0].style.visibility = "hidden";
137+
this._probe_element[0].style.position = "absolute";
138+
const tbody = document.createElement("tbody");
139+
const tr = document.createElement("tr");
140+
this._probe_element[1].textContent = "X";
141+
tr.appendChild(this._probe_element[1]);
142+
tbody.appendChild(tr);
143+
this._probe_element[0].appendChild(tbody);
144+
}
145+
146+
this.appendChild(this._probe_element[0]);
147+
this._column_sizes.row_height = this._probe_element[1].offsetHeight;
148+
this._probe_element[0].remove();
149+
}
150+
126151
/**
127152
* Create the DOM for this `shadowRoot`.
128153
*/
@@ -243,11 +268,13 @@ export class RegularVirtualTableViewModel extends HTMLElement {
243268
for (const w of this._column_sizes.indices) {
244269
totalWidth += w || 0;
245270
}
271+
246272
this._virtual_panel.style.width =
247273
totalWidth.toPrecision(10) + "px";
248274
} else {
249275
const virtual_width =
250276
this._calc_scrollable_column_width(num_columns);
277+
251278
if (virtual_width !== 0) {
252279
const panel_width =
253280
this._container_size.width + virtual_width + 2;
@@ -266,7 +293,7 @@ export class RegularVirtualTableViewModel extends HTMLElement {
266293
* @param {*} nrows
267294
*/
268295
protected _update_virtual_panel_height(nrows: number): void {
269-
const { row_height = 19 } = this._column_sizes;
296+
const { row_height = 0 } = this._column_sizes;
270297
const header_height =
271298
this._view_cache.column_headers_length * row_height;
272299
let virtual_panel_px_size;
@@ -287,7 +314,7 @@ export class RegularVirtualTableViewModel extends HTMLElement {
287314
last_cell?: CellTuple,
288315
): void {
289316
const y_offset =
290-
(this._column_sizes.row_height || 20) * (viewport.start_row % 1) ||
317+
(this._column_sizes.row_height || 0) * (viewport.start_row % 1) ||
291318
0;
292319

293320
const x_offset_index =
@@ -379,7 +406,7 @@ export class RegularVirtualTableViewModel extends HTMLElement {
379406
end_row: number;
380407
} {
381408
const { height, containerHeight } = this._container_size;
382-
const row_height = this._column_sizes.row_height || 19;
409+
const row_height = this._column_sizes.row_height || 0;
383410
const header_levels = this._view_cache.column_headers_length;
384411
const total_scroll_height = Math.max(
385412
1,
@@ -521,6 +548,10 @@ async function internal_draw(
521548
} = await this.table_model._getDimState(this._view_cache);
522549

523550
this._column_sizes.row_height = row_height || this._column_sizes.row_height;
551+
if (!this._column_sizes.row_height) {
552+
this._probe_row_height();
553+
}
554+
524555
if (num_row_headers !== undefined) {
525556
this._view_cache.row_headers_length = num_row_headers;
526557
}

0 commit comments

Comments
 (0)