feat(grid): add configurable column width density
This commit is contained in:
1 parent
ea22dc3652
commit
6c661e8744
7 files changed
+180
-34
No files matched your search
@@ -20,6 +20,12 @@ describe("dataGridTranspose density widths", () => {
|
||||
expect(transposeFieldWidth([], { density: "compact" })).toBe(minTransposeFieldWidth("compact"));
|
||||
});
|
||||
|
||||
it("keeps compact record width independent of field order", () => {
|
||||
const longValue = "x".repeat(100);
|
||||
|
||||
expect(calculateTransposeRecordWidth([longValue, "a"], "compact")).toBe(calculateTransposeRecordWidth(["a", longValue], "compact"));
|
||||
});
|
||||
|
||||
it("recalculates automatic widths while preserving manual pixel overrides", () => {
|
||||
const records = [["x".repeat(40)], ["y".repeat(20)]];
|
||||
const standardWidths = transposeRecordWidthsForDensity({ records, density: "standard" });
|
||||
|
||||
@@ -15,45 +15,60 @@ export const DATA_GRID_AUTO_FIT_VALUE_TEXT_LIMIT = 160;
|
||||
export interface ColumnWidthDensityPreset {
|
||||
charWidth: number;
|
||||
headerControlWidth: number;
|
||||
headerControlWidthCompact: number;
|
||||
cellPadding: number;
|
||||
valueTextLimit: number;
|
||||
maxWidth: number;
|
||||
sampleRows: number;
|
||||
headerControlWidthCompact: number;
|
||||
// 采样值宽度百分位(0-100)。100=最大值,<100 忽略离群值实现自适应
|
||||
valueWidthPercentile: number;
|
||||
}
|
||||
|
||||
export const COLUMN_WIDTH_DENSITY_PRESETS: Record<ColumnWidthDensity, ColumnWidthDensityPreset> = {
|
||||
compact: {
|
||||
// 紧凑:以字段名为基准。控件垂直堆叠 16px + padding 16px + gap 4px + border 1px = 37 + 渲染余量 8px = 45
|
||||
charWidth: 7,
|
||||
headerControlWidth: 36,
|
||||
headerControlWidthCompact: 20,
|
||||
cellPadding: 18,
|
||||
valueTextLimit: 30,
|
||||
maxWidth: 250,
|
||||
headerControlWidth: 45,
|
||||
headerControlWidthCompact: 45,
|
||||
cellPadding: 24,
|
||||
valueTextLimit: 20,
|
||||
maxWidth: 480,
|
||||
sampleRows: 30,
|
||||
valueWidthPercentile: 100,
|
||||
},
|
||||
standard: {
|
||||
// 标准:紧凑表头,减少固定开销
|
||||
// compactActions → 实际 57px + 2px 余量 = 59
|
||||
// 非 compactActions → 实际 81px + 2px 余量 = 83
|
||||
charWidth: 8,
|
||||
headerControlWidth: 80,
|
||||
headerControlWidthCompact: 80,
|
||||
cellPadding: 28,
|
||||
valueTextLimit: 60,
|
||||
maxWidth: 400,
|
||||
headerControlWidth: 83,
|
||||
headerControlWidthCompact: 59,
|
||||
cellPadding: 24,
|
||||
valueTextLimit: 40,
|
||||
maxWidth: 360,
|
||||
sampleRows: 50,
|
||||
valueWidthPercentile: 90,
|
||||
},
|
||||
comfortable: {
|
||||
charWidth: 9,
|
||||
headerControlWidth: 96,
|
||||
headerControlWidthCompact: 96,
|
||||
cellPadding: 36,
|
||||
valueTextLimit: 80,
|
||||
// 宽松:展示更多内容,valueTextLimit=120 几乎不截断,maxWidth=600
|
||||
charWidth: 8,
|
||||
headerControlWidth: 83,
|
||||
headerControlWidthCompact: 59,
|
||||
cellPadding: 24,
|
||||
valueTextLimit: 120,
|
||||
maxWidth: 600,
|
||||
sampleRows: 50,
|
||||
valueWidthPercentile: 95,
|
||||
},
|
||||
};
|
||||
|
||||
// 全角字符(CJK 等)按 2 倍宽度估算,确保中文字段名完整显示
|
||||
function estimateTextWidth(text: string, padding: number, charWidth: number): number {
|
||||
return text.length * charWidth + padding;
|
||||
let width = 0;
|
||||
for (const ch of text) {
|
||||
width += ch.codePointAt(0)! > 0x7e ? charWidth * 2 : charWidth;
|
||||
}
|
||||
return width + padding;
|
||||
}
|
||||
|
||||
function displaySampleValue(value: CellValue): string | null {
|
||||
@@ -61,21 +76,44 @@ 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; density?: ColumnWidthDensity; compactColumnHeaderActions?: boolean }): number {
|
||||
// 取排序后第 percentile 百分位的值
|
||||
export function percentileValue(values: number[], percentile: number): number {
|
||||
if (values.length === 0) return 0;
|
||||
// P100 must be independent of input order and does not require sorting.
|
||||
if (percentile >= 100) {
|
||||
let maximum = values[0];
|
||||
for (let index = 1; index < values.length; index++) maximum = Math.max(maximum, values[index]);
|
||||
return maximum;
|
||||
}
|
||||
if (values.length === 1) return values[0];
|
||||
const sorted = [...values].sort((a, b) => a - b);
|
||||
const idx = Math.min(sorted.length - 1, Math.floor((percentile / 100) * sorted.length));
|
||||
return sorted[idx];
|
||||
}
|
||||
|
||||
export function calculateDataGridColumnWidth(options: { columnName: string; sampleValues: readonly CellValue[]; maxWidth?: number; valueTextLimit?: number; density?: ColumnWidthDensity; compactColumnHeaderActions?: boolean; includeValues?: 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);
|
||||
const headerWidth = estimateTextWidth(options.columnName, headerControl, preset.charWidth);
|
||||
|
||||
// Compact defaults to header-only sizing, while explicit auto-fit still measures values.
|
||||
if (density === "compact" && !options.includeValues) {
|
||||
return Math.max(DATA_GRID_COL_MIN_WIDTH, Math.min(maxAllowedWidth, Math.round(headerWidth)));
|
||||
}
|
||||
|
||||
const valueWidths: number[] = [];
|
||||
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 * preset.charWidth + preset.cellPadding;
|
||||
if (width > maxContentWidth) maxContentWidth = width;
|
||||
valueWidths.push(displayLen * preset.charWidth + preset.cellPadding);
|
||||
}
|
||||
|
||||
const valueWidth = percentileValue(valueWidths, preset.valueWidthPercentile);
|
||||
const maxContentWidth = Math.max(headerWidth, valueWidth);
|
||||
|
||||
return Math.max(DATA_GRID_COL_MIN_WIDTH, Math.min(maxAllowedWidth, Math.round(maxContentWidth)));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { COLUMN_WIDTH_DENSITY_PRESETS } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import { COLUMN_WIDTH_DENSITY_PRESETS, percentileValue } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import type { ColumnWidthDensity } from "@/stores/settingsStore";
|
||||
|
||||
export interface DataGridTransposeState {
|
||||
@@ -99,10 +99,16 @@ 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;
|
||||
|
||||
// 转置视图每种密度对应的缩放因子(不再依赖 charWidth)
|
||||
const TRANSPOSE_DENSITY_SCALE: Record<ColumnWidthDensity, number> = {
|
||||
compact: 0.875,
|
||||
standard: 1,
|
||||
comfortable: 1.125,
|
||||
};
|
||||
|
||||
function densityScaledWidth(width: number, density: ColumnWidthDensity): number {
|
||||
return Math.round((width * COLUMN_WIDTH_DENSITY_PRESETS[density].charWidth) / STANDARD_CHAR_WIDTH);
|
||||
return Math.round(width * TRANSPOSE_DENSITY_SCALE[density]);
|
||||
}
|
||||
|
||||
function transposeDisplayText(value: unknown): string {
|
||||
@@ -125,11 +131,13 @@ export function minTransposeFieldWidth(density: ColumnWidthDensity): number {
|
||||
export function calculateTransposeRecordWidth(values: readonly unknown[], density: ColumnWidthDensity): number {
|
||||
const preset = COLUMN_WIDTH_DENSITY_PRESETS[density];
|
||||
const minWidth = minTransposeRecordWidth(density);
|
||||
let maxWidth = minWidth;
|
||||
const scale = TRANSPOSE_DENSITY_SCALE[density];
|
||||
const valueWidths: number[] = [];
|
||||
for (const value of values) {
|
||||
const displayLen = Math.min(transposeDisplayText(value).length, preset.valueTextLimit);
|
||||
maxWidth = Math.max(maxWidth, displayLen * preset.charWidth + preset.cellPadding);
|
||||
valueWidths.push(Math.round((displayLen * preset.charWidth + preset.cellPadding) * scale));
|
||||
}
|
||||
const maxWidth = Math.max(minWidth, percentileValue(valueWidths, preset.valueWidthPercentile));
|
||||
return Math.max(minWidth, Math.min(preset.maxWidth, Math.round(maxWidth)));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user