@macrostrat/data-sheet 3.1.0 → 4.0.0-dev1

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 (184) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/actions/builtin.cjs +56 -0
  3. package/dist/actions/builtin.cjs.map +1 -0
  4. package/dist/actions/builtin.d.ts +15 -0
  5. package/dist/actions/builtin.js +56 -0
  6. package/dist/actions/builtin.js.map +1 -0
  7. package/dist/actions/clipboard.cjs +250 -0
  8. package/dist/actions/clipboard.cjs.map +1 -0
  9. package/dist/actions/clipboard.d.ts +42 -0
  10. package/dist/actions/clipboard.js +250 -0
  11. package/dist/actions/clipboard.js.map +1 -0
  12. package/dist/actions/filters.cjs +123 -0
  13. package/dist/actions/filters.cjs.map +1 -0
  14. package/dist/actions/filters.d.ts +6 -0
  15. package/dist/actions/filters.js +123 -0
  16. package/dist/actions/filters.js.map +1 -0
  17. package/dist/actions/index.d.ts +6 -0
  18. package/dist/actions/selection.cjs +194 -0
  19. package/dist/actions/selection.cjs.map +1 -0
  20. package/dist/actions/selection.d.ts +40 -0
  21. package/dist/actions/selection.js +192 -0
  22. package/dist/actions/selection.js.map +1 -0
  23. package/dist/actions/toolbar.cjs +171 -0
  24. package/dist/actions/toolbar.cjs.map +1 -0
  25. package/dist/actions/toolbar.d.ts +10 -0
  26. package/dist/actions/toolbar.js +171 -0
  27. package/dist/actions/toolbar.js.map +1 -0
  28. package/dist/actions/toolbar.module.sass.cjs +22 -0
  29. package/dist/actions/toolbar.module.sass.cjs.map +1 -0
  30. package/dist/actions/toolbar.module.sass.js +20 -0
  31. package/dist/actions/toolbar.module.sass.js.map +1 -0
  32. package/dist/actions/types.d.ts +143 -0
  33. package/dist/cell-renderer.cjs +44 -41
  34. package/dist/cell-renderer.cjs.map +1 -1
  35. package/dist/cell-renderer.d.ts +1 -6
  36. package/dist/cell-renderer.js +45 -42
  37. package/dist/cell-renderer.js.map +1 -1
  38. package/dist/components/actions.module.sass.cjs +3 -3
  39. package/dist/components/actions.module.sass.js +2 -2
  40. package/dist/components/colors.cjs +20 -16
  41. package/dist/components/colors.cjs.map +1 -1
  42. package/dist/components/colors.d.ts +4 -3
  43. package/dist/components/colors.js +20 -16
  44. package/dist/components/colors.js.map +1 -1
  45. package/dist/components/editor-popup.cjs +4 -3
  46. package/dist/components/editor-popup.cjs.map +1 -1
  47. package/dist/components/editor-popup.js +4 -3
  48. package/dist/components/editor-popup.js.map +1 -1
  49. package/dist/components/index.d.ts +1 -0
  50. package/dist/components/main.module.sass.cjs +12 -12
  51. package/dist/components/main.module.sass.js +11 -11
  52. package/dist/components/text-areas.cjs +23 -18
  53. package/dist/components/text-areas.cjs.map +1 -1
  54. package/dist/components/text-areas.d.ts +8 -4
  55. package/dist/components/text-areas.js +20 -13
  56. package/dist/components/text-areas.js.map +1 -1
  57. package/dist/components/view-info.cjs +18 -0
  58. package/dist/components/view-info.cjs.map +1 -0
  59. package/dist/components/view-info.d.ts +1 -0
  60. package/dist/components/view-info.js +18 -0
  61. package/dist/components/view-info.js.map +1 -0
  62. package/dist/core.cjs +184 -156
  63. package/dist/core.cjs.map +1 -1
  64. package/dist/core.d.ts +14 -1
  65. package/dist/core.js +187 -157
  66. package/dist/core.js.map +1 -1
  67. package/dist/data-sheet.css +181 -43
  68. package/dist/index.cjs +43 -13
  69. package/dist/index.cjs.map +1 -1
  70. package/dist/index.d.ts +1 -0
  71. package/dist/index.js +43 -10
  72. package/dist/index.js.map +1 -1
  73. package/dist/main.module.sass.cjs +15 -14
  74. package/dist/main.module.sass.cjs.map +1 -1
  75. package/dist/main.module.sass.js +14 -13
  76. package/dist/main.module.sass.js.map +1 -1
  77. package/dist/notifications.cjs +19 -0
  78. package/dist/notifications.cjs.map +1 -0
  79. package/dist/notifications.d.ts +9 -0
  80. package/dist/notifications.js +19 -0
  81. package/dist/notifications.js.map +1 -0
  82. package/dist/postgrest-table/data-loaders.cjs +181 -104
  83. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  84. package/dist/postgrest-table/data-loaders.d.ts +52 -28
  85. package/dist/postgrest-table/data-loaders.js +181 -104
  86. package/dist/postgrest-table/data-loaders.js.map +1 -1
  87. package/dist/postgrest-table/index.cjs +205 -79
  88. package/dist/postgrest-table/index.cjs.map +1 -1
  89. package/dist/postgrest-table/index.d.ts +13 -16
  90. package/dist/postgrest-table/index.js +186 -58
  91. package/dist/postgrest-table/index.js.map +1 -1
  92. package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
  93. package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
  94. package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
  95. package/dist/postgrest-table/lazy-loader-table.js +17 -14
  96. package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
  97. package/dist/postgrest-table/loading-utils.cjs +16 -0
  98. package/dist/postgrest-table/loading-utils.cjs.map +1 -0
  99. package/dist/postgrest-table/loading-utils.d.ts +13 -0
  100. package/dist/postgrest-table/loading-utils.js +16 -0
  101. package/dist/postgrest-table/loading-utils.js.map +1 -0
  102. package/dist/postgrest-table/main.module.sass.cjs +18 -13
  103. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  104. package/dist/postgrest-table/main.module.sass.js +18 -13
  105. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  106. package/dist/provider.cjs +58 -26
  107. package/dist/provider.cjs.map +1 -1
  108. package/dist/provider.js +57 -23
  109. package/dist/provider.js.map +1 -1
  110. package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
  111. package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
  112. package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
  113. package/dist/renderers/column-header-cell-postgrest.js +216 -0
  114. package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
  115. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
  116. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
  117. package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
  118. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
  119. package/dist/renderers/column-header-cell.cjs +103 -0
  120. package/dist/renderers/column-header-cell.cjs.map +1 -0
  121. package/dist/renderers/column-header-cell.d.ts +30 -0
  122. package/dist/renderers/column-header-cell.js +101 -0
  123. package/dist/renderers/column-header-cell.js.map +1 -0
  124. package/dist/renderers/index.d.ts +2 -0
  125. package/dist/table-updates/data-parameter.d.ts +14 -0
  126. package/dist/table-updates/filter.d.ts +16 -0
  127. package/dist/table-updates/index.d.ts +19 -0
  128. package/dist/types.cjs +9 -0
  129. package/dist/types.cjs.map +1 -0
  130. package/dist/types.d.ts +58 -4
  131. package/dist/types.js +9 -0
  132. package/dist/types.js.map +1 -0
  133. package/dist/utils/column-spec.cjs +9 -1
  134. package/dist/utils/column-spec.cjs.map +1 -1
  135. package/dist/utils/column-spec.d.ts +21 -0
  136. package/dist/utils/column-spec.js +9 -1
  137. package/dist/utils/column-spec.js.map +1 -1
  138. package/dist/utils/key-handling.cjs +141 -52
  139. package/dist/utils/key-handling.cjs.map +1 -1
  140. package/dist/utils/key-handling.d.ts +2 -3
  141. package/dist/utils/key-handling.js +142 -53
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/zustand-store.cjs +216 -51
  144. package/dist/zustand-store.cjs.map +1 -1
  145. package/dist/zustand-store.js +197 -32
  146. package/dist/zustand-store.js.map +1 -1
  147. package/package.json +3 -3
  148. package/src/actions/builtin.ts +65 -0
  149. package/src/actions/clipboard.ts +350 -0
  150. package/src/actions/filters.ts +150 -0
  151. package/src/actions/index.ts +6 -0
  152. package/src/actions/selection.ts +271 -0
  153. package/src/actions/toolbar.module.sass +30 -0
  154. package/src/actions/toolbar.ts +212 -0
  155. package/src/actions/types.ts +159 -0
  156. package/src/cell-renderer.ts +55 -51
  157. package/src/components/colors.ts +22 -19
  158. package/src/components/editor-popup.ts +3 -2
  159. package/src/components/index.ts +1 -0
  160. package/src/components/main.module.sass +1 -0
  161. package/src/components/text-areas.ts +34 -19
  162. package/src/components/view-info.ts +17 -0
  163. package/src/core.ts +243 -170
  164. package/src/index.ts +1 -0
  165. package/src/main.module.sass +14 -2
  166. package/src/notifications.ts +22 -0
  167. package/src/postgrest-table/data-loaders.ts +300 -201
  168. package/src/postgrest-table/index.ts +258 -84
  169. package/src/postgrest-table/lazy-loader-table.ts +18 -17
  170. package/src/postgrest-table/loading-utils.ts +49 -0
  171. package/src/postgrest-table/main.module.sass +16 -0
  172. package/src/provider.ts +63 -20
  173. package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
  174. package/src/renderers/column-header-cell-postgrest.ts +282 -0
  175. package/src/renderers/column-header-cell.ts +388 -0
  176. package/src/renderers/index.ts +2 -0
  177. package/src/table-updates/data-parameter.ts +44 -0
  178. package/src/table-updates/filter.ts +124 -0
  179. package/src/table-updates/index.ts +239 -0
  180. package/src/types.ts +58 -3
  181. package/src/utils/column-spec.ts +47 -0
  182. package/src/utils/key-handling.ts +181 -72
  183. package/src/zustand-store.ts +241 -36
  184. package/dist/provider.d.ts +0 -15
