@macrostrat/data-sheet 4.8.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 (95) hide show
  1. package/CHANGELOG.md +33 -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-sheet.css +208 -20
  41. package/dist/index.cjs +27 -0
  42. package/dist/index.cjs.map +1 -1
  43. package/dist/index.js +29 -2
  44. package/dist/index.js.map +1 -1
  45. package/dist/main.module.sass.cjs +14 -13
  46. package/dist/main.module.sass.cjs.map +1 -1
  47. package/dist/main.module.sass.js +14 -13
  48. package/dist/main.module.sass.js.map +1 -1
  49. package/dist/provider/column-spec.cjs +8 -1
  50. package/dist/provider/column-spec.cjs.map +1 -1
  51. package/dist/provider/column-spec.d.ts +105 -1
  52. package/dist/provider/column-spec.js +8 -1
  53. package/dist/provider/column-spec.js.map +1 -1
  54. package/dist/provider/core.cjs +16 -4
  55. package/dist/provider/core.cjs.map +1 -1
  56. package/dist/provider/core.d.ts +4 -0
  57. package/dist/provider/core.js +16 -4
  58. package/dist/provider/core.js.map +1 -1
  59. package/dist/provider/types.cjs.map +1 -1
  60. package/dist/provider/types.d.ts +1 -1
  61. package/dist/provider/types.js.map +1 -1
  62. package/dist/provider/zustand-store.cjs +26 -13
  63. package/dist/provider/zustand-store.cjs.map +1 -1
  64. package/dist/provider/zustand-store.js +14 -1
  65. package/dist/provider/zustand-store.js.map +1 -1
  66. package/dist/renderers/cell-renderer.cjs +15 -4
  67. package/dist/renderers/cell-renderer.cjs.map +1 -1
  68. package/dist/renderers/cell-renderer.js +13 -4
  69. package/dist/renderers/cell-renderer.js.map +1 -1
  70. package/dist/renderers/column-header-cell.cjs +12 -1
  71. package/dist/renderers/column-header-cell.cjs.map +1 -1
  72. package/dist/renderers/column-header-cell.js +11 -1
  73. package/dist/renderers/column-header-cell.js.map +1 -1
  74. package/dist/types.cjs.map +1 -1
  75. package/dist/types.d.ts +5 -1
  76. package/dist/types.js.map +1 -1
  77. package/package.json +2 -2
  78. package/src/actions/context.ts +13 -2
  79. package/src/actions/types.ts +5 -0
  80. package/src/components/data-editor.ts +741 -0
  81. package/src/components/editor-actions.ts +228 -0
  82. package/src/components/editor-popup.ts +16 -3
  83. package/src/components/index.ts +3 -0
  84. package/src/components/indicators/selection.ts +12 -4
  85. package/src/components/row-editor.module.sass +161 -0
  86. package/src/components/row-editor.ts +620 -0
  87. package/src/components/toolbars/actions-toolbar.ts +3 -2
  88. package/src/main.module.sass +19 -0
  89. package/src/provider/column-spec.ts +94 -16
  90. package/src/provider/core.ts +19 -4
  91. package/src/provider/types.ts +1 -1
  92. package/src/provider/zustand-store.ts +30 -1
  93. package/src/renderers/cell-renderer.ts +18 -3
  94. package/src/renderers/column-header-cell.ts +10 -0
  95. package/src/types.ts +5 -1
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Table actions in a form.
3
+ *
4
+ * A form's actions are a sheet's actions scoped to one row, one column or
5
+ * one cell — the single-selection cases — so a set of `TableAction`s serves
6
+ * both. In a form over one record:
7
+ *
8
+ * - **record actions** are those for a row selection (`FULL_ROWS`), run as if
9
+ * the record's row were selected, and shown in the editor's footer;
10
+ * - **field actions** are those for a cell or column selection (`CELLS`,
11
+ * `FULL_COLUMNS`), plus a field's own column `actions`, run as if that one
12
+ * cell were selected, and shown beside the field's label.
13
+ *
14
+ * Actions for the whole table (targeting `FULL_TABLE` — Save, Reset) are
15
+ * left out, as are live controls (`render`, the sort and filter widgets) and
16
+ * keyboard-bound ones (`hotkey`), which belong to a sheet. An action's
17
+ * `appliesTo` decides as it does in a sheet, against the scoped context.
18
+ *
19
+ * The context an action gets is built over the record as a one-row table:
20
+ * `onCellEdited` / `editCells` set the form's fields, `resetChanges` resets
21
+ * it, `getSelectedRows()` is the record as edited, and `saveRows` saves
22
+ * through the form's `onSave`. Nothing else in a sheet exists here, so the
23
+ * rest of the context is inert.
24
+ */
25
+ import hyper from "@macrostrat/hyper";
26
+ import { type ReactNode, useState } from "react";
27
+ import { Button, PopoverNext } from "@blueprintjs/core";
28
+ import { type Region, RegionCardinality } from "@blueprintjs/table";
29
+ import type { TableAction, TableActionContext } from "../actions";
30
+ import { computeSelectionShape } from "../actions/selection.ts";
31
+ import type { CellEdit, ColumnSpec } from "../provider";
32
+ import styles from "./row-editor.module.sass";
33
+
34
+ const h = hyper.styled(styles);
35
+
36
+ /** What a form offers actions to act on. */
37
+ export interface EditorActionTarget<T = any> {
38
+ dataSpec: ColumnSpec[];
39
+ /** The record as loaded. */
40
+ data: T;
41
+ /** The record with its pending edits. */
42
+ value: T;
43
+ edits: Partial<T>;
44
+ editable: boolean;
45
+ /** Set several fields at once. */
46
+ setFields: (patch: Record<string, any>) => void;
47
+ reset: () => void;
48
+ onSave?: (value: T, edits: Partial<T>) => void | Promise<void>;
49
+ }
50
+
51
+ export type EditorActionScope =
52
+ { kind: "record" } | { kind: "field"; columnKey: string };
53
+
54
+ /** The actions for a scope, and their context. */
55
+ export function editorActionsFor<T>(
56
+ actions: TableAction<T>[],
57
+ target: EditorActionTarget<T>,
58
+ scope: EditorActionScope,
59
+ ): { actions: TableAction<T>[]; ctx: TableActionContext<T> } {
60
+ const ctx = editorActionContext(target, scope);
61
+ let candidates = actions;
62
+ let cardinalities = [RegionCardinality.FULL_ROWS];
63
+ if (scope.kind === "field") {
64
+ const field = target.dataSpec.find((d) => d.key === scope.columnKey);
65
+ candidates = [...actions, ...((field?.actions as TableAction<T>[]) ?? [])];
66
+ cardinalities = [RegionCardinality.CELLS, RegionCardinality.FULL_COLUMNS];
67
+ }
68
+ const applicable = candidates.filter((action) => {
69
+ if (action.render != null || action.hotkey != null) return false;
70
+ if (action.targets.includes(RegionCardinality.FULL_TABLE)) return false;
71
+ if (!cardinalities.some((c) => action.targets.includes(c))) return false;
72
+ if (action.requiresEditable && !target.editable) return false;
73
+ return action.appliesTo?.(ctx) ?? true;
74
+ });
75
+ return { actions: applicable, ctx };
76
+ }
77
+
78
+ /** A table action's context over a form's record, scoped to the record or
79
+ * one of its fields. */
80
+ export function editorActionContext<T>(
81
+ target: EditorActionTarget<T>,
82
+ scope: EditorActionScope,
83
+ ): TableActionContext<T> {
84
+ const { dataSpec, data, value, edits, editable } = target;
85
+ let selection: Region[] = [{ rows: [0, 0] }];
86
+ let columnKey: string | null = null;
87
+ if (scope.kind === "field") {
88
+ columnKey = scope.columnKey;
89
+ const col = dataSpec.findIndex((d) => d.key === columnKey);
90
+ selection = [{ rows: [0, 0], cols: [col, col] }];
91
+ }
92
+ const setCell = (_row: number, key: string, next: any) =>
93
+ target.setFields({ [key]: next });
94
+ const noop = () => {};
95
+
96
+ let saveRows: TableActionContext<T>["saveRows"];
97
+ if (target.onSave != null) {
98
+ const onSave = target.onSave;
99
+ saveRows = async (rows) => {
100
+ const next = rows[0] ?? value;
101
+ const changed: any = {};
102
+ for (const key of Object.keys(next as any)) {
103
+ if ((next as any)[key] !== (data as any)?.[key]) {
104
+ changed[key] = (next as any)[key];
105
+ }
106
+ }
107
+ await onSave(next, changed);
108
+ };
109
+ }
110
+
111
+ return {
112
+ selection,
113
+ selectionCardinality: computeSelectionShape(selection).cardinality,
114
+ selectionShape: computeSelectionShape(selection),
115
+ columnKey,
116
+ rowIndex: 0,
117
+ cell: columnKey != null ? { rowIndex: 0, columnKey } : null,
118
+ data: [data],
119
+ updatedData: [edits as T],
120
+ rowStatus: [],
121
+ columnSpec: dataSpec,
122
+ editable,
123
+ canDeleteRows: false,
124
+ getSelectedRowIndices: () => [0],
125
+ getSelectedRows: () => [value],
126
+ getSelectedColumnKeys: () => {
127
+ if (columnKey != null) return [columnKey];
128
+ return dataSpec.map((d) => d.key);
129
+ },
130
+ saveRows,
131
+ onCellEdited: setCell,
132
+ editCells(cellEdits: CellEdit[]) {
133
+ const patch: Record<string, any> = {};
134
+ for (const e of cellEdits) {
135
+ patch[(e as any).columnKey ?? e.column] = e.value;
136
+ }
137
+ target.setFields(patch);
138
+ },
139
+ deleteSelectedRows: noop,
140
+ addRow: noop,
141
+ setUpdatedData(next: any) {
142
+ let rows = next;
143
+ if (typeof next === "function") rows = next([edits]);
144
+ target.reset();
145
+ target.setFields(rows?.[0] ?? {});
146
+ },
147
+ resetChanges: target.reset,
148
+ clearSelection: noop,
149
+ scrollToRow: noop,
150
+ setState: noop,
151
+ clipboardProxy: null,
152
+ setClipboardProxy: noop,
153
+ filteredRowIndices: null,
154
+ } as TableActionContext<T>;
155
+ }
156
+
157
+ /** A form's action: a button, or — with a `detailsForm` — a button opening
158
+ * its form and a Run. `iconOnly` for the small buttons beside a field. */
159
+ export function EditorActionButton<T>({
160
+ action,
161
+ ctx,
162
+ iconOnly = false,
163
+ }: {
164
+ action: TableAction<T, any>;
165
+ ctx: TableActionContext<T>;
166
+ iconOnly?: boolean;
167
+ }) {
168
+ const [state, setState] = useState(action.defaultState ?? null);
169
+ const [open, setOpen] = useState(false);
170
+ let disabled = false;
171
+ if (typeof action.disabled === "boolean") disabled = action.disabled;
172
+ if (typeof action.disabled === "function") disabled = action.disabled(ctx);
173
+
174
+ const run = async (config?: any) => {
175
+ try {
176
+ await action.run?.(ctx, config);
177
+ } catch (err) {
178
+ console.error(err);
179
+ }
180
+ };
181
+
182
+ let text: ReactNode = action.name;
183
+ if (iconOnly && action.icon != null) text = undefined;
184
+ const button = h(Button, {
185
+ small: true,
186
+ minimal: true,
187
+ icon: action.icon,
188
+ intent: action.intent,
189
+ text,
190
+ title: action.description ?? action.name,
191
+ disabled,
192
+ className: "editor-action",
193
+ onClick() {
194
+ if (action.detailsForm != null) {
195
+ setOpen(!open);
196
+ return;
197
+ }
198
+ run();
199
+ },
200
+ });
201
+ if (action.detailsForm == null) return button;
202
+
203
+ let ready = true;
204
+ if (action.isReady != null) ready = action.isReady(state);
205
+ return h(
206
+ PopoverNext,
207
+ {
208
+ isOpen: open,
209
+ onClose: () => setOpen(false),
210
+ placement: "bottom-start",
211
+ content: h("div.editor-action-form", [
212
+ h(action.detailsForm as any, { state, setState }),
213
+ h(Button, {
214
+ intent: action.intent ?? "primary",
215
+ icon: "play",
216
+ text: "Run",
217
+ fill: true,
218
+ disabled: !ready,
219
+ onClick() {
220
+ run(state);
221
+ setOpen(false);
222
+ },
223
+ }),
224
+ ]),
225
+ },
226
+ button,
227
+ );
228
+ }
@@ -5,8 +5,8 @@ import { useCallback, useRef } from "react";
5
5
  import { useSelector } from "../provider";
