@macrostrat/data-sheet 4.8.0 → 4.9.1
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 +42 -0
- package/README.md +60 -19
- package/dist/actions/context.cjs +10 -1
- package/dist/actions/context.cjs.map +1 -1
- package/dist/actions/context.js +10 -1
- package/dist/actions/context.js.map +1 -1
- package/dist/actions/types.d.ts +4 -0
- package/dist/components/data-editor.cjs +527 -0
- package/dist/components/data-editor.cjs.map +1 -0
- package/dist/components/data-editor.d.ts +142 -0
- package/dist/components/data-editor.js +524 -0
- package/dist/components/data-editor.js.map +1 -0
- package/dist/components/editor-actions.cjs +167 -0
- package/dist/components/editor-actions.cjs.map +1 -0
- package/dist/components/editor-actions.d.ts +37 -0
- package/dist/components/editor-actions.js +165 -0
- package/dist/components/editor-actions.js.map +1 -0
- package/dist/components/editor-popup.cjs +11 -1
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +11 -1
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +3 -0
- package/dist/components/indicators/selection.cjs +10 -4
- package/dist/components/indicators/selection.cjs.map +1 -1
- package/dist/components/indicators/selection.js +10 -4
- package/dist/components/indicators/selection.js.map +1 -1
- package/dist/components/row-editor.cjs +351 -0
- package/dist/components/row-editor.cjs.map +1 -0
- package/dist/components/row-editor.d.ts +181 -0
- package/dist/components/row-editor.js +349 -0
- package/dist/components/row-editor.js.map +1 -0
- package/dist/components/row-editor.module.sass.cjs +51 -0
- package/dist/components/row-editor.module.sass.cjs.map +1 -0
- package/dist/components/row-editor.module.sass.js +49 -0
- package/dist/components/row-editor.module.sass.js.map +1 -0
- package/dist/components/toolbars/actions-toolbar.cjs +1 -1
- package/dist/components/toolbars/actions-toolbar.cjs.map +1 -1
- package/dist/components/toolbars/actions-toolbar.js +1 -1
- package/dist/components/toolbars/actions-toolbar.js.map +1 -1
- package/dist/data-sheet.css +208 -20
- package/dist/index.cjs +27 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +29 -2
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +14 -13
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/provider/column-spec.cjs +8 -1
- package/dist/provider/column-spec.cjs.map +1 -1
- package/dist/provider/column-spec.d.ts +105 -1
- package/dist/provider/column-spec.js +8 -1
- package/dist/provider/column-spec.js.map +1 -1
- package/dist/provider/core.cjs +16 -4
- package/dist/provider/core.cjs.map +1 -1
- package/dist/provider/core.d.ts +4 -0
- package/dist/provider/core.js +16 -4
- package/dist/provider/core.js.map +1 -1
- package/dist/provider/types.cjs.map +1 -1
- package/dist/provider/types.d.ts +6 -1
- package/dist/provider/types.js.map +1 -1
- package/dist/provider/zustand-store.cjs +55 -33
- package/dist/provider/zustand-store.cjs.map +1 -1
- package/dist/provider/zustand-store.js +44 -22
- package/dist/provider/zustand-store.js.map +1 -1
- package/dist/renderers/cell-renderer.cjs +15 -4
- package/dist/renderers/cell-renderer.cjs.map +1 -1
- package/dist/renderers/cell-renderer.js +13 -4
- package/dist/renderers/cell-renderer.js.map +1 -1
- package/dist/renderers/column-header-cell.cjs +12 -1
- package/dist/renderers/column-header-cell.cjs.map +1 -1
- package/dist/renderers/column-header-cell.js +11 -1
- package/dist/renderers/column-header-cell.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/actions/context.ts +13 -2
- package/src/actions/types.ts +5 -0
- package/src/components/data-editor.ts +741 -0
- package/src/components/editor-actions.ts +228 -0
- package/src/components/editor-popup.ts +16 -3
- package/src/components/index.ts +3 -0
- package/src/components/indicators/selection.ts +12 -4
- package/src/components/row-editor.module.sass +161 -0
- package/src/components/row-editor.ts +620 -0
- package/src/components/toolbars/actions-toolbar.ts +3 -2
- package/src/main.module.sass +19 -0
- package/src/provider/column-spec.ts +94 -16
- package/src/provider/core.ts +19 -4
- package/src/provider/types.ts +6 -1
- package/src/provider/zustand-store.ts +78 -26
- package/src/renderers/cell-renderer.ts +18 -3
- package/src/renderers/column-header-cell.ts +10 -0
- package/src/types.ts +5 -1
|
@@ -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
|
-
| "
|
|
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
|
-
/**
|
|
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
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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 {
|
package/src/provider/core.ts
CHANGED
|
@@ -402,7 +402,7 @@ export function DataSheetProviderInner<T>(
|
|
|
402
402
|
const rows = loadedData.filter((r) => r != null);
|
|
403
403
|
if (rows.length === 0) return;
|
|
404
404
|
derivedSpecFor.current = columnSpec;
|
|
405
|
-
storeAPI.setState({ columnSpec: columnSpec(rows) });
|
|
405
|
+
storeAPI.setState({ columnSpec: postprocessColumnSpec(columnSpec(rows)) });
|
|
406
406
|
}, [columnSpec, loadedData, storeAPI]);
|
|
407
407
|
|
|
408
408
|
const bumpRefresh = ctx.useSet(dataRefreshTokenAtom);
|
|
@@ -642,10 +642,25 @@ function itemCount(n: number, dataKind: string) {
|
|
|
642
642
|
}
|
|
643
643
|
|
|
644
644
|
export function pluralize(singularForm: string, n: number) {
|
|
645
|
-
const pluralForm = singularForm + "s";
|
|
646
|
-
if (n == 0) return `${pluralForm}`;
|
|
647
645
|
if (n == 1) return singularForm;
|
|
648
|
-
return
|
|
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;
|
|
649
664
|
}
|
|
650
665
|
|
|
651
666
|
export function capitalize(s: string) {
|
package/src/provider/types.ts
CHANGED
|
@@ -115,7 +115,10 @@ export interface DataSheetState<T> {
|
|
|
115
115
|
* suppressed — a function `columnSpec` derives it from the loaded rows
|
|
116
116
|
* instead (in `_DataSheet`). Set at init from the prop's shape. */
|
|
117
117
|
deferColumnSpec: boolean;
|
|
118
|
+
/** The cell a fill-drag copies from, while the drag lasts. */
|
|
118
119
|
fillValueBaseCell: FocusedCellCoordinates | null;
|
|
120
|
+
/** The overlay as it stood when the fill-drag began. */
|
|
121
|
+
fillBaseData: T[] | null;
|
|
119
122
|
focusedCell: FocusedCellCoordinates | null;
|
|
120
123
|
topLeftCell: FocusedCellCoordinates | null;
|
|
121
124
|
/**
|
|
@@ -147,7 +150,7 @@ export interface DataSheetState<T> {
|
|
|
147
150
|
/** Whether selecting a cell automatically activates its surface (opens an
|
|
148
151
|
* editor/detail panel and, for editors, focuses it). `"manual"` opens only
|
|
149
152
|
* on click. */
|
|
150
|
-
cellInteraction: "auto" | "manual";
|
|
153
|
+
cellInteraction: "auto" | "manual" | "second-click";
|
|
151
154
|
/** Runtime override of `cellInteraction`: while `false`, auto-activation is
|
|
152
155
|
* suppressed (set by pressing Escape, re-armed by clicking a cell) so the
|
|
153
156
|
* keyboard prioritizes navigation. */
|
|
@@ -204,6 +207,8 @@ export type StateUpdater<T> = T[] | ((state: T[]) => T[]);
|
|
|
204
207
|
export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
|
|
205
208
|
setSelection(selection: Region[]): void;
|
|
206
209
|
onDragValue(event: MouseEvent): void;
|
|
210
|
+
/** End a fill-drag: report the cells it wrote and disarm the anchor. */
|
|
211
|
+
finishFill(): void;
|
|
207
212
|
setUpdatedData(data: StateUpdater<T>): void;
|
|
208
213
|
onCellEdited(rowIndex: number, columnName: string, value: any): void;
|
|
209
214
|
onSelectionEdited(value: any): void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CellEdit,
|
|
2
3
|
DataSheetState,
|
|
3
4
|
DataSheetStore,
|
|
4
5
|
DataSheetStoreMain,
|
|
@@ -19,6 +20,7 @@ import {
|
|
|
19
20
|
getSelectionCardinality,
|
|
20
21
|
range,
|
|
21
22
|
} from "../actions";
|
|
23
|
+
import { isColumnWritable } from "./column-spec.ts";
|
|
22
24
|
|
|
23
25
|
/** Monotonic counter for synthetic ids on in-table-added rows. */
|
|
24
26
|
let _addedRowCounter = 0;
|
|
@@ -36,6 +38,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
36
38
|
editable: false,
|
|
37
39
|
selection: [],
|
|
38
40
|
fillValueBaseCell: null,
|
|
41
|
+
fillBaseData: null,
|
|
39
42
|
focusedCell: null,
|
|
40
43
|
topLeftCell: null,
|
|
41
44
|
initialized: false,
|
|
@@ -234,12 +237,37 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
234
237
|
});
|
|
235
238
|
},
|
|
236
239
|
onDragValue(event: MouseEvent) {
|
|
237
|
-
set((state) => {
|
|
238
|
-
return { fillValueBaseCell: state.focusedCell };
|
|
239
|
-
});
|
|
240
240
|
event.preventDefault();
|
|
241
|
+
const { focusedCell, updatedData } = get();
|
|
242
|
+
if (focusedCell == null) return;
|
|
243
|
+
// The fill is drawn from the overlay as it stood when the drag began, so
|
|
244
|
+
// shrinking the drag takes back the rows it leaves.
|
|
245
|
+
set({ fillValueBaseCell: focusedCell, fillBaseData: updatedData });
|
|
246
|
+
// A fill belongs to its drag alone, and the drag ends when the button is
|
|
247
|
+
// released, wherever that is. Left armed, the anchor would fill every
|
|
248
|
+
// later selection that isn't a single cell — a row picked by its header,
|
|
249
|
+
// say — with the anchor's value.
|
|
250
|
+
window.addEventListener("mouseup", () => get().finishFill(), {
|
|
251
|
+
once: true,
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
finishFill() {
|
|
255
|
+
const state = get();
|
|
256
|
+
if (state.fillValueBaseCell == null) return;
|
|
257
|
+
const { cells } = fillSelection(state, state.selection);
|
|
258
|
+
set({ fillValueBaseCell: null, fillBaseData: null });
|
|
259
|
+
// Reported like any other write, so a consumer that holds the edits
|
|
260
|
+
// (a controlled overlay) records the fill rather than having it drawn
|
|
261
|
+
// once and then overwritten.
|
|
262
|
+
if (cells.length > 0) get().onEdit?.({ type: "setCells", cells });
|
|
241
263
|
},
|
|
242
264
|
onCellEdited(rowIndex: number, columnName: string, value: any) {
|
|
265
|
+
// The column decides whether it takes writes (see `isColumnWritable`):
|
|
266
|
+
// a locked or derived column is refused here as well as in the cell
|
|
267
|
+
// renderer, so a programmatic caller — the row editor, say — can't
|
|
268
|
+
// write what the grid wouldn't let you type.
|
|
269
|
+
const targetColumn = get().columnSpec.find((c) => c.key === columnName);
|
|
270
|
+
if (!isColumnWritable(targetColumn, get().editable)) return;
|
|
243
271
|
set((state) => {
|
|
244
272
|
const { editable, updatedData, data } = state;
|
|
245
273
|
if (!editable) return {};
|
|
@@ -297,6 +325,8 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
297
325
|
if (rowIsDeleted) continue;
|
|
298
326
|
let vals = {};
|
|
299
327
|
for (const col of colRange) {
|
|
328
|
+
// A locked or derived column is never cleared
|
|
329
|
+
if (!isColumnWritable(columnSpec[col], state.editable)) continue;
|
|
300
330
|
const key = columnSpec[col].key;
|
|
301
331
|
const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
|
|
302
332
|
if (currentValue != null && currentValue !== "") {
|
|
@@ -309,6 +339,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
309
339
|
});
|
|
310
340
|
}
|
|
311
341
|
}
|
|
342
|
+
if (Object.keys(vals).length === 0) continue;
|
|
312
343
|
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
313
344
|
spec[row] = { [op]: vals };
|
|
314
345
|
}
|
|
@@ -344,7 +375,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
344
375
|
|
|
345
376
|
let spec = updateSelection(selection);
|
|
346
377
|
if (state.fillValueBaseCell != null) {
|
|
347
|
-
spec.updatedData =
|
|
378
|
+
spec.updatedData = fillSelection(state, selection).updatedData;
|
|
348
379
|
}
|
|
349
380
|
// A click (user selection) re-arms auto-activation and clears the
|
|
350
381
|
// travel direction so the editor opens with a default cursor.
|
|
@@ -355,8 +386,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
355
386
|
// target's click toggle — this avoids racing the selection event
|
|
356
387
|
// (which fires on mousedown, possibly more than once) and is what
|
|
357
388
|
// makes both re-focus-to-open and click-to-close reliable.
|
|
389
|
+
// In "second-click" mode a new selection only selects; the click on
|
|
390
|
+
// the already-selected cell opens it.
|
|
358
391
|
if (!selectionEquals(selection, state.selection)) {
|
|
359
|
-
spec.cellSurfaceOpen =
|
|
392
|
+
spec.cellSurfaceOpen =
|
|
393
|
+
state.cellInteraction !== "second-click" &&
|
|
394
|
+
singleFocusedCell(selection) != null;
|
|
360
395
|
}
|
|
361
396
|
return spec;
|
|
362
397
|
});
|
|
@@ -381,8 +416,13 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
381
416
|
for (const visibleRow of rowRange) {
|
|
382
417
|
// Selection indices are visible positions; map to the data row.
|
|
383
418
|
const row = toDataRowIndex(state, visibleRow);
|
|
419
|
+
// A deleted row takes no edits
|
|
420
|
+
if (state.rowStatus[row] === TableElementStatus.DELETED) continue;
|
|
384
421
|
let vals = {};
|
|
385
422
|
for (const col of colRange) {
|
|
423
|
+
// Only the columns that take writes — a selection spanning a
|
|
424
|
+
// locked or derived column writes around it
|
|
425
|
+
if (!isColumnWritable(columnSpec[col], editable)) continue;
|
|
386
426
|
const key = columnSpec[col].key;
|
|
387
427
|
vals[key] = value;
|
|
388
428
|
edits.push({
|
|
@@ -392,6 +432,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
|
392
432
|
row: state.data[row],
|
|
393
433
|
});
|
|
394
434
|
}
|
|
435
|
+
if (Object.keys(vals).length === 0) continue;
|
|
395
436
|
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
396
437
|
spec[row] = { [op]: vals };
|
|
397
438
|
}
|
|
@@ -494,9 +535,6 @@ export function updateSelection<T>(selection: Region[]) {
|
|
|
494
535
|
focusedCell,
|
|
495
536
|
topLeftCell: topLeftCell(selection),
|
|
496
537
|
};
|
|
497
|
-
if (focusedCell != null) {
|
|
498
|
-
spec.fillValueBaseCell = null;
|
|
499
|
-
}
|
|
500
538
|
return spec;
|
|
501
539
|
}
|
|
502
540
|
|
|
@@ -580,34 +618,42 @@ export function singleFocusedCell(
|
|
|
580
618
|
return topLeftCell(sel, true);
|
|
581
619
|
}
|
|
582
620
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
621
|
+
/** A fill-drag over `selection`: the anchor cell's value written down the
|
|
622
|
+
* anchor's column in every selected row. Drawn from the overlay as it stood
|
|
623
|
+
* when the drag began (`fillBaseData`), so the result is the fill alone, and
|
|
624
|
+
* returned with the cells it changed. */
|
|
625
|
+
function fillSelection<T>(
|
|
626
|
+
state: DataSheetStore<T>,
|
|
627
|
+
selection: Region[],
|
|
628
|
+
): { updatedData: T[]; cells: CellEdit[] } {
|
|
629
|
+
const { columnSpec, editable, fillValueBaseCell, data } = state;
|
|
630
|
+
const baseData = state.fillBaseData ?? state.updatedData;
|
|
631
|
+
const noFill = { updatedData: state.updatedData, cells: [] };
|
|
593
632
|
|
|
594
|
-
|
|
595
|
-
if (!editable || fillValueBaseCell == null) return updatedData;
|
|
633
|
+
if (!editable || fillValueBaseCell == null) return noFill;
|
|
596
634
|
const { col, row: baseVisibleRow } = fillValueBaseCell;
|
|
635
|
+
// Dragging from a locked or derived column fills nothing
|
|
636
|
+
if (!isColumnWritable(columnSpec[col], editable)) return noFill;
|
|
597
637
|
// The base cell and target rows are visible positions; map to data rows.
|
|
598
638
|
const baseRow = toDataRowIndex(state, baseVisibleRow);
|
|
599
639
|
const key = columnSpec[col].key;
|
|
600
|
-
const value =
|
|
640
|
+
const value = baseData[baseRow]?.[key] ?? data[baseRow]?.[key];
|
|
641
|
+
|
|
601
642
|
const spec = {};
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
for (const visibleRow of range(rows ?? [])) {
|
|
643
|
+
const cells: CellEdit[] = [];
|
|
644
|
+
for (const region of selection) {
|
|
645
|
+
for (const visibleRow of range(region.rows ?? [])) {
|
|
605
646
|
const row = toDataRowIndex(state, visibleRow);
|
|
606
|
-
|
|
647
|
+
if (row === baseRow) continue;
|
|
648
|
+
if (state.rowStatus[row] === TableElementStatus.DELETED) continue;
|
|
649
|
+
const current = baseData[row]?.[key] ?? data[row]?.[key];
|
|
650
|
+
if (valuesAreEquivalent(value, current)) continue;
|
|
651
|
+
let op = baseData[row] == null ? "$set" : "$merge";
|
|
607
652
|
spec[row] = { [op]: { [key]: value } };
|
|
653
|
+
cells.push({ rowIndex: row, column: key, value, row: data[row] });
|
|
608
654
|
}
|
|
609
655
|
}
|
|
610
|
-
return update(
|
|
656
|
+
return { updatedData: update(baseData, spec), cells };
|
|
611
657
|
}
|
|
612
658
|
|
|
613
659
|
/** Number of rows currently visible (post-filter/sort), or the full data
|
|
@@ -643,6 +689,12 @@ function valuesAreEquivalent(a: any, b: any): boolean {
|
|
|
643
689
|
const aBlank = a == null || a === "";
|
|
644
690
|
const bBlank = b == null || b === "";
|
|
645
691
|
if (aBlank || bBlank) return aBlank && bBlank;
|
|
692
|
+
// Structured values (an interval position, a list of lithologies) compare
|
|
693
|
+
// by content: as strings, every object is "[object Object]", so any edit
|
|
694
|
+
// to one — or to a list that keeps its length — read as no change
|
|
695
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
696
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
697
|
+
}
|
|
646
698
|
return String(a) === String(b);
|
|
647
699
|
}
|
|
648
700
|
|
|
@@ -13,6 +13,7 @@ import { singleFocusedCell } from "../provider";
|
|
|
13
13
|
import { Cell } from "@blueprintjs/table";
|
|
14
14
|
import { ctx, dragValueHandlerAtom, useSelector } from "../provider";
|
|
15
15
|
import { Intent } from "@blueprintjs/core";
|
|
16
|
+
import classNames from "classnames";
|
|
16
17
|
|
|
17
18
|
/** Two validations are equivalent if they convey the same thing — so a fresh
|
|
18
19
|
* `validateCell` result object doesn't force a re-render when nothing changed. */
|
|
@@ -111,7 +112,10 @@ export function basicCellRenderer<T>(
|
|
|
111
112
|
? null
|
|
112
113
|
: validateCell(col, value, row, dataRowIndex);
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
// A derived column is never writable, whatever `editable` says — see
|
|
116
|
+
// `isColumnWritable`, which every write path in the store applies too.
|
|
117
|
+
const editable =
|
|
118
|
+
(col.editable ?? state.editable) && !isDeleted && !col.derived;
|
|
115
119
|
const tableIsEditable = state.editable;
|
|
116
120
|
|
|
117
121
|
// topLeftCell stores visible row indices, so compare with the visible rowIndex
|
|
@@ -160,6 +164,10 @@ export function basicCellRenderer<T>(
|
|
|
160
164
|
const detailPresentation =
|
|
161
165
|
col.cellDetail != null ? (col.detailPresentation ?? "popover") : null;
|
|
162
166
|
|
|
167
|
+
// Derived values are drawn dimmed, so a reader can tell a record from a
|
|
168
|
+
// restatement of one. Applied on every path below.
|
|
169
|
+
const derivedClass = col.derived ? "derived-cell" : undefined;
|
|
170
|
+
|
|
163
171
|
// Fast path — any non-focused cell whose *unfocused* appearance is just the
|
|
164
172
|
// value viewer (a plain Blueprint `Cell`). That's everything except a custom
|
|
165
173
|
// `cellComponent` (its own markup on every cell) and an *inline* `cellDetail`
|
|
@@ -179,6 +187,7 @@ export function basicCellRenderer<T>(
|
|
|
179
187
|
loading,
|
|
180
188
|
//value,
|
|
181
189
|
style,
|
|
190
|
+
className: derivedClass,
|
|
182
191
|
interactive: false,
|
|
183
192
|
//disabled: tableIsEditable && !editable,
|
|
184
193
|
},
|
|
@@ -216,6 +225,7 @@ export function basicCellRenderer<T>(
|
|
|
216
225
|
// Build the detail context for the unified `cellDetail` surface.
|
|
217
226
|
const makeDetailCtx = (editableFlag: boolean): CellDetailContext<T> => ({
|
|
218
227
|
...cellContext,
|
|
228
|
+
surface: "cell",
|
|
219
229
|
editable: editableFlag,
|
|
220
230
|
onChange(v: any) {
|
|
221
231
|
if (editableFlag) onCellEdited(dataRowIndex, col.key, v);
|
|
@@ -255,6 +265,7 @@ export function basicCellRenderer<T>(
|
|
|
255
265
|
loading,
|
|
256
266
|
value,
|
|
257
267
|
style,
|
|
268
|
+
className: derivedClass,
|
|
258
269
|
disabled: tableIsEditable && !editable,
|
|
259
270
|
interactive: false,
|
|
260
271
|
...cellComponentProps,
|
|
@@ -308,7 +319,11 @@ export function basicCellRenderer<T>(
|
|
|
308
319
|
}
|
|
309
320
|
|
|
310
321
|
// Default: popover (same open/close machinery as editors and panels).
|
|
311
|
-
const panel = h(
|
|
322
|
+
const panel = h(
|
|
323
|
+
EditorPopup,
|
|
324
|
+
{ valueViewer: _renderedValue, placement: col.detailPlacement },
|
|
325
|
+
content,
|
|
326
|
+
);
|
|
312
327
|
return h(
|
|
313
328
|
_Cell,
|
|
314
329
|
{
|
|
@@ -385,7 +400,7 @@ export function basicCellRenderer<T>(
|
|
|
385
400
|
if (_dataEditor != null) {
|
|
386
401
|
cellContents = _dataEditor;
|
|
387
402
|
}
|
|
388
|
-
const className = "value-viewer-cell";
|
|
403
|
+
const className = classNames("value-viewer-cell", derivedClass);
|
|
389
404
|
|
|
390
405
|
return h(
|
|
391
406
|
_Cell,
|
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
} from "../postgrest-table";
|
|
17
17
|
import { buildActionContext } from "../actions";
|
|
18
18
|
import { useMemo } from "react";
|
|
19
|
+
import classNames from "classnames";
|
|
19
20
|
|
|
20
21
|
export interface ColumnActionsConfig {
|
|
21
22
|
activeSort?: ColumnSortEntry | null;
|
|
@@ -107,9 +108,18 @@ function ColumnHeaderName({ col, hasSortActive, hasFilterActive, activeSort }) {
|
|
|
107
108
|
textOverflow: "ellipsis",
|
|
108
109
|
whiteSpace: "nowrap",
|
|
109
110
|
},
|
|
111
|
+
className: classNames({ "derived-column-name": col.derived }),
|
|
110
112
|
},
|
|
111
113
|
col.name,
|
|
112
114
|
),
|
|
115
|
+
// A derived column says so in its header: the values below are computed
|
|
116
|
+
// and won't take an edit.
|
|
117
|
+
h.if(col.derived === true)(Icon, {
|
|
118
|
+
icon: "function",
|
|
119
|
+
size: 12,
|
|
120
|
+
title: "Derived — computed from other values, not editable",
|
|
121
|
+
style: { marginLeft: "4px", flexShrink: 0, opacity: 0.6 },
|
|
122
|
+
}),
|
|
113
123
|
h.if(hasSortActive || hasFilterActive)(
|
|
114
124
|
"span",
|
|
115
125
|
{
|
package/src/types.ts
CHANGED
|
@@ -143,8 +143,12 @@ export enum DataSheetDensity {
|
|
|
143
143
|
* until the next click.
|
|
144
144
|
* - `"manual"` (default when `autoFocusEditor` is `false`): the surface stays
|
|
145
145
|
* closed until the cell is clicked; arrow keys always navigate the table.
|
|
146
|
+
* - `"second-click"`: a click selects a cell and nothing more; a second click
|
|
147
|
+
* on it (or Enter / F2) opens its surface. For a sheet whose selection
|
|
148
|
+
* already feeds an editor of its own — a row editor beside the grid — so
|
|
149
|
+
* selecting doesn't also open a popover over the cells.
|
|
146
150
|
*/
|
|
147
|
-
export type CellInteraction = "auto" | "manual";
|
|
151
|
+
export type CellInteraction = "auto" | "manual" | "second-click";
|
|
148
152
|
|
|
149
153
|
export interface DataSheetRendererProps<T = any>
|
|
150
154
|
extends TableProps, DataViewSharedProps<T> {
|