@macrostrat/data-sheet 3.0.3 → 3.0.4
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 +15 -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 +48 -61
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +20 -32
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -1
- 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 +1 -0
- 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 +5 -2
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/provider.js +5 -2
- package/dist/provider.js.map +1 -1
- 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/package.json +4 -4
- 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 +38 -50
- package/src/postgrest-table/data-loaders.ts +151 -1
- package/src/postgrest-table/index.ts +1 -0
- package/src/postgrest-table/lazy-loader-table.ts +24 -0
- package/src/provider.ts +6 -3
- package/src/utils/column-spec.ts +6 -1
|
@@ -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,
|
|
@@ -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
|
+
}
|
package/src/provider.ts
CHANGED
|
@@ -44,7 +44,7 @@ type StateUpdater<T> = T[] | ((state: T[]) => T[]);
|
|
|
44
44
|
|
|
45
45
|
export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
|
|
46
46
|
setSelection(selection: Region[]): void;
|
|
47
|
-
onDragValue(
|
|
47
|
+
onDragValue(event: MouseEvent): void;
|
|
48
48
|
setUpdatedData(data: StateUpdater<T>): void;
|
|
49
49
|
onCellEdited(rowIndex: number, columnName: string, value: any): void;
|
|
50
50
|
onSelectionEdited(value: any): void;
|
|
@@ -252,8 +252,11 @@ export function DataSheetProvider<T>({
|
|
|
252
252
|
// Right now we don't store this in the state
|
|
253
253
|
visibleCellsRef.current = visibleCells;
|
|
254
254
|
},
|
|
255
|
-
onDragValue(
|
|
256
|
-
set(
|
|
255
|
+
onDragValue(event: MouseEvent) {
|
|
256
|
+
set((state) => {
|
|
257
|
+
return { fillValueBaseCell: state.focusedCell };
|
|
258
|
+
});
|
|
259
|
+
event.preventDefault();
|
|
257
260
|
},
|
|
258
261
|
onCellEdited(rowIndex: number, columnName: string, value: any) {
|
|
259
262
|
set((state) => {
|
package/src/utils/column-spec.ts
CHANGED
|
@@ -133,7 +133,12 @@ export function generateColumnSpec<T>(
|
|
|
133
133
|
options: ColumnSpecOptions<T>,
|
|
134
134
|
): ColumnSpec[] {
|
|
135
135
|
/** Generate a column spec from a dataset */
|
|
136
|
-
const {
|
|
136
|
+
const {
|
|
137
|
+
overrides = {},
|
|
138
|
+
nRows = 10,
|
|
139
|
+
omitColumns,
|
|
140
|
+
includeColumns,
|
|
141
|
+
} = options ?? {};
|
|
137
142
|
|
|
138
143
|
if (data == null) return [];
|
|
139
144
|
|