6
6
 
7
7
  export function EditorPopup(props) {
8
- const { children, targetClassName, valueViewer, placement = "right-start" } =
9
- props;
8
+ const { children, targetClassName, valueViewer } = props;
9
+ const placement = props.placement ?? "right-start";
10
10
 
11
11
  // Open state is owned by the store (not local), so navigation, clicks, and
12
12
  // the Escape handler all agree on whether the focused cell's surface is
@@ -49,7 +49,14 @@ export function EditorPopup(props) {
49
49
  evt.preventDefault();
50
50
  return;
51
51
  }
52
- // Climb over the interaction barrier to propagate the key event to the table
52
+ // Typing in one of the surface's own fields stays there: the
53
+ // sheet's hotkeys run inside inputs, so a Backspace in a search
54
+ // box would otherwise clear the cell, and Enter move off it.
55
+ if (isTextEntry(evt.target)) {
56
+ evt.stopPropagation();
57
+ return;
58
+ }
59
+ // Otherwise climb over the interaction barrier to propagate the key event to the table
53
60
  },
54
61
  },
55
62
  h(ErrorBoundary, null, children),
@@ -90,3 +97,9 @@ export function EditorPopup(props) {
90
97
  ),
91
98
  );
92
99
  }
100
+
101
+ function isTextEntry(target: EventTarget | null): boolean {
102
+ if (!(target instanceof HTMLElement)) return false;
103
+ if (target.isContentEditable) return true;
104
+ return target.tagName === "INPUT" || target.tagName === "TEXTAREA";
105
+ }
@@ -4,3 +4,6 @@ export * from "./colors";
4
4
  export * from "./text-areas";
