@macrostrat/data-sheet 4.0.0-dev2 → 4.0.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 (212) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +44 -0
  3. package/dist/actions/builtin.cjs +36 -5
  4. package/dist/actions/builtin.cjs.map +1 -1
  5. package/dist/actions/builtin.d.ts +3 -2
  6. package/dist/actions/builtin.js +36 -5
  7. package/dist/actions/builtin.js.map +1 -1
  8. package/dist/actions/column-controls.cjs +149 -0
  9. package/dist/actions/column-controls.cjs.map +1 -0
  10. package/dist/actions/column-controls.d.ts +11 -0
  11. package/dist/actions/column-controls.js +147 -0
  12. package/dist/actions/column-controls.js.map +1 -0
  13. package/dist/actions/column-filter.cjs +68 -0
  14. package/dist/actions/column-filter.cjs.map +1 -0
  15. package/dist/actions/column-filter.d.ts +11 -0
  16. package/dist/actions/column-filter.js +66 -0
  17. package/dist/actions/column-filter.js.map +1 -0
  18. package/dist/actions/edit-actions.cjs +42 -0
  19. package/dist/actions/edit-actions.cjs.map +1 -0
  20. package/dist/actions/edit-actions.d.ts +14 -0
  21. package/dist/actions/edit-actions.js +42 -0
  22. package/dist/actions/edit-actions.js.map +1 -0
  23. package/dist/actions/filters.cjs +39 -23
  24. package/dist/actions/filters.cjs.map +1 -1
  25. package/dist/actions/filters.d.ts +6 -3
  26. package/dist/actions/filters.js +41 -25
  27. package/dist/actions/filters.js.map +1 -1
  28. package/dist/actions/index.d.ts +4 -0
  29. package/dist/actions/selection.cjs +39 -30
  30. package/dist/actions/selection.cjs.map +1 -1
  31. package/dist/actions/selection.d.ts +3 -7
  32. package/dist/actions/selection.js +39 -30
  33. package/dist/actions/selection.js.map +1 -1
  34. package/dist/actions/table-controls.cjs +41 -0
  35. package/dist/actions/table-controls.cjs.map +1 -0
  36. package/dist/actions/table-controls.d.ts +4 -0
  37. package/dist/actions/table-controls.js +39 -0
  38. package/dist/actions/table-controls.js.map +1 -0
  39. package/dist/actions/toolbar.cjs +69 -10
  40. package/dist/actions/toolbar.cjs.map +1 -1
  41. package/dist/actions/toolbar.d.ts +9 -6
  42. package/dist/actions/toolbar.js +69 -10
  43. package/dist/actions/toolbar.js.map +1 -1
  44. package/dist/actions/toolbar.module.sass.cjs +12 -7
  45. package/dist/actions/toolbar.module.sass.cjs.map +1 -1
  46. package/dist/actions/toolbar.module.sass.js +12 -7
  47. package/dist/actions/toolbar.module.sass.js.map +1 -1
  48. package/dist/actions/types.d.ts +49 -4
  49. package/dist/cell-renderer.cjs +150 -14
  50. package/dist/cell-renderer.cjs.map +1 -1
  51. package/dist/cell-renderer.d.ts +1 -1
  52. package/dist/cell-renderer.js +151 -15
  53. package/dist/cell-renderer.js.map +1 -1
  54. package/dist/components/cell-detail.cjs +24 -0
  55. package/dist/components/cell-detail.cjs.map +1 -0
  56. package/dist/components/cell-detail.d.ts +12 -0
  57. package/dist/components/cell-detail.js +24 -0
  58. package/dist/components/cell-detail.js.map +1 -0
  59. package/dist/components/editor-popup.cjs +28 -24
  60. package/dist/components/editor-popup.cjs.map +1 -1
  61. package/dist/components/editor-popup.js +29 -25
  62. package/dist/components/editor-popup.js.map +1 -1
  63. package/dist/components/index.d.ts +2 -2
  64. package/dist/components/text-areas.cjs +51 -7
  65. package/dist/components/text-areas.cjs.map +1 -1
  66. package/dist/components/text-areas.d.ts +4 -1
  67. package/dist/components/text-areas.js +51 -7
  68. package/dist/components/text-areas.js.map +1 -1
  69. package/dist/components/view-info.cjs +46 -9
  70. package/dist/components/view-info.cjs.map +1 -1
  71. package/dist/components/view-info.d.ts +5 -1
  72. package/dist/components/view-info.js +47 -10
  73. package/dist/components/view-info.js.map +1 -1
  74. package/dist/core.cjs +167 -73
  75. package/dist/core.cjs.map +1 -1
  76. package/dist/core.d.ts +53 -6
  77. package/dist/core.js +169 -75
  78. package/dist/core.js.map +1 -1
  79. package/dist/data-sheet.css +72 -123
  80. package/dist/filters/operators.cjs +91 -0
  81. package/dist/filters/operators.cjs.map +1 -0
  82. package/dist/filters/operators.d.ts +17 -0
  83. package/dist/filters/operators.js +91 -0
  84. package/dist/filters/operators.js.map +1 -0
  85. package/dist/index.cjs +38 -10
  86. package/dist/index.cjs.map +1 -1
  87. package/dist/index.d.ts +2 -0
  88. package/dist/index.js +44 -16
  89. package/dist/index.js.map +1 -1
  90. package/dist/main.module.sass.cjs +13 -12
  91. package/dist/main.module.sass.cjs.map +1 -1
  92. package/dist/main.module.sass.js +13 -12
  93. package/dist/main.module.sass.js.map +1 -1
  94. package/dist/postgrest-table/data-loaders.cjs +255 -146
  95. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  96. package/dist/postgrest-table/data-loaders.d.ts +144 -46
  97. package/dist/postgrest-table/data-loaders.js +259 -150
  98. package/dist/postgrest-table/data-loaders.js.map +1 -1
  99. package/dist/postgrest-table/index.cjs +58 -169
  100. package/dist/postgrest-table/index.cjs.map +1 -1
  101. package/dist/postgrest-table/index.d.ts +5 -2
  102. package/dist/postgrest-table/index.js +63 -174
  103. package/dist/postgrest-table/index.js.map +1 -1
  104. package/dist/postgrest-table/loading-utils.cjs +0 -4
  105. package/dist/postgrest-table/loading-utils.cjs.map +1 -1
  106. package/dist/postgrest-table/loading-utils.js +1 -5
  107. package/dist/postgrest-table/loading-utils.js.map +1 -1
  108. package/dist/postgrest-table/main.module.sass.cjs +14 -13
  109. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  110. package/dist/postgrest-table/main.module.sass.js +14 -13
  111. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  112. package/dist/provider.cjs +68 -11
  113. package/dist/provider.cjs.map +1 -1
  114. package/dist/provider.d.ts +25 -0
  115. package/dist/provider.js +68 -11
  116. package/dist/provider.js.map +1 -1
  117. package/dist/renderers/column-header-cell.cjs +95 -75
  118. package/dist/renderers/column-header-cell.cjs.map +1 -1
  119. package/dist/renderers/column-header-cell.d.ts +3 -18
  120. package/dist/renderers/column-header-cell.js +98 -78
  121. package/dist/renderers/column-header-cell.js.map +1 -1
  122. package/dist/renderers/index.d.ts +0 -1
  123. package/dist/types.cjs +2 -0
  124. package/dist/types.cjs.map +1 -1
  125. package/dist/types.d.ts +84 -1
  126. package/dist/types.js +2 -0
  127. package/dist/types.js.map +1 -1
  128. package/dist/utils/column-spec.cjs.map +1 -1
  129. package/dist/utils/column-spec.d.ts +101 -1
  130. package/dist/utils/column-spec.js.map +1 -1
  131. package/dist/utils/debug-overlay.cjs +24 -0
  132. package/dist/utils/debug-overlay.cjs.map +1 -0
  133. package/dist/utils/debug-overlay.d.ts +4 -0
  134. package/dist/utils/debug-overlay.js +24 -0
  135. package/dist/utils/debug-overlay.js.map +1 -0
  136. package/dist/utils/index.cjs.map +1 -1
  137. package/dist/utils/index.d.ts +2 -0
  138. package/dist/utils/index.js.map +1 -1
  139. package/dist/utils/key-handling.cjs +52 -6
  140. package/dist/utils/key-handling.cjs.map +1 -1
  141. package/dist/utils/key-handling.js +52 -6
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/utils/validation.cjs +41 -0
  144. package/dist/utils/validation.cjs.map +1 -0
  145. package/dist/utils/validation.d.ts +26 -0
  146. package/dist/utils/validation.js +41 -0
  147. package/dist/utils/validation.js.map +1 -0
  148. package/dist/zustand-store.cjs +119 -61
  149. package/dist/zustand-store.cjs.map +1 -1
  150. package/dist/zustand-store.js +121 -63
  151. package/dist/zustand-store.js.map +1 -1
  152. package/package.json +6 -6
  153. package/src/actions/builtin.ts +62 -7
  154. package/src/actions/column-controls.ts +185 -0
  155. package/src/actions/column-filter.ts +104 -0
  156. package/src/actions/edit-actions.ts +67 -0
  157. package/src/actions/filters.ts +54 -28
  158. package/src/actions/index.ts +4 -0
  159. package/src/actions/selection.ts +59 -52
  160. package/src/actions/table-controls.ts +46 -0
  161. package/src/actions/toolbar.module.sass +22 -2
  162. package/src/actions/toolbar.ts +113 -16
  163. package/src/actions/types.ts +49 -4
  164. package/src/cell-renderer.ts +211 -16
  165. package/src/components/cell-detail.ts +30 -0
  166. package/src/components/editor-popup.ts +39 -25
  167. package/src/components/index.ts +2 -2
  168. package/src/components/text-areas.ts +80 -16
  169. package/src/components/view-info.ts +63 -4
  170. package/src/core.ts +321 -103
  171. package/src/filters/operators.ts +127 -0
  172. package/src/index.ts +2 -0
  173. package/src/main.module.sass +6 -0
  174. package/src/postgrest-table/data-loaders.ts +459 -209
  175. package/src/postgrest-table/index.ts +81 -254
  176. package/src/postgrest-table/main.module.sass +11 -0
  177. package/src/provider.ts +109 -12
  178. package/src/renderers/column-header-cell.ts +60 -272
  179. package/src/renderers/index.ts +0 -1
  180. package/src/types.ts +76 -2
  181. package/src/utils/column-spec.ts +112 -1
  182. package/src/utils/debug-overlay.ts +22 -0
  183. package/src/utils/index.ts +2 -0
  184. package/src/utils/key-handling.ts +70 -6
  185. package/src/utils/validation.ts +72 -0
  186. package/src/zustand-store.ts +167 -60
  187. package/dist/components/actions.cjs +0 -12
  188. package/dist/components/actions.cjs.map +0 -1
  189. package/dist/components/actions.d.ts +0 -2
  190. package/dist/components/actions.js +0 -12
  191. package/dist/components/actions.js.map +0 -1
  192. package/dist/components/actions.module.sass.cjs +0 -12
  193. package/dist/components/actions.module.sass.cjs.map +0 -1
  194. package/dist/components/actions.module.sass.js +0 -11
  195. package/dist/components/actions.module.sass.js.map +0 -1
  196. package/dist/postgrest-table/lazy-loader-table.cjs +0 -27
  197. package/dist/postgrest-table/lazy-loader-table.cjs.map +0 -1
  198. package/dist/postgrest-table/lazy-loader-table.d.ts +0 -3
  199. package/dist/postgrest-table/lazy-loader-table.js +0 -27
  200. package/dist/postgrest-table/lazy-loader-table.js.map +0 -1
  201. package/dist/renderers/column-header-cell-postgrest.cjs +0 -216
  202. package/dist/renderers/column-header-cell-postgrest.cjs.map +0 -1
  203. package/dist/renderers/column-header-cell-postgrest.d.ts +0 -13
  204. package/dist/renderers/column-header-cell-postgrest.js +0 -216
  205. package/dist/renderers/column-header-cell-postgrest.js.map +0 -1
  206. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +0 -23
  207. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +0 -1
  208. package/dist/renderers/column-header-cell-postgrest.module.sass.js +0 -22
  209. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +0 -1
  210. package/src/components/actions.ts +0 -9
  211. package/src/postgrest-table/lazy-loader-table.ts +0 -25
  212. package/src/renderers/column-header-cell-postgrest.ts +0 -282
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Built-in `FULL_COLUMNS`-scoped controls — sort and filter — expressed as
3
+ * `TableAction`s with a `render` (live control) rather than a one-shot `run`.
4
+ * They read the single selected column from the action context and are gated
5
+ * by the column's `sortable` / `filterable` flags. Overridable: consumers can
6
+ * omit them, replace them, or add their own controls at any cardinality.
7
+ */
8
+ import h from "@macrostrat/hyper";
9
+ import { useMemo } from "react";
10
+ import { Button, Menu, MenuItem, PopoverNext } from "@blueprintjs/core";
11
+ import { RegionCardinality } from "@blueprintjs/table";
12
+ import { useSelector, useStoreAPI } from "../provider";
13
+ import type { TableAction, TableActionContext, TableFilter } from "./types";
14
+ import type { ColumnSpec } from "../utils/column-spec";
15
+ import { columnFilter } from "./column-filter";
16
+
17
+ function selectedColumn(ctx: TableActionContext) {
18
+ const key = ctx.getSelectedColumnKeys()[0];
19
+ if (key == null) return null;
20
+ return ctx.columnSpec.find((c) => c.key === key) ?? null;
21
+ }
22
+
23
+ // ---- Sort ----
24
+
25
+ function ColumnSortControl({ columnKey }: { columnKey: string }) {
26
+ const storeAPI = useStoreAPI();
27
+ const sort = useSelector((s) => s.columnSorts.find((x) => x.key === columnKey));
28
+ const set = (ascending: boolean | null) =>
29
+ storeAPI.getState().setColumnSort(columnKey, ascending);
30
+
31
+ const icon = sort == null ? "sort" : sort.ascending ? "sort-asc" : "sort-desc";
32
+ const label =
33
+ sort == null ? "Sort" : sort.ascending ? "Ascending" : "Descending";
34
+
35
+ return h(
36
+ PopoverNext,
37
+ {
38
+ placement: "bottom-start",
39
+ content: h(Menu, [
40
+ h(MenuItem, {
41
+ key: "asc",
42
+ text: "Ascending",
43
+ icon: "sort-asc",
44
+ active: sort?.ascending === true,
45
+ onClick: () => set(true),
46
+ }),
47
+ h(MenuItem, {
48
+ key: "desc",
49
+ text: "Descending",
50
+ icon: "sort-desc",
51
+ active: sort?.ascending === false,
52
+ onClick: () => set(false),
53
+ }),
54
+ h(MenuItem, {
55
+ key: "clear",
56
+ text: "Clear sort",
57
+ icon: "cross",
58
+ disabled: sort == null,
59
+ onClick: () => set(null),
60
+ }),
61
+ ]),
62
+ },
63
+ h(
64
+ Button,
65
+ {
66
+ small: true,
67
+ minimal: true,
68
+ icon,
69
+ rightIcon: "caret-down",
70
+ intent: sort != null ? "primary" : "none",
71
+ },
72
+ label,
73
+ ),
74
+ );
75
+ }
76
+
77
+ /** Single-column sort control. Gated by `col.sortable`. */
78
+ export const columnSortAction: TableAction = {
79
+ id: "column-sort",
80
+ name: "Sort",
81
+ icon: "sort",
82
+ targets: [RegionCardinality.FULL_COLUMNS],
83
+ requiresEditable: false,
84
+ // Only a single column (within the FULL_COLUMNS target), not multi-column.
85
+ appliesTo: (ctx) => ctx.columnKey != null,
86
+ render(ctx) {
87
+ const col = selectedColumn(ctx);
88
+ if (!col?.sortable) return null;
89
+ return h(ColumnSortControl, { columnKey: col.key });
90
+ },
91
+ };
92
+
93
+ // ---- Filter ----
94
+
95
+ /** The filter offered for a column: its own rich `TableFilter` (from
96
+ * `col.filters`) when present — so the header matches the top bar and the rich
97
+ * filter is prioritized — else the built-in operator `columnFilter`. */
98
+ function resolveColumnFilter(col: ColumnSpec): TableFilter {
99
+ const rich = (col.filters as TableFilter[] | undefined)?.find(
100
+ (f) => (f.columnKey ?? col.key) === col.key,
101
+ );
102
+ if (rich != null) return { ...rich, columnKey: rich.columnKey ?? col.key };
103
+ return columnFilter(col);
104
+ }
105
+
106
+ function ColumnFilterControl({ col }: { col: ColumnSpec }) {
107
+ const storeAPI = useStoreAPI();
108
+ // Same `TableFilter` the FilterBar and any provider consume, so the header,
109
+ // the bar, and the query stay in sync (rich filter prioritized).
110
+ const filter = useMemo(() => resolveColumnFilter(col), [col]);
111
+ const state = useSelector((s) => s.activeFilters.get(filter.id)?.state);
112
+ const isActive = state != null;
113
+ const summary = isActive ? filter.describeState?.(state) : null;
114
+
115
+ const setState = (next: any) => {
116
+ const store = storeAPI.getState();
117
+ // An empty operator-filter value clears; otherwise activate/update.
118
+ if (next == null || next.value === "") {
119
+ store.removeFilter(filter.id);
120
+ } else {
121
+ store.setFilter(filter.id, filter, next);
122
+ }
123
+ };
124
+
125
+ const label = isActive ? String(summary ?? filter.name) : "Filter";
126
+
127
+ return h(
128
+ PopoverNext,
129
+ {
130
+ placement: "bottom-start",
131
+ content: h(
132
+ "div",
133
+ { style: { padding: "6px", minWidth: "220px" } },
134
+ filter.filterForm != null
135
+ ? h(filter.filterForm, {
136
+ state: state ?? filter.defaultState,
137
+ setState,
138
+ })
139
+ : null,
140
+ ),
141
+ },
142
+ h(
143
+ Button,
144
+ {
145
+ small: true,
146
+ minimal: true,
147
+ icon: "filter",
148
+ rightIcon: "caret-down",
149
+ intent: isActive ? "warning" : "none",
150
+ },
151
+ label,
152
+ ),
153
+ );
154
+ }
155
+
156
+ /** Single-column filter control. Gated by `col.filterable`. */
157
+ export const columnFilterAction: TableAction = {
158
+ id: "column-filter",
159
+ name: "Filter",
160
+ icon: "filter",
161
+ targets: [RegionCardinality.FULL_COLUMNS],
162
+ requiresEditable: false,
163
+ appliesTo: (ctx) => ctx.columnKey != null,
164
+ render(ctx) {
165
+ const col = selectedColumn(ctx);
166
+ if (col == null || !isColumnFilterable(col)) return null;
167
+ return h(ColumnFilterControl, { col });
168
+ },
169
+ };
170
+
171
+ /** A column offers a filter when it declares `filterable`, or supplies its own
172
+ * `filters`. */
173
+ export function isColumnFilterable(col: ColumnSpec): boolean {
174
+ return (
175
+ col.filterable === true ||
176
+ typeof col.filterable === "object" ||
177
+ (Array.isArray(col.filters) && col.filters.length > 0)
178
+ );
179
+ }
180
+
181
+ /** The built-in column controls (sort + filter), for inclusion in `actions`. */
182
+ export const columnControlActions: TableAction[] = [
183
+ columnSortAction,
184
+ columnFilterAction,
185
+ ];
@@ -0,0 +1,104 @@
1
+ /**
2
+ * The built-in, operator-first column filter — the core generalization of the
3
+ * PostgREST column header's per-type operator model. `columnFilter(col)` yields
4
+ * a standard `TableFilter` whose state is `{ operator, value }`, so it flows
5
+ * through the same store, `FilterBar`, and provider seam as any other
6
+ * filter. One definition serves both targets: `predicate` filters in-memory
7
+ * tables client-side, while `columnKey` + `state` are what a server provider
8
+ * translates to a query. Operators are inferred from the column's `dataType`
9
+ * (overridable via `filterable: { operators }`).
10
+ */
11
+ import hyper from "@macrostrat/hyper";
12
+ import { useEffect, useState } from "react";
13
+ import { ControlGroup, FormGroup, HTMLSelect, InputGroup } from "@blueprintjs/core";
14
+ import type { ColumnSpec } from "../utils/column-spec";
15
+ import type { TableFilter } from "./types";
16
+ import {
17
+ FilterOperator,
18
+ getOperatorsForColumn,
19
+ OPERATOR_LABELS,
20
+ testFilterOperator,
21
+ } from "../filters/operators";
22
+
23
+ const h = hyper;
24
+
25
+ export interface ColumnFilterState {
26
+ operator: FilterOperator;
27
+ value: string;
28
+ }
29
+
30
+ /** Stable filter id for a column's built-in operator filter. */
31
+ export function columnFilterId(key: string): string {
32
+ return `column-filter:${key}`;
33
+ }
34
+
35
+ /** Generate the built-in operator filter for a `filterable` column. */
36
+ export function columnFilter(
37
+ col: ColumnSpec,
38
+ ): TableFilter<any, ColumnFilterState> {
39
+ const operators = getOperatorsForColumn(col);
40
+ return {
41
+ id: columnFilterId(col.key),
42
+ name: col.name,
43
+ icon: "filter",
44
+ columnKey: col.key,
45
+ defaultState: { operator: operators[0], value: "" },
46
+ describeState: (s) =>
47
+ s?.value
48
+ ? `${OPERATOR_LABELS[s.operator] ?? s.operator} ${s.value}`
49
+ : null,
50
+ predicate: (row, s) =>
51
+ testFilterOperator(row?.[col.key], s.operator, s.value),
52
+ filterForm: ({ state, setState }) =>
53
+ h(ColumnFilterForm, { operators, state, setState }),
54
+ };
55
+ }
56
+
57
+ function ColumnFilterForm({
58
+ operators,
59
+ state,
60
+ setState,
61
+ }: {
62
+ operators: FilterOperator[];
63
+ state: ColumnFilterState;
64
+ setState: (s: ColumnFilterState) => void;
65
+ }) {
66
+ // The value is edited in local state and only committed (which triggers a
67
+ // re-fetch for server sources) on blur or Enter — not on every keystroke.
68
+ // The operator select commits immediately (a discrete choice).
69
+ const [draft, setDraft] = useState(state.value ?? "");
70
+ useEffect(() => setDraft(state.value ?? ""), [state.value]);
71
+ const commit = () => {
72
+ if (draft !== state.value) setState({ ...state, value: draft });
73
+ };
74
+ return h(
75
+ FormGroup,
76
+ { label: "Filter" },
77
+ h(ControlGroup, { fill: true }, [
78
+ h(HTMLSelect, {
79
+ key: "op",
80
+ value: state.operator,
81
+ options: operators.map((op) => ({
82
+ value: op,
83
+ label: OPERATOR_LABELS[op] ?? op,
84
+ })),
85
+ onChange: (e) =>
86
+ setState({
87
+ ...state,
88
+ operator: e.currentTarget.value as FilterOperator,
89
+ }),
90
+ }),
91
+ h(InputGroup, {
92
+ key: "val",
93
+ value: draft,
94
+ placeholder: "value",
95
+ autoFocus: true,
96
+ onChange: (e) => setDraft(e.target.value),
97
+ onBlur: commit,
98
+ onKeyDown: (e) => {
99
+ if (e.key === "Enter") commit();
100
+ },
101
+ }),
102
+ ]),
103
+ );
104
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Built-in Save action. It targets every cardinality (including no selection /
3
+ * full table), so it's present in the toolbar at all times — which keeps the
4
+ * toolbar mounted regardless of selection, avoiding show/hide layout motion.
5
+ * It's disabled (not hidden) when there are no pending changes. (Reset already
6
+ * lives in `builtin.ts` as `resetChangesAction`.)
7
+ */
8
+ import { RegionCardinality } from "@blueprintjs/table";
9
+ import { collectValidationErrors } from "../utils/validation";
10
+ import type { SelectionCardinality, TableAction, TableActionContext } from "./types";
11
+
12
+ /** Every selection cardinality — an action targeting all of these is always
13
+ * applicable, so the toolbar never empties out. */
14
+ export const ALL_CARDINALITIES: SelectionCardinality[] = [
15
+ "none",
16
+ RegionCardinality.FULL_TABLE,
17
+ RegionCardinality.CELLS,
18
+ RegionCardinality.FULL_ROWS,
19
+ RegionCardinality.FULL_COLUMNS,
20
+ ];
21
+
22
+ /** Whether the table has uncommitted edits (cell overlays or added/deleted
23
+ * rows). Accepts either the store state or an action context (both carry
24
+ * `updatedData` / `rowStatus`). */
25
+ export function hasPendingChanges(s: {
26
+ updatedData?: any[];
27
+ rowStatus?: readonly string[];
28
+ }): boolean {
29
+ const edited =
30
+ s.updatedData?.some(
31
+ (r) => r != null && typeof r === "object" && Object.keys(r).length > 0,
32
+ ) ?? false;
33
+ // TableElementStatus enum values are the strings "added" / "deleted";
34
+ // compared literally to avoid a value import (and an extra module cycle).
35
+ const structural =
36
+ s.rowStatus?.some((st) => st === "added" || st === "deleted") ?? false;
37
+ return edited || structural;
38
+ }
39
+
40
+ /** Create a save action bound to a persistence handler. Always present;
41
+ * disabled when there are no pending changes. */
42
+ export function createSaveAction<T = any>(
43
+ onSave: (ctx: TableActionContext<T>) => void | Promise<void>,
44
+ ): TableAction<T> {
45
+ return {
46
+ id: "save-changes",
47
+ name: "Save",
48
+ icon: "floppy-disk",
49
+ intent: "primary",
50
+ targets: ALL_CARDINALITIES,
51
+ requiresEditable: true,
52
+ disabled: (s: any) => !hasPendingChanges(s),
53
+ run: (ctx) => {
54
+ // Validation errors block saving (warnings don't). The offending cells
55
+ // are already highlighted; refuse and surface a summary.
56
+ const errors = collectValidationErrors(ctx as any);
57
+ if (errors.length > 0) {
58
+ throw new Error(
59
+ `Cannot save: ${errors.length} validation error${
60
+ errors.length === 1 ? "" : "s"
61
+ }. Fix the highlighted cells.`,
62
+ );
63
+ }
64
+ return onSave(ctx);
65
+ },
66
+ };
67
+ }
@@ -7,55 +7,77 @@ import {
7
7
  Tag,
8
8
  } from "@blueprintjs/core";
