@macrostrat/data-sheet 4.1.1 → 4.2.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.
Files changed (304) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/dist/actions/builtin.cjs +1 -1
  3. package/dist/actions/builtin.cjs.map +1 -1
  4. package/dist/actions/builtin.d.ts +0 -2
  5. package/dist/actions/builtin.js +1 -1
  6. package/dist/actions/builtin.js.map +1 -1
  7. package/dist/actions/clipboard.cjs +1 -1
  8. package/dist/actions/clipboard.cjs.map +1 -1
  9. package/dist/actions/clipboard.d.ts +3 -2
  10. package/dist/actions/clipboard.js +1 -1
  11. package/dist/actions/clipboard.js.map +1 -1
  12. package/dist/actions/column-controls.cjs +7 -196
  13. package/dist/actions/column-controls.cjs.map +1 -1
  14. package/dist/actions/column-controls.d.ts +1 -1
  15. package/dist/actions/column-controls.js +5 -194
  16. package/dist/actions/column-controls.js.map +1 -1
  17. package/dist/actions/column-filter.cjs +24 -73
  18. package/dist/actions/column-filter.cjs.map +1 -1
  19. package/dist/actions/column-filter.d.ts +6 -4
  20. package/dist/actions/column-filter.js +25 -74
  21. package/dist/actions/column-filter.js.map +1 -1
  22. package/dist/actions/context.cjs +147 -0
  23. package/dist/actions/context.cjs.map +1 -0
  24. package/dist/actions/context.d.ts +93 -0
  25. package/dist/actions/context.js +145 -0
  26. package/dist/actions/context.js.map +1 -0
  27. package/dist/actions/edit-actions.cjs.map +1 -1
  28. package/dist/actions/edit-actions.d.ts +3 -1
  29. package/dist/actions/edit-actions.js.map +1 -1
  30. package/dist/actions/index.d.ts +2 -3
  31. package/dist/actions/selection.cjs +70 -115
  32. package/dist/actions/selection.cjs.map +1 -1
  33. package/dist/actions/selection.d.ts +22 -12
  34. package/dist/actions/selection.js +71 -114
  35. package/dist/actions/selection.js.map +1 -1
  36. package/dist/actions/table-actions.cjs +41 -0
  37. package/dist/actions/table-actions.cjs.map +1 -0
  38. package/dist/actions/{table-controls.d.ts → table-actions.d.ts} +0 -4
  39. package/dist/actions/{table-controls.js → table-actions.js} +2 -23
  40. package/dist/actions/table-actions.js.map +1 -0
  41. package/dist/actions/types.d.ts +11 -89
  42. package/dist/components/cell-detail.cjs +6 -6
  43. package/dist/components/cell-detail.cjs.map +1 -1
  44. package/dist/components/cell-detail.js +1 -1
  45. package/dist/components/colors.cjs.map +1 -1
  46. package/dist/components/colors.js.map +1 -1
  47. package/dist/components/editor-popup.cjs +7 -7
  48. package/dist/components/editor-popup.cjs.map +1 -1
  49. package/dist/components/editor-popup.js +1 -1
  50. package/dist/components/index.d.ts +2 -1
  51. package/dist/components/indicators/filter-and-sort.cjs +378 -0
  52. package/dist/components/indicators/filter-and-sort.cjs.map +1 -0
  53. package/dist/components/indicators/filter-and-sort.d.ts +59 -0
  54. package/dist/components/indicators/filter-and-sort.js +376 -0
  55. package/dist/components/indicators/filter-and-sort.js.map +1 -0
  56. package/dist/components/indicators/filter-and-sort.module.sass.cjs +16 -0
  57. package/dist/components/indicators/filter-and-sort.module.sass.cjs.map +1 -0
  58. package/dist/components/indicators/filter-and-sort.module.sass.js +14 -0
  59. package/dist/components/indicators/filter-and-sort.module.sass.js.map +1 -0
  60. package/dist/components/indicators/index.d.ts +4 -0
  61. package/dist/components/indicators/selection.cjs +113 -0
  62. package/dist/components/indicators/selection.cjs.map +1 -0
  63. package/dist/components/indicators/selection.d.ts +3 -0
  64. package/dist/components/indicators/selection.js +111 -0
  65. package/dist/components/indicators/selection.js.map +1 -0
  66. package/dist/components/indicators/selection.module.sass.cjs +19 -0
  67. package/dist/components/indicators/selection.module.sass.cjs.map +1 -0
  68. package/dist/components/indicators/selection.module.sass.js +17 -0
  69. package/dist/components/indicators/selection.module.sass.js.map +1 -0
  70. package/dist/components/indicators/unified-filters.cjs +130 -0
  71. package/dist/components/indicators/unified-filters.cjs.map +1 -0
  72. package/dist/components/indicators/unified-filters.d.ts +10 -0
  73. package/dist/components/indicators/unified-filters.js +130 -0
  74. package/dist/components/indicators/unified-filters.js.map +1 -0
  75. package/dist/components/indicators/view-info.cjs +59 -0
  76. package/dist/components/indicators/view-info.cjs.map +1 -0
  77. package/dist/components/indicators/view-info.d.ts +11 -0
  78. package/dist/components/{view-info.js → indicators/view-info.js} +20 -16
  79. package/dist/components/indicators/view-info.js.map +1 -0
  80. package/dist/components/text-areas.cjs +5 -5
  81. package/dist/components/text-areas.cjs.map +1 -1
  82. package/dist/components/text-areas.js +1 -1
  83. package/dist/components/toolbars/actions-toolbar.cjs +185 -0
  84. package/dist/components/toolbars/actions-toolbar.cjs.map +1 -0
  85. package/dist/{actions/toolbar.d.ts → components/toolbars/actions-toolbar.d.ts} +9 -5
  86. package/dist/{actions/toolbar.js → components/toolbars/actions-toolbar.js} +37 -112
  87. package/dist/components/toolbars/actions-toolbar.js.map +1 -0
  88. package/dist/components/toolbars/actions-toolbar.module.sass.cjs +29 -0
  89. package/dist/components/toolbars/actions-toolbar.module.sass.cjs.map +1 -0
  90. package/dist/components/toolbars/actions-toolbar.module.sass.js +27 -0
  91. package/dist/components/toolbars/actions-toolbar.module.sass.js.map +1 -0
  92. package/dist/components/toolbars/active-filters.cjs +120 -0
  93. package/dist/components/toolbars/active-filters.cjs.map +1 -0
  94. package/dist/{actions/filters.d.ts → components/toolbars/active-filters.d.ts} +3 -3
  95. package/dist/components/toolbars/active-filters.js +120 -0
  96. package/dist/components/toolbars/active-filters.js.map +1 -0
  97. package/dist/components/toolbars/index.d.ts +2 -0
  98. package/dist/data-panel.cjs +291 -0
  99. package/dist/data-panel.cjs.map +1 -0
  100. package/dist/data-panel.d.ts +110 -0
  101. package/dist/data-panel.js +289 -0
  102. package/dist/data-panel.js.map +1 -0
  103. package/dist/data-panel.module.sass.cjs +40 -0
  104. package/dist/data-panel.module.sass.cjs.map +1 -0
  105. package/dist/data-panel.module.sass.js +38 -0
  106. package/dist/data-panel.module.sass.js.map +1 -0
  107. package/dist/{core.cjs → data-sheet.cjs} +126 -188
  108. package/dist/data-sheet.cjs.map +1 -0
  109. package/dist/data-sheet.css +241 -13
  110. package/dist/data-sheet.d.ts +16 -0
  111. package/dist/{core.js → data-sheet.js} +95 -157
  112. package/dist/data-sheet.js.map +1 -0
  113. package/dist/data-view.cjs +47 -0
  114. package/dist/data-view.cjs.map +1 -0
  115. package/dist/data-view.d.ts +15 -0
  116. package/dist/data-view.js +45 -0
  117. package/dist/data-view.js.map +1 -0
  118. package/dist/filters/operators.cjs +19 -2
  119. package/dist/filters/operators.cjs.map +1 -1
  120. package/dist/filters/operators.d.ts +4 -3
  121. package/dist/filters/operators.js +19 -2
  122. package/dist/filters/operators.js.map +1 -1
  123. package/dist/index.cjs +106 -47
  124. package/dist/index.cjs.map +1 -1
  125. package/dist/index.d.ts +4 -3
  126. package/dist/index.js +76 -17
  127. package/dist/index.js.map +1 -1
  128. package/dist/notifications.cjs +4 -4
  129. package/dist/notifications.cjs.map +1 -1
  130. package/dist/notifications.js +1 -1
  131. package/dist/postgrest-table/data-loaders.cjs +128 -95
  132. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  133. package/dist/postgrest-table/data-loaders.d.ts +52 -82
  134. package/dist/postgrest-table/data-loaders.js +109 -76
  135. package/dist/postgrest-table/data-loaders.js.map +1 -1
  136. package/dist/postgrest-table/index.cjs +18 -17
  137. package/dist/postgrest-table/index.cjs.map +1 -1
  138. package/dist/postgrest-table/index.d.ts +4 -3
  139. package/dist/postgrest-table/index.js +9 -8
  140. package/dist/postgrest-table/index.js.map +1 -1
  141. package/dist/{utils → provider}/column-spec.cjs +23 -9
  142. package/dist/provider/column-spec.cjs.map +1 -0
  143. package/dist/{utils → provider}/column-spec.d.ts +73 -2
  144. package/dist/{utils → provider}/column-spec.js +24 -10
  145. package/dist/provider/column-spec.js.map +1 -0
  146. package/dist/provider/core.cjs +382 -0
  147. package/dist/provider/core.cjs.map +1 -0
  148. package/dist/provider/core.d.ts +74 -0
  149. package/dist/provider/core.js +380 -0
  150. package/dist/provider/core.js.map +1 -0
  151. package/dist/provider/index.d.ts +7 -0
  152. package/dist/provider/interactions.cjs +128 -0
  153. package/dist/provider/interactions.cjs.map +1 -0
  154. package/dist/provider/interactions.d.ts +39 -0
  155. package/dist/provider/interactions.js +128 -0
  156. package/dist/provider/interactions.js.map +1 -0
  157. package/dist/provider/selection.cjs +29 -0
  158. package/dist/provider/selection.cjs.map +1 -0
  159. package/dist/provider/selection.d.ts +9 -0
  160. package/dist/provider/selection.js +29 -0
  161. package/dist/provider/selection.js.map +1 -0
  162. package/dist/provider/table-data.cjs +92 -0
  163. package/dist/provider/table-data.cjs.map +1 -0
  164. package/dist/provider/table-data.d.ts +81 -0
  165. package/dist/provider/table-data.js +92 -0
  166. package/dist/provider/table-data.js.map +1 -0
  167. package/dist/provider/types.cjs +22 -0
  168. package/dist/provider/types.cjs.map +1 -0
  169. package/dist/provider/types.d.ts +256 -0
  170. package/dist/provider/types.js +22 -0
  171. package/dist/provider/types.js.map +1 -0
  172. package/dist/{zustand-store.cjs → provider/zustand-store.cjs} +36 -23
  173. package/dist/provider/zustand-store.cjs.map +1 -0
  174. package/dist/{zustand-store.js → provider/zustand-store.js} +21 -8
  175. package/dist/provider/zustand-store.js.map +1 -0
  176. package/dist/{cell-renderer.cjs → renderers/cell-renderer.cjs} +67 -34
  177. package/dist/renderers/cell-renderer.cjs.map +1 -0
  178. package/dist/{cell-renderer.d.ts → renderers/cell-renderer.d.ts} +1 -2
  179. package/dist/{cell-renderer.js → renderers/cell-renderer.js} +65 -32
  180. package/dist/renderers/cell-renderer.js.map +1 -0
  181. package/dist/renderers/column-header-cell.cjs +21 -13
  182. package/dist/renderers/column-header-cell.cjs.map +1 -1
  183. package/dist/renderers/column-header-cell.d.ts +3 -2
  184. package/dist/renderers/column-header-cell.js +20 -12
  185. package/dist/renderers/column-header-cell.js.map +1 -1
  186. package/dist/renderers/index.d.ts +1 -0
  187. package/dist/types.cjs +7 -19
  188. package/dist/types.cjs.map +1 -1
  189. package/dist/types.d.ts +117 -229
  190. package/dist/types.js +7 -19
  191. package/dist/types.js.map +1 -1
  192. package/dist/utils/atoms.cjs +24 -0
  193. package/dist/utils/atoms.cjs.map +1 -0
  194. package/dist/utils/atoms.d.ts +4 -0
  195. package/dist/utils/atoms.js +24 -0
  196. package/dist/utils/atoms.js.map +1 -0
  197. package/dist/utils/debug-overlay.cjs.map +1 -1
  198. package/dist/utils/debug-overlay.js.map +1 -1
  199. package/dist/utils/index.d.ts +2 -3
  200. package/dist/utils/key-handling.cjs +140 -139
  201. package/dist/utils/key-handling.cjs.map +1 -1
  202. package/dist/utils/key-handling.d.ts +4 -2
  203. package/dist/utils/key-handling.js +136 -135
  204. package/dist/utils/key-handling.js.map +1 -1
  205. package/dist/utils/validation.cjs +6 -1
  206. package/dist/utils/validation.cjs.map +1 -1
  207. package/dist/utils/validation.d.ts +1 -1
  208. package/dist/utils/validation.js +6 -1
  209. package/dist/utils/validation.js.map +1 -1
  210. package/package.json +4 -3
  211. package/src/actions/builtin.ts +1 -4
  212. package/src/actions/clipboard.ts +4 -3
  213. package/src/actions/column-controls.ts +12 -245
  214. package/src/actions/column-filter.ts +28 -92
  215. package/src/actions/context.ts +299 -0
  216. package/src/actions/edit-actions.ts +3 -1
  217. package/src/actions/index.ts +2 -3
  218. package/src/actions/selection.ts +109 -156
  219. package/src/actions/{table-controls.ts → table-actions.ts} +0 -24
  220. package/src/actions/types.ts +17 -101
  221. package/src/components/colors.ts +1 -1
  222. package/src/components/index.ts +2 -1
  223. package/src/components/indicators/filter-and-sort.module.sass +13 -0
  224. package/src/components/indicators/filter-and-sort.ts +478 -0
  225. package/src/components/indicators/index.ts +4 -0
  226. package/src/components/indicators/selection.module.sass +22 -0
  227. package/src/components/indicators/selection.ts +141 -0
  228. package/src/components/indicators/unified-filters.ts +161 -0
  229. package/src/components/{view-info.ts → indicators/view-info.ts} +19 -30
  230. package/src/{actions/toolbar.module.sass → components/toolbars/actions-toolbar.module.sass} +17 -4
  231. package/src/{actions/toolbar.ts → components/toolbars/actions-toolbar.ts} +49 -150
  232. package/src/components/toolbars/active-filters.ts +150 -0
  233. package/src/components/toolbars/index.ts +2 -0
  234. package/src/data-panel.module.sass +185 -0
  235. package/src/data-panel.ts +556 -0
  236. package/src/{core.ts → data-sheet.ts} +156 -327
  237. package/src/data-view.ts +70 -0
  238. package/src/filters/operators.ts +33 -3
  239. package/src/index.ts +4 -3
  240. package/src/postgrest-table/data-loaders.ts +244 -171
  241. package/src/postgrest-table/index.ts +10 -9
  242. package/src/{utils → provider}/column-spec.ts +30 -15
  243. package/src/provider/core.ts +585 -0
  244. package/src/provider/index.ts +7 -0
  245. package/src/provider/interactions.ts +161 -0
  246. package/src/provider/selection.ts +31 -0
  247. package/src/provider/table-data.ts +178 -0
  248. package/src/provider/types.ts +273 -0
  249. package/src/{zustand-store.ts → provider/zustand-store.ts} +35 -14
  250. package/src/{cell-renderer.ts → renderers/cell-renderer.ts} +140 -57
  251. package/src/renderers/column-header-cell.ts +21 -14
  252. package/src/renderers/index.ts +1 -0
  253. package/src/types.ts +137 -239
  254. package/src/utils/atoms.ts +28 -0
  255. package/src/utils/debug-overlay.ts +1 -1
  256. package/src/utils/index.ts +2 -8
  257. package/src/utils/key-handling.ts +170 -164
  258. package/src/utils/validation.ts +7 -2
  259. package/dist/actions/filters.cjs +0 -139
  260. package/dist/actions/filters.cjs.map +0 -1
  261. package/dist/actions/filters.js +0 -139
  262. package/dist/actions/filters.js.map +0 -1
  263. package/dist/actions/table-controls.cjs +0 -62
  264. package/dist/actions/table-controls.cjs.map +0 -1
  265. package/dist/actions/table-controls.js.map +0 -1
  266. package/dist/actions/toolbar.cjs +0 -260
  267. package/dist/actions/toolbar.cjs.map +0 -1
  268. package/dist/actions/toolbar.js.map +0 -1
  269. package/dist/actions/toolbar.module.sass.cjs +0 -27
  270. package/dist/actions/toolbar.module.sass.cjs.map +0 -1
  271. package/dist/actions/toolbar.module.sass.js +0 -25
  272. package/dist/actions/toolbar.module.sass.js.map +0 -1
  273. package/dist/cell-renderer.cjs.map +0 -1
  274. package/dist/cell-renderer.js.map +0 -1
  275. package/dist/components/view-info.cjs +0 -55
  276. package/dist/components/view-info.cjs.map +0 -1
  277. package/dist/components/view-info.d.ts +0 -5
  278. package/dist/components/view-info.js.map +0 -1
  279. package/dist/core.cjs.map +0 -1
  280. package/dist/core.d.ts +0 -117
  281. package/dist/core.js.map +0 -1
  282. package/dist/provider.cjs +0 -197
  283. package/dist/provider.cjs.map +0 -1
  284. package/dist/provider.d.ts +0 -26
  285. package/dist/provider.js +0 -195
  286. package/dist/provider.js.map +0 -1
  287. package/dist/table-updates/data-parameter.d.ts +0 -14
  288. package/dist/table-updates/filter.d.ts +0 -16
  289. package/dist/table-updates/index.d.ts +0 -19
  290. package/dist/utils/column-spec.cjs.map +0 -1
  291. package/dist/utils/column-spec.js.map +0 -1
  292. package/dist/utils/index.cjs +0 -9
  293. package/dist/utils/index.cjs.map +0 -1
  294. package/dist/utils/index.js +0 -9
  295. package/dist/utils/index.js.map +0 -1
  296. package/dist/zustand-store.cjs.map +0 -1
  297. package/dist/zustand-store.js.map +0 -1
  298. package/src/actions/filters.ts +0 -176
  299. package/src/components/actions.module.sass +0 -8
  300. package/src/provider.ts +0 -267
  301. package/src/table-updates/data-parameter.ts +0 -44
  302. package/src/table-updates/filter.ts +0 -124
  303. package/src/table-updates/index.ts +0 -239
  304. /package/dist/{zustand-store.d.ts → provider/zustand-store.d.ts} +0 -0
