@macrostrat/data-sheet 4.9.0 → 4.10.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 (58) hide show
  1. package/CHANGELOG.md +70 -31
  2. package/dist/actions/selection.cjs +5 -1
  3. package/dist/actions/selection.cjs.map +1 -1
  4. package/dist/actions/selection.d.ts +7 -1
  5. package/dist/actions/selection.js +5 -1
  6. package/dist/actions/selection.js.map +1 -1
  7. package/dist/components/editor-actions.cjs +1 -1
  8. package/dist/components/editor-actions.cjs.map +1 -1
  9. package/dist/components/editor-actions.js +2 -2
  10. package/dist/components/editor-actions.js.map +1 -1
  11. package/dist/components/indicators/unified-filters.cjs +3 -0
  12. package/dist/components/indicators/unified-filters.cjs.map +1 -1
  13. package/dist/components/indicators/unified-filters.js +3 -0
  14. package/dist/components/indicators/unified-filters.js.map +1 -1
  15. package/dist/components/toolbars/actions-toolbar.cjs +5 -2
  16. package/dist/components/toolbars/actions-toolbar.cjs.map +1 -1
  17. package/dist/components/toolbars/actions-toolbar.js +6 -3
  18. package/dist/components/toolbars/actions-toolbar.js.map +1 -1
  19. package/dist/data-sheet.cjs +4 -0
  20. package/dist/data-sheet.cjs.map +1 -1
  21. package/dist/data-sheet.css +38 -22
  22. package/dist/data-sheet.js +4 -0
  23. package/dist/data-sheet.js.map +1 -1
  24. package/dist/index.cjs +15 -14
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +2 -1
  27. package/dist/main.module.sass.cjs +14 -14
  28. package/dist/main.module.sass.js +14 -14
  29. package/dist/provider/column-spec.cjs.map +1 -1
  30. package/dist/provider/column-spec.d.ts +8 -0
  31. package/dist/provider/column-spec.js.map +1 -1
  32. package/dist/provider/types.cjs.map +1 -1
  33. package/dist/provider/types.d.ts +11 -0
  34. package/dist/provider/types.js.map +1 -1
  35. package/dist/provider/zustand-store.cjs +40 -24
  36. package/dist/provider/zustand-store.cjs.map +1 -1
  37. package/dist/provider/zustand-store.js +40 -24
  38. package/dist/provider/zustand-store.js.map +1 -1
  39. package/dist/renderers/cell-renderer.cjs +9 -2
  40. package/dist/renderers/cell-renderer.cjs.map +1 -1
  41. package/dist/renderers/cell-renderer.js +9 -2
  42. package/dist/renderers/cell-renderer.js.map +1 -1
  43. package/dist/utils/key-handling.cjs +6 -5
  44. package/dist/utils/key-handling.cjs.map +1 -1
  45. package/dist/utils/key-handling.js +2 -1
  46. package/dist/utils/key-handling.js.map +1 -1
  47. package/package.json +2 -2
  48. package/src/actions/selection.ts +11 -2
  49. package/src/components/editor-actions.ts +5 -2
  50. package/src/components/indicators/unified-filters.ts +3 -0
  51. package/src/components/toolbars/actions-toolbar.ts +10 -4
  52. package/src/data-sheet.ts +6 -0
  53. package/src/main.module.sass +14 -0
  54. package/src/provider/column-spec.ts +8 -0
  55. package/src/provider/types.ts +15 -1
  56. package/src/provider/zustand-store.ts +58 -28
  57. package/src/renderers/cell-renderer.ts +12 -0
  58. package/src/utils/key-handling.ts +2 -1
@@ -1,4 +1,5 @@
1
1
  import {
2
+ CellEdit,
2
3
  DataSheetState,
3
4
  DataSheetStore,
4
5
  DataSheetStoreMain,
@@ -34,9 +35,11 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
34
35
  columnSpec: [],
35
36
  deferColumnSpec: false,
36
37
  defaultColumnWidth: 150,
38
+ rowHeight: 20,
37
39
  editable: false,
38
40
  selection: [],
39
41
  fillValueBaseCell: null,
42
+ fillBaseData: null,
40
43
  focusedCell: null,
41
44
  topLeftCell: null,
42
45
  initialized: false,
@@ -164,10 +167,12 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
164
167
  if (!get().canDeleteRows) return;
165
168
  // Remove selected rows from the data and updatedData arrays
166
169
  let deletedIndices: number[] = [];
170
+ let deletedRows: T[] = [];
167
171
  set((state) => {
168
- const { selection, deletedRows } = state;
172
+ const { selection } = state;
169
173
  const rowIndices = getRowIndices(selection);
170
174
  deletedIndices = rowIndices;
175
+ deletedRows = rowIndices.map((i) => state.data[i]);
171
176
 
172
177
  // Delete rows from both updatedData and data
173
178
 
@@ -217,7 +222,11 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
217
222
  };
218
223
  });
