@macrostrat/data-sheet 2.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 (79) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +19 -0
  3. package/dist/actions.5740e8e2.js +21 -0
  4. package/dist/actions.5740e8e2.js.map +1 -0
  5. package/dist/actions.module.6ec06797.css +11 -0
  6. package/dist/actions.module.6ec06797.css.map +1 -0
  7. package/dist/actions.module.ea96403b.js +16 -0
  8. package/dist/actions.module.ea96403b.js.map +1 -0
  9. package/dist/cell-renderers.de70f471.js +31 -0
  10. package/dist/cell-renderers.de70f471.js.map +1 -0
  11. package/dist/colors.41c3ed7a.js +83 -0
  12. package/dist/colors.41c3ed7a.js.map +1 -0
  13. package/dist/column-spec.4710da93.js +70 -0
  14. package/dist/column-spec.4710da93.js.map +1 -0
  15. package/dist/components.c3ad66e9.js +11 -0
  16. package/dist/components.c3ad66e9.js.map +1 -0
  17. package/dist/core.d8d80480.js +356 -0
  18. package/dist/core.d8d80480.js.map +1 -0
  19. package/dist/data-loaders.5302dedd.js +196 -0
  20. package/dist/data-loaders.5302dedd.js.map +1 -0
  21. package/dist/editor-popup.9259608f.js +68 -0
  22. package/dist/editor-popup.9259608f.js.map +1 -0
  23. package/dist/index.d.ts +202 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +9 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/main.module.846377cd.css +35 -0
  28. package/dist/main.module.846377cd.css.map +1 -0
  29. package/dist/main.module.914caa19.css +91 -0
  30. package/dist/main.module.914caa19.css.map +1 -0
  31. package/dist/main.module.93f91ad4.js +25 -0
  32. package/dist/main.module.93f91ad4.js.map +1 -0
  33. package/dist/main.module.dc8f6293.css +62 -0
  34. package/dist/main.module.dc8f6293.css.map +1 -0
  35. package/dist/main.module.de002429.js +43 -0
  36. package/dist/main.module.de002429.js.map +1 -0
  37. package/dist/main.module.ff717a08.js +37 -0
  38. package/dist/main.module.ff717a08.js.map +1 -0
  39. package/dist/postgrest-table.15ded3c1.js +173 -0
  40. package/dist/postgrest-table.15ded3c1.js.map +1 -0
  41. package/dist/provider.67e982b8.js +232 -0
  42. package/dist/provider.67e982b8.js.map +1 -0
  43. package/dist/text-areas.079e8b15.js +78 -0
  44. package/dist/text-areas.079e8b15.js.map +1 -0
  45. package/dist/utils.a37ed786.js +14 -0
  46. package/dist/utils.a37ed786.js.map +1 -0
  47. package/package.json +50 -0
  48. package/src/__archive/base.ts +55 -0
  49. package/src/__archive/components/column-utils.ts +126 -0
  50. package/src/__archive/components/index.ts +115 -0
  51. package/src/__archive/components/toolbar.ts +79 -0
  52. package/src/__archive/editors/index.ts +78 -0
  53. package/src/__archive/editors/styles.module.scss +10 -0
  54. package/src/__archive/enhancers.ts +36 -0
  55. package/src/__archive/index.ts +8 -0
  56. package/src/__archive/main.ts +180 -0
  57. package/src/__archive/provider.ts +179 -0
  58. package/src/__archive/styles.module.scss +56 -0
  59. package/src/__archive/test-sheet/attitude-sheet.old.styl +53 -0
  60. package/src/__archive/test-sheet/attitude-sheet.scss +60 -0
  61. package/src/__archive/test-sheet/index.ts +210 -0
  62. package/src/__archive/virtualized.ts +82 -0
  63. package/src/components/actions.module.sass +8 -0
  64. package/src/components/actions.ts +11 -0
  65. package/src/components/colors.ts +90 -0
  66. package/src/components/editor-popup.ts +69 -0
  67. package/src/components/index.ts +3 -0
  68. package/src/components/main.module.sass +35 -0
  69. package/src/components/text-areas.ts +88 -0
  70. package/src/core.ts +481 -0
  71. package/src/index.ts +2 -0
  72. package/src/main.module.sass +77 -0
  73. package/src/postgrest-table/cell-renderers.ts +24 -0
  74. package/src/postgrest-table/data-loaders.ts +287 -0
  75. package/src/postgrest-table/index.ts +172 -0
  76. package/src/postgrest-table/main.module.sass +52 -0
  77. package/src/provider.ts +282 -0
  78. package/src/utils/column-spec.ts +97 -0
  79. package/src/utils/index.ts +7 -0
