@macrostrat/data-sheet 3.0.3 → 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.
- package/CHANGELOG.md +29 -0
- package/dist/cell-renderer.cjs +162 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +8 -0
- package/dist/cell-renderer.js +162 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/components/actions.cjs +2 -6
- package/dist/components/actions.cjs.map +1 -1
- package/dist/components/actions.js +1 -3
- package/dist/components/actions.js.map +1 -1
- package/dist/components/colors.cjs +4 -7
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.js +1 -3
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +15 -19
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.d.ts +1 -1
- package/dist/components/editor-popup.js +10 -12
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/core.cjs +67 -207
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +4 -9
- package/dist/core.js +48 -185
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +9 -9
- package/dist/index.cjs +16 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +17 -8
- package/dist/index.js.map +1 -1
- package/dist/postgrest-table/data-loaders.cjs +109 -0
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +6 -0
- package/dist/postgrest-table/data-loaders.js +111 -2
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +1 -0
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +2 -1
- package/dist/postgrest-table/index.js +2 -0
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +26 -0
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -0
- package/dist/postgrest-table/lazy-loader-table.d.ts +3 -0
- package/dist/postgrest-table/lazy-loader-table.js +24 -0
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -0
- package/dist/provider.cjs +59 -336
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +11 -65
- package/dist/provider.js +58 -336
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +54 -0
- package/dist/utils/column-spec.cjs +6 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.js +6 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/dist/utils/key-handling.cjs +26 -1
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -2
- package/dist/utils/key-handling.js +27 -2
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +320 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +318 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +7 -6
- package/src/cell-renderer.ts +217 -0
- package/src/components/actions.ts +1 -3
- package/src/components/colors.ts +1 -4
- package/src/components/editor-popup.ts +9 -12
- package/src/core.ts +82 -254
- package/src/index.ts +3 -0
- package/src/postgrest-table/data-loaders.ts +151 -1
- package/src/postgrest-table/index.ts +2 -1
- package/src/postgrest-table/lazy-loader-table.ts +24 -0
- package/src/provider.ts +66 -483
- package/src/types.ts +66 -0
- package/src/utils/column-spec.ts +6 -1
- package/src/utils/key-handling.ts +31 -5
- package/src/zustand-store.ts +377 -0
package/src/core.ts
CHANGED
|
@@ -5,25 +5,30 @@ import {
|
|
|
5
5
|
InputGroup,
|
|
6
6
|
Intent,
|
|
7
7
|
} from "@blueprintjs/core";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
Column,
|
|
10
|
+
Region,
|
|
11
|
+
RegionCardinality,
|
|
12
|
+
RowHeaderCell,
|
|
13
|
+
Table,
|
|
14
|
+
TableProps,
|
|
15
|
+
} from "@blueprintjs/table";
|
|
9
16
|
import "@blueprintjs/table/lib/css/table.css";
|
|
10
|
-
import hyper from "@macrostrat/hyper";
|
|
11
17
|
import update from "immutability-helper";
|
|
12
18
|
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
|
|
13
|
-
import {
|
|
14
|
-
import
|
|
19
|
+
import { DataSheetAction } from "./components";
|
|
20
|
+
import h from "./main.module.sass";
|
|
15
21
|
import {
|
|
16
22
|
DataSheetProvider,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
singleFocusedCell,
|
|
23
|
+
storeAtom,
|
|
24
|
+
useAtomValue,
|
|
20
25
|
useSelector,
|
|
21
26
|
useStoreAPI,
|
|
22
|
-
|
|
27
|
+
atom,
|
|
23
28
|
} from "./provider";
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
import { DataSheetProviderProps, VisibleCells } from "./types.ts";
|
|
30
|
+
import { basicCellRenderer } from "./cell-renderer.ts";
|
|
31
|
+
import { tableKeyHandlerAtom } from "./utils";
|
|
27
32
|
|
|
28
33
|
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
|
|
29
34
|
|
|
@@ -36,7 +41,7 @@ export enum DataSheetDensity {
|
|
|
36
41
|
LOW = "low",
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
interface DataSheetInternalProps<T> {
|
|
44
|
+
interface DataSheetInternalProps<T> extends TableProps {
|
|
40
45
|
onVisibleCellsChange?: (visibleCells: VisibleCells) => void;
|
|
41
46
|
onSaveData?: (updatedData: any[], data: T[]) => void;
|
|
42
47
|
onUpdateData?: (updatedData: any[], data: T[]) => void;
|
|
@@ -71,7 +76,7 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
|
71
76
|
{
|
|
72
77
|
data,
|
|
73
78
|
columnSpec,
|
|
74
|
-
columnSpecOptions
|
|
79
|
+
columnSpecOptions,
|
|
75
80
|
enableColumnReordering,
|
|
76
81
|
defaultColumnWidth,
|
|
77
82
|
editable,
|
|
@@ -87,9 +92,13 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
|
87
92
|
);
|
|
88
93
|
}
|
|
89
94
|
|
|
95
|
+
const deletedRowHeaderStyle = {
|
|
96
|
+
opacity: 0.5,
|
|
97
|
+
textDecoration: "line-through",
|
|
98
|
+
};
|
|
99
|
+
|
|
90
100
|
function _DataSheet<T>({
|
|
91
101
|
onVisibleCellsChange,
|
|
92
|
-
enableColumnReordering,
|
|
93
102
|
onSaveData,
|
|
94
103
|
onUpdateData,
|
|
95
104
|
onDeleteRows,
|
|
@@ -98,6 +107,8 @@ function _DataSheet<T>({
|
|
|
98
107
|
enableFocusedCell,
|
|
99
108
|
autoFocusEditor = true,
|
|
100
109
|
density = DataSheetDensity.HIGH,
|
|
110
|
+
selectionModes,
|
|
111
|
+
...rest
|
|
101
112
|
}: DataSheetInternalProps<T>) {
|
|
102
113
|
/**
|
|
103
114
|
* @param data: The data to be displayed in the table
|
|
@@ -181,7 +192,7 @@ function _DataSheet<T>({
|
|
|
181
192
|
[selectedRegions],
|
|
182
193
|
);
|
|
183
194
|
|
|
184
|
-
const columnWidths =
|
|
195
|
+
const columnWidths = useAtomValue(columnWidthsAtom);
|
|
185
196
|
|
|
186
197
|
const numRows = Math.max(updatedData.length, data.length);
|
|
187
198
|
|
|
@@ -207,25 +218,25 @@ function _DataSheet<T>({
|
|
|
207
218
|
};
|
|
208
219
|
}
|
|
209
220
|
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
);
|
|
221
|
+
const onColumnsReordered = useSelector((state) => state.onColumnsReordered);
|
|
222
|
+
|
|
223
|
+
const children = useMemo(() => {
|
|
224
|
+
return columnSpec.map((col, colIndex) => {
|
|
225
|
+
return h(Column, {
|
|
226
|
+
name: col.name,
|
|
227
|
+
cellRenderer: (rowIndex) => {
|
|
228
|
+
const state = storeAPI.getState();
|
|
229
|
+
return basicCellRenderer<T>(
|
|
230
|
+
rowIndex,
|
|
231
|
+
colIndex,
|
|
232
|
+
col,
|
|
233
|
+
state,
|
|
234
|
+
autoFocusEditor,
|
|
235
|
+
);
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}, [columnSpec, storeAPI, autoFocusEditor]);
|
|
229
240
|
|
|
230
241
|
const onColumnWidthChanged = useSelector(
|
|
231
242
|
(state) => state.onColumnWidthChanged,
|
|
@@ -233,14 +244,10 @@ function _DataSheet<T>({
|
|
|
233
244
|
|
|
234
245
|
const rowHeaderCellRenderer = useCallback(
|
|
235
246
|
(rowIndex: number) => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
style = {
|
|
239
|
-
opacity: 0.5,
|
|
240
|
-
textDecoration: "line-through",
|
|
241
|
-
};
|
|
242
|
-
}
|
|
247
|
+
const style = deletedRows.has(rowIndex) ? deletedRowHeaderStyle : null;
|
|
248
|
+
|
|
243
249
|
return h(RowHeaderCell, {
|
|
250
|
+
enableRowReordering: false,
|
|
244
251
|
index: rowIndex,
|
|
245
252
|
name: `${rowIndex + 1}`,
|
|
246
253
|
style,
|
|
@@ -249,9 +256,17 @@ function _DataSheet<T>({
|
|
|
249
256
|
[deletedRows],
|
|
250
257
|
);
|
|
251
258
|
|
|
252
|
-
const onKeyDown =
|
|
259
|
+
const onKeyDown = useAtomValue(tableKeyHandlerAtom);
|
|
253
260
|
|
|
254
|
-
|
|
261
|
+
let _selectionModes = selectionModes;
|
|
262
|
+
if (
|
|
263
|
+
editable &&
|
|
264
|
+
_selectionModes != null &&
|
|
265
|
+
!_selectionModes.includes(RegionCardinality.CELLS)
|
|
266
|
+
) {
|
|
267
|
+
_selectionModes = [..._selectionModes, RegionCardinality.CELLS];
|
|
268
|
+
// Ensure selection mode includes "cells"
|
|
269
|
+
}
|
|
255
270
|
|
|
256
271
|
return h("div.data-sheet-container", { className, style }, [
|
|
257
272
|
h.if(editable)(DataSheetEditToolbar, {
|
|
@@ -263,15 +278,13 @@ function _DataSheet<T>({
|
|
|
263
278
|
"div.data-sheet-holder",
|
|
264
279
|
{ onKeyDown },
|
|
265
280
|
h(
|
|
266
|
-
// @ts-expect-error
|
|
267
281
|
Table,
|
|
268
282
|
{
|
|
269
283
|
ref,
|
|
270
284
|
numRows,
|
|
271
285
|
className: "data-sheet",
|
|
272
286
|
enableFocusedCell,
|
|
273
|
-
|
|
274
|
-
//onColumnsReordered,
|
|
287
|
+
onColumnsReordered,
|
|
275
288
|
focusedCell,
|
|
276
289
|
selectedRegions,
|
|
277
290
|
defaultRowHeight: rowHeight,
|
|
@@ -279,6 +292,8 @@ function _DataSheet<T>({
|
|
|
279
292
|
columnWidths,
|
|
280
293
|
onColumnWidthChanged,
|
|
281
294
|
onSelection,
|
|
295
|
+
enableRowReordering: false,
|
|
296
|
+
enableRowResizing: false,
|
|
282
297
|
// The cell renderer is memoized internally based on these data dependencies
|
|
283
298
|
cellRendererDependencies: [
|
|
284
299
|
data,
|
|
@@ -289,6 +304,8 @@ function _DataSheet<T>({
|
|
|
289
304
|
],
|
|
290
305
|
onVisibleCellsChange: _onVisibleCellsChange,
|
|
291
306
|
rowHeaderCellRenderer,
|
|
307
|
+
selectionModes: _selectionModes,
|
|
308
|
+
...rest,
|
|
292
309
|
},
|
|
293
310
|
children,
|
|
294
311
|
),
|
|
@@ -296,210 +313,34 @@ function _DataSheet<T>({
|
|
|
296
313
|
]);
|
|
297
314
|
}
|
|
298
315
|
|
|
299
|
-
|
|
300
|
-
rowIndex: number,
|
|
301
|
-
colIndex: number,
|
|
302
|
-
columnSpec: ColumnSpec,
|
|
303
|
-
state: DataSheetStore<T>,
|
|
304
|
-
autoFocusEditor = true,
|
|
305
|
-
): any {
|
|
306
|
-
const data = state.data;
|
|
307
|
-
const updatedData = state.updatedData;
|
|
308
|
-
|
|
309
|
-
const isDeleted = state.deletedRows.has(rowIndex);
|
|
310
|
-
|
|
311
|
-
const row = data[rowIndex] ?? updatedData[rowIndex];
|
|
312
|
-
const loading = row == null;
|
|
313
|
-
const col = columnSpec;
|
|
314
|
-
|
|
315
|
-
const focusedCell = state.focusedCell;
|
|
316
|
-
const _topLeftCell = state.topLeftCell;
|
|
317
|
-
const onCellEdited = state.onCellEdited;
|
|
318
|
-
const clearSelection = state.clearSelection;
|
|
319
|
-
|
|
320
|
-
const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
|
|
321
|
-
const _renderedValue = col.valueRenderer?.(value) ?? value;
|
|
322
|
-
|
|
323
|
-
let style = col.style ?? {};
|
|
324
|
-
if (isDeleted) {
|
|
325
|
-
style = {
|
|
326
|
-
...style,
|
|
327
|
-
opacity: 0.5,
|
|
328
|
-
textDecoration: "line-through",
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const focused =
|
|
333
|
-
focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
|
|
334
|
-
|
|
335
|
-
const editable = (col.editable ?? state.editable) && !isDeleted;
|
|
336
|
-
|
|
337
|
-
// Top left cell of a ranged selection
|
|
338
|
-
const topLeft =
|
|
339
|
-
_topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
|
|
340
|
-
|
|
341
|
-
if (!editable && state.editable) {
|
|
342
|
-
// If the cell is not editable but the sheet is editable, we want to differentiate
|
|
343
|
-
// between editable and non-editable cells.
|
|
344
|
-
style.color = "var(--secondary-color)";
|
|
345
|
-
}
|
|
316
|
+
/** Atoms for efficient sub-selection of state */
|
|
346
317
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (isDeleted) {
|
|
350
|
-
intent = "danger";
|
|
351
|
-
}
|
|
318
|
+
const deletedRowsAtom = atom((get) => get(storeAtom).deletedRows);
|
|
319
|
+
const updatedDataAtom = atom((get) => get(storeAtom).updatedData);
|
|
352
320
|
|
|
353
|
-
|
|
321
|
+
const hasUpdatesAtom = atom((get) => {
|
|
322
|
+
// Readable atom to indicate whether there are any updates in the updatedData array
|
|
323
|
+
const deletedRows = get(deletedRowsAtom);
|
|
324
|
+
const updatedData = get(updatedDataAtom);
|
|
325
|
+
return updatedData.length > 0 || deletedRows.size > 0;
|
|
326
|
+
});
|
|
354
327
|
|
|
355
|
-
|
|
328
|
+
const columnSpecAtom = atom((get) => get(storeAtom).columnSpec);
|
|
329
|
+
const columnWidthsIndexAtom = atom((get) => get(storeAtom).columnWidthsIndex);
|
|
330
|
+
const defaultColumnWidthAtom = atom((get) => get(storeAtom).defaultColumnWidth);
|
|
356
331
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
{
|
|
362
|
-
intent,
|
|
363
|
-
loading,
|
|
364
|
-
value,
|
|
365
|
-
style,
|
|
366
|
-
isDeleted,
|
|
367
|
-
},
|
|
368
|
-
_renderedValue,
|
|
369
|
-
);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// The rest is for the top-left cell of a selection or the focused cell
|
|
373
|
-
|
|
374
|
-
// Hidden input to capture key events
|
|
375
|
-
let hiddenInput = h("input.hidden-input", {
|
|
376
|
-
autoFocus: true,
|
|
377
|
-
onKeyDown: state.editorKeyHandler,
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
let cellContents: ReactNode = _renderedValue;
|
|
381
|
-
|
|
382
|
-
let _dataEditor = null;
|
|
383
|
-
let className = null;
|
|
384
|
-
|
|
385
|
-
if (col.dataEditor != null) {
|
|
386
|
-
_dataEditor = h(
|
|
387
|
-
EditorPopup,
|
|
388
|
-
{
|
|
389
|
-
autoFocus: autoFocusEditor,
|
|
390
|
-
valueViewer: _renderedValue,
|
|
391
|
-
},
|
|
392
|
-
[
|
|
393
|
-
h(col.dataEditor, {
|
|
394
|
-
value,
|
|
395
|
-
editable,
|
|
396
|
-
onChange(value) {
|
|
397
|
-
if (!editable) return;
|
|
398
|
-
state.onSelectionEdited(value);
|
|
399
|
-
},
|
|
400
|
-
}),
|
|
401
|
-
],
|
|
402
|
-
);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if (!editable) {
|
|
406
|
-
// Most cells are not focused and don't need to be editable.
|
|
407
|
-
// This will be the rendering logic for almost all cells
|
|
408
|
-
|
|
409
|
-
if (_dataEditor != null) {
|
|
410
|
-
cellContents = _dataEditor;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return h(
|
|
414
|
-
_Cell,
|
|
415
|
-
{
|
|
416
|
-
intent,
|
|
417
|
-
value,
|
|
418
|
-
style,
|
|
419
|
-
},
|
|
420
|
-
cellContents,
|
|
421
|
-
);
|
|
422
|
-
// Could probably put the hidden input elsewhere,
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/* The remaining logic covers cells that are focused and editable */
|
|
426
|
-
|
|
427
|
-
const onChange = (e) => {
|
|
428
|
-
if (!editable) return;
|
|
429
|
-
if (value === e.target.value) return;
|
|
430
|
-
onCellEdited(rowIndex, col.key, e.target.value);
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
const isSingleCellSelection = singleFocusedCell(state.selection) != null;
|
|
434
|
-
|
|
435
|
-
let _inlineEditor: ReactNode = null;
|
|
436
|
-
if (typeof inlineEditor == "boolean") {
|
|
437
|
-
let _value = value;
|
|
438
|
-
if (
|
|
439
|
-
typeof _renderedValue === "string" ||
|
|
440
|
-
typeof _renderedValue === "number" ||
|
|
441
|
-
_renderedValue == null
|
|
442
|
-
) {
|
|
443
|
-
_value = _renderedValue;
|
|
444
|
-
}
|
|
445
|
-
_inlineEditor = h("input.main-editor", {
|
|
446
|
-
value: _value ?? "",
|
|
447
|
-
autoFocus: autoFocusEditor,
|
|
448
|
-
onChange,
|
|
449
|
-
onKeyDown: state.editorKeyHandler,
|
|
450
|
-
});
|
|
451
|
-
} else {
|
|
452
|
-
// If inlineEditor is a ReactNode, we use it directly
|
|
453
|
-
_inlineEditor = inlineEditor as ReactNode;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
if (_dataEditor != null) {
|
|
457
|
-
className = "editor-cell";
|
|
458
|
-
cellContents = _dataEditor;
|
|
459
|
-
hiddenInput = null;
|
|
460
|
-
} else if (_inlineEditor != null) {
|
|
461
|
-
cellContents = _inlineEditor;
|
|
462
|
-
className = "input-cell";
|
|
463
|
-
hiddenInput = null;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// Hidden html input
|
|
467
|
-
return h(
|
|
468
|
-
_Cell,
|
|
469
|
-
{
|
|
470
|
-
intent,
|
|
471
|
-
value,
|
|
472
|
-
className,
|
|
473
|
-
style,
|
|
474
|
-
//truncated: false,
|
|
475
|
-
},
|
|
476
|
-
[
|
|
477
|
-
cellContents,
|
|
478
|
-
h.if(editable && isSingleCellSelection)(DragHandle, {
|
|
479
|
-
focusedCell,
|
|
480
|
-
}),
|
|
481
|
-
hiddenInput,
|
|
482
|
-
],
|
|
332
|
+
const columnWidthsAtom = atom((get) => {
|
|
333
|
+
const ix = get(columnWidthsIndexAtom);
|
|
334
|
+
return get(columnSpecAtom).map(
|
|
335
|
+
(col) => ix.get(col.key) ?? col.width ?? get(defaultColumnWidthAtom),
|
|
483
336
|
);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
function DragHandle({ focusedCell }) {
|
|
487
|
-
// TODO: we might want to drag multiple columns in some cases
|
|
488
|
-
// This should be on the last cell of a selection
|
|
489
|
-
const onDragValue = useSelector((state) => state.onDragValue);
|
|
490
|
-
|
|
491
|
-
return h("div.corner-drag-handle", {
|
|
492
|
-
onMouseDown(e) {
|
|
493
|
-
onDragValue(focusedCell);
|
|
494
|
-
e.preventDefault();
|
|
495
|
-
},
|
|
496
|
-
});
|
|
497
|
-
}
|
|
337
|
+
});
|
|
498
338
|
|
|
499
339
|
function DataSheetEditToolbar({ onSaveData, onDeleteRows }) {
|
|
500
340
|
const selection = useSelector((state) => state.selection);
|
|
501
341
|
const resetChanges = useSelector((state) => state.resetChanges);
|
|
502
|
-
|
|
342
|
+
|
|
343
|
+
const hasUpdates = useAtomValue(hasUpdatesAtom);
|
|
503
344
|
|
|
504
345
|
return h("div.data-sheet-toolbar", [
|
|
505
346
|
h(ButtonGroup, { minimal: true }, [
|
|
@@ -580,19 +421,6 @@ function AddRowButton() {
|
|
|
580
421
|
);
|
|
581
422
|
}
|
|
582
423
|
|
|
583
|
-
export function BaseCell({ children, value, ...rest }) {
|
|
584
|
-
return h(
|
|
585
|
-
Cell,
|
|
586
|
-
{
|
|
587
|
-
interactive: true,
|
|
588
|
-
...rest,
|
|
589
|
-
},
|
|
590
|
-
children,
|
|
591
|
-
);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
export { Cell };
|
|
595
|
-
|
|
596
424
|
export function getRowsToDelete(selection) {
|
|
597
425
|
let rowIndices: number[] = [];
|
|
598
426
|
for (const sel of selection) {
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Lazy loading of data from a PostgREST endpoint */
|
|
2
2
|
|
|
3
3
|
import { useAsyncEffect } from "@macrostrat/ui-components";
|
|
4
|
-
import { debounce } from "underscore";
|
|
4
|
+
import { debounce, range } from "underscore";
|
|
5
5
|
import { useCallback, useMemo, useReducer, useRef, useEffect } from "react";
|
|
6
6
|
import update, { Spec } from "immutability-helper";
|
|
7
7
|
|
|
@@ -337,3 +337,153 @@ function indexOfFirstNullInRegion(
|
|
|
337
337
|
}
|
|
338
338
|
return null;
|
|
339
339
|
}
|
|
340
|
+
|
|
341
|
+
export function useTestLazyLoader(config: LazyLoaderOptions = {}) {
|
|
342
|
+
const initialState: LazyLoaderState<any> = {
|
|
343
|
+
data: [],
|
|
344
|
+
loading: false,
|
|
345
|
+
error: null,
|
|
346
|
+
visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
|
|
347
|
+
initialized: false,
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
|
|
351
|
+
const { data, loading } = state;
|
|
352
|
+
|
|
353
|
+
useAsyncEffect(async () => {
|
|
354
|
+
testDataLoader(config, state, dispatch);
|
|
355
|
+
}, [
|
|
356
|
+
data,
|
|
357
|
+
state.visibleRegion.rowIndexStart,
|
|
358
|
+
state.visibleRegion.rowIndexEnd,
|
|
359
|
+
]);
|
|
360
|
+
|
|
361
|
+
// Reference to hold onto the scroll position
|
|
362
|
+
const ref = useRef(null);
|
|
363
|
+
|
|
364
|
+
const onScroll = useCallback(
|
|
365
|
+
debounce((visibleCells: RowRegion) => {
|
|
366
|
+
if (
|
|
367
|
+
visibleCells.rowIndexEnd == ref.current?.rowIndexEnd &&
|
|
368
|
+
visibleCells.rowIndexStart == ref.current?.rowIndexStart
|
|
369
|
+
) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
console.log("Visible cells changed", visibleCells);
|
|
373
|
+
dispatch({
|
|
374
|
+
type: "set-visible",
|
|
375
|
+
region: visibleCells,
|
|
376
|
+
});
|
|
377
|
+
ref.current = visibleCells;
|
|
378
|
+
}, 500),
|
|
379
|
+
[dispatch],
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
data,
|
|
384
|
+
loading,
|
|
385
|
+
onScroll,
|
|
386
|
+
dispatch,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function testDataLoader(
|
|
391
|
+
config: { chunkSize: number },
|
|
392
|
+
state: LazyLoaderState<any>,
|
|
393
|
+
dispatch,
|
|
394
|
+
) {
|
|
395
|
+
const sortKey = "id";
|
|
396
|
+
const rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);
|
|
397
|
+
if (state.loading || rowIndex == null) {
|
|
398
|
+
if (state.initialized) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const chunkSize = 10;
|
|
404
|
+
|
|
405
|
+
let cfg: QueryConfig = {
|
|
406
|
+
//...rest,
|
|
407
|
+
limit: chunkSize,
|
|
408
|
+
offset: null,
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// Allows random seeking
|
|
412
|
+
const isInitialQuery = !state.initialized;
|
|
413
|
+
if (isInitialQuery) {
|
|
414
|
+
cfg.count = "exact";
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// This only works for forward queries
|
|
418
|
+
if (!isInitialQuery) {
|
|
419
|
+
cfg.after = state.data[rowIndex - 1]?.[sortKey];
|
|
420
|
+
if (cfg.after == null) {
|
|
421
|
+
cfg.offset = rowIndex;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
dispatch({ type: "start-loading" });
|
|
426
|
+
|
|
427
|
+
const query = testQueryFunc(cfg);
|
|
428
|
+
query.then((res) => {
|
|
429
|
+
console.log(res);
|
|
430
|
+
const { data, count } = res;
|
|
431
|
+
dispatch({
|
|
432
|
+
type: "loaded",
|
|
433
|
+
data,
|
|
434
|
+
offset: rowIndex,
|
|
435
|
+
totalSize: count,
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
interface QueryParams {
|
|
441
|
+
count: "exact" | "estimated";
|
|
442
|
+
limit: number;
|
|
443
|
+
offset?: number;
|
|
444
|
+
after?: number;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const testQueryFunc = async (
|
|
448
|
+
cfg: QueryParams,
|
|
449
|
+
): Promise<{
|
|
450
|
+
data: any[];
|
|
451
|
+
count: number | null;
|
|
452
|
+
}> => {
|
|
453
|
+
console.log(cfg);
|
|
454
|
+
let offset = null;
|
|
455
|
+
if (cfg.after) {
|
|
456
|
+
offset = cfg.after;
|
|
457
|
+
} else if (cfg.offset) {
|
|
458
|
+
offset = cfg.offset;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let count = cfg.limit;
|
|
462
|
+
const totalCount = 50000;
|
|
463
|
+
count = Math.min(count, totalCount - offset);
|
|
464
|
+
|
|
465
|
+
// wait for a little bit before returning
|
|
466
|
+
await sleep(100);
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
data: buildSyntheticData(offset, count),
|
|
470
|
+
count: cfg.count != null ? 50000 : undefined,
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
async function sleep(ms: number) {
|
|
475
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function buildSyntheticData(offset: number, count: number | null) {
|
|
479
|
+
console.log(
|
|
480
|
+
`Getting data at indices between ${offset} and ${offset + count}`,
|
|
481
|
+
);
|
|
482
|
+
return range(offset, offset + count).map((i) => {
|
|
483
|
+
const id = i + 1;
|
|
484
|
+
return {
|
|
485
|
+
id,
|
|
486
|
+
name: `This is some long text content in row ${id}`,
|
|
487
|
+
};
|
|
488
|
+
});
|
|
489
|
+
}
|
|
@@ -7,6 +7,7 @@ import { PostgrestOrder, usePostgRESTLazyLoader } from "./data-loaders";
|
|
|
7
7
|
import { Spinner, InputGroup } from "@blueprintjs/core";
|
|
8
8
|
|
|
9
9
|
export * from "./data-loaders";
|
|
10
|
+
export * from "./lazy-loader-table";
|
|
10
11
|
import { useCallback, useState, useRef } from "react";
|
|
11
12
|
import {
|
|
12
13
|
ErrorBoundary,
|
|
@@ -15,7 +16,6 @@ import {
|
|
|
15
16
|
useToaster,
|
|
16
17
|
} from "@macrostrat/ui-components";
|
|
17
18
|
import { Spec } from "immutability-helper";
|
|
18
|
-
import { DataSheetProviderProps } from "../provider";
|
|
19
19
|
import { PostgrestFilterBuilder } from "@supabase/postgrest-js";
|
|
20
20
|
import type {
|
|
21
21
|
GenericFunction,
|
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
GenericView,
|
|
24
24
|
} from "@supabase/postgrest-js/dist/cjs/index";
|
|
25
25
|
import { ColorCell } from "../components";
|
|
26
|
+
import { DataSheetProviderProps } from "../types.ts";
|
|
26
27
|
|
|
27
28
|
const h = hyper.styled(styles);
|
|
28
29
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hyper from "@macrostrat/hyper";
|
|
2
|
+
import styles from "./main.module.sass";
|
|
3
|
+
import { DataSheet } from "../core";
|
|
4
|
+
import { useTestLazyLoader } from "./data-loaders";
|
|
5
|
+
import { Spinner } from "@blueprintjs/core";
|
|
6
|
+
|
|
7
|
+
const h = hyper.styled(styles);
|
|
8
|
+
|
|
9
|
+
export function TestLazyLoaderTableView<T>({ ...rest }) {
|
|
10
|
+
const { data, onScroll } = useTestLazyLoader();
|
|
11
|
+
|
|
12
|
+
if (data == null) {
|
|
13
|
+
return h(Spinner);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return h("div.data-sheet-outer", [
|
|
17
|
+
h(DataSheet, {
|
|
18
|
+
...rest,
|
|
19
|
+
data,
|
|
20
|
+
editable: false,
|
|
21
|
+
onVisibleCellsChange: onScroll,
|
|
22
|
+
}),
|
|
23
|
+
]);
|
|
24
|
+
}
|