@macrostrat/data-sheet 3.0.4 → 3.1.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.
- package/CHANGELOG.md +15 -1
- package/dist/cell-renderer.cjs +162 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +8 -0
- package/dist/cell-renderer.js +162 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/core.cjs +32 -159
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +4 -9
- package/dist/core.js +33 -158
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +9 -9
- package/dist/index.cjs +13 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +1 -1
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/provider.cjs +59 -339
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +11 -65
- package/dist/provider.js +58 -339
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +54 -0
- package/dist/utils/key-handling.cjs +26 -1
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -2
- package/dist/utils/key-handling.js +27 -2
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +320 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +318 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +5 -4
- package/src/cell-renderer.ts +217 -0
- package/src/core.ts +52 -212
- package/src/index.ts +3 -0
- package/src/postgrest-table/index.ts +1 -1
- package/src/provider.ts +66 -486
- package/src/types.ts +66 -0
- package/src/utils/key-handling.ts +31 -5
- package/src/zustand-store.ts +377 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataSheetState,
|
|
3
|
+
DataSheetStore,
|
|
4
|
+
DataSheetStoreMain,
|
|
5
|
+
StateUpdater,
|
|
6
|
+
VisibleCells,
|
|
7
|
+
} from "./types.ts";
|
|
8
|
+
import type { FocusedCellCoordinates, Region } from "@blueprintjs/table";
|
|
9
|
+
import { range } from "./utils";
|
|
10
|
+
import update, { Spec } from "immutability-helper";
|
|
11
|
+
|
|
12
|
+
export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
|
|
13
|
+
return {
|
|
14
|
+
data: [],
|
|
15
|
+
columnSpec: [],
|
|
16
|
+
defaultColumnWidth: 150,
|
|
17
|
+
editable: false,
|
|
18
|
+
deletedRows: new Set<number>(),
|
|
19
|
+
selection: [],
|
|
20
|
+
fillValueBaseCell: null,
|
|
21
|
+
updatedData: [],
|
|
22
|
+
focusedCell: null,
|
|
23
|
+
topLeftCell: null,
|
|
24
|
+
initialized: false,
|
|
25
|
+
tableRef: null,
|
|
26
|
+
visibleCellsRef: null,
|
|
27
|
+
// This is a placeholder
|
|
28
|
+
enableColumnReordering: false,
|
|
29
|
+
setSelection(selection: Region[]) {
|
|
30
|
+
set(updateSelection(selection));
|
|
31
|
+
},
|
|
32
|
+
columnWidthsIndex: new Map<string, number>(),
|
|
33
|
+
moveFocusedCell(direction: "up" | "down" | "left" | "right") {
|
|
34
|
+
set((state) => {
|
|
35
|
+
const { topLeftCell } = state;
|
|
36
|
+
if (topLeftCell == null) return {};
|
|
37
|
+
let { col, row } = topLeftCell;
|
|
38
|
+
switch (direction) {
|
|
39
|
+
case "up":
|
|
40
|
+
row = Math.max(0, row - 1);
|
|
41
|
+
break;
|
|
42
|
+
case "down":
|
|
43
|
+
row = Math.min(
|
|
44
|
+
row + 1,
|
|
45
|
+
Math.max(state.data.length, state.updatedData.length) - 1,
|
|
46
|
+
);
|
|
47
|
+
break;
|
|
48
|
+
case "left":
|
|
49
|
+
col = Math.max(0, col - 1);
|
|
50
|
+
break;
|
|
51
|
+
case "right":
|
|
52
|
+
col = Math.min(col + 1, state.columnSpec.length - 1);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
const region: Region = { cols: [col, col], rows: [row, row] };
|
|
56
|
+
return updateSelection([region]);
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
addRow(row: Partial<T> = {} as T) {
|
|
60
|
+
/** Add a new row. If there is a selection, use the last row index to determine
|
|
61
|
+
* where to insert the new row. Otherwise, append to the end of the data. */
|
|
62
|
+
set((state) => {
|
|
63
|
+
const { updatedData, data, selection } = state;
|
|
64
|
+
const lastRowIndex =
|
|
65
|
+
getLastRowIndex(selection) ??
|
|
66
|
+
Math.max(data.length, updatedData.length) - 1;
|
|
67
|
+
// If there is a selection, insert the new row after the last selected row
|
|
68
|
+
const spec: Spec<any> = {
|
|
69
|
+
$splice: [[lastRowIndex + 1, 0, row]],
|
|
70
|
+
};
|
|
71
|
+
console.log(spec);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
updatedData: update(updatedData, spec),
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
setUpdatedData(data: StateUpdater<T>) {
|
|
79
|
+
if (Array.isArray(data)) {
|
|
80
|
+
set({ updatedData: data });
|
|
81
|
+
} else {
|
|
82
|
+
set((state) => {
|
|
83
|
+
return { updatedData: data(state.updatedData) };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
deleteSelectedRows() {
|
|
88
|
+
// Remove selected rows from the data and updatedData arrays
|
|
89
|
+
set((state) => {
|
|
90
|
+
const { selection, deletedRows } = state;
|
|
91
|
+
const rowIndices = getRowIndices(selection);
|
|
92
|
+
|
|
93
|
+
// Delete rows from both updatedData and data
|
|
94
|
+
const newDeletedRows = new Set(deletedRows);
|
|
95
|
+
for (const rowIndex of rowIndices) {
|
|
96
|
+
newDeletedRows.add(rowIndex);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Remove selected rows and reset selection
|
|
100
|
+
return {
|
|
101
|
+
deletedRows: newDeletedRows,
|
|
102
|
+
selection: [],
|
|
103
|
+
focusedCell: null,
|
|
104
|
+
topLeftCell: null,
|
|
105
|
+
fillValueBaseCell: null,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
resetChanges() {
|
|
110
|
+
// Reset the updated data to the initial data
|
|
111
|
+
set((state) => ({
|
|
112
|
+
updatedData: [],
|
|
113
|
+
deletedRows: new Set<number>(),
|
|
114
|
+
columnWidths: new Map<string, number>(),
|
|
115
|
+
selection: [],
|
|
116
|
+
focusedCell: null,
|
|
117
|
+
topLeftCell: null,
|
|
118
|
+
fillValueBaseCell: null,
|
|
119
|
+
}));
|
|
120
|
+
},
|
|
121
|
+
onColumnWidthChanged(columnIx: number, newWidth: number) {
|
|
122
|
+
set((state) => {
|
|
123
|
+
const { columnSpec, columnWidthsIndex } = state;
|
|
124
|
+
const colKey = columnSpec[columnIx].key;
|
|
125
|
+
const newColumnWidths = new Map(columnWidthsIndex);
|
|
126
|
+
newColumnWidths.set(colKey, newWidth);
|
|
127
|
+
return { columnWidthsIndex: newColumnWidths };
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
setVisibleCells(visibleCells: VisibleCells) {
|
|
131
|
+
// Visible cells are used for infinite scrolling
|
|
132
|
+
// Right now we don't store this in the state
|
|
133
|
+
const ref = get().visibleCellsRef;
|
|
134
|
+
if (ref == null) return;
|
|
135
|
+
ref.current = visibleCells;
|
|
136
|
+
},
|
|
137
|
+
onDragValue(event: MouseEvent) {
|
|
138
|
+
set((state) => {
|
|
139
|
+
return { fillValueBaseCell: state.focusedCell };
|
|
140
|
+
});
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
},
|
|
143
|
+
onCellEdited(rowIndex: number, columnName: string, value: any) {
|
|
144
|
+
set((state) => {
|
|
145
|
+
const { editable, updatedData, data } = state;
|
|
146
|
+
if (!editable) return {};
|
|
147
|
+
let rowSpec: any;
|
|
148
|
+
|
|
149
|
+
// Check to see if the new value is the same as the old one
|
|
150
|
+
if (value !== data[rowIndex]?.[columnName]) {
|
|
151
|
+
const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
|
|
152
|
+
rowSpec = { [rowOp]: { [columnName]: value } };
|
|
153
|
+
} else {
|
|
154
|
+
rowSpec = { $unset: [columnName] };
|
|
155
|
+
}
|
|
156
|
+
const spec: Spec<T[]> = { [rowIndex]: rowSpec };
|
|
157
|
+
return { updatedData: update(updatedData, spec) };
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
initialize(props: Partial<DataSheetStoreMain<T>>) {
|
|
161
|
+
set({ ...props, initialized: true });
|
|
162
|
+
},
|
|
163
|
+
clearSelection() {
|
|
164
|
+
set((state) => {
|
|
165
|
+
// Delete all selected cells
|
|
166
|
+
const { selection, updatedData, columnSpec, data } = state;
|
|
167
|
+
let spec = {};
|
|
168
|
+
for (const region of selection) {
|
|
169
|
+
console.log("Clearing region", region);
|
|
170
|
+
const { cols, rows } = region;
|
|
171
|
+
const rowRange = range(rows ?? [0, updatedData.length - 1]);
|
|
172
|
+
const colRange = range(cols ?? [0, columnSpec.length - 1]);
|
|
173
|
+
for (const row of rowRange) {
|
|
174
|
+
let vals = {};
|
|
175
|
+
for (const col of colRange) {
|
|
176
|
+
const key = columnSpec[col].key;
|
|
177
|
+
const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
|
|
178
|
+
if (currentValue != null && currentValue !== "") {
|
|
179
|
+
vals[key] = "";
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
183
|
+
spec[row] = { [op]: vals };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
updatedData: update(updatedData, spec),
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
onSelection(selection: Region[]) {
|
|
192
|
+
set((state) => {
|
|
193
|
+
if (
|
|
194
|
+
selectionEquals(selection, state.selection) &&
|
|
195
|
+
singleFocusedCell(selection) == null // Only if we're in a multi-cell selection mode
|
|
196
|
+
) {
|
|
197
|
+
// If the selection is the same as the current selection, remove the selection.
|
|
198
|
+
// In practice this only happens for whole-row and whole-column selections
|
|
199
|
+
return {
|
|
200
|
+
selection: [],
|
|
201
|
+
focusedCell: null,
|
|
202
|
+
topLeftCell: null,
|
|
203
|
+
fillValueBaseCell: null,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let spec = updateSelection(selection);
|
|
208
|
+
if (state.fillValueBaseCell != null) {
|
|
209
|
+
spec.updatedData = fillValues(state, selection);
|
|
210
|
+
}
|
|
211
|
+
return spec;
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
onSelectionEdited(value: any) {
|
|
215
|
+
// Apply the same value to all selected cells
|
|
216
|
+
set((state) => {
|
|
217
|
+
const { selection, updatedData, columnSpec, editable } = state;
|
|
218
|
+
if (!editable) return {};
|
|
219
|
+
let spec = {};
|
|
220
|
+
for (const region of selection) {
|
|
221
|
+
const { cols, rows } = region;
|
|
222
|
+
const rowRange = range(rows ?? [0, updatedData.length - 1]);
|
|
223
|
+
const colRange = range(cols ?? [0, columnSpec.length - 1]);
|
|
224
|
+
for (const row of rowRange) {
|
|
225
|
+
let vals = {};
|
|
226
|
+
for (const col of colRange) {
|
|
227
|
+
const key = columnSpec[col].key;
|
|
228
|
+
vals[key] = value;
|
|
229
|
+
}
|
|
230
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
231
|
+
spec[row] = { [op]: vals };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
updatedData: update(updatedData, spec),
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
onColumnsReordered(oldIndex: number, newIndex: number, length: number) {
|
|
240
|
+
set((state) => {
|
|
241
|
+
if (!state.enableColumnReordering) return {};
|
|
242
|
+
const { columnSpec } = state;
|
|
243
|
+
const newSpec = [...columnSpec];
|
|
244
|
+
const removed = newSpec.splice(oldIndex, length);
|
|
245
|
+
newSpec.splice(newIndex, 0, ...removed);
|
|
246
|
+
return { columnSpec: newSpec };
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
scrollToRow(rowIndex: number) {
|
|
250
|
+
const tableRef = get().tableRef;
|
|
251
|
+
if (tableRef.current == null) return;
|
|
252
|
+
tableRef.current.scrollToRegion({
|
|
253
|
+
rows: [rowIndex, rowIndex],
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function updateSelection<T>(selection: Region[]) {
|
|
260
|
+
const focusedCell = singleFocusedCell(selection);
|
|
261
|
+
let spec: Partial<DataSheetState<T>> = {
|
|
262
|
+
selection,
|
|
263
|
+
focusedCell,
|
|
264
|
+
topLeftCell: topLeftCell(selection),
|
|
265
|
+
};
|
|
266
|
+
if (focusedCell != null) {
|
|
267
|
+
spec.fillValueBaseCell = null;
|
|
268
|
+
}
|
|
269
|
+
return spec;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function getLastRowIndex(regions: Region[]): number | null {
|
|
273
|
+
/** Get the last row index from a selection of regions */
|
|
274
|
+
if (regions == null || regions.length === 0) return null;
|
|
275
|
+
let lastRow = -1;
|
|
276
|
+
for (const region of regions) {
|
|
277
|
+
const { rows } = region;
|
|
278
|
+
if (rows == null || rows.length !== 2) continue;
|
|
279
|
+
lastRow = Math.max(lastRow, rows[1]);
|
|
280
|
+
}
|
|
281
|
+
if (lastRow < 0) {
|
|
282
|
+
/** If no valid rows are found, return null */
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
return lastRow;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function getRowIndices(regions: Region[]): number[] {
|
|
289
|
+
/** Get the row indices from a selection of regions */
|
|
290
|
+
if (regions == null || regions.length === 0) return [];
|
|
291
|
+
const rowIndices = new Set<number>();
|
|
292
|
+
for (const region of regions) {
|
|
293
|
+
const { rows } = region;
|
|
294
|
+
if (rows == null || rows.length !== 2) continue;
|
|
295
|
+
for (let i = rows[0]; i <= rows[1]; i++) {
|
|
296
|
+
rowIndices.add(i);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return Array.from(rowIndices).sort((a, b) => a - b);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function topLeftCell(
|
|
303
|
+
regions: Region[],
|
|
304
|
+
requireSolitaryCell: boolean = false,
|
|
305
|
+
): FocusedCellCoordinates | null {
|
|
306
|
+
/** Top left cell of a ranged selection */
|
|
307
|
+
if (regions == null) return null;
|
|
308
|
+
const [region] = regions;
|
|
309
|
+
if (region == null) return null;
|
|
310
|
+
const { cols, rows } = region;
|
|
311
|
+
if (cols == null || rows == null) return null;
|
|
312
|
+
if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
|
|
313
|
+
return null;
|
|
314
|
+
return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function selectionEquals(a: Region[], b: Region[]): boolean {
|
|
318
|
+
/** Check if two selections are equal */
|
|
319
|
+
if (a.length !== b.length) return false;
|
|
320
|
+
for (let i = 0; i < a.length; i++) {
|
|
321
|
+
const regionA = a[i];
|
|
322
|
+
const regionB = b[i];
|
|
323
|
+
|
|
324
|
+
const colsA = regionA.cols ?? [];
|
|
325
|
+
const colsB = regionB.cols ?? [];
|
|
326
|
+
|
|
327
|
+
const rowsA = regionA.rows ?? [];
|
|
328
|
+
const rowsB = regionB.rows ?? [];
|
|
329
|
+
|
|
330
|
+
if (
|
|
331
|
+
colsA.length !== colsB.length ||
|
|
332
|
+
rowsA.length !== rowsB.length ||
|
|
333
|
+
colsA[0] !== colsB[0] ||
|
|
334
|
+
colsA[1] !== colsB[1] ||
|
|
335
|
+
rowsA[0] !== rowsB[0] ||
|
|
336
|
+
rowsA[1] !== rowsB[1]
|
|
337
|
+
) {
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function singleFocusedCell(
|
|
345
|
+
sel: Region[],
|
|
346
|
+
): FocusedCellCoordinates | null {
|
|
347
|
+
/** Derive a single focused cell from a selected region, if possible */
|
|
348
|
+
if (sel?.length !== 1) return null;
|
|
349
|
+
return topLeftCell(sel, true);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
|
|
353
|
+
const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
|
|
354
|
+
|
|
355
|
+
// Prepare regions by unnesting columns
|
|
356
|
+
let regions = selection.map((region) => {
|
|
357
|
+
const { cols, rows } = region;
|
|
358
|
+
// Get the first column (maybe should be the last)
|
|
359
|
+
const [col] = cols;
|
|
360
|
+
return { cols: [col, col], rows };
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
// Fill values downwards
|
|
364
|
+
if (!editable || fillValueBaseCell == null) return updatedData;
|
|
365
|
+
const { col, row } = fillValueBaseCell;
|
|
366
|
+
const key = columnSpec[col].key;
|
|
367
|
+
const value = updatedData[row]?.[key] ?? data[row][key];
|
|
368
|
+
const spec = {};
|
|
369
|
+
for (const region of regions) {
|
|
370
|
+
const { cols, rows } = region;
|
|
371
|
+
for (const row of range(rows)) {
|
|
372
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
373
|
+
spec[row] = { [op]: { [key]: value } };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return update(updatedData, spec);
|
|
377
|
+
}
|