@omnigrid/core 0.1.5 → 0.1.7

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.
package/README.md CHANGED
@@ -1,22 +1,197 @@
1
1
  # @omnigrid/core
2
2
 
3
- Framework-agnostic core engine for OmniGrid — a fast, extensible data grid.
3
+ Framework-agnostic core engine for OmniGrid — a fast, headless, extensible data grid.
4
+ Pure TypeScript, zero runtime dependencies, **no DOM, no framework imports**. The core owns data,
5
+ state, the data pipeline, 2D virtualization, DOM-pool row mapping, slots, and plugins; an adapter
6
+ (the React adapter in `@omnigrid/react`) renders it into a UI.
4
7
 
5
8
  ## What it provides
6
9
 
7
- - **State Store** — lightweight observable state manager for column sizing, order, scroll position, and selection
8
- - **Data Pipeline** — raw data → filter → sort → group/aggregate → paginate → virtual viewport
9
- - **Virtualization Engine** — 2D windowing (rows + columns) with overscan for smooth scrolling on large datasets
10
- - **Event Bus** — typed event system with `on`/`off`/`emit` for `cellClick`, `rowHover`, `scroll`, etc.
11
- - **DOM Pool** — reusable DOM node pool for high-performance rendering
12
- - **Slot Manager** — extensible slot system for headers, footers, cells, and custom UI
10
+ - **`Grid`** — entry point (`new Grid<T>(options)`), implements `GridApi<T>`.
11
+ - **State Store** — observable `GridState<T>`; structural changes notify subscribers and emit `stateChange`.
12
+ - **Data Pipeline** — plugins attach pure transforms (`T[] → T[]`) in registration order: raw → processed → virtualized.
13
+ - **Virtualization Engine (2D)** — visible window of rows *and* columns plus overscan; offsets are absolute table-space coordinates.
14
+ - **Event Bus** — typed `on(event, listener)`; returns an unsubscribe function.
15
+ - **DOM Pool mapping** — the core decides *which* rows are visible; the adapter performs DOM ops via injected `DomPoolBindings`.
16
+ - **Slot Manager / `GridApi.slots`** — headless widgets mounted by plugins into `top | bottom | left | right`.
17
+ - **Icon Registry** — framework-agnostic declarative SVG icons for plugin UI (`api.icons.get(name)`).
13
18
 
