@duskmoon-dev/data-grid-core 0.0.0-stage → 2.5.1
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 +13 -2
- package/dist/bridge.d.ts +51 -0
- package/dist/bridge.js +169 -0
- package/dist/browser.d.ts +26 -0
- package/dist/browser.js +180 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/model.d.ts +22 -0
- package/dist/model.js +202 -0
- package/dist/protocol.d.ts +18 -0
- package/dist/protocol.js +156 -0
- package/dist/request.d.ts +8 -0
- package/dist/request.js +41 -0
- package/dist/state.d.ts +4 -0
- package/dist/state.js +183 -0
- package/dist/types.d.ts +221 -0
- package/dist/types.js +1 -0
- package/dist/values.d.ts +8 -0
- package/dist/values.js +124 -0
- package/package.json +41 -4
package/README.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Duskmoon DataGrid core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@duskmoon-dev/data-grid-core` adapts the Duskmoon DataGrid protocol to TanStack Table. The root module can be imported on the server. Use immutable inputs and keep unchanged row, column, and state slice references stable.
|
|
4
|
+
|
|
5
|
+
- `createGridDefaults`, `reduceGridState` and `gridEvents` define user actions and state changes.
|
|
6
|
+
- `createGridTableOptions` supplies options for the React adapter's single table instance. `createGridModel` is the imperative equivalent.
|
|
7
|
+
- `createViewSnapshot` and `readViewSnapshot` handle scoped appearance and column preferences.
|
|
8
|
+
- `createRequestGate` tracks the latest request and accepts increasing revisions before replacing an application's server page.
|
|
9
|
+
- The `/browser` entry provides a mounted `createVirtualWindow` implementation based on TanStack Virtual. Call `snapshot()` for the initial layout and `destroy()` when releasing the scroll element. Provide geometry in CSS pixels. It keeps a layout anchor during changes to row height, scroll margin, or effective CSS zoom. Pass `resetScroll: true` when applying a new query. For a sticky header pass the content offset as `scrollMargin` and the sticky obstruction as `scrollPaddingStart`. A focused row, if supplied, adds a single sparse item to the visible window.
|
|
10
|
+
- The `/bridge` entry projects portable row IDs and field values to the same model. The caller consumes `snapshot()` once after initialization, then subscribes through the constructor callback. Updates with increasing revisions carry data and controlled state in batches. Scroll notifications contain only current renderable rows and column layout. The host retains the original records and mounts its own DOM.
|
|
11
|
+
|
|
12
|
+
The engine uses TanStack Table v8.21.3 with the matching React adapter and TanStack Virtual Core v3.19.13. The React Virtual 3.14.28 package resolves to that Virtual Core version. Virtual measurement and scroll observation start after a browser adapter mounts a scroll element.
|
|
13
|
+
|
|
14
|
+
See [the repository specification](https://github.com/duskmoon-dev/duskmoon-web/tree/main/specs/data-grid/v1) for the v1 rules and shared fixture.
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type VirtualWindowOptions } from "./browser.js";
|
|
2
|
+
import { type ColumnSpec, type GridAction, type GridEvent, type GridModelInput, type GridState, type Pin, type Scalar } from "./types.js";
|
|
3
|
+
export { GRID_PROTOCOL_VERSION } from "./types.js";
|
|
4
|
+
export interface ProjectedRow {
|
|
5
|
+
id: string;
|
|
6
|
+
values: Record<string, Scalar>;
|
|
7
|
+
}
|
|
8
|
+
type StateSlice = keyof GridState;
|
|
9
|
+
export type BridgeVirtualOptions = Omit<VirtualWindowOptions, "rowIds">;
|
|
10
|
+
export type GridBridgeInput = Omit<GridModelInput<ProjectedRow>, "getRowId"> & {
|
|
11
|
+
columns: readonly ColumnSpec[];
|
|
12
|
+
revision: number;
|
|
13
|
+
controlled?: readonly StateSlice[];
|
|
14
|
+
defaults?: GridState;
|
|
15
|
+
};
|
|
16
|
+
export interface GridBridgeSnapshot {
|
|
17
|
+
state: GridState;
|
|
18
|
+
rows: {
|
|
19
|
+
id: string;
|
|
20
|
+
index: number;
|
|
21
|
+
start?: number;
|
|
22
|
+
size?: number;
|
|
23
|
+
}[];
|
|
24
|
+
columns: {
|
|
25
|
+
id: string;
|
|
26
|
+
width: number;
|
|
27
|
+
pin: Pin;
|
|
28
|
+
offset: number;
|
|
29
|
+
}[];
|
|
30
|
+
totalSize?: number;
|
|
31
|
+
rowCount?: number;
|
|
32
|
+
loadedRowCount: number;
|
|
33
|
+
pageCount?: number;
|
|
34
|
+
hasPreviousPage: boolean;
|
|
35
|
+
hasNextPage: boolean;
|
|
36
|
+
pageAllSelected: boolean;
|
|
37
|
+
/** At least one, but not every, row on the loaded page is selected. */
|
|
38
|
+
pageSomeSelected: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function createGridBridge(initial: GridBridgeInput, onSnapshot: (snapshot: GridBridgeSnapshot) => void, onEvent?: (event: GridEvent) => void): {
|
|
41
|
+
snapshot: () => GridBridgeSnapshot;
|
|
42
|
+
update(patch: Partial<GridBridgeInput> & Pick<GridBridgeInput, "revision">): boolean;
|
|
43
|
+
dispatch: (action: GridAction) => void;
|
|
44
|
+
selectPage(selected: boolean): void;
|
|
45
|
+
rowAction(rowId: string, action: string): void;
|
|
46
|
+
mount(element: HTMLElement, options: BridgeVirtualOptions): void;
|
|
47
|
+
measure(options: BridgeVirtualOptions): void;
|
|
48
|
+
scrollToRow(rowId: string): boolean;
|
|
49
|
+
unmount(): void;
|
|
50
|
+
destroy(): void;
|
|
51
|
+
};
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { createVirtualWindow } from "./browser.js";
|
|
2
|
+
import { createGridModel } from "./model.js";
|
|
3
|
+
import { createGridDefaults, normalizeView, queryKey } from "./protocol.js";
|
|
4
|
+
import { gridEvents, reduceGridState } from "./state.js";
|
|
5
|
+
export { GRID_PROTOCOL_VERSION } from "./types.js";
|
|
6
|
+
const getRowId = (row) => row.id;
|
|
7
|
+
const bridgeColumns = new WeakMap();
|
|
8
|
+
function toModelInput(input) {
|
|
9
|
+
let columns = bridgeColumns.get(input.columns);
|
|
10
|
+
if (!columns) {
|
|
11
|
+
columns = input.columns.map((column) => ({
|
|
12
|
+
...column,
|
|
13
|
+
accessor: (row) => Object.hasOwn(row.values, column.field) ? row.values[column.field] : undefined,
|
|
14
|
+
}));
|
|
15
|
+
bridgeColumns.set(input.columns, columns);
|
|
16
|
+
}
|
|
17
|
+
return { ...input, columns, getRowId };
|
|
18
|
+
}
|
|
19
|
+
export function createGridBridge(initial, onSnapshot, onEvent) {
|
|
20
|
+
if (!Number.isSafeInteger(initial.revision) || initial.revision < 0) {
|
|
21
|
+
throw new TypeError("Invalid DataGrid revision");
|
|
22
|
+
}
|
|
23
|
+
let destroyed = false;
|
|
24
|
+
let input = initial;
|
|
25
|
+
let defaults = input.defaults ?? createGridDefaults(input.columns);
|
|
26
|
+
const model = createGridModel(toModelInput(input));
|
|
27
|
+
let modelSnapshot = model.snapshot();
|
|
28
|
+
let rowIds = modelSnapshot.rows.map((row) => row.id);
|
|
29
|
+
let pageSelection = selectedOnPage();
|
|
30
|
+
function selectedOnPage() {
|
|
31
|
+
const selected = new Set(input.state.selection.rowIds);
|
|
32
|
+
const any = selected.size > 0 && rowIds.some((id) => selected.has(id));
|
|
33
|
+
const all = any && rowIds.length <= selected.size && rowIds.every((id) => selected.has(id));
|
|
34
|
+
return { pageAllSelected: all, pageSomeSelected: any && !all };
|
|
35
|
+
}
|
|
36
|
+
let viewport;
|
|
37
|
+
let virtualOptions;
|
|
38
|
+
let published;
|
|
39
|
+
function snapshot() {
|
|
40
|
+
const layout = viewport?.snapshot();
|
|
41
|
+
return {
|
|
42
|
+
state: input.state,
|
|
43
|
+
rows: layout ? layout.items.map(({ key, index, start, size }) => ({ id: key, index, start, size })) :
|
|
44
|
+
rowIds.map((id, index) => ({ id, index })),
|
|
45
|
+
columns: modelSnapshot.columns.map((column) => {
|
|
46
|
+
const pin = column.getIsPinned() || null;
|
|
47
|
+
return {
|
|
48
|
+
id: column.id,
|
|
49
|
+
width: column.getSize(),
|
|
50
|
+
pin,
|
|
51
|
+
offset: pin === "left" ? column.getStart("left") : pin === "right" ? column.getAfter("right") : 0,
|
|
52
|
+
};
|
|
53
|
+
}),
|
|
54
|
+
totalSize: layout?.totalSize,
|
|
55
|
+
rowCount: modelSnapshot.rowCount,
|
|
56
|
+
loadedRowCount: rowIds.length,
|
|
57
|
+
pageCount: modelSnapshot.pageCount,
|
|
58
|
+
hasPreviousPage: modelSnapshot.hasPreviousPage,
|
|
59
|
+
hasNextPage: modelSnapshot.hasNextPage,
|
|
60
|
+
...pageSelection,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function publish() {
|
|
64
|
+
if (destroyed)
|
|
65
|
+
return;
|
|
66
|
+
const next = snapshot();
|
|
67
|
+
if (published && next.state === published.state && next.totalSize === published.totalSize &&
|
|
68
|
+
next.rowCount === published.rowCount && next.loadedRowCount === published.loadedRowCount && next.pageCount === published.pageCount &&
|
|
69
|
+
next.hasPreviousPage === published.hasPreviousPage && next.hasNextPage === published.hasNextPage &&
|
|
70
|
+
next.pageAllSelected === published.pageAllSelected && next.pageSomeSelected === published.pageSomeSelected &&
|
|
71
|
+
next.rows.length === published.rows.length && next.columns.length === published.columns.length &&
|
|
72
|
+
next.rows.every((row, i) => row.id === published.rows[i].id && row.index === published.rows[i].index &&
|
|
73
|
+
row.start === published.rows[i].start && row.size === published.rows[i].size) &&
|
|
74
|
+
next.columns.every((column, i) => column.id === published.columns[i].id && column.width === published.columns[i].width &&
|
|
75
|
+
column.offset === published.columns[i].offset && column.pin === published.columns[i].pin))
|
|
76
|
+
return;
|
|
77
|
+
published = next;
|
|
78
|
+
onSnapshot(next);
|
|
79
|
+
}
|
|
80
|
+
function commit(next) {
|
|
81
|
+
const resetScroll = next.state.query !== input.state.query && queryKey(next.state.query) !== queryKey(input.state.query);
|
|
82
|
+
model.update(toModelInput(next));
|
|
83
|
+
const previousRows = modelSnapshot.rows;
|
|
84
|
+
modelSnapshot = model.snapshot();
|
|
85
|
+
const selectionChanged = next.state.selection !== input.state.selection;
|
|
86
|
+
input = next;
|
|
87
|
+
if (previousRows !== modelSnapshot.rows)
|
|
88
|
+
rowIds = modelSnapshot.rows.map((row) => row.id);
|
|
89
|
+
if (previousRows !== modelSnapshot.rows || selectionChanged)
|
|
90
|
+
pageSelection = selectedOnPage();
|
|
91
|
+
if (viewport && virtualOptions)
|
|
92
|
+
viewport.update({ ...virtualOptions, rowIds }, { resetScroll });
|
|
93
|
+
publish();
|
|
94
|
+
}
|
|
95
|
+
function dispatch(action) {
|
|
96
|
+
if (destroyed)
|
|
97
|
+
return;
|
|
98
|
+
const previous = input.state;
|
|
99
|
+
const proposed = reduceGridState(previous, action, input.columns, defaults);
|
|
100
|
+
if (previous === proposed)
|
|
101
|
+
return;
|
|
102
|
+
const controlled = input.controlled ?? [];
|
|
103
|
+
const state = {
|
|
104
|
+
query: controlled.includes("query") ? previous.query : proposed.query,
|
|
105
|
+
view: controlled.includes("view") ? previous.view : proposed.view,
|
|
106
|
+
selection: controlled.includes("selection") ? previous.selection : proposed.selection,
|
|
107
|
+
};
|
|
108
|
+
if (state.query !== previous.query || state.view !== previous.view || state.selection !== previous.selection) {
|
|
109
|
+
commit({ ...input, state });
|
|
110
|
+
}
|
|
111
|
+
for (const event of gridEvents(previous, proposed, action)) {
|
|
112
|
+
if (!destroyed)
|
|
113
|
+
onEvent?.(event);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
snapshot,
|
|
118
|
+
update(patch) {
|
|
119
|
+
if (destroyed || !Number.isSafeInteger(patch.revision) || patch.revision <= input.revision)
|
|
120
|
+
return false;
|
|
121
|
+
const next = { ...input, ...patch };
|
|
122
|
+
if (next.columns !== input.columns)
|
|
123
|
+
next.state = { ...next.state, view: normalizeView(next.state.view, next.columns) };
|
|
124
|
+
commit(next);
|
|
125
|
+
if (patch.defaults)
|
|
126
|
+
defaults = patch.defaults;
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
dispatch,
|
|
130
|
+
selectPage(selected) {
|
|
131
|
+
dispatch({ type: "select", rowIds, selected });
|
|
132
|
+
},
|
|
133
|
+
rowAction(rowId, action) {
|
|
134
|
+
if (!destroyed && model.table.getCoreRowModel().rowsById[rowId])
|
|
135
|
+
onEvent?.({ type: "row", rowId, action });
|
|
136
|
+
},
|
|
137
|
+
mount(element, options) {
|
|
138
|
+
if (destroyed)
|
|
139
|
+
return;
|
|
140
|
+
viewport?.destroy();
|
|
141
|
+
virtualOptions = options;
|
|
142
|
+
viewport = createVirtualWindow(element, { ...options, rowIds }, publish);
|
|
143
|
+
publish();
|
|
144
|
+
},
|
|
145
|
+
measure(options) {
|
|
146
|
+
if (destroyed)
|
|
147
|
+
return;
|
|
148
|
+
virtualOptions = options;
|
|
149
|
+
viewport?.update({ ...options, rowIds });
|
|
150
|
+
},
|
|
151
|
+
scrollToRow(rowId) {
|
|
152
|
+
return viewport?.scrollToRow(rowId) ?? false;
|
|
153
|
+
},
|
|
154
|
+
unmount() {
|
|
155
|
+
if (destroyed || !viewport)
|
|
156
|
+
return;
|
|
157
|
+
viewport.destroy();
|
|
158
|
+
viewport = undefined;
|
|
159
|
+
publish();
|
|
160
|
+
},
|
|
161
|
+
destroy() {
|
|
162
|
+
if (destroyed)
|
|
163
|
+
return;
|
|
164
|
+
destroyed = true;
|
|
165
|
+
viewport?.destroy();
|
|
166
|
+
viewport = undefined;
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface VirtualWindowOptions {
|
|
2
|
+
rowIds: readonly string[];
|
|
3
|
+
rowHeight: number;
|
|
4
|
+
overscan?: number;
|
|
5
|
+
scrollMargin?: number;
|
|
6
|
+
scrollPaddingStart?: number;
|
|
7
|
+
focusedRowId?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface VirtualWindowSnapshot {
|
|
10
|
+
items: {
|
|
11
|
+
index: number;
|
|
12
|
+
key: string;
|
|
13
|
+
start: number;
|
|
14
|
+
size: number;
|
|
15
|
+
}[];
|
|
16
|
+
totalSize: number;
|
|
17
|
+
}
|
|
18
|
+
/** The viewport and fixed-height row geometry are owned by the caller. */
|
|
19
|
+
export declare function createVirtualWindow(element: HTMLElement, initial: VirtualWindowOptions, onChange: (snapshot: VirtualWindowSnapshot) => void): {
|
|
20
|
+
snapshot: () => VirtualWindowSnapshot;
|
|
21
|
+
update(next: VirtualWindowOptions, behavior?: {
|
|
22
|
+
resetScroll?: boolean;
|
|
23
|
+
}): void;
|
|
24
|
+
scrollToRow(rowId: string): boolean;
|
|
25
|
+
destroy(): void;
|
|
26
|
+
};
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { defaultRangeExtractor, elementScroll, observeElementOffset, observeElementRect, Virtualizer, } from "@tanstack/virtual-core";
|
|
2
|
+
function validateOptions(options) {
|
|
3
|
+
if (!Number.isFinite(options.rowHeight) || options.rowHeight <= 0) {
|
|
4
|
+
throw new TypeError("Virtual row height must be a finite positive number");
|
|
5
|
+
}
|
|
6
|
+
for (const distance of [options.scrollMargin ?? 0, options.scrollPaddingStart ?? 0]) {
|
|
7
|
+
if (!Number.isFinite(distance) || distance < 0)
|
|
8
|
+
throw new TypeError("Invalid virtual scroll offset");
|
|
9
|
+
}
|
|
10
|
+
const overscan = options.overscan ?? 5;
|
|
11
|
+
if (!Number.isSafeInteger(overscan) || overscan < 0)
|
|
12
|
+
throw new TypeError("Invalid virtual overscan");
|
|
13
|
+
}
|
|
14
|
+
/** The viewport and fixed-height row geometry are owned by the caller. */
|
|
15
|
+
export function createVirtualWindow(element, initial, onChange) {
|
|
16
|
+
validateOptions(initial);
|
|
17
|
+
let options = initial;
|
|
18
|
+
let destroyed = false;
|
|
19
|
+
let updating = true;
|
|
20
|
+
let frame;
|
|
21
|
+
let pendingOffset;
|
|
22
|
+
const host = element.ownerDocument.defaultView;
|
|
23
|
+
const readZoom = () => element.currentCSSZoom ?? 1;
|
|
24
|
+
let zoom = readZoom();
|
|
25
|
+
let stableScrollOffset = element.scrollTop;
|
|
26
|
+
const mountOptions = {
|
|
27
|
+
getScrollElement: () => element,
|
|
28
|
+
scrollToFn: elementScroll,
|
|
29
|
+
observeElementRect,
|
|
30
|
+
observeElementOffset: (instance, callback) => observeElementOffset(instance, (offset, scrolling) => {
|
|
31
|
+
// CSS zoom may update the DOM scroll position before a new layout is measured.
|
|
32
|
+
if (pendingOffset !== undefined || readZoom() !== zoom) {
|
|
33
|
+
callback(pendingOffset ?? stableScrollOffset, scrolling);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
stableScrollOffset = offset;
|
|
37
|
+
callback(offset, scrolling);
|
|
38
|
+
}
|
|
39
|
+
}),
|
|
40
|
+
onChange: () => {
|
|
41
|
+
if (!destroyed && !updating)
|
|
42
|
+
publish();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
let cachedOptions;
|
|
46
|
+
let cachedIds;
|
|
47
|
+
let cachedHeight;
|
|
48
|
+
let cachedFocus;
|
|
49
|
+
function virtualOptions() {
|
|
50
|
+
const { rowIds, rowHeight, focusedRowId } = options;
|
|
51
|
+
const keyChanged = rowIds !== cachedIds || !cachedOptions;
|
|
52
|
+
let rangeExtractor = cachedOptions?.rangeExtractor;
|
|
53
|
+
if (keyChanged || focusedRowId !== cachedFocus) {
|
|
54
|
+
const focusIndex = focusedRowId === undefined ? -1 : rowIds.indexOf(focusedRowId);
|
|
55
|
+
rangeExtractor = (range) => {
|
|
56
|
+
const indexes = defaultRangeExtractor(range);
|
|
57
|
+
if (focusIndex < 0 || indexes.includes(focusIndex))
|
|
58
|
+
return indexes;
|
|
59
|
+
return focusIndex < indexes[0] ? [focusIndex, ...indexes] : [...indexes, focusIndex];
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
cachedOptions = {
|
|
63
|
+
...mountOptions,
|
|
64
|
+
count: rowIds.length,
|
|
65
|
+
estimateSize: cachedHeight === rowHeight ? cachedOptions.estimateSize : () => rowHeight,
|
|
66
|
+
getItemKey: keyChanged ? (index) => rowIds[index] : cachedOptions.getItemKey,
|
|
67
|
+
overscan: options.overscan ?? 5,
|
|
68
|
+
scrollMargin: options.scrollMargin ?? 0,
|
|
69
|
+
scrollPaddingStart: options.scrollPaddingStart ?? 0,
|
|
70
|
+
rangeExtractor,
|
|
71
|
+
};
|
|
72
|
+
cachedIds = rowIds;
|
|
73
|
+
cachedHeight = rowHeight;
|
|
74
|
+
cachedFocus = focusedRowId;
|
|
75
|
+
return cachedOptions;
|
|
76
|
+
}
|
|
77
|
+
const virtualizer = new Virtualizer(virtualOptions());
|
|
78
|
+
let current;
|
|
79
|
+
function snapshot() {
|
|
80
|
+
const items = virtualizer.getVirtualItems();
|
|
81
|
+
const totalSize = virtualizer.getTotalSize();
|
|
82
|
+
const margin = options.scrollMargin ?? 0;
|
|
83
|
+
if (current?.totalSize === totalSize && current.items.length === items.length &&
|
|
84
|
+
current.items.every((row, index) => {
|
|
85
|
+
const item = items[index];
|
|
86
|
+
return row.key === item.key && row.start === item.start - margin && row.size === item.size;
|
|
87
|
+
}))
|
|
88
|
+
return current;
|
|
89
|
+
current = {
|
|
90
|
+
totalSize,
|
|
91
|
+
items: items.map(({ index, key, start, size }) => ({ index, key: String(key), start: start - margin, size })),
|
|
92
|
+
};
|
|
93
|
+
return current;
|
|
94
|
+
}
|
|
95
|
+
let published;
|
|
96
|
+
function publish() {
|
|
97
|
+
const next = snapshot();
|
|
98
|
+
if (published === next)
|
|
99
|
+
return;
|
|
100
|
+
published = next;
|
|
101
|
+
onChange(next);
|
|
102
|
+
}
|
|
103
|
+
function applyScroll(offset) {
|
|
104
|
+
pendingOffset = offset;
|
|
105
|
+
if (!host) {
|
|
106
|
+
element.scrollTop = offset;
|
|
107
|
+
pendingOffset = undefined;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (frame !== undefined)
|
|
111
|
+
host.cancelAnimationFrame(frame);
|
|
112
|
+
// Apply after the new spacer height reaches the DOM, otherwise the old
|
|
113
|
+
// scrollHeight can clamp the anchor when compact rows become taller.
|
|
114
|
+
frame = host.requestAnimationFrame(() => {
|
|
115
|
+
frame = undefined;
|
|
116
|
+
pendingOffset = undefined;
|
|
117
|
+
if (!destroyed)
|
|
118
|
+
element.scrollTop = offset;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const unmount = virtualizer._didMount();
|
|
122
|
+
virtualizer._willUpdate();
|
|
123
|
+
updating = false;
|
|
124
|
+
return {
|
|
125
|
+
snapshot,
|
|
126
|
+
update(next, behavior = {}) {
|
|
127
|
+
if (destroyed)
|
|
128
|
+
return;
|
|
129
|
+
validateOptions(next);
|
|
130
|
+
const before = options;
|
|
131
|
+
const nextZoom = readZoom();
|
|
132
|
+
const zoomChanged = nextZoom !== zoom;
|
|
133
|
+
const layoutChanged = zoomChanged || before.rowHeight !== next.rowHeight ||
|
|
134
|
+
before.scrollMargin !== next.scrollMargin || before.scrollPaddingStart !== next.scrollPaddingStart;
|
|
135
|
+
const offset = pendingOffset ?? (zoomChanged ? stableScrollOffset : element.scrollTop);
|
|
136
|
+
const relative = offset + (before.scrollPaddingStart ?? 0) - (before.scrollMargin ?? 0);
|
|
137
|
+
const anchorIndex = Math.floor(relative / before.rowHeight);
|
|
138
|
+
const anchor = before.rowIds[anchorIndex];
|
|
139
|
+
const nextIndex = layoutChanged && anchor !== undefined ?
|
|
140
|
+
(before.rowIds === next.rowIds ? anchorIndex : next.rowIds.indexOf(anchor)) : -1;
|
|
141
|
+
options = next;
|
|
142
|
+
zoom = nextZoom;
|
|
143
|
+
updating = true;
|
|
144
|
+
virtualizer.setOptions(virtualOptions());
|
|
145
|
+
if (before.rowHeight !== next.rowHeight)
|
|
146
|
+
virtualizer.measure();
|
|
147
|
+
virtualizer._willUpdate();
|
|
148
|
+
updating = false;
|
|
149
|
+
if (behavior.resetScroll)
|
|
150
|
+
applyScroll(0);
|
|
151
|
+
else if (nextIndex >= 0) {
|
|
152
|
+
const withinRow = Math.min(relative - anchorIndex * before.rowHeight, Math.max(0, next.rowHeight - 1));
|
|
153
|
+
applyScroll(Math.max(0, nextIndex * next.rowHeight + withinRow +
|
|
154
|
+
(next.scrollMargin ?? 0) - (next.scrollPaddingStart ?? 0)));
|
|
155
|
+
}
|
|
156
|
+
publish();
|
|
157
|
+
},
|
|
158
|
+
scrollToRow(rowId) {
|
|
159
|
+
if (destroyed)
|
|
160
|
+
return false;
|
|
161
|
+
const index = options.rowIds.indexOf(rowId);
|
|
162
|
+
if (index < 0)
|
|
163
|
+
return false;
|
|
164
|
+
if (frame !== undefined)
|
|
165
|
+
host?.cancelAnimationFrame(frame);
|
|
166
|
+
frame = undefined;
|
|
167
|
+
pendingOffset = undefined;
|
|
168
|
+
virtualizer.scrollToIndex(index, { align: "start" });
|
|
169
|
+
return true;
|
|
170
|
+
},
|
|
171
|
+
destroy() {
|
|
172
|
+
if (destroyed)
|
|
173
|
+
return;
|
|
174
|
+
destroyed = true;
|
|
175
|
+
if (frame !== undefined)
|
|
176
|
+
host?.cancelAnimationFrame(frame);
|
|
177
|
+
unmount();
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Table, type TableOptions } from "@tanstack/table-core";
|
|
2
|
+
import type { GridModelInput } from "./types.js";
|
|
3
|
+
/** Use these options in the framework's one table instance. Treat model inputs as immutable. */
|
|
4
|
+
export declare function createGridTableOptions<T>(input: GridModelInput<T>): TableOptions<T>;
|
|
5
|
+
export declare function getGridPageInfo<T>(table: Table<T>, input: GridModelInput<T>): {
|
|
6
|
+
rowCount: number | undefined;
|
|
7
|
+
pageCount: number | undefined;
|
|
8
|
+
hasPreviousPage: boolean;
|
|
9
|
+
hasNextPage: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function createGridModel<T>(initial: GridModelInput<T>): {
|
|
12
|
+
table: Table<T>;
|
|
13
|
+
update(next: GridModelInput<T>): void;
|
|
14
|
+
snapshot(): {
|
|
15
|
+
rowCount: number | undefined;
|
|
16
|
+
pageCount: number | undefined;
|
|
17
|
+
hasPreviousPage: boolean;
|
|
18
|
+
hasNextPage: boolean;
|
|
19
|
+
rows: import("@tanstack/table-core").Row<T>[];
|
|
20
|
+
columns: import("@tanstack/table-core").Column<T, unknown>[];
|
|
21
|
+
};
|
|
22
|
+
};
|