@omnigrid/core 0.1.33 → 0.1.35
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 +300 -300
- package/dist/events.d.ts +2 -0
- package/dist/events.js +9 -0
- package/dist/grid.d.ts +19 -0
- package/dist/grid.js +74 -9
- package/dist/index.d.ts +1 -1
- package/dist/store.d.ts +34 -1
- package/dist/store.js +23 -2
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,300 +1,300 @@
|
|
|
1
|
-
# @omnigrid/core
|
|
2
|
-
|
|
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.
|
|
7
|
-
|
|
8
|
-
## What it provides
|
|
9
|
-
|
|
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
|
-
- **Column drag events** — adapters emit `columnDragStart`, `columnDrop`, and `columnDragEnd` for movable leaf and group headers.
|
|
16
|
-
- **DOM Pool mapping** — the core decides _which_ rows are visible; the adapter performs DOM ops via injected `DomPoolBindings`.
|
|
17
|
-
- **Slot Manager / `GridApi.slots`** — headless widgets mounted by plugins into `top | bottom | left | right`.
|
|
18
|
-
- **Column header extensions** — plugins register independent header elements; adapters render them beside the shared label and controls.
|
|
19
|
-
- **Icon Registry** — framework-agnostic declarative SVG icons for plugin UI (`api.icons.get(name)`).
|
|
20
|
-
|
|
21
|
-
**Scroll is ephemeral, not state.** `scrollTop`/`scrollLeft` are kept out of the Store:
|
|
22
|
-
scroll-only `setViewport({ scrollTop, scrollLeft })` calls never notify subscribers, so adapters do
|
|
23
|
-
not re-render on scroll frames. Read them via `getScrollPosition()`; use `scrollTo()` to request an
|
|
24
|
-
adapter-native scroll adjustment without moving scroll coordinates into the Store.
|
|
25
|
-
|
|
26
|
-
## Key contracts
|
|
27
|
-
|
|
28
|
-
### `GridOptions<T>`
|
|
29
|
-
|
|
30
|
-
```ts
|
|
31
|
-
interface GridOptions<T> {
|
|
32
|
-
data?: T[] | null; // row data; null/omitted means initial loading
|
|
33
|
-
columns: ColumnDef<T>[]; // required
|
|
34
|
-
getRowId?: (row: T, index: number) => RowId; // default: index-based
|
|
35
|
-
rowHeight?: number; // fixed px row height; default 32 (offset math is O(1))
|
|
36
|
-
rowOverscan?: number; // extra rows above/below viewport; default 5
|
|
37
|
-
columnOverscan?: number; // extra columns beyond horizontal window; default 2
|
|
38
|
-
cellFlash?: boolean | number; // highlight changed cells after applyTransaction
|
|
39
|
-
suppressRowHoverHighlight?: boolean;
|
|
40
|
-
rowStyle?: RowStyle;
|
|
41
|
-
getRowStyle?: (params: RowRenderParams<T>) => RowStyle | undefined;
|
|
42
|
-
rowClass?: string | ((params: RowRenderParams<T>) => string | undefined);
|
|
43
|
-
getRowClass?: (params: RowRenderParams<T>) => string | undefined;
|
|
44
|
-
rowClassRules?: RowClassRules<T>; // class name → predicate
|
|
45
|
-
plugins?: GridPlugin<T>[]; // plugin instances registered at construction
|
|
46
|
-
loading?: boolean; // active fetch; shows loading even with prior rows
|
|
47
|
-
overlayComponents?: Record<string, OverlayComponent<T>>;
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Row callbacks receive `params.rowType` (`"leaf"` or `"group"`). Group rows are synthetic and have no `data`; checking `rowType` narrows the callback parameters.
|
|
52
|
-
|
|
53
|
-
### `ColumnDef<T>`
|
|
54
|
-
|
|
55
|
-
`ColumnDef<T> = ColumnLeafDef<T> | ColumnGroupDef<T>`. Groups nest via `children`; width/visibility/pinning can be combined freely.
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
interface ColumnLeafDef<T> {
|
|
59
|
-
id: string; // unique column id (also stable slot key)
|
|
60
|
-
header: string;
|
|
61
|
-
field?: keyof T | string; // value accessor (default cell value source)
|
|
62
|
-
valueGetter?: (row: T) => unknown; // overrides field
|
|
63
|
-
valueFormatter?: (value: unknown) => string;
|
|
64
|
-
width?: number; // fixed px width
|
|
65
|
-
flex?: number; // flex weight; expands to fill remaining space
|
|
66
|
-
minWidth?: number;
|
|
67
|
-
maxWidth?: number;
|
|
68
|
-
hidden?: boolean;
|
|
69
|
-
movable?: boolean; // opt in to leaf drag-and-drop with a reorder plugin
|
|
70
|
-
pinned?: "left" | "right"; // fixed column in horizontal viewport
|
|
71
|
-
sortable?: boolean; // read by SortingPlugin
|
|
72
|
-
sortState?: SortDirection; // "asc" | "desc" — initial sort for SortingPlugin
|
|
73
|
-
sortIndex?: number; // runtime 1-based priority when multi-sorted
|
|
74
|
-
align?: "left" | "center" | "right";
|
|
75
|
-
cellRenderer?: (params: CellRenderParams<T>) => unknown; // opaque content, adapter materializes
|
|
76
|
-
cellFlash?: boolean; // set false to opt out when GridOptions.cellFlash is enabled
|
|
77
|
-
cellStyle?: RowStyle;
|
|
78
|
-
getCellStyle?: (params: CellRenderParams<T>) => RowStyle | undefined;
|
|
79
|
-
cellClass?: string | ((params: CellRenderParams<T>) => string | undefined);
|
|
80
|
-
getCellClass?: (params: CellRenderParams<T>) => string | undefined;
|
|
81
|
-
cellClassRules?: CellClassRules<T>;
|
|
82
|
-
getColumnStyle?: (params: ColumnStyleParams<T>) => ColumnStyle | undefined;
|
|
83
|
-
getColumnClass?: (params: ColumnStyleParams<T>) => string | undefined;
|
|
84
|
-
columnClassRules?: ColumnClassRules<T>;
|
|
85
|
-
headerStyle?: HeaderStyle;
|
|
86
|
-
getHeaderStyle?: (params: HeaderRenderParams<T>) => HeaderStyle | undefined;
|
|
87
|
-
headerClass?: string | ((params: HeaderRenderParams<T>) => string | undefined);
|
|
88
|
-
getHeaderClass?: (params: HeaderRenderParams<T>) => string | undefined;
|
|
89
|
-
headerClassRules?: HeaderClassRules<T>;
|
|
90
|
-
headerRenderer?: (column: ColumnDef<T>) => unknown; // primary header content; adapter controls remain separate
|
|
91
|
-
stopRowClick?: boolean;
|
|
92
|
-
stopHeaderClick?: boolean;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
interface ColumnGroupDef<T> {
|
|
96
|
-
id: string;
|
|
97
|
-
header: string;
|
|
98
|
-
children: ColumnDef<T>[]; // nested leaves / groups
|
|
99
|
-
movable?: boolean; // opt in to moving the entire group with a reorder plugin
|
|
100
|
-
pinned?: "left" | "right";
|
|
101
|
-
collapsible?: boolean;
|
|
102
|
-
defaultExpanded?: boolean;
|
|
103
|
-
headerStyle?: HeaderStyle;
|
|
104
|
-
getHeaderStyle?: (params: HeaderGroupRenderParams<T>) => HeaderStyle | undefined;
|
|
105
|
-
headerClass?: string | ((params: HeaderGroupRenderParams<T>) => string | undefined);
|
|
106
|
-
getHeaderClass?: (params: HeaderGroupRenderParams<T>) => string | undefined;
|
|
107
|
-
headerClassRules?: HeaderGroupClassRules<T>;
|
|
108
|
-
headerRenderer?: (column: ColumnDef<T>) => unknown;
|
|
109
|
-
}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
`movable` is available on both leaf columns and groups. Dragging a group moves its complete
|
|
113
|
-
subtree; dropping after the last leaf in the final group places the dragged column after the group.
|
|
114
|
-
|
|
115
|
-
When grid-level `cellFlash` is enabled, set `cellFlash: false` on an individual leaf column to opt it out.
|
|
116
|
-
|
|
117
|
-
### `GridApi<T>`
|
|
118
|
-
|
|
119
|
-
`Grid<T>` implements this interface — the single public surface for adapters and plugins.
|
|
120
|
-
UI code must never touch core internals directly.
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
interface GridApi<T> {
|
|
124
|
-
getState(): GridState<T>; // { data, columns, viewport, rowHeight, rowOverscan, columnOverscan }
|
|
125
|
-
getScrollPosition(): ScrollPosition; // ephemeral scrollTop/scrollLeft
|
|
126
|
-
scrollTo(position: Partial<ScrollPosition>): void; // request a native scroll adjustment from the adapter
|
|
127
|
-
getViewportData(): ViewportData<T>; // virtualized window: rows/columns + absolute offsets
|
|
128
|
-
getProcessedData(): T[]; // after all registered data processors
|
|
129
|
-
getRowId(row: T, index: number): RowId;
|
|
130
|
-
setData(data: T[] | null | undefined): void;
|
|
131
|
-
setViewport(viewport: Partial<ViewportState>): void; // width/height/scrollTop/scrollLeft; scroll-only ⇒ no Store notify
|
|
132
|
-
refresh(): void; // repaint hints (rowClassRules), bumps revision
|
|
133
|
-
subscribe(listener: () => void): () => void;
|
|
134
|
-
on<E extends keyof GridEvents<T>>(event: E, listener: (p: GridEvents<T>[E]) => void): () => void;
|
|
135
|
-
registerPlugin(plugin: GridPlugin<T>): () => void;
|
|
136
|
-
registerHeaderExtension(extension: ColumnHeaderExtension<T>): () => void;
|
|
137
|
-
getHeaderExtensions(position?: ColumnHeaderExtensionPosition): ColumnHeaderExtension<T>[];
|
|
138
|
-
registerDataProcessor(processor: DataProcessor<T>): () => void;
|
|
139
|
-
setColumns(columns: ColumnDef<T>[]): void;
|
|
140
|
-
headerClick(columnId: string, multiSort?: boolean): void;
|
|
141
|
-
rowClick(row: RowClickEvent<T>): void;
|
|
142
|
-
rowHover(row: RowHoverEvent<T>): void;
|
|
143
|
-
readonly slots: SlotManager<T>; // mount headless widgets
|
|
144
|
-
readonly icons: IconRegistry<T>; // declarative SVG icons for plugin UI
|
|
145
|
-
getSlotMounts(slot: SlotName): SlotMount<T>[];
|
|
146
|
-
showOverlay(options: OverlayOptions): void;
|
|
147
|
-
hideOverlay(): void;
|
|
148
|
-
getOverlayState(): OverlayState;
|
|
149
|
-
setLoading(isLoading: boolean, error?: unknown): void;
|
|
150
|
-
getRevision(): number; // monotonic counter for structural changes
|
|
151
|
-
destroy(): void;
|
|
152
|
-
isDestroyed(): boolean;
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
`registerHeaderExtension` lets plugins add header content without wrapping or replacing a
|
|
157
|
-
column's `headerRenderer`. Extensions render for each visible leaf column and can target the
|
|
158
|
-
`"start"`, `"end"`, or `"overlay"` region. Lower `priority` values render first; equal priorities
|
|
159
|
-
retain registration order. The returned cleanup callback removes the extension.
|
|
160
|
-
|
|
161
|
-
### Overlays
|
|
162
|
-
|
|
163
|
-
Overlays are managed in core state, so state transitions notify subscribers only when the
|
|
164
|
-
overlay changes (scroll position stays ephemeral). Initial `data: null` or omitted data shows
|
|
165
|
-
loading; an explicit empty array shows the no-rows state. Empty processed results with source
|
|
166
|
-
rows show no-matching-rows and leave horizontal column inspection enabled. Report fetch failures
|
|
167
|
-
through `setLoading(false, error)`; a successful `setData` clears the error.
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
grid.showOverlay({
|
|
171
|
-
type: "error",
|
|
172
|
-
// Errors default to "full"; other overlay types default to "bodyOnly".
|
|
173
|
-
// Set scope explicitly to choose a different placement.
|
|
174
|
-
message: "Could not load records",
|
|
175
|
-
});
|
|
176
|
-
grid.hideOverlay(); // restores automatic loading / empty-state evaluation
|
|
177
|
-
const overlay = grid.getOverlayState();
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
Every active overlay locks vertical scrolling while preserving the scrollbar gutter when one
|
|
181
|
-
is present. Horizontal scrolling remains enabled by default and can be locked with
|
|
182
|
-
`blockScroll: true`.
|
|
183
|
-
|
|
184
|
-
`overlayComponents` accepts framework-specific components through the adapter or core render
|
|
185
|
-
callbacks. Component props include `type`, `message`, `payload`, and the current `state`.
|
|
186
|
-
|
|
187
|
-
`GridEvents<T>`: `stateChange` | `viewportChange` | `dataChange` | `headerClick` | `rowClick` | `rowHover` | `slotsChange`.
|
|
188
|
-
Example: `grid.on("rowClick", (e) => console.log(e.id, e.index, e.data))`.
|
|
189
|
-
|
|
190
|
-
### Plugin contract — `GridPlugin<T>`
|
|
191
|
-
|
|
192
|
-
```ts
|
|
193
|
-
interface GridPlugin<T> {
|
|
194
|
-
name: string; // unique, namespaced, e.g. "@omnigrid/sorting-plugin"
|
|
195
|
-
register(api: GridApi<T>): void | (() => void); // returns cleanup
|
|
196
|
-
getRowClass?(params: RowRenderParams<T>): string | undefined;
|
|
197
|
-
getRowStyle?(params: RowRenderParams<T>): RowStyle | undefined;
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
A plugin hooks the grid via `api.on(...)`, `api.registerDataProcessor(...)`, `api.slots.mount(...)`,
|
|
202
|
-
and `api.icons.register(...)`; it must return a cleanup function that tears down everything it created.
|
|
203
|
-
Plugin source must remain framework-agnostic (see [`AI_INSTRUCTIONS.md`](../AI_INSTRUCTIONS.md) and
|
|
204
|
-
[`core/AI_INSTRUCTIONS.md`](AI_INSTRUCTIONS.md) in this repo).
|
|
205
|
-
|
|
206
|
-
## Quick start
|
|
207
|
-
|
|
208
|
-
Headless core only (no UI):
|
|
209
|
-
|
|
210
|
-
```ts
|
|
211
|
-
import { Grid } from "@omnigrid/core";
|
|
212
|
-
import type { ColumnDef } from "@omnigrid/core";
|
|
213
|
-
|
|
214
|
-
interface Row {
|
|
215
|
-
id: string;
|
|
216
|
-
name: string;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const columns: ColumnDef<Row>[] = [{ id: "name", field: "name", header: "Name", width: 220 }];
|
|
220
|
-
|
|
221
|
-
const grid = new Grid<Row>({ data: [{ id: "1", name: "Ada" }], columns, getRowId: (row) => row.id });
|
|
222
|
-
|
|
223
|
-
grid.setViewport({ width: 400, height: 300 }); // the adapter normally feeds geometry
|
|
224
|
-
const viewport = grid.getViewportData(); // virtualized rows + columns + offsets
|
|
225
|
-
grid.on("rowClick", (e) => console.log(e.index));
|
|
226
|
-
grid.destroy();
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
With base plugins:
|
|
230
|
-
|
|
231
|
-
```ts
|
|
232
|
-
import { Grid } from "@omnigrid/core";
|
|
233
|
-
import { PaginationPlugin } from "@omnigrid/pagination-plugin";
|
|
234
|
-
import { SelectionPlugin } from "@omnigrid/selection-plugin";
|
|
235
|
-
import { SortingPlugin } from "@omnigrid/sorting-plugin";
|
|
236
|
-
|
|
237
|
-
const grid = new Grid<Row>({
|
|
238
|
-
data,
|
|
239
|
-
columns,
|
|
240
|
-
getRowId: (row) => row.id,
|
|
241
|
-
plugins: [
|
|
242
|
-
new SortingPlugin<Row>(),
|
|
243
|
-
new SelectionPlugin<Row>({ mode: "multiple", showRowCheckboxes: true }),
|
|
244
|
-
new PaginationPlugin<Row>({ pageSize: 50 }),
|
|
245
|
-
],
|
|
246
|
-
});
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
In a UI framework, use the adapter — e.g. `@omnigrid/react`:
|
|
250
|
-
|
|
251
|
-
```tsx
|
|
252
|
-
<OmniGrid
|
|
253
|
-
columns={columns}
|
|
254
|
-
data={rows}
|
|
255
|
-
getRowId={(row) => row.id}
|
|
256
|
-
plugins={plugins}
|
|
257
|
-
style={{ height: 480 }}
|
|
258
|
-
/>
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
See [@omnigrid/react](../../adapters/react/README.md).
|
|
262
|
-
|
|
263
|
-
## Exported helpers
|
|
264
|
-
|
|
265
|
-
- `flattenColumns`, `isColumnLeaf`, `isColumnGroup`, `getColumnGroups` — column tree utilities.
|
|
266
|
-
- `resolveColSpan`, `getRowSpans`, `getCellSpanningParams` — column spanning
|
|
267
|
-
(`ColumnDef.colSpan`): normalized per-cell spans and per-row merged layouts.
|
|
268
|
-
- `Store`, `Virtualizer`, `EventBus`, `DomPool`, `SlotManager`, `IconRegistry` — building blocks
|
|
269
|
-
(mostly used internally; public for advanced integrations/tests).
|
|
270
|
-
|
|
271
|
-
### Column spanning (`colSpan`)
|
|
272
|
-
|
|
273
|
-
A leaf column may merge horizontally across consecutive visible columns:
|
|
274
|
-
|
|
275
|
-
```ts
|
|
276
|
-
const columns: ColumnDef<Row>[] = [
|
|
277
|
-
{ id: "title", width: 200, colSpan: 2 }, // static: always 2
|
|
278
|
-
{ id: "note", width: 160, colSpan: ({ data }) => (data.merged ? 3 : 1) },
|
|
279
|
-
];
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
`resolveColSpan` normalizes one cell (floors, clamps to `>= 1`, clips to the
|
|
283
|
-
pin group, falls back to `1` on resolver errors); `getRowSpans` / `Grid.getRowSpans`
|
|
284
|
-
aggregates a whole row — the origin carries the summed pixel width aligned to
|
|
285
|
-
the starting column, covered cells are skipped. `Grid.getViewportData`
|
|
286
|
-
additionally exposes the full leaf groups (`leafColumns` + `columnOffsets`,
|
|
287
|
-
pinned siblings likewise) and expands the shared column window so spans
|
|
288
|
-
crossing the viewport boundary are never clipped. The React adapter resolves
|
|
289
|
-
per-row layouts (memoized per viewport commit) and paints merged widths via
|
|
290
|
-
`translateX` without breaking node recycling.
|
|
291
|
-
|
|
292
|
-
## Links
|
|
293
|
-
|
|
294
|
-
- **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/core
|
|
295
|
-
- **Demo:** https://omnigrid.cc/
|
|
296
|
-
- **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
|
|
297
|
-
|
|
298
|
-
## License
|
|
299
|
-
|
|
300
|
-
MIT © KRZK Apps
|
|
1
|
+
# @omnigrid/core
|
|
2
|
+
|
|
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.
|
|
7
|
+
|
|
8
|
+
## What it provides
|
|
9
|
+
|
|
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
|
+
- **Column drag events** — adapters emit `columnDragStart`, `columnDrop`, and `columnDragEnd` for movable leaf and group headers.
|
|
16
|
+
- **DOM Pool mapping** — the core decides _which_ rows are visible; the adapter performs DOM ops via injected `DomPoolBindings`.
|
|
17
|
+
- **Slot Manager / `GridApi.slots`** — headless widgets mounted by plugins into `top | bottom | left | right`.
|
|
18
|
+
- **Column header extensions** — plugins register independent header elements; adapters render them beside the shared label and controls.
|
|
19
|
+
- **Icon Registry** — framework-agnostic declarative SVG icons for plugin UI (`api.icons.get(name)`).
|
|
20
|
+
|
|
21
|
+
**Scroll is ephemeral, not state.** `scrollTop`/`scrollLeft` are kept out of the Store:
|
|
22
|
+
scroll-only `setViewport({ scrollTop, scrollLeft })` calls never notify subscribers, so adapters do
|
|
23
|
+
not re-render on scroll frames. Read them via `getScrollPosition()`; use `scrollTo()` to request an
|
|
24
|
+
adapter-native scroll adjustment without moving scroll coordinates into the Store.
|
|
25
|
+
|
|
26
|
+
## Key contracts
|
|
27
|
+
|
|
28
|
+
### `GridOptions<T>`
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
interface GridOptions<T> {
|
|
32
|
+
data?: T[] | null; // row data; null/omitted means initial loading
|
|
33
|
+
columns: ColumnDef<T>[]; // required
|
|
34
|
+
getRowId?: (row: T, index: number) => RowId; // default: index-based
|
|
35
|
+
rowHeight?: number; // fixed px row height; default 32 (offset math is O(1))
|
|
36
|
+
rowOverscan?: number; // extra rows above/below viewport; default 5
|
|
37
|
+
columnOverscan?: number; // extra columns beyond horizontal window; default 2
|
|
38
|
+
cellFlash?: boolean | number; // highlight changed cells after applyTransaction
|
|
39
|
+
suppressRowHoverHighlight?: boolean;
|
|
40
|
+
rowStyle?: RowStyle;
|
|
41
|
+
getRowStyle?: (params: RowRenderParams<T>) => RowStyle | undefined;
|
|
42
|
+
rowClass?: string | ((params: RowRenderParams<T>) => string | undefined);
|
|
43
|
+
getRowClass?: (params: RowRenderParams<T>) => string | undefined;
|
|
44
|
+
rowClassRules?: RowClassRules<T>; // class name → predicate
|
|
45
|
+
plugins?: GridPlugin<T>[]; // plugin instances registered at construction
|
|
46
|
+
loading?: boolean; // active fetch; shows loading even with prior rows
|
|
47
|
+
overlayComponents?: Record<string, OverlayComponent<T>>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Row callbacks receive `params.rowType` (`"leaf"` or `"group"`). Group rows are synthetic and have no `data`; checking `rowType` narrows the callback parameters.
|
|
52
|
+
|
|
53
|
+
### `ColumnDef<T>`
|
|
54
|
+
|
|
55
|
+
`ColumnDef<T> = ColumnLeafDef<T> | ColumnGroupDef<T>`. Groups nest via `children`; width/visibility/pinning can be combined freely.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
interface ColumnLeafDef<T> {
|
|
59
|
+
id: string; // unique column id (also stable slot key)
|
|
60
|
+
header: string;
|
|
61
|
+
field?: keyof T | string; // value accessor (default cell value source)
|
|
62
|
+
valueGetter?: (row: T) => unknown; // overrides field
|
|
63
|
+
valueFormatter?: (value: unknown) => string;
|
|
64
|
+
width?: number; // fixed px width
|
|
65
|
+
flex?: number; // flex weight; expands to fill remaining space
|
|
66
|
+
minWidth?: number;
|
|
67
|
+
maxWidth?: number;
|
|
68
|
+
hidden?: boolean;
|
|
69
|
+
movable?: boolean; // opt in to leaf drag-and-drop with a reorder plugin
|
|
70
|
+
pinned?: "left" | "right"; // fixed column in horizontal viewport
|
|
71
|
+
sortable?: boolean; // read by SortingPlugin
|
|
72
|
+
sortState?: SortDirection; // "asc" | "desc" — initial sort for SortingPlugin
|
|
73
|
+
sortIndex?: number; // runtime 1-based priority when multi-sorted
|
|
74
|
+
align?: "left" | "center" | "right";
|
|
75
|
+
cellRenderer?: (params: CellRenderParams<T>) => unknown; // opaque content, adapter materializes
|
|
76
|
+
cellFlash?: boolean; // set false to opt out when GridOptions.cellFlash is enabled
|
|
77
|
+
cellStyle?: RowStyle;
|
|
78
|
+
getCellStyle?: (params: CellRenderParams<T>) => RowStyle | undefined;
|
|
79
|
+
cellClass?: string | ((params: CellRenderParams<T>) => string | undefined);
|
|
80
|
+
getCellClass?: (params: CellRenderParams<T>) => string | undefined;
|
|
81
|
+
cellClassRules?: CellClassRules<T>;
|
|
82
|
+
getColumnStyle?: (params: ColumnStyleParams<T>) => ColumnStyle | undefined;
|
|
83
|
+
getColumnClass?: (params: ColumnStyleParams<T>) => string | undefined;
|
|
84
|
+
columnClassRules?: ColumnClassRules<T>;
|
|
85
|
+
headerStyle?: HeaderStyle;
|
|
86
|
+
getHeaderStyle?: (params: HeaderRenderParams<T>) => HeaderStyle | undefined;
|
|
87
|
+
headerClass?: string | ((params: HeaderRenderParams<T>) => string | undefined);
|
|
88
|
+
getHeaderClass?: (params: HeaderRenderParams<T>) => string | undefined;
|
|
89
|
+
headerClassRules?: HeaderClassRules<T>;
|
|
90
|
+
headerRenderer?: (column: ColumnDef<T>) => unknown; // primary header content; adapter controls remain separate
|
|
91
|
+
stopRowClick?: boolean;
|
|
92
|
+
stopHeaderClick?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface ColumnGroupDef<T> {
|
|
96
|
+
id: string;
|
|
97
|
+
header: string;
|
|
98
|
+
children: ColumnDef<T>[]; // nested leaves / groups
|
|
99
|
+
movable?: boolean; // opt in to moving the entire group with a reorder plugin
|
|
100
|
+
pinned?: "left" | "right";
|
|
101
|
+
collapsible?: boolean;
|
|
102
|
+
defaultExpanded?: boolean;
|
|
103
|
+
headerStyle?: HeaderStyle;
|
|
104
|
+
getHeaderStyle?: (params: HeaderGroupRenderParams<T>) => HeaderStyle | undefined;
|
|
105
|
+
headerClass?: string | ((params: HeaderGroupRenderParams<T>) => string | undefined);
|
|
106
|
+
getHeaderClass?: (params: HeaderGroupRenderParams<T>) => string | undefined;
|
|
107
|
+
headerClassRules?: HeaderGroupClassRules<T>;
|
|
108
|
+
headerRenderer?: (column: ColumnDef<T>) => unknown;
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`movable` is available on both leaf columns and groups. Dragging a group moves its complete
|
|
113
|
+
subtree; dropping after the last leaf in the final group places the dragged column after the group.
|
|
114
|
+
|
|
115
|
+
When grid-level `cellFlash` is enabled, set `cellFlash: false` on an individual leaf column to opt it out.
|
|
116
|
+
|
|
117
|
+
### `GridApi<T>`
|
|
118
|
+
|
|
119
|
+
`Grid<T>` implements this interface — the single public surface for adapters and plugins.
|
|
120
|
+
UI code must never touch core internals directly.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
interface GridApi<T> {
|
|
124
|
+
getState(): GridState<T>; // { data, columns, viewport, rowHeight, rowOverscan, columnOverscan }
|
|
125
|
+
getScrollPosition(): ScrollPosition; // ephemeral scrollTop/scrollLeft
|
|
126
|
+
scrollTo(position: Partial<ScrollPosition>): void; // request a native scroll adjustment from the adapter
|
|
127
|
+
getViewportData(): ViewportData<T>; // virtualized window: rows/columns + absolute offsets
|
|
128
|
+
getProcessedData(): T[]; // after all registered data processors
|
|
129
|
+
getRowId(row: T, index: number): RowId;
|
|
130
|
+
setData(data: T[] | null | undefined): void;
|
|
131
|
+
setViewport(viewport: Partial<ViewportState>): void; // width/height/scrollTop/scrollLeft; scroll-only ⇒ no Store notify
|
|
132
|
+
refresh(): void; // repaint hints (rowClassRules), bumps revision
|
|
133
|
+
subscribe(listener: () => void): () => void;
|
|
134
|
+
on<E extends keyof GridEvents<T>>(event: E, listener: (p: GridEvents<T>[E]) => void): () => void;
|
|
135
|
+
registerPlugin(plugin: GridPlugin<T>): () => void;
|
|
136
|
+
registerHeaderExtension(extension: ColumnHeaderExtension<T>): () => void;
|
|
137
|
+
getHeaderExtensions(position?: ColumnHeaderExtensionPosition): ColumnHeaderExtension<T>[];
|
|
138
|
+
registerDataProcessor(processor: DataProcessor<T>): () => void;
|
|
139
|
+
setColumns(columns: ColumnDef<T>[]): void;
|
|
140
|
+
headerClick(columnId: string, multiSort?: boolean): void;
|
|
141
|
+
rowClick(row: RowClickEvent<T>): void;
|
|
142
|
+
rowHover(row: RowHoverEvent<T>): void;
|
|
143
|
+
readonly slots: SlotManager<T>; // mount headless widgets
|
|
144
|
+
readonly icons: IconRegistry<T>; // declarative SVG icons for plugin UI
|
|
145
|
+
getSlotMounts(slot: SlotName): SlotMount<T>[];
|
|
146
|
+
showOverlay(options: OverlayOptions): void;
|
|
147
|
+
hideOverlay(): void;
|
|
148
|
+
getOverlayState(): OverlayState;
|
|
149
|
+
setLoading(isLoading: boolean, error?: unknown): void;
|
|
150
|
+
getRevision(): number; // monotonic counter for structural changes
|
|
151
|
+
destroy(): void;
|
|
152
|
+
isDestroyed(): boolean;
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`registerHeaderExtension` lets plugins add header content without wrapping or replacing a
|
|
157
|
+
column's `headerRenderer`. Extensions render for each visible leaf column and can target the
|
|
158
|
+
`"start"`, `"end"`, or `"overlay"` region. Lower `priority` values render first; equal priorities
|
|
159
|
+
retain registration order. The returned cleanup callback removes the extension.
|
|
160
|
+
|
|
161
|
+
### Overlays
|
|
162
|
+
|
|
163
|
+
Overlays are managed in core state, so state transitions notify subscribers only when the
|
|
164
|
+
overlay changes (scroll position stays ephemeral). Initial `data: null` or omitted data shows
|
|
165
|
+
loading; an explicit empty array shows the no-rows state. Empty processed results with source
|
|
166
|
+
rows show no-matching-rows and leave horizontal column inspection enabled. Report fetch failures
|
|
167
|
+
through `setLoading(false, error)`; a successful `setData` clears the error.
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
grid.showOverlay({
|
|
171
|
+
type: "error",
|
|
172
|
+
// Errors default to "full"; other overlay types default to "bodyOnly".
|
|
173
|
+
// Set scope explicitly to choose a different placement.
|
|
174
|
+
message: "Could not load records",
|
|
175
|
+
});
|
|
176
|
+
grid.hideOverlay(); // restores automatic loading / empty-state evaluation
|
|
177
|
+
const overlay = grid.getOverlayState();
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Every active overlay locks vertical scrolling while preserving the scrollbar gutter when one
|
|
181
|
+
is present. Horizontal scrolling remains enabled by default and can be locked with
|
|
182
|
+
`blockScroll: true`.
|
|
183
|
+
|
|
184
|
+
`overlayComponents` accepts framework-specific components through the adapter or core render
|
|
185
|
+
callbacks. Component props include `type`, `message`, `payload`, and the current `state`.
|
|
186
|
+
|
|
187
|
+
`GridEvents<T>`: `stateChange` | `viewportChange` | `dataChange` | `headerClick` | `rowClick` | `rowHover` | `slotsChange`.
|
|
188
|
+
Example: `grid.on("rowClick", (e) => console.log(e.id, e.index, e.data))`.
|
|
189
|
+
|
|
190
|
+
### Plugin contract — `GridPlugin<T>`
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
interface GridPlugin<T> {
|
|
194
|
+
name: string; // unique, namespaced, e.g. "@omnigrid/sorting-plugin"
|
|
195
|
+
register(api: GridApi<T>): void | (() => void); // returns cleanup
|
|
196
|
+
getRowClass?(params: RowRenderParams<T>): string | undefined;
|
|
197
|
+
getRowStyle?(params: RowRenderParams<T>): RowStyle | undefined;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
A plugin hooks the grid via `api.on(...)`, `api.registerDataProcessor(...)`, `api.slots.mount(...)`,
|
|
202
|
+
and `api.icons.register(...)`; it must return a cleanup function that tears down everything it created.
|
|
203
|
+
Plugin source must remain framework-agnostic (see [`AI_INSTRUCTIONS.md`](../AI_INSTRUCTIONS.md) and
|
|
204
|
+
[`core/AI_INSTRUCTIONS.md`](AI_INSTRUCTIONS.md) in this repo).
|
|
205
|
+
|
|
206
|
+
## Quick start
|
|
207
|
+
|
|
208
|
+
Headless core only (no UI):
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { Grid } from "@omnigrid/core";
|
|
212
|
+
import type { ColumnDef } from "@omnigrid/core";
|
|
213
|
+
|
|
214
|
+
interface Row {
|
|
215
|
+
id: string;
|
|
216
|
+
name: string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const columns: ColumnDef<Row>[] = [{ id: "name", field: "name", header: "Name", width: 220 }];
|
|
220
|
+
|
|
221
|
+
const grid = new Grid<Row>({ data: [{ id: "1", name: "Ada" }], columns, getRowId: (row) => row.id });
|
|
222
|
+
|
|
223
|
+
grid.setViewport({ width: 400, height: 300 }); // the adapter normally feeds geometry
|
|
224
|
+
const viewport = grid.getViewportData(); // virtualized rows + columns + offsets
|
|
225
|
+
grid.on("rowClick", (e) => console.log(e.index));
|
|
226
|
+
grid.destroy();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
With base plugins:
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { Grid } from "@omnigrid/core";
|
|
233
|
+
import { PaginationPlugin } from "@omnigrid/pagination-plugin";
|
|
234
|
+
import { SelectionPlugin } from "@omnigrid/selection-plugin";
|
|
235
|
+
import { SortingPlugin } from "@omnigrid/sorting-plugin";
|
|
236
|
+
|
|
237
|
+
const grid = new Grid<Row>({
|
|
238
|
+
data,
|
|
239
|
+
columns,
|
|
240
|
+
getRowId: (row) => row.id,
|
|
241
|
+
plugins: [
|
|
242
|
+
new SortingPlugin<Row>(),
|
|
243
|
+
new SelectionPlugin<Row>({ mode: "multiple", showRowCheckboxes: true }),
|
|
244
|
+
new PaginationPlugin<Row>({ pageSize: 50 }),
|
|
245
|
+
],
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
In a UI framework, use the adapter — e.g. `@omnigrid/react`:
|
|
250
|
+
|
|
251
|
+
```tsx
|
|
252
|
+
<OmniGrid
|
|
253
|
+
columns={columns}
|
|
254
|
+
data={rows}
|
|
255
|
+
getRowId={(row) => row.id}
|
|
256
|
+
plugins={plugins}
|
|
257
|
+
style={{ height: 480 }}
|
|
258
|
+
/>
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
See [@omnigrid/react](../../adapters/react/README.md).
|
|
262
|
+
|
|
263
|
+
## Exported helpers
|
|
264
|
+
|
|
265
|
+
- `flattenColumns`, `isColumnLeaf`, `isColumnGroup`, `getColumnGroups` — column tree utilities.
|
|
266
|
+
- `resolveColSpan`, `getRowSpans`, `getCellSpanningParams` — column spanning
|
|
267
|
+
(`ColumnDef.colSpan`): normalized per-cell spans and per-row merged layouts.
|
|
268
|
+
- `Store`, `Virtualizer`, `EventBus`, `DomPool`, `SlotManager`, `IconRegistry` — building blocks
|
|
269
|
+
(mostly used internally; public for advanced integrations/tests).
|
|
270
|
+
|
|
271
|
+
### Column spanning (`colSpan`)
|
|
272
|
+
|
|
273
|
+
A leaf column may merge horizontally across consecutive visible columns:
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
const columns: ColumnDef<Row>[] = [
|
|
277
|
+
{ id: "title", width: 200, colSpan: 2 }, // static: always 2
|
|
278
|
+
{ id: "note", width: 160, colSpan: ({ data }) => (data.merged ? 3 : 1) },
|
|
279
|
+
];
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
`resolveColSpan` normalizes one cell (floors, clamps to `>= 1`, clips to the
|
|
283
|
+
pin group, falls back to `1` on resolver errors); `getRowSpans` / `Grid.getRowSpans`
|
|
284
|
+
aggregates a whole row — the origin carries the summed pixel width aligned to
|
|
285
|
+
the starting column, covered cells are skipped. `Grid.getViewportData`
|
|
286
|
+
additionally exposes the full leaf groups (`leafColumns` + `columnOffsets`,
|
|
287
|
+
pinned siblings likewise) and expands the shared column window so spans
|
|
288
|
+
crossing the viewport boundary are never clipped. The React adapter resolves
|
|
289
|
+
per-row layouts (memoized per viewport commit) and paints merged widths via
|
|
290
|
+
`translateX` without breaking node recycling.
|
|
291
|
+
|
|
292
|
+
## Links
|
|
293
|
+
|
|
294
|
+
- **Source:** https://github.com/KRZK-Apps/omnigrid/tree/main/core
|
|
295
|
+
- **Demo:** https://omnigrid.cc/demo/react/quick-start
|
|
296
|
+
- **Issues:** https://github.com/KRZK-Apps/omnigrid/issues
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
MIT © KRZK Apps
|
package/dist/events.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare class EventBus<Events extends object> {
|
|
2
2
|
private readonly listeners;
|
|
3
|
+
private readonly anyListeners;
|
|
3
4
|
on<EventName extends keyof Events>(event: EventName, listener: (payload: Events[EventName]) => void): () => void;
|
|
5
|
+
onAny(listener: (event: string, payload: unknown) => void): () => void;
|
|
4
6
|
emit<EventName extends keyof Events>(event: EventName, payload: Events[EventName]): void;
|
|
5
7
|
clear(): void;
|
|
6
8
|
}
|
package/dist/events.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export class EventBus {
|
|
2
2
|
constructor() {
|
|
3
3
|
this.listeners = new Map();
|
|
4
|
+
this.anyListeners = new Set();
|
|
4
5
|
}
|
|
5
6
|
on(event, listener) {
|
|
6
7
|
let eventListeners = this.listeners.get(event);
|
|
@@ -13,10 +14,18 @@ export class EventBus {
|
|
|
13
14
|
eventListeners?.delete(listener);
|
|
14
15
|
};
|
|
15
16
|
}
|
|
17
|
+
onAny(listener) {
|
|
18
|
+
this.anyListeners.add(listener);
|
|
19
|
+
return () => {
|
|
20
|
+
this.anyListeners.delete(listener);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
16
23
|
emit(event, payload) {
|
|
17
24
|
this.listeners.get(event)?.forEach((listener) => listener(payload));
|
|
25
|
+
this.anyListeners.forEach((listener) => listener(event, payload));
|
|
18
26
|
}
|
|
19
27
|
clear() {
|
|
20
28
|
this.listeners.clear();
|
|
29
|
+
this.anyListeners.clear();
|
|
21
30
|
}
|
|
22
31
|
}
|
package/dist/grid.d.ts
CHANGED
|
@@ -33,6 +33,8 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
33
33
|
private readonly popups;
|
|
34
34
|
private revision;
|
|
35
35
|
private destroyed;
|
|
36
|
+
/** Guards the coalesced `stateChange` microtask so one synchronous tick emits at most one event. */
|
|
37
|
+
private stateChangeScheduled;
|
|
36
38
|
/**
|
|
37
39
|
* Overlay management state:
|
|
38
40
|
* - `loading`: whether the grid is fetching data (set via setLoading / options.loading)
|
|
@@ -218,6 +220,12 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
218
220
|
setGroupRowRenderer(): void;
|
|
219
221
|
getFilteredData(): T[];
|
|
220
222
|
private static cloneFilterModel;
|
|
223
|
+
/**
|
|
224
|
+
* Shallow structural comparison of two filter models. Conditions are
|
|
225
|
+
* compared by operator and operands so an equivalent re-applied model is
|
|
226
|
+
* detected without allocating a canonical string form.
|
|
227
|
+
*/
|
|
228
|
+
private static filterModelsEqual;
|
|
221
229
|
setFilterModel(model: FilterModel): void;
|
|
222
230
|
getFilterModel(): FilterModel;
|
|
223
231
|
setColumnFilter(field: string, condition: FilterCondition | null): void;
|
|
@@ -310,6 +318,16 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
310
318
|
*/
|
|
311
319
|
applyTransaction(transaction: RowTransaction<T>): TransactionResult<T>;
|
|
312
320
|
setViewport(viewport: Partial<ViewportState>): void;
|
|
321
|
+
/**
|
|
322
|
+
* Coalesces `stateChange` into a single microtask per synchronous tick.
|
|
323
|
+
*
|
|
324
|
+
* A burst of synchronous state updates (e.g. a page change that writes both
|
|
325
|
+
* the pagination slice and the dataset) collapses into ONE `stateChange`
|
|
326
|
+
* carrying the final snapshot, instead of a stream of intermediate events.
|
|
327
|
+
* Domain events (`dataChange`, `viewportChange`, `overlayChange`) stay
|
|
328
|
+
* synchronous and precise.
|
|
329
|
+
*/
|
|
330
|
+
private scheduleStateChange;
|
|
313
331
|
/**
|
|
314
332
|
* Repaints the current viewport: notifies Store subscribers that
|
|
315
333
|
* visible rows and row classes (rowClassRules) may need rebinding
|
|
@@ -331,6 +349,7 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
331
349
|
rowHover(row: RowHoverEvent<T>): void;
|
|
332
350
|
subscribe(listener: () => void): () => void;
|
|
333
351
|
on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
|
|
352
|
+
onAny(listener: (event: string, payload: unknown) => void): () => void;
|
|
334
353
|
emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
|
|
335
354
|
registerPlugin(plugin: GridPlugin<T>): () => void;
|
|
336
355
|
/**
|
package/dist/grid.js
CHANGED
|
@@ -31,6 +31,8 @@ export class Grid {
|
|
|
31
31
|
this.popups = new PopupManager((popup) => this.events.emit("popupChange", popup));
|
|
32
32
|
this.revision = 0;
|
|
33
33
|
this.destroyed = false;
|
|
34
|
+
/** Guards the coalesced `stateChange` microtask so one synchronous tick emits at most one event. */
|
|
35
|
+
this.stateChangeScheduled = false;
|
|
34
36
|
this.loading = false;
|
|
35
37
|
this.hasData = true;
|
|
36
38
|
this.overlayState = { type: null };
|
|
@@ -96,7 +98,7 @@ export class Grid {
|
|
|
96
98
|
});
|
|
97
99
|
this.slots = new SlotManager({
|
|
98
100
|
onChange: (slot, mounts) => {
|
|
99
|
-
this.store.setState({});
|
|
101
|
+
this.store.setState({}, { force: true });
|
|
100
102
|
this.events.emit("slotsChange", { slot, mounts });
|
|
101
103
|
},
|
|
102
104
|
});
|
|
@@ -107,7 +109,7 @@ export class Grid {
|
|
|
107
109
|
if (!this.destroyed)
|
|
108
110
|
this.recomputeOverlay();
|
|
109
111
|
});
|
|
110
|
-
this.store.subscribe(() => this.
|
|
112
|
+
this.store.subscribe(() => this.scheduleStateChange());
|
|
111
113
|
options.plugins?.forEach((plugin) => this.registerPlugin(plugin));
|
|
112
114
|
this.recomputeOverlay();
|
|
113
115
|
}
|
|
@@ -678,6 +680,40 @@ export class Grid {
|
|
|
678
680
|
}
|
|
679
681
|
return clone;
|
|
680
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* Shallow structural comparison of two filter models. Conditions are
|
|
685
|
+
* compared by operator and operands so an equivalent re-applied model is
|
|
686
|
+
* detected without allocating a canonical string form.
|
|
687
|
+
*/
|
|
688
|
+
static filterModelsEqual(left, right) {
|
|
689
|
+
const leftKeys = Object.keys(left);
|
|
690
|
+
const rightKeys = Object.keys(right);
|
|
691
|
+
if (leftKeys.length !== rightKeys.length)
|
|
692
|
+
return false;
|
|
693
|
+
for (const key of leftKeys) {
|
|
694
|
+
const a = left[key];
|
|
695
|
+
const b = right[key];
|
|
696
|
+
if (!a || !b)
|
|
697
|
+
return false;
|
|
698
|
+
if (a.logic !== b.logic)
|
|
699
|
+
return false;
|
|
700
|
+
if (a.conditions.length !== b.conditions.length)
|
|
701
|
+
return false;
|
|
702
|
+
for (let index = 0; index < a.conditions.length; index += 1) {
|
|
703
|
+
const ca = a.conditions[index];
|
|
704
|
+
const cb = b.conditions[index];
|
|
705
|
+
if (ca.operator !== cb.operator)
|
|
706
|
+
return false;
|
|
707
|
+
if (!Object.is(ca.value, cb.value))
|
|
708
|
+
return false;
|
|
709
|
+
if (!Object.is(ca.valueTo, cb.valueTo))
|
|
710
|
+
return false;
|
|
711
|
+
if (!Object.is(ca.caseSensitive, cb.caseSensitive))
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
681
717
|
setFilterModel(model) {
|
|
682
718
|
this.assertActive();
|
|
683
719
|
const next = {};
|
|
@@ -685,6 +721,10 @@ export class Grid {
|
|
|
685
721
|
if (entry.conditions.length > 0)
|
|
686
722
|
next[key] = entry;
|
|
687
723
|
}
|
|
724
|
+
// Repeated writes of an equivalent model (e.g. a controlled prop
|
|
725
|
+
// re-applied on every render) must not trigger stateChange/filterChanged.
|
|
726
|
+
if (Grid.filterModelsEqual(this.filterModel, next))
|
|
727
|
+
return;
|
|
688
728
|
this.filterModel = next;
|
|
689
729
|
this.filteredDataCache = null;
|
|
690
730
|
this.processedDataCache = null;
|
|
@@ -692,7 +732,8 @@ export class Grid {
|
|
|
692
732
|
this.groupedRowsRevision = -1;
|
|
693
733
|
this.revision += 1;
|
|
694
734
|
this.scrollTo({ scrollTop: 0 });
|
|
695
|
-
|
|
735
|
+
// The filter model lives outside the Store, so force a repaint notification.
|
|
736
|
+
this.store.setState({}, { force: true });
|
|
696
737
|
this.recomputeOverlay();
|
|
697
738
|
this.events.emit("filterChanged", this.getFilterModel());
|
|
698
739
|
}
|
|
@@ -808,7 +849,7 @@ export class Grid {
|
|
|
808
849
|
if (this.virtualizer.getRowHeight(rowIndex) === previousHeight)
|
|
809
850
|
return 0;
|
|
810
851
|
if (options?.notify !== false)
|
|
811
|
-
this.store.setState({});
|
|
852
|
+
this.store.setState({}, { force: true });
|
|
812
853
|
if (correction !== 0 && options?.anchor !== false) {
|
|
813
854
|
this.scrollTo({ scrollTop: scrollTop + correction });
|
|
814
855
|
}
|
|
@@ -828,7 +869,7 @@ export class Grid {
|
|
|
828
869
|
const scrollTop = this.scrollPosition.scrollTop;
|
|
829
870
|
const correction = this.virtualizer.resetRowHeight(rowIndex, scrollTop, count);
|
|
830
871
|
if (options?.notify !== false)
|
|
831
|
-
this.store.setState({});
|
|
872
|
+
this.store.setState({}, { force: true });
|
|
832
873
|
if (correction !== 0 && options?.anchor !== false) {
|
|
833
874
|
this.scrollTo({ scrollTop: scrollTop + correction });
|
|
834
875
|
}
|
|
@@ -839,7 +880,7 @@ export class Grid {
|
|
|
839
880
|
this.assertActive();
|
|
840
881
|
this.virtualizer.resetAllRowHeights();
|
|
841
882
|
this.revision += 1;
|
|
842
|
-
this.store.setState({});
|
|
883
|
+
this.store.setState({}, { force: true });
|
|
843
884
|
}
|
|
844
885
|
/**
|
|
845
886
|
* Returns the absolute vertical offset (px) of the top of a row,
|
|
@@ -1072,6 +1113,26 @@ export class Grid {
|
|
|
1072
1113
|
}
|
|
1073
1114
|
this.events.emit("viewportChange", nextViewport);
|
|
1074
1115
|
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Coalesces `stateChange` into a single microtask per synchronous tick.
|
|
1118
|
+
*
|
|
1119
|
+
* A burst of synchronous state updates (e.g. a page change that writes both
|
|
1120
|
+
* the pagination slice and the dataset) collapses into ONE `stateChange`
|
|
1121
|
+
* carrying the final snapshot, instead of a stream of intermediate events.
|
|
1122
|
+
* Domain events (`dataChange`, `viewportChange`, `overlayChange`) stay
|
|
1123
|
+
* synchronous and precise.
|
|
1124
|
+
*/
|
|
1125
|
+
scheduleStateChange() {
|
|
1126
|
+
if (this.stateChangeScheduled)
|
|
1127
|
+
return;
|
|
1128
|
+
this.stateChangeScheduled = true;
|
|
1129
|
+
queueMicrotask(() => {
|
|
1130
|
+
this.stateChangeScheduled = false;
|
|
1131
|
+
if (this.destroyed)
|
|
1132
|
+
return;
|
|
1133
|
+
this.events.emit("stateChange", this.store.getState());
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1075
1136
|
/**
|
|
1076
1137
|
* Repaints the current viewport: notifies Store subscribers that
|
|
1077
1138
|
* visible rows and row classes (rowClassRules) may need rebinding
|
|
@@ -1081,7 +1142,7 @@ export class Grid {
|
|
|
1081
1142
|
refresh() {
|
|
1082
1143
|
this.assertActive();
|
|
1083
1144
|
this.revision += 1;
|
|
1084
|
-
this.store.setState({});
|
|
1145
|
+
this.store.setState({}, { force: true });
|
|
1085
1146
|
}
|
|
1086
1147
|
/**
|
|
1087
1148
|
* Replaces the column definitions and optionally updates shared leaf-column defaults.
|
|
@@ -1140,6 +1201,10 @@ export class Grid {
|
|
|
1140
1201
|
this.assertActive();
|
|
1141
1202
|
return this.events.on(event, listener);
|
|
1142
1203
|
}
|
|
1204
|
+
onAny(listener) {
|
|
1205
|
+
this.assertActive();
|
|
1206
|
+
return this.events.onAny(listener);
|
|
1207
|
+
}
|
|
1143
1208
|
emit(event, payload) {
|
|
1144
1209
|
this.assertActive();
|
|
1145
1210
|
this.events.emit(event, payload);
|
|
@@ -1167,13 +1232,13 @@ export class Grid {
|
|
|
1167
1232
|
this.assertActive();
|
|
1168
1233
|
const registered = { ...extension };
|
|
1169
1234
|
this.headerExtensions.set(extension.id, registered);
|
|
1170
|
-
this.store.setState({});
|
|
1235
|
+
this.store.setState({}, { force: true });
|
|
1171
1236
|
return () => {
|
|
1172
1237
|
if (this.headerExtensions.get(extension.id) !== registered)
|
|
1173
1238
|
return;
|
|
1174
1239
|
this.headerExtensions.delete(extension.id);
|
|
1175
1240
|
if (!this.destroyed)
|
|
1176
|
-
this.store.setState({});
|
|
1241
|
+
this.store.setState({}, { force: true });
|
|
1177
1242
|
};
|
|
1178
1243
|
}
|
|
1179
1244
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { Grid } from "./grid.js";
|
|
|
7
7
|
export { Grid as GridCore } from "./grid.js";
|
|
8
8
|
export { SlotManager } from "./slots.js";
|
|
9
9
|
export { Store } from "./store.js";
|
|
10
|
-
export type { StateUpdater } from "./store.js";
|
|
10
|
+
export type { SetStateOptions, StateUpdater } from "./store.js";
|
|
11
11
|
export { DataPipeline, PIPELINE_STAGE_ORDER } from "./pipeline.js";
|
|
12
12
|
export type { PipelineProcessor, PipelineStage } from "./pipeline.js";
|
|
13
13
|
export { applyFilterModel, applyQuickFilter, isActiveCondition, isFilterModelActive, matchesColumnFilter, } from "./filtering.js";
|
package/dist/store.d.ts
CHANGED
|
@@ -1,10 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A partial state patch, or a reducer that derives one from the current state.
|
|
3
|
+
*
|
|
4
|
+
* @api
|
|
5
|
+
*/
|
|
1
6
|
export type StateUpdater<State> = Partial<State> | ((state: State) => Partial<State>);
|
|
7
|
+
/**
|
|
8
|
+
* Options for {@link Store.setState}.
|
|
9
|
+
*
|
|
10
|
+
* @api
|
|
11
|
+
*/
|
|
12
|
+
export interface SetStateOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Notify subscribers even when the patch does not change any field.
|
|
15
|
+
*
|
|
16
|
+
* Used for forced repaints — e.g. a geometry refresh or a slot/header
|
|
17
|
+
* change — where the Store state itself is unchanged but subscribers must
|
|
18
|
+
* re-read it to repaint.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
force?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Minimal observable state container.
|
|
25
|
+
*
|
|
26
|
+
* `setState` performs a shallow, per-key equality check and only notifies
|
|
27
|
+
* subscribers when at least one field actually changes (by reference). This
|
|
28
|
+
* suppresses the redundant notifications caused by empty patches
|
|
29
|
+
* (`setState({})`) and repeated writes of the same value. Callers that need an
|
|
30
|
+
* unconditional notification pass `{ force: true }`.
|
|
31
|
+
*
|
|
32
|
+
* @api
|
|
33
|
+
*/
|
|
2
34
|
export declare class Store<State extends object> {
|
|
3
35
|
private state;
|
|
4
36
|
private readonly listeners;
|
|
5
37
|
constructor(initialState: State);
|
|
6
38
|
getState(): State;
|
|
7
|
-
setState(updater: StateUpdater<State
|
|
39
|
+
setState(updater: StateUpdater<State>, options?: SetStateOptions): void;
|
|
8
40
|
subscribe(listener: () => void): () => void;
|
|
9
41
|
clear(): void;
|
|
42
|
+
private static hasChanges;
|
|
10
43
|
}
|
package/dist/store.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal observable state container.
|
|
3
|
+
*
|
|
4
|
+
* `setState` performs a shallow, per-key equality check and only notifies
|
|
5
|
+
* subscribers when at least one field actually changes (by reference). This
|
|
6
|
+
* suppresses the redundant notifications caused by empty patches
|
|
7
|
+
* (`setState({})`) and repeated writes of the same value. Callers that need an
|
|
8
|
+
* unconditional notification pass `{ force: true }`.
|
|
9
|
+
*
|
|
10
|
+
* @api
|
|
11
|
+
*/
|
|
1
12
|
export class Store {
|
|
2
13
|
constructor(initialState) {
|
|
3
14
|
this.listeners = new Set();
|
|
@@ -6,8 +17,11 @@ export class Store {
|
|
|
6
17
|
getState() {
|
|
7
18
|
return this.state;
|
|
8
19
|
}
|
|
9
|
-
setState(updater) {
|
|
10
|
-
const update = typeof updater ===
|
|
20
|
+
setState(updater, options) {
|
|
21
|
+
const update = typeof updater === "function" ? updater(this.state) : updater;
|
|
22
|
+
if (options?.force !== true && !Store.hasChanges(this.state, update)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
11
25
|
this.state = { ...this.state, ...update };
|
|
12
26
|
this.listeners.forEach((listener) => listener());
|
|
13
27
|
}
|
|
@@ -18,4 +32,11 @@ export class Store {
|
|
|
18
32
|
clear() {
|
|
19
33
|
this.listeners.clear();
|
|
20
34
|
}
|
|
35
|
+
static hasChanges(previous, patch) {
|
|
36
|
+
for (const key of Object.keys(patch)) {
|
|
37
|
+
if (!Object.is(previous[key], patch[key]))
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
21
42
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1394,6 +1394,7 @@ export interface CoreGridApi<T> {
|
|
|
1394
1394
|
refresh(): void;
|
|
1395
1395
|
subscribe(listener: () => void): () => void;
|
|
1396
1396
|
on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
|
|
1397
|
+
onAny(listener: (event: string, payload: unknown) => void): () => void;
|
|
1397
1398
|
/** Emits a typed grid event for plugin-to-plugin and adapter notifications. */
|
|
1398
1399
|
emit<EventName extends keyof GridEvents<T>>(event: EventName, payload: GridEvents<T>[EventName]): void;
|
|
1399
1400
|
registerPlugin(plugin: GridPlugin<T>): () => void;
|