@macrostrat/data-sheet 3.0.4 → 4.0.0-dev1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/actions/builtin.cjs +56 -0
  3. package/dist/actions/builtin.cjs.map +1 -0
  4. package/dist/actions/builtin.d.ts +15 -0
  5. package/dist/actions/builtin.js +56 -0
  6. package/dist/actions/builtin.js.map +1 -0
  7. package/dist/actions/clipboard.cjs +250 -0
  8. package/dist/actions/clipboard.cjs.map +1 -0
  9. package/dist/actions/clipboard.d.ts +42 -0
  10. package/dist/actions/clipboard.js +250 -0
  11. package/dist/actions/clipboard.js.map +1 -0
  12. package/dist/actions/filters.cjs +123 -0
  13. package/dist/actions/filters.cjs.map +1 -0
  14. package/dist/actions/filters.d.ts +6 -0
  15. package/dist/actions/filters.js +123 -0
  16. package/dist/actions/filters.js.map +1 -0
  17. package/dist/actions/index.d.ts +6 -0
  18. package/dist/actions/selection.cjs +194 -0
  19. package/dist/actions/selection.cjs.map +1 -0
  20. package/dist/actions/selection.d.ts +40 -0
  21. package/dist/actions/selection.js +192 -0
  22. package/dist/actions/selection.js.map +1 -0
  23. package/dist/actions/toolbar.cjs +171 -0
  24. package/dist/actions/toolbar.cjs.map +1 -0
  25. package/dist/actions/toolbar.d.ts +10 -0
  26. package/dist/actions/toolbar.js +171 -0
  27. package/dist/actions/toolbar.js.map +1 -0
  28. package/dist/actions/toolbar.module.sass.cjs +22 -0
  29. package/dist/actions/toolbar.module.sass.cjs.map +1 -0
  30. package/dist/actions/toolbar.module.sass.js +20 -0
  31. package/dist/actions/toolbar.module.sass.js.map +1 -0
  32. package/dist/actions/types.d.ts +143 -0
  33. package/dist/cell-renderer.cjs +165 -0
  34. package/dist/cell-renderer.cjs.map +1 -0
  35. package/dist/cell-renderer.d.ts +3 -0
  36. package/dist/cell-renderer.js +165 -0
  37. package/dist/cell-renderer.js.map +1 -0
  38. package/dist/components/actions.module.sass.cjs +3 -3
  39. package/dist/components/actions.module.sass.js +2 -2
  40. package/dist/components/colors.cjs +20 -16
  41. package/dist/components/colors.cjs.map +1 -1
  42. package/dist/components/colors.d.ts +4 -3
  43. package/dist/components/colors.js +20 -16
  44. package/dist/components/colors.js.map +1 -1
  45. package/dist/components/editor-popup.cjs +4 -3
  46. package/dist/components/editor-popup.cjs.map +1 -1
  47. package/dist/components/editor-popup.js +4 -3
  48. package/dist/components/editor-popup.js.map +1 -1
  49. package/dist/components/index.d.ts +1 -0
  50. package/dist/components/main.module.sass.cjs +12 -12
  51. package/dist/components/main.module.sass.js +11 -11
  52. package/dist/components/text-areas.cjs +23 -18
  53. package/dist/components/text-areas.cjs.map +1 -1
  54. package/dist/components/text-areas.d.ts +8 -4
  55. package/dist/components/text-areas.js +20 -13
  56. package/dist/components/text-areas.js.map +1 -1
  57. package/dist/components/view-info.cjs +18 -0
  58. package/dist/components/view-info.cjs.map +1 -0
  59. package/dist/components/view-info.d.ts +1 -0
  60. package/dist/components/view-info.js +18 -0
  61. package/dist/components/view-info.js.map +1 -0
  62. package/dist/core.cjs +201 -300
  63. package/dist/core.cjs.map +1 -1
  64. package/dist/core.d.ts +18 -10
  65. package/dist/core.js +204 -299
  66. package/dist/core.js.map +1 -1
  67. package/dist/data-sheet.css +190 -52
  68. package/dist/index.cjs +47 -8
  69. package/dist/index.cjs.map +1 -1
  70. package/dist/index.d.ts +3 -0
  71. package/dist/index.js +48 -9
  72. package/dist/index.js.map +1 -1
  73. package/dist/main.module.sass.cjs +15 -14
  74. package/dist/main.module.sass.cjs.map +1 -1
  75. package/dist/main.module.sass.js +14 -13
  76. package/dist/main.module.sass.js.map +1 -1
  77. package/dist/notifications.cjs +19 -0
  78. package/dist/notifications.cjs.map +1 -0
  79. package/dist/notifications.d.ts +9 -0
  80. package/dist/notifications.js +19 -0
  81. package/dist/notifications.js.map +1 -0
  82. package/dist/postgrest-table/data-loaders.cjs +181 -104
  83. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  84. package/dist/postgrest-table/data-loaders.d.ts +52 -28
  85. package/dist/postgrest-table/data-loaders.js +181 -104
  86. package/dist/postgrest-table/data-loaders.js.map +1 -1
  87. package/dist/postgrest-table/index.cjs +205 -79
  88. package/dist/postgrest-table/index.cjs.map +1 -1
  89. package/dist/postgrest-table/index.d.ts +14 -17
  90. package/dist/postgrest-table/index.js +186 -58
  91. package/dist/postgrest-table/index.js.map +1 -1
  92. package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
  93. package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
  94. package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
  95. package/dist/postgrest-table/lazy-loader-table.js +17 -14
  96. package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
  97. package/dist/postgrest-table/loading-utils.cjs +16 -0
  98. package/dist/postgrest-table/loading-utils.cjs.map +1 -0
  99. package/dist/postgrest-table/loading-utils.d.ts +13 -0
  100. package/dist/postgrest-table/loading-utils.js +16 -0
  101. package/dist/postgrest-table/loading-utils.js.map +1 -0
  102. package/dist/postgrest-table/main.module.sass.cjs +18 -13
  103. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  104. package/dist/postgrest-table/main.module.sass.js +18 -13
  105. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  106. package/dist/provider.cjs +96 -344
  107. package/dist/provider.cjs.map +1 -1
  108. package/dist/provider.js +96 -343
  109. package/dist/provider.js.map +1 -1
  110. package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
  111. package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
  112. package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
  113. package/dist/renderers/column-header-cell-postgrest.js +216 -0
  114. package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
  115. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
  116. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
  117. package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
  118. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
  119. package/dist/renderers/column-header-cell.cjs +103 -0
  120. package/dist/renderers/column-header-cell.cjs.map +1 -0
  121. package/dist/renderers/column-header-cell.d.ts +30 -0
  122. package/dist/renderers/column-header-cell.js +101 -0
  123. package/dist/renderers/column-header-cell.js.map +1 -0
  124. package/dist/renderers/index.d.ts +2 -0
  125. package/dist/table-updates/data-parameter.d.ts +14 -0
  126. package/dist/table-updates/filter.d.ts +16 -0
  127. package/dist/table-updates/index.d.ts +19 -0
  128. package/dist/types.cjs +9 -0
  129. package/dist/types.cjs.map +1 -0
  130. package/dist/types.d.ts +108 -0
  131. package/dist/types.js +9 -0
  132. package/dist/types.js.map +1 -0
  133. package/dist/utils/column-spec.cjs +9 -1
  134. package/dist/utils/column-spec.cjs.map +1 -1
  135. package/dist/utils/column-spec.d.ts +21 -0
  136. package/dist/utils/column-spec.js +9 -1
  137. package/dist/utils/column-spec.js.map +1 -1
  138. package/dist/utils/key-handling.cjs +161 -47
  139. package/dist/utils/key-handling.cjs.map +1 -1
  140. package/dist/utils/key-handling.d.ts +3 -3
  141. package/dist/utils/key-handling.js +161 -47
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/zustand-store.cjs +485 -0
  144. package/dist/zustand-store.cjs.map +1 -0
  145. package/dist/zustand-store.d.ts +6 -0
  146. package/dist/zustand-store.js +483 -0
  147. package/dist/zustand-store.js.map +1 -0
  148. package/package.json +6 -5
  149. package/src/actions/builtin.ts +65 -0
  150. package/src/actions/clipboard.ts +350 -0
  151. package/src/actions/filters.ts +150 -0
  152. package/src/actions/index.ts +6 -0
  153. package/src/actions/selection.ts +271 -0
  154. package/src/actions/toolbar.module.sass +30 -0
  155. package/src/actions/toolbar.ts +212 -0
  156. package/src/actions/types.ts +159 -0
  157. package/src/cell-renderer.ts +221 -0
  158. package/src/components/colors.ts +22 -19
  159. package/src/components/editor-popup.ts +3 -2
  160. package/src/components/index.ts +1 -0
  161. package/src/components/main.module.sass +1 -0
  162. package/src/components/text-areas.ts +34 -19
  163. package/src/components/view-info.ts +17 -0
  164. package/src/core.ts +273 -360
  165. package/src/index.ts +4 -0
  166. package/src/main.module.sass +14 -2
  167. package/src/notifications.ts +22 -0
  168. package/src/postgrest-table/data-loaders.ts +300 -201
  169. package/src/postgrest-table/index.ts +260 -86
  170. package/src/postgrest-table/lazy-loader-table.ts +18 -17
  171. package/src/postgrest-table/loading-utils.ts +49 -0
  172. package/src/postgrest-table/main.module.sass +16 -0
  173. package/src/provider.ts +110 -487
  174. package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
  175. package/src/renderers/column-header-cell-postgrest.ts +282 -0
  176. package/src/renderers/column-header-cell.ts +388 -0
  177. package/src/renderers/index.ts +2 -0
  178. package/src/table-updates/data-parameter.ts +44 -0
  179. package/src/table-updates/filter.ts +124 -0
  180. package/src/table-updates/index.ts +239 -0
  181. package/src/types.ts +121 -0
  182. package/src/utils/column-spec.ts +47 -0
  183. package/src/utils/key-handling.ts +201 -66
  184. package/src/zustand-store.ts +582 -0
  185. package/dist/provider.d.ts +0 -69
