mirror of
https://github.com/t8y2/dbx.git
synced 2026-11-02 00:29:30 +00:00
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
type CellValue = string | number | boolean | null;
|
|
|
|
export const DATA_GRID_COL_MIN_WIDTH = 60;
|
|
export const DATA_GRID_COL_MAX_WIDTH = 400;
|
|
export const DATA_GRID_CHAR_WIDTH = 8;
|
|
export const DATA_GRID_HEADER_CONTROL_WIDTH = 64;
|
|
export const DATA_GRID_CELL_PADDING = 28;
|
|
export const DATA_GRID_SAMPLE_ROWS = 50;
|
|
|
|
function estimateTextWidth(text: string, padding: number): number {
|
|
return text.length * DATA_GRID_CHAR_WIDTH + padding;
|
|
}
|
|
|
|
function displaySampleValue(value: CellValue): string | null {
|
|
if (value == null) return null;
|
|
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
}
|
|
|
|
export function calculateDataGridColumnWidth(options: {
|
|
columnName: string;
|
|
sampleValues: readonly CellValue[];
|
|
}): number {
|
|
let maxWidth = estimateTextWidth(options.columnName, DATA_GRID_HEADER_CONTROL_WIDTH);
|
|
|
|
for (const value of options.sampleValues.slice(0, DATA_GRID_SAMPLE_ROWS)) {
|
|
const text = displaySampleValue(value);
|
|
if (text == null) continue;
|
|
const displayLen = Math.min(text.length, 60);
|
|
const width = displayLen * DATA_GRID_CHAR_WIDTH + DATA_GRID_CELL_PADDING;
|
|
if (width > maxWidth) maxWidth = width;
|
|
}
|
|
|
|
return Math.max(DATA_GRID_COL_MIN_WIDTH, Math.min(DATA_GRID_COL_MAX_WIDTH, Math.round(maxWidth)));
|
|
}
|