@macrostrat/data-sheet 4.7.0 → 4.9.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 (125) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +60 -19
  3. package/dist/actions/context.cjs +10 -1
  4. package/dist/actions/context.cjs.map +1 -1
  5. package/dist/actions/context.js +10 -1
  6. package/dist/actions/context.js.map +1 -1
  7. package/dist/actions/types.d.ts +4 -0
  8. package/dist/components/data-editor.cjs +527 -0
  9. package/dist/components/data-editor.cjs.map +1 -0
  10. package/dist/components/data-editor.d.ts +142 -0
  11. package/dist/components/data-editor.js +524 -0
  12. package/dist/components/data-editor.js.map +1 -0
  13. package/dist/components/editor-actions.cjs +167 -0
  14. package/dist/components/editor-actions.cjs.map +1 -0
  15. package/dist/components/editor-actions.d.ts +37 -0
  16. package/dist/components/editor-actions.js +165 -0
  17. package/dist/components/editor-actions.js.map +1 -0
  18. package/dist/components/editor-popup.cjs +11 -1
  19. package/dist/components/editor-popup.cjs.map +1 -1
  20. package/dist/components/editor-popup.js +11 -1
  21. package/dist/components/editor-popup.js.map +1 -1
  22. package/dist/components/index.d.ts +3 -0
  23. package/dist/components/indicators/selection.cjs +10 -4
  24. package/dist/components/indicators/selection.cjs.map +1 -1
  25. package/dist/components/indicators/selection.js +10 -4
  26. package/dist/components/indicators/selection.js.map +1 -1
  27. package/dist/components/row-editor.cjs +351 -0
  28. package/dist/components/row-editor.cjs.map +1 -0
  29. package/dist/components/row-editor.d.ts +181 -0
  30. package/dist/components/row-editor.js +349 -0
  31. package/dist/components/row-editor.js.map +1 -0
  32. package/dist/components/row-editor.module.sass.cjs +51 -0
  33. package/dist/components/row-editor.module.sass.cjs.map +1 -0
  34. package/dist/components/row-editor.module.sass.js +49 -0
  35. package/dist/components/row-editor.module.sass.js.map +1 -0
  36. package/dist/components/toolbars/actions-toolbar.cjs +1 -1
  37. package/dist/components/toolbars/actions-toolbar.cjs.map +1 -1
  38. package/dist/components/toolbars/actions-toolbar.js +1 -1
  39. package/dist/components/toolbars/actions-toolbar.js.map +1 -1
  40. package/dist/data-panel.cjs +36 -0
  41. package/dist/data-panel.cjs.map +1 -1
  42. package/dist/data-panel.d.ts +3 -2
  43. package/dist/data-panel.js +37 -1
  44. package/dist/data-panel.js.map +1 -1
  45. package/dist/data-panel.module.sass.cjs +28 -26
  46. package/dist/data-panel.module.sass.cjs.map +1 -1
  47. package/dist/data-panel.module.sass.js +28 -26
  48. package/dist/data-panel.module.sass.js.map +1 -1
  49. package/dist/data-sheet.css +267 -56
  50. package/dist/data-sheet.d.ts +2 -1
  51. package/dist/data-view.d.ts +1 -0
  52. package/dist/index.cjs +28 -0
  53. package/dist/index.cjs.map +1 -1
  54. package/dist/index.js +31 -3
  55. package/dist/index.js.map +1 -1
  56. package/dist/main.module.sass.cjs +14 -13
  57. package/dist/main.module.sass.cjs.map +1 -1
  58. package/dist/main.module.sass.js +14 -13
  59. package/dist/main.module.sass.js.map +1 -1
  60. package/dist/postgrest-table/data-loaders.cjs +8 -1
  61. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  62. package/dist/postgrest-table/data-loaders.js +9 -2
  63. package/dist/postgrest-table/data-loaders.js.map +1 -1
  64. package/dist/provider/column-spec.cjs +8 -1
  65. package/dist/provider/column-spec.cjs.map +1 -1
  66. package/dist/provider/column-spec.d.ts +105 -1
  67. package/dist/provider/column-spec.js +8 -1
  68. package/dist/provider/column-spec.js.map +1 -1
  69. package/dist/provider/core.cjs +32 -7
  70. package/dist/provider/core.cjs.map +1 -1
  71. package/dist/provider/core.d.ts +5 -1
  72. package/dist/provider/core.js +32 -7
  73. package/dist/provider/core.js.map +1 -1
  74. package/dist/provider/loader-state.cjs +11 -3
  75. package/dist/provider/loader-state.cjs.map +1 -1
  76. package/dist/provider/loader-state.d.ts +8 -1
  77. package/dist/provider/loader-state.js +12 -4
  78. package/dist/provider/loader-state.js.map +1 -1
  79. package/dist/provider/table-data.cjs +10 -1
  80. package/dist/provider/table-data.cjs.map +1 -1
  81. package/dist/provider/table-data.d.ts +10 -0
  82. package/dist/provider/table-data.js +11 -2
  83. package/dist/provider/table-data.js.map +1 -1
  84. package/dist/provider/types.cjs.map +1 -1
  85. package/dist/provider/types.d.ts +4 -1
  86. package/dist/provider/types.js.map +1 -1
  87. package/dist/provider/zustand-store.cjs +26 -13
  88. package/dist/provider/zustand-store.cjs.map +1 -1
  89. package/dist/provider/zustand-store.js +14 -1
  90. package/dist/provider/zustand-store.js.map +1 -1
  91. package/dist/renderers/cell-renderer.cjs +15 -4
  92. package/dist/renderers/cell-renderer.cjs.map +1 -1
  93. package/dist/renderers/cell-renderer.js +13 -4
  94. package/dist/renderers/cell-renderer.js.map +1 -1
  95. package/dist/renderers/column-header-cell.cjs +12 -1
  96. package/dist/renderers/column-header-cell.cjs.map +1 -1
  97. package/dist/renderers/column-header-cell.js +11 -1
  98. package/dist/renderers/column-header-cell.js.map +1 -1
  99. package/dist/types.cjs.map +1 -1
  100. package/dist/types.d.ts +30 -1
  101. package/dist/types.js.map +1 -1
  102. package/package.json +2 -2
  103. package/src/actions/context.ts +13 -2
  104. package/src/actions/types.ts +5 -0
  105. package/src/components/data-editor.ts +741 -0
  106. package/src/components/editor-actions.ts +228 -0
  107. package/src/components/editor-popup.ts +16 -3
  108. package/src/components/index.ts +3 -0
  109. package/src/components/indicators/selection.ts +12 -4
  110. package/src/components/row-editor.module.sass +161 -0
  111. package/src/components/row-editor.ts +620 -0
  112. package/src/components/toolbars/actions-toolbar.ts +3 -2
  113. package/src/data-panel.module.sass +25 -0
  114. package/src/data-panel.ts +50 -1
  115. package/src/main.module.sass +19 -0
  116. package/src/postgrest-table/data-loaders.ts +13 -0
  117. package/src/provider/column-spec.ts +94 -16
  118. package/src/provider/core.ts +41 -9
  119. package/src/provider/loader-state.ts +15 -2
  120. package/src/provider/table-data.ts +23 -1
  121. package/src/provider/types.ts +4 -1
  122. package/src/provider/zustand-store.ts +30 -1
  123. package/src/renderers/cell-renderer.ts +18 -3
  124. package/src/renderers/column-header-cell.ts +10 -0
  125. package/src/types.ts +31 -1