@@ -0,0 +1,299 @@
1
+ import {
2
+ CellEdit,
3
+ ClipboardProxy,
4
+ RowStatusValue,
5
+ tableDataAtom,
6
+ } from "../provider";
7
+ import { type Region, RegionCardinality } from "@blueprintjs/table";
8
+ import update from "immutability-helper";
9
+ import {
10
+ computeFilteredRowIndices,
11
+ computeSelectionShape,
12
+ getSelectedColumnKeys,
13
+ getSelectedRowIndices,
14
+ SelectionCardinality,
15
+ SelectionShape,
16
+ } from "./selection.ts";
17
+ import { Getter, Setter } from "jotai";
18
+ import type { Store as JotaiStore } from "jotai/vanilla/store";
19
+ import { storeAPIAtom } from "../provider";
20
+ import type { ColumnSpec } from "../provider";
21
+ import type { TableAction } from "./types.ts";
22
+
23
+ interface ActionContextOptions {
24
+ /** Override the action context to scope to a single column.
25
+ This is needed for actions that are invoked for a column header
26
+ drop-down, for instance */
27
+ singleColumn: number;
28
+ }
29
+
30
+ type StoreInterface = Omit<JotaiStore, "sub">;
31
+
32
+ /** Context passed to an action's `run` function, providing both data access
33
+ * and store manipulation methods. Constructed fresh at action-run time
34
+ * to ensure current state. */
35
+ export interface TableActionContext<T = any> {
36
+ /** Current selection regions */
37
+ selection: Region[];
38
+ /** Derived cardinality of the current selection */
39
+ selectionCardinality: SelectionCardinality;
40
+ /** Concrete shape of the current selection (cardinality + column/row counts). */
41
+ selectionShape: SelectionShape;
42
+ /** The single selected column's key when exactly one column is scoped
43
+ * (a single full column, or cells within one column); otherwise `null`. */
44
+ columnKey: string | null;
45
+ /** The single selected data-row index when exactly one row is scoped;
46
+ * otherwise `null`. */
47
+ rowIndex: number | null;
48
+ /** The single selected cell (data-row index + column key) when exactly one
49
+ * cell is selected; otherwise `null`. Presence of these resolved fields is
50
+ * how actions discriminate selection shape — e.g. `appliesTo: ctx =>
51
+ * ctx.cell != null` for a single-cell control, or `ctx.columnKey != null`
52
+ * within a `FULL_COLUMNS` target for a single-column control. */
53
+ cell: { rowIndex: number; columnKey: string } | null;
54
+ /** The table's base data */
55
+ data: T[];
56
+ /** Sparse overlay of edited data */
57
+ updatedData: (T | null | undefined)[];
58
+ /** Row status tracking added/deleted rows */
59
+ rowStatus: (RowStatusValue | null | undefined)[];
60
+ /** Column definitions */
61
+ columnSpec: ColumnSpec[];
62
+ /** Whether the table is in edit mode */
63
+ editable: boolean;
64
+ /** Whether row deletion is available (false when the provider can't delete). */
65
+ canDeleteRows: boolean;
66
+
67
+ // Convenience methods (derived from selection)
68
+ /** Row indices covered by the current selection */
69
+ getSelectedRowIndices(): number[];
70
+ /** The selected rows as resolved objects (not indices) — the natural input
71
+ * for an immediate-edit action. Only loaded rows are returned. */
72
+ getSelectedRows(): T[];
73
+ /** Column keys covered by the current selection */
74
+ getSelectedColumnKeys(): string[];
75
+
76
+ // Immediate-edit persistence (present when a persisting provider is wired,
77
+ // e.g. via DataPanel). Each mutates through the provider and auto-refreshes,
78
+ // so a selection action can edit rows without touching a `refreshToken`.
79
+ /** Persist edited rows (upsert), then refresh. */
80
+ saveRows?: (rows: T[]) => Promise<void>;
81
+ /** Delete rows by identity, then refresh. */
82
+ deleteRows?: (ids: Array<string | number>) => Promise<void>;
83
+ /** Insert a row, then refresh. */
84
+ insertRow?: (row: Partial<T>) => Promise<void>;
85
+ /** Force a re-fetch from scratch. */
86
+ refresh?: () => void;
87
+
88
+ // Store manipulation methods
89
+ onCellEdited(rowIndex: number, columnKey: string, value: any): void;
90
+ /** Edit multiple cells in a single batch update. Preferred over calling
91
+ * `onCellEdited` in a loop, which triggers separate store updates
92
+ * and may produce inconsistent intermediate states. */
93
+ editCells(edits: CellEdit[]): void;
94
+ deleteSelectedRows(): void;
95
+ addRow(row?: Partial<T>): void;
96
+ setUpdatedData(data: any): void;
97
+ resetChanges(region?: Region[]): void;
98
+ clearSelection(): void;
99
+ scrollToRow(rowIndex: number): void;
100
+ /** Direct store mutation for cases not covered by the convenience methods
101
+ * above (e.g., modifying `columnSpec` or `deletedRows`). */
102
+ setState(partial: Record<string, any>): void;
103
+
104
+ // Clipboard proxy support
105
+ /** Active clipboard proxy from a prior copy, if any */
106
+ clipboardProxy: ClipboardProxy | null;
107
+ /** Store a clipboard proxy for potential backend-mediated paste */
108
+ setClipboardProxy(proxy: ClipboardProxy | null): void;
109
+
110
+ // Filter support
111
+ /** Row index mapping when filters are active. When non-null,
112
+ * visible row `i` maps to data row `filteredRowIndices[i]`. */
113
+ filteredRowIndices: number[] | null;
114
+ }
115
+
116
+ /** Construct a `TableActionContext` from the current store state.
117
+ * Call this at action-run time (not render time) to ensure
118
+ * the context reflects the latest state.
119
+ * @param setState - Optional store setState for direct mutation. Omit for
120
+ * read-only contexts (e.g., disabled checks). */
121
+ export function buildActionContext<T>(
122
+ get: Getter,
123
+ set: Setter,
124
+ options?: ActionContextOptions,
125
+ ): TableActionContext<T> {
126
+ const storeAPI = get(storeAPIAtom);
127
+ if (storeAPI == null) {
128
+ throw new Error(
129
+ "No DataSheetProvider found in context. Wrap your component in a <DataSheetProvider>.",
130
+ );
131
+ }
132
+
133
+ const data = get(tableDataAtom) as T[];
134
+
135
+ let state = storeAPI.getState();
136
+ if (options?.singleColumn != null) {
137
+ const colIndex = options.singleColumn;
138
+ state.selection = [{ cols: [colIndex, colIndex], rows: undefined }];
139
+ }
140
+ const setState = storeAPI.setState;
141
+
142
+ // Lazy-compute filteredRowIndices to avoid cost during disabled checks
143
+ let _filteredRowIndices: number[] | null | undefined = undefined;
144
+ function getFilteredRowIndices(): number[] | null {
145
+ if (_filteredRowIndices === undefined) {
146
+ _filteredRowIndices = computeFilteredRowIndices(
147
+ data,
148
+ state.updatedData,
149
+ state.activeFilters,
150
+ );
151
+ }
152
+ return _filteredRowIndices;
153
+ }
154
+
155
+ const { selection, updatedData, rowStatus, rowEditing = {} } = state;
156
+
157
+ const shape = computeSelectionShape(selection);
158
+
159
+ return {
160
+ selection,
161
+ selectionCardinality: shape.cardinality,
162
+ selectionShape: shape,
163
+ data,
164
+ updatedData,
165
+ rowStatus,
166
+ // Immediate-edit persistence, wired by the consumer (DataPanel) when a
167
+ // persisting provider is present; each already auto-refreshes.
168
+ ...rowEditing,
169
+ // Resolved single-target identity (lazy — mapping rows through filters is
170
+ // only paid when accessed).
171
+ get columnKey() {
172
+ return shape.columns === 1
173
+ ? (getSelectedColumnKeys(state.selection, state.columnSpec)[0] ?? null)
174
+ : null;
175
+ },
176
+ get rowIndex() {
177
+ return shape.rows === 1
178
+ ? (getSelectedRowIndices(state.selection, getFilteredRowIndices())[0] ??
179
+ null)
180
+ : null;
181
+ },
182
+ get cell() {
183
+ if (
184
+ shape.cardinality !== RegionCardinality.CELLS ||
185
+ shape.columns !== 1 ||
186
+ shape.rows !== 1
187
+ ) {
188
+ return null;
189
+ }
190
+ const columnKey =
191
+ getSelectedColumnKeys(state.selection, state.columnSpec)[0] ?? null;
192
+ const rowIndex =
193
+ getSelectedRowIndices(state.selection, getFilteredRowIndices())[0] ??
194
+ null;
195
+ return columnKey != null && rowIndex != null
196
+ ? { rowIndex, columnKey }
197
+ : null;
198
+ },
199
+ columnSpec: state.columnSpec,
200
+ editable: state.editable,
201
+ canDeleteRows: state.canDeleteRows,
202
+ getSelectedRowIndices: () =>
203
+ getSelectedRowIndices(state.selection, getFilteredRowIndices()),
204
+ getSelectedRows: () =>
205
+ getSelectedRowIndices(state.selection, getFilteredRowIndices())
206
+ .map((i) => data[i])
207
+ .filter((r) => r != null),
208
+ getSelectedColumnKeys: () =>
209
+ getSelectedColumnKeys(state.selection, state.columnSpec),
210
+ onCellEdited: state.onCellEdited,
211
+ editCells(edits: CellEdit[]) {
212
+ state.setUpdatedData((updatedData: T[]) => {
213
+ const spec: Record<number, any> = {};
214
+ for (const e of edits) {
215
+ const rowIndex = e.rowIndex;
216
+ const columnKey = (e as any).columnKey ?? e.column;
217
+ if (spec[rowIndex] == null) {
218
+ const op = updatedData[rowIndex] != null ? "$merge" : "$set";
219
+ spec[rowIndex] = { [op]: {} };
220
+ }
221
+ const opKey = Object.keys(spec[rowIndex])[0];
222
+ spec[rowIndex][opKey][columnKey] = e.value;
223
+ }
224
+ return update(updatedData, spec);
225
+ });
226
+ // Emit a structured edit event so controlled consumers capture programmatic
227
+ // batch edits (clipboard paste, fill) the same as inline edits.
228
+ state.onEdit?.({
229
+ type: "setCells",
230
+ cells: edits.map((e) => ({
231
+ rowIndex: e.rowIndex,
232
+ column: (e as any).columnKey ?? e.column,
233
+ value: e.value,
234
+ row: data[e.rowIndex],
235
+ })),
236
+ });
237
+ },
238
+ deleteSelectedRows: state.deleteSelectedRows,
239
+ addRow: state.addRow,
240
+ setUpdatedData: state.setUpdatedData,
241
+ resetChanges: state.resetChanges,
242
+ clearSelection: state.clearSelection,
243
+ scrollToRow: state.scrollToRow,
244
+ setState,
245
+ clipboardProxy: state.clipboardProxy,
246
+ setClipboardProxy(proxy) {
247
+ state.setClipboardProxy(proxy);
248
+ },
249
+ get filteredRowIndices() {
250
+ return getFilteredRowIndices();
251
+ },
252
+ };
253
+ }
254
+
255
+ function getMessageForError(e: any) {
256
+ if (e instanceof Error) return e.message;
257
+ if (typeof e === "string") return e;
258
+ if (e instanceof Object) return JSON.stringify(e);
259
+ return null;
260
+ }
261
+
262
+ export function runActionWrapper<T>(
263
+ action: TableAction<T>,
264
+ get: Getter,
265
+ set: Setter,
266
+ toaster: any,
267
+ configState: any = undefined,
268
+ ) {
269
+ console.log("runActionWrapper", action.id, configState);
270
+ const ctx = buildActionContext(get, set) as TableActionContext<T>;
271
+ if (action.disabled instanceof Function) {
272
+ if (action.disabled(ctx)) {
273
+ return;
274
+ }
275
+ } else if (action.disabled) {
276
+ return;
277
+ }
278
+ try {
279
+ const res = action.run?.(ctx, configState);
280
+ if (res instanceof Promise) {
281
+ res
282
+ .then(() => {})
283
+ .catch((e) => {
284
+ displayErrorForAction(action, e, toaster);
285
+ });
286
+ }
287
+ } catch (e) {
288
+ displayErrorForAction(action, e, toaster);
289
+ }
290
+ }
291
+
292
+ function displayErrorForAction(action: TableAction, error: any, toaster: any) {
293
+ const message =
294
+ getMessageForError(error) ?? action.errorMessage ?? "Action failed";
295
+ toaster.show({
296
+ message,
297
+ intent: "danger",
298
+ });
299
+ }
@@ -7,7 +7,9 @@
7
7
  */
