feat(grid): add transposed table view

This commit is contained in:
t8y2
2026-05-17 13:32:42 +08:00
parent 933c1fd803
commit e4bb342bf4
3 changed files with 988 additions and 560 deletions
File diff suppressed because it is too large Load Diff
+157
View File
@@ -0,0 +1,157 @@
export interface DataGridTransposeState {
showTranspose: boolean;
transposeRowIndex: number | null;
}
export interface BuildTransposeRowsOptions<T> {
columns: string[];
records: T[][];
typeByColumn?: Map<string, string>;
displayValue: (value: T, column: string, columnIndex: number, recordIndex: number) => string;
}
export interface DataGridTransposeCell<T> {
value: T;
display: string;
isNull: boolean;
}
export interface DataGridTransposeRow<T> {
id: string;
column: string;
type: string;
values: Array<DataGridTransposeCell<T>>;
}
export interface TransposeRecordWindowOptions {
totalRecords: number;
scrollLeft: number;
viewportWidth: number;
pinnedWidth: number;
recordWidth: number;
overscan?: number;
}
export interface TransposeRecordWindow {
start: number;
end: number;
beforeWidth: number;
afterWidth: number;
}
export interface TransposeSelectionRange {
startRow: number;
endRow: number;
startCol: number;
endCol: number;
}
export interface TransposeAnchorOptions {
requestedRowIndex: number;
rowIds: number[];
selectedRowIds: Set<number>;
selectedRange: TransposeSelectionRange | null;
}
export interface TransposeFieldWidthOptions {
minWidth?: number;
maxWidth?: number;
charWidth?: number;
padding?: number;
}
export interface TransposeScrollLeftOptions {
recordIndex: number;
totalRecords: number;
viewportWidth: number;
pinnedWidth: number;
recordWidth: number;
}
export function nextTransposeState(
showTranspose: boolean,
transposeRowIndex: number | null,
requestedRowIndex: number,
): DataGridTransposeState {
if (showTranspose && transposeRowIndex === requestedRowIndex) {
return { showTranspose: false, transposeRowIndex: null };
}
return { showTranspose: true, transposeRowIndex: requestedRowIndex };
}
export function buildTransposeRows<T>(options: BuildTransposeRowsOptions<T>): Array<DataGridTransposeRow<T>> {
return options.columns.map((column, columnIndex) => {
return {
id: `${columnIndex}:${column}`,
column,
type: options.typeByColumn?.get(column) || "",
values: options.records.map((record, recordIndex) => {
const value = record[columnIndex] as T;
return {
value,
display: options.displayValue(value, column, columnIndex, recordIndex),
isNull: value === null,
};
}),
};
});
}
export function visibleTransposeRecordWindow(options: TransposeRecordWindowOptions): TransposeRecordWindow {
if (options.totalRecords <= 0 || options.recordWidth <= 0) {
return { start: 0, end: 0, beforeWidth: 0, afterWidth: 0 };
}
const overscan = options.overscan ?? 2;
const recordScrollLeft = Math.max(0, options.scrollLeft - options.pinnedWidth);
const recordViewportWidth = Math.max(0, options.viewportWidth - options.pinnedWidth);
const start = Math.max(0, Math.floor(recordScrollLeft / options.recordWidth) - overscan);
const end = Math.min(
options.totalRecords,
Math.ceil((recordScrollLeft + recordViewportWidth) / options.recordWidth) + overscan + 1,
);
return {
start,
end,
beforeWidth: start * options.recordWidth,
afterWidth: Math.max(0, (options.totalRecords - end) * options.recordWidth),
};
}
export function transposeAnchorRowIndex(options: TransposeAnchorOptions): number {
const requestedRowId = options.rowIds[options.requestedRowIndex];
if (requestedRowId !== undefined && options.selectedRowIds.size > 1 && options.selectedRowIds.has(requestedRowId)) {
const firstSelectedIndex = options.rowIds.findIndex((rowId) => options.selectedRowIds.has(rowId));
if (firstSelectedIndex >= 0) return firstSelectedIndex;
}
const range = options.selectedRange;
if (
range &&
range.startRow !== range.endRow &&
options.requestedRowIndex >= range.startRow &&
options.requestedRowIndex <= range.endRow
) {
return range.startRow;
}
return options.requestedRowIndex;
}
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 longest = columns.reduce((max, column) => Math.max(max, column.length), 0);
return Math.min(maxWidth, Math.max(minWidth, Math.ceil(longest * charWidth + padding)));
}
export function transposeScrollLeftForRecord(options: TransposeScrollLeftOptions): number {
if (options.recordWidth <= 0 || options.totalRecords <= 0) return 0;
const desired = Math.max(0, options.recordIndex) * options.recordWidth;
const totalWidth = options.pinnedWidth + options.totalRecords * options.recordWidth;
const maxScrollLeft = Math.max(0, totalWidth - options.viewportWidth);
return Math.min(desired, maxScrollLeft);
}
+141
View File
@@ -0,0 +1,141 @@
import { strict as assert } from "node:assert";
import test from "node:test";
import {
buildTransposeRows,
nextTransposeState,
transposeAnchorRowIndex,
transposeFieldWidth,
transposeScrollLeftForRecord,
visibleTransposeRecordWindow,
} from "../src/lib/dataGridTranspose.ts";
test("row number double click opens transpose for a row", () => {
assert.deepEqual(nextTransposeState(false, null, 2), {
showTranspose: true,
transposeRowIndex: 2,
});
});
test("row number double click switches transpose to another row", () => {
assert.deepEqual(nextTransposeState(true, 2, 4), {
showTranspose: true,
transposeRowIndex: 4,
});
});
test("row number double click closes transpose for the same row", () => {
assert.deepEqual(nextTransposeState(true, 2, 2), {
showTranspose: false,
transposeRowIndex: null,
});
});
test("builds one virtualizable transpose row per field with all record values", () => {
const rows = buildTransposeRows({
columns: ["id", "name", "notes"],
records: [
[1, "Ada", null],
[2, "Grace", "compiler"],
],
typeByColumn: new Map([
["id", "int"],
["name", "varchar"],
]),
displayValue: (value) => (value === null ? "NULL" : String(value)),
});
assert.deepEqual(rows, [
{
id: "0:id",
column: "id",
type: "int",
values: [
{ value: 1, display: "1", isNull: false },
{ value: 2, display: "2", isNull: false },
],
},
{
id: "1:name",
column: "name",
type: "varchar",
values: [
{ value: "Ada", display: "Ada", isNull: false },
{ value: "Grace", display: "Grace", isNull: false },
],
},
{
id: "2:notes",
column: "notes",
type: "",
values: [
{ value: null, display: "NULL", isNull: true },
{ value: "compiler", display: "compiler", isNull: false },
],
},
]);
});
test("calculates a horizontal record window with spacer widths", () => {
assert.deepEqual(
visibleTransposeRecordWindow({
totalRecords: 100,
scrollLeft: 680,
viewportWidth: 720,
pinnedWidth: 320,
recordWidth: 160,
overscan: 1,
}),
{
start: 1,
end: 7,
beforeWidth: 160,
afterWidth: 14880,
},
);
});
test("uses the first selected row as the transpose anchor when context row is inside row selection", () => {
assert.equal(
transposeAnchorRowIndex({
requestedRowIndex: 3,
rowIds: [10, 11, 12, 13, 14, 15],
selectedRowIds: new Set([12, 13, 14]),
selectedRange: null,
}),
2,
);
});
test("uses the first selected cell range row as the transpose anchor when context row is inside range", () => {
assert.equal(
transposeAnchorRowIndex({
requestedRowIndex: 5,
rowIds: [10, 11, 12, 13, 14, 15],
selectedRowIds: new Set(),
selectedRange: { startRow: 2, endRow: 5, startCol: 0, endCol: 2 },
}),
2,
);
});
test("sizes the transpose field column from visible field names", () => {
assert.equal(transposeFieldWidth(["id", "iso3", "year"]), 104);
assert.equal(transposeFieldWidth(["country_name"]), 128);
});
test("caps the transpose field column width for long field names", () => {
assert.equal(transposeFieldWidth(["a_very_long_metric_column_name"]), 220);
});
test("aligns the selected transpose record at the start of the scrollable records", () => {
assert.equal(
transposeScrollLeftForRecord({
recordIndex: 55,
totalRecords: 100,
viewportWidth: 1200,
pinnedWidth: 104,
recordWidth: 168,
}),
9240,
);
});