@@ -0,0 +1,582 @@
1
+ import {
2
+ DataSheetState,
3
+ DataSheetStore,
4
+ DataSheetStoreMain,
5
+ StateUpdater,
6
+ TableElementStatus,
7
+ VisibleCells,
8
+ } from "./types.ts";
9
+ import {
10
+ type FocusedCellCoordinates,
11
+ type Region,
12
+ RegionCardinality,
13
+ } from "@blueprintjs/table";
14
+ import { range } from "./utils";
15
+ import update, { Spec } from "immutability-helper";
16
+ import {
17
+ getSelectedColumnKeys,
18
+ getSelectedRowIndices,
19
+ getSelectionCardinality,
20
+ computeFilteredRowIndices,
21
+ computeTransformedRowIndices,
22
+ } from "./actions";
23
+
24
+ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
25
+ return {
26
+ /** All the data in the table */
27
+ data: [],
28
+ updatedData: [],
29
+ rowStatus: [],
30
+ columnSpec: [],
31
+ defaultColumnWidth: 150,
32
+ editable: false,
33
+ selection: [],
34
+ fillValueBaseCell: null,
35
+ focusedCell: null,
36
+ topLeftCell: null,
37
+ initialized: false,
38
+ tableRef: null,
39
+ // This is a placeholder
40
+ enableColumnReordering: false,
41
+ activeFilters: new Map<string, { filter: any; state: any }>(),
42
+ columnSorts: [],
43
+ clipboardProxy: null,
44
+ filteredRowIndices: null,
45
+ setSelection(selection: Region[]) {
46
+ set(updateSelection(selection));
47
+ },
48
+ columnWidthsIndex: new Map<string, number>(),
49
+ moveFocusedCell(direction: "up" | "down" | "left" | "right") {
50
+ set((state) => {
51
+ const { topLeftCell } = state;
52
+ if (topLeftCell == null) return {};
53
+ let { col, row } = topLeftCell;
54
+ switch (direction) {
55
+ case "up":
56
+ row = Math.max(0, row - 1);
57
+ break;
58
+ case "down":
59
+ row = Math.min(
60
+ row + 1,
61
+ Math.max(state.data.length, state.updatedData.length) - 1,
62
+ );
63
+ break;
64
+ case "left":
65
+ col = Math.max(0, col - 1);
66
+ break;
67
+ case "right":
68
+ col = Math.min(col + 1, state.columnSpec.length - 1);
69
+ break;
70
+ }
71
+ const region: Region = { cols: [col, col], rows: [row, row] };
72
+ return updateSelection([region]);
73
+ });
74
+ },
75
+ addRow(row: Partial<T> = {} as T) {
76
+ /** Add a new row. If there is a selection, use the last row index to determine
77
+ * where to insert the new row. Otherwise, append to the end of the data. */
78
+ set((state) => {
79
+ const { updatedData, data, selection, rowStatus } = state;
80
+
81
+ const lastRowIndex =
82
+ getLastRowIndex(selection) ??
83
+ Math.max(data.length, updatedData.length) - 1;
84
+ // Use $set at a specific index to correctly extend the sparse array.
85
+ // $splice fails when the target index exceeds the array length.
86
+ const newIndex = lastRowIndex + 1;
87
+
88
+ return {
89
+ updateData: insertItemAtIndex(updatedData, newIndex, row as T),
90
+ data: insertItemAtIndex(data, newIndex, row as T),
91
+ rowStatus: insertItemAtIndex(
92
+ rowStatus,
93
+ newIndex,
94
+ TableElementStatus.ADDED,
95
+ ),
96
+ };
97
+ });
98
+ },
99
+ setUpdatedData(data: StateUpdater<T>) {
100
+ if (Array.isArray(data)) {
101
+ set({ updatedData: data });
102
+ } else {
103
+ set((state) => {
104
+ return { updatedData: data(state.updatedData) };
105
+ });
106
+ }
107
+ },
108
+ deleteSelectedRows() {
109
+ // Remove selected rows from the data and updatedData arrays
110
+ set((state) => {
111
+ const { selection, deletedRows } = state;
112
+ const rowIndices = getRowIndices(selection);
113
+
114
+ // Delete rows from both updatedData and data
115
+
116
+ // If rows are in addedRows, we just delete them outright from the data and updatedData arrays
117
+ const { addedRows } = state;
118
+
119
+ const rowsNewlyDeleted = new Set(rowIndices);
120
+
121
+ // We outright remove rows that have not been added to the data array
122
+ const rowsToRemoveFromDataArrays = new Set(
123
+ rowIndices.filter(
124
+ (d) => state.rowStatus[d] == TableElementStatus.ADDED,
125
+ ),
126
+ );
127
+ // ...otherwise we set them to 'deleted'
128
+ const rowsToMarkAsDeleted = rowsNewlyDeleted.difference(
129
+ rowsToRemoveFromDataArrays,
130
+ );
131
+
132
+ let data = state.data;
133
+ let updatedData = state.updatedData;
134
+ let rowStatus = state.rowStatus;
135
+ if (rowsToMarkAsDeleted.size > 0) {
136
+ let spec: Spec<TableElementStatus[]> = {};
137
+ for (const ix of rowsToMarkAsDeleted) {
138
+ spec[ix] = { $set: TableElementStatus.DELETED };
139
+ }
140
+ rowStatus = update(rowStatus, spec);
141
+ }
142
+
143
+ if (rowsToRemoveFromDataArrays.size > 0) {
144
+ const filterFunc = (_, i) => !rowsToRemoveFromDataArrays.has(i);
145
+ data = data.filter(filterFunc);
146
+ updatedData = updatedData.filter(filterFunc);
147
+ rowStatus = rowStatus.filter(filterFunc);
148
+ }
149
+
150
+ // Remove selected rows and reset selection
151
+ return {
152
+ selection: [],
153
+ data,
154
+ updatedData,
155
+ rowStatus,
156
+ focusedCell: null,
157
+ topLeftCell: null,
158
+ fillValueBaseCell: null,
159
+ };
160
+ });
161
+ },
162
+ resetChanges(regions?: Region[]) {
163
+ // Reset the updated data to the initial data
164
+ set((state) => resetChangesForSelection(state, regions));
165
+ },
166
+ onColumnWidthChanged(columnIx: number, newWidth: number) {
167
+ set((state) => {
168
+ const { columnSpec, columnWidthsIndex } = state;
169
+ const colKey = columnSpec[columnIx].key;
170
+ const newColumnWidths = new Map(columnWidthsIndex);
171
+ newColumnWidths.set(colKey, newWidth);
172
+ return { columnWidthsIndex: newColumnWidths };
173
+ });
174
+ },
175
+ onDragValue(event: MouseEvent) {
176
+ set((state) => {
177
+ return { fillValueBaseCell: state.focusedCell };
178
+ });
179
+ event.preventDefault();
180
+ },
181
+ onCellEdited(rowIndex: number, columnName: string, value: any) {
182
+ set((state) => {
183
+ const { editable, updatedData, data } = state;
184
+ if (!editable) return {};
185
+ let rowSpec: any;
186
+
187
+ // Check to see if the new value is the same as the old one
188
+ if (value !== data[rowIndex]?.[columnName]) {
189
+ const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
190
+ rowSpec = { [rowOp]: { [columnName]: value } };
191
+ } else {
192
+ rowSpec = { $unset: [columnName] };
193
+ }
194
+ const spec: Spec<T[]> = { [rowIndex]: rowSpec };
195
+ return { updatedData: update(updatedData, spec) };
196
+ });
197
+ },
198
+ clearSelection() {
199
+ set((state) => {
200
+ // Delete all selected cells
201
+ const { selection, updatedData, columnSpec, data } = state;
202
+ let spec = {};
203
+ for (const region of selection) {
204
+ const { cols, rows } = region;
205
+ const rowRange = range(rows ?? [0, updatedData.length - 1]);
206
+ const colRange = range(cols ?? [0, columnSpec.length - 1]);
207
+ for (const row of rowRange) {
208
+ // Don't clear row if it has been deleted
209
+ const rowIsDeleted =
210
+ state.rowStatus[row] === TableElementStatus.DELETED;
211
+ if (rowIsDeleted) continue;
212
+ let vals = {};
213
+ for (const col of colRange) {
214
+ const key = columnSpec[col].key;
215
+ const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
216
+ if (currentValue != null && currentValue !== "") {
217
+ vals[key] = "";
218
+ }
219
+ }
220
+ let op = updatedData[row] == null ? "$set" : "$merge";
221
+ spec[row] = { [op]: vals };
222
+ }
223
+ }
224
+ return {
225
+ updatedData: update(updatedData, spec),
226
+ };
227
+ });
228
+ },
229
+ onSelection(selection: Region[]) {
230
+ set((state) => {
231
+ if (
232
+ selectionEquals(selection, state.selection) &&
233
+ singleFocusedCell(selection) == null // Only if we're in a multi-cell selection mode
234
+ ) {
235
+ // If the selection is the same as the current selection, remove the selection.
236
+ // In practice this only happens for whole-row and whole-column selections
237
+ return {
238
+ selection: [],
239
+ focusedCell: null,
240
+ topLeftCell: null,
241
+ fillValueBaseCell: null,
242
+ };
243
+ }
244
+
245
+ let spec = updateSelection(selection);
246
+ if (state.fillValueBaseCell != null) {
247
+ spec.updatedData = fillValues(state, selection);
248
+ }
249
+ return spec;
250
+ });
251
+ },
252
+ onSelectionEdited(value: any) {
253
+ // Apply the same value to all selected cells
254
+ set((state) => {
255
+ const { selection, updatedData, columnSpec, editable } = state;
256
+ if (!editable) return {};
257
+ let spec = {};
258
+ for (const region of selection) {
259
+ const { cols, rows } = region;
260
+ const rowRange = range(rows ?? [0, updatedData.length - 1]);
261
+ const colRange = range(cols ?? [0, columnSpec.length - 1]);
262
+ for (const row of rowRange) {
263
+ let vals = {};
264
+ for (const col of colRange) {
265
+ const key = columnSpec[col].key;
266
+ vals[key] = value;
267
+ }
268
+ let op = updatedData[row] == null ? "$set" : "$merge";
269
+ spec[row] = { [op]: vals };
270
+ }
271
+ }
272
+ return {
273
+ updatedData: update(updatedData, spec),
274
+ };
275
+ });
276
+ },
277
+ onColumnsReordered(oldIndex: number, newIndex: number, length: number) {
278
+ set((state) => {
279
+ if (!state.enableColumnReordering) return {};
280
+ const { columnSpec } = state;
281
+ const newSpec = [...columnSpec];
282
+ const removed = newSpec.splice(oldIndex, length);
283
+ newSpec.splice(newIndex, 0, ...removed);
284
+ return { columnSpec: newSpec };
285
+ });
286
+ },
287
+ scrollToRow(rowIndex: number) {
288
+ const tableRef = get().tableRef;
289
+ if (tableRef.current == null) return;
290
+ tableRef.current.scrollToRegion({
291
+ rows: [rowIndex, rowIndex],
292
+ });
293
+ },
294
+ setFilter(filterId: string, filter: any, filterState: any) {
295
+ set((state) => {
296
+ const newFilters = new Map<string, { filter: any; state: any }>(
297
+ state.activeFilters,
298
+ );
299
+ 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
+ };
309
+ });
310
+ },
311
+ removeFilter(filterId: string) {
312
+ set((state) => {
313
+ const newFilters = new Map<string, { filter: any; state: any }>(
314
+ state.activeFilters,
315
+ );
316
+ newFilters.delete(filterId);
317
+ return {
318
+ activeFilters: newFilters,
319
+ filteredRowIndices: computeTransformedRowIndices(
320
+ state.data,
321
+ state.updatedData,
322
+ newFilters,
323
+ state.columnSorts,
324
+ ),
325
+ };
326
+ });
327
+ },
328
+ clearFilters() {
329
+ set((state) => ({
330
+ 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
+ }));
338
+ },
339
+ setColumnSort(key: string, ascending: boolean | null) {
340
+ set((state) => {
341
+ let newSorts;
342
+ if (ascending == null) {
343
+ newSorts = state.columnSorts.filter((s) => s.key !== key);
344
+ } else {
345
+ const without = state.columnSorts.filter((s) => s.key !== key);
346
+ newSorts = [...without, { key, ascending }];
347
+ }
348
+ return {
349
+ columnSorts: newSorts,
350
+ filteredRowIndices: computeTransformedRowIndices(
351
+ state.data,
352
+ state.updatedData,
353
+ state.activeFilters,
354
+ newSorts,
355
+ ),
356
+ };
357
+ });
358
+ },
359
+ clearColumnSorts() {
360
+ set((state) => ({
361
+ columnSorts: [],
362
+ filteredRowIndices: computeTransformedRowIndices(
363
+ state.data,
364
+ state.updatedData,
365
+ state.activeFilters,
366
+ [],
367
+ ),
368
+ }));
369
+ },
370
+ setClipboardProxy(proxy) {
371
+ set({ clipboardProxy: proxy });
372
+ },
373
+ };
374
+ }
375
+
376
+ export function updateSelection<T>(selection: Region[]) {
377
+ const focusedCell = singleFocusedCell(selection);
378
+ let spec: Partial<DataSheetState<T>> = {
379
+ selection,
380
+ focusedCell,
381
+ topLeftCell: topLeftCell(selection),
382
+ };
383
+ if (focusedCell != null) {
384
+ spec.fillValueBaseCell = null;
385
+ }
386
+ return spec;
387
+ }
388
+
389
+ function getLastRowIndex(regions: Region[]): number | null {
390
+ /** Get the last row index from a selection of regions */
391
+ if (regions == null || regions.length === 0) return null;
392
+ let lastRow = -1;
393
+ for (const region of regions) {
394
+ const { rows } = region;
395
+ if (rows == null || rows.length !== 2) continue;
396
+ lastRow = Math.max(lastRow, rows[1]);
397
+ }
398
+ if (lastRow < 0) {
399
+ /** If no valid rows are found, return null */
400
+ return null;
401
+ }
402
+ return lastRow;
403
+ }
404
+
405
+ function getRowIndices(regions: Region[]): number[] {
406
+ /** Get the row indices from a selection of regions */
407
+ if (regions == null || regions.length === 0) return [];
408
+ const rowIndices = new Set<number>();
409
+ for (const region of regions) {
410
+ const { rows } = region;
411
+ if (rows == null || rows.length !== 2) continue;
412
+ for (let i = rows[0]; i <= rows[1]; i++) {
413
+ rowIndices.add(i);
414
+ }
415
+ }
416
+ return Array.from(rowIndices).sort((a, b) => a - b);
417
+ }
418
+
419
+ export function topLeftCell(
420
+ regions: Region[],
421
+ requireSolitaryCell: boolean = false,
422
+ ): FocusedCellCoordinates | null {
423
+ /** Top left cell of a ranged selection */
424
+ if (regions == null) return null;
425
+ const [region] = regions;
426
+ if (region == null) return null;
427
+ const { cols, rows } = region;
428
+ if (cols == null || rows == null) return null;
429
+ if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
430
+ return null;
431
+ return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
432
+ }
433
+
434
+ function selectionEquals(a: Region[], b: Region[]): boolean {
435
+ /** Check if two selections are equal */
436
+ if (a.length !== b.length) return false;
437
+ for (let i = 0; i < a.length; i++) {
438
+ const regionA = a[i];
439
+ const regionB = b[i];
440
+
441
+ const colsA = regionA.cols ?? [];
442
+ const colsB = regionB.cols ?? [];
443
+
444
+ const rowsA = regionA.rows ?? [];
445
+ const rowsB = regionB.rows ?? [];
446
+
447
+ if (
448
+ colsA.length !== colsB.length ||
449
+ rowsA.length !== rowsB.length ||
450
+ colsA[0] !== colsB[0] ||
451
+ colsA[1] !== colsB[1] ||
452
+ rowsA[0] !== rowsB[0] ||
453
+ rowsA[1] !== rowsB[1]
454
+ ) {
455
+ return false;
456
+ }
457
+ }
458
+ return true;
459
+ }
460
+
461
+ export function singleFocusedCell(
462
+ sel: Region[],
463
+ ): FocusedCellCoordinates | null {
464
+ /** Derive a single focused cell from a selected region, if possible */
465
+ if (sel?.length !== 1) return null;
466
+ return topLeftCell(sel, true);
467
+ }
468
+
469
+ function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
470
+ const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
471
+
472
+ // Prepare regions by unnesting columns
473
+ let regions = selection.map((region) => {
474
+ const { cols, rows } = region;
475
+ // Get the first column (maybe should be the last)
476
+ const [col] = cols;
477
+ return { cols: [col, col], rows };
478
+ });
479
+
480
+ // Fill values downwards
481
+ if (!editable || fillValueBaseCell == null) return updatedData;
482
+ const { col, row } = fillValueBaseCell;
483
+ const key = columnSpec[col].key;
484
+ const value = updatedData[row]?.[key] ?? data[row][key];
485
+ const spec = {};
486
+ for (const region of regions) {
487
+ const { cols, rows } = region;
488
+ for (const row of range(rows)) {
489
+ let op = updatedData[row] == null ? "$set" : "$merge";
490
+ spec[row] = { [op]: { [key]: value } };
491
+ }
492
+ }
493
+ return update(updatedData, spec);
494
+ }
495
+
496
+ function resetChangesForSelection<T>(
497
+ state: DataSheetStore<T>,
498
+ selection: Region[],
499
+ ): Partial<DataSheetStore<T>> {
500
+ const emptySelection = {
501
+ selection: [],
502
+ focusedCell: null,
503
+ topLeftCell: null,
504
+ fillValueBaseCell: null,
505
+ };
506
+
507
+ const { data } = state;
508
+ const cardinality =
509
+ getSelectionCardinality(selection ?? []) ?? RegionCardinality.FULL_TABLE;
510
+
511
+ const rowStatus = [...state.rowStatus];
512
+ const updatedData = [...state.updatedData];
513
+ const addedRows = new Set<number>();
514
+
515
+ // Global reset
516
+ switch (cardinality) {
517
+ case RegionCardinality.FULL_TABLE:
518
+ for (let i = 0; i < rowStatus.length; i++) {
519
+ if (rowStatus[i] === TableElementStatus.ADDED) {
520
+ addedRows.add(i);
521
+ }
522
+ }
523
+
524
+ return {
525
+ updatedData: [],
526
+ rowStatus: [],
527
+ data: data.filter((_, i) => !addedRows.has(i)),
528
+ ...emptySelection,
529
+ };
530
+ case RegionCardinality.FULL_COLUMNS:
531
+ case RegionCardinality.CELLS:
532
+ /** Columns or cells. Note, these only work the same because we don't
533
+ * allow column deletion, only unsetting all values.
534
+ */
535
+ const columnKeys = getSelectedColumnKeys(selection, state.columnSpec);
536
+ const spec: Spec<Record<string, any>[]> = {};
537
+ const rowIndices =
538
+ cardinality == RegionCardinality.FULL_COLUMNS
539
+ ? Array.from({ length: updatedData.length }, (_, i) => i)
540
+ : getSelectedRowIndices(selection);
541
+ for (const row of rowIndices) {
542
+ if (updatedData[row] == null) continue;
543
+ spec[row] = { $unset: columnKeys };
544
+ }
545
+ // TODO: if we allow column addition/deletion, this will require more adjustment
546
+ return {
547
+ updatedData: update(updatedData, spec),
548
+ ...emptySelection,
549
+ };
550
+ case RegionCardinality.FULL_ROWS:
551
+ for (const row of getSelectedRowIndices(selection)) {
552
+ updatedData[row] = undefined;
553
+ if (state.rowStatus[row] === TableElementStatus.DELETED) {
554
+ rowStatus[row] = undefined;
555
+ } else if (state.rowStatus[row] === TableElementStatus.ADDED) {
556
+ addedRows.add(row);
557
+ }
558
+ }
559
+
560
+ const removeCandidateRows = (arr: any[]) => {
561
+ return arr.filter((d, i) => !addedRows.has(i));
562
+ };
563
+
564
+ return {
565
+ updatedData: removeCandidateRows(updatedData),
566
+ data: removeCandidateRows(state.data),
567
+ rowStatus: removeCandidateRows(rowStatus),
568
+ ...emptySelection,
569
+ };
570
+ }
571
+ }
572
+
573
+ function insertItemAtIndex<T>(arr: T[], index: number, item: T) {
574
+ if (arr.length < index) {
575
+ const newArr = [...arr];
576
+ newArr.length = index;
577
+ newArr[index] = item;
578
+ return newArr;
579
+ }
580
+
581
+ return [...arr.slice(0, index), item, ...arr.slice(index)];
582
+ }
@@ -1,69 +0,0 @@
1
- import { StoreApi } from 'zustand';
2
- import { FocusedCellCoordinates, Region, Table2 } from '@blueprintjs/table';
3
- import { ColumnSpec, ColumnSpecOptions } from './utils';
4
- export interface DataSheetCoreProps<T> {
5
- data: T[];
6
- columnSpec?: ColumnSpec[];
7
- editable?: boolean;
8
- enableColumnReordering?: boolean;
9
- defaultColumnWidth?: number;
10
- }
11
- export interface DataSheetState<T> {
12
- selection: Region[];
13
- columnSpec: ColumnSpec[];
14
- fillValueBaseCell: FocusedCellCoordinates | null;
15
- focusedCell: FocusedCellCoordinates | null;
16
- topLeftCell: FocusedCellCoordinates | null;
17
- deletedRows: Set<number>;
18
- updatedData: T[];
19
- initialized: boolean;
20
- columnWidthsIndex: Map<string, number>;
21
- }
22
- type DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;
23
- type StateUpdater<T> = T[] | ((state: T[]) => T[]);
24
- export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
25
- setSelection(selection: Region[]): void;
26
- onDragValue(event: MouseEvent): void;
27
- setUpdatedData(data: StateUpdater<T>): void;
28
- onCellEdited(rowIndex: number, columnName: string, value: any): void;
29
- onSelectionEdited(value: any): void;
30
- onColumnsReordered(oldIndex: number, newIndex: number, length: number): void;
31
- onColumnWidthChanged(columnIndex: number, newWidth: number): void;
32
- moveFocusedCell(direction: "up" | "down" | "left" | "right"): void;
33
- deleteSelectedRows(): void;
34
- clearSelection(): void;
35
- resetChanges(): void;
36
- addRow(): void;
37
- initialize(props: DataSheetCoreProps<T>): void;
38
- onSelection(selection: Region[]): void;
39
- setVisibleCells(visibleCells: VisibleCells): void;
40
- scrollToRow(rowIndex: number): void;
41
- tableRef: React.MutableRefObject<Table2>;
42
- columnWidthsIndex: Map<string, number>;
43
- defaultColumnWidth: number;
44
- }
45
- export type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {
46
- children: React.ReactNode;
47
- columnSpecOptions?: ColumnSpecOptions<T>;
48
- };
49
- export interface VisibleCells {
50
- rowIndexStart: number;
51
- rowIndexEnd: number;
52
- }
53
- interface DataSheetComputedVals {
54
- hasUpdates: boolean;
55
- /** State for column widths (if resized).
56
- * This will reset if the columnSpec prop changes
57
- */
58
- columnWidths: number[];
59
- tableKeyHandler: (evt: React.KeyboardEvent) => void;
60
- editorKeyHandler: (evt: React.KeyboardEvent) => void;
61
- isSingleCellSelection?: boolean;
62
- }
63
- export type DataSheetStore<T> = DataSheetComputedVals & DataSheetStoreMain<T>;
64
- export declare function DataSheetProvider<T>({ children, data, columnSpec, columnSpecOptions, editable, enableColumnReordering, defaultColumnWidth, }: DataSheetProviderProps<T>): import('react').FunctionComponentElement<import('react').ProviderProps<StoreApi<DataSheetStore<any>>>>;
65
- export declare function useStoreAPI<T>(): StoreApi<DataSheetStore<T>>;
66
- export declare function useSelector<T = any, A = any>(selector: (state: DataSheetStore<T> & DataSheetComputedVals) => A): A;
67
- export declare function topLeftCell(regions: Region[], requireSolitaryCell?: boolean): FocusedCellCoordinates | null;
68
- export declare function singleFocusedCell(sel: Region[]): FocusedCellCoordinates | null;
69
- export {};