8
8
  import { RegionCardinality } from "@blueprintjs/table";
9
9
  import { collectValidationErrors } from "../utils/validation";
10
- import type { SelectionCardinality, TableAction, TableActionContext } from "./types";
10
+ import type { TableAction } from "./types";
11
+ import { SelectionCardinality } from "./selection.ts";
12
+ import { TableActionContext } from "./context.ts";
11
13
 
12
14
  /** Every selection cardinality — an action targeting all of these is always
13
15
  * applicable, so the toolbar never empties out. */
@@ -1,10 +1,9 @@
1
1
  export * from "./types";
2
2
  export * from "./selection";
3
3
  export * from "./builtin";
4
- export * from "./toolbar";
5
4
  export * from "./clipboard";
6
- export * from "./filters";
7
5
  export * from "./column-controls";
8
6
  export * from "./edit-actions";
9
7
  export * from "./column-filter";
10
- export * from "./table-controls";
8
+ export * from "./table-actions.ts";
9
+ export * from "./context.ts";
@@ -1,18 +1,82 @@
1
1
  import { type Region, RegionCardinality } from "@blueprintjs/table";
2
- import type { ColumnSpec } from "../utils";
3
- import type {
4
- CellEdit,
5
- SelectionShape,
6
- TableAction,
7
- TableActionContext,
8
- TableFilter,
9
- } from "./types";
10
- import type { DataSheetStore } from "../types";
11
- import { columnFilter } from "./column-filter";
12
- import update from "immutability-helper";
2
+ import type { ColumnSpec } from "../provider";
3
+ import type { TableAction } from "./types";
4
+ import { RefObject } from "react";
5
+ import { SelectModifiers } from "../data-panel.ts";
6
+
7
+ export function buildDataViewSelection(
8
+ // index of selected item
9
+ index: number,
10
+ mods: SelectModifiers,
11
+ selection: Region[],
12
+ anchorRef: RefObject<number | null>,
13
+ enableMultipleSelection: boolean,
14
+ ): Region[] {
15
+ /** Build the selection for data views */
16
+ const current = new Set(getSelectedRowIndices(selection));
17
+ let next: Set<number>;
18
+
19
+ const isSingleSelect = current.size === 1;
20
+ const isCurrentlySelected = current.has(index);
21
+
22
+ if (!enableMultipleSelection) {
23
+ // Simpler path for single selection
24
+ next = new Set();
25
+ if (!isCurrentlySelected) {
26
+ next.add(index);
27
+ }
28
+ return rowIndicesToRegions(next);
29
+ }
30
+ // multiple selection
31
+ if (isSingleSelect && isCurrentlySelected && !mods.additive && !mods.range) {
32
+ // Clicking the only selected row with no modifiers clears the selection.
33
+ next = new Set();
34
+ } else if (mods.range && anchorRef.current != null) {
35
+ // We have a range select and an anchor: select the range between the anchor and the clicked row.
36
+ const a = anchorRef.current;
37
+ const [lo, hi] = a <= index ? [a, index] : [index, a];
38
+ // Shift extends the existing selection when combined with cmd/ctrl,
39
+ // otherwise replaces it with the range. The anchor stays put so the
40
+ // range can be re-dragged from the same origin.
41
+ next = mods.additive ? new Set(current) : new Set();
42
+ for (let i = lo; i <= hi; i++) next.add(i);
43
+ } else if (mods.additive) {
44
+ // We are adding or removing to the selection with the ctrl key
45
+ next = new Set(current);
46
+ if (next.has(index)) next.delete(index);
47
+ else next.add(index);
48
+ anchorRef.current = index;
49
+ } else {
50
+ next = new Set([index]);
51
+ anchorRef.current = index;
52
+ }
53
+
54
+ return rowIndicesToRegions(next);
55
+ }
56
+
57
+ /** Collapse a set of selected row indices into `FULL_ROWS` regions, merging
58
+ * contiguous runs into a single `{ rows: [start, end] }` range. */
59
+ export function rowIndicesToRegions(indices: Set<number>): Region[] {
60
+ const sorted = Array.from(indices).sort((a, b) => a - b);
61
+ const regions: Region[] = [];
62
+ let start: number | null = null;
63
+ let prev: number | null = null;
64
+ for (const i of sorted) {
65
+ if (start == null) {
66
+ start = prev = i;
67
+ } else if (i === prev! + 1) {
68
+ prev = i;
69
+ } else {
70
+ regions.push({ rows: [start, prev!] });
71
+ start = prev = i;
72
+ }
73
+ }
74
+ if (start != null) regions.push({ rows: [start, prev!] });
75
+ return regions;
76
+ }
13
77
 