9
9
  import { useMemo, useState } from "react";
10
+ import { RegionCardinality } from "@blueprintjs/table";
10
11
  import h from "./toolbar.module.sass";
11
12
  import { useSelector, useStoreAPI } from "../provider";
12
- import { collectAllFilters } from "./selection";
13
+ import { collectAllFilters, getSelectionCardinality } from "./selection";
13
14
  import type { TableFilter } from "./types";
14
15
 
15
- /** Bar showing active filters as interactive tags, with a button to
16
- * add new filters from the available set (global + column-spec). */
16
+ /** The single view-state bar: active **sorts** and **filters** shown as
17
+ * removable tags (filters reconfigurable in place via their `filterForm`). The
18
+ * "Add filter" button offers only **table-level** filters (no `columnKey`) and
19
+ * only when no column is selected — per-column filters are added/configured
20
+ * from the column header dropdown instead, so there's no redundancy. */
17
21
  export function FilterBar<T>({ filters = [] }: { filters?: TableFilter<T>[] }) {
18
22
  const columnSpec = useSelector((state) => state.columnSpec);
19
23
  const storeAPI = useStoreAPI();
20
24
  const activeFilters = useSelector((state) => state.activeFilters);
25
+ const columnSorts = useSelector((state) => state.columnSorts);
26
+ const selection = useSelector((state) => state.selection);
27
+
28
+ const columnSelected =
29
+ getSelectionCardinality(selection) === RegionCardinality.FULL_COLUMNS;
21
30
 
22
31
  const allFilters = useMemo(
23
32
  () => collectAllFilters(filters, columnSpec),
24
33
  [filters, columnSpec],
25
34
  );
26
35
 
27
- const activeIds = useMemo(() => new Set(activeFilters.keys()), [activeFilters]);
36
+ const activeIds = useMemo(
37
+ () => new Set(activeFilters.keys()),
38
+ [activeFilters],
39
+ );
28
40
 
41
+ // Only table-level filters are addable from the bar; column filters live in
42
+ // the column header dropdown.
29
43
  const availableFilters = useMemo(
30
- () => allFilters.filter((f) => !activeIds.has(f.id)),
44
+ () => allFilters.filter((f) => f.columnKey == null && !activeIds.has(f.id)),
31
45
  [allFilters, activeIds],
32
46
  );
47
+ const showAdd = !columnSelected && availableFilters.length > 0;
33
48
 
34
- const hasActive = activeFilters.size > 0;
49
+ const hasAnything =
50
+ columnSorts.length > 0 || activeFilters.size > 0 || showAdd;
51
+ if (!hasAnything) return null;
35
52
 
36
53
  return h("div.filter-bar", [
37
54
  h(
38
- ButtonGroup,
39
- { minimal: true },
55
+ "div.group.sorts",
56
+ columnSorts.map((s) =>
57
+ h(
58
+ Tag,
59
+ {
60
+ key: `sort-${s.key}`,
61
+ icon: s.ascending ? "sort-asc" : "sort-desc",
62
+ intent: "primary",
63
+ minimal: true,
64
+ onRemove() {
65
+ storeAPI.getState().setColumnSort(s.key, null);
66
+ },
67
+ },
68
+ `${s.key}: ${s.ascending ? "Ascending" : "Descending"}`,
69
+ ),
70
+ ),
71
+ ),
72
+ h(
73
+ "div.group.filters",
40
74
  Array.from(activeFilters.entries()).map(([id, entry]) =>
41
75
  h(ActiveFilterChip, { key: id, filterId: id, entry }),
42
76
  ),
43
77
  ),
44
- h.if(availableFilters.length > 0)(AddFilterPopover, {
78
+ h.if(showAdd)(AddFilterPopover, {
45
79
  filters: availableFilters,
46
80
  }),
47
- h.if(hasActive)(
48
- Button,
49
- {
50
- minimal: true,
51
- small: true,
52
- icon: "cross",
53
- onClick() {
54
- storeAPI.getState().clearFilters();
55
- },
56
- },
57
- "Clear filters",
58
- ),
59
81
  ]);
60
82
  }
61
83
 
@@ -72,6 +94,10 @@ function ActiveFilterChip<T>({
72
94
  const [configOpen, setConfigOpen] = useState(false);
73
95
  const { filter, state: filterState } = entry;
74
96
 
97
+ // Summarize the active filter's window (e.g. "0–250") next to its name, so
98
+ // the tag conveys not just what is filtered but the current setting.
99
+ const summary = filter.describeState?.(filterState);
100
+
75
101
  const tag = h(
76
102
  Tag,
77
103
  {
@@ -83,7 +109,12 @@ function ActiveFilterChip<T>({
83
109
  intent: "primary",
84
110
  onClick: filter.filterForm ? () => setConfigOpen(!configOpen) : undefined,
85
111
  },
86
- filter.name,
112
+ [
113
+ filter.name,
114
+ summary != null && summary !== ""
115
+ ? h("span.filter-window", [": ", summary])
116
+ : null,
117
+ ],
87
118
  );
88
119
 
89
120
  if (filter.filterForm == null) return tag;
@@ -111,11 +142,7 @@ function ActiveFilterChip<T>({
111
142
  }
112
143
 
113
144
  /** Popover menu listing available (not-yet-active) filters. */
114
- function AddFilterPopover<T>({
115
- filters,
116
- }: {
117
- filters: TableFilter<T>[];
118
- }) {
145
+ function AddFilterPopover<T>({ filters }: { filters: TableFilter<T>[] }) {
119
146
  const storeAPI = useStoreAPI();
120
147
 
121
148
  return h(
@@ -147,4 +174,3 @@ function AddFilterPopover<T>({
147
174
  ),
148
175
  );
149
176
  }
150
-
@@ -4,3 +4,7 @@ export * from "./builtin";
4
4
  export * from "./toolbar";
5
5
  export * from "./clipboard";
6
6
  export * from "./filters";
7
+ export * from "./column-controls";
8
+ export * from "./edit-actions";
9
+ export * from "./column-filter";
10
+ export * from "./table-controls";
@@ -1,15 +1,14 @@
1
1
  import { type Region, RegionCardinality } from "@blueprintjs/table";
2
2
  import type { ColumnSpec } from "../utils";
3
3
  import type {
4
- ActiveFilterEntry,
5
4
  CellEdit,
6
- ColumnSort,
7
- SelectionCardinality,
5
+ SelectionShape,
8
6
  TableAction,
9
7
  TableActionContext,
10
8
  TableFilter,
11
9
  } from "./types";
12
10
  import type { DataSheetStore } from "../types";
11
+ import { columnFilter } from "./column-filter";
13
12
  import update from "immutability-helper";
14
13
 
15
14
  /** Derive the selection cardinality from the current set of selected regions.
@@ -27,6 +26,19 @@ export function getSelectionCardinality(
27
26
  return RegionCardinality.FULL_TABLE;
28
27
  }
29
28
 
29
+ /** Compute the concrete shape of a selection (cardinality + column/row spans). */
30
+ export function computeSelectionShape(regions: Region[]): SelectionShape {
31
+ const cardinality = getSelectionCardinality(regions) ?? "none";
32
+ const region = regions?.[0];
33
+ const cols = region?.cols;
34
+ const rows = region?.rows;
35
+ return {
36
+ cardinality,
37
+ columns: cols != null ? cols[1] - cols[0] + 1 : 0,
38
+ rows: rows != null ? rows[1] - rows[0] + 1 : 0,
39
+ };
40
+ }
41
+
30
42
  /** Filter actions to those applicable for the current selection cardinality
31
43
  * and edit mode. */
32
44
  export function getApplicableActions<T>(
@@ -58,9 +70,7 @@ export function getSelectedRowIndices(
58
70
  }
59
71
  const sorted = Array.from(indices).sort((a, b) => a - b);
60
72
  if (filteredRowIndices == null) return sorted;
61
- return sorted
62
- .map((i) => filteredRowIndices[i])
63
- .filter((i) => i != null);
73
+ return sorted.map((i) => filteredRowIndices[i]).filter((i) => i != null);
64
74
  }
65
75
 
66
76
  /** Extract column keys covered by a set of selected regions. */
@@ -108,14 +118,48 @@ export function buildActionContext<T>(
108
118
  return _filteredRowIndices;
109
119
  }
110
120
 
121
+ const shape = computeSelectionShape(state.selection);
122
+
111
123
  return {
112
124
  selection: state.selection,
113
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
+ },
114
157
  data: state.data,
115
158
  updatedData: state.updatedData,
116
159
  rowStatus: state.rowStatus,
117
160
  columnSpec: state.columnSpec,
118
161
  editable: state.editable,
162
+ canDeleteRows: state.canDeleteRows,
119
163
  getSelectedRowIndices: () =>
120
164
  getSelectedRowIndices(state.selection, getFilteredRowIndices()),
121
165
  getSelectedColumnKeys: () =>
@@ -215,57 +259,20 @@ export function collectAllFilters<T>(
215
259
  for (const col of columnSpec) {
216
260
  if (col.filters != null) {
217
261
  for (const f of col.filters as TableFilter<T>[]) {
218
- const withKey: TableFilter<T> = { ...f, columnKey: f.columnKey ?? col.key };
262
+ const withKey: TableFilter<T> = {
263
+ ...f,
264
+ columnKey: f.columnKey ?? col.key,
265
+ };
219
266
  if (!result.some((r) => r.id === withKey.id)) {
220
267
  result.push(withKey);
221
268
  }
222
269
  }
223
270
  }
271
+ // Auto-generate the built-in operator filter for a `filterable` column,
272
+ // unless the column already supplies an explicit filter.
273
+ if (col.filterable && !result.some((r) => r.columnKey === col.key)) {
274
+ result.push(columnFilter(col) as TableFilter<T>);
275
+ }
224
276
  }
225
277
  return result;
226
278
  }
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
-