@youp-grid/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,181 @@
1
+ export type GridRowId = string | number;
2
+ export type GridRowModelType = "client" | "server";
3
+ export type SortDirection = "asc" | "desc";
4
+ export type ColumnPin = "left" | "right";
5
+ export type FilterOperator = "contains" | "equals" | "startsWith" | "endsWith" | "gt" | "gte" | "lt" | "lte" | "between" | "isEmpty" | "isNotEmpty" | "in";
6
+ export type Accessor<TRow, TValue = unknown> = (row: TRow) => TValue;
7
+ export type ColumnComparator<TValue = unknown, TRow = unknown> = (leftValue: TValue, rightValue: TValue, leftRow: TRow, rightRow: TRow) => number;
8
+ export type ColumnFilterPredicate<TValue = unknown, TRow = unknown> = (value: TValue, filter: FilterRule, row: TRow) => boolean;
9
+ export type ValueFormatter<TValue = unknown, TRow = unknown> = (value: TValue, row: TRow) => string;
10
+ export type ValueParser<TValue = unknown, TRow = unknown> = (input: string, row: TRow) => TValue;
11
+ export type ColumnDef<TRow, TValue = unknown> = {
12
+ id?: string;
13
+ field?: Extract<keyof TRow, string> | string;
14
+ headerName?: string;
15
+ headerGroup?: string;
16
+ accessor?: Accessor<TRow, TValue>;
17
+ sortable?: boolean;
18
+ filterable?: boolean;
19
+ editable?: boolean;
20
+ hidden?: boolean;
21
+ pinned?: ColumnPin;
22
+ width?: number;
23
+ minWidth?: number;
24
+ maxWidth?: number;
25
+ comparator?: ColumnComparator<TValue, TRow>;
26
+ filterPredicate?: ColumnFilterPredicate<TValue, TRow>;
27
+ valueFormatter?: ValueFormatter<TValue, TRow>;
28
+ valueParser?: ValueParser<TValue, TRow>;
29
+ };
30
+ export type ResolvedColumnDef<TRow, TValue = unknown> = Omit<ColumnDef<TRow, TValue>, "id" | "accessor"> & {
31
+ id: string;
32
+ headerName: string;
33
+ accessor: Accessor<TRow, TValue>;
34
+ };
35
+ export type SortRule = {
36
+ columnId: string;
37
+ direction: SortDirection;
38
+ };
39
+ export type ColumnState = {
40
+ columnId: string;
41
+ hidden?: boolean;
42
+ pinned?: ColumnPin;
43
+ width?: number;
44
+ order?: number;
45
+ };
46
+ export type FilterRule = {
47
+ columnId: string;
48
+ operator: FilterOperator;
49
+ value?: unknown;
50
+ };
51
+ export type AggregationFunctionName = "sum" | "avg" | "min" | "max" | "count";
52
+ export type AggregationRule = {
53
+ columnId: string;
54
+ function: AggregationFunctionName;
55
+ label?: string;
56
+ };
57
+ export type AggregationResult = {
58
+ columnId: string;
59
+ function: AggregationFunctionName;
60
+ label: string;
61
+ value: number | undefined;
62
+ rowCount: number;
63
+ valueCount: number;
64
+ };
65
+ export type RowGroupingState = {
66
+ columnIds: string[];
67
+ collapsedGroupIds?: string[];
68
+ };
69
+ export type PaginationState = {
70
+ pageIndex: number;
71
+ pageSize: number;
72
+ };
73
+ export type CursorPaginationState = {
74
+ cursor?: string;
75
+ pageSize: number;
76
+ previousCursor?: string;
77
+ nextCursor?: string;
78
+ hasPreviousPage?: boolean;
79
+ hasNextPage?: boolean;
80
+ };
81
+ export type RemoteRequestStatus = "idle" | "loading" | "success" | "error" | "cancelled";
82
+ export type RemoteRequestState = {
83
+ requestId?: string;
84
+ sequence: number;
85
+ status: RemoteRequestStatus;
86
+ error?: string;
87
+ };
88
+ export type RemoteCacheState = {
89
+ key?: string;
90
+ version: number;
91
+ stale?: boolean;
92
+ invalidatedKeys?: string[];
93
+ };
94
+ export type GridState = {
95
+ columns?: ColumnState[];
96
+ sort?: SortRule[];
97
+ filters?: FilterRule[];
98
+ aggregation?: AggregationRule[];
99
+ rowGrouping?: RowGroupingState;
100
+ pagination?: PaginationState;
101
+ cursorPagination?: CursorPaginationState;
102
+ remoteRequest?: RemoteRequestState;
103
+ remoteCache?: RemoteCacheState;
104
+ selectedRowIds?: GridRowId[];
105
+ };
106
+ export type RowNode<TRow> = {
107
+ id: GridRowId;
108
+ index: number;
109
+ original: TRow;
110
+ };
111
+ export type RowGroupNode = {
112
+ type: "group";
113
+ id: string;
114
+ groupId: string;
115
+ index: number;
116
+ depth: number;
117
+ columnId: string;
118
+ value: unknown;
119
+ label: string;
120
+ rowCount: number;
121
+ expanded: boolean;
122
+ };
123
+ export type RowDisplayNode<TRow> = RowNode<TRow> | RowGroupNode;
124
+ export type BuildRowModelOptions<TRow> = {
125
+ rows: readonly TRow[];
126
+ columns: readonly ColumnDef<TRow>[];
127
+ state?: GridState;
128
+ getRowId?: (row: TRow, index: number) => GridRowId;
129
+ rowModelType?: GridRowModelType;
130
+ serverRowCount?: number;
131
+ serverFilteredRowCount?: number;
132
+ };
133
+ export type RowModel<TRow> = {
134
+ columns: ResolvedColumnDef<TRow>[];
135
+ visibleColumns: ResolvedColumnDef<TRow>[];
136
+ allRows: RowNode<TRow>[];
137
+ filteredRows: RowNode<TRow>[];
138
+ sortedRows: RowNode<TRow>[];
139
+ visibleRows: RowNode<TRow>[];
140
+ displayRows: RowDisplayNode<TRow>[];
141
+ aggregation: AggregationResult[];
142
+ totalRowCount: number;
143
+ filteredRowCount: number;
144
+ visibleRowCount: number;
145
+ pageCount?: number;
146
+ };
147
+ export type VirtualRangeOptions = {
148
+ itemCount: number;
149
+ itemSize: number;
150
+ viewportSize: number;
151
+ scrollOffset: number;
152
+ overscan?: number;
153
+ };
154
+ export type InfiniteScrollTriggerOptions = {
155
+ rowCount: number;
156
+ lastVisibleRowIndex: number;
157
+ threshold?: number;
158
+ hasMoreRows?: boolean;
159
+ loading?: boolean;
160
+ };
161
+ export type InfiniteScrollTrigger = {
162
+ shouldLoadMore: boolean;
163
+ rowCount: number;
164
+ lastVisibleRowIndex: number;
165
+ threshold: number;
166
+ remainingRows: number;
167
+ };
168
+ export type VirtualRange = {
169
+ startIndex: number;
170
+ endIndex: number;
171
+ beforeSize: number;
172
+ afterSize: number;
173
+ totalSize: number;
174
+ items: VirtualItem[];
175
+ };
176
+ export type VirtualItem = {
177
+ index: number;
178
+ start: number;
179
+ size: number;
180
+ end: number;
181
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { VirtualRange, VirtualRangeOptions } from "./types.ts";
2
+ export declare function getVirtualRange(options: VirtualRangeOptions): VirtualRange;
@@ -0,0 +1,47 @@
1
+ export function getVirtualRange(options) {
2
+ const itemCount = Math.max(0, options.itemCount);
3
+ const itemSize = Math.max(1, options.itemSize);
4
+ const viewportSize = Math.max(0, options.viewportSize);
5
+ const scrollOffset = Math.max(0, options.scrollOffset);
6
+ const overscan = Math.max(0, options.overscan ?? 3);
7
+ const totalSize = itemCount * itemSize;
8
+ if (itemCount === 0 || viewportSize === 0) {
9
+ return {
10
+ startIndex: 0,
11
+ endIndex: -1,
12
+ beforeSize: 0,
13
+ afterSize: 0,
14
+ totalSize,
15
+ items: [],
16
+ };
17
+ }
18
+ const visibleStart = Math.floor(scrollOffset / itemSize);
19
+ const visibleEnd = Math.ceil((scrollOffset + viewportSize) / itemSize) - 1;
20
+ const startIndex = clamp(visibleStart - overscan, 0, itemCount - 1);
21
+ const endIndex = clamp(visibleEnd + overscan, startIndex, itemCount - 1);
22
+ const items = createVirtualItems(startIndex, endIndex, itemSize);
23
+ return {
24
+ startIndex,
25
+ endIndex,
26
+ beforeSize: startIndex * itemSize,
27
+ afterSize: Math.max(0, totalSize - (endIndex + 1) * itemSize),
28
+ totalSize,
29
+ items,
30
+ };
31
+ }
32
+ function createVirtualItems(startIndex, endIndex, itemSize) {
33
+ const items = [];
34
+ for (let index = startIndex; index <= endIndex; index += 1) {
35
+ const start = index * itemSize;
36
+ items.push({
37
+ index,
38
+ start,
39
+ size: itemSize,
40
+ end: start + itemSize,
41
+ });
42
+ }
43
+ return items;
44
+ }
45
+ function clamp(value, min, max) {
46
+ return Math.min(Math.max(value, min), max);
47
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@youp-grid/core",
3
+ "version": "0.1.0",
4
+ "description": "Framework-agnostic data grid core for Youp Grid.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+ssh://git@github.com/SeungyoupBaek/youp-grid.git",
21
+ "directory": "packages/core"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.build.json"
28
+ }
29
+ }