@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
@@ -0,0 +1,620 @@
1
+ /**
2
+ * Row editor — a selection's fields as a form, derived from the column spec:
3
+ * the `DataEditor`'s form (see `data-editor.ts`) over the sheet's selected
4
+ * rows.
5
+ *
6
+ * The row-level mirror of the cell viewer/editor split: every column becomes a
7
+ * field whose label is the column's `name`, whose value is drawn by the
8
+ * column's `valueRenderer`, and whose editor is the column's `cellDetail` when
9
+ * it has one, else an input chosen from `dataType`. What may be written is the
10
+ * same rule the grid applies (`isColumnWritable`): a locked or derived column
11
+ * shows as read-only, and the form is read-only as a whole when `editable` is
12
+ * off — a read-only "row viewer" and an editable "row editor" are one surface
13
+ * in two modes.
14
+ *
15
+ * **Several rows at once.** Over a multi-row selection each field stands for
16
+ * that column's cells in every selected row. A value they share is shown and
17
+ * edited as one; where they differ the field reads "Multiple values". A
18
+ * default editor (an input by `dataType`) sets one value on every cell, which
19
+ * is always meaningful; a column's own `cellDetail` edits several cells only
20
+ * when the column declares `multiCell`, and is otherwise shown read-only.
21
+ *
22
+ * **A cell selection.** When particular cells are selected rather than whole
23
+ * rows, the columns they fall in are the *focus*: those fields are editable
24
+ * and marked as selected, and the rest of the row is shown for context,
25
+ * read-only.
26
+ *
27
+ * `RowEditor` knows nothing about the sheet's store: it takes rows, their edit
28
+ * overlays, and an `onChange`, so a consumer can mount it beside a table, in a
29
+ * dialog, or on a page where the table isn't rendered at all.
30
+ * `SelectedRowEditor` binds it to a `DataSheetProvider`'s store — the selected
31
+ * rows and columns, the edit overlay, the `editable` flag — and writes through
32
+ * `onCellEdited`, so edits land in the same overlay and the same `onEdit`
33
+ * stream as typing into the grid. It is drawn as a panel, titled with what it
34
+ * is doing to how many of the sheet's items ("Editing 3 samples") in a bar
35
+ * drawn as the toolbar's selection tag, whose ✕ clears the selection.
36
+ *
37
+ * Whether it is shown at all is the sheet's `rowEditorOpenAtom`, which the
38
+ * toolbar's `ShowRowEditor` control toggles (add `showRowEditorAction` to a
39
+ * sheet's `actions`), so the editor can be mounted wherever the layout wants
40
+ * it and still be switched from the sheet.
41
+ */
42
+ import hyper from "@macrostrat/hyper";
43
+ import classNames from "classnames";
44
+ import { Button, type IconName, NonIdealState } from "@blueprintjs/core";
45
+ import { atom } from "jotai";
46
+ import { type ReactNode, useMemo, useRef } from "react";
47
+ import { type Region, RegionCardinality } from "@blueprintjs/table";
48
+ import {
49
+ type CellValidation,
50
+ type ColumnSpec,
51
+ ctx as sheetScope,
52
+ itemLabelAtom,
53
+ pluralize,
54
+ selectionAtom,
55
+ tableActionsAtom,
56
+ useSelector,
57
+ useStoreAPI,
58
+ } from "../provider";
59
+ import {
60
+ getSelectedColumnKeys,
61
+ getSelectedRowIndices,
62
+ getSelectionCardinality,
63
+ } from "../actions/selection.ts";
64
+ import type { TableAction } from "../actions";
65
+ import {
66
+ DataEditor,
67
+ DataEditorField,
68
+ DataEditorFields,
69
+ DataEditorFrame,
70
+ type DataEditorProps,
71
+ type FieldSpec,
72
+ } from "./data-editor";
73
+ import styles from "./row-editor.module.sass";
74
+
75
+ const h = hyper.styled(styles);
76
+
77
+ export interface RowEditorProps<T = any> {
78
+ /** The columns to show, in order. Hidden columns are left out unless
79
+ * `showHidden` is set; derived and locked columns show read-only. */
80
+ columnSpec: ColumnSpec[];
81
+ /** The row as loaded (the base values). One row; see `rows` for several. */
82
+ row?: T | null;
83
+ /** The pending edits to `row`, laid over it for display. A field present
84
+ * here is marked as edited. */
85
+ edits?: Partial<T> | null;
86
+ /** Several rows at once (a multi-row selection). Takes precedence over
87
+ * `row`. Each field then stands for that column across all of them. */
88
+ rows?: T[];
89
+ /** Edit overlays aligned with `rows`. */
90
+ rowEdits?: (Partial<T> | null | undefined)[];
91
+ /** The rows' data indices, aligned with `rows`, handed to `validate` and
92
+ * `cellDetail`. */
93
+ rowIndices?: number[];
94
+ /** The columns the selection falls in, when it is a cell selection rather
95
+ * than whole rows. Those fields are editable and marked; the others are
96
+ * shown read-only for context. `null` means every writable field. */
97
+ focusColumns?: string[] | null;
98
+ /** Whether the form takes edits at all. Off, it is a row *viewer*. */
99
+ editable?: boolean;
100
+ /** A field was changed — for every row the form stands for. Absent, the
101
+ * form is read-only. */
102
+ onChange?: (columnKey: string, value: any) => void;
103
+ /** A field was changed row by row: a value for each of the form's rows,
104
+ * aligned with them (a `cellDetail`'s `onChangeCells`). */
105
+ onChangeCells?: (columnKey: string, values: any[]) => void;
106
+ /** The field to mark as active — the cell the sheet has selected, when the
107
+ * selection came from the form — without narrowing what is editable. */
108
+ activeColumn?: string | null;
109
+ /** A field was clicked. */
110
+ onFieldClick?: (columnKey: string) => void;
111
+ /** Revert one field to its base value, in every row. Absent, edited fields
112
+ * have no revert affordance. */
113
+ onResetField?: (columnKey: string) => void;
114
+ /** Also show the columns the spec hides (read-only). */
115
+ showHidden?: boolean;
116
+ /** Leave out fields with no value — a compact read-only view. Editable
117
+ * fields always show, so there is somewhere to type. */
118
+ hideEmpty?: boolean;
119
+ /** Labels beside values rather than above them. */
120
+ inline?: boolean;
121
+ className?: string;
122
+ /** Draw the form as a panel: bordered, with a title bar. */
123
+ panel?: boolean;
124
+ /** The panel's title ("Editing 1 row"). */
125
+ title?: ReactNode;
126
+ /** Close the panel — a ✕ at the end of the title bar. */
127
+ onClose?: () => void;
128
+ /** A label for the ✕ ("Clear selection"). */
129
+ closeLabel?: string;
130
+ /** Rendered above the fields. */
131
+ header?: ReactNode;
132
+ children?: ReactNode;
133
+ }
134
+
135
+ /** The form over one row or several, as the sheet's row editor draws it:
136
+ * `DataEditorFields` in a `DataEditorFrame`, with the sheet's notions of a
137
+ * focus (the columns a cell selection falls in) and an active field. */
138
+ export function RowEditor<T = any>(props: RowEditorProps<T>) {
139
+ const {
140
+ columnSpec,
141
+ focusColumns = null,
142
+ editable = true,
143
+ onChange,
144
+ onChangeCells,
145
+ activeColumn = null,
146
+ onFieldClick,
147
+ onResetField,
148
+ showHidden = false,
149
+ hideEmpty = false,
150
+ inline = false,
151
+ className,
152
+ panel = false,
153
+ title,
154
+ onClose,
155
+ closeLabel,
156
+ header,
157
+ children,
158
+ } = props;
159
+
160
+ const { rows, rowEdits, rowIndices } = resolveRows(props);
161
+
162
+ return h(DataEditorFrame, { className, panel, title, onClose, closeLabel }, [
163
+ header,
164
+ h(DataEditorFields<T>, {
165
+ dataSpec: columnSpec,
166
+ rows,
167
+ rowEdits,
168
+ rowIndices,
169
+ surface: "row-editor",
170
+ editable,
171
+ onChange,
172
+ onChangeCells,
173
+ onResetField,
174
+ focusFields: focusColumns,
175
+ activeField: activeColumn,
176
+ onFieldClick,
177
+ showHidden,
178
+ hideEmpty,
179
+ inline,
180
+ }),
181
+ children,
182
+ ]);
183
+ }
184
+
185
+ /** The row editor's frame: the data editor's. */
186
+ export const RowEditorFrame = DataEditorFrame;
187
+
188
+ /** A row editor field: the data editor's. */
189
+ export const RowEditorField = DataEditorField;
190
+
191
+ /** One row or several, as arrays either way. */
192
+ function resolveRows<T>(props: RowEditorProps<T>): {
193
+ rows: (T | null | undefined)[];
194
+ rowEdits: (Partial<T> | null | undefined)[];
195
+ rowIndices: number[];
196
+ } {
197
+ if (props.rows != null) {
198
+ return {
199
+ rows: props.rows,
200
+ rowEdits: props.rowEdits ?? [],
201
+ rowIndices: props.rowIndices ?? [],
202
+ };
203
+ }
204
+ return {
205
+ rows: [props.row],
206
+ rowEdits: [props.edits],
207
+ rowIndices: props.rowIndices ?? [],
208
+ };
209
+ }
210
+
211
+ /* ---------------------------------------------------------- store-bound */
212
+
213
+ export interface SelectedRows<T = any> {
214
+ /** Data indices of the selected rows; empty when nothing is selected. */
215
+ rowIndices: number[];
216
+ rows: (T | null | undefined)[];
217
+ edits: (Partial<T> | null | undefined)[];
218
+ /** The columns a cell selection falls in; `null` for whole rows (or the
219
+ * whole table), when every column is in play. */
220
+ columnKeys: string[] | null;
221
+ cardinality: RegionCardinality | null;
222
+ /** Whether any selected row is marked deleted. */
223
+ anyDeleted: boolean;
224
+ }
225
+
226
+ /** The rows the sheet's selection addresses, with their edit overlays and the
227
+ * columns the selection falls in. Must be read inside a
228
+ * `DataSheetProvider`. */
229
+ export function useSelectedRows<T = any>(): SelectedRows<T> {
230
+ const selection = useSelector((s) => s.selection);
231
+ const data = useSelector((s) => s.data);
232
+ const updatedData = useSelector((s) => s.updatedData);
233
+ const rowStatus = useSelector((s) => s.rowStatus);
234
+ const columnSpec = useSelector((s) => s.columnSpec);
235
+ const filteredRowIndices = useSelector((s) => s.filteredRowIndices);
236
+
237
+ return useMemo(() => {
238
+ const regions = selection ?? [];
239
+ const rowIndices = getSelectedRowIndices(regions, filteredRowIndices);
240
+ const cardinality = getSelectionCardinality(regions) ?? null;
241
+ let columnKeys: string[] | null = null;
242
+ if (cardinality === RegionCardinality.CELLS) {
243
+ columnKeys = getSelectedColumnKeys(regions, columnSpec);
244
+ }
245
+ return {
246
+ rowIndices,
247
+ rows: rowIndices.map((i) => data?.[i] as T | null | undefined),
248
+ edits: rowIndices.map((i) => updatedData?.[i] as Partial<T> | undefined),
249
+ columnKeys,
250
+ cardinality,
251
+ anyDeleted: rowIndices.some((i) => rowStatus?.[i] === "deleted"),
252
+ };
253
+ }, [selection, data, updatedData, rowStatus, columnSpec, filteredRowIndices]);
254
+ }
255
+
256
+ export interface SelectedRow<T = any> {
257
+ rowIndex: number | null;
258
+ row: T | null;
259
+ edits: Partial<T> | null;
260
+ isDeleted: boolean;
261
+ }
262
+
263
+ /** The first selected row alone — the single-row view of `useSelectedRows`. */
264
+ export function useSelectedRow<T = any>(): SelectedRow<T> {
265
+ const { rowIndices, rows, edits, anyDeleted } = useSelectedRows<T>();
266
+ if (rowIndices.length === 0) {
267
+ return { rowIndex: null, row: null, edits: null, isDeleted: false };
268
+ }
269
+ return {
270
+ rowIndex: rowIndices[0],
271
+ row: rows[0] ?? null,
272
+ edits: edits[0] ?? null,
273
+ isDeleted: anyDeleted,
274
+ };
275
+ }
276
+
277
+ export interface SelectedRowEditorProps extends Omit<
278
+ RowEditorProps,
279
+ | "columnSpec"
280
+ | "row"
281
+ | "edits"
282
+ | "rows"
283
+ | "rowEdits"
284
+ | "rowIndices"
285
+ | "focusColumns"
286
+ | "onChange"
287
+ | "onResetField"
288
+ > {
289
+ /** Shown when no row is selected. Defaults to a `NonIdealState` saying so
290
+ * ("No units selected"). */
291
+ emptyState?: ReactNode;
292
+ /** Shown above the fields over a multi-row selection; the default says a
293
+ * change applies to all of them. Pass `null` for nothing. */
294
+ selectionSummary?: ((count: number) => ReactNode) | null;
295
+ /** Whether the panel's ✕ clears the selection (default). */
296
+ closeable?: boolean;
297
+ /** Follow the sheet's `rowEditorOpenAtom`, rendering nothing while it is
298
+ * off (default). */
299
+ toggleable?: boolean;
300
+ }
301
+
302
+ /**
303
+ * The row editor bound to the enclosing sheet's store: it follows the
304
+ * selected rows and the columns a cell selection falls in, and writes through
305
+ * the store's `onCellEdited` for every selected row, so an edit made here is
306
+ * the same edit as one typed into the grid — same overlay, same `onEdit`
307
+ * events, same reset. Render it anywhere under the `DataSheetProvider` (the
308
+ * `DataSheet`'s children, a sibling panel, or a page where the grid itself is
309
+ * not mounted).
310
+ */
311
+ export function SelectedRowEditor(props: SelectedRowEditorProps) {
312
+ const {
313
+ emptyState,
314
+ selectionSummary,
315
+ closeable = true,
316
+ toggleable = true,
317
+ panel = true,
318
+ editable,
319
+ header,
320
+ ...rest
321
+ } = props;
322
+ const store = useStoreAPI();
323
+ const columnSpec = useSelector((s) => s.columnSpec);
324
+ const tableEditable = useSelector((s) => s.editable);
325
+ const itemLabel = sheetScope.useValue(itemLabelAtom);
326
+ // Not the store's `clearSelection`, which clears the selected cells' values
327
+ const setSelection = sheetScope.useSet(selectionAtom);
328
+ const open = sheetScope.useValue(rowEditorOpenAtom);
329
+ const selection = useSelector((s) => s.selection);
330
+ const filteredRowIndices = useSelector((s) => s.filteredRowIndices);
331
+ const { rowIndices, rows, edits, columnKeys, anyDeleted } = useSelectedRows();
332
+ const count = rowIndices.length;
333
+ // A click on a field selects its cells in the sheet. That selection is the
334
+ // form's own: it marks the field without narrowing the form to it, as a
335
+ // cell selection made in the grid would.
336
+ const formSelection = useRef<{ regions: Region[]; key: string } | null>(null);
337
+ let activeColumn: string | null = null;
338
+ let focusColumns = columnKeys;
339
+ const own = formSelection.current;
340
+ if (own != null && sameRegions(own.regions, selection ?? [])) {
341
+ activeColumn = own.key;
342
+ focusColumns = null;
343
+ }
344
+ const items = `${count} ${pluralize(itemLabel, count)}`;
345
+
346
+ if (toggleable && !open) return null;
347
+
348
+ if (count === 0) {
349
+ let empty = emptyState;
350
+ if (empty === undefined) {
351
+ empty = h(NonIdealState, {
352
+ title: `No ${pluralize(itemLabel, 0)} selected`,
353
+ className: "row-editor-non-ideal",
354
+ });
355
+ }
356
+ return h(RowEditorFrame, { panel, className: "row-editor-empty" }, empty);
357
+ }
358
+
359
+ const canEdit = (editable ?? true) && tableEditable && !anyDeleted;
360
+ let title = `Viewing ${items}`;
361
+ if (canEdit) title = `Editing ${items}`;
362
+
363
+ let onClose: (() => void) | undefined;
364
+ if (closeable) onClose = () => setSelection([]);
365
+
366
+ const onChange = (columnKey: string, value: any) => {
367
+ const { onCellEdited } = store.getState();
368
+ for (const rowIndex of rowIndices) onCellEdited(rowIndex, columnKey, value);
369
+ };
370
+ // Reverting a field is writing its loaded value back: the store drops an
371
+ // override equal to the base, and a controlled consumer sees an ordinary
372
+ // `setCells` rather than a whole-table reset.
373
+ const onResetField = (columnKey: string) => {
374
+ const { onCellEdited } = store.getState();
375
+ rowIndices.forEach((rowIndex, i) => {
376
+ const base = rows[i];
377
+ if (base == null) return;
378
+ onCellEdited(rowIndex, columnKey, base[columnKey]);
379
+ });
380
+ };
381
+
382
+ const onChangeCells = (columnKey: string, values: any[]) => {
383
+ const { onCellEdited } = store.getState();
384
+ rowIndices.forEach((rowIndex, i) => {
385
+ onCellEdited(rowIndex, columnKey, values[i]);
386
+ });
387
+ };
388
+
389
+ const onFieldClick = (columnKey: string) => {
390
+ if (activeColumn === columnKey) return;
391
+ const regions = cellRegions(
392
+ rowIndices,
393
+ columnSpec.findIndex((c) => c.key === columnKey),
394
+ filteredRowIndices,
395
+ );
396
+ if (regions.length === 0) return;
397
+ formSelection.current = { regions, key: columnKey };
398
+ const state = store.getState();
399
+ // Selecting from the form opens nothing over the grid
400
+ store.setState({ cellSurfaceOpen: false });
401
+ state.setSelection(regions);
402
+ };
403
+
404
+ let summary: ReactNode = null;
405
+ if (count > 1 && canEdit && selectionSummary !== null) {
406
+ let text: ReactNode = `A change here applies to all ${items}.`;
407
+ if (selectionSummary != null) text = selectionSummary(count);
408
+ summary = h("div.selection-summary", text);
409
+ }
410
+
411
+ return h(RowEditor, {
412
+ ...rest,
413
+ columnSpec,
414
+ rows,
415
+ rowEdits: edits,
416
+ rowIndices,
417
+ focusColumns,
418
+ activeColumn,
419
+ onFieldClick,
420
+ panel,
421
+ title,
422
+ onClose,
423
+ closeLabel: "Clear selection",
424
+ header: h([header, summary]),
425
+ editable: canEdit,
426
+ onChange,
427
+ onChangeCells,
428
+ onResetField,
429
+ });
430
+ }
431
+
432
+ /** One column's cells in the given rows, as the table's regions: the rows'
433
+ * places in the view (under any filter), one region per run of adjacent
434
+ * rows. */
435
+ function cellRegions(
436
+ rowIndices: number[],
437
+ colIndex: number,
438
+ filteredRowIndices: number[] | null | undefined,
439
+ ): Region[] {
440
+ if (colIndex < 0) return [];
441
+ const visible = rowIndices
442
+ .map((i) => {
443
+ if (filteredRowIndices == null) return i;
444
+ return filteredRowIndices.indexOf(i);
445
+ })
446
+ .filter((i) => i >= 0)
447
+ .sort((a, b) => a - b);
448
+ const regions: Region[] = [];
449
+ for (const row of visible) {
450
+ const last = regions[regions.length - 1];
451
+ if (last != null && last.rows![1] === row - 1) {
452
+ last.rows = [last.rows![0], row];
453
+ continue;
454
+ }
455
+ regions.push({ rows: [row, row], cols: [colIndex, colIndex] });
456
+ }
457
+ return regions;
458
+ }
459
+
460
+ function sameRegions(a: Region[], b: Region[]): boolean {
461
+ if (a.length !== b.length) return false;
462
+ return a.every((r, i) => {
463
+ const s = b[i];
464
+ return (
465
+ r.rows?.[0] === s.rows?.[0] &&
466
+ r.rows?.[1] === s.rows?.[1] &&
467
+ r.cols?.[0] === s.cols?.[0] &&
468
+ r.cols?.[1] === s.cols?.[1]
469
+ );
470
+ });
471
+ }
472
+
473
+ /* -------------------------------------------------- selected data editor */
474
+
475
+ export interface SelectedDataEditorProps<T = any> extends Omit<
476
+ DataEditorProps<T>,
477
+ "dataSpec" | "data" | "onSave"
478
+ > {
479
+ /** The fields; defaults to the sheet's column spec. */
480
+ dataSpec?: FieldSpec[];
481
+ /** Persist the edited record. Defaults to the sheet's provider
482
+ * (`rowEditing.saveRows`), which refreshes the rows after. Without either,
483
+ * the editor is a viewer. */
484
+ onSave?: DataEditorProps<T>["onSave"];
485
+ /** Whether the ✕ in the title bar clears the selection (default). */
486
+ closeable?: boolean;
487
+ /** Show the sheet's actions, scoped to the record and its fields (off by
488
+ * default: the sheet's toolbar carries them). */
489
+ showActions?: boolean;
490
+ }
491
+
492
+ /**
493
+ * A `DataEditor` over the one row a sheet's or panel's selection holds: the
494
+ * immediate-edit counterpart of `SelectedRowEditor`, for a view (a `DataPanel`
495
+ * card list) whose edits are saved one record at a time rather than
496
+ * accumulated in the sheet's overlay. It keeps its own pending edits, and
497
+ * Save writes the record through the provider. Several rows selected, or
498
+ * none, it says so.
499
+ */
500
+ export function SelectedDataEditor<T = any>(props: SelectedDataEditorProps<T>) {
501
+ const {
502
+ dataSpec,
503
+ onSave,
504
+ closeable = true,
505
+ showActions = false,
506
+ panel = true,
507
+ title,
508
+ editable,
509
+ ...rest
510
+ } = props;
511
+ const store = useStoreAPI();
512
+ const columnSpec = useSelector((s) => s.columnSpec);
513
+ const saveRows = useSelector((s) => s.rowEditing?.saveRows);
514
+ const tableActions = sheetScope.useValue(
515
+ tableActionsAtom,
516
+ ) as TableAction<T>[];
517
+ const itemLabel = sheetScope.useValue(itemLabelAtom);
518
+ const setSelection = sheetScope.useSet(selectionAtom);
519
+ const { rows } = useSelectedRows<T>();
520
+ const count = rows.length;
521
+
522
+ if (count !== 1 || rows[0] == null) {
523
+ let noun = pluralize(itemLabel, count);
524
+ let stateTitle = `${count} ${noun} selected`;
525
+ if (count === 0) stateTitle = `No ${noun} selected`;
526
+ return h(
527
+ RowEditorFrame,
528
+ { panel, className: "row-editor-empty" },
529
+ h(NonIdealState, {
530
+ title: stateTitle,
531
+ className: "row-editor-non-ideal",
532
+ }),
533
+ );
534
+ }
535
+
536
+ let save = onSave;
537
+ if (save == null && saveRows != null) {
538
+ save = async (value: T) => {
539
+ await saveRows([value]);
540
+ };
541
+ }
542
+ const canEdit = (editable ?? true) && save != null;
543
+ let heading = title;
544
+ if (heading == null) {
545
+ heading = `Viewing 1 ${itemLabel}`;
546
+ if (canEdit) heading = `Editing 1 ${itemLabel}`;
547
+ }
548
+ let onClose: (() => void) | undefined;
549
+ if (closeable) onClose = () => setSelection([]);
550
+
551
+ return h(DataEditor<T>, {
552
+ ...rest,
553
+ dataSpec: dataSpec ?? columnSpec,
554
+ data: rows[0] as T,
555
+ onSave: save,
556
+ editable: canEdit,
557
+ panel,
558
+ title: heading,
559
+ onClose,
560
+ closeLabel: "Clear selection",
561
+ actions: tableActions,
562
+ showActions,
563
+ });
564
+ }
565
+
566
+ /* ------------------------------------------------------------ the toggle */
567
+
568
+ /** Whether the sheet's row editor is shown. Scoped to the sheet, like the
569
+ * rest of its state; `SelectedRowEditor` follows it unless `toggleable` is
570
+ * off. */
571
+ export const rowEditorOpenAtom = atom(true);
572
+
573
+ /** The row editor's open state, and a setter. */
574
+ export function useRowEditorOpen(): [boolean, (open: boolean) => void] {
575
+ const open = sheetScope.useValue(rowEditorOpenAtom);
576
+ const setOpen = sheetScope.useSet(rowEditorOpenAtom);
577
+ return [open, setOpen];
578
+ }
579
+
580
+ /** A toolbar toggle for the row editor, wherever it is mounted. */
581
+ export function ShowRowEditor({
582
+ label = "Row editor",
583
+ icon = "properties",
584
+ }: {
585
+ label?: string;
586
+ icon?: IconName;
587
+ }) {
588
+ const [open, setOpen] = useRowEditorOpen();
589
+ let title = "Show the row editor";
590
+ if (open) title = "Hide the row editor";
591
+ return h(Button, {
592
+ minimal: true,
593
+ small: true,
594
+ icon,
595
+ active: open,
596
+ text: label,
597
+ title,
598
+ "aria-pressed": open,
599
+ onClick: () => setOpen(!open),
600
+ });
601
+ }
602
+
603
+ /** `ShowRowEditor` as a sheet action: add it to `actions` and the toggle sits
604
+ * at the toolbar's right end, whatever is selected, editable or not. */
605
+ export const showRowEditorAction: TableAction = {
606
+ id: "show-row-editor",
607
+ name: "Row editor",
608
+ icon: "properties",
609
+ targets: [
610
+ RegionCardinality.CELLS,
611
+ RegionCardinality.FULL_ROWS,
612
+ RegionCardinality.FULL_COLUMNS,
613
+ RegionCardinality.FULL_TABLE,
614
+ ],
615
+ requiresEditable: false,
616
+ placement: "end",
617
+ render: () => h(ShowRowEditor),
618
+ };
619
+
620
+ export type { CellValidation };
@@ -91,9 +91,10 @@ export function ActionsToolbar<T>({
91
91
  // Reset); everything else is contextual and stays on the left. So the left
92
92
  // edge tracks the selection and the right edge is constant.
93
93
  const isGlobal = (a: TableAction<T>) =>
94
- (a.targets.includes(RegionCardinality.FULL_TABLE) ||
94
+ a.placement === "end" ||
95
+ ((a.targets.includes(RegionCardinality.FULL_TABLE) ||
95
96
  a.targets.includes("none" as any)) &&
96
- a.requiresEditable;
97
+ a.requiresEditable);
97
98
  const contextual = shownActions.filter((a) => !isGlobal(a));
98
99
  // Order the built-in global actions least→most impactful, left→right: reset
99
100
  // changes, then save. Any other global actions keep their natural order to
@@ -65,6 +65,31 @@
65
65
  align-items: center
66
66
  justify-content: center
67
67
 
68
+ // Crawlable next-page link: in the HTML for crawlers, out of the way for
69
+ // readers. Visually hidden rather than `display: none`, which crawlers may
70
+ // discount.
71
+ .crawl-next-link
72
+ position: absolute
73
+ width: 1px
74
+ height: 1px
75
+ overflow: hidden
76
+ clip-path: inset(50%)
77
+ white-space: nowrap
78
+
79
+ // Heads the content while the view starts mid-list (a `?after=` page).
80
+ .view-start-notice
81
+ flex: 0 0 auto
82
+ display: flex
83
+ align-items: center
84
+ justify-content: space-between
85
+ gap: var(--data-panel-item-gap, 8px)
86
+ padding: 6px 12px
87
+ margin-bottom: var(--data-panel-item-gap, 8px)
88
+ border-radius: 4px
89
+ background: var(--panel-background-color, rgba(128, 128, 128, 0.08))
90
+ color: var(--text-subtle, #738091)
91
+ font-size: 13px
92
+
68
93
  // Skeleton placeholder shown where a loading page's rows will land — sized like
69
94
  // a card so the body height stays stable (no blank flash, no footer ping). The
70
95
  // shimmer comes from Blueprint's `.bp6-skeleton`.