5
5
  export * from "./indicators";
6
6
  export * from "./toolbars";
7
+ export * from "./data-editor";
8
+ export * from "./editor-actions";
9
+ export * from "./row-editor";
@@ -35,12 +35,20 @@ const selectionTitleAtom = atom<string | null>((get) => {
35
35
  }
36
36
  case RegionCardinality.FULL_ROWS:
37
37
  return itemCount(sh.rows, itemName);
38
- case RegionCardinality.CELLS:
39
- if (sh.columns == 1 || sh.rows == 1) {
40
- const nCells = Math.max(sh.columns, sh.rows);
41
- return itemCount(nCells, "cell");
38
+ case RegionCardinality.CELLS: {
39
+ // Cells in one column are named for what the column holds: "1
40
+ // lithology", "3 lithologies"
41
+ if (sh.columns == 1) {
42
+ const columnSpec = get(columnSpecAtom);
43
+ const columnKey = getSelectedColumnKeys(sel, columnSpec)[0];
44
+ const col = columnSpec.find((c) => c.key === columnKey);
45
+ let noun = "cell";
46
+ if (col != null) noun = col.cellLabel ?? col.name.toLowerCase();
47
+ return itemCount(sh.rows, noun);
42
48
  }
49
+ if (sh.rows == 1) return itemCount(sh.columns, "cell");
43
50
  return `${sh.columns}×${sh.rows} cells`;
51
+ }
44
52
  case RegionCardinality.FULL_TABLE:
45
53
  return null;
46
54
  default:
@@ -0,0 +1,161 @@
1
+ // The row editor: one row's fields as a form. Labels carry a marker for what
2
+ // kind of value a field holds (derived, required) and a revert button once
3
+ // it has been edited; the value or its editor sits beneath.
4
+
5
+ .row-editor
6
+ display: flex
7
+ flex-direction: column
8
+ min-width: 0
9
+ min-height: 0
10
+
11
+ // A field's actions, after its label
12
+ .field-actions
13
+ display: inline-flex
14
+ gap: 0.1em
15
+ margin-left: 0.2em
16
+ :global(.bp6-button)
17
+ min-height: 0
18
+ min-width: 0
19
+ padding: 0 3px
20
+
21
+ .editor-action-form
22
+ display: flex
23
+ flex-direction: column
24
+ gap: 0.5em
25
+ padding: 0.6em
26
+ min-width: 14em
27
+
28
+ // The data editor's footer: its actions, then Reset and Save
29
+ .data-editor-footer
30
+ flex: none
31
+ display: flex
32
+ align-items: center
33
+ gap: 0.3em
34
+ padding-top: 0.5em
35
+
36
+ .data-editor-footer-spacer
37
+ flex: 1
38
+
39
+ .row-editor.panel .data-editor-footer
40
+ padding: 0.4em 0.6em
41
+ border-top: 1px solid var(--pz-border-color, rgba(128, 128, 128, 0.3))
42
+
43
+ .row-editor-body
44
+ display: flex
45
+ flex-direction: column
46
+ gap: 0.25em
47
+ min-width: 0
48
+
49
+ // As a panel: bordered, a title bar over a body that scrolls on its own
50
+ .row-editor.panel
51
+ max-height: 100%
52
+ border: 1px solid var(--pz-border-color, rgba(128, 128, 128, 0.3))
53
+ border-radius: 4px
54
+ background: var(--panel-background-color, var(--background-color, transparent))
55
+ overflow: hidden
56
+ .row-editor-body
57
+ flex: 1 1 auto
58
+ min-height: 0
59
+ overflow-y: auto
60
+ padding: 0.75em 0.9em
61
+
62
+ // The title bar is the selection tag (minimal, large, primary) at full
63
+ // width, squared off to the panel's top edge
64
+ .row-editor-title-bar
65
+ flex: none
66
+ min-height: 36px !important
67
+ padding-left: 0.9em !important
68
+ border-radius: 0 !important
69
+ font-weight: 600
70
+ box-shadow: inset 0 -1px 0 var(--pz-border-color, rgba(128, 128, 128, 0.3))
71
+
72
+ .row-editor-fields
73
+ display: flex
74
+ flex-direction: column
75
+ gap: 0.1em
76
+
77
+ .row-editor-field
78
+ margin-bottom: 0.4em
79
+ min-width: 0
80
+ :global(.bp6-label)
81
+ margin-bottom: 0.15em
82
+ &.edited .field-name
83
+ color: var(--pz-intent-success, #0f9960)
84
+ &.derived .field-value
85
+ color: var(--pz-text-subtle-color, var(--secondary-color))
86
+ font-style: italic
87
+ &.read-only :global(.bp6-form-content)
88
+ padding: 0.15em 0
89
+
90
+ .row-editor.inline .row-editor-field
91
+ align-items: baseline
92
+
93
+ .field-label
94
+ display: inline-flex
95
+ align-items: center
96
+ gap: 0.35em
97
+ min-height: 1.4em
98
+
99
+ .field-name
100
+ font-weight: 600
101
+ font-size: 0.85em
102
+ color: var(--secondary-color)
103
+
104
+ .field-marker
105
+ font-size: 0.7em
106
+ line-height: 1
107
+ padding: 0 0.4em
108
+ min-height: 1.4em
109
+
110
+ .required-marker
111
+ color: var(--pz-intent-danger, #cd4246)
112
+
113
+ .reset-field
114
+ margin-left: -0.15em
115
+ min-height: 0
116
+ :global(.bp6-icon)
117
+ color: var(--pz-text-subtle-color, var(--secondary-color))
118
+
119
+ .field-value
120
+ display: inline-block
121
+ min-width: 0
122
+ overflow-wrap: anywhere
123
+ &.empty
124
+ color: var(--pz-text-subtle-color, var(--secondary-color))
125
+
126
+ .field-surface.cell-detail
127
+ min-width: 0
128
+
129
+ .row-editor-non-ideal
130
+ padding: 1.5em 0
131
+
132
+ // A cell selection's columns are the focus: marked as the table marks a
133
+ // selected region — a light fill and a full outline in the selection colour —
134
+ // and the only writable fields; the rest of the row is context.
135
+ .row-editor-field.selected
136
+ // Padding for the outline, taken back by margins, so selecting a field
137
+ // moves nothing (fields sit 0.4em apart)
138
+ margin: -0.3em -0.4em 0.1em
139
+ padding: 0.3em 0.4em
140
+ border-radius: 3px
141
+ // A faint fill — enough to say "selected" without tinting what the field
142
+ // shows; the outline carries the selection
143
+ background: color-mix(in srgb, var(--pz-intent-primary, #2d72d2) 4%, transparent)
144
+ box-shadow: inset 0 0 0 1px var(--pz-intent-primary, #2d72d2)
145
+ // Tags keep an edge against the selection's fill
146
+ --tag-outline: 0 0 0 1px rgba(128, 128, 128, 0.35)
147
+ .field-name
148
+ color: var(--pz-intent-primary, #2d72d2)
149
+
150
+ .mixed-values
151
+ color: var(--pz-text-subtle-color, var(--secondary-color))
152
+ font-style: italic
153
+
154
+ .selection-summary
155
+ font-size: 0.85em
156
+ color: var(--pz-text-subtle-color, var(--secondary-color))
157
+ margin-bottom: 0.5em
158
+
159
+ // Takes no space: it only catches a click on its field
160
+ .row-editor-field-target
161
+ display: contents