14
- ## Usage
19
+ **Scroll is ephemeral, not state.** `scrollTop`/`scrollLeft` are kept out of the Store:
20
+ scroll-only `setViewport({ scrollTop, scrollLeft })` calls never notify subscribers, so adapters do
21
+ not re-render on scroll frames. Read them via `getScrollPosition()`.
22
+
23
+ ## Key contracts
24
+
25
+ ### `GridOptions<T>`
26
+
27
+ ```ts
28
+ interface GridOptions<T> {
29
+ data?: T[]; // row data; omit when a plugin provides it (server pagination)
30
+ columns: ColumnDef<T>[]; // required
31
+ getRowId?: (row: T, index: number) => RowId; // default: index-based
32
+ rowHeight?: number; // fixed px row height; default 32 (offset math is O(1))
33
+ rowOverscan?: number; // extra rows above/below viewport; default 5
34
+ columnOverscan?: number; // extra columns beyond horizontal window; default 2
35
+ suppressRowHoverHighlight?: boolean;
36
+ rowStyle?: RowStyle;
37
+ getRowStyle?: (params: RowRenderParams<T>) => RowStyle | undefined;
38
+ rowClass?: string | ((params: RowRenderParams<T>) => string | undefined);
39
+ getRowClass?: (params: RowRenderParams<T>) => string | undefined;
40
+ rowClassRules?: RowClassRules<T>; // class name → predicate
41
+ plugins?: GridPlugin<T>[]; // plugin instances registered at construction
42
+ }
43
+ ```
44
+
45
+ ### `ColumnDef<T>`
46
+
47
+ `ColumnDef<T> = ColumnLeafDef<T> | ColumnGroupDef<T>`. Groups nest via `children`; width/visibility/pinning can be combined freely.
48
+
49
+ ```ts
50
+ interface ColumnLeafDef<T> {
51
+ id: string; // unique column id (also stable slot key)
52
+ header: string;
53
+ field?: keyof T | string; // value accessor (default cell value source)
54
+ valueGetter?: (row: T) => unknown; // overrides field
55
+ valueFormatter?: (value: unknown) => string;
56
+ width?: number; // fixed px width
57
+ flex?: number; // flex weight; expands to fill remaining space
58
+ minWidth?: number;
59
+ maxWidth?: number;
60
+ hidden?: boolean;
61
+ pinned?: "left" | "right"; // fixed column in horizontal viewport
62
+ sortable?: boolean; // read by SortingPlugin
63
+ sortState?: SortDirection; // "asc" | "desc" — initial sort for SortingPlugin
64
+ sortIndex?: number; // runtime 1-based priority when multi-sorted
65
+ align?: "left" | "center" | "right";
66
+ cellRenderer?: (params: CellRenderParams<T>) => unknown; // opaque content, adapter materializes
67
+ cellStyle?: RowStyle;
68
+ getCellStyle?: (params: CellRenderParams<T>) => RowStyle | undefined;
69
+ cellClass?: string | ((params: CellRenderParams<T>) => string | undefined);
70
+ getCellClass?: (params: CellRenderParams<T>) => string | undefined;
71
+ cellClassRules?: CellClassRules<T>;
72
+ headerRenderer?: (column: ColumnDef<T>) => unknown;
73
+ stopRowClick?: boolean;
74
+ stopHeaderClick?: boolean;
75
+ }
76
+
77
+ interface ColumnGroupDef<T> {
78
+ id: string;
79
+ header: string;
80
+ children: ColumnDef<T>[]; // nested leaves / groups
81
+ pinned?: "left" | "right";
82
+ collapsible?: boolean;
83
+ defaultExpanded?: boolean;
84
+ headerRenderer?: (column: ColumnDef<T>) => unknown;
85
+ }
86
+ ```
87
+
88
+ ### `GridApi<T>`
89
+
90
+ `Grid<T>` implements this interface — the single public surface for adapters and plugins.
91
+ UI code must never touch core internals directly.
92
+
93
+ ```ts
94
+ interface GridApi<T> {
95
+ getState(): GridState<T>; // { data, columns, viewport, rowHeight, rowOverscan, columnOverscan }
96
+ getScrollPosition(): ScrollPosition; // ephemeral scrollTop/scrollLeft
97
+ getViewportData(): ViewportData<T>; // virtualized window: rows/columns + absolute offsets
98
+ getProcessedData(): T[]; // after all registered data processors
99
+ getRowId(row: T, index: number): RowId;
100
+ setData(data: T[]): void;
101
+ setViewport(viewport: Partial<ViewportState>): void; // width/height/scrollTop/scrollLeft; scroll-only ⇒ no Store notify
102
+ refresh(): void; // repaint hints (rowClassRules), bumps revision
103
+ subscribe(listener: () => void): () => void;
104
+ on<E extends keyof GridEvents<T>>(event: E, listener: (p: GridEvents<T>[E]) => void): () => void;
105
+ registerPlugin(plugin: GridPlugin<T>): () => void;
106
+ registerDataProcessor(processor: DataProcessor<T>): () => void;
107
+ setColumns(columns: ColumnDef<T>[]): void;
108
+ headerClick(columnId: string, multiSort?: boolean): void;
109
+ rowClick(row: RowClickEvent<T>): void;
110
+ rowHover(row: RowHoverEvent<T>): void;
111
+ readonly slots: SlotManager<T>; // mount headless widgets
112
+ readonly icons: IconRegistry<T>; // declarative SVG icons for plugin UI
113
+ getSlotMounts(slot: SlotName): SlotMount<T>[];
114
+ getRevision(): number; // monotonic counter for structural changes
115
+ destroy(): void;
116
+ isDestroyed(): boolean;
117
+ }
118
+ ```
119
+
120
+ `GridEvents<T>`: `stateChange` | `viewportChange` | `dataChange` | `headerClick` | `rowClick` | `rowHover` | `slotsChange`.
121
+ Example: `grid.on("rowClick", (e) => console.log(e.id, e.index, e.data))`.
122
+
123
+ ### Plugin contract — `GridPlugin<T>`
15
124
 
