@omnigrid/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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KRZK Apps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,33 @@
1
+ import type { ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef } from "./types";
2
+ /**
3
+ * Runtime narrowing: does the column definition describe a GROUP
4
+ * (it carries `children`) rather than a leaf column?
5
+ */
6
+ export declare function isColumnGroup<T>(column: ColumnDef<T>): column is ColumnGroupDef<T>;
7
+ /** Inverse of `isColumnGroup` — handy for filters over the flat working set. */
8
+ export declare function isColumnLeaf<T>(column: ColumnDef<T>): column is ColumnLeafDef<T>;
9
+ /**
10
+ * Depth-first flattening of the hierarchical column definitions into the
11
+ * flat array of LEAF columns.
12
+ *
13
+ * This is the working set for virtualization: the Virtualizer and all
14
+ * viewport geometry operate on `ColumnLeafDef<T>` only and never see group
15
+ * nodes. Order is preserved (pre-order traversal), so grouped children stay
16
+ * in place. `hidden` leaves are NOT filtered here — visibility filtering is
17
+ * the caller's concern.
18
+ */
19
+ export declare function flattenColumns<T>(columns: ColumnDef<T>[]): ColumnLeafDef<T>[];
20
+ /**
21
+ * Projects every group onto the flat VISIBLE leaf layout.
22
+ *
23
+ * For each group (in pre-order) returns its span over the visible leaf
24
+ * indices: `[startIndex, endIndex)` is the range of leaf columns the group
25
+ * header covers on its header row. The indices address the same flat
26
+ * ordering as the `index` on `ViewportData` columns — computed by walking
27
+ * the hierarchy while counting leaves for which `isVisible` returns true.
28
+ *
29
+ * Groups that contain no visible leaves are omitted. `depth` is the nesting
30
+ * level (top-level groups are 0), letting the adapter reconstruct the group
31
+ * tree when rendering stacked header rows.
32
+ */
33
+ export declare function getColumnGroups<T>(columns: ColumnDef<T>[], isVisible?: (leaf: ColumnLeafDef<T>) => boolean): ColumnGroupViewport<T>[];
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Runtime narrowing: does the column definition describe a GROUP
3
+ * (it carries `children`) rather than a leaf column?
4
+ */
5
+ export function isColumnGroup(column) {
6
+ return "children" in column;
7
+ }
8
+ /** Inverse of `isColumnGroup` — handy for filters over the flat working set. */
9
+ export function isColumnLeaf(column) {
10
+ return !isColumnGroup(column);
11
+ }
12
+ /**
13
+ * Depth-first flattening of the hierarchical column definitions into the
14
+ * flat array of LEAF columns.
15
+ *
16
+ * This is the working set for virtualization: the Virtualizer and all
17
+ * viewport geometry operate on `ColumnLeafDef<T>` only and never see group
18
+ * nodes. Order is preserved (pre-order traversal), so grouped children stay
19
+ * in place. `hidden` leaves are NOT filtered here — visibility filtering is
20
+ * the caller's concern.
21
+ */
22
+ export function flattenColumns(columns) {
23
+ const result = [];
24
+ for (const column of columns) {
25
+ if (isColumnGroup(column)) {
26
+ if (column.children.length > 0)
27
+ result.push(...flattenColumns(column.children));
28
+ }
29
+ else {
30
+ result.push(column);
31
+ }
32
+ }
33
+ return result;
34
+ }
35
+ /**
36
+ * Projects every group onto the flat VISIBLE leaf layout.
37
+ *
38
+ * For each group (in pre-order) returns its span over the visible leaf
39
+ * indices: `[startIndex, endIndex)` is the range of leaf columns the group
40
+ * header covers on its header row. The indices address the same flat
41
+ * ordering as the `index` on `ViewportData` columns — computed by walking
42
+ * the hierarchy while counting leaves for which `isVisible` returns true.
43
+ *
44
+ * Groups that contain no visible leaves are omitted. `depth` is the nesting
45
+ * level (top-level groups are 0), letting the adapter reconstruct the group
46
+ * tree when rendering stacked header rows.
47
+ */
48
+ export function getColumnGroups(columns, isVisible = () => true) {
49
+ const result = [];
50
+ let leafIndex = 0;
51
+ const walk = (nodes, depth) => {
52
+ for (const node of nodes) {
53
+ if (isColumnGroup(node)) {
54
+ // Emit BEFORE walking children (pre-order): the adapter can
55
+ // reconstruct the group tree with a depth stack because every
56
+ // parent is seen before its nested children.
57
+ const entry = {
58
+ group: node,
59
+ startIndex: leafIndex,
60
+ endIndex: leafIndex,
61
+ depth,
62
+ };
63
+ result.push(entry);
64
+ walk(node.children, depth + 1);
65
+ entry.endIndex = leafIndex;
66
+ // Drop groups that ended up covering no visible leaves.
67
+ if (entry.endIndex <= entry.startIndex)
68
+ result.pop();
69
+ }
70
+ else if (isVisible(node)) {
71
+ leafIndex += 1;
72
+ }
73
+ }
74
+ };
75
+ walk(columns, 0);
76
+ return result;
77
+ }
@@ -0,0 +1,70 @@
1
+ import type { DomPoolBindings, DomPoolUpdate, Range, RowHost, RowId } from "./types";
2
+ export interface DomPoolOptions<T> {
3
+ rowHeight: number;
4
+ getRowId?: (row: T, index: number) => RowId;
5
+ /**
6
+ * Injected DOM operations. The core NEVER touches `document` —
7
+ * the adapter performs all node manipulations through this contract.
8
+ */
9
+ bindings: DomPoolBindings<T>;
10
+ }
11
+ /**
12
+ * Viewport frame for the DomPool.
13
+ *
14
+ * The `rowRange` and `columnRanges` are computed by the Grid's single
15
+ * Virtualizer (see `Grid.getViewportData()`). The DomPool does NOT virtualize
16
+ * on its own — it trusts the range provided by the core.
17
+ */
18
+ export interface DomPoolViewport<T> {
19
+ /** Inclusive start, exclusive end of visible rows (from Virtualizer). */
20
+ rowRange: Range;
21
+ /** Full (post-processing) array of rows. */
22
+ data: T[];
23
+ /** Pixel height of each row. */
24
+ rowHeight: number;
25
+ /** Pixel width of each row (sum of all column widths). */
26
+ rowWidth: number;
27
+ }
28
+ /**
29
+ * Pool of reusable row nodes.
30
+ *
31
+ * The core maintains a fixed-size set of node hosts (`hosts[]`) and maps
32
+ * "pool slot i → row index". On scroll nodes are NOT created or destroyed —
33
+ * a node simply changes `transform: translateY` (position) and, if its row
34
+ * index changed, receives new data via `bindings.bindRow`. Text/content is
35
+ * updated in-place without recreating elements.
36
+ *
37
+ * The row range is provided by the caller (the Grid's single Virtualizer),
38
+ * ensuring there is exactly one source of truth for virtualization.
39
+ */
40
+ export declare class DomPool<T> {
41
+ private readonly getRowId;
42
+ private readonly bindings;
43
+ private readonly hosts;
44
+ private readonly hostRowIndex;
45
+ private readonly hostOffsetY;
46
+ /** Data changed — force-rebind visible nodes on the next frame. */
47
+ private forceRebind;
48
+ private lastData;
49
+ private destroyed;
50
+ constructor(options: DomPoolOptions<T>);
51
+ get size(): number;
52
+ /** Returns current pool nodes (read-only; order matches pool indices). */
53
+ getHosts(): RowHost<T>[];
54
+ /**
55
+ * Processes the next viewport frame. Idempotent: calls bindings
56
+ * ONLY for nodes that have actually changed and returns an info
57
+ * report describing the update.
58
+ */
59
+ update(viewport: DomPoolViewport<T>): DomPoolUpdate<T>;
60
+ /**
61
+ * Force-rebinds all visible nodes on the next frame.
62
+ * Used by the adapter on `refresh()` or data replacement when
63
+ * the visible range hasn't changed but content/styles have.
64
+ */
65
+ invalidate(): void;
66
+ /** Releases all pool nodes (called when the grid is destroyed). */
67
+ destroy(): void;
68
+ isDestroyed(): boolean;
69
+ private assertActive;
70
+ }
@@ -0,0 +1,138 @@
1
+ /** Off-screen Y coordinate for rows removed from the viewport. */
2
+ const OFFSCREEN_Y = -200000;
3
+ /**
4
+ * Pool of reusable row nodes.
5
+ *
6
+ * The core maintains a fixed-size set of node hosts (`hosts[]`) and maps
7
+ * "pool slot i → row index". On scroll nodes are NOT created or destroyed —
8
+ * a node simply changes `transform: translateY` (position) and, if its row
9
+ * index changed, receives new data via `bindings.bindRow`. Text/content is
10
+ * updated in-place without recreating elements.
11
+ *
12
+ * The row range is provided by the caller (the Grid's single Virtualizer),
13
+ * ensuring there is exactly one source of truth for virtualization.
14
+ */
15
+ export class DomPool {
16
+ constructor(options) {
17
+ this.hosts = [];
18
+ this.hostRowIndex = [];
19
+ this.hostOffsetY = [];
20
+ /** Data changed — force-rebind visible nodes on the next frame. */
21
+ this.forceRebind = false;
22
+ this.lastData = null;
23
+ this.destroyed = false;
24
+ this.getRowId = options.getRowId ?? ((_row, index) => index);
25
+ this.bindings = options.bindings;
26
+ }
27
+ get size() {
28
+ return this.hosts.length;
29
+ }
30
+ /** Returns current pool nodes (read-only; order matches pool indices). */
31
+ getHosts() {
32
+ return this.hosts;
33
+ }
34
+ /**
35
+ * Processes the next viewport frame. Idempotent: calls bindings
36
+ * ONLY for nodes that have actually changed and returns an info
37
+ * report describing the update.
38
+ */
39
+ update(viewport) {
40
+ this.assertActive();
41
+ const { data, rowRange, rowHeight, rowWidth } = viewport;
42
+ if (data !== this.lastData) {
43
+ this.lastData = data;
44
+ this.forceRebind = true;
45
+ }
46
+ const range = rowRange;
47
+ const windowSize = range.end - range.start;
48
+ // Pool grows only up to the high-water mark of the window: during
49
+ // normal scrolling the pool size stays stable — nodes are neither
50
+ // created nor destroyed.
51
+ while (this.hosts.length < windowSize) {
52
+ const host = this.bindings.createRow();
53
+ this.hosts.push(host);
54
+ this.hostRowIndex.push(null);
55
+ this.hostOffsetY.push(OFFSCREEN_Y);
56
+ }
57
+ const bound = [];
58
+ const positioned = [];
59
+ const recycled = [];
60
+ for (let slot = 0; slot < windowSize; slot += 1) {
61
+ const host = this.hosts[slot];
62
+ const rowIndex = range.start + slot;
63
+ const offsetY = rowIndex * rowHeight;
64
+ if (this.hostRowIndex[slot] !== rowIndex || this.forceRebind) {
65
+ const dataRow = data[rowIndex];
66
+ const row = {
67
+ index: rowIndex,
68
+ rowId: this.getRowId(dataRow, rowIndex),
69
+ data: dataRow,
70
+ offsetY,
71
+ height: rowHeight,
72
+ width: rowWidth,
73
+ };
74
+ this.hostRowIndex[slot] = rowIndex;
75
+ this.bindings.bindRow(host, row);
76
+ bound.push({ host, row });
77
+ }
78
+ if (this.hostOffsetY[slot] !== offsetY) {
79
+ this.hostOffsetY[slot] = offsetY;
80
+ this.bindings.transformRow(host, offsetY);
81
+ positioned.push({ host, offsetY });
82
+ }
83
+ }
84
+ // Tail nodes that fell outside the current window: clear their content
85
+ // and move them off-screen, but keep the nodes for the next frame.
86
+ for (let slot = windowSize; slot < this.hosts.length; slot += 1) {
87
+ if (this.hostRowIndex[slot] === null)
88
+ continue;
89
+ const host = this.hosts[slot];
90
+ this.hostRowIndex[slot] = null;
91
+ this.hostOffsetY[slot] = OFFSCREEN_Y;
92
+ this.bindings.recycleRow(host);
93
+ this.bindings.transformRow(host, OFFSCREEN_Y);
94
+ recycled.push(host);
95
+ }
96
+ this.forceRebind = false;
97
+ return {
98
+ windowStart: range.start,
99
+ windowEnd: range.end,
100
+ poolSize: this.hosts.length,
101
+ bound,
102
+ positioned,
103
+ recycled,
104
+ };
105
+ }
106
+ /**
107
+ * Force-rebinds all visible nodes on the next frame.
108
+ * Used by the adapter on `refresh()` or data replacement when
109
+ * the visible range hasn't changed but content/styles have.
110
+ */
111
+ invalidate() {
112
+ this.assertActive();
113
+ this.forceRebind = true;
114
+ }
115
+ /** Releases all pool nodes (called when the grid is destroyed). */
116
+ destroy() {
117
+ if (this.destroyed)
118
+ return;
119
+ for (let slot = 0; slot < this.hosts.length; slot += 1) {
120
+ if (this.hostRowIndex[slot] === null)
121
+ continue;
122
+ this.bindings.recycleRow(this.hosts[slot]);
123
+ this.hostRowIndex[slot] = null;
124
+ }
125
+ this.hosts.length = 0;
126
+ this.hostRowIndex.length = 0;
127
+ this.hostOffsetY.length = 0;
128
+ this.lastData = null;
129
+ this.destroyed = true;
130
+ }
131
+ isDestroyed() {
132
+ return this.destroyed;
133
+ }
134
+ assertActive() {
135
+ if (this.destroyed)
136
+ throw new Error("DomPool has been destroyed");
137
+ }
138
+ }
@@ -0,0 +1,6 @@
1
+ export declare class EventBus<Events extends object> {
2
+ private readonly listeners;
3
+ on<EventName extends keyof Events>(event: EventName, listener: (payload: Events[EventName]) => void): () => void;
4
+ emit<EventName extends keyof Events>(event: EventName, payload: Events[EventName]): void;
5
+ clear(): void;
6
+ }
package/dist/events.js ADDED
@@ -0,0 +1,22 @@
1
+ export class EventBus {
2
+ constructor() {
3
+ this.listeners = new Map();
4
+ }
5
+ on(event, listener) {
6
+ let eventListeners = this.listeners.get(event);
7
+ if (!eventListeners) {
8
+ eventListeners = new Set();
9
+ this.listeners.set(event, eventListeners);
10
+ }
11
+ eventListeners.add(listener);
12
+ return () => {
13
+ eventListeners?.delete(listener);
14
+ };
15
+ }
16
+ emit(event, payload) {
17
+ this.listeners.get(event)?.forEach((listener) => listener(payload));
18
+ }
19
+ clear() {
20
+ this.listeners.clear();
21
+ }
22
+ }
package/dist/grid.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { SlotManager } from "./slots";
2
+ import type { DataProcessor, GridApi, GridEvents, GridOptions, GridPlugin, GridState, RowClickEvent, RowHoverEvent, RowId, ScrollPosition, SlotMount, SlotName, ViewportData, ViewportState } from "./types";
3
+ export declare class Grid<T> implements GridApi<T> {
4
+ private readonly store;
5
+ private readonly events;
6
+ private readonly virtualizer;
7
+ private readonly cleanupPlugins;
8
+ private readonly dataProcessors;
9
+ private readonly resolveRowId;
10
+ readonly slots: SlotManager<T>;
11
+ private processedDataCache;
12
+ private revision;
13
+ private destroyed;
14
+ /**
15
+ * Separate ephemeral scroll position that is NOT stored in the Store.
16
+ *
17
+ * Scroll-only updates (scrollTop/scrollLeft) flow through here and never
18
+ * trigger Store notifications, so React (or any adapter subscribed to the
19
+ * Store) does NOT re-render on every scroll frame. Dimension changes
20
+ * (width / height) still go through the Store and DO trigger re-renders.
21
+ */
22
+ private scrollPosition;
23
+ constructor(options: GridOptions<T>);
24
+ getState(): GridState<T>;
25
+ /** Returns the current ephemeral scroll position (not part of Store state). */
26
+ getScrollPosition(): ScrollPosition;
27
+ getViewportData(): ViewportData<T>;
28
+ getProcessedData(): T[];
29
+ getRowId(row: T, index: number): RowId;
30
+ setData(data: T[]): void;
31
+ setViewport(viewport: Partial<ViewportState>): void;
32
+ /**
33
+ * Repaints the current viewport: notifies Store subscribers that
34
+ * visible rows and row classes (rowClassRules) may need rebinding
35
+ * in a single batch. Increments revision so adapters can detect
36
+ * structural changes.
37
+ */
38
+ refresh(): void;
39
+ setColumns(columns: GridState<T>["columns"]): void;
40
+ registerDataProcessor(processor: DataProcessor<T>): () => void;
41
+ headerClick(columnId: string, multiSort?: boolean): void;
42
+ rowClick(row: RowClickEvent<T>): void;
43
+ rowHover(row: RowHoverEvent<T>): void;
44
+ subscribe(listener: () => void): () => void;
45
+ on<EventName extends keyof GridEvents<T>>(event: EventName, listener: (payload: GridEvents<T>[EventName]) => void): () => void;
46
+ registerPlugin(plugin: GridPlugin<T>): () => void;
47
+ getSlotMounts(slot: SlotName): SlotMount<T>[];
48
+ /** Monotonic counter for structural changes (refresh / data / columns). */
49
+ getRevision(): number;
50
+ destroy(): void;
51
+ isDestroyed(): boolean;
52
+ private assertActive;
53
+ }