@macrostrat/data-sheet 3.0.4 → 3.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +15 -1
  2. package/dist/cell-renderer.cjs +162 -0
  3. package/dist/cell-renderer.cjs.map +1 -0
  4. package/dist/cell-renderer.d.ts +8 -0
  5. package/dist/cell-renderer.js +162 -0
  6. package/dist/cell-renderer.js.map +1 -0
  7. package/dist/core.cjs +32 -159
  8. package/dist/core.cjs.map +1 -1
  9. package/dist/core.d.ts +4 -9
  10. package/dist/core.js +33 -158
  11. package/dist/core.js.map +1 -1
  12. package/dist/data-sheet.css +9 -9
  13. package/dist/index.cjs +13 -4
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.js +13 -7
  17. package/dist/index.js.map +1 -1
  18. package/dist/postgrest-table/index.cjs.map +1 -1
  19. package/dist/postgrest-table/index.d.ts +1 -1
  20. package/dist/postgrest-table/index.js.map +1 -1
  21. package/dist/provider.cjs +59 -339
  22. package/dist/provider.cjs.map +1 -1
  23. package/dist/provider.d.ts +11 -65
  24. package/dist/provider.js +58 -339
  25. package/dist/provider.js.map +1 -1
  26. package/dist/types.d.ts +54 -0
  27. package/dist/utils/key-handling.cjs +26 -1
  28. package/dist/utils/key-handling.cjs.map +1 -1
  29. package/dist/utils/key-handling.d.ts +3 -2
  30. package/dist/utils/key-handling.js +27 -2
  31. package/dist/utils/key-handling.js.map +1 -1
  32. package/dist/zustand-store.cjs +320 -0
  33. package/dist/zustand-store.cjs.map +1 -0
  34. package/dist/zustand-store.d.ts +6 -0
  35. package/dist/zustand-store.js +318 -0
  36. package/dist/zustand-store.js.map +1 -0
  37. package/package.json +5 -4
  38. package/src/cell-renderer.ts +217 -0
  39. package/src/core.ts +52 -212
  40. package/src/index.ts +3 -0
  41. package/src/postgrest-table/index.ts +1 -1
  42. package/src/provider.ts +66 -486
  43. package/src/types.ts +66 -0
  44. package/src/utils/key-handling.ts +31 -5
  45. package/src/zustand-store.ts +377 -0
package/src/provider.ts CHANGED
@@ -1,117 +1,66 @@
1
- import { createContext, useContext, useEffect, useRef, useState } from "react";
1
+ import { SetStateAction, useEffect, useRef, useState } from "react";
2
2
  import h from "@macrostrat/hyper";
3
3
  import { createStore, StoreApi, useStore } from "zustand";