14
78
  /** Derive the selection cardinality from the current set of selected regions.
15
- * Returns "none" when there is no active selection. */
79
+ * Returns "null" when there is no active selection. */
16
80
  export function getSelectionCardinality(
17
81
  regions: Region[],
18
82
  ): RegionCardinality | null {
@@ -26,16 +90,39 @@ export function getSelectionCardinality(
26
90
  return RegionCardinality.FULL_TABLE;
27
91
  }
28
92
 
93
+ /** Selection cardinality including the case of no active selection */
94
+ export type SelectionCardinality = RegionCardinality | "none";
95
+
96
+ /** The concrete *shape* of the current selection — richer than cardinality
97
+ * alone. The "single X" cases are exposed as resolved identity fields on the
98
+ * action context (`columnKey`, `rowIndex`, `cell`); this carries the counts. */
99
+ export interface SelectionShape {
100
+ cardinality: SelectionCardinality;
101
+ /** Number of columns the selection spans (0 when not column-scoped). */
102
+ columns: number;
103
+ /** Number of rows the selection spans (0 when not row-scoped). */
104
+ rows: number;
105
+ }
106
+
29
107
  /** Compute the concrete shape of a selection (cardinality + column/row spans). */
30
108
  export function computeSelectionShape(regions: Region[]): SelectionShape {
31
109
  const cardinality = getSelectionCardinality(regions) ?? "none";
32
- const region = regions?.[0];
33
- const cols = region?.cols;
34
- const rows = region?.rows;
110
+ let columns = 0;
111
+ let rows = 0;
112
+ for (const region of regions) {
113
+ const c = region.cols;
114
+ if (c != null) {
115
+ columns += c[1] - c[0] + 1;
116
+ }
117
+ const r = region.rows;
118
+ if (r != null) {
119
+ rows += r[1] - r[0] + 1;
120
+ }
121
+ }
35
122
  return {
36
123
  cardinality,
37
- columns: cols != null ? cols[1] - cols[0] + 1 : 0,
38
- rows: rows != null ? rows[1] - rows[0] + 1 : 0,
124
+ columns,
125
+ rows,
39
126
  };
40
127
  }
41
128
 
@@ -47,7 +134,7 @@ export function getApplicableActions<T>(
47
134
  editable: boolean,
48
135
  ): TableAction<T>[] {
49
136
  return actions.filter((action) => {
50
- if (action.requiresEditable !== false && !editable) return false;
137
+ if (action.requiresEditable && !editable) return false;
51
138
  return action.targets.includes(cardinality);
52
139
  });
53
140
  }
@@ -96,119 +183,6 @@ export function getSelectedColumnKeys(
96
183
  return Array.from(keys);
97
184
  }
98
185
 
99
- /** Construct a `TableActionContext` from the current store state.
100
- * Call this at action-run time (not render time) to ensure
101
- * the context reflects the latest state.
102
- * @param setState - Optional store setState for direct mutation. Omit for
103
- * read-only contexts (e.g., disabled checks). */
104
- export function buildActionContext<T>(
105
- state: DataSheetStore<T>,
106
- setState: (partial: Record<string, any>) => void = () => {},
107
- ): TableActionContext<T> {
108
- // Lazy-compute filteredRowIndices to avoid cost during disabled checks
109
- let _filteredRowIndices: number[] | null | undefined = undefined;
110
- function getFilteredRowIndices(): number[] | null {
111
- if (_filteredRowIndices === undefined) {
112
- _filteredRowIndices = computeFilteredRowIndices(
113
- state.data,
114
- state.updatedData,
115
- state.activeFilters,
116
- );
117
- }
118
- return _filteredRowIndices;
119
- }
120
-
121
- const shape = computeSelectionShape(state.selection);
122
-
123
- return {
124
- selection: state.selection,
125
- selectionCardinality: getSelectionCardinality(state.selection),
126
- selectionShape: shape,
127
- // Resolved single-target identity (lazy — mapping rows through filters is
128
- // only paid when accessed).
129
- get columnKey() {
130
- return shape.columns === 1
131
- ? (getSelectedColumnKeys(state.selection, state.columnSpec)[0] ?? null)
132
- : null;
133
- },
134
- get rowIndex() {
135
- return shape.rows === 1
136
- ? (getSelectedRowIndices(state.selection, getFilteredRowIndices())[0] ??
137
- null)
138
- : null;
139
- },
140
- get cell() {
141
- if (
142
- shape.cardinality !== RegionCardinality.CELLS ||
143
- shape.columns !== 1 ||
144
- shape.rows !== 1
145
- ) {
146
- return null;
147
- }
148
- const columnKey =
149
- getSelectedColumnKeys(state.selection, state.columnSpec)[0] ?? null;
150
- const rowIndex =
151
- getSelectedRowIndices(state.selection, getFilteredRowIndices())[0] ??
152
- null;
153
- return columnKey != null && rowIndex != null
154
- ? { rowIndex, columnKey }
155
- : null;
156
- },
157
- data: state.data,
158
- updatedData: state.updatedData,
159
- rowStatus: state.rowStatus,
160
- columnSpec: state.columnSpec,
161
- editable: state.editable,
162
- canDeleteRows: state.canDeleteRows,
163
- getSelectedRowIndices: () =>
164
- getSelectedRowIndices(state.selection, getFilteredRowIndices()),
165
- getSelectedColumnKeys: () =>
166
- getSelectedColumnKeys(state.selection, state.columnSpec),
167
- onCellEdited: state.onCellEdited,
168
- editCells(edits: CellEdit[]) {
169
- state.setUpdatedData((updatedData: T[]) => {
170
- const spec: Record<number, any> = {};
171
- for (const e of edits) {
172
- const rowIndex = e.rowIndex;
173
- const columnKey = (e as any).columnKey ?? e.column;
174
- if (spec[rowIndex] == null) {
175
- const op = updatedData[rowIndex] != null ? "$merge" : "$set";
176
- spec[rowIndex] = { [op]: {} };
177
- }
178
- const opKey = Object.keys(spec[rowIndex])[0];
179
- spec[rowIndex][opKey][columnKey] = e.value;
180
- }
181
- return update(updatedData, spec);
182
- });
183
- // Emit a structured edit event so controlled consumers capture programmatic
184
- // batch edits (clipboard paste, fill) the same as inline edits.
185
- state.onEdit?.({
186
- type: "setCells",
187
- cells: edits.map((e) => ({
188
- rowIndex: e.rowIndex,
189
- column: (e as any).columnKey ?? e.column,
190
- value: e.value,
191
- row: state.data[e.rowIndex],
192
- })),
193
- });
194
- },
195
- deleteSelectedRows: state.deleteSelectedRows,
196
- addRow: state.addRow,
197
- setUpdatedData: state.setUpdatedData,
198
- resetChanges: state.resetChanges,
199
- clearSelection: state.clearSelection,
200
- scrollToRow: state.scrollToRow,
201
- setState,
202
- clipboardProxy: state.clipboardProxy,
203
- setClipboardProxy(proxy) {
204
- state.setClipboardProxy(proxy);
205
- },
206
- get filteredRowIndices() {
207
- return getFilteredRowIndices();
208
- },
209
- };
210
- }
211
-
212
186
  /** Compute which data row indices are visible given the active filters.
213
187
  * Returns `null` when no filters are active (all rows visible). */
214
188
  export function computeFilteredRowIndices<T>(
@@ -263,29 +237,8 @@ export function mergeColumnActions<T>(
263
237
  return [...globalActions, ...columnActions];
264
238
  }
265
239
 
266
- /** Collect all available filters from global filters and column specs. */
267
- export function collectAllFilters<T>(
268
- globalFilters: TableFilter<T>[],
269
- columnSpec: ColumnSpec[],
270
- ): TableFilter<T>[] {
271
- const result: TableFilter<T>[] = [...globalFilters];
272
- for (const col of columnSpec) {
273
- if (col.filters != null) {
274
- for (const f of col.filters as TableFilter<T>[]) {
275
- const withKey: TableFilter<T> = {
276
- ...f,
277
- columnKey: f.columnKey ?? col.key,
278
- };
279
- if (!result.some((r) => r.id === withKey.id)) {
280
- result.push(withKey);
281
- }
282
- }
283
- }
284
- // Auto-generate the built-in operator filter for a `filterable` column,
285
- // unless the column already supplies an explicit filter.
286
- if (col.filterable && !result.some((r) => r.columnKey === col.key)) {
287
- result.push(columnFilter(col) as TableFilter<T>);
288
- }
289
- }
290
- return result;
240
+ export function range(arr: number[]) {
241
+ if (arr.length != 2) throw new Error("Range must have two elements");
242
+ const [start, end] = arr;
243
+ return Array.from({ length: end - start + 1 }, (_, i) => i + start);
291
244
  }
@@ -7,7 +7,6 @@
7
7
  import h from "@macrostrat/hyper";
8
8
  import { useState } from "react";
9
9
  import { Button, ControlGroup, InputGroup } from "@blueprintjs/core";
10
- import { RegionCardinality } from "@blueprintjs/table";
11
10
  import { useSelector } from "../provider";
12
11
  import type { TableAction } from "./types";
13
12
  import { ALL_CARDINALITIES } from "./edit-actions";
@@ -35,29 +34,6 @@ function ScrollToRowControl() {
35
34
  ]);
36
35
  }
37
36
 
38
- /** Clear the current selection. Auto-included: appears (on the right, being a
39
- * general action) whenever something is selected — targets every cardinality
40
- * except `"none"`. */
41
- export const clearSelectionAction: TableAction = {
42
- id: "clear-selection",
43
- name: "Clear selection",
44
- icon: "cross",
45
- requiresEditable: false,
46
- targets: [
47
- RegionCardinality.CELLS,
48
- RegionCardinality.FULL_ROWS,
49
- RegionCardinality.FULL_COLUMNS,
50
- RegionCardinality.FULL_TABLE,
51
- ],
52
- // Empty selection maps to the FULL_TABLE cardinality, so the action still
53
- // shows with nothing selected — grey it out there rather than act on nothing.
54
- // (`disabled` is probed with a bare state, hence the `?? []`.)
55
- disabled: (s: any) => (s.selection?.length ?? 0) === 0,
56
- run(ctx) {
57
- ctx.setState({ selection: [], focusedCell: null, topLeftCell: null });
58
- },
59
- };
60
-
61
37
  /** Scroll to a 1-based row number. Meaningful for large, addressable sources;
62
38
  * add it to `actions`. Present in the toolbar regardless of selection. */
63
39
  export const scrollToRowAction: TableAction = {