Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

perf(tables): skip max-content measurement when columns fit (#11061)

Danila Katalshov8408b1fcdff5
diff --git a/packages/views/rich-content/rich-table.test.tsx b/packages/views/rich-content/rich-table.test.tsx
index a6af1f0f67..28ce679239 100644
--- a/packages/views/rich-content/rich-table.test.tsx
+++ b/packages/views/rich-content/rich-table.test.tsx
@@ -74,6 +74,26 @@ describe("RichTable", () => {
expect(screen.queryByRole("button", { name: "Reset widths" })).toBeNull();
});
+ it("skips max-content measurements when minimum column widths fit", () => {
+ const measurements: string[] = [];
+ const rect = vi
+ .spyOn(HTMLTableCellElement.prototype, "getBoundingClientRect")
+ .mockImplementation(function (this: HTMLTableCellElement) {
+ measurements.push(this.closest("table")?.style.width ?? "");
+ return new DOMRect(0, 0, 100, 20);
+ });
+ const clientWidth = vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(560);
+ try {
+ const { table } = renderTable();
+ expect(measurements).toContain("min-content");
+ expect(measurements).not.toContain("max-content");
+ expect(table).not.toHaveAttribute("data-column-widths");
+ } finally {
+ rect.mockRestore();
+ clientWidth.mockRestore();
+ }
+ });
+
it("gives an overflowing table capped content widths without saving them", () => {
const natural = { "max-content": [500, 260, 90], "min-content": [200, 330, 64] } as const;
const rect = vi
diff --git a/packages/views/rich-content/rich-table.tsx b/packages/views/rich-content/rich-table.tsx
index 5bd5589b4f..d94bd13985 100644
--- a/packages/views/rich-content/rich-table.tsx
+++ b/packages/views/rich-content/rich-table.tsx
@@ -91,7 +91,10 @@ function headerCells(table: HTMLTableElement): HTMLTableCellElement[] {
// sizing lifted and restored within the same task, so the browser paints only
// the restored layout. Only the <table> and its <col>s are touched: in the
// editor, a style written to a cell would reach ProseMirror as an edit.
-function measureColumns(table: HTMLTableElement): { min: number[]; max: number[] } {
+function measureColumns(
+ table: HTMLTableElement,
+ available?: number,
+): { min: number[]; max: number[] } {
const { style } = table;
const saved = { layout: style.tableLayout, width: style.width, minWidth: style.minWidth };
const fixed = table.hasAttribute("data-column-widths");
@@ -103,10 +106,13 @@ function measureColumns(table: HTMLTableElement): { min: number[]; max: number[]
for (const col of cols) col.style.width = "";
style.tableLayout = "auto";
style.minWidth = "0";
- style.width = "max-content";
- const max = read();
style.width = "min-content";
const min = read();
+ let max: number[] = [];
+ if (available === undefined || sum(min) > available) {
+ style.width = "max-content";
+ max = read();
+ }
style.tableLayout = saved.layout;
style.width = saved.width;
@@ -126,7 +132,7 @@ function measureColumns(table: HTMLTableElement): { min: number[]; max: number[]
const PREFERRED_MAX_COLUMN_WIDTH = 320;
function overflowWidths(table: HTMLTableElement, available: number): number[] | null {
- const { min, max } = measureColumns(table);
+ const { min, max } = measureColumns(table, available);
if (min.length === 0 || sum(min) <= available) return null;
return min.map((least, i) => Math.max(least, Math.min(max[i] ?? least, PREFERRED_MAX_COLUMN_WIDTH)));
}