4
- import { createComputed } from "zustand-computed";
5
- import type {
6
- FocusedCellCoordinates,
7
- Region,
8
- Table2,
9
- } from "@blueprintjs/table";
4
+ import type { Region, Table } from "@blueprintjs/table";
5
+ import { generateColumnSpec } from "./utils";
6
+ import { createScopedStore } from "@macrostrat/data-components";
10
7
  import {
11
- ColumnSpec,
12
- ColumnSpecOptions,
13
- editorKeyHandler,
14
- generateColumnSpec,
15
- range,
16
- tableKeyHandler,
17
- } from "./utils";
18
- import update, { Spec } from "immutability-helper";
19
-
20
- export interface DataSheetCoreProps<T> {
21
- data: T[];
22
- columnSpec?: ColumnSpec[];
23
- editable?: boolean;
24
- enableColumnReordering?: boolean;
25
- defaultColumnWidth?: number;
26
- }
27
-
28
- export interface DataSheetState<T> {
29
- selection: Region[];
30
- columnSpec: ColumnSpec[];
31
- fillValueBaseCell: FocusedCellCoordinates | null;
32
- focusedCell: FocusedCellCoordinates | null;
33
- topLeftCell: FocusedCellCoordinates | null;
34
- deletedRows: Set<number>;
35
- // Sparse data structure for updated data
36
- updatedData: T[];
37
- initialized: boolean;
38
- columnWidthsIndex: Map<string, number>;
39
- }
40
-
41
- type DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;
42
-
43
- type StateUpdater<T> = T[] | ((state: T[]) => T[]);
44
-
45
- export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
46
- setSelection(selection: Region[]): void;
47
- onDragValue(event: MouseEvent): void;
48
- setUpdatedData(data: StateUpdater<T>): void;
49
- onCellEdited(rowIndex: number, columnName: string, value: any): void;
50
- onSelectionEdited(value: any): void;
51
- onColumnsReordered(oldIndex: number, newIndex: number, length: number): void;
52
- onColumnWidthChanged(columnIndex: number, newWidth: number): void;
53
- moveFocusedCell(direction: "up" | "down" | "left" | "right"): void;
54
- deleteSelectedRows(): void;
55
- clearSelection(): void;
56
- resetChanges(): void;
57
- addRow(): void;
58
- initialize(props: DataSheetCoreProps<T>): void;
59
- onSelection(selection: Region[]): void;
60
- // Internal method used for infinite scrolling
61
- setVisibleCells(visibleCells: VisibleCells): void;
62
- scrollToRow(rowIndex: number): void;
63
- tableRef: React.MutableRefObject<Table2>;
64
- columnWidthsIndex: Map<string, number>;
65
- defaultColumnWidth: number;
66
- }
67
-
68
- export type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {
69
- children: React.ReactNode;
70
- columnSpecOptions?: ColumnSpecOptions<T>;
71
- };
72
-
73
- const DataSheetContext = createContext<StoreApi<DataSheetStore<any>>>(null);
74
-
75
- export interface VisibleCells {
76
- rowIndexStart: number;
77
- rowIndexEnd: number;
78
- }
8
+ DataSheetProviderProps,
9
+ DataSheetStore,
10
+ VisibleCells,
11
+ } from "./types.ts";
12
+ import { createZustandStore } from "./zustand-store.ts";
13
+ import { atomWithStore } from "jotai-zustand";
14
+ import { atom } from "jotai";
15
+
16
+ /** Create a Jotai scoped store */
17
+ const scope = createScopedStore();
18
+ const { useAtom, useAtomValue, useSetAtom } = scope;
19
+ export { useAtom, useAtomValue, useSetAtom };
20
+ export { atom };
21
+
22
+ export const storeAPIAtom = atom<StoreApi<DataSheetStore<any>>>();
23
+
24
+ const storeWrapperAtom = atom((get) => {
25
+ return atomWithStore(get(storeAPIAtom));
26
+ });
27
+
28
+ /** This is the basis for all other atoms that manipulate the store. */
29
+ export const storeAtom = atom(
30
+ (get) => {
31
+ return get(get(storeWrapperAtom));
32
+ },
33
+ (get, set, action: SetStateAction<T>) => {
34
+ return set(get(storeWrapperAtom), action);
35
+ },
36
+ );
37
+
38
+ const initializeStoreAtom = atom(
39
+ null,
40
+ (get, set, payload: Partial<DataSheetStore<T>>) => {
41
+ set(storeAtom, (state) => {
42
+ return {
43
+ ...state,
44
+ ...payload,
45
+ };
46
+ });
47
+ },
48
+ );
79
49
 