package/src/data-panel.ts CHANGED
@@ -35,7 +35,14 @@ import {
35
35
  useRef,
36
36
  useState,
37
37
  } from "react";
38
- import { Classes, Icon, NonIdealState, Spinner } from "@blueprintjs/core";
38
+ import {
39
+ AnchorButton,
40
+ Classes,
41
+ Icon,
42
+ NonIdealState,
43
+ Spinner,
44
+ } from "@blueprintjs/core";
45
+ import { startAfterAtom } from "./provider/loader-state.ts";
39
46
  import {
40
47
  anchorRefAtom,
41
48
  ctx,
@@ -205,6 +212,7 @@ export function DataPanelRenderer<T>({
205
212
  contentFooter,
206
213
  autoLoadPages,
207
214
  scrollBody,
215
+ pageLinks,
208
216
  className,
209
217
  toolbarStyle = DataPanelToolbarStyle.BORDERED,
210
218
  viewControls = "inline",
@@ -412,6 +420,45 @@ export function DataPanelRenderer<T>({
412
420
  contentFooter ?? defaultFooter,
413
421
  );
414
422
 
423
+ // Crawlable paging (see `PageLinks`). Both are plain links, so they work in
424
+ // the server-rendered HTML with no script: the next-page link is visually
425
+ // hidden after the loaded rows and points past the last loaded row; the
426
+ // "Return to top" notice heads the content while the view starts mid-list.
427
+ const startAfter = ctx.useValue(startAfterAtom);
428
+ const lastLoadedRow = useMemo(() => {
429
+ for (let i = data.length - 1; i >= 0; i--) {
430
+ if (data[i] != null) return data[i];
431
+ }
432
+ return null;
433
+ }, [data]);
434
+ // Gated on the counts rather than `hasMore`, which reads the resolved
435
+ // provider — synced after mount, so absent in a server render.
436
+ const moreToCome = total == null || loadedCount < total;
437
+ let nextPageLink: ReactNode = null;
438
+ if (pageLinks != null && lastLoadedRow != null && moreToCome) {
439
+ nextPageLink = h(
440
+ "a.crawl-next-link",
441
+ {
442
+ href: pageLinks.after(lastLoadedRow),
443
+ rel: "next",
444
+ tabIndex: -1,
445
+ "aria-hidden": true,
446
+ },
447
+ "Next page",
448
+ );
449
+ }
450
+ let viewStartNotice: ReactNode = null;
451
+ if (startAfter != null && pageLinks?.top != null) {
452
+ viewStartNotice = h("div.view-start-notice", [
453
+ h("span.view-start-text", `Continued after #${startAfter}`),
454
+ h(
455
+ AnchorButton,
456
+ { href: pageLinks.top, minimal: true, small: true, icon: "arrow-up" },
457
+ pageLinks.topLabel ?? "Return to top",
458
+ ),
459
+ ]);
460
+ }
461
+
415
462
  // Body + optional filter/detail sidebar share a horizontal row so each
416
463
  // scrolls independently.
417
464
 
@@ -461,7 +508,9 @@ export function DataPanelRenderer<T>({
461
508
  },
462
509
  [
463
510
  h("div.data-panel-body-content", [
511
+ viewStartNotice,
464
512
  emptyState ?? h(ScrollBody, { placeholders }, cards),
513
+ nextPageLink,
465
514
  _contentFooter,
466
515
  ]),
467
516
  ],
@@ -8,7 +8,15 @@
8
8
  --data-sheet-cell-padding: 0px 8px
9
9
 
10
10
  :global
11
+ // Tags drawn in a cell (a tag picker as a value renderer, say) are pinned
12
+ // inside it: a tighter line height than the row's, one line centred in
13
+ // the cell clear of its bottom border, and clipped — through the
14
+ // variables the tag and picker styles read in @macrostrat/data-components
11
15
  .bp6-table-cell
16
+ --tag-line-height: 1.25
17
+ --tag-picker-height: calc(var(--data-sheet-row-height) - 1px)
18
+ --tag-picker-overflow: hidden
19
+ --tag-row-wrap: nowrap
12
20
  padding: var(--data-sheet-cell-padding)
13
21
  &:not([class*="bp6-intent-"]):not(.bp6-loading)
14
22
  color: var(--text-color)
@@ -56,6 +64,12 @@
56
64
  &:focus
57
65
  outline: none
58
66
 
67
+ // Derived (computed) values: dimmed and italic, so a restatement reads apart
68
+ // from a record. The intent colours still win for an invalid derived cell.
69
+ .derived-cell
70
+ color: var(--pz-text-subtle-color, var(--secondary-color)) !important
71
+ font-style: italic
72
+
59
73
  .value-viewer-cell
60
74
  :global(.bp6-popover-target)
61
75
  width: 100%
@@ -64,6 +78,11 @@
64
78
  padding: var(--data-sheet-cell-padding)
65
79
  margin: calc(var(--data-sheet-cell-padding) * -1)
66
80
 
81
+ // The selected cell sits under the selection's fill, which swallows a tag's
82
+ // own background; a faint outline keeps tags apart from it
83
+ .editor-cell
84
+ --tag-outline: 0 0 0 1px rgba(128, 128, 128, 0.35)
85
+
67
86
  // Make sure popover target fills the entire cell
68
87
  .editor-cell :global(.bp6-popover-target)
69
88
  //pointer-events: none
@@ -27,6 +27,7 @@ import {
27
27
  lazyLoaderCoreStateAtom,
28
28
  resolveInitialData,
29
29
  seededRows,
30
+ startAfterAtom,
30
31
  type LazyLoaderStateCore,
31
32
  } from "../provider/loader-state.ts";
32
33
 
@@ -677,6 +678,10 @@ export function useDataLoader<T = any>(
677
678
  const page = ctx.useValue(chunkPageAtom);
678
679
  const setPage = ctx.useSet(chunkPageAtom);
679
680
  const refreshToken = ctx.useValue(dataRefreshTokenAtom);
681
+ // The keyset cursor of a view that starts mid-list. Seeded at store creation
682
+ // (`startAfter`); cleared below on the first view change, since it describes
683
+ // only the view it arrived with.
684
+ const [startAfter, setStartAfter] = ctx.use(startAfterAtom);
680
685
  const columnSorts = useSelector((s) => s.columnSorts);
681
686
  const activeFilters = useSelector((s) => s.activeFilters);
682
687
  const abortRef = useRef<AbortController | null>(null);
@@ -729,9 +734,16 @@ export function useDataLoader<T = any>(
729
734
  const seedRef = useRef(resolveInitialData(initialData));
730
735
  // Gate on the seed until it's actually in state (see the fetch effect).
731
736
  const awaitingSeedRef = useRef(seedRef.current != null);
737
+ const firstResetRef = useRef(true);
732
738
  useEffect(() => {
733
739
  const seed = seedRef.current;
734
740
  seedRef.current = null;
741
+ // A view change (not the mount) ends a mid-list start: the cursor only
742
+ // described the view it arrived with.
743
+ if (!firstResetRef.current) {
744
+ setStartAfter(null);
745
+ }
746
+ firstResetRef.current = false;
735
747
  if (seed != null) {
736
748
  if (!state.initialized) {
737
749
  dispatch({
@@ -798,6 +810,7 @@ export function useDataLoader<T = any>(
798
810
  sorts: columnSorts,
799
811
  filters,
800
812
  cursor,
813
+ after: startAfter,
801
814
  });
802
815
  if (controller.signal.aborted) return;
803
816
  const rows = result.rows ?? [];
@@ -1,4 +1,5 @@
1
1
  import React from "react";
2
+ import type { Placement } from "@blueprintjs/core";
2
3
  import { enhanceColumnFilter, TableFilter } from "../actions";
3
4
 
4
5
  const defaultRenderers = {
@@ -21,13 +22,7 @@ const defaultWidthForValue = (val) => String(val).length * 8;
21
22
  /** Inferred data type of a column, used to select appropriate
22
23
  * sort/filter operators. */
23
24
  export type ColumnDataType =
24
- | "text"
25
- | "string"
26
- | "number"
27
- | "integer"
28
- | "boolean"
29
- | "object"
30
- | "array";
25
+ "text" | "string" | "number" | "integer" | "boolean" | "object" | "array";
31
26
 
32
27
  /** Severity of a cell validation result. `warning` is soft (flagged, doesn't
33
28
  * block saving); `error` is hard (blocks saving). */
@@ -91,14 +86,42 @@ export interface CellEditors {
91
86
  export interface CellDetailContext<T = any> extends CellRenderContext<T> {
92
87
  /** Whether this cell is editable (column × table × not-deleted). */
93
88
  editable: boolean;
94
- /** Commit a new value for this cell. */
89
+ /**
90
+ * When the surface stands for several cells at once (the row editor over a
91
+ * multi-row selection), the cells it stands for. `value` is then the value
92
+ * they share, or `undefined` when they differ (`mixed`), and `onChange`
93
+ * writes every one of them. Only offered to a column that declares
94
+ * `multiCell`; absent for a single cell.
95
+ */
96
+ cells?: CellSelectionEntry<T>[];
97
+ /** True when `cells` hold differing values. */
98
+ mixed?: boolean;
99
+ /** Where the surface is drawn: `"cell"`, the grid's own popover, modal or
100
+ * in-cell surface; `"row-editor"`, a field of the sheet's row editor; or
101
+ * `"editor"`, a field of a standalone `DataEditor` — so a surface can lay
102
+ * itself out to suit (a picker nesting its own popovers only where it has
103
+ * room). */
104
+ surface?: "cell" | "row-editor" | "editor";
105
+ /** Commit a new value for this cell — for every cell, when the surface
106
+ * stands for several. */
95
107
  onChange: (value: any) => void;
108
+ /** With `cells`, commit a value for each cell, aligned with them — for a
109
+ * surface that changes each cell's own value (adding a tag to every row's
110
+ * list) rather than setting them all to one. */
111
+ onChangeCells?: (values: any[]) => void;
96
112
  /** Reset this cell (and selection) to its base value. */
97
113
  resetValue: () => void;
98
114
  /** Close the surface and return focus to the table. */
99
115
  close: () => void;
100
116
  }
101
117
 
118
+ /** One of the cells a multi-cell surface stands for. */
119
+ export interface CellSelectionEntry<T = any> {
120
+ rowIndex: number;
121
+ row: T | null | undefined;
122
+ value: any;
123
+ }
124
+
102
125
  /** How a cell's detail surface is presented. Orthogonal to what it renders. */
103
126
  export type DetailPresentation = "popover" | "modal" | "inline";
104
127
 
@@ -148,11 +171,45 @@ export interface ColumnSpec {
148
171
  * same component works in any container.
149
172
  */
150
173
  cellDetail?: (ctx: CellDetailContext) => React.ReactNode;
174
+ /**
175
+ * Whether this column's `cellDetail` can stand for several cells at once —
176
+ * read the shared value (or `ctx.mixed`) and write every cell in
177
+ * `ctx.cells` through one `onChange`. A surface that doesn't declare this
178
+ * is shown read-only over a multi-row selection, with "Multiple values"
179
+ * where they differ. The default editors (an input by `dataType`) always
180
+ * can, since setting one value on every cell is what they do.
181
+ */
182
+ multiCell?: boolean;
151
183
  /** How `cellDetail` is presented. Defaults to `"popover"`. */
152
184
  detailPresentation?: DetailPresentation;
185
+ /** What one cell of the column is called, for the selection indicator
186
+ * ("1 lithology", "3 lithologies"). Defaults to the column's name. */
187
+ cellLabel?: string;
188
+ /** Where a popover `cellDetail` opens against its cell. Defaults to
189
+ * `"right-start"`; a wide surface (a tag picker) reads better below, at
190
+ * `"bottom-start"`. */
191
+ detailPlacement?: Placement;
153
192
  cellComponent?: any;
154
193
  category?: string;
155
194
  editable?: boolean;
195
+ /**
196
+ * A **derived** column holds values computed from other data — a total, an
197
+ * age that follows from an interval and a proportion — rather than values
198
+ * anyone types. It is never writable, whatever `editable` or the table
199
+ * says, and every write path (inline editing, fill, paste, the row editor)
200
+ * leaves it alone; it is drawn dimmed so a reader can tell what is a record
201
+ * and what is a restatement of one. Use it instead of `editable: false` when
202
+ * the read-only-ness is a property of the *data* rather than of the view.
203
+ */
204
+ derived?: boolean;
205
+ /**
206
+ * A **hidden** column stays in the spec but is left out of the working
207
+ * spec the table is built from: it isn't rendered, isn't part of a
208
+ * selection, and isn't written by a fill or a paste. A consumer keeps one
209
+ * spec and toggles this flag ("show absolute ages", "show generated ids")
210
+ * rather than maintaining two arrays. Resolved by `postprocessColumnSpec`.
211
+ */
212
+ hidden?: boolean;
156
213
  inlineEditor?: boolean | React.ComponentType<any> | string | null;
157
214
  style?: React.CSSProperties;
158
215
  width?: number;
@@ -328,15 +385,36 @@ export function generateColumnSpec<T>(
328
385
 
329
386
  export function postprocessColumnSpec(columnSpec: ColumnSpec[]) {
330
387
  /** Postprocess column spec to make sure that, e.g., column filters are
331
- * properly established, etc.
388
+ * properly established, etc. Hidden columns are dropped here, so everything
389
+ * downstream — the renderer, the selection, the edit paths — works with the
390
+ * visible columns only and indexes into one array.
332
391
  */
333
- return columnSpec.map((col) => {
334
- return {
335
- ...col,
336
- filters: postprocessColumnFilters(col),
337
- actions: col.actions ?? [],
338
- };
339
- });
392
+ return columnSpec
393
+ .filter((col) => !col.hidden)
394
+ .map((col) => {
395
+ return {
396
+ ...col,
397
+ filters: postprocessColumnFilters(col),
398
+ actions: col.actions ?? [],
399
+ };
400
+ });
401
+ }
402
+
403
+ /**
404
+ * Whether a column takes writes at all: the table is editable, the column
405
+ * isn't locked (`editable: false`) and it isn't `derived`. This is the one
406
+ * rule every write path consults — the inline editor, `onSelectionEdited`,
407
+ * fill, clear, clipboard paste and the row editor — so a locked column is
408
+ * locked however the edit arrives, not only when it is typed into.
409
+ */
410
+ export function isColumnWritable(
411
+ col: ColumnSpec | null | undefined,
412
+ tableEditable: boolean = true,
413
+ ): boolean {
414
+ if (col == null) return false;
415
+ if (!tableEditable) return false;
416
+ if (col.derived) return false;
417
+ return col.editable !== false;
340
418
  }
341
419
 
342
420
  function postprocessColumnFilters(col: ColumnSpec): TableFilter[] | undefined {
@@ -162,7 +162,14 @@ export function useResolvedProvider<T>(props: {
162
162
  }
163
163
 
164
164
  function DataSheetStoreWrapper<T>(props: DataSheetProviderProps<T>) {
165
- const { toaster, initialFilters, initialSorts, initialData, ...rest } = props;
165
+ const {
166
+ toaster,
167
+ initialFilters,
168
+ initialSorts,
169
+ initialData,
170
+ startAfter,
171
+ ...rest
172
+ } = props;
166
173
 
167
174
  // Initial view state is folded into the store's *creation*, not applied in an
168
175
  // effect: the loader mounts below this and its effects run first, so an
@@ -186,7 +193,10 @@ function DataSheetStoreWrapper<T>(props: DataSheetProviderProps<T>) {
186
193
  {
187
194
  ctx,
188
195
  initializeStore,
189
- atoms: [[toasterAtom, toaster], ...loaderSeedAtoms(initialData)],
196
+ atoms: [
197
+ [toasterAtom, toaster],
198
+ ...loaderSeedAtoms(initialData, startAfter),
199
+ ],
190
200
  debugName: "DataSheetProvider",
191
201
  },
192
202
  h(DataSheetProviderInner, rest),
@@ -258,12 +268,19 @@ export function splitDataProviderProps<T>(props: AnyDataSheetProps<T>) {
258
268
  .union(interactionOptionsKeys)
259
269
  .union(tableDataProviderKeys) as Set<keyof DataSheetProviderProps<T>>,
260
270
  );
261
- // `initialData` goes to both sides: the provider creates the store seeded
262
- // with it, and the loader takes it as the first window it needn't fetch.
263
- const initialData = (props as { initialData?: any }).initialData;
271
+ // `initialData` and `startAfter` go to both sides: the provider creates the
272
+ // store seeded with them, and the loader takes them as the first window it
273
+ // needn't fetch and the cursor its chunks are counted from.
274
+ const { initialData, startAfter } = props as {
275
+ initialData?: any;
276
+ startAfter?: string | number | null;
277
+ };
264
278
  if (initialData != null) {
265
279
  providerProps.initialData = initialData;
266
280
  }
281
+ if (startAfter != null) {
282
+ providerProps.startAfter = startAfter;
283
+ }
267
284
  return [providerProps, rendererProps] as const;
268
285
  }
269
286
 
@@ -385,7 +402,7 @@ export function DataSheetProviderInner<T>(
385
402
  const rows = loadedData.filter((r) => r != null);
386
403
  if (rows.length === 0) return;
387
404
  derivedSpecFor.current = columnSpec;
388
- storeAPI.setState({ columnSpec: columnSpec(rows) });
405
+ storeAPI.setState({ columnSpec: postprocessColumnSpec(columnSpec(rows)) });
389
406
  }, [columnSpec, loadedData, storeAPI]);
390
407
 
391
408
  const bumpRefresh = ctx.useSet(dataRefreshTokenAtom);
@@ -625,10 +642,25 @@ function itemCount(n: number, dataKind: string) {
625
642
  }
626
643
 
627
644
  export function pluralize(singularForm: string, n: number) {
628
- const pluralForm = singularForm + "s";
629
- if (n == 0) return `${pluralForm}`;
630
645
  if (n == 1) return singularForm;
631
- return pluralForm;
646
+ return pluralFormOf(singularForm);
647
+ }
648
+
649
+ /** An English plural, well enough for the nouns a sheet uses: "row" → "rows",
650
+ * "lithology" → "lithologies", "thickness (m)" → "thicknesses (m)" — a
651
+ * trailing parenthetical (a unit, say) stays as it is. */
652
+ export function pluralFormOf(singularForm: string): string {
653
+ const match = singularForm.match(/^(.*?)(\s*\([^)]*\))?$/);
654
+ const head = match?.[1] ?? singularForm;
655
+ const tail = match?.[2] ?? "";
656
+ if (head === "") return singularForm;
657
+ let plural = head + "s";
658
+ if (/(s|x|z|ch|sh)$/i.test(head)) {
659
+ plural = head + "es";
660
+ } else if (/[^aeiou]y$/i.test(head)) {
661
+ plural = head.slice(0, -1) + "ies";
662
+ }
663
+ return plural + tail;
632
664
  }
633
665
 
634
666
  export function capitalize(s: string) {
@@ -70,13 +70,26 @@ export function seededLoaderCore<T>(
70
70
  };
71
71
  }
72
72
 
73
+ /** The identity of the row the current view starts after (`startAfter`), or
74
+ * `null` for a view that starts at the top. Set at store creation so a server
75
+ * render shows the "Return to top" link; the loader clears it on the first view
76
+ * change. */
77
+ export const startAfterAtom = atom<string | number | null>(null);
78
+
73
79
  /** Scoped-store atoms that make a store start out seeded. The store's own
74
80
  * `data` is seeded alongside (see `DataSheetStoreWrapper`). Empty when there is
75
81
  * no seed. */
76
82
  export function loaderSeedAtoms<T>(
77
83
  initialData: FetchDataOptions<T>["initialData"],
84
+ startAfter: FetchDataOptions<T>["startAfter"] = null,
78
85
  ): AtomMap {
86
+ const atoms: AtomMap = [];
87
+ if (startAfter != null) {
88
+ atoms.push([startAfterAtom, startAfter]);
89
+ }
79
90
  const seed = resolveInitialData(initialData);
80
- if (seed == null) return [];
81
- return [[lazyLoaderCoreStateAtom, seededLoaderCore(seed)]];
91
+ if (seed != null) {
92
+ atoms.push([lazyLoaderCoreStateAtom, seededLoaderCore(seed)]);
93
+ }
94
+ return atoms;
82
95
  }
@@ -32,6 +32,12 @@ export interface FetchDataParams {
32
32
  * sources can page from this cursor (e.g. `WHERE key > cursor`) instead of a
33
33
  * slow `OFFSET`; offset-based sources can ignore it. */
34
34
  cursor?: { row: any; index: number } | null;
35
+ /** Identity of the row the view is counted from: `offset` 0 is the first
36
+ * row *after* it. Set while a view starts mid-list (see `startAfter` in
37
+ * `FetchDataOptions`); absent or `null` when it starts at the top. Keyset
38
+ * sources translate it to `WHERE key > after`; the local provider slices past
39
+ * the row. `totalCount` then reports the rows after it. */
40
+ after?: string | number | null;
35
41
  }
36
42
 
37
43
  /** Result of a `fetchChunk` call. `totalCount` reports the source length when
@@ -125,7 +131,7 @@ export function createLocalProvider<T = any>(
125
131
 
126
132
  return {
127
133
  identity,
128
- async fetchData({ offset, limit, sorts, filters }) {
134
+ async fetchData({ offset, limit, sorts, filters, after }) {
129
135
  let rows = data;
130
136
  if (filters != null && filters.length > 0) {
131
137
  rows = rows.filter((row) =>
@@ -137,6 +143,7 @@ export function createLocalProvider<T = any>(
137
143
  if (sorts != null && sorts.length > 0) {
138
144
  rows = [...rows].sort(compareRowsBySorts(sorts));
139
145
  }
146
+ rows = rowsAfter(rows, after, identity);
140
147
  return {
141
148
  rows: rows.slice(offset, offset + limit),
142
149
  totalCount: rows.length,
@@ -148,6 +155,21 @@ export function createLocalProvider<T = any>(
148
155
  };
149
156
  }
150
157
 
158
+ /** The rows past the one with identity `after` in the (filtered, sorted) view;
159
+ * the whole view when `after` is absent or names no row. Identities are compared
160
+ * as strings, since a cursor read from a URL arrives as one. */
161
+ export function rowsAfter<T>(
162
+ rows: T[],
163
+ after: string | number | null | undefined,
164
+ identity: (row: T) => string | number | null | undefined,
165
+ ): T[] {
166
+ if (after == null) return rows;
167
+ const key = String(after);
168
+ const index = rows.findIndex((row) => String(identity(row)) === key);
169
+ if (index < 0) return rows;
170
+ return rows.slice(index + 1);
171
+ }
172
+
151
173
  // Stable synthetic identity for in-memory rows lacking a natural `id`. Keyed by
152
174
  // object reference (a local data array keeps the same row objects across
153
175
  // re-sorts), so edits survive re-ordering even without an id field.
@@ -147,7 +147,7 @@ export interface DataSheetState<T> {
147
147
  /** Whether selecting a cell automatically activates its surface (opens an
148
148
  * editor/detail panel and, for editors, focuses it). `"manual"` opens only
149
149
  * on click. */
150
- cellInteraction: "auto" | "manual";
150
+ cellInteraction: "auto" | "manual" | "second-click";
151
151
  /** Runtime override of `cellInteraction`: while `false`, auto-activation is
152
152
  * suppressed (set by pressing Escape, re-armed by clicking a cell) so the
153
153
  * keyboard prioritizes navigation. */
@@ -255,6 +255,9 @@ export type DataSheetProviderProps<T> = DataViewCoreProps<T> & {
255
255
  * The provider creates the store seeded with it, so the rows are in the very
256
256
  * first render — a server render included. */
257
257
  initialData?: FetchDataOptions<T>["initialData"];
258
+ /** The row the view starts after (see `FetchDataOptions`). Seeded into the
259
+ * store at creation alongside `initialData`. */
260
+ startAfter?: FetchDataOptions<T>["startAfter"];
258
261
  viewType?: DataViewRendererType;
259
262
  children?: React.ReactNode;
260
263
  };
@@ -19,6 +19,7 @@ import {
19
19
  getSelectionCardinality,
20
20
  range,
21
21
  } from "../actions";
22
+ import { isColumnWritable } from "./column-spec.ts";
22
23
 
23
24
  /** Monotonic counter for synthetic ids on in-table-added rows. */
24
25
  let _addedRowCounter = 0;
@@ -240,6 +241,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
240
241
  event.preventDefault();
241
242
  },
242
243
  onCellEdited(rowIndex: number, columnName: string, value: any) {
244
+ // The column decides whether it takes writes (see `isColumnWritable`):
245
+ // a locked or derived column is refused here as well as in the cell
246
+ // renderer, so a programmatic caller — the row editor, say — can't
247
+ // write what the grid wouldn't let you type.
248
+ const targetColumn = get().columnSpec.find((c) => c.key === columnName);
249
+ if (!isColumnWritable(targetColumn, get().editable)) return;
243
250
  set((state) => {
244
251
  const { editable, updatedData, data } = state;
245
252
  if (!editable) return {};
@@ -297,6 +304,8 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
297
304
  if (rowIsDeleted) continue;
298
305
  let vals = {};
299
306
  for (const col of colRange) {
307
+ // A locked or derived column is never cleared
308
+ if (!isColumnWritable(columnSpec[col], state.editable)) continue;
300
309
  const key = columnSpec[col].key;
301
310
  const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
302
311
  if (currentValue != null && currentValue !== "") {
@@ -309,6 +318,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
309
318
  });
310
319
  }
311
320
  }
321
+ if (Object.keys(vals).length === 0) continue;
312
322
  let op = updatedData[row] == null ? "$set" : "$merge";
313
323
  spec[row] = { [op]: vals };
314
324
  }
@@ -355,8 +365,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
355
365
  // target's click toggle — this avoids racing the selection event
356
366
  // (which fires on mousedown, possibly more than once) and is what
357
367
  // makes both re-focus-to-open and click-to-close reliable.
368
+ // In "second-click" mode a new selection only selects; the click on
369
+ // the already-selected cell opens it.
358
370
  if (!selectionEquals(selection, state.selection)) {
359
- spec.cellSurfaceOpen = singleFocusedCell(selection) != null;
371
+ spec.cellSurfaceOpen =
372
+ state.cellInteraction !== "second-click" &&
373
+ singleFocusedCell(selection) != null;
360
374
  }
361
375
  return spec;
362
376
  });
@@ -381,8 +395,13 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
381
395
  for (const visibleRow of rowRange) {
382
396
  // Selection indices are visible positions; map to the data row.
383
397
  const row = toDataRowIndex(state, visibleRow);
398
+ // A deleted row takes no edits
399
+ if (state.rowStatus[row] === TableElementStatus.DELETED) continue;
384
400
  let vals = {};
385
401
  for (const col of colRange) {
402
+ // Only the columns that take writes — a selection spanning a
403
+ // locked or derived column writes around it
404
+ if (!isColumnWritable(columnSpec[col], editable)) continue;
386
405
  const key = columnSpec[col].key;
387
406
  vals[key] = value;
388
407
  edits.push({
@@ -392,6 +411,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
392
411
  row: state.data[row],
393
412
  });
394
413
  }
414
+ if (Object.keys(vals).length === 0) continue;
395
415
  let op = updatedData[row] == null ? "$set" : "$merge";
396
416
  spec[row] = { [op]: vals };
397
417
  }
@@ -594,6 +614,8 @@ function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
594
614
  // Fill values downwards
595
615
  if (!editable || fillValueBaseCell == null) return updatedData;
596
616
  const { col, row: baseVisibleRow } = fillValueBaseCell;
617
+ // Dragging from a locked or derived column fills nothing
618
+ if (!isColumnWritable(columnSpec[col], editable)) return updatedData;
597
619
  // The base cell and target rows are visible positions; map to data rows.
598
620
  const baseRow = toDataRowIndex(state, baseVisibleRow);
599
621
  const key = columnSpec[col].key;
@@ -603,6 +625,7 @@ function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
603
625
  const { rows } = region;
604
626
  for (const visibleRow of range(rows ?? [])) {
605
627
  const row = toDataRowIndex(state, visibleRow);
628
+ if (state.rowStatus[row] === TableElementStatus.DELETED) continue;
606
629
  let op = updatedData[row] == null ? "$set" : "$merge";
607
630
  spec[row] = { [op]: { [key]: value } };
608
631
  }
@@ -643,6 +666,12 @@ function valuesAreEquivalent(a: any, b: any): boolean {
643
666
  const aBlank = a == null || a === "";
644
667
  const bBlank = b == null || b === "";
645
668
  if (aBlank || bBlank) return aBlank && bBlank;
669
+ // Structured values (an interval position, a list of lithologies) compare
670
+ // by content: as strings, every object is "[object Object]", so any edit
671
+ // to one — or to a list that keeps its length — read as no change
672
+ if (typeof a === "object" || typeof b === "object") {
673
+ return JSON.stringify(a) === JSON.stringify(b);
674
+ }
646
675
  return String(a) === String(b);
647
676
  }
648
677