16
125
  ```ts
17
- import { Grid, Store, Virtualizer } from "@omnigrid/core";
126
+ interface GridPlugin<T> {
127
+ name: string; // unique, namespaced, e.g. "@omnigrid/sorting-plugin"
128
+ register(api: GridApi<T>): void | (() => void); // returns cleanup
129
+ getRowClass?(params: RowRenderParams<T>): string | undefined;
130
+ getRowStyle?(params: RowRenderParams<T>): RowStyle | undefined;
131
+ }
18
132
  ```
19
133
 
134
+ A plugin hooks the grid via `api.on(...)`, `api.registerDataProcessor(...)`, `api.slots.mount(...)`,
135
+ and `api.icons.register(...)`; it must return a cleanup function that tears down everything it created.
136
+ Plugin source must remain framework-agnostic (see [`AI_INSTRUCTIONS.md`](../AI_INSTRUCTIONS.md) and
137
+ [`core/AI_INSTRUCTIONS.md`](AI_INSTRUCTIONS.md) in this repo).
138
+
139
+ ## Quick start
140
+
141
+ Headless core only (no UI):
142
+
143
+ ```ts
144
+ import { Grid } from "@omnigrid/core";
145
+ import type { ColumnDef } from "@omnigrid/core";
146
+
147
+ interface Row { id: string; name: string }
148
+
149
+ const columns: ColumnDef<Row>[] = [
150
+ { id: "name", field: "name", header: "Name", width: 220 },
151
+ ];
152
+
153
+ const grid = new Grid<Row>({ data: [{ id: "1", name: "Ada" }], columns, getRowId: (row) => row.id });
154
+
155
+ grid.setViewport({ width: 400, height: 300 }); // the adapter normally feeds geometry
156
+ const viewport = grid.getViewportData(); // virtualized rows + columns + offsets
157
+ grid.on("rowClick", (e) => console.log(e.index));
158
+ grid.destroy();
159
+ ```
160
+
161
+ With base plugins:
162
+
163
+ ```ts
164
+ import { Grid } from "@omnigrid/core";
165
+ import { SortingPlugin } from "@omnigrid/sorting-plugin";
166
+ import { SelectionPlugin } from "@omnigrid/selection-plugin";
167
+ import { PaginationPlugin } from "@omnigrid/pagination-plugin";
168
+
169
+ const grid = new Grid<Row>({
170
+ data,
171
+ columns,
172
+ getRowId: (row) => row.id,
173
+ plugins: [
174
+ new SortingPlugin<Row>(),
175
+ new SelectionPlugin<Row>({ mode: "multiple", showRowCheckboxes: true }),
176
+ new PaginationPlugin<Row>({ pageSize: 50 }),
177
+ ],
178
+ });
179
+ ```
180
+
181
+ In a UI framework, use the adapter — e.g. `@omnigrid/react`:
182
+
183
+ ```tsx
184
+ <OmniGrid columns={columns} data={rows} getRowId={(row) => row.id} plugins={plugins} style={{ height: 480 }} />
185
+ ```
186
+
187
+ See [@omnigrid/react](../../adapters/react/README.md).
188
+
189
+ ## Exported helpers
190
+
191
+ - `flattenColumns`, `isColumnLeaf`, `isColumnGroup`, `getColumnGroups` — column tree utilities.
192
+ - `Store`, `Virtualizer`, `EventBus`, `DomPool`, `SlotManager`, `IconRegistry` — building blocks
193
+ (mostly used internally; public for advanced integrations/tests).
194
+
20
195
  ## Links
