@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
@@ -95,13 +95,36 @@ export const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {
95
95
  })
96
96
  .filter(Boolean) as HotkeyConfig[];
97
97
 
98
+ // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise
99
+ // clear the selected cells.
100
+ const clearOrDeleteSelection = keyHandler((e, state) => {
101
+ const sel = state.selection ?? [];
102
+ const isRowSelection =
103
+ sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);
104
+ if (isRowSelection) {
105
+ state.deleteSelectedRows();
106
+ } else {
107
+ state.clearSelection();
108
+ }
109
+ });
110
+
98
111
  return [
99
112
  {
100
113
  combo: "esc",
101
- label: "Clear selection",
114
+ label: "Exit editing / clear selection",
102
115
  group: "Selection",
103
116
  allowInInput: true,
104
117
  onKeyDown: keyHandler((e, state) => {
118
+ // First Escape closes the focused cell's open surface (editor/panel)
119
+ // while keeping the selection, so arrow keys keep working — in both
120
+ // auto and manual modes. A second Escape (nothing open) clears the
121
+ // selection.
122
+ if (state.cellSurfaceOpen) {
123
+ state.closeCellSurface();
124
+ e.preventDefault();
125
+ e.stopPropagation();
126
+ return;
127
+ }
105
128
  if (state.selection.length == 0) {
106
129
  // Focus goes back to parent
107
130
  return;
@@ -113,13 +136,19 @@ export const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {
113
136
  },
114
137
  {
115
138
  combo: "backspace",
116
- label: "Clear selection",
139
+ label: "Clear cells / delete rows",
117
140
  group: "Selection",
118
141
  allowInInput: true,
119
142
  disabled: !editable,
120
- onKeyDown: keyHandler((e, state) => {
121
- state.clearSelection();
122
- }),
143
+ onKeyDown: clearOrDeleteSelection,
144
+ },
145
+ {
146
+ combo: "del",
147
+ label: "Clear cells / delete rows",
148
+ group: "Selection",
149
+ allowInInput: true,
150
+ disabled: !editable,
151
+ onKeyDown: clearOrDeleteSelection,
123
152
  },
124
153
  {
125
154
  combo: "tab",
@@ -132,13 +161,35 @@ export const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {
132
161
  },
133
162
  {
134
163
  combo: "enter",
135
- label: "Move focus down",
164
+ label: "Edit cell / move focus down",
136
165
  group: "Navigation",
137
166
  allowInInput: true,
138
167
  onKeyDown: keyHandler((e, state) => {
168
+ // In navigation mode (no surface open), Enter enters edit mode —
169
+ // opening and focusing the focused cell's surface (Google Sheets
170
+ // style). While a surface is open, Enter advances downward as usual
171
+ // (an inline editor blurs and this moves to the next row).
172
+ if (!state.cellSurfaceOpen) {
173
+ state.armAutoActivate();
174
+ state.openCellSurface();
175
+ e.preventDefault();
176
+ return;
177
+ }
139
178
  state.moveFocusedCell("down");
140
179
  }),
141
180
  },
181
+ {
182
+ combo: "f2",
183
+ label: "Edit / open cell",
184
+ group: "Editing",
185
+ onKeyDown: keyHandler((e, state) => {
186
+ // Enter edit mode from the keyboard: re-arm auto-activation and open
187
+ // the focused cell's surface (which focuses the editor).
188
+ state.armAutoActivate();
189
+ state.openCellSurface();
190
+ e.preventDefault();
191
+ }),
192
+ },
142
193
  ...directionCombos,
143
194
  ...actionsCombos,
144
195
  ];
@@ -155,7 +206,20 @@ const isSingleCellSelectionAtom = atom((get) => {
155
206
  });
156
207
 
157
208
  export const editorKeyHandlerAtom = atom((get) => {
209
+ const store = get(storeAPIAtom);
158
210
  return (e: KeyboardEvent) => {
211
+ if (e.key === "Escape") {
212
+ // Escape exits the inline editor: close the surface (which enters
213
+ // navigation mode in auto), keep the selection, and return focus to the
214
+ // table so arrow keys navigate. Stop propagation so the table's own
215
+ // Escape hotkey doesn't also fire.
216
+ const state = store?.getState();
217
+ state?.closeCellSurface();
218
+ state?.tableElement?.focus();
219
+ e.preventDefault();
220
+ e.stopPropagation();
221
+ return;
222
+ }
159
223
  const isSingleCellSelection = get(isSingleCellSelectionAtom);
160
224
  editorKeyHandler(e, isSingleCellSelection);
161
225
  };
@@ -0,0 +1,72 @@
1
+ import type { CellValidation, ColumnSpec } from "./column-spec";
2
+
3
+ /**
4
+ * Validate a single cell against its column spec. Order: `required` (empty →
5
+ * error), then `validate`, then the deprecated `isValid` boolean. Returns
6
+ * `null` when valid.
7
+ */
8
+ export function validateCell(
9
+ col: ColumnSpec,
10
+ value: any,
11
+ row: any,
12
+ rowIndex: number,
13
+ ): CellValidation | null {
14
+ const isEmpty = value == null || value === "";
15
+
16
+ if (col.required && isEmpty) {
17
+ return { severity: "error", message: `${col.name} is required` };
18
+ }
19
+ if (col.validate != null) {
20
+ return col.validate(value, row, { rowIndex });
21
+ }
22
+ if (col.isValid != null && !col.isValid(value)) {
23
+ return { severity: "error", message: `Invalid ${col.name}` };
24
+ }
25
+ return null;
26
+ }
27
+
28
+ /** A validation error located to a specific cell. */
29
+ export interface CellValidationError {
30
+ rowIndex: number;
31
+ columnKey: string;
32
+ severity: "error";
33
+ message?: string;
34
+ }
35
+
36
+ /**
37
+ * Collect all cell **errors** (not warnings) across the loaded rows, using the
38
+ * merged (base + edited) row values. Skips deleted rows. Used to block saving.
39
+ * Iterates loaded rows × validating columns — for very large tables, consider
40
+ * a memoized index; here it early-exits are the caller's (e.g. a length check).
41
+ */
42
+ export function collectValidationErrors(s: {
43
+ data?: any[];
44
+ updatedData?: any[];
45
+ columnSpec?: ColumnSpec[];
46
+ rowStatus?: readonly string[];
47
+ }): CellValidationError[] {
48
+ const data = s.data ?? [];
49
+ const updatedData = s.updatedData ?? [];
50
+ const columnSpec = s.columnSpec ?? [];
51
+ const rowStatus = s.rowStatus ?? [];
52
+
53
+ // Only columns that actually validate need scanning.
54
+ const cols = columnSpec.filter(
55
+ (c) => c.required || c.validate != null || c.isValid != null,
56
+ );
57
+ if (cols.length === 0) return [];
58
+
59
+ const errors: CellValidationError[] = [];
60
+ const n = Math.max(data.length, updatedData.length);
61
+ for (let i = 0; i < n; i++) {
62
+ if (rowStatus[i] === "deleted") continue;
63
+ const row = { ...data[i], ...updatedData[i] };
64
+ for (const col of cols) {
65
+ const v = validateCell(col, row[col.key], row, i);
66
+ if (v?.severity === "error") {
67
+ errors.push({ rowIndex: i, columnKey: col.key, severity: "error", message: v.message });
68
+ }
69
+ }
70
+ }
71
+ return errors;
72
+ }
@@ -2,6 +2,7 @@ import {
2
2
  DataSheetState,
3
3
  DataSheetStore,
4
4
  DataSheetStoreMain,
5
+ DS_ROW_ID,
5
6
  StateUpdater,
6
7
  TableElementStatus,
7
8
  VisibleCells,
@@ -17,10 +18,11 @@ import {
17
18
  getSelectedColumnKeys,
18
19
  getSelectedRowIndices,
19
20
  getSelectionCardinality,
20
- computeFilteredRowIndices,
21
- computeTransformedRowIndices,
22
21
  } from "./actions";
23
22
 
23
+ /** Monotonic counter for synthetic ids on in-table-added rows. */
24
+ let _addedRowCounter = 0;
25
+
24
26
  export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
25
27
  return {
26
28
  /** All the data in the table */
@@ -42,10 +44,40 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
42
44
  columnSorts: [],
43
45
  clipboardProxy: null,
44
46
  filteredRowIndices: null,
47
+ cellInteraction: "auto",
48
+ autoActivateArmed: true,
49
+ lastNavDirection: null,
50
+ tableElement: null,
51
+ cellSurfaceOpen: false,
52
+ identity: (row: any) => row?.id,
53
+ pendingOverlayById: new Map(),
54
+ controlledOverlay: false,
55
+ canDeleteRows: true,
45
56
  setSelection(selection: Region[]) {
46
57
  set(updateSelection(selection));
47
58
  },
48
59
  columnWidthsIndex: new Map<string, number>(),
60
+ suppressAutoActivate() {
61
+ set({ autoActivateArmed: false });
62
+ },
63
+ armAutoActivate() {
64
+ set({ autoActivateArmed: true });
65
+ },
66
+ openCellSurface() {
67
+ set({ cellSurfaceOpen: true });
68
+ },
69
+ closeCellSurface(opts?: { suppress?: boolean }) {
70
+ const suppress = opts?.suppress ?? true;
71
+ set((state) => ({
72
+ cellSurfaceOpen: false,
73
+ // Escape (suppress) drops auto mode into navigation mode so surfaces
74
+ // stop auto-opening until the next click. A click-dismiss doesn't.
75
+ autoActivateArmed:
76
+ suppress && state.cellInteraction === "auto"
77
+ ? false
78
+ : state.autoActivateArmed,
79
+ }));
80
+ },
49
81
  moveFocusedCell(direction: "up" | "down" | "left" | "right") {
50
82
  set((state) => {
51
83
  const { topLeftCell } = state;
@@ -69,12 +101,21 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
69
101
  break;
70
102
  }
71
103
  const region: Region = { cols: [col, col], rows: [row, row] };
72
- return updateSelection([region]);
104
+ // Record the travel direction (so the next editor places its cursor on
105
+ // the side we're moving toward) and open the new cell's surface only
106
+ // when auto-activation is armed.
107
+ return {
108
+ ...updateSelection([region]),
109
+ lastNavDirection: direction,
110
+ cellSurfaceOpen:
111
+ state.cellInteraction === "auto" && state.autoActivateArmed,
112
+ };
73
113
  });
74
114
  },
75
115
  addRow(row: Partial<T> = {} as T) {
76
116
  /** Add a new row. If there is a selection, use the last row index to determine
77
117
  * where to insert the new row. Otherwise, append to the end of the data. */
118
+ let newIndex = -1;
78
119
  set((state) => {
79
120
  const { updatedData, data, selection, rowStatus } = state;
80
121
 
@@ -83,11 +124,19 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
83
124
  Math.max(data.length, updatedData.length) - 1;
84
125
  // Use $set at a specific index to correctly extend the sparse array.
85
126
  // $splice fails when the target index exceeds the array length.
86
- const newIndex = lastRowIndex + 1;
127
+ newIndex = lastRowIndex + 1;
128
+
129
+ // Stamp a stable synthetic id so the added row survives a provider
130
+ // re-fetch (it has no provider row to match on). Its values live in the
131
+ // overlay (`updatedData`), which the loader-boundary remap re-appends.
132
+ const newRow = {
133
+ ...(row as any),
134
+ [DS_ROW_ID]: `__added_${_addedRowCounter++}`,
135
+ } as T;
87
136
 
88
137
  return {
89
- updateData: insertItemAtIndex(updatedData, newIndex, row as T),
90
- data: insertItemAtIndex(data, newIndex, row as T),
138
+ updatedData: insertItemAtIndex(updatedData, newIndex, newRow),
139
+ data: insertItemAtIndex(data, newIndex, newRow),
91
140
  rowStatus: insertItemAtIndex(
92
141
  rowStatus,
93
142
  newIndex,
@@ -95,6 +144,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
95
144
  ),
96
145
  };
97
146
  });
147
+ get().onEdit?.({ type: "addRow", rowIndex: newIndex, value: row });
98
148
  },
99
149
  setUpdatedData(data: StateUpdater<T>) {
100
150
  if (Array.isArray(data)) {
@@ -106,10 +156,15 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
106
156
  }
107
157
  },
108
158
  deleteSelectedRows() {
159
+ // Deletion is a provider capability: if the active data provider can't
160
+ // delete (no `deleteRows`), the whole affordance is disabled.
161
+ if (!get().canDeleteRows) return;
109
162
  // Remove selected rows from the data and updatedData arrays
163
+ let deletedIndices: number[] = [];
110
164
  set((state) => {
111
165
  const { selection, deletedRows } = state;
112
166
  const rowIndices = getRowIndices(selection);
167
+ deletedIndices = rowIndices;
113
168
 
114
169
  // Delete rows from both updatedData and data
115
170
 
@@ -158,10 +213,14 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
158
213
  fillValueBaseCell: null,
159
214
  };
160
215
  });
216
+ if (deletedIndices.length > 0) {
217
+ get().onEdit?.({ type: "deleteRows", rowIndices: deletedIndices });
218
+ }
161
219
  },
162
220
  resetChanges(regions?: Region[]) {
163
221
  // Reset the updated data to the initial data
164
222
  set((state) => resetChangesForSelection(state, regions));
223
+ get().onEdit?.({ type: "resetChanges" });
165
224
  },
166
225
  onColumnWidthChanged(columnIx: number, newWidth: number) {
167
226
  set((state) => {
@@ -184,27 +243,45 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
184
243
  if (!editable) return {};
185
244
  let rowSpec: any;
186
245
 
187
- // Check to see if the new value is the same as the old one
188
- if (value !== data[rowIndex]?.[columnName]) {
246
+ // Check to see if the new value is meaningfully different from the old
247
+ // one. Empty string and null/undefined are treated as equivalent, so
248
+ // focusing (and blurring) an empty cell doesn't record a phantom edit.
249
+ const oldValue = data[rowIndex]?.[columnName];
250
+ if (!valuesAreEquivalent(value, oldValue)) {
189
251
  const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
190
252
  rowSpec = { [rowOp]: { [columnName]: value } };
191
- } else {
253
+ } else if (updatedData[rowIndex] != null) {
254
+ // No change — drop any prior override for this cell.
192
255
  rowSpec = { $unset: [columnName] };
256
+ } else {
257
+ // No change and no prior override: nothing to do. (`$unset` on an
258
+ // absent row would throw in immutability-helper.)
259
+ return {};
193
260
  }
194
261
  const spec: Spec<T[]> = { [rowIndex]: rowSpec };
195
262
  return { updatedData: update(updatedData, spec) };
196
263
  });
264
+ if (get().editable) {
265
+ get().onEdit?.({
266
+ type: "setCells",
267
+ cells: [{ rowIndex, column: columnName, value }],
268
+ });
269
+ }
197
270
  },
198
271
  clearSelection() {
272
+ const edits: { rowIndex: number; column: string; value: any }[] = [];
199
273
  set((state) => {
200
274
  // Delete all selected cells
201
275
  const { selection, updatedData, columnSpec, data } = state;
276
+ const numVisible = visibleRowCount(state);
202
277
  let spec = {};
203
278
  for (const region of selection) {
204
279
  const { cols, rows } = region;
205
- const rowRange = range(rows ?? [0, updatedData.length - 1]);
280
+ const rowRange = range(rows ?? [0, numVisible - 1]);
206
281
  const colRange = range(cols ?? [0, columnSpec.length - 1]);
207
- for (const row of rowRange) {
282
+ for (const visibleRow of rowRange) {
283
+ // Selection indices are visible positions; map to the data row.
284
+ const row = toDataRowIndex(state, visibleRow);
208
285
  // Don't clear row if it has been deleted
209
286
  const rowIsDeleted =
210
287
  state.rowStatus[row] === TableElementStatus.DELETED;
@@ -215,6 +292,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
215
292
  const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
216
293
  if (currentValue != null && currentValue !== "") {
217
294
  vals[key] = "";
295
+ edits.push({ rowIndex: row, column: key, value: "" });
218
296
  }
219
297
  }
220
298
  let op = updatedData[row] == null ? "$set" : "$merge";
@@ -225,6 +303,9 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
225
303
  updatedData: update(updatedData, spec),
226
304
  };
227
305
  });
306
+ if (edits.length > 0) {
307
+ get().onEdit?.({ type: "setCells", cells: edits });
308
+ }
228
309
  },
229
310
  onSelection(selection: Region[]) {
230
311
  set((state) => {
@@ -239,6 +320,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
239
320
  focusedCell: null,
240
321
  topLeftCell: null,
241
322
  fillValueBaseCell: null,
323
+ cellSurfaceOpen: false,
242
324
  };
243
325
  }
244
326
 
@@ -246,24 +328,41 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
246
328
  if (state.fillValueBaseCell != null) {
247
329
  spec.updatedData = fillValues(state, selection);
248
330
  }
331
+ // A click (user selection) re-arms auto-activation and clears the
332
+ // travel direction so the editor opens with a default cursor.
333
+ spec.autoActivateArmed = true;
334
+ spec.lastNavDirection = null;
335
+ // Only open when the selection actually MOVES to a (single) cell.
336
+ // A click on the already-selected cell leaves open-state to the
337
+ // target's click toggle — this avoids racing the selection event
338
+ // (which fires on mousedown, possibly more than once) and is what
339
+ // makes both re-focus-to-open and click-to-close reliable.
340
+ if (!selectionEquals(selection, state.selection)) {
341
+ spec.cellSurfaceOpen = singleFocusedCell(selection) != null;
342
+ }
249
343
  return spec;
250
344
  });
251
345
  },
252
346
  onSelectionEdited(value: any) {
253
347
  // Apply the same value to all selected cells
348
+ const edits: { rowIndex: number; column: string; value: any }[] = [];
254
349
  set((state) => {
255
350
  const { selection, updatedData, columnSpec, editable } = state;
256
351
  if (!editable) return {};
352
+ const numVisible = visibleRowCount(state);
257
353
  let spec = {};
258
354
  for (const region of selection) {
259
355
  const { cols, rows } = region;
260
- const rowRange = range(rows ?? [0, updatedData.length - 1]);
356
+ const rowRange = range(rows ?? [0, numVisible - 1]);
261
357
  const colRange = range(cols ?? [0, columnSpec.length - 1]);
262
- for (const row of rowRange) {
358
+ for (const visibleRow of rowRange) {
359
+ // Selection indices are visible positions; map to the data row.
360
+ const row = toDataRowIndex(state, visibleRow);
263
361
  let vals = {};
264
362
  for (const col of colRange) {
265
363
  const key = columnSpec[col].key;
266
364
  vals[key] = value;
365
+ edits.push({ rowIndex: row, column: key, value });
267
366
  }
268
367
  let op = updatedData[row] == null ? "$set" : "$merge";
269
368
  spec[row] = { [op]: vals };
@@ -273,6 +372,9 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
273
372
  updatedData: update(updatedData, spec),
274
373
  };
275
374
  });
375
+ if (edits.length > 0) {
376
+ get().onEdit?.({ type: "setCells", cells: edits });
377
+ }
276
378
  },
277
379
  onColumnsReordered(oldIndex: number, newIndex: number, length: number) {
278
380
  set((state) => {
@@ -291,21 +393,17 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
291
393
  rows: [rowIndex, rowIndex],
292
394
  });
293
395
  },
396
+ // Sort/filter state changes flow to the data provider (via the loader,
397
+ // which reads `activeFilters`/`columnSorts` and re-fetches). The library no
398
+ // longer computes a client-side `filteredRowIndices` view — `data` arrives
399
+ // already ordered from the provider (in memory for a local source).
294
400
  setFilter(filterId: string, filter: any, filterState: any) {
295
401
  set((state) => {
296
402
  const newFilters = new Map<string, { filter: any; state: any }>(
297
403
  state.activeFilters,
298
404
  );
299
405
  newFilters.set(filterId, { filter, state: filterState });
300
- return {
301
- activeFilters: newFilters,
302
- filteredRowIndices: computeTransformedRowIndices(
303
- state.data,
304
- state.updatedData,
305
- newFilters,
306
- state.columnSorts,
307
- ),
308
- };
406
+ return { activeFilters: newFilters };
309
407
  });
310
408
  },
311
409
  removeFilter(filterId: string) {
@@ -314,26 +412,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
314
412
  state.activeFilters,
315
413
  );
316
414
  newFilters.delete(filterId);
317
- return {
318
- activeFilters: newFilters,
319
- filteredRowIndices: computeTransformedRowIndices(
320
- state.data,
321
- state.updatedData,
322
- newFilters,
323
- state.columnSorts,
324
- ),
325
- };
415
+ return { activeFilters: newFilters };
326
416
  });
327
417
  },
328
418
  clearFilters() {
329
- set((state) => ({
419
+ set(() => ({
330
420
  activeFilters: new Map<string, { filter: any; state: any }>(),
331
- filteredRowIndices: computeTransformedRowIndices(
332
- state.data,
333
- state.updatedData,
334
- new Map(),
335
- state.columnSorts,
336
- ),
337
421
  }));
338
422
  },
339
423
  setColumnSort(key: string, ascending: boolean | null) {
@@ -345,27 +429,11 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
345
429
  const without = state.columnSorts.filter((s) => s.key !== key);
346
430
  newSorts = [...without, { key, ascending }];
347
431
  }
348
- return {
349
- columnSorts: newSorts,
350
- filteredRowIndices: computeTransformedRowIndices(
351
- state.data,
352
- state.updatedData,
353
- state.activeFilters,
354
- newSorts,
355
- ),
356
- };
432
+ return { columnSorts: newSorts };
357
433
  });
358
434
  },
359
435
  clearColumnSorts() {
360
- set((state) => ({
361
- columnSorts: [],
362
- filteredRowIndices: computeTransformedRowIndices(
363
- state.data,
364
- state.updatedData,
365
- state.activeFilters,
366
- [],
367
- ),
368
- }));
436
+ set(() => ({ columnSorts: [] }));
369
437
  },
370
438
  setClipboardProxy(proxy) {
371
439
  set({ clipboardProxy: proxy });
@@ -479,13 +547,16 @@ function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
479
547
 
480
548
  // Fill values downwards
481
549
  if (!editable || fillValueBaseCell == null) return updatedData;
482
- const { col, row } = fillValueBaseCell;
550
+ const { col, row: baseVisibleRow } = fillValueBaseCell;
551
+ // The base cell and target rows are visible positions; map to data rows.
552
+ const baseRow = toDataRowIndex(state, baseVisibleRow);
483
553
  const key = columnSpec[col].key;
484
- const value = updatedData[row]?.[key] ?? data[row][key];
554
+ const value = updatedData[baseRow]?.[key] ?? data[baseRow]?.[key];
485
555
  const spec = {};
486
556
  for (const region of regions) {
487
- const { cols, rows } = region;
488
- for (const row of range(rows)) {
557
+ const { rows } = region;
558
+ for (const visibleRow of range(rows)) {
559
+ const row = toDataRowIndex(state, visibleRow);
489
560
  let op = updatedData[row] == null ? "$set" : "$merge";
490
561
  spec[row] = { [op]: { [key]: value } };
491
562
  }
@@ -493,6 +564,42 @@ function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
493
564
  return update(updatedData, spec);
494
565
  }
495
566
 
567
+ /** Number of rows currently visible (post-filter/sort), or the full data
568
+ * length when no view transform is active. Uses the max of `data` and
569
+ * `updatedData` lengths because `updatedData` is sparse — keying off it alone
570
+ * would stop a whole-column edit at the last already-edited row. */
571
+ function visibleRowCount<T>(state: DataSheetStore<T>): number {
572
+ const { filteredRowIndices, updatedData, data } = state;
573
+ return filteredRowIndices != null
574
+ ? filteredRowIndices.length
575
+ : Math.max(data.length, updatedData.length);
576
+ }
577
+
578
+ /** Map a visible (post-filter/sort) row position to its underlying data
579
+ * index. Edit methods receive selection indices in visible space, so they
580
+ * must translate before writing to `updatedData`/`data`. */
581
+ function toDataRowIndex<T>(
582
+ state: DataSheetStore<T>,
583
+ visibleRow: number,
584
+ ): number {
585
+ const { filteredRowIndices } = state;
586
+ if (filteredRowIndices == null) return visibleRow;
587
+ return filteredRowIndices[visibleRow] ?? visibleRow;
588
+ }
589
+
590
+ /** Whether two cell values are equivalent for edit-tracking. Empty string and
591
+ * null/undefined are equivalent (so clearing an empty cell is a no-op), and
592
+ * non-blank values are compared by string form so a text editor's `"42"`
593
+ * matches a numeric base `42` (otherwise the cell reads as edited when its
594
+ * value didn't actually change). */
595
+ function valuesAreEquivalent(a: any, b: any): boolean {
596
+ if (a === b) return true;
597
+ const aBlank = a == null || a === "";
598
+ const bBlank = b == null || b === "";
599
+ if (aBlank || bBlank) return aBlank && bBlank;
600
+ return String(a) === String(b);
601
+ }
602
+
496
603
  function resetChangesForSelection<T>(
497
604
  state: DataSheetStore<T>,
498
605
  selection: Region[],
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const actions_module = require("./actions.module.sass.cjs");
4
- function DataSheetActionsRow(props) {
5
- return actions_module("div.table-actions", props.children);
6
- }
7
- function DataSheetAction(props) {
8
- return actions_module("div.table-action", props.children);
9
- }
10
- exports.DataSheetAction = DataSheetAction;
11
- exports.DataSheetActionsRow = DataSheetActionsRow;
12
- //# sourceMappingURL=actions.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"actions.cjs","sources":["../../src/components/actions.ts"],"sourcesContent":["import h from \"./actions.module.sass\";\n\nexport function DataSheetActionsRow(props) {\n return h(\"div.table-actions\", props.children);\n}\n\nexport function DataSheetAction(props) {\n return h(\"div.table-action\", props.children);\n}\n"],"names":["h"],"mappings":";;;AAEO,SAAS,oBAAoB,OAAO;AACzC,SAAOA,eAAE,qBAAqB,MAAM,QAAQ;AAC9C;AAEO,SAAS,gBAAgB,OAAO;AACrC,SAAOA,eAAE,oBAAoB,MAAM,QAAQ;AAC7C;;;"}
@@ -1,2 +0,0 @@
1
- export declare function DataSheetActionsRow(props: any): import('react').DOMElement<any, Element>;
2
- export declare function DataSheetAction(props: any): import('react').DOMElement<any, Element>;
@@ -1,12 +0,0 @@
1
- import h from "./actions.module.sass.js";
2
- function DataSheetActionsRow(props) {
3
- return h("div.table-actions", props.children);
4
- }
5
- function DataSheetAction(props) {
6
- return h("div.table-action", props.children);
7
- }
8
- export {
9
- DataSheetAction,
10
- DataSheetActionsRow
11
- };
12
- //# sourceMappingURL=actions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"actions.js","sources":["../../src/components/actions.ts"],"sourcesContent":["import h from \"./actions.module.sass\";\n\nexport function DataSheetActionsRow(props) {\n return h(\"div.table-actions\", props.children);\n}\n\nexport function DataSheetAction(props) {\n return h(\"div.table-action\", props.children);\n}\n"],"names":[],"mappings":";AAEO,SAAS,oBAAoB,OAAO;AACzC,SAAO,EAAE,qBAAqB,MAAM,QAAQ;AAC9C;AAEO,SAAS,gBAAgB,OAAO;AACrC,SAAO,EAAE,oBAAoB,MAAM,QAAQ;AAC7C;"}
@@ -1,12 +0,0 @@
1
- "use strict";
2
- const h$1 = require("@macrostrat/hyper");
3
- const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
4
- const h__default = /* @__PURE__ */ _interopDefault(h$1);
5
- const styles = {
6
- "table-actions": "_table-actions_1xz9a_1",
7
- "table-action": "_table-action_1xz9a_1"
8
- };
9
- let h = h__default.default.styled(styles);
10
- Object.assign(h, styles);
11
- module.exports = h;
12
- //# sourceMappingURL=actions.module.sass.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"actions.module.sass.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
@@ -1,11 +0,0 @@
1
- import h$1 from "@macrostrat/hyper";
2
- const styles = {
3
- "table-actions": "_table-actions_1xz9a_1",
4
- "table-action": "_table-action_1xz9a_1"
5
- };
6
- let h = h$1.styled(styles);
7
- Object.assign(h, styles);
8
- export {
9
- h as default
10
- };
11
- //# sourceMappingURL=actions.module.sass.js.map