219
224
  if (deletedIndices.length > 0) {
220
- get().onEdit?.({ type: "deleteRows", rowIndices: deletedIndices });
225
+ get().onEdit?.({
226
+ type: "deleteRows",
227
+ rowIndices: deletedIndices,
228
+ rows: deletedRows,
229
+ });
221
230
  }
222
231
  },
223
232
  resetChanges(regions?: Region[]) {
@@ -235,10 +244,29 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
235
244
  });
236
245
  },
237
246
  onDragValue(event: MouseEvent) {
238
- set((state) => {
239
- return { fillValueBaseCell: state.focusedCell };
240
- });
241
247
  event.preventDefault();
248
+ const { focusedCell, updatedData } = get();
249
+ if (focusedCell == null) return;
250
+ // The fill is drawn from the overlay as it stood when the drag began, so
251
+ // shrinking the drag takes back the rows it leaves.
252
+ set({ fillValueBaseCell: focusedCell, fillBaseData: updatedData });
253
+ // A fill belongs to its drag alone, and the drag ends when the button is
254
+ // released, wherever that is. Left armed, the anchor would fill every
255
+ // later selection that isn't a single cell — a row picked by its header,
256
+ // say — with the anchor's value.
257
+ window.addEventListener("mouseup", () => get().finishFill(), {
258
+ once: true,
259
+ });
260
+ },
261
+ finishFill() {
262
+ const state = get();
263
+ if (state.fillValueBaseCell == null) return;
264
+ const { cells } = fillSelection(state, state.selection);
265
+ set({ fillValueBaseCell: null, fillBaseData: null });
266
+ // Reported like any other write, so a consumer that holds the edits
267
+ // (a controlled overlay) records the fill rather than having it drawn
268
+ // once and then overwritten.
269
+ if (cells.length > 0) get().onEdit?.({ type: "setCells", cells });
242
270
  },