package/src/core.ts ADDED
@@ -0,0 +1,481 @@
1
+ import {
2
+ Button,
3
+ ButtonGroup,
4
+ HotkeysProvider,
5
+ InputGroup,
6
+ Intent,
7
+ } from "@blueprintjs/core";
8
+ import { Cell, Column, Region, Table2 } from "@blueprintjs/table";
9
+ import "@blueprintjs/table/lib/css/table.css";
10
+ import hyper from "@macrostrat/hyper";
11
+ import update from "immutability-helper";
12
+ import { ReactNode, useCallback, useEffect, useRef, useState } from "react";
13
+ import { EditorPopup, handleSpecialKeys } from "./components";
14
+ import styles from "./main.module.sass";
15
+ import {
16
+ ColumnSpec,
17
+ ColumnSpecOptions,
18
+ DataSheetProvider,
19
+ DataSheetProviderProps,
20
+ DataSheetStore,
21
+ useSelector,
22
+ useStoreAPI,
23
+ VisibleCells,
24
+ } from "./provider";
25
+ import { DataSheetAction } from "./components/actions";
26
+
27
+ export type { ColumnSpec, ColumnSpecOptions };
28
+ export * from "./components";
29
+
30
+ const h = hyper.styled(styles);
31
+
32
+ // More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
33
+
34
+ // TODO: add a "copy to selection" tool (the little square in the bottom right corner of a cell)
35
+ // This should copy the value of a cell (or a set of cells in the same row) downwards.
36
+
37
+ interface DataSheetInternalProps<T> {
38
+ onVisibleCellsChange?: (visibleCells: VisibleCells) => void;
39
+ onSaveData: (updatedData: any[], data: any[]) => void;
40
+ onDeleteRows?: (selection: Region[]) => void;
41
+ verbose?: boolean;
42
+ enableColumnReordering?: boolean;
43
+ dataSheetActions?: ReactNode | null;
44
+ }
45
+
46
+ type DataSheetProps<T> = DataSheetProviderProps<T> & DataSheetInternalProps<T>;
47
+
48
+ export function DataSheet<T>(props: DataSheetProps<T>) {
49
+ const {
50
+ data,
51
+ columnSpec,
52
+ columnSpecOptions,
53
+ editable = true,
54
+ enableColumnReordering = false,
55
+ ...rest
56
+ } = props;
57
+
58
+ return h(
59
+ HotkeysProvider,
60
+ h(
61
+ DataSheetProvider<T>,
62
+ {
63
+ data,
64
+ columnSpec,
65
+ columnSpecOptions: columnSpecOptions,
66
+ enableColumnReordering,
67
+ editable,
68
+ ...rest,
69
+ },
70
+ h(_DataSheet, { ...rest, editable, enableColumnReordering })
71
+ )
72
+ );
73
+ }
74
+
75
+ function _DataSheet<T>({
76
+ onVisibleCellsChange,
77
+ enableColumnReordering,
78
+ onSaveData,
79
+ onDeleteRows,
80
+ verbose = true,
81
+ dataSheetActions = null,
82
+ }: DataSheetInternalProps<T>) {
83
+ /**
84
+ * @param data: The data to be displayed in the table
85
+ * @param columnSpec: The specification for all columns in the table. If not provided, the column spec will be generated from the data.
86
+ * @param columnSpecOptions: Options for generating a column spec from data
87
+ */
88
+
89
+ // For now, we only consider a single cell "focused" when we have one cell selected.
90
+ // Multi-cell selections have a different set of "bulk" actions.
91
+ const selection = useSelector<T>((state) => state.selection);
92
+
93
+ const data = useSelector((state) => state.data);
94
+ const editable = useSelector((state) => state.editable);
95
+
96
+ const focusedCell = useSelector((state) => state.focusedCell);
97
+
98
+ const ref = useSelector((state) => state.tableRef);
99
+
100
+ const columnSpec = useSelector((state) => state.columnSpec);
101
+
102
+ // A sparse array to hold updates
103
+ // TODO: create a "changeset" concept to facilitate undo/redo
104
+ const updatedData = useSelector((state) => state.updatedData);
105
+ const setUpdatedData = useSelector((state) => state.setUpdatedData);
106
+
107
+ const hasUpdates = updatedData.length > 0;
108
+
109
+ const onSelection = useSelector((state) => state.onSelection);
110
+
111
+ const storeAPI = useStoreAPI<T>();
112
+
113
+ const onColumnsReordered = useSelector((state) => state.onColumnsReordered);
114
+
115
+ const _onSaveData = useCallback(() => {
116
+ onSaveData(updatedData, data);
117
+ setUpdatedData([]);
118
+ }, [updatedData, data, onSaveData]);
119
+
120
+ const setVisibleCells = useSelector((state) => state.setVisibleCells);
121
+
122
+ const _onVisibleCellsChange = useCallback(
123
+ (visibleCells: VisibleCells) => {
124
+ setVisibleCells(visibleCells);
125
+ onVisibleCellsChange?.(visibleCells);
126
+ },
127
+ [onVisibleCellsChange, setVisibleCells]
128
+ );
129
+
130
+ const onAddRow = useCallback(() => {
131
+ setUpdatedData((updatedData) => {
132
+ const ix = data.length;
133
+ const addRowSpec = { [ix]: { $set: {} } };
134
+ const newUpdatedData = update(updatedData, addRowSpec);
135
+ return newUpdatedData;
136
+ });
137
+ }, [setUpdatedData, data]);
138
+
139
+ const _onDeleteRows = useCallback(() => {
140
+ onDeleteRows(selection);
141
+ }, [onDeleteRows, selection]);
142
+
143
+ useEffect(() => {
144
+ if (!verbose) return;
145
+ console.log("Updated data", updatedData);
146
+ }, [updatedData]);
147
+
148
+ useEffect(() => {
149
+ if (!verbose) return;
150
+ console.log("Selection", selection);
151
+ }, [selection]);
152
+
153
+ if (data == null) return null;
154
+
155
+ const nDeletionCandidates = getRowsToDelete(selection).length;
156
+
157
+ const numRows = Math.max(updatedData.length, data.length);
158
+
159
+ return h("div.data-sheet-container", [
160
+ h.if(editable)(DataSheetEditToolbar, {
161
+ hasUpdates,
162
+ setUpdatedData,
163
+ onSaveData: _onSaveData,
164
+ onAddRow,
165
+ onDeleteRows: nDeletionCandidates > 0 ? _onDeleteRows : null,
166
+ }),
167
+ dataSheetActions,
168
+ h("div.data-sheet-holder", [
169
+ h(
170
+ Table2,
171
+ {
172
+ ref,
173
+ numRows,
174
+ className: "data-sheet",
175
+ enableFocusedCell: true,
176
+ enableColumnReordering,
177
+ onColumnsReordered,
178
+ focusedCell,
179
+ selectedRegions: selection,
180
+ onSelection,
181
+ // The cell renderer is memoized internally based on these data dependencies
182
+ cellRendererDependencies: [selection, updatedData, focusedCell, data],
183
+ onVisibleCellsChange: _onVisibleCellsChange,
184
+ },
185
+ columnSpec.map((col, colIndex) => {
186
+ return h(Column, {
187
+ name: col.name,
188
+ cellRenderer: (rowIndex) => {
189
+ const state = storeAPI.getState();
190
+ return basicCellRenderer<T>(rowIndex, colIndex, col, state);
191
+ },
192
+ });
193
+ })
194
+ ),
195
+ ]),
196
+ ]);
197
+ }
198
+
199
+ function basicCellRenderer<T>(
200
+ rowIndex: number,
201
+ colIndex: number,
202
+ columnSpec: ColumnSpec,
203
+ state: DataSheetStore<T>
204
+ ): any {
205
+ const data = state.data;
206
+ const updatedData = state.updatedData;
207
+
208
+ const row = data[rowIndex] ?? updatedData[rowIndex];
209
+ const loading = row == null;
210
+ const col = columnSpec;
211
+
212
+ const focusedCell = state.focusedCell;
213
+ const _topLeftCell = state.topLeftCell;
214
+ const onCellEdited = state.onCellEdited;
215
+ const clearSelection = state.clearSelection;
216
+
217
+ const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
218
+
219
+ const valueRenderer = col.valueRenderer ?? ((d) => d);
220
+
221
+ const autoFocusEditor = true;
222
+
223
+ const { style } = col;
224
+
225
+ const focused =
226
+ focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
227
+ // Top left cell of a ranged selection
228
+ const topLeft =
229
+ _topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
230
+
231
+ const editable = col.editable ?? state.editable;
232
+
233
+ const edited = updatedData[rowIndex]?.[col.key] != null;
234
+ const intent = edited ? "success" : undefined;
235
+
236
+ const _Cell = col.cellComponent ?? BaseCell;
237
+
238
+ const _renderedValue = valueRenderer(value);
239
+ let inlineEditor = editable ? col.inlineEditor ?? true : false;
240
+
241
+ if (!topLeft) {
242
+ // This should be the case for every cell except the focused one
243
+ return h(
244
+ _Cell,
245
+ {
246
+ intent,
247
+ loading,
248
+ value,
249
+ style,
250
+ },
251
+ _renderedValue
252
+ );
253
+ }
254
+
255
+ if (!editable) {
256
+ // Most cells are not focused and don't need to be editable.
257
+ // This will be the rendering logic for almost all cells
258
+ return h(_Cell, { intent, value }, [
259
+ h.if(!focused)("input.hidden-input", {
260
+ autoFocus: true,
261
+ onKeyDown(e) {
262
+ if (e.key == "Backspace" || e.key == "Delete") {
263
+ clearSelection();
264
+ }
265
+ e.preventDefault();
266
+ },
267
+ }),
268
+ _renderedValue,
269
+ ]);
270
+ // Could probably put the hidden input elsewhere,
271
+ }
272
+
273
+ /* The remaining logic covers cells that are focused and editable */
274
+
275
+ const onChange = (e) => {
276
+ if (!editable) return;
277
+ if (value === e.target.value) return;
278
+ onCellEdited(rowIndex, col.key, e.target.value);
279
+ };
280
+
281
+ let cellContents = _renderedValue;
282
+ let cellClass = null;
283
+
284
+ if (typeof inlineEditor == "boolean") {
285
+ let _value = value;
286
+ if (
287
+ typeof _renderedValue === "string" ||
288
+ typeof _renderedValue === "number" ||
289
+ _renderedValue == null
290
+ ) {
291
+ _value = _renderedValue;
292
+ }
293
+ inlineEditor = h("input", {
294
+ value: _value ?? "",
295
+ autoFocus: autoFocusEditor,
296
+ onChange,
297
+ onKeyDown(e) {
298
+ if (e.key == "Enter") {
299
+ e.target.blur();
300
+ }
301
+
302
+ if (e.key == "Escape") {
303
+ e.target.blur();
304
+ e.preventDefault();
305
+ return;
306
+ }
307
+
308
+ const shouldPropagate = handleSpecialKeys(e, e.target);
309
+ if (!shouldPropagate) {
310
+ e.stopPropagation();
311
+ } else {
312
+ e.target.blur();
313
+ if (e.key !== "Escape") {
314
+ e.target.parentNode.dispatchEvent(new KeyboardEvent("keydown", e));
315
+ }
316
+ }
317
+ },
318
+ });
319
+ }
320
+
321
+ if (col.dataEditor != null) {
322
+ cellClass = "editor-cell";
323
+ cellContents = h([
324
+ h(EditorPopup, {
325
+ autoFocus: autoFocusEditor,
326
+ content: h(col.dataEditor, {
327
+ value,
328
+ onChange(value) {
329
+ if (!editable) return;
330
+ onCellEdited(rowIndex, col.key, value);
331
+ },
332
+ }),
333
+ inlineEditor,
334
+ valueViewer: _renderedValue,
335
+ }),
336
+ ]);
337
+ } else if (inlineEditor != null) {
338
+ cellContents = inlineEditor;
339
+ cellClass = "input-cell";
340
+ }
341
+
342
+ // Hidden html input
343
+ return h(
344
+ _Cell,
345
+ {
346
+ intent,
347
+ value,
348
+ className: cellClass,
349
+ style,
350
+ //truncated: false,
351
+ },
352
+ [cellContents, h.if(editable)(DragHandle, { focusedCell })]
353
+ );
354
+ }
355
+
356
+ function DragHandle({ focusedCell }) {
357
+ // TODO: we might want to drag multiple columns in some cases
358
+ // This should be on the last cell of a selection
359
+ const onDragValue = useSelector((state) => state.onDragValue);
360
+
361
+ return h("div.corner-drag-handle", {
362
+ onMouseDown(e) {
363
+ onDragValue(focusedCell);
364
+ e.preventDefault();
365
+ },
366
+ });
367
+ }
368
+
369
+ function DataSheetEditToolbar({
370
+ hasUpdates,
371
+ setUpdatedData,
372
+ onSaveData,
373
+ onAddRow,
374
+ onDeleteRows,
375
+ }) {
376
+ return h("div.data-sheet-toolbar", [
377
+ h(ButtonGroup, { minimal: true }, [
378
+ h.if(onAddRow != null)(AddRowButton, { onAddRow }),
379
+ h(
380
+ Button,
381
+ {
382
+ intent: Intent.DANGER,
383
+ disabled: onDeleteRows == null,
384
+ onClick() {
385
+ onDeleteRows?.();
386
+ },
387
+ },
388
+ "Delete"
389
+ ),
390
+ ]),
391
+ h("div.spacer"),
392
+ h(ButtonGroup, [
393
+ h(
394
+ Button,
395
+ {
396
+ intent: Intent.WARNING,
397
+ disabled: !hasUpdates,
398
+ onClick() {
399
+ setUpdatedData([]);
400
+ },
401
+ },
402
+ "Reset"
403
+ ),
404
+ h(
405
+ Button,
406
+ {
407
+ intent: Intent.SUCCESS,
408
+ icon: "floppy-disk",
409
+ disabled: !hasUpdates,
410
+ onClick: onSaveData,
411
+ },
412
+ "Save"
413
+ ),
414
+ ]),
415
+ ]);
416
+ }
417
+
418
+ export function ScrollToRowControl() {
419
+ const [value, setValue] = useState("");
420
+ const scrollToRow = useSelector((state) => state.scrollToRow);
421
+
422
+ return h(DataSheetAction, [
423
+ h(InputGroup, {
424
+ type: "number",
425
+ placeholder: "Row number",
426
+ value,
427
+ onValueChange(value) {
428
+ setValue(value);
429
+ },
430
+ }),
431
+ h(
432
+ Button,
433
+ {
434
+ icon: "arrow-right",
435
+ onClick() {
436
+ const row = parseInt(value);
437
+ scrollToRow(row - 1);
438
+ },
439
+ },
440
+ "Scroll to row"
441
+ ),
442
+ ]);
443
+ }
444
+
445
+ function AddRowButton({ onAddRow }) {
446
+ return h(
447
+ Button,
448
+ {
449
+ icon: "plus",
450
+ onClick: onAddRow,
451
+ },
452
+ "Add row"
453
+ );
454
+ }
455
+
456
+ export function BaseCell({ children, value, ...rest }) {
457
+ return h(
458
+ Cell,
459
+ {
460
+ interactive: true,
461
+ ...rest,
462
+ },
463
+ children
464
+ );
465
+ }
466
+
467
+ export { Cell };
468
+
469
+ export function getRowsToDelete(selection) {
470
+ let rowIndices: number[] = [];
471
+ for (const sel of selection) {
472
+ // This isn't a full-row selection
473
+ if (sel.cols != null) continue;
474
+ if (sel.rows == null) continue;
475
+ const [startIndex, endIndex] = sel.rows;
476
+ for (let i = startIndex; i <= endIndex; i++) {
477
+ rowIndices.push(i);
478
+ }
479
+ }
480
+ return rowIndices;
481
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./core";
2
+ export * from "./postgrest-table";
@@ -0,0 +1,77 @@
1
+ .data-sheet-container, .data-sheet-holder
2
+ flex: 1
3
+ min-height: 0
4
+
5
+ .data-sheet-container
6
+ display: flex
7
+ position: relative
8
+ height: 100%
9
+ flex-direction: column
10
+ min-height: 0
11
+ max-width: 100%
12
+
13
+ .data-sheet
14
+ height: 100%
15
+ :global(.bp5-table-quadrant)
16
+ background-color: var(--panel-secondary-background-color)
17
+
18
+ .input-cell
19
+ padding: 0 2px !important
20
+
21
+ input
22
+ width: 100%
23
+ height: 100%
24
+ padding: 0 8px
25
+ z-index: 0
26
+ position: relative
27
+ border: none
28
+ margin: 0
29
+ pointer-events: all
30
+ background: transparent
31
+ font-family: inherit
32
+ font-size: inherit
33
+
34
+ &:focus
35
+ outline: none
36
+
37
+ // Make sure popover target fills the entire cell
38
+ .editor-cell :global(.bp5-popover-target)
39
+ //pointer-events: none
40
+ position: absolute
41
+ top: 0
42
+ left: 0
43
+ width: 100%
44
+ height: 100%
45
+
46
+ .hidden-input
47
+ opacity: 0
48
+ position: absolute
49
+ width: 0
50
+
51
+ .data-editor-anchor
52
+ width: 100%
53
+ height: 100%
54
+ margin: 0
55
+ pointer-events: none
56
+
57
+ .corner-drag-handle
58
+ position: absolute
59
+ bottom: 0
60
+ right: 0
61
+ width: 8px
62
+ height: 8px
63
+ cursor: ns-resize
64
+ background-color: dodgerblue
65
+
66
+ .data-sheet-toolbar
67
+ display: flex
68
+ flex-direction: row
69
+ margin-bottom: 4px
70
+
71
+ .spacer
72
+ flex-grow: 1
73
+
74
+ .table-control
75
+ display: flex
76
+ flex-direction: row
77
+ margin-bottom: 0.25em
@@ -0,0 +1,24 @@
1
+ import { useInDarkMode } from "@macrostrat/ui-components";
2
+ import h from "@macrostrat/hyper";
3
+ import { Tag } from "@blueprintjs/core";
4
+ import { asChromaColor } from "@macrostrat/color-utils";
5
+ import classNames from "classnames";
6
+
7
+ export function LithologyTag({ data, className = null }) {
8
+ const darkMode = useInDarkMode();
9
+ const luminance = darkMode ? 0.9 : 0.4;
10
+ const color = asChromaColor(data.color);
11
+ return h(
12
+ Tag,
13
+ {
14
+ key: data.id,
15
+ className: classNames("lithology-tag", className),
16
+ minimal: true,
17
+ style: {
18
+ color: color?.luminance(luminance).hex(),
19
+ backgroundColor: color?.luminance(1 - luminance).hex(),
20
+ },
21
+ },
22
+ h("span.contents", [h("span.name", data.name)])
23
+ );
24
+ }