21
196
 
22
197
  - **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/core
package/dist/grid.d.ts CHANGED
@@ -23,6 +23,8 @@ export declare class Grid<T> implements GridApi<T> {
23
23
  */
24
24
  private scrollPosition;
25
25
  constructor(options: GridOptions<T>);
26
+ /** Returns the calculated width for a visible column, including off-screen columns. */
27
+ getColumnWidth(columnId: string): number | undefined;
26
28
  getState(): GridState<T>;
27
29
  /** Returns the current ephemeral scroll position (not part of Store state). */
28
30
  getScrollPosition(): ScrollPosition;
@@ -45,6 +47,7 @@ export declare class Grid<T> implements GridApi<T> {
45
47
  rowHover(row: RowHoverEvent<T>): void;
46
48
  subscribe(listener: () => void): () => void;
47
49
  on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
50
+ emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
48
51
  registerPlugin(plugin: GridPlugin<T>): () => void;
49
52
  getSlotMounts(slot: SlotName): SlotMount<T>[];
50
53
  /** Monotonic counter for structural changes (refresh / data / columns). */
package/dist/grid.js CHANGED
@@ -46,6 +46,30 @@ export class Grid {
46
46
  this.store.subscribe(() => this.events.emit("stateChange", this.store.getState()));
47
47
  options.plugins?.forEach((plugin) => this.registerPlugin(plugin));
48
48
  }
49
+ /** Returns the calculated width for a visible column, including off-screen columns. */
50
+ getColumnWidth(columnId) {
51
+ const state = this.getState();
52
+ const visibleColumns = flattenColumns(state.columns).filter((column) => !column.hidden);
53
+ if (!visibleColumns.some((column) => column.id === columnId))
54
+ return undefined;
55
+ const pinnedLeft = visibleColumns.filter((column) => column.pinned === "left");
56
+ const scrollable = visibleColumns.filter((column) => !column.pinned);
57
+ const pinnedRight = visibleColumns.filter((column) => column.pinned === "right");
58
+ const pinnedLeftOffsets = this.virtualizer.getColumnOffsets(pinnedLeft, state.viewport.width);
59
+ const pinnedRightOffsets = this.virtualizer.getColumnOffsets(pinnedRight, state.viewport.width);
60
+ const pinnedLeftWidth = pinnedLeftOffsets.reduce((sum, item) => sum + item.size, 0);
61
+ const pinnedRightWidth = pinnedRightOffsets.reduce((sum, item) => sum + item.size, 0);
62
+ const scrollableWidth = Math.max(0, state.viewport.width - pinnedLeftWidth - pinnedRightWidth);
63
+ const scrollableOffsets = this.virtualizer.getColumnOffsets(scrollable, scrollableWidth);
64
+ const pinnedLeftIndex = pinnedLeft.findIndex((column) => column.id === columnId);
65
+ if (pinnedLeftIndex >= 0)
66
+ return pinnedLeftOffsets[pinnedLeftIndex].size;
67
+ const pinnedRightIndex = pinnedRight.findIndex((column) => column.id === columnId);
68
+ if (pinnedRightIndex >= 0)
69
+ return pinnedRightOffsets[pinnedRightIndex].size;
70
+ const scrollableIndex = scrollable.findIndex((column) => column.id === columnId);
71
+ return scrollableOffsets[scrollableIndex]?.size;
72
+ }
49
73
  getState() {
50
74
  return this.store.getState();
51
75
  }
@@ -253,6 +277,10 @@ export class Grid {
253
277
  this.assertActive();
254
278
  return this.events.on(event, listener);
255
279
  }
280
+ emit(event, payload) {
281
+ this.assertActive();
282
+ this.events.emit(event, payload);
283
+ }
256
284
  registerPlugin(plugin) {
257
285
  this.assertActive();
258
286
  const cleanup = plugin.register(this);
package/dist/types.d.ts CHANGED
@@ -52,37 +52,70 @@ export interface CheckboxControl extends CheckboxRenderParams {
52
52
  type: "@omnigrid/checkbox";
53
53
  }
54
54
  interface BaseColumnDef<T> {
55
+ /** Stable identifier used by the grid and plugins. */
55
56
  id: string;
57
+ /** Text displayed in the column header. */
56
58
  header: string;
59
+ /** Pins the column to a horizontal edge of the grid. */
57
60
  pinned?: "left" | "right";
61
+ /** Whether this column may be resized by the resize plugin. */
62
+ resizable?: boolean;
63
+ /** Custom renderer for this column's header. */
58
64
  headerRenderer?: (column: ColumnDef<T>) => unknown;
59
65
  }
60
66
  export interface ColumnLeafDef<T> extends BaseColumnDef<T> {
67
+ /** Row property used as the cell value source. */
61
68
  field?: keyof T | string;
69
+ /** Hides this column without removing it from the column definitions. */
62
70
  hidden?: boolean;
71
+ /** Relative width factor used to distribute available space. */
63
72
  flex?: number;
73
+ /** Fixed column width in pixels. */
64
74
  width?: number;
75
+ /** Minimum column width in pixels. */
65
76
  minWidth?: number;
77
+ /** Maximum column width in pixels. */
66
78
  maxWidth?: number;
79
+ /** Whether this column can be sorted. */
67
80
  sortable?: boolean;
81
+ /** Horizontal alignment of cell content. */
68
82
  align?: CellAlign;
83
+ /** Initial sort direction used by SortingPlugin. */
69
84
  sortState?: SortDirection;
85
+ /** 1-based sort priority, set only while multiple columns are sorted. */
86
+ sortIndex?: number;
87
+ /** Compares values for this column, overriding the sorting plugin default. */
88
+ comparator?: (left: unknown, right: unknown, column: ColumnLeafDef<T>) => number;
89
+ /** Prevents clicks in this column's cells from triggering row-click behavior. */
70
90
  stopRowClick?: boolean;
91
+ /** Prevents clicks on this column's header from triggering header-click behavior. */
71
92
  stopHeaderClick?: boolean;
93
+ /** Computes the cell value from the row; takes precedence over `field`. */
72
94
  valueGetter?: (row: T) => unknown;
95
+ /** Formats the raw cell value for display. */
73
96
  valueFormatter?: (value: unknown) => string;
97
+ /** Replaces the default cell content with custom content. */
74
98
  cellRenderer?: (params: CellRenderParams<T>) => unknown;
99
+ /** Static inline styles applied to cells in this column. */
75
100
  cellStyle?: RowStyle;
101
+ /** Computes inline styles for each rendered cell. */
76
102
  getCellStyle?: (params: CellRenderParams<T>) => RowStyle | undefined;
103
+ /** Static CSS class applied to cells in this column. */
77
104
  cellClass?: string | ((params: CellRenderParams<T>) => string | undefined);
105
+ /** Computes a CSS class for each rendered cell. */
78
106
  getCellClass?: (params: CellRenderParams<T>) => string | undefined;
107
+ /** Maps CSS class names to predicates evaluated for each rendered cell. */
79
108
  cellClassRules?: CellClassRules<T>;
80
109
  }
81
110
  export interface ColumnGroupDef<T> extends BaseColumnDef<T> {
111
+ /** Child column definitions nested under this group. */
82
112
  children: ColumnDef<T>[];
113
+ /** Allows users to collapse and expand this group. */
83
114
  collapsible?: boolean;
115
+ /** Whether this group starts expanded when it is collapsible. */
84
116
  defaultExpanded?: boolean;
85
117
  }
118
+ /** Leaf or grouped column definition accepted by the grid. */
86
119
  export type ColumnDef<T> = ColumnLeafDef<T> | ColumnGroupDef<T>;
87
120
  export interface GridOptions<T> {
88
121
  data?: T[];
@@ -205,6 +238,10 @@ export interface GridEvents<T> {
205
238
  columnId: string;
206
239
  multiSort: boolean;
207
240
  };
241
+ sortingChanged: Array<{
242
+ columnId: string;
243
+ direction: SortDirection;
244
+ }>;
208
245
  rowClick: RowClickEvent<T>;
209
246
  rowHover: RowHoverEvent<T>;
210
247
  slotsChange: {
@@ -278,10 +315,13 @@ export interface BlockConfig<Name extends string = string> {
278
315
  position?: SlotPosition;
279
316
  priority?: number;
280
317
  }
281
- export type SlotNodeEventName = "click" | "change" | "keydown";
318
+ export type SlotNodeEventName = "click" | "change" | "keydown" | "pointerdown" | "pointermove" | "pointerup" | "dblclick";
282
319
  export interface SlotNodeEvent {
283
320
  value?: string;
284
321
  key?: string;
322
+ clientX?: number;
323
+ clientY?: number;
324
+ button?: number;
285
325
  }
286
326
  /**
287
327
  * Context passed to the slot content provider. Headless: the plugin
@@ -354,6 +394,7 @@ export interface GridApi<T> {
354
394
  getState(): GridState<T>;
355
395
  getScrollPosition(): ScrollPosition;
356
396
  getViewportData(): ViewportData<T>;
397
+ getColumnWidth(columnId: string): number | undefined;
357
398
  getProcessedData(): T[];
358
399
  getRowId(row: T, index: number): RowId;
359
400
  setData(data: T[]): void;
@@ -361,6 +402,8 @@ export interface GridApi<T> {
361
402
  refresh(): void;
362
403
  subscribe(listener: () => void): () => void;
363
404
  on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
405
+ /** Emits a typed grid event for plugin-to-plugin and adapter notifications. */
406
+ emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
364
407
  registerPlugin(plugin: GridPlugin<T>): () => void;
365
408
  registerDataProcessor(processor: DataProcessor<T>): () => void;
366
409
  setColumns(columns: ColumnDef<T>[]): void;
@@ -103,7 +103,9 @@ export class Virtualizer {
103
103
  }
104
104
  getColumnWidths(columns, viewportWidth) {
105
105
  const widths = columns.map((column) => this.getBaseColumnWidth(column));
106
- const flexIndexes = columns.map((column, index) => (column.flex && column.flex > 0 ? index : -1)).filter((index) => index >= 0);
106
+ const flexIndexes = columns
107
+ .map((column, index) => (column.flex && column.flex > 0 && column.width === undefined ? index : -1))
108
+ .filter((index) => index >= 0);
107
109
  if (flexIndexes.length === 0)
108
110
  return widths;
109
111
  const fixedWidth = widths.reduce((total, width, index) => (flexIndexes.includes(index) ? total : total + width), 0);
@@ -168,8 +170,12 @@ export class Virtualizer {
168
170
  return widths;
169
171
  }
170
172
  getBaseColumnWidth(column) {
171
- if (column.flex && column.flex > 0)
173
+ if (column.flex && column.flex > 0) {
174
+ if (column.width !== undefined) {
175
+ return Math.min(this.getMaxColumnWidth(column), Math.max(this.getMinColumnWidth(column), column.width));
176
+ }
172
177
  return this.getMinColumnWidth(column);
178
+ }
173
179
  const width = column.width ?? 120;
174
180
  return Math.min(this.getMaxColumnWidth(column), Math.max(this.getMinColumnWidth(column), width));
175
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/core",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Framework-agnostic core engine for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",