243
271
  onCellEdited(rowIndex: number, columnName: string, value: any) {
244
272
  // The column decides whether it takes writes (see `isColumnWritable`):
@@ -354,7 +382,7 @@ export function createZustandStore<T>(set, get): DataSheetStoreMain<T> {
354
382
 
355
383
  let spec = updateSelection(selection);
356
384
  if (state.fillValueBaseCell != null) {
357
- spec.updatedData = fillValues(state, selection);
385
+ spec.updatedData = fillSelection(state, selection).updatedData;
358
386
  }
359
387
  // A click (user selection) re-arms auto-activation and clears the
360
388
  // travel direction so the editor opens with a default cursor.
@@ -514,9 +542,6 @@ export function updateSelection<T>(selection: Region[]) {
514
542
  focusedCell,
515
543
  topLeftCell: topLeftCell(selection),
516
544
  };
517
- if (focusedCell != null) {
518
- spec.fillValueBaseCell = null;
519
- }
520
545
  return spec;
521
546
  }
522
547
 
@@ -600,37 +625,42 @@ export function singleFocusedCell(
600
625
  return topLeftCell(sel, true);
601
626
  }
602
627
 
603
- function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
604
- const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
605
-
606
- // Prepare regions by unnesting columns
607
- let regions = selection.map((region) => {
608
- const { cols, rows } = region;
609
- // Get the first column (maybe should be the last)
610
- const [col] = cols ?? [];
611
- return { cols: [col, col], rows };
612
- });
628
+ /** A fill-drag over `selection`: the anchor cell's value written down the
629
+ * anchor's column in every selected row. Drawn from the overlay as it stood
630
+ * when the drag began (`fillBaseData`), so the result is the fill alone, and
631
+ * returned with the cells it changed. */
632
+ function fillSelection<T>(
633
+ state: DataSheetStore<T>,
634
+ selection: Region[],
635
+ ): { updatedData: T[]; cells: CellEdit[] } {
636
+ const { columnSpec, editable, fillValueBaseCell, data } = state;
637
+ const baseData = state.fillBaseData ?? state.updatedData;
638
+ const noFill = { updatedData: state.updatedData, cells: [] };
613
639
 
614
- // Fill values downwards
615
- if (!editable || fillValueBaseCell == null) return updatedData;
640
+ if (!editable || fillValueBaseCell == null) return noFill;
616
641
  const { col, row: baseVisibleRow } = fillValueBaseCell;
617
642
  // Dragging from a locked or derived column fills nothing
618
- if (!isColumnWritable(columnSpec[col], editable)) return updatedData;
643
+ if (!isColumnWritable(columnSpec[col], editable)) return noFill;
619
644
  // The base cell and target rows are visible positions; map to data rows.
620
645
  const baseRow = toDataRowIndex(state, baseVisibleRow);
621
646
  const key = columnSpec[col].key;
622
- const value = updatedData[baseRow]?.[key] ?? data[baseRow]?.[key];
647
+ const value = baseData[baseRow]?.[key] ?? data[baseRow]?.[key];
648
+
623
649
  const spec = {};
624
- for (const region of regions) {
625
- const { rows } = region;
626
- for (const visibleRow of range(rows ?? [])) {
650
+ const cells: CellEdit[] = [];
651
+ for (const region of selection) {
652
+ for (const visibleRow of range(region.rows ?? [])) {
627
653
  const row = toDataRowIndex(state, visibleRow);
654
+ if (row === baseRow) continue;
628
655
  if (state.rowStatus[row] === TableElementStatus.DELETED) continue;
629
- let op = updatedData[row] == null ? "$set" : "$merge";
656
+ const current = baseData[row]?.[key] ?? data[row]?.[key];
657
+ if (valuesAreEquivalent(value, current)) continue;
658
+ let op = baseData[row] == null ? "$set" : "$merge";
630
659
  spec[row] = { [op]: { [key]: value } };
660
+ cells.push({ rowIndex: row, column: key, value, row: data[row] });
631
661
  }
632
662
  }
633
- return update(updatedData, spec);
663
+ return { updatedData: update(baseData, spec), cells };
634
664
  }
635
665
 
636
666
  /** Number of rows currently visible (post-filter/sort), or the full data
@@ -40,6 +40,8 @@ function cellContentsEqual(
40
40
  x.isEdited === y.isEdited &&
41
41
  x.isDeleted === y.isDeleted &&
42
42
  x.status === y.status &&
43
+ x.width === y.width &&
44
+ x.height === y.height &&
43
45
  validationEqual(x.validation, y.validation)
44
46
  );
45
47
  }
@@ -149,6 +151,13 @@ export function basicCellRenderer<T>(
149
151
  isDeleted,
150
152
  status: statusVal,
151
153
  validation,
154
+ // The size the table gives the cell (resized columns included), so a
155
+ // renderer that fits itself to it needn't measure
156
+ width:
157
+ state.columnWidthsIndex?.get(col.key) ??
158
+ col.width ??
159
+ state.defaultColumnWidth,
160
+ height: state.rowHeight,
152
161
  };
153
162
 
154
163
  // Only allocate a new style object when a row-status style must be layered on
@@ -225,6 +234,9 @@ export function basicCellRenderer<T>(
225
234
  // Build the detail context for the unified `cellDetail` surface.
226
235
  const makeDetailCtx = (editableFlag: boolean): CellDetailContext<T> => ({
227
236
  ...cellContext,
237
+ // A popover isn't laid out in the cell
238
+ width: undefined,
239
+ height: undefined,
228
240
  surface: "cell",
229
241
  editable: editableFlag,
230
242
  onChange(v: any) {
@@ -9,6 +9,7 @@ import {
9
9
  } from "../provider";
10
10
  import { atom } from "jotai";
11
11
  import { runActionWrapper } from "../actions";
12
+ import { actionRequiresEditable } from "../actions/selection.ts";
12
13
  import { HotkeyConfig } from "@blueprintjs/core";
13
14
  import { toasterAtom } from "../notifications.ts";
14
15
 
@@ -92,7 +93,7 @@ export const tableHotkeysAtom = atom<null, [], HotkeyConfig[]>(
92
93
  label: action.name,
93
94
  group: action.group ?? "Actions",
94
95
  preventDefault: true,
95
- disabled: action.requiresEditable !== false && !editable,
96
+ disabled: actionRequiresEditable(action) && !editable,
96
97
  onKeyDown(event: KeyboardEvent<any>): any {
97
98
  runActionWrapper(action, get, set, toaster);
98
99
  },