feat(grid): adjust column widths by density
This commit is contained in:
1 parent
b509520382
commit
ae4bbe1564
14 files changed
+623
-330
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { averageTransposeRecordWidth, calculateTransposeRecordWidth, defaultTransposeRecordWidth, minTransposeFieldWidth, transposeFieldWidth, transposeRecordWidthsForDensity, visibleTransposeRecordWindow } from "@/lib/dataGrid/dataGridTranspose";
|
||||
|
||||
describe("dataGridTranspose density widths", () => {
|
||||
it("uses the shared density preset for record and field widths", () => {
|
||||
const values = ["x".repeat(40)];
|
||||
const columns = ["a_very_long_transpose_field_name"];
|
||||
|
||||
const compactRecord = calculateTransposeRecordWidth(values, "compact");
|
||||
const standardRecord = calculateTransposeRecordWidth(values, "standard");
|
||||
const comfortableRecord = calculateTransposeRecordWidth(values, "comfortable");
|
||||
const compactField = transposeFieldWidth(columns, { density: "compact" });
|
||||
const standardField = transposeFieldWidth(columns, { density: "standard" });
|
||||
const comfortableField = transposeFieldWidth(columns, { density: "comfortable" });
|
||||
|
||||
expect(compactRecord).toBeLessThan(standardRecord);
|
||||
expect(standardRecord).toBeLessThan(comfortableRecord);
|
||||
expect(compactField).toBeLessThan(standardField);
|
||||
expect(standardField).toBeLessThan(comfortableField);
|
||||
expect(transposeFieldWidth([], { density: "compact" })).toBe(minTransposeFieldWidth("compact"));
|
||||
});
|
||||
|
||||
it("recalculates automatic widths while preserving manual pixel overrides", () => {
|
||||
const records = [["x".repeat(40)], ["y".repeat(20)]];
|
||||
const standardWidths = transposeRecordWidthsForDensity({ records, density: "standard" });
|
||||
const comfortableWidths = transposeRecordWidthsForDensity({
|
||||
records,
|
||||
density: "comfortable",
|
||||
previousWidths: [333, standardWidths[1]],
|
||||
manualWidthIndexes: new Set([0]),
|
||||
});
|
||||
|
||||
expect(comfortableWidths[0]).toBe(333);
|
||||
expect(comfortableWidths[1]).toBeGreaterThan(standardWidths[1]);
|
||||
});
|
||||
|
||||
it("updates the virtual spacer estimate from recalculated density widths", () => {
|
||||
const records = Array.from({ length: 20 }, (_, index) => [`row-${index}-${"x".repeat(20)}`]);
|
||||
const compactWidths = transposeRecordWidthsForDensity({ records, density: "compact" });
|
||||
const comfortableWidths = transposeRecordWidthsForDensity({ records, density: "comfortable" });
|
||||
const compactWindow = visibleTransposeRecordWindow({
|
||||
totalRecords: records.length,
|
||||
scrollLeft: 500,
|
||||
viewportWidth: 400,
|
||||
pinnedWidth: transposeFieldWidth(["field"], { density: "compact" }),
|
||||
recordWidth: averageTransposeRecordWidth(compactWidths, "compact"),
|
||||
overscan: 0,
|
||||
});
|
||||
const comfortableWindow = visibleTransposeRecordWindow({
|
||||
totalRecords: records.length,
|
||||
scrollLeft: 500,
|
||||
viewportWidth: 400,
|
||||
pinnedWidth: transposeFieldWidth(["field"], { density: "comfortable" }),
|
||||
recordWidth: averageTransposeRecordWidth(comfortableWidths, "comfortable"),
|
||||
overscan: 0,
|
||||
});
|
||||
|
||||
expect(averageTransposeRecordWidth(compactWidths, "compact")).toBeLessThan(averageTransposeRecordWidth(comfortableWidths, "comfortable"));
|
||||
expect(compactWindow.beforeWidth).not.toBe(comfortableWindow.beforeWidth);
|
||||
expect(compactWindow.afterWidth).not.toBe(comfortableWindow.afterWidth);
|
||||
expect(averageTransposeRecordWidth([], "compact")).toBe(defaultTransposeRecordWidth("compact"));
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { ColumnWidthDensity } from "@/stores/settingsStore";
|
||||
|
||||
type CellValue = string | number | boolean | null;
|
||||
|
||||
export const DATA_GRID_COL_MIN_WIDTH = 60;
|
||||
@@ -10,8 +12,48 @@ export const DATA_GRID_SAMPLE_ROWS = 50;
|
||||
export const DATA_GRID_VALUE_TEXT_LIMIT = 60;
|
||||
export const DATA_GRID_AUTO_FIT_VALUE_TEXT_LIMIT = 160;
|
||||
|
||||
function estimateTextWidth(text: string, padding: number): number {
|
||||
return text.length * DATA_GRID_CHAR_WIDTH + padding;
|
||||
export interface ColumnWidthDensityPreset {
|
||||
charWidth: number;
|
||||
headerControlWidth: number;
|
||||
cellPadding: number;
|
||||
valueTextLimit: number;
|
||||
maxWidth: number;
|
||||
sampleRows: number;
|
||||
headerControlWidthCompact: number;
|
||||
}
|
||||
|
||||
export const COLUMN_WIDTH_DENSITY_PRESETS: Record<ColumnWidthDensity, ColumnWidthDensityPreset> = {
|
||||
compact: {
|
||||
charWidth: 7,
|
||||
headerControlWidth: 36,
|
||||
headerControlWidthCompact: 20,
|
||||
cellPadding: 18,
|
||||
valueTextLimit: 30,
|
||||
maxWidth: 250,
|
||||
sampleRows: 30,
|
||||
},
|
||||
standard: {
|
||||
charWidth: 8,
|
||||
headerControlWidth: 80,
|
||||
headerControlWidthCompact: 80,
|
||||
cellPadding: 28,
|
||||
valueTextLimit: 60,
|
||||
maxWidth: 400,
|
||||
sampleRows: 50,
|
||||
},
|
||||
comfortable: {
|
||||
charWidth: 9,
|
||||
headerControlWidth: 96,
|
||||
headerControlWidthCompact: 96,
|
||||
cellPadding: 36,
|
||||
valueTextLimit: 80,
|
||||
maxWidth: 600,
|
||||
sampleRows: 50,
|
||||
},
|
||||
};
|
||||
|
||||
function estimateTextWidth(text: string, padding: number, charWidth: number): number {
|
||||
return text.length * charWidth + padding;
|
||||
}
|
||||
|
||||
function displaySampleValue(value: CellValue): string | null {
|
||||
@@ -19,16 +61,19 @@ function displaySampleValue(value: CellValue): string | null {
|
||||
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
||||
}
|
||||
|
||||
export function calculateDataGridColumnWidth(options: { columnName: string; sampleValues: readonly CellValue[]; maxWidth?: number; valueTextLimit?: number }): number {
|
||||
const maxAllowedWidth = options.maxWidth ?? DATA_GRID_COL_MAX_WIDTH;
|
||||
const valueTextLimit = options.valueTextLimit ?? DATA_GRID_VALUE_TEXT_LIMIT;
|
||||
let maxContentWidth = estimateTextWidth(options.columnName, DATA_GRID_HEADER_CONTROL_WIDTH);
|
||||
export function calculateDataGridColumnWidth(options: { columnName: string; sampleValues: readonly CellValue[]; maxWidth?: number; valueTextLimit?: number; density?: ColumnWidthDensity; compactColumnHeaderActions?: boolean }): number {
|
||||
const density = options.density ?? "standard";
|
||||
const preset = COLUMN_WIDTH_DENSITY_PRESETS[density];
|
||||
const maxAllowedWidth = options.maxWidth ?? preset.maxWidth;
|
||||
const valueTextLimit = options.valueTextLimit ?? preset.valueTextLimit;
|
||||
const headerControl = options.compactColumnHeaderActions ? preset.headerControlWidthCompact : preset.headerControlWidth;
|
||||
let maxContentWidth = estimateTextWidth(options.columnName, headerControl, preset.charWidth);
|
||||
|
||||
for (const value of options.sampleValues.slice(0, DATA_GRID_SAMPLE_ROWS)) {
|
||||
for (const value of options.sampleValues.slice(0, preset.sampleRows)) {
|
||||
const text = displaySampleValue(value);
|
||||
if (text == null) continue;
|
||||
const displayLen = Math.min(text.length, valueTextLimit);
|
||||
const width = displayLen * DATA_GRID_CHAR_WIDTH + DATA_GRID_CELL_PADDING;
|
||||
const width = displayLen * preset.charWidth + preset.cellPadding;
|
||||
if (width > maxContentWidth) maxContentWidth = width;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { COLUMN_WIDTH_DENSITY_PRESETS } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import type { ColumnWidthDensity } from "@/stores/settingsStore";
|
||||
|
||||
export interface DataGridTransposeState {
|
||||
showTranspose: boolean;
|
||||
transposeRowIndex: number | null;
|
||||
@@ -82,6 +85,63 @@ export interface TransposeFieldWidthOptions {
|
||||
maxWidth?: number;
|
||||
charWidth?: number;
|
||||
padding?: number;
|
||||
density?: ColumnWidthDensity;
|
||||
}
|
||||
|
||||
export interface TransposeRecordWidthsOptions {
|
||||
records: readonly (readonly unknown[])[];
|
||||
density: ColumnWidthDensity;
|
||||
previousWidths?: readonly number[];
|
||||
manualWidthIndexes?: ReadonlySet<number>;
|
||||
}
|
||||
|
||||
const TRANSPOSE_RECORD_MIN_WIDTH = 96;
|
||||
const TRANSPOSE_RECORD_DEFAULT_WIDTH = 168;
|
||||
const TRANSPOSE_FIELD_MIN_WIDTH = 104;
|
||||
const TRANSPOSE_FIELD_MAX_WIDTH = 220;
|
||||
const STANDARD_CHAR_WIDTH = COLUMN_WIDTH_DENSITY_PRESETS.standard.charWidth;
|
||||
|
||||
function densityScaledWidth(width: number, density: ColumnWidthDensity): number {
|
||||
return Math.round((width * COLUMN_WIDTH_DENSITY_PRESETS[density].charWidth) / STANDARD_CHAR_WIDTH);
|
||||
}
|
||||
|
||||
function transposeDisplayText(value: unknown): string {
|
||||
if (value === null || value === undefined) return "";
|
||||
return typeof value === "object" ? (JSON.stringify(value) ?? String(value)) : String(value);
|
||||
}
|
||||
|
||||
export function defaultTransposeRecordWidth(density: ColumnWidthDensity): number {
|
||||
return densityScaledWidth(TRANSPOSE_RECORD_DEFAULT_WIDTH, density);
|
||||
}
|
||||
|
||||
export function minTransposeRecordWidth(density: ColumnWidthDensity): number {
|
||||
return densityScaledWidth(TRANSPOSE_RECORD_MIN_WIDTH, density);
|
||||
}
|
||||
|
||||
export function minTransposeFieldWidth(density: ColumnWidthDensity): number {
|
||||
return densityScaledWidth(TRANSPOSE_FIELD_MIN_WIDTH, density);
|
||||
}
|
||||
|
||||
export function calculateTransposeRecordWidth(values: readonly unknown[], density: ColumnWidthDensity): number {
|
||||
const preset = COLUMN_WIDTH_DENSITY_PRESETS[density];
|
||||
const minWidth = minTransposeRecordWidth(density);
|
||||
let maxWidth = minWidth;
|
||||
for (const value of values) {
|
||||
const displayLen = Math.min(transposeDisplayText(value).length, preset.valueTextLimit);
|
||||
maxWidth = Math.max(maxWidth, displayLen * preset.charWidth + preset.cellPadding);
|
||||
}
|
||||
return Math.max(minWidth, Math.min(preset.maxWidth, Math.round(maxWidth)));
|
||||
}
|
||||
|
||||
export function transposeRecordWidthsForDensity(options: TransposeRecordWidthsOptions): number[] {
|
||||
const previousWidths = options.previousWidths ?? [];
|
||||
const manualWidthIndexes = options.manualWidthIndexes ?? new Set<number>();
|
||||
return options.records.map((record, index) => (manualWidthIndexes.has(index) && previousWidths[index] !== undefined ? previousWidths[index] : calculateTransposeRecordWidth(record, options.density)));
|
||||
}
|
||||
|
||||
export function averageTransposeRecordWidth(widths: readonly number[], density: ColumnWidthDensity): number {
|
||||
if (widths.length === 0) return defaultTransposeRecordWidth(density);
|
||||
return widths.reduce((sum, width) => sum + width, 0) / widths.length;
|
||||
}
|
||||
|
||||
export interface TransposeScrollLeftOptions {
|
||||
@@ -218,10 +278,12 @@ export function transposeAnchorRowIndex(options: TransposeAnchorOptions): number
|
||||
}
|
||||
|
||||
export function transposeFieldWidth(columns: string[], options: TransposeFieldWidthOptions = {}): number {
|
||||
const minWidth = options.minWidth ?? 104;
|
||||
const maxWidth = options.maxWidth ?? 220;
|
||||
const charWidth = options.charWidth ?? 8;
|
||||
const padding = options.padding ?? 32;
|
||||
const density = options.density ?? "standard";
|
||||
const preset = COLUMN_WIDTH_DENSITY_PRESETS[density];
|
||||
const minWidth = options.minWidth ?? minTransposeFieldWidth(density);
|
||||
const maxWidth = options.maxWidth ?? densityScaledWidth(TRANSPOSE_FIELD_MAX_WIDTH, density);
|
||||
const charWidth = options.charWidth ?? preset.charWidth;
|
||||
const padding = options.padding ?? preset.cellPadding + 4;
|
||||
const longest = columns.reduce((max, column) => Math.max(max, column.length), 0);
|
||||
return Math.min(maxWidth, Math.max(minWidth, Math.ceil(longest * charWidth + padding)));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user