@@ -0,0 +1,271 @@
1
+ import { type Region, RegionCardinality } from "@blueprintjs/table";
2
+ import type { ColumnSpec } from "../utils";
3
+ import type {
4
+ ActiveFilterEntry,
5
+ CellEdit,
6
+ ColumnSort,
7
+ SelectionCardinality,
8
+ TableAction,
9
+ TableActionContext,
10
+ TableFilter,
11
+ } from "./types";
12
+ import type { DataSheetStore } from "../types";
13
+ import update from "immutability-helper";
14
+
15
+ /** Derive the selection cardinality from the current set of selected regions.
16
+ * Returns "none" when there is no active selection. */
17
+ export function getSelectionCardinality(
18
+ regions: Region[],
19
+ ): RegionCardinality | null {
20
+ if (regions == null || regions.length === 0) return null;
21
+ const region = regions[0];
22
+ const hasRows = region.rows != null;
23
+ const hasCols = region.cols != null;
24
+ if (hasRows && hasCols) return RegionCardinality.CELLS;
25
+ if (hasRows && !hasCols) return RegionCardinality.FULL_ROWS;
26
+ if (!hasRows && hasCols) return RegionCardinality.FULL_COLUMNS;
27
+ return RegionCardinality.FULL_TABLE;
28
+ }
29
+
30
+ /** Filter actions to those applicable for the current selection cardinality
31
+ * and edit mode. */
32
+ export function getApplicableActions<T>(
33
+ actions: TableAction<T>[],
34
+ cardinality: RegionCardinality,
35
+ editable: boolean,
36
+ ): TableAction<T>[] {
37
+ return actions.filter((action) => {
38
+ if (action.requiresEditable !== false && !editable) return false;
39
+ return action.targets.includes(cardinality);
40
+ });
41
+ }
42
+
43
+ /** Extract concrete row indices from a set of selected regions.
44
+ * When `filteredRowIndices` is provided, visible indices are mapped
45
+ * to actual data indices. */
46
+ export function getSelectedRowIndices(
47
+ regions: Region[],
48
+ filteredRowIndices?: number[] | null,
49
+ ): number[] {
50
+ if (regions == null || regions.length === 0) return [];
51
+ const indices = new Set<number>();
52
+ for (const region of regions) {
53
+ if (region.rows == null) continue;
54
+ const [start, end] = region.rows;
55
+ for (let i = start; i <= end; i++) {
56
+ indices.add(i);
57
+ }
58
+ }
59
+ const sorted = Array.from(indices).sort((a, b) => a - b);
60
+ if (filteredRowIndices == null) return sorted;
61
+ return sorted
62
+ .map((i) => filteredRowIndices[i])
63
+ .filter((i) => i != null);
64
+ }
65
+
66
+ /** Extract column keys covered by a set of selected regions. */
67
+ export function getSelectedColumnKeys(
68
+ regions: Region[],
69
+ columnSpec: ColumnSpec[],
70
+ ): string[] {
71
+ if (regions == null || regions.length === 0) return [];
72
+ const keys = new Set<string>();
73
+ for (const region of regions) {
74
+ if (region.cols == null) {
75
+ // Full-row or full-table selection covers all columns
76
+ for (const col of columnSpec) {
77
+ keys.add(col.key);
78
+ }
79
+ } else {
80
+ const [start, end] = region.cols;
81
+ for (let i = start; i <= end; i++) {
82
+ if (columnSpec[i]) keys.add(columnSpec[i].key);
83
+ }
84
+ }
85
+ }
86
+ return Array.from(keys);
87
+ }
88
+
89
+ /** Construct a `TableActionContext` from the current store state.
90
+ * Call this at action-run time (not render time) to ensure
91
+ * the context reflects the latest state.
92
+ * @param setState - Optional store setState for direct mutation. Omit for
93
+ * read-only contexts (e.g., disabled checks). */
94
+ export function buildActionContext<T>(
95
+ state: DataSheetStore<T>,
96
+ setState: (partial: Record<string, any>) => void = () => {},
97
+ ): TableActionContext<T> {
98
+ // Lazy-compute filteredRowIndices to avoid cost during disabled checks
99
+ let _filteredRowIndices: number[] | null | undefined = undefined;
100
+ function getFilteredRowIndices(): number[] | null {
101
+ if (_filteredRowIndices === undefined) {
102
+ _filteredRowIndices = computeFilteredRowIndices(
103
+ state.data,
104
+ state.updatedData,
105
+ state.activeFilters,
106
+ );
107
+ }
108
+ return _filteredRowIndices;
109
+ }
110
+
111
+ return {
112
+ selection: state.selection,
113
+ selectionCardinality: getSelectionCardinality(state.selection),
114
+ data: state.data,
115
+ updatedData: state.updatedData,
116
+ rowStatus: state.rowStatus,
117
+ columnSpec: state.columnSpec,
118
+ editable: state.editable,
119
+ getSelectedRowIndices: () =>
120
+ getSelectedRowIndices(state.selection, getFilteredRowIndices()),
121
+ getSelectedColumnKeys: () =>
122
+ getSelectedColumnKeys(state.selection, state.columnSpec),
123
+ onCellEdited: state.onCellEdited,
124
+ editCells(edits: CellEdit[]) {
125
+ state.setUpdatedData((updatedData: T[]) => {
126
+ const spec: Record<number, any> = {};
127
+ for (const { rowIndex, columnKey, value } of edits) {
128
+ if (spec[rowIndex] == null) {
129
+ const op = updatedData[rowIndex] != null ? "$merge" : "$set";
130
+ spec[rowIndex] = { [op]: {} };
131
+ }
132
+ const opKey = Object.keys(spec[rowIndex])[0];
133
+ spec[rowIndex][opKey][columnKey] = value;
134
+ }
135
+ return update(updatedData, spec);
136
+ });
137
+ },
138
+ deleteSelectedRows: state.deleteSelectedRows,
139
+ addRow: state.addRow,
140
+ setUpdatedData: state.setUpdatedData,
141
+ resetChanges: state.resetChanges,
142
+ clearSelection: state.clearSelection,
143
+ scrollToRow: state.scrollToRow,
144
+ setState,
145
+ clipboardProxy: state.clipboardProxy,
146
+ setClipboardProxy(proxy) {
147
+ state.setClipboardProxy(proxy);
148
+ },
149
+ get filteredRowIndices() {
150
+ return getFilteredRowIndices();
151
+ },
152
+ };
153
+ }
154
+
155
+ /** Compute which data row indices are visible given the active filters.
156
+ * Returns `null` when no filters are active (all rows visible). */
157
+ export function computeFilteredRowIndices<T>(
158
+ data: T[],
159
+ updatedData: T[],
160
+ activeFilters: Map<string, { filter: any; state: any }>,
161
+ ): number[] | null {
162
+ if (activeFilters == null || activeFilters.size === 0) return null;
163
+ const numRows = Math.max(data.length, updatedData.length);
164
+ const indices: number[] = [];
165
+ for (let i = 0; i < numRows; i++) {
166
+ const baseRow = data[i];
167
+ const overlay = updatedData[i];
168
+ const row = overlay != null ? { ...baseRow, ...overlay } : baseRow;
169
+ if (row == null) continue;
170
+ let passes = true;
171
+ for (const [, { filter, state }] of activeFilters) {
172
+ if (!filter.predicate(row, state)) {
173
+ passes = false;
174
+ break;
175
+ }
176
+ }
177
+ if (passes) indices.push(i);
178
+ }
179
+ return indices;
180
+ }
181
+
182
+ /** Merge global actions with column-specific actions from the selected columns.
183
+ * Column-specific actions are extracted from `columnSpec[i].actions` for
184
+ * each column in the current selection. */
185
+ export function mergeColumnActions<T>(
186
+ globalActions: TableAction<T>[],
187
+ columnSpec: ColumnSpec[],
188
+ selection: Region[],
189
+ ): TableAction<T>[] {
190
+ const columnKeys = getSelectedColumnKeys(selection, columnSpec);
191
+ if (columnKeys.length === 0) return globalActions;
192
+ const columnActions: TableAction<T>[] = [];
193
+ for (const col of columnSpec) {
194
+ if (columnKeys.includes(col.key) && col.actions != null) {
195
+ for (const action of col.actions as TableAction<T>[]) {
196
+ // Avoid duplicates by id
197
+ if (
198
+ !globalActions.some((a) => a.id === action.id) &&
199
+ !columnActions.some((a) => a.id === action.id)
200
+ ) {
201
+ columnActions.push(action);
202
+ }
203
+ }
204
+ }
205
+ }
206
+ return [...globalActions, ...columnActions];
207
+ }
208
+
209
+ /** Collect all available filters from global filters and column specs. */
210
+ export function collectAllFilters<T>(
211
+ globalFilters: TableFilter<T>[],
212
+ columnSpec: ColumnSpec[],
213
+ ): TableFilter<T>[] {
214
+ const result: TableFilter<T>[] = [...globalFilters];
215
+ for (const col of columnSpec) {
216
+ if (col.filters != null) {
217
+ for (const f of col.filters as TableFilter<T>[]) {
218
+ const withKey: TableFilter<T> = { ...f, columnKey: f.columnKey ?? col.key };
219
+ if (!result.some((r) => r.id === withKey.id)) {
220
+ result.push(withKey);
221
+ }
222
+ }
223
+ }
224
+ }
225
+ return result;
226
+ }
227
+
228
+ /** Compute row indices after applying both filters and sorts.
229
+ * Returns `null` when no filters or sorts are active (natural order). */
230
+ export function computeTransformedRowIndices<T>(
231
+ data: T[],
232
+ updatedData: T[],
233
+ activeFilters: Map<string, { filter: any; state: any }>,
234
+ columnSorts: ColumnSort[],
235
+ ): number[] | null {
236
+ const hasFilters = activeFilters != null && activeFilters.size > 0;
237
+ const hasSorts = columnSorts != null && columnSorts.length > 0;
238
+
239
+ if (!hasFilters && !hasSorts) return null;
240
+
241
+ let indices: number[];
242
+ if (hasFilters) {
243
+ // Apply filter predicates
244
+ const filtered = computeFilteredRowIndices(data, updatedData, activeFilters);
245
+ indices = filtered ?? Array.from({ length: Math.max(data.length, updatedData.length) }, (_, i) => i);
246
+ } else {
247
+ const numRows = Math.max(data.length, updatedData.length);
248
+ indices = Array.from({ length: numRows }, (_, i) => i);
249
+ }
250
+
251
+ if (!hasSorts) return indices;
252
+
253
+ // Sort the indices by column values
254
+ return [...indices].sort((a, b) => {
255
+ for (const sort of columnSorts) {
256
+ const rowA = updatedData[a] != null ? { ...data[a], ...updatedData[a] } : data[a];
257
+ const rowB = updatedData[b] != null ? { ...data[b], ...updatedData[b] } : data[b];
258
+ const valA = rowA?.[sort.key];
259
+ const valB = rowB?.[sort.key];
260
+
261
+ if (valA == null && valB == null) continue;
262
+ if (valA == null) return sort.ascending ? -1 : 1;
263
+ if (valB == null) return sort.ascending ? 1 : -1;
264
+
265
+ const cmp = valA < valB ? -1 : valA > valB ? 1 : 0;
266
+ if (cmp !== 0) return sort.ascending ? cmp : -cmp;
267
+ }
268
+ return 0;
269
+ });
270
+ }
271
+
@@ -0,0 +1,30 @@
1
+ .actions-toolbar
2
+ display: flex
3
+ flex-direction: row
4
+ gap: 4px
5
+ margin-bottom: 4px
6
+ align-items: center
7
+
8
+ .spacer
9
+ flex-grow: 1
10
+
11
+ .action-popover-content, .filter-config
12
+ padding: 12px
13
+ max-width: 300px
14
+
15
+ p.description
16
+ margin: 0 0 8px 0
17
+ color: var(--secondary-color)
18
+ font-size: 0.9em
19
+
20
+ .run-button
21
+ margin-top: 8px
22
+ width: 100%
23
+
24
+ .filter-bar
25
+ display: flex
26
+ flex-direction: row
27
+ flex-wrap: wrap
28
+ gap: 4px
29
+ margin-bottom: 4px
30
+ align-items: center
@@ -0,0 +1,212 @@
1
+ import { Button, ButtonGroup, PopoverNext } from "@blueprintjs/core";
2
+ import { useCallback, useMemo, useState } from "react";
3
+ import h from "./toolbar.module.sass";
4
+ import { useSelector, useStoreAPI } from "../provider";
5
+ import {
6
+ buildActionContext,
7
+ getApplicableActions,
8
+ getSelectionCardinality,
9
+ mergeColumnActions,
10
+ } from "./selection";
11
+ import type { TableAction, TableActionContext } from "./types";
12
+ import { RegionCardinality } from "@blueprintjs/table";
13
+ import { useToaster } from "../notifications.ts";
14
+ import { DataSheetStore } from "../types.ts";
15
+
16
+ /** Toolbar that renders applicable table actions based on the
17
+ * current selection cardinality and edit mode.
18
+ * Automatically merges column-specific actions from `ColumnSpec.actions`
19
+ * when the corresponding columns are in the selection. */
20
+ export function ActionsToolbar<T>({ actions }: { actions: TableAction<T>[] }) {
21
+ const selection = useSelector((state) => state.selection);
22
+ const editable = useSelector((state) => state.editable);
23
+ const columnSpec = useSelector((state) => state.columnSpec);
24
+
25
+ const cardinality = useMemo(
26
+ () => getSelectionCardinality(selection) ?? RegionCardinality.FULL_TABLE,
27
+ [selection],
28
+ );
29
+
30
+ // Merge global actions with column-specific actions from the selection
31
+ const allActions = useMemo(
32
+ () => mergeColumnActions(actions, columnSpec, selection),
33
+ [actions, columnSpec, selection],
34
+ );
35
+
36
+ const applicableActions = useMemo(
37
+ () => getApplicableActions(allActions, cardinality, editable),
38
+ [allActions, cardinality, editable],
39
+ );
40
+
41
+ if (applicableActions.length === 0) return null;
42
+
43
+ return h(
44
+ "div.actions-toolbar",
45
+ h(
46
+ ButtonGroup,
47
+ { minimal: true },
48
+ applicableActions.map((action) =>
49
+ h(ActionButton, { key: action.id, action }),
50
+ ),
51
+ ),
52
+ );
53
+ }
54
+
55
+ function getMessageForError(e: any) {
56
+ if (e instanceof Error) return e.message;
57
+ if (typeof e === "string") return e;
58
+ if (e instanceof Object) return JSON.stringify(e);
59
+ return null;
60
+ }
61
+
62
+ function isActionDisabled(action: TableAction, state: any): boolean {
63
+ if (typeof action.disabled === "boolean") return action.disabled;
64
+ if (typeof action.disabled === "function") {
65
+ return action.disabled(state);
66
+ }
67
+ return false;
68
+ }
69
+
70
+ export function runActionWrapper<T>(
71
+ action: TableAction<T>,
72
+ state: DataSheetStore<any>,
73
+ setState: (state: Partial<DataSheetStore<any>>) => void,
74
+ toaster: any,
75
+ configState: any = undefined,
76
+ ) {
77
+ const ctx = buildActionContext(state, setState) as TableActionContext<T>;
78
+ if (action.disabled instanceof Function) {
79
+ if (action.disabled(ctx)) {
80
+ return;
81
+ }
82
+ } else if (action.disabled) {
83
+ return;
84
+ }
85
+ try {
86
+ const res = action.run(ctx, configState);
87
+ if (res instanceof Promise) {
88
+ res
89
+ .then(() => {})
90
+ .catch((e) => {
91
+ displayErrorForAction(action, e, toaster);
92
+ });
93
+ }
94
+ } catch (e) {
95
+ displayErrorForAction(action, e, toaster);
96
+ }
97
+ }
98
+
99
+ /** A single action button. Handles both simple actions (direct click)
100
+ * and actions with a `detailsForm` (popover with config + run). */
101
+ function ActionButton<T>({ action }: { action: TableAction<T> }) {
102
+ const storeAPI = useStoreAPI();
103
+ const toaster = useToaster();
104
+
105
+ // Reactive disabled check — re-renders when relevant state changes
106
+ const isDisabled = useSelector((state) => {
107
+ return isActionDisabled(action, state);
108
+ });
109
+
110
+ const runAction = useCallback(
111
+ (configState?: any) => {
112
+ runActionWrapper(
113
+ action,
114
+ storeAPI.getState(),
115
+ storeAPI.setState,
116
+ toaster,
117
+ configState,
118
+ );
119
+ },
120
+ [storeAPI, action, toaster],
121
+ );
122
+
123
+ if (action.detailsForm != null) {
124
+ return h(ActionButtonWithForm, { action, runAction, isDisabled });
125
+ }
126
+
127
+ return h(
128
+ Button,
129
+ {
130
+ icon: action.icon,
131
+ intent: action.intent,
132
+ disabled: isDisabled,
133
+ onClick() {
134
+ runAction();
135
+ },
136
+ },
137
+ action.name,
138
+ );
139
+ }
140
+
141
+ function displayErrorForAction(action: TableAction, error: any, toaster: any) {
142
+ const message =
143
+ getMessageForError(error) ?? action.errorMessage ?? "Action failed";
144
+ toaster.show({
145
+ message,
146
+ intent: "danger",
147
+ });
148
+ }
149
+
150
+ /** Action button that opens a popover for pre-run configuration. */
151
+ function ActionButtonWithForm<T, S>({
152
+ action,
153
+ runAction,
154
+ isDisabled,
155
+ }: {
156
+ action: TableAction<T, S>;
157
+ runAction: (configState?: S) => void;
158
+ isDisabled: boolean;
159
+ }) {
160
+ const [configState, setConfigState] = useState<S>(
161
+ action.defaultState ?? (null as S),
162
+ );
163
+ const [isOpen, setIsOpen] = useState(false);
164
+
165
+ let ready = true;
166
+ if (action.isReady != null) {
167
+ ready = action.isReady(configState);
168
+ }
169
+
170
+ return h(
171
+ PopoverNext,
172
+ {
173
+ isOpen,
174
+ onClose: () => setIsOpen(false),
175
+ content: h("div.action-popover-content", [
176
+ h.if(action.description != null)("p.description", action.description),
177
+ h(action.detailsForm, {
178
+ state: configState,
179
+ setState: setConfigState,
180
+ }),
181
+ h(
182
+ Button,
183
+ {
184
+ className: "run-button",
185
+ intent: action.intent ?? "primary",
186
+ icon: "play",
187
+ disabled: !ready,
188
+ fill: true,
189
+ onClick() {
190
+ runAction(configState);
191
+ setIsOpen(false);
192
+ },
193
+ },
194
+ "Run",
195
+ ),
196
+ ]),
197
+ placement: "bottom-start",
198
+ enforceFocus: false,
199
+ autoFocus: false,
200
+ },
201
+ h(
202
+ Button,
203
+ {
204
+ icon: action.icon,
205
+ intent: action.intent,
206
+ disabled: isDisabled,
207
+ onClick: () => setIsOpen(!isOpen),
208
+ },
209
+ action.name,
210
+ ),
211
+ );
212
+ }
@@ -0,0 +1,159 @@
1
+ import type { IconName, Intent } from "@blueprintjs/core";
2
+ import type { Region, RegionCardinality } from "@blueprintjs/table";
3
+ import type { ComponentType } from "react";
4
+ import type { ColumnSpec } from "../utils";
5
+ import { ClipboardProxy, TableElementStatus } from "../types.ts";
6
+
7
+ /** Selection cardinality including the case of no active selection */
8
+ export type SelectionCardinality = RegionCardinality | "none";
9
+
10
+ /** A single cell edit, used with `editCells` for batch updates. */
11
+ export interface CellEdit {
12
+ rowIndex: number;
13
+ columnKey: string;
14
+ value: any;
15
+ }
16
+
17
+ /** A column filter that can be activated to hide non-matching rows.
18
+ * Parallel to `TableAction` but with persistent state while active.
19
+ * Follows the same `{ state, setState }` form pattern.
20
+ *
21
+ * @typeParam T - Row data type
22
+ * @typeParam S - Filter configuration state type
23
+ */
24
+ export interface TableFilter<T = any, S = any> {
25
+ /** Unique identifier */
26
+ id: string;
27
+ /** Display name */
28
+ name: string;
29
+ /** Blueprint icon */
30
+ icon?: IconName;
31
+ /** Description shown in the filter popover */
32
+ description?: string;
33
+ /** Column this filter targets. Automatically set when defined in a ColumnSpec. */
34
+ columnKey?: string;
35
+ /** Default filter configuration state */
36
+ defaultState?: S;
37
+ /** Component for configuring the filter value.
38
+ * Same `{ state, setState }` pattern as `TableAction.detailsForm`. */
39
+ filterForm?: ComponentType<{ state: S; setState(state: S): void }>;
40
+ /** Row predicate: return `true` if the row should be visible.
41
+ * Receives the merged row (updatedData overlaid on data). */
42
+ predicate(row: T, state: S): boolean;
43
+ }
44
+
45
+ /** An active filter entry stored in the table state. */
46
+ export interface ActiveFilterEntry<T = any> {
47
+ filter: TableFilter<T>;
48
+ state: any;
49
+ }
50
+
51
+ // Re-export ColumnSort from the main types module (defined there to
52
+ // avoid a circular import, since actions/types imports from types.ts).
53
+ export type { ColumnSort } from "../types.ts";
54
+
55
+ /** Context passed to an action's `run` function, providing both data access
56
+ * and store manipulation methods. Constructed fresh at action-run time
57
+ * to ensure current state. */
58
+ export interface TableActionContext<T = any> {
59
+ /** Current selection regions */
60
+ selection: Region[];
61
+ /** Derived cardinality of the current selection */
62
+ selectionCardinality: SelectionCardinality;
63
+ /** The table's base data */
64
+ data: T[];
65
+ /** Sparse overlay of edited data */
66
+ updatedData: T[];
67
+ /** Row status tracking added/deleted rows */
68
+ rowStatus: TableElementStatus[];
69
+ /** Column definitions */
70
+ columnSpec: ColumnSpec[];
71
+ /** Whether the table is in edit mode */
72
+ editable: boolean;
73
+
74
+ // Convenience methods (derived from selection)
75
+ /** Row indices covered by the current selection */
76
+ getSelectedRowIndices(): number[];
77
+ /** Column keys covered by the current selection */
78
+ getSelectedColumnKeys(): string[];
79
+
80
+ // Store manipulation methods
81
+ onCellEdited(rowIndex: number, columnKey: string, value: any): void;
82
+ /** Edit multiple cells in a single batch update. Preferred over calling
83
+ * `onCellEdited` in a loop, which triggers separate store updates
84
+ * and may produce inconsistent intermediate states. */
85
+ editCells(edits: CellEdit[]): void;
86
+ deleteSelectedRows(): void;
87
+ addRow(row?: Partial<T>): void;
88
+ setUpdatedData(data: any): void;
89
+ resetChanges(region?: Region[]): void;
90
+ clearSelection(): void;
91
+ scrollToRow(rowIndex: number): void;
92
+ /** Direct store mutation for cases not covered by the convenience methods
93
+ * above (e.g., modifying `columnSpec` or `deletedRows`). */
94
+ setState(partial: Record<string, any>): void;
95
+
96
+ // Clipboard proxy support
97
+ /** Active clipboard proxy from a prior copy, if any */
98
+ clipboardProxy: ClipboardProxy | null;
99
+ /** Store a clipboard proxy for potential backend-mediated paste */
100
+ setClipboardProxy(proxy: ClipboardProxy | null): void;
101
+
102
+ // Filter support
103
+ /** Row index mapping when filters are active. When non-null,
104
+ * visible row `i` maps to data row `filteredRowIndices[i]`. */
105
+ filteredRowIndices: number[] | null;
106
+ }
107
+
108
+ /** Definition of a table action. Follows the `ActionDef` pattern from
109
+ * `@macrostrat/form-components` with table-specific extensions.
110
+ *
111
+ * @typeParam T - Row data type
112
+ * @typeParam S - Configuration state type (for actions with a `detailsForm`)
113
+ */
114
+ export interface TableAction<T = any, S = null> {
115
+ /** Unique identifier */
116
+ id: string;
117
+ /** Display name */
118
+ name: string;
119
+ /** Group name (for grouping actions together) */
120
+ group?: string;
121
+ /** Blueprint icon name */
122
+ icon?: IconName;
123
+ /** Visual intent (e.g., "danger" for destructive actions) */
124
+ intent?: Intent;
125
+ /** Description shown as tooltip or in the details panel */
126
+ description?: string;
127
+
128
+ /** Which selection cardinalities this action applies to.
129
+ * The action will appear in the toolbar when the current selection
130
+ * matches any of these cardinalities. Use `"none"` for actions that
131
+ * should be available even with no active selection. */
132
+ targets: SelectionCardinality[];
133
+
134
+ /** Whether this action requires the table to be in edit mode.
135
+ * Defaults to `true`. Set to `false` for read-only actions
136
+ * (e.g., "open URL", "copy to clipboard"). */
137
+ requiresEditable?: boolean;
138
+
139
+ /** Whether the action is currently disabled. Can be a static boolean
140
+ * or a function that receives the current context for dynamic checks. */
141
+ disabled?: boolean | ((context: TableActionContext<T>) => boolean);
142
+
143
+ /** Default configuration state for the action's preflight form */
144
+ defaultState?: S;
145
+ /** Component for pre-run configuration, rendered in a popover.
146
+ * Follows the same pattern as `ActionDef.detailsForm` from
147
+ * `@macrostrat/form-components`. */
148
+ detailsForm?: ComponentType<{ state: S; setState(state: S): void }>;
149
+ /** Check if the action is ready to run given its configuration state */
150
+ isReady?: (state: S) => boolean;
151
+
152
+ /** Execute the action. May be synchronous (local state manipulation)
153
+ * or asynchronous (backend fulfillment). For lazy-loaded tables,
154
+ * async actions are required when targeting non-loaded cells. */
155
+ run(context: TableActionContext<T>, state?: S): void | Promise<void>;
156
+ successMessage?: string;
157
+ errorMessage?: string;
158
+ hotkey?: string;
159
+ }