80
- interface DataSheetComputedVals {
81
- hasUpdates: boolean;
82
- /** State for column widths (if resized).
83
- * This will reset if the columnSpec prop changes
84
- */
85
- columnWidths: number[];
86
- tableKeyHandler: (evt: React.KeyboardEvent) => void;
87
- editorKeyHandler: (evt: React.KeyboardEvent) => void;
88
- isSingleCellSelection?: boolean;
50
+ export function DataSheetProvider<T>(props: DataSheetProviderProps<T>) {
51
+ const [store] = useState(() => {
52
+ return createStore<DataSheetStore<T>>(createZustandStore);
53
+ });
54
+ return h(
55
+ scope.Provider,
56
+ {
57
+ atoms: [[storeAPIAtom, store]],
58
+ },
59
+ h(DataSheetProviderInner, props),
60
+ );
89
61
  }
90
62
 
91
- export type DataSheetStore<T> = DataSheetComputedVals & DataSheetStoreMain<T>;
92
-
93
- const computed = createComputed(
94
- (state: DataSheetStoreMain<any>): DataSheetComputedVals => {
95
- const isSingleCellSelection = singleFocusedCell(state.selection) != null;
96
- return {
97
- hasUpdates: state.updatedData.length > 0 || state.deletedRows.size > 0,
98
- columnWidths: state.columnSpec.map(
99
- (col) =>
100
- state.columnWidthsIndex.get(col.key) ??
101
- col.width ??
102
- state.defaultColumnWidth,
103
- ),
104
- editorKeyHandler: (e: React.KeyboardEvent) =>
105
- editorKeyHandler(e, isSingleCellSelection),
106
- isSingleCellSelection,
107
- tableKeyHandler: (e: React.KeyboardEvent) => {
108
- tableKeyHandler(e, state);
109
- },
110
- };
111
- },
112
- ) as any;
113
-
114
- export function DataSheetProvider<T>({
63
+ export function DataSheetProviderInner<T>({
115
64
  children,
116
65
  data,
117
66
  columnSpec,
@@ -125,277 +74,28 @@ export function DataSheetProvider<T>({
125
74
  rowIndexEnd: 0,
126
75
  });
127
76
 
128
- const tableRef = useRef<Table2>(null);
77
+ const tableRef = useRef<Table>(null);
129
78
 
130
- const [store] = useState(() => {
131
- const spec = columnSpec || generateColumnSpec(data, columnSpecOptions);
132
- return createStore<DataSheetStore<T>>(
133
- computed((set): DataSheetStore<T> => {
134
- return {
135
- data,
136
- columnSpec: spec,
137
- defaultColumnWidth,
138
- editable,
139
- deletedRows: new Set<number>(),
140
- selection: [],
141
- fillValueBaseCell: null,
142
- updatedData: [],
143
- focusedCell: null,
144
- topLeftCell: null,
145
- initialized: false,
146
- tableRef,
147
- // This is a placeholder
148
- enableColumnReordering: false,
149
- setSelection(selection: Region[]) {
150
- set(updateSelection(selection));
151
- },
152
- columnWidthsIndex: new Map<string, number>(),
153
- moveFocusedCell(direction: "up" | "down" | "left" | "right") {
154
- set((state) => {
155
- const { topLeftCell } = state;
156
- if (topLeftCell == null) return {};
157
- let { col, row } = topLeftCell;
158
- switch (direction) {
159
- case "up":
160
- row = Math.max(0, row - 1);
161
- break;
162
- case "down":
163
- row = Math.min(
164
- row + 1,
165
- Math.max(state.data.length, state.updatedData.length) - 1,
166
- );
167
- break;
168
- case "left":
169
- col = Math.max(0, col - 1);
170
- break;
171
- case "right":
172
- col = Math.min(col + 1, state.columnSpec.length - 1);
173
- break;
174
- }
175
- const region: Region = { cols: [col, col], rows: [row, row] };
176
- return updateSelection([region]);
177
- });
178
- },
179
- addRow(row: Partial<T> = {} as T) {
180
- /** Add a new row. If there is a selection, use the last row index to determine
181
- * where to insert the new row. Otherwise, append to the end of the data. */
182
- set((state) => {
183
- const { updatedData, data, selection } = state;
184
- const lastRowIndex =
185
- getLastRowIndex(selection) ??
186
- Math.max(data.length, updatedData.length) - 1;
187
- // If there is a selection, insert the new row after the last selected row
188
- const spec: Spec<any> = {
189
- $splice: [[lastRowIndex + 1, 0, row]],
190
- };
191
- console.log(spec);
192
-
193
- return {
194
- updatedData: update(updatedData, spec),
195
- };
196
- });
197
- },
198
- setUpdatedData(data: StateUpdater<T>) {
199
- if (Array.isArray(data)) {
200
- set({ updatedData: data });
201
- } else {
202
- set((state) => {
203
- return { updatedData: data(state.updatedData) };
204
- });
205
- }
206
- },
207
- deleteSelectedRows() {
208
- // Remove selected rows from the data and updatedData arrays
209
- set((state) => {
210
- const { selection, deletedRows } = state;
211
- const rowIndices = getRowIndices(selection);
212
-
213
- // Delete rows from both updatedData and data
214
- const newDeletedRows = new Set(deletedRows);
215
- for (const rowIndex of rowIndices) {
216
- newDeletedRows.add(rowIndex);
217
- }
218
-
219
- // Remove selected rows and reset selection
220
- return {
221
- deletedRows: newDeletedRows,
222
- selection: [],
223
- focusedCell: null,
224
- topLeftCell: null,
225
- fillValueBaseCell: null,
226
- };
227
- });
228
- },
229
- resetChanges() {
230
- // Reset the updated data to the initial data
231
- set((state) => ({
232
- updatedData: [],
233
- deletedRows: new Set<number>(),
234
- columnWidths: new Map<string, number>(),
235
- selection: [],
236
- focusedCell: null,
237
- topLeftCell: null,
238
- fillValueBaseCell: null,
239
- }));
240
- },
241
- onColumnWidthChanged(columnIx: number, newWidth: number) {
242
- set((state) => {
243
- const { columnSpec, columnWidthsIndex } = state;
244
- const colKey = columnSpec[columnIx].key;
245
- const newColumnWidths = new Map(columnWidthsIndex);
246
- newColumnWidths.set(colKey, newWidth);
247
- return { columnWidthsIndex: newColumnWidths };
248
- });
249
- },
250
- setVisibleCells(visibleCells: VisibleCells) {
251
- // Visible cells are used for infinite scrolling
252
- // Right now we don't store this in the state
253
- visibleCellsRef.current = visibleCells;
254
- },
255
- onDragValue(event: MouseEvent) {
256
- set((state) => {
257
- return { fillValueBaseCell: state.focusedCell };
258
- });
259
- event.preventDefault();
260
- },
261
- onCellEdited(rowIndex: number, columnName: string, value: any) {
262
- set((state) => {
263
- const { editable, updatedData, data } = state;
264
- if (!editable) return {};
265
- let rowSpec: any;
266
-
267
- // Check to see if the new value is the same as the old one
268
- if (value !== data[rowIndex]?.[columnName]) {
269
- const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
270
- rowSpec = { [rowOp]: { [columnName]: value } };
271
- } else {
272
- rowSpec = { $unset: [columnName] };
273
- }
274
- const spec: Spec<T[]> = { [rowIndex]: rowSpec };
275
- return { updatedData: update(updatedData, spec) };
276
- });
277
- },
278
- initialize(props: DataSheetCoreProps<T>) {
279
- set({ ...props, initialized: true });
280
- },
281
- clearSelection() {
282
- set((state) => {
283
- // Delete all selected cells
284
- const { selection, updatedData, columnSpec, data } = state;
285
- let spec = {};
286
- for (const region of selection) {
287
- console.log("Clearing region", region);
288
- const { cols, rows } = region;
289
- const rowRange = range(rows ?? [0, updatedData.length - 1]);
290
- const colRange = range(cols ?? [0, columnSpec.length - 1]);
291
- for (const row of rowRange) {
292
- let vals = {};
293
- for (const col of colRange) {
294
- const key = columnSpec[col].key;
295
- const currentValue =
296
- updatedData[row]?.[key] ?? data[row]?.[key];
297
- if (currentValue != null && currentValue !== "") {
298
- vals[key] = "";
299
- }
300
- }
301
- let op = updatedData[row] == null ? "$set" : "$merge";
302
- spec[row] = { [op]: vals };
303
- }
304
- }
305
- return {
306
- updatedData: update(updatedData, spec),
307
- };
308
- });
309
- },
310
- onSelection(selection: Region[]) {
311
- set((state) => {
312
- if (
313
- selectionEquals(selection, state.selection) &&
314
- singleFocusedCell(selection) == null // Only if we're in a multi-cell selection mode
315
- ) {
316
- // If the selection is the same as the current selection, remove the selection.
317
- // In practice this only happens for whole-row and whole-column selections
318
- return {
319
- selection: [],
320
- focusedCell: null,
321
- topLeftCell: null,
322
- fillValueBaseCell: null,
323
- };
324
- }
325
-
326
- let spec = updateSelection(selection);
327
- if (state.fillValueBaseCell != null) {
328
- spec.updatedData = fillValues(state, selection);
329
- }
330
- return spec;
331
- });
332
- },
333
- onSelectionEdited(value: any) {
334
- // Apply the same value to all selected cells
335
- set((state) => {
336
- const { selection, updatedData, columnSpec, editable } = state;
337
- if (!editable) return {};
338
- let spec = {};
339
- for (const region of selection) {
340
- const { cols, rows } = region;
341
- const rowRange = range(rows ?? [0, updatedData.length - 1]);
342
- const colRange = range(cols ?? [0, columnSpec.length - 1]);
343
- for (const row of rowRange) {
344
- let vals = {};
345
- for (const col of colRange) {
346
- const key = columnSpec[col].key;
347
- vals[key] = value;
348
- }
349
- let op = updatedData[row] == null ? "$set" : "$merge";
350
- spec[row] = { [op]: vals };
351
- }
352
- }
353
- return {
354
- updatedData: update(updatedData, spec),
355
- };
356
- });
357
- },
358
- onColumnsReordered(
359
- oldIndex: number,
360
- newIndex: number,
361
- length: number,
362
- ) {
363
- set((state) => {
364
- if (!state.enableColumnReordering) return {};
365
- const { columnSpec } = state;
366
- const newSpec = [...columnSpec];
367
- const removed = newSpec.splice(oldIndex, length);
368
- newSpec.splice(newIndex, 0, ...removed);
369
- return { columnSpec: newSpec };
370
- });
371
- },
372
- scrollToRow(rowIndex: number) {
373
- if (tableRef.current == null) return;
374
- tableRef.current.scrollToRegion({
375
- rows: [rowIndex, rowIndex],
376
- });
377
- },
378
- };
379
- }),
380
- );
381
- });
79
+ const initializeStore = scope.useSetAtom(initializeStoreAtom);
382
80
 
383
81
  // Not sure how required this initialization is
384
82
  useEffect(() => {
385
- const { initialize } = store.getState();
386
- initialize({
387
- data,
83
+ initializeStore({
388
84
  columnSpec: columnSpec ?? generateColumnSpec(data, columnSpecOptions),
389
85
  editable,
390
86
  enableColumnReordering,
87
+ data,
88
+ defaultColumnWidth,
89
+ tableRef,
90
+ visibleCellsRef,
391
91
  });
392
92
  }, [data, editable, columnSpec, columnSpecOptions, enableColumnReordering]);
393
93
 
394
- return h(DataSheetContext.Provider, { value: store }, children);
94
+ return children;
395
95
  }
396
96
 
397
97
  export function useStoreAPI<T>(): StoreApi<DataSheetStore<T>> {
398
- const store = useContext(DataSheetContext);
98
+ const store = scope.useAtomValue(storeAPIAtom);
399
99
  if (!store) {
400
100
  throw new Error("Missing DataSheetProvider");
401
101
  }
@@ -408,123 +108,3 @@ export function useSelector<T = any, A = any>(
408
108
  const store = useStoreAPI<T>();
409
109
  return useStore(store, selector);
410
110
  }
411
-
412
- function updateSelection<T>(selection: Region[]) {
413
- const focusedCell = singleFocusedCell(selection);
414
- let spec: Partial<DataSheetState<T>> = {
415
- selection,
416
- focusedCell,
417
- topLeftCell: topLeftCell(selection),
418
- };
419
- if (focusedCell != null) {
420
- spec.fillValueBaseCell = null;
421
- }
422
- return spec;
423
- }
424
-
425
- function getLastRowIndex(regions: Region[]): number | null {
426
- /** Get the last row index from a selection of regions */
427
- if (regions == null || regions.length === 0) return null;
428
- let lastRow = -1;
429
- for (const region of regions) {
430
- const { rows } = region;
431
- if (rows == null || rows.length !== 2) continue;
432
- lastRow = Math.max(lastRow, rows[1]);
433
- }
434
- if (lastRow < 0) {
435
- /** If no valid rows are found, return null */
436
- return null;
437
- }
438
- return lastRow;
439
- }
440
-
441
- function getRowIndices(regions: Region[]): number[] {
442
- /** Get the row indices from a selection of regions */
443
- if (regions == null || regions.length === 0) return [];
444
- const rowIndices = new Set<number>();
445
- for (const region of regions) {
446
- const { rows } = region;
447
- if (rows == null || rows.length !== 2) continue;
448
- for (let i = rows[0]; i <= rows[1]; i++) {
449
- rowIndices.add(i);
450
- }
451
- }
452
- return Array.from(rowIndices).sort((a, b) => a - b);
453
- }
454
-
455
- export function topLeftCell(
456
- regions: Region[],
457
- requireSolitaryCell: boolean = false,
458
- ): FocusedCellCoordinates | null {
459
- /** Top left cell of a ranged selection */
460
- if (regions == null) return null;
461
- const [region] = regions;
462
- if (region == null) return null;
463
- const { cols, rows } = region;
464
- if (cols == null || rows == null) return null;
465
- if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
466
- return null;
467
- return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
468
- }
469
-
470
- function selectionEquals(a: Region[], b: Region[]): boolean {
471
- /** Check if two selections are equal */
472
- if (a.length !== b.length) return false;
473
- for (let i = 0; i < a.length; i++) {
474
- const regionA = a[i];
475
- const regionB = b[i];
476
-
477
- const colsA = regionA.cols ?? [];
478
- const colsB = regionB.cols ?? [];
479
-
480
- const rowsA = regionA.rows ?? [];
481
- const rowsB = regionB.rows ?? [];
482
-
483
- if (
484
- colsA.length !== colsB.length ||
485
- rowsA.length !== rowsB.length ||
486
- colsA[0] !== colsB[0] ||
487
- colsA[1] !== colsB[1] ||
488
- rowsA[0] !== rowsB[0] ||
489
- rowsA[1] !== rowsB[1]
490
- ) {
491
- return false;
492
- }
493
- }
494
- return true;
495
- }
496
-
497
- export function singleFocusedCell(
498
- sel: Region[],
499
- ): FocusedCellCoordinates | null {
500
- /** Derive a single focused cell from a selected region, if possible */
501
- if (sel?.length !== 1) return null;
502
- return topLeftCell(sel, true);
503
- }
504
-
505
- function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
506
- const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
507
-
508
- // Prepare regions by unnesting columns
509
- let regions = selection.map((region) => {
510
- const { cols, rows } = region;
511
- // Get the first column (maybe should be the last)
512
- const [col] = cols;
513
- return { cols: [col, col], rows };
514
- });
515
-
516
- // Fill values downwards
517
- if (!editable || fillValueBaseCell == null) return updatedData;
518
- const { col, row } = fillValueBaseCell;
519
- const key = columnSpec[col].key;
520
- const value = updatedData[row]?.[key] ?? data[row][key];
521
- const spec = {};
522
- for (const region of regions) {
523
- const { cols, rows } = region;
524
- for (const row of range(rows)) {
525
- let op = updatedData[row] == null ? "$set" : "$merge";
526
- spec[row] = { [op]: { [key]: value } };
527
- }
528
- }
529
- return update(updatedData, spec);
530
- }
package/src/types.ts ADDED
@@ -0,0 +1,66 @@
1
+ import type {
2
+ FocusedCellCoordinates,
3
+ Region,
4
+ Table2,
5
+ } from "@blueprintjs/table";
6
+ import { ColumnSpec, ColumnSpecOptions } from "./utils";
7
+
8
+ export interface DataSheetCoreProps<T> {
9
+ data: T[];
10
+ columnSpec?: ColumnSpec[];
11
+ editable?: boolean;
12
+ enableColumnReordering?: boolean;
13
+ defaultColumnWidth?: number;
14
+ }
15
+
16
+ export interface DataSheetState<T> {
17
+ selection: Region[];
18
+ columnSpec: ColumnSpec[];
19
+ fillValueBaseCell: FocusedCellCoordinates | null;
20
+ focusedCell: FocusedCellCoordinates | null;
21
+ topLeftCell: FocusedCellCoordinates | null;
22
+ deletedRows: Set<number>;
23
+ // Sparse data structure for updated data
24
+ updatedData: T[];
25
+ initialized: boolean;
26
+ columnWidthsIndex: Map<string, number>;
27
+ }
28
+
29
+ type DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;
30
+ export type StateUpdater<T> = T[] | ((state: T[]) => T[]);
31
+
32
+ export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
33
+ setSelection(selection: Region[]): void;
34
+ onDragValue(event: MouseEvent): void;
35
+ setUpdatedData(data: StateUpdater<T>): void;
36
+ onCellEdited(rowIndex: number, columnName: string, value: any): void;
37
+ onSelectionEdited(value: any): void;
38
+ onColumnsReordered(oldIndex: number, newIndex: number, length: number): void;
39
+ onColumnWidthChanged(columnIndex: number, newWidth: number): void;
40
+ moveFocusedCell(direction: "up" | "down" | "left" | "right"): void;
41
+ deleteSelectedRows(): void;
42
+ clearSelection(): void;
43
+ resetChanges(): void;
44
+ addRow(): void;
45
+ initialize(props: Partial<DataSheetStoreMain<T>>): void;
46
+ onSelection(selection: Region[]): void;
47
+ // Internal method used for infinite scrolling
48
+ setVisibleCells(visibleCells: VisibleCells): void;
49
+ scrollToRow(rowIndex: number): void;
50
+ tableRef: React.MutableRefObject<Table2>;
51
+ visibleCellsRef: React.MutableRefObject<VisibleCells>;
52
+ columnWidthsIndex: Map<string, number>;
53
+ defaultColumnWidth: number;
54
+ }
55
+
56
+ export type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {
57
+ children: React.ReactNode;
58
+ columnSpecOptions?: ColumnSpecOptions<T>;
59
+ };
60
+
61
+ export interface VisibleCells {
62
+ rowIndexStart: number;
63
+ rowIndexEnd: number;
64
+ }
65
+
66
+ export type DataSheetStore<T> = DataSheetStoreMain<T>;
@@ -1,5 +1,17 @@
1
1
  import type { KeyboardEvent } from "react";
2
- import { DataSheetStoreMain } from "@macrostrat/data-sheet";
2
+ import { storeAPIAtom, storeAtom } from "../provider";
3
+ import type { DataSheetStoreMain } from "../types";
4
+ import { atom } from "jotai";
5
+ import { singleFocusedCell } from "../zustand-store.ts";
6
+
7
+ export const tableKeyHandlerAtom = atom((get) => {
8
+ const store = get(storeAPIAtom);
9
+ return (e: KeyboardEvent) => {
10
+ // Kind of a ridiculous hack to get the store from the atom.
11
+ const state = store.getState();
12
+ tableKeyHandler(e, state);
13
+ };
14
+ });
3
15
 
4
16
  export function tableKeyHandler<T>(
5
17
  e: KeyboardEvent,
@@ -43,10 +55,24 @@ const directionMap = {
43
55
  ArrowRight: "right",
44
56
  };
45
57
 
46
- export function editorKeyHandler(
47
- e: KeyboardEvent,
48
- isSingleCellSelection: boolean,
49
- ) {
58
+ const selectionAtom = atom((get) => {
59
+ const store = get(storeAtom);
60
+ return store.selection;
61
+ });
62
+
63
+ const isSingleCellSelectionAtom = atom((get) => {
64
+ const selection = get(selectionAtom);
65
+ return singleFocusedCell(selection) != null;
66
+ });
67
+
68
+ export const editorKeyHandlerAtom = atom((get) => {
69
+ return (e: KeyboardEvent) => {
70
+ const isSingleCellSelection = get(isSingleCellSelectionAtom);
71
+ editorKeyHandler(e, isSingleCellSelection);
72
+ };
73
+ });
74
+
75
+ function editorKeyHandler(e: KeyboardEvent, isSingleCellSelection: boolean) {
50
76
  /** Get a key handler for inline cell editing */
51
77
 
52
78
  // if (e.key == "Enter" || e.key == "Tab") {