zubin-grid 0.4.13 → 0.41.13
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 +100 -19
- package/dist/core/cell.d.ts +6 -6
- package/dist/core/cell.d.ts.map +1 -1
- package/dist/core/cell.js +125 -38
- package/dist/core/cell.js.map +1 -1
- package/dist/core/grid-id.d.ts +7 -0
- package/dist/core/grid-id.d.ts.map +1 -0
- package/dist/core/grid-id.js +51 -0
- package/dist/core/grid-id.js.map +1 -0
- package/dist/core/grid.d.ts +10 -9
- package/dist/core/grid.d.ts.map +1 -1
- package/dist/core/grid.js +388 -144
- package/dist/core/grid.js.map +1 -1
- package/dist/core/head.d.ts +9 -11
- package/dist/core/head.d.ts.map +1 -1
- package/dist/core/head.js +38 -33
- package/dist/core/head.js.map +1 -1
- package/dist/core/helpers.d.ts +3 -3
- package/dist/core/helpers.d.ts.map +1 -1
- package/dist/core/helpers.js +5 -2
- package/dist/core/helpers.js.map +1 -1
- package/dist/core/persist.d.ts.map +1 -1
- package/dist/core/persist.js +42 -41
- package/dist/core/persist.js.map +1 -1
- package/dist/core/tail.d.ts +7 -9
- package/dist/core/tail.d.ts.map +1 -1
- package/dist/core/tail.js +17 -19
- package/dist/core/tail.js.map +1 -1
- package/dist/core/types/grid.types.d.ts +69 -27
- package/dist/core/types/grid.types.d.ts.map +1 -1
- package/dist/core/types/head.types.d.ts +7 -6
- package/dist/core/types/head.types.d.ts.map +1 -1
- package/dist/core/types/tail.types.d.ts +3 -2
- package/dist/core/types/tail.types.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/core/gridPersist.d.ts +0 -3
- package/dist/core/gridPersist.d.ts.map +0 -1
- package/dist/core/gridPersist.js +0 -2
- package/dist/core/gridPersist.js.map +0 -1
- package/dist/core/types/gridPersist.types.d.ts +0 -2
- package/dist/core/types/gridPersist.types.d.ts.map +0 -1
- package/dist/core/types/gridPersist.types.js +0 -2
- package/dist/core/types/gridPersist.types.js.map +0 -1
package/README.md
CHANGED
|
@@ -7,7 +7,9 @@ A lightweight grid state manager for React and TypeScript.
|
|
|
7
7
|
## Why use it?
|
|
8
8
|
|
|
9
9
|
- Reactive cell store with subscriptions
|
|
10
|
+
- Flexible row and column ids with `string`, `number`, `boolean`, or `Date`
|
|
10
11
|
- Typed grid API for rows, columns, heads, and tails
|
|
12
|
+
- Row-bound and column-bound dimension grids for axis metadata
|
|
11
13
|
- React hooks for reading and updating state
|
|
12
14
|
- Row and column reordering helpers
|
|
13
15
|
- JSON-friendly initialization with typed row, column, and cell records
|
|
@@ -22,7 +24,7 @@ npm install zubin-grid react
|
|
|
22
24
|
|
|
23
25
|
## Local example app
|
|
24
26
|
|
|
25
|
-
This repository also includes a small Vite + React playground under `examples/` so you can try the JSON-friendly grid API, persistence, and
|
|
27
|
+
This repository also includes a small Vite + React playground under `examples/` so you can try the JSON-friendly grid API, persistence, demo controls, and a Conway's Game of Life reactivity showcase locally.
|
|
26
28
|
|
|
27
29
|
```bash
|
|
28
30
|
npm install
|
|
@@ -119,10 +121,10 @@ const budgetGrid = grid<BudgetSchema>(
|
|
|
119
121
|
)
|
|
120
122
|
|
|
121
123
|
export function BudgetCell(props: {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
row: number
|
|
125
|
+
col: number
|
|
124
126
|
}) {
|
|
125
|
-
const [value, setValue] = useCell(budgetGrid, props.
|
|
127
|
+
const [value, setValue] = useCell(budgetGrid, { row: props.row, col: props.col })
|
|
126
128
|
|
|
127
129
|
return (
|
|
128
130
|
<input
|
|
@@ -134,7 +136,7 @@ export function BudgetCell(props: {
|
|
|
134
136
|
}
|
|
135
137
|
```
|
|
136
138
|
|
|
137
|
-
`useCell(grid,
|
|
139
|
+
`useCell(grid, { row, col })` returns a React-friendly setter for that cell. For row-bound and column-bound dimension grids, pass `{ row }` or `{ col }` respectively. Outside React, mutate store cells with `grid.upsertCell(...)` or `grid.upsertCells(...)` rather than a store-level `setValue(...)` helper.
|
|
138
140
|
|
|
139
141
|
## Working with heads
|
|
140
142
|
|
|
@@ -308,6 +310,35 @@ const bootstrappedSalesGrid = grid<SalesSchema>({}, {
|
|
|
308
310
|
})
|
|
309
311
|
```
|
|
310
312
|
|
|
313
|
+
## Flexible row and column ids
|
|
314
|
+
|
|
315
|
+
`zubin-grid` exports `GridId` as `string | number | boolean | Date`.
|
|
316
|
+
|
|
317
|
+
Each row id must still be unique within the row axis, and each column id must be unique within the column axis. `Date` ids are compared by timestamp rather than object identity, so recreating the same date value still points at the same row or column.
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
const april14 = new Date('2026-04-14T00:00:00.000Z')
|
|
321
|
+
|
|
322
|
+
const availabilityGrid = grid({
|
|
323
|
+
rows: [
|
|
324
|
+
{ id: true, label: 'Available' },
|
|
325
|
+
{ id: false, label: 'Unavailable' },
|
|
326
|
+
],
|
|
327
|
+
columns: [
|
|
328
|
+
{ id: april14, label: 'April 14' },
|
|
329
|
+
],
|
|
330
|
+
cells: [
|
|
331
|
+
{ rowId: true, columnId: april14, value: 'x' },
|
|
332
|
+
],
|
|
333
|
+
}, {
|
|
334
|
+
rowHeaders: ['id', 'rowId'],
|
|
335
|
+
colHeaders: ['id', 'columnId'],
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
availabilityGrid.getValue(true, new Date('2026-04-14T00:00:00.000Z'))
|
|
339
|
+
// 'x'
|
|
340
|
+
```
|
|
341
|
+
|
|
311
342
|
## Non-reactive snapshots and upserts
|
|
312
343
|
|
|
313
344
|
`grid.getState()` returns a plain snapshot of the current rows, columns, and cells without subscribing React to anything.
|
|
@@ -322,6 +353,50 @@ salesGrid.upsertCell({ rowId: 'west', columnId: 'mar', value: 21 })
|
|
|
322
353
|
|
|
323
354
|
`getValue`, `getRowHead`, and `getColumnHead` are also non-reactive getters when you only need a targeted read.
|
|
324
355
|
|
|
356
|
+
## Dimension grids
|
|
357
|
+
|
|
358
|
+
Use `createDimensionGrid` when you want one reactive cell per row or one reactive cell per column instead of a full row/column matrix. This is handy for statuses, visibility flags, notes, or other metadata attached to a single axis.
|
|
359
|
+
|
|
360
|
+
```tsx
|
|
361
|
+
import { createDimensionGrid, useCell } from 'zubin-grid'
|
|
362
|
+
|
|
363
|
+
type ShipmentStatus = 'pending' | 'packed' | 'shipped'
|
|
364
|
+
|
|
365
|
+
const rowStatusGrid = createDimensionGrid<ShipmentStatus>(salesGrid, [], 'rows')
|
|
366
|
+
const columnStatusGrid = createDimensionGrid<ShipmentStatus>(
|
|
367
|
+
salesGrid,
|
|
368
|
+
['pending'],
|
|
369
|
+
'columns',
|
|
370
|
+
{ persist: false },
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
rowStatusGrid.getValue(0)
|
|
374
|
+
rowStatusGrid.setGrid(['packed', 'shipped'], 'update')
|
|
375
|
+
|
|
376
|
+
function RowStatus(props: { row: number }) {
|
|
377
|
+
const [status, setStatus] = useCell(rowStatusGrid, { row: props.row })
|
|
378
|
+
|
|
379
|
+
return (
|
|
380
|
+
<select
|
|
381
|
+
value={status ?? ''}
|
|
382
|
+
onChange={(event) => setStatus(event.target.value as ShipmentStatus)}
|
|
383
|
+
>
|
|
384
|
+
<option value="">Unset</option>
|
|
385
|
+
<option value="pending">Pending</option>
|
|
386
|
+
<option value="packed">Packed</option>
|
|
387
|
+
<option value="shipped">Shipped</option>
|
|
388
|
+
</select>
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
`createDimensionGrid(parentGrid, initialCells, 'rows' | 'columns', options?)` keeps its size and ordering in sync with the chosen parent axis.
|
|
394
|
+
|
|
395
|
+
- Row dimension grids use `useCell(rowDimensionGrid, { row })`.
|
|
396
|
+
- Column dimension grids use `useCell(columnDimensionGrid, { col })`.
|
|
397
|
+
- `setGrid(...)` accepts either a plain array or `{ dimension, cells }`.
|
|
398
|
+
- Persistence inherits from the parent by default, can be disabled with `persist: false`, or overridden with `persist: ['your-key']`.
|
|
399
|
+
|
|
325
400
|
## Sub grids
|
|
326
401
|
|
|
327
402
|
Use `createSubGrid` when you want a second grid layer that reuses the parent grid's row and column dimensions, but stores a different cell shape.
|
|
@@ -334,17 +409,18 @@ type SalesNoteCell = {
|
|
|
334
409
|
dirty?: boolean
|
|
335
410
|
}
|
|
336
411
|
|
|
337
|
-
const salesNotesGrid = createSubGrid<SalesNoteCell>(salesGrid, [],
|
|
412
|
+
const salesNotesGrid = createSubGrid<SalesNoteCell>(salesGrid, [], {
|
|
413
|
+
persist: ['sales-notes'],
|
|
414
|
+
})
|
|
338
415
|
|
|
339
416
|
function SalesNote(props: {
|
|
340
|
-
|
|
341
|
-
|
|
417
|
+
row: number
|
|
418
|
+
col: number
|
|
342
419
|
}) {
|
|
343
|
-
const [noteCell, setNoteCell] = useCell(
|
|
344
|
-
|
|
345
|
-
props.
|
|
346
|
-
|
|
347
|
-
)
|
|
420
|
+
const [noteCell, setNoteCell] = useCell(salesNotesGrid, {
|
|
421
|
+
row: props.row,
|
|
422
|
+
col: props.col,
|
|
423
|
+
})
|
|
348
424
|
|
|
349
425
|
return (
|
|
350
426
|
<input
|
|
@@ -366,15 +442,16 @@ Sub grids behave like normal grids for cell and hook usage, with a few important
|
|
|
366
442
|
- They inherit the parent grid's row ids, column ids, row heads, and column heads.
|
|
367
443
|
- Parent row and column changes stay in sync, including upserts, label changes, order changes, `setGrid(...)`, and `clearGrid()`.
|
|
368
444
|
- `useCell`, `useCellValue`, `useGrid`, `useRowHead`, `useColumnHead`, `useRowTail`, and `useColumnTail` all work with sub grids.
|
|
369
|
-
- Persistence
|
|
445
|
+
- Persistence inherits from the parent when omitted, can be disabled with `persist: false`, or can use a custom key with `persist: ['your-key']`.
|
|
370
446
|
- Row and column mutations called on a sub grid are forwarded to the parent grid.
|
|
371
447
|
- `clearGrid()` on a sub grid clears the sub grid's own cells while keeping the inherited parent dimensions intact.
|
|
372
448
|
|
|
373
|
-
|
|
449
|
+
Pass seeded cells as the second argument and persistence as the optional third argument:
|
|
374
450
|
|
|
375
451
|
```ts
|
|
376
|
-
const salesFlagsGrid = createSubGrid<SalesNoteCell>(salesGrid,
|
|
377
|
-
|
|
452
|
+
const salesFlagsGrid = createSubGrid<SalesNoteCell>(salesGrid, [
|
|
453
|
+
{ rowId: 'north', columnId: 'jan', value: { note: 'Review' } },
|
|
454
|
+
], {
|
|
378
455
|
persist: ['sales-flags'],
|
|
379
456
|
})
|
|
380
457
|
```
|
|
@@ -419,12 +496,15 @@ const persistedWithCustomAdapter = grid<SalesSchema>(initialState, {
|
|
|
419
496
|
- `cell(initialValue)` - creates a reactive cell
|
|
420
497
|
- `grid({ rows, columns, cells }, options)` - creates a grid from JSON-friendly state
|
|
421
498
|
- `grid(() => ({ rows, columns, cells }), options)` - lazily creates typed grid state
|
|
422
|
-
- `
|
|
499
|
+
- `createDimensionGrid(parentGrid, initialCells, dimension, options?)` - creates a row-bound or column-bound grid that stays synced with the parent axis
|
|
500
|
+
- `createSubGrid(parentGrid, initialCells?, options?)` - creates a child grid with its own cells while inheriting the parent grid dimensions
|
|
423
501
|
|
|
424
502
|
### Cell hooks
|
|
425
503
|
|
|
426
504
|
- `useCell(cell)`
|
|
427
|
-
- `useCell(grid,
|
|
505
|
+
- `useCell(grid, { row, col })`
|
|
506
|
+
- `useCell(rowDimensionGrid, { row })`
|
|
507
|
+
- `useCell(columnDimensionGrid, { col })`
|
|
428
508
|
- `useCellValue(...)`
|
|
429
509
|
|
|
430
510
|
### Head hooks
|
|
@@ -465,6 +545,7 @@ Use the root package for most cases:
|
|
|
465
545
|
```ts
|
|
466
546
|
import {
|
|
467
547
|
cell,
|
|
548
|
+
createDimensionGrid,
|
|
468
549
|
createSubGrid,
|
|
469
550
|
grid,
|
|
470
551
|
useCell,
|
package/dist/core/cell.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Cell, CellInitializer } from "./types/cell.types.js";
|
|
2
|
-
import type { Grid,
|
|
3
|
-
import type { GridHead } from "./types/head.types.js";
|
|
2
|
+
import type { DimensionGrid, DimensionGridPosition, Grid, GridCellPosition, GridDimension, GridState } from "./types/grid.types.js";
|
|
3
|
+
import type { GridHead, GridId } from "./types/head.types.js";
|
|
4
4
|
export type { Cell, Subscriber, Updater } from "./types/cell.types.js";
|
|
5
5
|
export declare function cell<TCell>(initialValue: TCell | CellInitializer<TCell>): Cell<TCell>;
|
|
6
6
|
export declare function useCell<TCell>(currentCell: Cell<TCell>): readonly [TCell, (newValue: TCell) => void];
|
|
7
|
-
export declare function useCell<TCell,
|
|
8
|
-
export declare function useCell<TCell, TRowId extends
|
|
7
|
+
export declare function useCell<TCell, TDimension extends GridDimension, TRowId extends GridId, TColumnId extends GridId>(currentGrid: DimensionGrid<TCell, TDimension, TRowId, TColumnId>, position: DimensionGridPosition<TDimension>): readonly [TCell | undefined, (newValue: TCell | undefined) => void];
|
|
8
|
+
export declare function useCell<TCell, TRowId extends GridId, TColumnId extends GridId, TRowHead extends GridHead<TRowId>, TColumnHead extends GridHead<TColumnId>, TStateCell, TState extends GridState<TStateCell, TRowHead, TColumnHead>>(currentGrid: Grid<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>, position: GridCellPosition): readonly [TCell, (newValue: TCell) => void];
|
|
9
9
|
export declare function useCellValue<TCell>(currentCell: Cell<TCell>): TCell;
|
|
10
|
-
export declare function useCellValue<TCell,
|
|
11
|
-
export declare function useCellValue<TCell, TRowId extends
|
|
10
|
+
export declare function useCellValue<TCell, TDimension extends GridDimension, TRowId extends GridId, TColumnId extends GridId>(currentGrid: DimensionGrid<TCell, TDimension, TRowId, TColumnId>, position: DimensionGridPosition<TDimension>): TCell | undefined;
|
|
11
|
+
export declare function useCellValue<TCell, TRowId extends GridId, TColumnId extends GridId, TRowHead extends GridHead<TRowId>, TColumnHead extends GridHead<TColumnId>, TStateCell, TState extends GridState<TStateCell, TRowHead, TColumnHead>>(currentGrid: Grid<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>, position: GridCellPosition): TCell;
|
|
12
12
|
//# sourceMappingURL=cell.d.ts.map
|
package/dist/core/cell.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cell.d.ts","sourceRoot":"","sources":["../../core/cell.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAuB,MAAM,uBAAuB,CAAC;AACxF,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"cell.d.ts","sourceRoot":"","sources":["../../core/cell.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAuB,MAAM,uBAAuB,CAAC;AACxF,OAAO,KAAK,EACV,aAAa,EACb,qBAAqB,EACrB,IAAI,EACJ,gBAAgB,EAChB,aAAa,EACb,SAAS,EAEV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE9D,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAsBvE,wBAAgB,IAAI,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAiCrF;AAED,wBAAgB,OAAO,CAAC,KAAK,EAC3B,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,GACvB,SAAS,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,KAAK,KAAK,IAAI,CAAC,CAAC;AAC/C,wBAAgB,OAAO,CACrB,KAAK,EACL,UAAU,SAAS,aAAa,EAChC,MAAM,SAAS,MAAM,EACrB,SAAS,SAAS,MAAM,EAExB,WAAW,EAAE,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,CAAC,E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,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,CAAC,EACtF,QAAQ,EAAE,gBAAgB,GACzB,KAAK,CAAC"}
|
package/dist/core/cell.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useSyncExternalStore } from "react";
|
|
1
|
+
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
2
2
|
export function cell(initialValue) {
|
|
3
3
|
let value = typeof initialValue === "function" ? null : initialValue;
|
|
4
4
|
const subscribers = new Set();
|
|
@@ -27,49 +27,136 @@ export function cell(initialValue) {
|
|
|
27
27
|
_subscribers: () => subscribers.size,
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
-
export function useCell(cellOrGrid,
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
columnId,
|
|
39
|
-
};
|
|
40
|
-
const currentCell = resolveCell(cellOrGrid, resolvedRowId, resolvedColumnId);
|
|
41
|
-
const subscribe = useCallback((callback) => currentCell.subscribe(callback), [currentCell]);
|
|
42
|
-
const getSnapshot = useCallback(() => currentCell.get(), [currentCell]);
|
|
43
|
-
const setValue = isGrid(cellOrGrid) && resolvedRowId !== undefined && resolvedColumnId !== undefined
|
|
44
|
-
? (newValue) => cellOrGrid.__setCellValue(resolvedRowId, resolvedColumnId, newValue)
|
|
45
|
-
: currentCell.set;
|
|
46
|
-
return [useSyncExternalStore(subscribe, getSnapshot), setValue];
|
|
30
|
+
export function useCell(cellOrGrid, position) {
|
|
31
|
+
const currentCellAccess = useMemo(() => resolveCellAccess(cellOrGrid, position), [cellOrGrid, position]);
|
|
32
|
+
const subscribe = useCallback((callback) => subscribeToResolvedCell(currentCellAccess, callback), [currentCellAccess]);
|
|
33
|
+
const getSnapshot = useCallback(() => currentCellAccess.getCell().get(), [currentCellAccess]);
|
|
34
|
+
return [
|
|
35
|
+
useSyncExternalStore(subscribe, getSnapshot),
|
|
36
|
+
currentCellAccess.setValue,
|
|
37
|
+
];
|
|
47
38
|
}
|
|
48
|
-
export function useCellValue(cellOrGrid,
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
columnId: rowIdOrPosition[0],
|
|
53
|
-
}
|
|
54
|
-
: {
|
|
55
|
-
rowId: rowIdOrPosition,
|
|
56
|
-
columnId,
|
|
57
|
-
};
|
|
58
|
-
const currentCell = resolveCell(cellOrGrid, resolvedRowId, resolvedColumnId);
|
|
59
|
-
const subscribe = useCallback((callback) => currentCell.subscribe(callback), [currentCell]);
|
|
60
|
-
const getSnapshot = useCallback(() => currentCell.get(), [currentCell]);
|
|
39
|
+
export function useCellValue(cellOrGrid, position) {
|
|
40
|
+
const currentCellAccess = useMemo(() => resolveCellAccess(cellOrGrid, position), [cellOrGrid, position]);
|
|
41
|
+
const subscribe = useCallback((callback) => subscribeToResolvedCell(currentCellAccess, callback), [currentCellAccess]);
|
|
42
|
+
const getSnapshot = useCallback(() => currentCellAccess.getCell().get(), [currentCellAccess]);
|
|
61
43
|
return useSyncExternalStore(subscribe, getSnapshot);
|
|
62
44
|
}
|
|
63
|
-
function
|
|
64
|
-
if (
|
|
65
|
-
return cellOrGrid;
|
|
45
|
+
function resolveCellAccess(cellOrGrid, position) {
|
|
46
|
+
if (isDimensionGrid(cellOrGrid)) {
|
|
47
|
+
return createDimensionGridCellAccess(cellOrGrid, position);
|
|
66
48
|
}
|
|
67
|
-
if (
|
|
68
|
-
|
|
49
|
+
if (isGrid(cellOrGrid)) {
|
|
50
|
+
return createGridCellAccess(cellOrGrid, position);
|
|
69
51
|
}
|
|
70
|
-
return
|
|
52
|
+
return {
|
|
53
|
+
getCell: () => cellOrGrid,
|
|
54
|
+
setValue: (newValue) => {
|
|
55
|
+
cellOrGrid.set(newValue);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
71
58
|
}
|
|
72
59
|
function isGrid(value) {
|
|
73
|
-
return "
|
|
60
|
+
return (typeof value === "object" &&
|
|
61
|
+
value !== null &&
|
|
62
|
+
"getCell" in value &&
|
|
63
|
+
"getRowHead" in value &&
|
|
64
|
+
"getColumnHead" in value);
|
|
65
|
+
}
|
|
66
|
+
function isDimensionGrid(value) {
|
|
67
|
+
return (typeof value === "object" &&
|
|
68
|
+
value !== null &&
|
|
69
|
+
"dimension" in value &&
|
|
70
|
+
"size" in value &&
|
|
71
|
+
"subscribeGrid" in value &&
|
|
72
|
+
"getCell" in value &&
|
|
73
|
+
!("getRowHead" in value));
|
|
74
|
+
}
|
|
75
|
+
function createDimensionGridCellAccess(currentGrid, position) {
|
|
76
|
+
const getIndex = () => resolveDimensionGridIndex(currentGrid, position);
|
|
77
|
+
return {
|
|
78
|
+
getCell: () => currentGrid.getCell(getIndex()),
|
|
79
|
+
setValue: (newValue) => {
|
|
80
|
+
currentGrid.__setCellValue(getIndex(), newValue);
|
|
81
|
+
},
|
|
82
|
+
subscribeToPositionChanges: (callback) => currentGrid.subscribeGrid((_updatedGrid, diff) => {
|
|
83
|
+
if (diff.type === "dimension") {
|
|
84
|
+
callback();
|
|
85
|
+
}
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function createIndexedGridCellAccess(currentGrid, position) {
|
|
90
|
+
const getCoordinates = () => resolveGridCoordinatesFromIndex(currentGrid, position);
|
|
91
|
+
return {
|
|
92
|
+
getCell: () => {
|
|
93
|
+
const { rowId, columnId } = getCoordinates();
|
|
94
|
+
return currentGrid.getCell(rowId, columnId);
|
|
95
|
+
},
|
|
96
|
+
setValue: (newValue) => {
|
|
97
|
+
const { rowId, columnId } = getCoordinates();
|
|
98
|
+
currentGrid.__setCellValue(rowId, columnId, newValue);
|
|
99
|
+
},
|
|
100
|
+
subscribeToPositionChanges: (callback) => currentGrid.subscribeGrid((_updatedGrid, diff) => {
|
|
101
|
+
if (diff.type === "grid" ||
|
|
102
|
+
diff.type === "rows" ||
|
|
103
|
+
diff.type === "columns" ||
|
|
104
|
+
diff.type === "row-head" ||
|
|
105
|
+
diff.type === "column-head") {
|
|
106
|
+
callback();
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function createGridCellAccess(currentGrid, position) {
|
|
112
|
+
return createIndexedGridCellAccess(currentGrid, position);
|
|
113
|
+
}
|
|
114
|
+
function subscribeToResolvedCell(currentCellAccess, callback) {
|
|
115
|
+
let currentCell = currentCellAccess.getCell();
|
|
116
|
+
let unsubscribeCell = currentCell.subscribe(callback);
|
|
117
|
+
if (!currentCellAccess.subscribeToPositionChanges) {
|
|
118
|
+
return unsubscribeCell;
|
|
119
|
+
}
|
|
120
|
+
const unsubscribePositionChange = currentCellAccess.subscribeToPositionChanges(() => {
|
|
121
|
+
const nextCell = currentCellAccess.getCell();
|
|
122
|
+
if (nextCell !== currentCell) {
|
|
123
|
+
unsubscribeCell();
|
|
124
|
+
currentCell = nextCell;
|
|
125
|
+
unsubscribeCell = currentCell.subscribe(callback);
|
|
126
|
+
}
|
|
127
|
+
callback();
|
|
128
|
+
});
|
|
129
|
+
return () => {
|
|
130
|
+
unsubscribePositionChange();
|
|
131
|
+
unsubscribeCell();
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function resolveDimensionGridIndex(currentGrid, position) {
|
|
135
|
+
if (currentGrid.dimension === "rows") {
|
|
136
|
+
const row = position.row;
|
|
137
|
+
if (typeof row !== "number" || !Number.isInteger(row)) {
|
|
138
|
+
throw new Error("Row position must be an integer.");
|
|
139
|
+
}
|
|
140
|
+
return row;
|
|
141
|
+
}
|
|
142
|
+
const col = position.col;
|
|
143
|
+
if (typeof col !== "number" || !Number.isInteger(col)) {
|
|
144
|
+
throw new Error("Column position must be an integer.");
|
|
145
|
+
}
|
|
146
|
+
return col;
|
|
147
|
+
}
|
|
148
|
+
function resolveGridCoordinatesFromIndex(currentGrid, position) {
|
|
149
|
+
if (!Number.isInteger(position.row) || !Number.isInteger(position.col)) {
|
|
150
|
+
throw new Error("Grid cell position indices must be integers.");
|
|
151
|
+
}
|
|
152
|
+
const rowId = currentGrid.rowHeaders[position.row];
|
|
153
|
+
const columnId = currentGrid.colHeaders[position.col];
|
|
154
|
+
if (rowId === undefined || columnId === undefined) {
|
|
155
|
+
throw new Error(`Grid cell position { row: ${position.row}, col: ${position.col} } is out of bounds.`);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
rowId,
|
|
159
|
+
columnId,
|
|
160
|
+
};
|
|
74
161
|
}
|
|
75
162
|
//# sourceMappingURL=cell.js.map
|
package/dist/core/cell.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cell.js","sourceRoot":"","sources":["../../core/cell.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"cell.js","sourceRoot":"","sources":["../../core/cell.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAoCnE,MAAM,UAAU,IAAI,CAAQ,YAA4C;IACtE,IAAI,KAAK,GAAG,OAAO,YAAY,KAAK,UAAU,CAAC,CAAC,CAAE,IAAc,CAAC,CAAC,CAAC,YAAY,CAAC;IAEhF,MAAM,WAAW,GAAG,IAAI,GAAG,EAAc,CAAC;IAE1C,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;IAChD,CAAC,CAAC;IAEF,MAAM,eAAe,GAAG,KAAK,IAAI,EAAE;QACjC,IAAI,OAAO,YAAY,KAAK,UAAU;YAAE,OAAO;QAE/C,KAAK,GAAG,MAAO,YAAuC,EAAE,CAAC;QACzD,iBAAiB,EAAE,CAAC;IACtB,CAAC,C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|
+
import type { GridId } from "./types/head.types.js";
|
|
2
|
+
export declare function isGridId(value: unknown): value is GridId;
|
|
3
|
+
export declare function assertGridId(value: unknown, context: string): asserts value is GridId;
|
|
4
|
+
export declare function createGridIdKey(id: GridId): string;
|
|
5
|
+
export declare function areEqualGridIds(left: GridId, right: GridId): boolean;
|
|
6
|
+
export declare function formatGridId(id: GridId): string;
|
|
7
|
+
//# sourceMappingURL=grid-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grid-id.d.ts","sourceRoot":"","sources":["../../core/grid-id.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAIpD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAOxD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAQrF;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,UAkBzC;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,WAE1D;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,UAYtC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const GRID_ID_DESCRIPTION = "string, number, boolean, or Date";
|
|
2
|
+
export function isGridId(value) {
|
|
3
|
+
return (typeof value === "string" ||
|
|
4
|
+
typeof value === "number" ||
|
|
5
|
+
typeof value === "boolean" ||
|
|
6
|
+
value instanceof Date);
|
|
7
|
+
}
|
|
8
|
+
export function assertGridId(value, context) {
|
|
9
|
+
if (!isGridId(value)) {
|
|
10
|
+
throw new Error(`${context} must be a ${GRID_ID_DESCRIPTION} id.`);
|
|
11
|
+
}
|
|
12
|
+
if (value instanceof Date) {
|
|
13
|
+
readGridDateValue(value, context);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function createGridIdKey(id) {
|
|
17
|
+
if (typeof id === "string") {
|
|
18
|
+
return `string:${encodeURIComponent(id)}`;
|
|
19
|
+
}
|
|
20
|
+
if (typeof id === "number") {
|
|
21
|
+
if (Number.isNaN(id)) {
|
|
22
|
+
return "number:NaN";
|
|
23
|
+
}
|
|
24
|
+
return Object.is(id, -0) ? "number:-0" : `number:${id}`;
|
|
25
|
+
}
|
|
26
|
+
if (typeof id === "boolean") {
|
|
27
|
+
return `boolean:${id}`;
|
|
28
|
+
}
|
|
29
|
+
return `date:${readGridDateValue(id, "Grid id")}`;
|
|
30
|
+
}
|
|
31
|
+
export function areEqualGridIds(left, right) {
|
|
32
|
+
return createGridIdKey(left) === createGridIdKey(right);
|
|
33
|
+
}
|
|
34
|
+
export function formatGridId(id) {
|
|
35
|
+
if (typeof id === "string") {
|
|
36
|
+
return id;
|
|
37
|
+
}
|
|
38
|
+
if (typeof id === "number" || typeof id === "boolean") {
|
|
39
|
+
return String(id);
|
|
40
|
+
}
|
|
41
|
+
readGridDateValue(id, "Grid id");
|
|
42
|
+
return id.toISOString();
|
|
43
|
+
}
|
|
44
|
+
function readGridDateValue(id, context) {
|
|
45
|
+
const time = id.getTime();
|
|
46
|
+
if (!Number.isNaN(time)) {
|
|
47
|
+
return time;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`${context} must be a valid Date id.`);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=grid-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grid-id.js","sourceRoot":"","sources":["../../core/grid-id.ts"],"names":[],"mappings":"AAEA,MAAM,mBAAmB,GAAG,kCAAkC,CAAC;AAE/D,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,SAAS;QAC1B,KAAK,YAAY,IAAI,CACtB,CAAC;AACJ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OAAO,EAAE,KAAK,SAAS,EAAE,CAAC;QACtD,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC;IACpB,CAAC;IAED,iBAAiB,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IAEjC,OAAO,EAAE,CAAC,WAAW,EAAE,CAAC;AAC1B,CAAC;AAED,SAAS,iBAAiB,CAAC,EAAQ,EAAE,OAAe;IAClD,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;IAE1B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,GAAG,OAAO,2BAA2B,CAAC,CAAC;AACzD,CAAC"}
|
package/dist/core/grid.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type { AnyGrid, BroadSchemaColumnHead, BroadSchemaRowHead, BroadSchemaSnapshot, Grid, GridAxisIds, GridColumnIdOf,
|
|
2
|
-
import type { GridHead } from "./types/head.types.js";
|
|
3
|
-
export type { CreateSubGridOptions, Grid, GridAxisIds, GridPersistAdapter, GridPersistOption,
|
|
4
|
-
export declare function grid<TState extends GridState<GridRecord, GridRecord, GridRecord>>(source: GridStateInitializer<TState>, options: GridSchemaOptions<TState, keyof SchemaRow<TState> & string, keyof SchemaColumn<TState> & string, keyof SchemaCell<TState> & string, keyof SchemaCell<TState> & string, BroadSchemaSnapshot<TState>>): Grid<SchemaCell<TState>, string, string, BroadSchemaRowHead<TState>, BroadSchemaColumnHead<TState>, SchemaCell<TState>, BroadSchemaSnapshot<TState>>;
|
|
1
|
+
import type { AnyGrid, BroadSchemaColumnHead, BroadSchemaRowHead, BroadSchemaSnapshot, Grid, GridAxisIds, GridColumnIdOf, GridDimension, GridRecord, GridRowIdOf, GridSchemaOptions, GridState, GridStateCell, ParentDimensionGrid, ParentDimensionGridOptions, ParentSubGrid, ParentSubGridOptions, GridStateInitializer, SchemaCell, SchemaCellValue, SchemaColumn, SchemaColumnHead, SchemaColumnId, SchemaRow, SchemaRowHead, SchemaRowId, SchemaSnapshot, UseGridOptions } from "./types/grid.types.js";
|
|
2
|
+
import type { GridHead, GridId } from "./types/head.types.js";
|
|
3
|
+
export type { CreateSubGridOptions, DimensionGrid, DimensionGridOptions, DimensionGridPosition, DimensionGridState, DimensionGridSubscriber, DimensionGridUpdateDiff, Grid, GridAxisIds, GridCellPosition, GridDimension, GridPersistAdapter, GridPersistOption, ParentDimensionGrid, ParentDimensionGridOptions, GridSchemaOptions, GridSetMode, GridState, GridStateCell, GridStateInitializer, GridSubscriber, GridUpdateAction, GridUpdateDiff, GridUpdateSource, GridUpdateType, GridUpsertHead, SubGrid, SubGridState, UseGridOptions, } from "./types/grid.types.js";
|
|
5
4
|
export declare function grid<TState extends GridState<GridRecord, GridRecord, GridRecord>, TRowHeadIdKey extends keyof SchemaRow<TState> & string, TColumnHeadIdKey extends keyof SchemaColumn<TState> & string, TRowCellKey extends keyof SchemaCell<TState> & string, TColumnCellKey extends keyof SchemaCell<TState> & string>(source: GridStateInitializer<TState>, options: GridSchemaOptions<TState, TRowHeadIdKey, TColumnHeadIdKey, TRowCellKey, TColumnCellKey, SchemaSnapshot<TState, TRowHeadIdKey, TColumnHeadIdKey, TRowCellKey, TColumnCellKey>>): Grid<SchemaCellValue<TState, TRowHeadIdKey, TColumnHeadIdKey, TRowCellKey, TColumnCellKey>, SchemaRowId<TState, TRowHeadIdKey>, SchemaColumnId<TState, TColumnHeadIdKey>, SchemaRowHead<TState, TRowHeadIdKey>, SchemaColumnHead<TState, TColumnHeadIdKey>, SchemaCellValue<TState, TRowHeadIdKey, TColumnHeadIdKey, TRowCellKey, TColumnCellKey>, SchemaSnapshot<TState, TRowHeadIdKey, TColumnHeadIdKey, TRowCellKey, TColumnCellKey>>;
|
|
5
|
+
export declare function grid<TState extends GridState<GridRecord, GridRecord, GridRecord>>(source: GridStateInitializer<TState>, options: GridSchemaOptions<TState, keyof SchemaRow<TState> & string, keyof SchemaColumn<TState> & string, keyof SchemaCell<TState> & string, keyof SchemaCell<TState> & string, BroadSchemaSnapshot<TState>>): Grid<SchemaCell<TState>, GridId, GridId, BroadSchemaRowHead<TState>, BroadSchemaColumnHead<TState>, SchemaCell<TState>, BroadSchemaSnapshot<TState>>;
|
|
6
|
+
export declare function createDimensionGrid<TCell, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid, initialCells: readonly (TCell | undefined)[], dimension: "rows", options?: ParentDimensionGridOptions<TCell, "rows", TParentGrid>): ParentDimensionGrid<TCell, "rows", TParentGrid>;
|
|
7
|
+
export declare function createDimensionGrid<TCell, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid, initialCells: readonly (TCell | undefined)[], dimension: "columns", options?: ParentDimensionGridOptions<TCell, "columns", TParentGrid>): ParentDimensionGrid<TCell, "columns", TParentGrid>;
|
|
8
|
+
export declare function createDimensionGrid<TCell, TDimension extends GridDimension = GridDimension, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid, initialCells: readonly (TCell | undefined)[], dimension: TDimension, options?: ParentDimensionGridOptions<TCell, TDimension, TParentGrid>): ParentDimensionGrid<TCell, TDimension, TParentGrid>;
|
|
6
9
|
export declare function createSubGrid<TCell, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid): ParentSubGrid<TCell, TParentGrid>;
|
|
7
|
-
export declare function createSubGrid<TCell, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid, initialCells: readonly GridStateCell<TCell, GridRowIdOf<TParentGrid>, GridColumnIdOf<TParentGrid>>[],
|
|
8
|
-
export declare function
|
|
9
|
-
export declare function
|
|
10
|
-
export declare function createGridKey(rowId: string, columnId: string): string;
|
|
11
|
-
export declare function useGrid<TCell, TRowId extends string, TColumnId extends string, TRowHead extends GridHead<TRowId>, TColumnHead extends GridHead<TColumnId>, TStateCell, TState extends GridState<TStateCell, TRowHead, TColumnHead>>(currentGrid: Grid<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>, options?: UseGridOptions<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>): GridAxisIds<TRowId, TColumnId>;
|
|
10
|
+
export declare function createSubGrid<TCell, TParentGrid extends AnyGrid = AnyGrid>(parentGrid: TParentGrid, initialCells: readonly GridStateCell<TCell, GridRowIdOf<TParentGrid>, GridColumnIdOf<TParentGrid>>[], options?: ParentSubGridOptions<TCell, TParentGrid>): ParentSubGrid<TCell, TParentGrid>;
|
|
11
|
+
export declare function createGridKey(rowId: GridId, columnId: GridId): string;
|
|
12
|
+
export declare function useGrid<TCell, TRowId extends GridId, TColumnId extends GridId, TRowHead extends GridHead<TRowId>, TColumnHead extends GridHead<TColumnId>, TStateCell, TState extends GridState<TStateCell, TRowHead, TColumnHead>>(currentGrid: Grid<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>, options?: UseGridOptions<TCell, TRowId, TColumnId, TRowHead, TColumnHead, TStateCell, TState>): GridAxisIds<TRowId, TColumnId>;
|
|
12
13
|
//# sourceMappingURL=grid.d.ts.map
|
package/dist/core/grid.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grid.d.ts","sourceRoot":"","sources":["../../core/grid.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"grid.d.ts","sourceRoot":"","sources":["../../core/grid.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,OAAO,EACP,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EAMnB,IAAI,EACJ,WAAW,EAEX,cAAc,EACd,aAAa,EAIb,UAAU,EACV,WAAW,EACX,iBAAiB,EAEjB,SAAS,EAET,aAAa,EAEb,mBAAmB,EACnB,0BAA0B,EAC1B,aAAa,EACb,oBAAoB,EAEpB,oBAAoB,EAIpB,UAAU,EACV,eAAe,EACf,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,aAAa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