@omnigrid/core 0.1.4 → 0.1.6

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
@@ -45,6 +45,7 @@ export declare class Grid<T> implements GridApi<T> {
45
45
  rowHover(row: RowHoverEvent<T>): void;
46
46
  subscribe(listener: () => void): () => void;
47
47
  on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
48
+ emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
48
49
  registerPlugin(plugin: GridPlugin<T>): () => void;
49
50
  getSlotMounts(slot: SlotName): SlotMount<T>[];
50
51
  /** Monotonic counter for structural changes (refresh / data / columns). */
package/dist/grid.js CHANGED
@@ -253,6 +253,10 @@ export class Grid {
253
253
  this.assertActive();
254
254
  return this.events.on(event, listener);
255
255
  }
256
+ emit(event, payload) {
257
+ this.assertActive();
258
+ this.events.emit(event, payload);
259
+ }
256
260
  registerPlugin(plugin) {
257
261
  this.assertActive();
258
262
  const cleanup = plugin.register(this);
package/dist/index.d.ts CHANGED
@@ -6,6 +6,6 @@ export { Grid } from "./grid";
6
6
  export { Grid as GridCore } from "./grid";
7
7
  export { SlotManager } from "./slots";
8
8
  export { Store } from "./store";
9
- export type { CellAlign, CellRenderParams, CellClassRules, CheckboxControl, CheckboxRenderParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, DataProcessor, DomPoolBindings, DomPoolUpdate, GridApi, GridEvents, GridOptions, GridPlugin, GridState, PooledRow, Range, RowClassRules, RowClickEvent, RowHost, RowHoverEvent, RowId, RowRenderParams, RowStyle, ScrollPosition, SlotComponentContent, SlotContent, SlotHtmlContent, SlotMount, SlotMountOptions, SlotName, SlotNodeContent, SlotNodeEvent, SlotNodeEventName, SlotPosition, SlotProvider, SlotRenderContext, SortDirection, ViewportData, ViewportState, VirtualItem, } from "./types";
9
+ export type { BlockConfig, CellAlign, CellRenderParams, CellClassRules, CheckboxControl, CheckboxRenderParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, DataProcessor, DomPoolBindings, DomPoolUpdate, GridApi, GridEvents, GridOptions, GridPlugin, GridState, PooledRow, Range, RowClassRules, RowClickEvent, RowHost, RowHoverEvent, RowId, RowRenderParams, RowStyle, ScrollPosition, SlotComponentContent, SlotContent, SlotHtmlContent, SlotMount, SlotMountOptions, SlotName, SlotNodeContent, SlotNodeEvent, SlotNodeEventName, SlotPosition, SlotProvider, SlotRenderContext, SortDirection, ViewportData, ViewportState, VirtualItem, } from "./types";
10
10
  export { getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf } from "./columns";
11
11
  export { Virtualizer } from "./virtualizer";
package/dist/types.d.ts CHANGED
@@ -67,6 +67,10 @@ export interface ColumnLeafDef<T> extends BaseColumnDef<T> {
67
67
  sortable?: boolean;
68
68
  align?: CellAlign;
69
69
  sortState?: SortDirection;
70
+ /** 1-based sort priority, set only while multiple columns are sorted. */
71
+ sortIndex?: number;
72
+ /** Compares values for this column, overriding the sorting plugin default. */
73
+ comparator?: (left: unknown, right: unknown, column: ColumnLeafDef<T>) => number;
70
74
  stopRowClick?: boolean;
71
75
  stopHeaderClick?: boolean;
72
76
  valueGetter?: (row: T) => unknown;
@@ -205,6 +209,10 @@ export interface GridEvents<T> {
205
209
  columnId: string;
206
210
  multiSort: boolean;
207
211
  };
212
+ sortingChanged: Array<{
213
+ columnId: string;
214
+ direction: SortDirection;
215
+ }>;
208
216
  rowClick: RowClickEvent<T>;
209
217
  rowHover: RowHoverEvent<T>;
210
218
  slotsChange: {
@@ -265,6 +273,19 @@ export interface GridPlugin<T> {
265
273
  }
266
274
  export type SlotName = "top" | "bottom" | "left" | "right";
267
275
  export type SlotPosition = "start" | "center" | "end";
276
+ /**
277
+ * Placement of a single UI block that a plugin mounts into a slot.
278
+ *
279
+ * Generic over `Name` so every plugin can restrict the accepted block names,
280
+ * e.g. `BlockConfig<"rowInfo" | "pageSize" | "navigation">` in the pagination
281
+ * plugin, while still being able to target any slot / position.
282
+ */
283
+ export interface BlockConfig<Name extends string = string> {
284
+ name: Name;
285
+ slot?: SlotName;
286
+ position?: SlotPosition;
287
+ priority?: number;
288
+ }
268
289
  export type SlotNodeEventName = "click" | "change" | "keydown";
269
290
  export interface SlotNodeEvent {
270
291
  value?: string;
@@ -348,6 +369,8 @@ export interface GridApi<T> {
348
369
  refresh(): void;
349
370
  subscribe(listener: () => void): () => void;
350
371
  on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
372
+ /** Emits a typed grid event for plugin-to-plugin and adapter notifications. */
373
+ emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
351
374
  registerPlugin(plugin: GridPlugin<T>): () => void;
352
375
  registerDataProcessor(processor: DataProcessor<T>): () => void;
353
376
  setColumns(columns: ColumnDef<T>[]): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnigrid/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Framework-agnostic core engine for OmniGrid",
5
5
  "keywords": [
6
6
  "omnigrid",