@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.
- package/CHANGELOG.md +16 -0
- package/README.md +19 -0
- package/dist/actions.5740e8e2.js +21 -0
- package/dist/actions.5740e8e2.js.map +1 -0
- package/dist/actions.module.6ec06797.css +11 -0
- package/dist/actions.module.6ec06797.css.map +1 -0
- package/dist/actions.module.ea96403b.js +16 -0
- package/dist/actions.module.ea96403b.js.map +1 -0
- package/dist/cell-renderers.de70f471.js +31 -0
- package/dist/cell-renderers.de70f471.js.map +1 -0
- package/dist/colors.41c3ed7a.js +83 -0
- package/dist/colors.41c3ed7a.js.map +1 -0
- package/dist/column-spec.4710da93.js +70 -0
- package/dist/column-spec.4710da93.js.map +1 -0
- package/dist/components.c3ad66e9.js +11 -0
- package/dist/components.c3ad66e9.js.map +1 -0
- package/dist/core.d8d80480.js +356 -0
- package/dist/core.d8d80480.js.map +1 -0
- package/dist/data-loaders.5302dedd.js +196 -0
- package/dist/data-loaders.5302dedd.js.map +1 -0
- package/dist/editor-popup.9259608f.js +68 -0
- package/dist/editor-popup.9259608f.js.map +1 -0
- package/dist/index.d.ts +202 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/main.module.846377cd.css +35 -0
- package/dist/main.module.846377cd.css.map +1 -0
- package/dist/main.module.914caa19.css +91 -0
- package/dist/main.module.914caa19.css.map +1 -0
- package/dist/main.module.93f91ad4.js +25 -0
- package/dist/main.module.93f91ad4.js.map +1 -0
- package/dist/main.module.dc8f6293.css +62 -0
- package/dist/main.module.dc8f6293.css.map +1 -0
- package/dist/main.module.de002429.js +43 -0
- package/dist/main.module.de002429.js.map +1 -0
- package/dist/main.module.ff717a08.js +37 -0
- package/dist/main.module.ff717a08.js.map +1 -0
- package/dist/postgrest-table.15ded3c1.js +173 -0
- package/dist/postgrest-table.15ded3c1.js.map +1 -0
- package/dist/provider.67e982b8.js +232 -0
- package/dist/provider.67e982b8.js.map +1 -0
- package/dist/text-areas.079e8b15.js +78 -0
- package/dist/text-areas.079e8b15.js.map +1 -0
- package/dist/utils.a37ed786.js +14 -0
- package/dist/utils.a37ed786.js.map +1 -0
- package/package.json +50 -0
- package/src/__archive/base.ts +55 -0
- package/src/__archive/components/column-utils.ts +126 -0
- package/src/__archive/components/index.ts +115 -0
- package/src/__archive/components/toolbar.ts +79 -0
- package/src/__archive/editors/index.ts +78 -0
- package/src/__archive/editors/styles.module.scss +10 -0
- package/src/__archive/enhancers.ts +36 -0
- package/src/__archive/index.ts +8 -0
- package/src/__archive/main.ts +180 -0
- package/src/__archive/provider.ts +179 -0
- package/src/__archive/styles.module.scss +56 -0
- package/src/__archive/test-sheet/attitude-sheet.old.styl +53 -0
- package/src/__archive/test-sheet/attitude-sheet.scss +60 -0
- package/src/__archive/test-sheet/index.ts +210 -0
- package/src/__archive/virtualized.ts +82 -0
- package/src/components/actions.module.sass +8 -0
- package/src/components/actions.ts +11 -0
- package/src/components/colors.ts +90 -0
- package/src/components/editor-popup.ts +69 -0
- package/src/components/index.ts +3 -0
- package/src/components/main.module.sass +35 -0
- package/src/components/text-areas.ts +88 -0
- package/src/core.ts +481 -0
- package/src/index.ts +2 -0
- package/src/main.module.sass +77 -0
- package/src/postgrest-table/cell-renderers.ts +24 -0
- package/src/postgrest-table/data-loaders.ts +287 -0
- package/src/postgrest-table/index.ts +172 -0
- package/src/postgrest-table/main.module.sass +52 -0
- package/src/provider.ts +282 -0
- package/src/utils/column-spec.ts +97 -0
- package/src/utils/index.ts +7 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import {handleSpecialKeys as $0aa421c11d2af971$export$52c8c5a1aad17e2a, EditorPopup as $88e63ff6e4eea209$export$2a7e60c8443c1751, ColorCell as $423bf54b4879ebeb$export$24aecfe8a925020d, pleasantCombination as $423bf54b4879ebeb$export$ba6491245befce5c, ColorPicker as $423bf54b4879ebeb$export$9feb1bc2e5f1ccb3, EditableTextArea as $0aa421c11d2af971$export$b8e68c321a6269f1} from "./components.c3ad66e9.js";
|
|
2
|
+
import "./main.module.914caa19.css";
|
|
3
|
+
import $8jZSu$mainmodulede002429js from "./main.module.de002429.js";
|
|
4
|
+
import {DataSheetProvider as $4b6643d78a77a8eb$export$135c527978291360, useSelector as $4b6643d78a77a8eb$export$2105062e25df4afe, useStoreAPI as $4b6643d78a77a8eb$export$54fc6f5c8f914926} from "./provider.67e982b8.js";
|
|
5
|
+
import {DataSheetAction as $a36e736abd533bdf$export$c2c7070a9fc246e1} from "./actions.5740e8e2.js";
|
|
6
|
+
import {HotkeysProvider as $8jZSu$HotkeysProvider, ButtonGroup as $8jZSu$ButtonGroup, Button as $8jZSu$Button, Intent as $8jZSu$Intent, InputGroup as $8jZSu$InputGroup} from "@blueprintjs/core";
|
|
7
|
+
import {Table2 as $8jZSu$Table2, Column as $8jZSu$Column, Cell as $8bdde5bf56f8afde$import$5573889ca4ff9ca2$f6f0c3fe4ec306ea} from "@blueprintjs/table";
|
|
8
|
+
import "@blueprintjs/table/lib/css/table.css";
|
|
9
|
+
import $8jZSu$macrostrathyper from "@macrostrat/hyper";
|
|
10
|
+
import $8jZSu$immutabilityhelper from "immutability-helper";
|
|
11
|
+
import {useCallback as $8jZSu$useCallback, useEffect as $8jZSu$useEffect, useState as $8jZSu$useState} from "react";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
function $parcel$interopDefault(a) {
|
|
15
|
+
return a && a.__esModule ? a.default : a;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const $8bdde5bf56f8afde$var$h = (0, $8jZSu$macrostrathyper).styled((0, ($parcel$interopDefault($8jZSu$mainmodulede002429js))));
|
|
28
|
+
function $8bdde5bf56f8afde$export$dea56b0125f0282d(props) {
|
|
29
|
+
const { data: data, columnSpec: columnSpec, columnSpecOptions: columnSpecOptions, editable: editable = true, enableColumnReordering: enableColumnReordering = false, ...rest } = props;
|
|
30
|
+
return $8bdde5bf56f8afde$var$h((0, $8jZSu$HotkeysProvider), $8bdde5bf56f8afde$var$h((0, $4b6643d78a77a8eb$export$135c527978291360), {
|
|
31
|
+
data: data,
|
|
32
|
+
columnSpec: columnSpec,
|
|
33
|
+
columnSpecOptions: columnSpecOptions,
|
|
34
|
+
enableColumnReordering: enableColumnReordering,
|
|
35
|
+
editable: editable,
|
|
36
|
+
...rest
|
|
37
|
+
}, $8bdde5bf56f8afde$var$h($8bdde5bf56f8afde$var$_DataSheet, {
|
|
38
|
+
...rest,
|
|
39
|
+
editable: editable,
|
|
40
|
+
enableColumnReordering: enableColumnReordering
|
|
41
|
+
})));
|
|
42
|
+
}
|
|
43
|
+
function $8bdde5bf56f8afde$var$_DataSheet({ onVisibleCellsChange: onVisibleCellsChange, enableColumnReordering: enableColumnReordering, onSaveData: onSaveData, onDeleteRows: onDeleteRows, verbose: verbose = true, dataSheetActions: dataSheetActions = null }) {
|
|
44
|
+
/**
|
|
45
|
+
* @param data: The data to be displayed in the table
|
|
46
|
+
* @param columnSpec: The specification for all columns in the table. If not provided, the column spec will be generated from the data.
|
|
47
|
+
* @param columnSpecOptions: Options for generating a column spec from data
|
|
48
|
+
*/ // For now, we only consider a single cell "focused" when we have one cell selected.
|
|
49
|
+
// Multi-cell selections have a different set of "bulk" actions.
|
|
50
|
+
const selection = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.selection);
|
|
51
|
+
const data = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.data);
|
|
52
|
+
const editable = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.editable);
|
|
53
|
+
const focusedCell = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.focusedCell);
|
|
54
|
+
const ref = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.tableRef);
|
|
55
|
+
const columnSpec = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.columnSpec);
|
|
56
|
+
// A sparse array to hold updates
|
|
57
|
+
// TODO: create a "changeset" concept to facilitate undo/redo
|
|
58
|
+
const updatedData = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.updatedData);
|
|
59
|
+
const setUpdatedData = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.setUpdatedData);
|
|
60
|
+
const hasUpdates = updatedData.length > 0;
|
|
61
|
+
const onSelection = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.onSelection);
|
|
62
|
+
const storeAPI = (0, $4b6643d78a77a8eb$export$54fc6f5c8f914926)();
|
|
63
|
+
const onColumnsReordered = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.onColumnsReordered);
|
|
64
|
+
const _onSaveData = (0, $8jZSu$useCallback)(()=>{
|
|
65
|
+
onSaveData(updatedData, data);
|
|
66
|
+
setUpdatedData([]);
|
|
67
|
+
}, [
|
|
68
|
+
updatedData,
|
|
69
|
+
data,
|
|
70
|
+
onSaveData
|
|
71
|
+
]);
|
|
72
|
+
const setVisibleCells = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.setVisibleCells);
|
|
73
|
+
const _onVisibleCellsChange = (0, $8jZSu$useCallback)((visibleCells)=>{
|
|
74
|
+
setVisibleCells(visibleCells);
|
|
75
|
+
onVisibleCellsChange?.(visibleCells);
|
|
76
|
+
}, [
|
|
77
|
+
onVisibleCellsChange,
|
|
78
|
+
setVisibleCells
|
|
79
|
+
]);
|
|
80
|
+
const onAddRow = (0, $8jZSu$useCallback)(()=>{
|
|
81
|
+
setUpdatedData((updatedData)=>{
|
|
82
|
+
const ix = data.length;
|
|
83
|
+
const addRowSpec = {
|
|
84
|
+
[ix]: {
|
|
85
|
+
$set: {}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const newUpdatedData = (0, $8jZSu$immutabilityhelper)(updatedData, addRowSpec);
|
|
89
|
+
return newUpdatedData;
|
|
90
|
+
});
|
|
91
|
+
}, [
|
|
92
|
+
setUpdatedData,
|
|
93
|
+
data
|
|
94
|
+
]);
|
|
95
|
+
const _onDeleteRows = (0, $8jZSu$useCallback)(()=>{
|
|
96
|
+
onDeleteRows(selection);
|
|
97
|
+
}, [
|
|
98
|
+
onDeleteRows,
|
|
99
|
+
selection
|
|
100
|
+
]);
|
|
101
|
+
(0, $8jZSu$useEffect)(()=>{
|
|
102
|
+
if (!verbose) return;
|
|
103
|
+
console.log("Updated data", updatedData);
|
|
104
|
+
}, [
|
|
105
|
+
updatedData
|
|
106
|
+
]);
|
|
107
|
+
(0, $8jZSu$useEffect)(()=>{
|
|
108
|
+
if (!verbose) return;
|
|
109
|
+
console.log("Selection", selection);
|
|
110
|
+
}, [
|
|
111
|
+
selection
|
|
112
|
+
]);
|
|
113
|
+
if (data == null) return null;
|
|
114
|
+
const nDeletionCandidates = $8bdde5bf56f8afde$export$222fa1b971ddc36(selection).length;
|
|
115
|
+
const numRows = Math.max(updatedData.length, data.length);
|
|
116
|
+
return $8bdde5bf56f8afde$var$h("div.data-sheet-container", [
|
|
117
|
+
$8bdde5bf56f8afde$var$h.if(editable)($8bdde5bf56f8afde$var$DataSheetEditToolbar, {
|
|
118
|
+
hasUpdates: hasUpdates,
|
|
119
|
+
setUpdatedData: setUpdatedData,
|
|
120
|
+
onSaveData: _onSaveData,
|
|
121
|
+
onAddRow: onAddRow,
|
|
122
|
+
onDeleteRows: nDeletionCandidates > 0 ? _onDeleteRows : null
|
|
123
|
+
}),
|
|
124
|
+
dataSheetActions,
|
|
125
|
+
$8bdde5bf56f8afde$var$h("div.data-sheet-holder", [
|
|
126
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$Table2), {
|
|
127
|
+
ref: ref,
|
|
128
|
+
numRows: numRows,
|
|
129
|
+
className: "data-sheet",
|
|
130
|
+
enableFocusedCell: true,
|
|
131
|
+
enableColumnReordering: enableColumnReordering,
|
|
132
|
+
onColumnsReordered: onColumnsReordered,
|
|
133
|
+
focusedCell: focusedCell,
|
|
134
|
+
selectedRegions: selection,
|
|
135
|
+
onSelection: onSelection,
|
|
136
|
+
// The cell renderer is memoized internally based on these data dependencies
|
|
137
|
+
cellRendererDependencies: [
|
|
138
|
+
selection,
|
|
139
|
+
updatedData,
|
|
140
|
+
focusedCell,
|
|
141
|
+
data
|
|
142
|
+
],
|
|
143
|
+
onVisibleCellsChange: _onVisibleCellsChange
|
|
144
|
+
}, columnSpec.map((col, colIndex)=>{
|
|
145
|
+
return $8bdde5bf56f8afde$var$h((0, $8jZSu$Column), {
|
|
146
|
+
name: col.name,
|
|
147
|
+
cellRenderer: (rowIndex)=>{
|
|
148
|
+
const state = storeAPI.getState();
|
|
149
|
+
return $8bdde5bf56f8afde$var$basicCellRenderer(rowIndex, colIndex, col, state);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}))
|
|
153
|
+
])
|
|
154
|
+
]);
|
|
155
|
+
}
|
|
156
|
+
function $8bdde5bf56f8afde$var$basicCellRenderer(rowIndex, colIndex, columnSpec, state) {
|
|
157
|
+
const data = state.data;
|
|
158
|
+
const updatedData = state.updatedData;
|
|
159
|
+
const row = data[rowIndex] ?? updatedData[rowIndex];
|
|
160
|
+
const loading = row == null;
|
|
161
|
+
const col = columnSpec;
|
|
162
|
+
const focusedCell = state.focusedCell;
|
|
163
|
+
const _topLeftCell = state.topLeftCell;
|
|
164
|
+
const onCellEdited = state.onCellEdited;
|
|
165
|
+
const clearSelection = state.clearSelection;
|
|
166
|
+
const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
|
|
167
|
+
const valueRenderer = col.valueRenderer ?? ((d)=>d);
|
|
168
|
+
const autoFocusEditor = true;
|
|
169
|
+
const { style: style } = col;
|
|
170
|
+
const focused = focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
|
|
171
|
+
// Top left cell of a ranged selection
|
|
172
|
+
const topLeft = _topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
|
|
173
|
+
const editable = col.editable ?? state.editable;
|
|
174
|
+
const edited = updatedData[rowIndex]?.[col.key] != null;
|
|
175
|
+
const intent = edited ? "success" : undefined;
|
|
176
|
+
const _Cell = col.cellComponent ?? $8bdde5bf56f8afde$export$41ad0bbfa4755cac;
|
|
177
|
+
const _renderedValue = valueRenderer(value);
|
|
178
|
+
let inlineEditor = editable ? col.inlineEditor ?? true : false;
|
|
179
|
+
if (!topLeft) // This should be the case for every cell except the focused one
|
|
180
|
+
return $8bdde5bf56f8afde$var$h(_Cell, {
|
|
181
|
+
intent: intent,
|
|
182
|
+
loading: loading,
|
|
183
|
+
value: value,
|
|
184
|
+
style: style
|
|
185
|
+
}, _renderedValue);
|
|
186
|
+
if (!editable) // Most cells are not focused and don't need to be editable.
|
|
187
|
+
// This will be the rendering logic for almost all cells
|
|
188
|
+
return $8bdde5bf56f8afde$var$h(_Cell, {
|
|
189
|
+
intent: intent,
|
|
190
|
+
value: value
|
|
191
|
+
}, [
|
|
192
|
+
$8bdde5bf56f8afde$var$h.if(!focused)("input.hidden-input", {
|
|
193
|
+
autoFocus: true,
|
|
194
|
+
onKeyDown (e) {
|
|
195
|
+
if (e.key == "Backspace" || e.key == "Delete") clearSelection();
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
}
|
|
198
|
+
}),
|
|
199
|
+
_renderedValue
|
|
200
|
+
]);
|
|
201
|
+
/* The remaining logic covers cells that are focused and editable */ const onChange = (e)=>{
|
|
202
|
+
if (!editable) return;
|
|
203
|
+
if (value === e.target.value) return;
|
|
204
|
+
onCellEdited(rowIndex, col.key, e.target.value);
|
|
205
|
+
};
|
|
206
|
+
let cellContents = _renderedValue;
|
|
207
|
+
let cellClass = null;
|
|
208
|
+
if (typeof inlineEditor == "boolean") {
|
|
209
|
+
let _value = value;
|
|
210
|
+
if (typeof _renderedValue === "string" || typeof _renderedValue === "number" || _renderedValue == null) _value = _renderedValue;
|
|
211
|
+
inlineEditor = $8bdde5bf56f8afde$var$h("input", {
|
|
212
|
+
value: _value ?? "",
|
|
213
|
+
autoFocus: autoFocusEditor,
|
|
214
|
+
onChange: onChange,
|
|
215
|
+
onKeyDown (e) {
|
|
216
|
+
if (e.key == "Enter") e.target.blur();
|
|
217
|
+
if (e.key == "Escape") {
|
|
218
|
+
e.target.blur();
|
|
219
|
+
e.preventDefault();
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const shouldPropagate = (0, $8jZSu$handleSpecialKeys)(e, e.target);
|
|
223
|
+
if (!shouldPropagate) e.stopPropagation();
|
|
224
|
+
else {
|
|
225
|
+
e.target.blur();
|
|
226
|
+
if (e.key !== "Escape") e.target.parentNode.dispatchEvent(new KeyboardEvent("keydown", e));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
if (col.dataEditor != null) {
|
|
232
|
+
cellClass = "editor-cell";
|
|
233
|
+
cellContents = $8bdde5bf56f8afde$var$h([
|
|
234
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$EditorPopup), {
|
|
235
|
+
autoFocus: autoFocusEditor,
|
|
236
|
+
content: $8bdde5bf56f8afde$var$h(col.dataEditor, {
|
|
237
|
+
value: value,
|
|
238
|
+
onChange (value) {
|
|
239
|
+
if (!editable) return;
|
|
240
|
+
onCellEdited(rowIndex, col.key, value);
|
|
241
|
+
}
|
|
242
|
+
}),
|
|
243
|
+
inlineEditor: inlineEditor,
|
|
244
|
+
valueViewer: _renderedValue
|
|
245
|
+
})
|
|
246
|
+
]);
|
|
247
|
+
} else if (inlineEditor != null) {
|
|
248
|
+
cellContents = inlineEditor;
|
|
249
|
+
cellClass = "input-cell";
|
|
250
|
+
}
|
|
251
|
+
// Hidden html input
|
|
252
|
+
return $8bdde5bf56f8afde$var$h(_Cell, {
|
|
253
|
+
intent: intent,
|
|
254
|
+
value: value,
|
|
255
|
+
className: cellClass,
|
|
256
|
+
style: style
|
|
257
|
+
}, [
|
|
258
|
+
cellContents,
|
|
259
|
+
$8bdde5bf56f8afde$var$h.if(editable)($8bdde5bf56f8afde$var$DragHandle, {
|
|
260
|
+
focusedCell: focusedCell
|
|
261
|
+
})
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
function $8bdde5bf56f8afde$var$DragHandle({ focusedCell: focusedCell }) {
|
|
265
|
+
// TODO: we might want to drag multiple columns in some cases
|
|
266
|
+
// This should be on the last cell of a selection
|
|
267
|
+
const onDragValue = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.onDragValue);
|
|
268
|
+
return $8bdde5bf56f8afde$var$h("div.corner-drag-handle", {
|
|
269
|
+
onMouseDown (e) {
|
|
270
|
+
onDragValue(focusedCell);
|
|
271
|
+
e.preventDefault();
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
function $8bdde5bf56f8afde$var$DataSheetEditToolbar({ hasUpdates: hasUpdates, setUpdatedData: setUpdatedData, onSaveData: onSaveData, onAddRow: onAddRow, onDeleteRows: onDeleteRows }) {
|
|
276
|
+
return $8bdde5bf56f8afde$var$h("div.data-sheet-toolbar", [
|
|
277
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$ButtonGroup), {
|
|
278
|
+
minimal: true
|
|
279
|
+
}, [
|
|
280
|
+
$8bdde5bf56f8afde$var$h.if(onAddRow != null)($8bdde5bf56f8afde$var$AddRowButton, {
|
|
281
|
+
onAddRow: onAddRow
|
|
282
|
+
}),
|
|
283
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$Button), {
|
|
284
|
+
intent: (0, $8jZSu$Intent).DANGER,
|
|
285
|
+
disabled: onDeleteRows == null,
|
|
286
|
+
onClick () {
|
|
287
|
+
onDeleteRows?.();
|
|
288
|
+
}
|
|
289
|
+
}, "Delete")
|
|
290
|
+
]),
|
|
291
|
+
$8bdde5bf56f8afde$var$h("div.spacer"),
|
|
292
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$ButtonGroup), [
|
|
293
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$Button), {
|
|
294
|
+
intent: (0, $8jZSu$Intent).WARNING,
|
|
295
|
+
disabled: !hasUpdates,
|
|
296
|
+
onClick () {
|
|
297
|
+
setUpdatedData([]);
|
|
298
|
+
}
|
|
299
|
+
}, "Reset"),
|
|
300
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$Button), {
|
|
301
|
+
intent: (0, $8jZSu$Intent).SUCCESS,
|
|
302
|
+
icon: "floppy-disk",
|
|
303
|
+
disabled: !hasUpdates,
|
|
304
|
+
onClick: onSaveData
|
|
305
|
+
}, "Save")
|
|
306
|
+
])
|
|
307
|
+
]);
|
|
308
|
+
}
|
|
309
|
+
function $8bdde5bf56f8afde$export$e7db3202e96fc463() {
|
|
310
|
+
const [value, setValue] = (0, $8jZSu$useState)("");
|
|
311
|
+
const scrollToRow = (0, $4b6643d78a77a8eb$export$2105062e25df4afe)((state)=>state.scrollToRow);
|
|
312
|
+
return $8bdde5bf56f8afde$var$h((0, $a36e736abd533bdf$export$c2c7070a9fc246e1), [
|
|
313
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$InputGroup), {
|
|
314
|
+
type: "number",
|
|
315
|
+
placeholder: "Row number",
|
|
316
|
+
value: value,
|
|
317
|
+
onValueChange (value) {
|
|
318
|
+
setValue(value);
|
|
319
|
+
}
|
|
320
|
+
}),
|
|
321
|
+
$8bdde5bf56f8afde$var$h((0, $8jZSu$Button), {
|
|
322
|
+
icon: "arrow-right",
|
|
323
|
+
onClick () {
|
|
324
|
+
const row = parseInt(value);
|
|
325
|
+
scrollToRow(row - 1);
|
|
326
|
+
}
|
|
327
|
+
}, "Scroll to row")
|
|
328
|
+
]);
|
|
329
|
+
}
|
|
330
|
+
function $8bdde5bf56f8afde$var$AddRowButton({ onAddRow: onAddRow }) {
|
|
331
|
+
return $8bdde5bf56f8afde$var$h((0, $8jZSu$Button), {
|
|
332
|
+
icon: "plus",
|
|
333
|
+
onClick: onAddRow
|
|
334
|
+
}, "Add row");
|
|
335
|
+
}
|
|
336
|
+
function $8bdde5bf56f8afde$export$41ad0bbfa4755cac({ children: children, value: value, ...rest }) {
|
|
337
|
+
return $8bdde5bf56f8afde$var$h((0, $8bdde5bf56f8afde$import$5573889ca4ff9ca2$f6f0c3fe4ec306ea), {
|
|
338
|
+
interactive: true,
|
|
339
|
+
...rest
|
|
340
|
+
}, children);
|
|
341
|
+
}
|
|
342
|
+
function $8bdde5bf56f8afde$export$222fa1b971ddc36(selection) {
|
|
343
|
+
let rowIndices = [];
|
|
344
|
+
for (const sel of selection){
|
|
345
|
+
// This isn't a full-row selection
|
|
346
|
+
if (sel.cols != null) continue;
|
|
347
|
+
if (sel.rows == null) continue;
|
|
348
|
+
const [startIndex, endIndex] = sel.rows;
|
|
349
|
+
for(let i = startIndex; i <= endIndex; i++)rowIndices.push(i);
|
|
350
|
+
}
|
|
351
|
+
return rowIndices;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
export {$8bdde5bf56f8afde$export$dea56b0125f0282d as DataSheet, $8bdde5bf56f8afde$export$222fa1b971ddc36 as getRowsToDelete, $8bdde5bf56f8afde$export$41ad0bbfa4755cac as BaseCell, $8bdde5bf56f8afde$export$e7db3202e96fc463 as ScrollToRowControl, $8bdde5bf56f8afde$import$5573889ca4ff9ca2$f6f0c3fe4ec306ea as Cell, $88e63ff6e4eea209$export$2a7e60c8443c1751 as EditorPopup, $423bf54b4879ebeb$export$24aecfe8a925020d as ColorCell, $423bf54b4879ebeb$export$ba6491245befce5c as pleasantCombination, $423bf54b4879ebeb$export$9feb1bc2e5f1ccb3 as ColorPicker, $0aa421c11d2af971$export$b8e68c321a6269f1 as EditableTextArea, $0aa421c11d2af971$export$52c8c5a1aad17e2a as handleSpecialKeys};
|
|
356
|
+
//# sourceMappingURL=core.d8d80480.js.map
|
|
@@ -0,0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C3B,kCAAkC;QAClC,IAAI,IAAI,IAAI,IAAI,MAAM;QACtB,IAAI,IAAI,IAAI,IAAI,MAAM;QACtB,MAAM,CAAC,YAAY,SAAS,GAAG,IAAI,IAAI;QACvC,IAAK,IAAI,IAAI,YAAY,KAAK,UAAU,IACtC,WAAW,IAAI,CAAC;IAEpB;IACA,OAAO;AACT","sources":["packages/data-sheet/src/core.ts"],"sourcesContent":["import {\n Button,\n ButtonGroup,\n HotkeysProvider,\n InputGroup,\n Intent,\n} from \"@blueprintjs/core\";\nimport { Cell, Column, Region, Table2 } from \"@blueprintjs/table\";\nimport \"@blueprintjs/table/lib/css/table.css\";\nimport hyper from \"@macrostrat/hyper\";\nimport update from \"immutability-helper\";\nimport { ReactNode, useCallback, useEffect, useRef, useState } from \"react\";\nimport { EditorPopup, handleSpecialKeys } from \"./components\";\nimport styles from \"./main.module.sass\";\nimport {\n ColumnSpec,\n ColumnSpecOptions,\n DataSheetProvider,\n DataSheetProviderProps,\n DataSheetStore,\n useSelector,\n useStoreAPI,\n VisibleCells,\n} from \"./provider\";\nimport { DataSheetAction } from \"./components/actions\";\n\nexport type { ColumnSpec, ColumnSpecOptions };\nexport * from \"./components\";\n\nconst h = hyper.styled(styles);\n\n// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args\n\n// TODO: add a \"copy to selection\" tool (the little square in the bottom right corner of a cell)\n// This should copy the value of a cell (or a set of cells in the same row) downwards.\n\ninterface DataSheetInternalProps<T> {\n onVisibleCellsChange?: (visibleCells: VisibleCells) => void;\n onSaveData: (updatedData: any[], data: any[]) => void;\n onDeleteRows?: (selection: Region[]) => void;\n verbose?: boolean;\n enableColumnReordering?: boolean;\n dataSheetActions?: ReactNode | null;\n}\n\ntype DataSheetProps<T> = DataSheetProviderProps<T> & DataSheetInternalProps<T>;\n\nexport function DataSheet<T>(props: DataSheetProps<T>) {\n const {\n data,\n columnSpec,\n columnSpecOptions,\n editable = true,\n enableColumnReordering = false,\n ...rest\n } = props;\n\n return h(\n HotkeysProvider,\n h(\n DataSheetProvider<T>,\n {\n data,\n columnSpec,\n columnSpecOptions: columnSpecOptions,\n enableColumnReordering,\n editable,\n ...rest,\n },\n h(_DataSheet, { ...rest, editable, enableColumnReordering })\n )\n );\n}\n\nfunction _DataSheet<T>({\n onVisibleCellsChange,\n enableColumnReordering,\n onSaveData,\n onDeleteRows,\n verbose = true,\n dataSheetActions = null,\n}: DataSheetInternalProps<T>) {\n /**\n * @param data: The data to be displayed in the table\n * @param columnSpec: The specification for all columns in the table. If not provided, the column spec will be generated from the data.\n * @param columnSpecOptions: Options for generating a column spec from data\n */\n\n // For now, we only consider a single cell \"focused\" when we have one cell selected.\n // Multi-cell selections have a different set of \"bulk\" actions.\n const selection = useSelector<T>((state) => state.selection);\n\n const data = useSelector((state) => state.data);\n const editable = useSelector((state) => state.editable);\n\n const focusedCell = useSelector((state) => state.focusedCell);\n\n const ref = useSelector((state) => state.tableRef);\n\n const columnSpec = useSelector((state) => state.columnSpec);\n\n // A sparse array to hold updates\n // TODO: create a \"changeset\" concept to facilitate undo/redo\n const updatedData = useSelector((state) => state.updatedData);\n const setUpdatedData = useSelector((state) => state.setUpdatedData);\n\n const hasUpdates = updatedData.length > 0;\n\n const onSelection = useSelector((state) => state.onSelection);\n\n const storeAPI = useStoreAPI<T>();\n\n const onColumnsReordered = useSelector((state) => state.onColumnsReordered);\n\n const _onSaveData = useCallback(() => {\n onSaveData(updatedData, data);\n setUpdatedData([]);\n }, [updatedData, data, onSaveData]);\n\n const setVisibleCells = useSelector((state) => state.setVisibleCells);\n\n const _onVisibleCellsChange = useCallback(\n (visibleCells: VisibleCells) => {\n setVisibleCells(visibleCells);\n onVisibleCellsChange?.(visibleCells);\n },\n [onVisibleCellsChange, setVisibleCells]\n );\n\n const onAddRow = useCallback(() => {\n setUpdatedData((updatedData) => {\n const ix = data.length;\n const addRowSpec = { [ix]: { $set: {} } };\n const newUpdatedData = update(updatedData, addRowSpec);\n return newUpdatedData;\n });\n }, [setUpdatedData, data]);\n\n const _onDeleteRows = useCallback(() => {\n onDeleteRows(selection);\n }, [onDeleteRows, selection]);\n\n useEffect(() => {\n if (!verbose) return;\n console.log(\"Updated data\", updatedData);\n }, [updatedData]);\n\n useEffect(() => {\n if (!verbose) return;\n console.log(\"Selection\", selection);\n }, [selection]);\n\n if (data == null) return null;\n\n const nDeletionCandidates = getRowsToDelete(selection).length;\n\n const numRows = Math.max(updatedData.length, data.length);\n\n return h(\"div.data-sheet-container\", [\n h.if(editable)(DataSheetEditToolbar, {\n hasUpdates,\n setUpdatedData,\n onSaveData: _onSaveData,\n onAddRow,\n onDeleteRows: nDeletionCandidates > 0 ? _onDeleteRows : null,\n }),\n dataSheetActions,\n h(\"div.data-sheet-holder\", [\n h(\n Table2,\n {\n ref,\n numRows,\n className: \"data-sheet\",\n enableFocusedCell: true,\n enableColumnReordering,\n onColumnsReordered,\n focusedCell,\n selectedRegions: selection,\n onSelection,\n // The cell renderer is memoized internally based on these data dependencies\n cellRendererDependencies: [selection, updatedData, focusedCell, data],\n onVisibleCellsChange: _onVisibleCellsChange,\n },\n columnSpec.map((col, colIndex) => {\n return h(Column, {\n name: col.name,\n cellRenderer: (rowIndex) => {\n const state = storeAPI.getState();\n return basicCellRenderer<T>(rowIndex, colIndex, col, state);\n },\n });\n })\n ),\n ]),\n ]);\n}\n\nfunction basicCellRenderer<T>(\n rowIndex: number,\n colIndex: number,\n columnSpec: ColumnSpec,\n state: DataSheetStore<T>\n): any {\n const data = state.data;\n const updatedData = state.updatedData;\n\n const row = data[rowIndex] ?? updatedData[rowIndex];\n const loading = row == null;\n const col = columnSpec;\n\n const focusedCell = state.focusedCell;\n const _topLeftCell = state.topLeftCell;\n const onCellEdited = state.onCellEdited;\n const clearSelection = state.clearSelection;\n\n const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];\n\n const valueRenderer = col.valueRenderer ?? ((d) => d);\n\n const autoFocusEditor = true;\n\n const { style } = col;\n\n const focused =\n focusedCell?.col === colIndex && focusedCell?.row === rowIndex;\n // Top left cell of a ranged selection\n const topLeft =\n _topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;\n\n const editable = col.editable ?? state.editable;\n\n const edited = updatedData[rowIndex]?.[col.key] != null;\n const intent = edited ? \"success\" : undefined;\n\n const _Cell = col.cellComponent ?? BaseCell;\n\n const _renderedValue = valueRenderer(value);\n let inlineEditor = editable ? col.inlineEditor ?? true : false;\n\n if (!topLeft) {\n // This should be the case for every cell except the focused one\n return h(\n _Cell,\n {\n intent,\n loading,\n value,\n style,\n },\n _renderedValue\n );\n }\n\n if (!editable) {\n // Most cells are not focused and don't need to be editable.\n // This will be the rendering logic for almost all cells\n return h(_Cell, { intent, value }, [\n h.if(!focused)(\"input.hidden-input\", {\n autoFocus: true,\n onKeyDown(e) {\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n clearSelection();\n }\n e.preventDefault();\n },\n }),\n _renderedValue,\n ]);\n // Could probably put the hidden input elsewhere,\n }\n\n /* The remaining logic covers cells that are focused and editable */\n\n const onChange = (e) => {\n if (!editable) return;\n if (value === e.target.value) return;\n onCellEdited(rowIndex, col.key, e.target.value);\n };\n\n let cellContents = _renderedValue;\n let cellClass = null;\n\n if (typeof inlineEditor == \"boolean\") {\n let _value = value;\n if (\n typeof _renderedValue === \"string\" ||\n typeof _renderedValue === \"number\" ||\n _renderedValue == null\n ) {\n _value = _renderedValue;\n }\n inlineEditor = h(\"input\", {\n value: _value ?? \"\",\n autoFocus: autoFocusEditor,\n onChange,\n onKeyDown(e) {\n if (e.key == \"Enter\") {\n e.target.blur();\n }\n\n if (e.key == \"Escape\") {\n e.target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(e, e.target);\n if (!shouldPropagate) {\n e.stopPropagation();\n } else {\n e.target.blur();\n if (e.key !== \"Escape\") {\n e.target.parentNode.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n }\n },\n });\n }\n\n if (col.dataEditor != null) {\n cellClass = \"editor-cell\";\n cellContents = h([\n h(EditorPopup, {\n autoFocus: autoFocusEditor,\n content: h(col.dataEditor, {\n value,\n onChange(value) {\n if (!editable) return;\n onCellEdited(rowIndex, col.key, value);\n },\n }),\n inlineEditor,\n valueViewer: _renderedValue,\n }),\n ]);\n } else if (inlineEditor != null) {\n cellContents = inlineEditor;\n cellClass = \"input-cell\";\n }\n\n // Hidden html input\n return h(\n _Cell,\n {\n intent,\n value,\n className: cellClass,\n style,\n //truncated: false,\n },\n [cellContents, h.if(editable)(DragHandle, { focusedCell })]\n );\n}\n\nfunction DragHandle({ focusedCell }) {\n // TODO: we might want to drag multiple columns in some cases\n // This should be on the last cell of a selection\n const onDragValue = useSelector((state) => state.onDragValue);\n\n return h(\"div.corner-drag-handle\", {\n onMouseDown(e) {\n onDragValue(focusedCell);\n e.preventDefault();\n },\n });\n}\n\nfunction DataSheetEditToolbar({\n hasUpdates,\n setUpdatedData,\n onSaveData,\n onAddRow,\n onDeleteRows,\n}) {\n return h(\"div.data-sheet-toolbar\", [\n h(ButtonGroup, { minimal: true }, [\n h.if(onAddRow != null)(AddRowButton, { onAddRow }),\n h(\n Button,\n {\n intent: Intent.DANGER,\n disabled: onDeleteRows == null,\n onClick() {\n onDeleteRows?.();\n },\n },\n \"Delete\"\n ),\n ]),\n h(\"div.spacer\"),\n h(ButtonGroup, [\n h(\n Button,\n {\n intent: Intent.WARNING,\n disabled: !hasUpdates,\n onClick() {\n setUpdatedData([]);\n },\n },\n \"Reset\"\n ),\n h(\n Button,\n {\n intent: Intent.SUCCESS,\n icon: \"floppy-disk\",\n disabled: !hasUpdates,\n onClick: onSaveData,\n },\n \"Save\"\n ),\n ]),\n ]);\n}\n\nexport function ScrollToRowControl() {\n const [value, setValue] = useState(\"\");\n const scrollToRow = useSelector((state) => state.scrollToRow);\n\n return h(DataSheetAction, [\n h(InputGroup, {\n type: \"number\",\n placeholder: \"Row number\",\n value,\n onValueChange(value) {\n setValue(value);\n },\n }),\n h(\n Button,\n {\n icon: \"arrow-right\",\n onClick() {\n const row = parseInt(value);\n scrollToRow(row - 1);\n },\n },\n \"Scroll to row\"\n ),\n ]);\n}\n\nfunction AddRowButton({ onAddRow }) {\n return h(\n Button,\n {\n icon: \"plus\",\n onClick: onAddRow,\n },\n \"Add row\"\n );\n}\n\nexport function BaseCell({ children, value, ...rest }) {\n return h(\n Cell,\n {\n interactive: true,\n ...rest,\n },\n children\n );\n}\n\nexport { Cell };\n\nexport function getRowsToDelete(selection) {\n let rowIndices: number[] = [];\n for (const sel of selection) {\n // This isn't a full-row selection\n if (sel.cols != null) continue;\n if (sel.rows == null) continue;\n const [startIndex, endIndex] = sel.rows;\n for (let i = startIndex; i <= endIndex; i++) {\n rowIndices.push(i);\n }\n }\n return rowIndices;\n}\n"],"names":[],"version":3,"file":"core.d8d80480.js.map"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {useAsyncEffect as $enKHI$useAsyncEffect} from "@macrostrat/ui-components";
|
|
2
|
+
import {debounce as $enKHI$debounce} from "underscore";
|
|
3
|
+
import {useMemo as $enKHI$useMemo, useReducer as $enKHI$useReducer, useCallback as $enKHI$useCallback} from "react";
|
|
4
|
+
import $enKHI$immutabilityhelper from "immutability-helper";
|
|
5
|
+
import {PostgrestClient as $enKHI$PostgrestClient} from "@supabase/postgrest-js";
|
|
6
|
+
|
|
7
|
+
/** Lazy loading of data from a PostgREST endpoint */
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
function $4c00def725e3d600$var$adjustArraySize(arr, newSize) {
|
|
13
|
+
if (newSize == null || arr.length === newSize) return arr;
|
|
14
|
+
else if (arr.length > newSize) // Trim the array
|
|
15
|
+
arr = arr.slice(0, newSize);
|
|
16
|
+
return [
|
|
17
|
+
...arr,
|
|
18
|
+
...Array(newSize - arr.length).fill(null)
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
function $4c00def725e3d600$var$lazyLoadingReducer(state, action) {
|
|
22
|
+
console.log(action);
|
|
23
|
+
switch(action.type){
|
|
24
|
+
case "start-loading":
|
|
25
|
+
return {
|
|
26
|
+
...state,
|
|
27
|
+
loading: true,
|
|
28
|
+
initialized: true
|
|
29
|
+
};
|
|
30
|
+
case "set-visible":
|
|
31
|
+
return {
|
|
32
|
+
...state,
|
|
33
|
+
visibleRegion: action.region
|
|
34
|
+
};
|
|
35
|
+
case "update-data":
|
|
36
|
+
return {
|
|
37
|
+
...state,
|
|
38
|
+
loading: false,
|
|
39
|
+
data: (0, $enKHI$immutabilityhelper)(state.data, action.changes)
|
|
40
|
+
};
|
|
41
|
+
case "loaded":
|
|
42
|
+
let data = $4c00def725e3d600$var$adjustArraySize(state.data, action.totalSize);
|
|
43
|
+
data = [
|
|
44
|
+
...data.slice(0, action.offset),
|
|
45
|
+
...action.data,
|
|
46
|
+
...data.slice(action.offset + action.data.length)
|
|
47
|
+
];
|
|
48
|
+
return {
|
|
49
|
+
...state,
|
|
50
|
+
data: data,
|
|
51
|
+
loading: false
|
|
52
|
+
};
|
|
53
|
+
case "error":
|
|
54
|
+
return {
|
|
55
|
+
...state,
|
|
56
|
+
error: action.error,
|
|
57
|
+
loading: false
|
|
58
|
+
};
|
|
59
|
+
default:
|
|
60
|
+
return state;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
var $4c00def725e3d600$var$LoadDirection = /*#__PURE__*/ function(LoadDirection) {
|
|
64
|
+
LoadDirection[LoadDirection["up"] = 0] = "up";
|
|
65
|
+
LoadDirection[LoadDirection["down"] = 1] = "down";
|
|
66
|
+
return LoadDirection;
|
|
67
|
+
}($4c00def725e3d600$var$LoadDirection || {});
|
|
68
|
+
function $4c00def725e3d600$var$overlapsNulls(data, region) {
|
|
69
|
+
for(let i = region.rowIndexStart; i < region.rowIndexEnd; i++){
|
|
70
|
+
if (data[i] == null) return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
function $4c00def725e3d600$var$distanceToNextNonEmptyRow(data, start, direction, limit) {
|
|
75
|
+
let i = start;
|
|
76
|
+
while(i < data.length && i > 0 && limit > 0){
|
|
77
|
+
if (data[i] != null) return i;
|
|
78
|
+
i += direction === 1 ? 1 : -1;
|
|
79
|
+
limit -= 1;
|
|
80
|
+
}
|
|
81
|
+
return i;
|
|
82
|
+
}
|
|
83
|
+
function $4c00def725e3d600$var$buildQuery(client, config) {
|
|
84
|
+
const { columns: columns = "*", count: count } = config;
|
|
85
|
+
const opts = {
|
|
86
|
+
count: count
|
|
87
|
+
};
|
|
88
|
+
let query = client.select(columns, opts);
|
|
89
|
+
if (config.order != null) {
|
|
90
|
+
query = query.order(config.order.key, {
|
|
91
|
+
ascending: config.order.ascending
|
|
92
|
+
});
|
|
93
|
+
if (config.after != null) {
|
|
94
|
+
const op = config.order.ascending ? "gt" : "lt";
|
|
95
|
+
query = query[op](config.order.key, config.after);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (config.limit != null) {
|
|
99
|
+
if (config.offset != null) {
|
|
100
|
+
query = query.range(config.offset, config.offset + config.limit - 1);
|
|
101
|
+
console.log(`Random seek from ${config.offset}, this will be slow`);
|
|
102
|
+
} else query = query.limit(config.limit);
|
|
103
|
+
}
|
|
104
|
+
return query;
|
|
105
|
+
}
|
|
106
|
+
function $4c00def725e3d600$var$_loadMoreData(client, config, state, dispatch) {
|
|
107
|
+
const rowIndex = $4c00def725e3d600$var$indexOfFirstNullInRegion(state.data, state.visibleRegion);
|
|
108
|
+
if (state.loading || rowIndex == null) {
|
|
109
|
+
if (state.initialized) return;
|
|
110
|
+
}
|
|
111
|
+
const { chunkSize: chunkSize = 100, ...rest } = config;
|
|
112
|
+
const sortKey = config.order?.key ?? "id";
|
|
113
|
+
let cfg = {
|
|
114
|
+
...rest,
|
|
115
|
+
limit: chunkSize,
|
|
116
|
+
offset: null
|
|
117
|
+
};
|
|
118
|
+
// Allows random seeking
|
|
119
|
+
const isInitialQuery = !state.initialized;
|
|
120
|
+
if (isInitialQuery) cfg.count = "exact";
|
|
121
|
+
// This only works for forward queries
|
|
122
|
+
if (!isInitialQuery) {
|
|
123
|
+
cfg.after = state.data[rowIndex - 1]?.[sortKey];
|
|
124
|
+
if (cfg.after == null) cfg.offset = rowIndex;
|
|
125
|
+
}
|
|
126
|
+
dispatch({
|
|
127
|
+
type: "start-loading"
|
|
128
|
+
});
|
|
129
|
+
const query = $4c00def725e3d600$var$buildQuery(client, cfg);
|
|
130
|
+
query.then((res)=>{
|
|
131
|
+
const { data: data, count: count } = res;
|
|
132
|
+
dispatch({
|
|
133
|
+
type: "loaded",
|
|
134
|
+
data: data,
|
|
135
|
+
offset: rowIndex,
|
|
136
|
+
totalSize: count
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// Ensure only one data load is in progress at a time
|
|
141
|
+
const $4c00def725e3d600$var$loadMoreData = (0, $enKHI$debounce)($4c00def725e3d600$var$_loadMoreData, 100);
|
|
142
|
+
function $4c00def725e3d600$export$4dffb9bb65ef8b2(endpoint, table, config = {}) {
|
|
143
|
+
const initialState = {
|
|
144
|
+
data: [],
|
|
145
|
+
loading: false,
|
|
146
|
+
error: null,
|
|
147
|
+
visibleRegion: {
|
|
148
|
+
rowIndexStart: 0,
|
|
149
|
+
rowIndexEnd: 0
|
|
150
|
+
},
|
|
151
|
+
initialized: false
|
|
152
|
+
};
|
|
153
|
+
const client = (0, $enKHI$useMemo)(()=>{
|
|
154
|
+
return new (0, $enKHI$PostgrestClient)(endpoint).from(table);
|
|
155
|
+
}, [
|
|
156
|
+
endpoint,
|
|
157
|
+
table
|
|
158
|
+
]);
|
|
159
|
+
const [state, dispatch] = (0, $enKHI$useReducer)($4c00def725e3d600$var$lazyLoadingReducer, initialState);
|
|
160
|
+
const { data: data, loading: loading } = state;
|
|
161
|
+
(0, $enKHI$useAsyncEffect)(async ()=>{
|
|
162
|
+
$4c00def725e3d600$var$loadMoreData(client, config, state, dispatch);
|
|
163
|
+
}, [
|
|
164
|
+
data,
|
|
165
|
+
state.visibleRegion.rowIndexStart,
|
|
166
|
+
state.visibleRegion.rowIndexEnd
|
|
167
|
+
]);
|
|
168
|
+
const onScroll = (0, $enKHI$useCallback)((0, $enKHI$debounce)((visibleCells)=>{
|
|
169
|
+
dispatch({
|
|
170
|
+
type: "set-visible",
|
|
171
|
+
region: visibleCells
|
|
172
|
+
});
|
|
173
|
+
}, 500), [
|
|
174
|
+
dispatch
|
|
175
|
+
]);
|
|
176
|
+
return {
|
|
177
|
+
data: data,
|
|
178
|
+
loading: loading,
|
|
179
|
+
onScroll: onScroll,
|
|
180
|
+
dispatch: dispatch,
|
|
181
|
+
client: client
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function $4c00def725e3d600$var$getRowIndexToLoadFrom(data, visibleRegion, chunkSize) {
|
|
185
|
+
return $4c00def725e3d600$var$indexOfFirstNullInRegion(data, visibleRegion);
|
|
186
|
+
}
|
|
187
|
+
function $4c00def725e3d600$var$indexOfFirstNullInRegion(data, region) {
|
|
188
|
+
for(let i = region.rowIndexStart; i < region.rowIndexEnd; i++){
|
|
189
|
+
if (data[i] == null) return i;
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
export {$4c00def725e3d600$export$4dffb9bb65ef8b2 as usePostgRESTLazyLoader};
|
|
196
|
+
//# sourceMappingURL=data-loaders.5302dedd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;AAAA,mDAAmD;;;;;AA8BnD,SAAS,sCAAmB,GAAQ,EAAE,OAAe;IACnD,IAAI,WAAW,QAAQ,IAAI,MAAM,KAAK,SACpC,OAAO;SACF,IAAI,IAAI,MAAM,GAAG,SACtB,iBAAiB;IACjB,MAAM,IAAI,KAAK,CAAC,GAAG;IAErB,OAAO;WAAI;WAAQ,MAAM,UAAU,IAAI,MAAM,EAAE,IAAI,CAAC;KAAM;AAC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EjB,OAAO,+CAAyB,MAAM;AACxC;AAEA,SAAS,+CACP,IAAW,EACX,MAAiB;IAEjB,IAAK,IAAI,IAAI,OAAO,aAAa,EAAE,IAAI,OAAO,WAAW,EAAE,IAAK;QAC9D,IAAI,IAAI,CAAC,EAAE,IAAI,MACb,OAAO;IAEX;IACA,OAAO;AACT","sources":["packages/data-sheet/src/postgrest-table/data-loaders.ts"],"sourcesContent":["/** Lazy loading of data from a PostgREST endpoint */\n\nimport { useAsyncEffect } from \"@macrostrat/ui-components\";\nimport { debounce } from \"underscore\";\nimport { useCallback, useMemo, useReducer } from \"react\";\nimport update, { Spec } from \"immutability-helper\";\n\ninterface ChunkIndex {\n startRow: number;\n endRow: number;\n lastValue: any;\n}\n\nimport { PostgrestClient, PostgrestQueryBuilder } from \"@supabase/postgrest-js\";\n\ninterface LazyLoaderState<T> {\n data: (T | null)[];\n loading: boolean;\n error: Error | null;\n visibleRegion: RowRegion;\n initialized: boolean;\n}\n\ntype LazyLoaderAction<T> =\n | { type: \"start-loading\" }\n | { type: \"loaded\"; data: T[]; offset: number; totalSize: number }\n | { type: \"error\"; error: Error }\n | { type: \"set-visible\"; region: RowRegion }\n | { type: \"update-data\"; changes: Spec<T[]> };\n\nfunction adjustArraySize<T>(arr: T[], newSize: number) {\n if (newSize == null || arr.length === newSize) {\n return arr;\n } else if (arr.length > newSize) {\n // Trim the array\n arr = arr.slice(0, newSize);\n }\n return [...arr, ...Array(newSize - arr.length).fill(null)];\n}\n\nfunction lazyLoadingReducer<T>(\n state: LazyLoaderState<T>,\n action: LazyLoaderAction<T>\n): LazyLoaderState<T> {\n console.log(action);\n switch (action.type) {\n case \"start-loading\":\n return {\n ...state,\n loading: true,\n initialized: true,\n };\n case \"set-visible\":\n return {\n ...state,\n visibleRegion: action.region,\n };\n case \"update-data\":\n return {\n ...state,\n loading: false,\n data: update(state.data, action.changes),\n };\n case \"loaded\":\n let data = adjustArraySize(state.data, action.totalSize);\n data = [\n ...data.slice(0, action.offset),\n ...action.data,\n ...data.slice(action.offset + action.data.length),\n ];\n\n return {\n ...state,\n data,\n loading: false,\n };\n case \"error\":\n return {\n ...state,\n error: action.error,\n loading: false,\n };\n default:\n return state;\n }\n}\n\ninterface RowRegion {\n rowIndexStart: number;\n rowIndexEnd: number;\n}\n\nenum LoadDirection {\n \"up\",\n \"down\",\n}\n\nfunction overlapsNulls(data: any[], region: RowRegion) {\n for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {\n if (data[i] == null) {\n return true;\n }\n }\n return false;\n}\n\nfunction distanceToNextNonEmptyRow(\n data: any[],\n start: number,\n direction: LoadDirection,\n limit: number\n): number {\n let i = start;\n while (i < data.length && i > 0 && limit > 0) {\n if (data[i] != null) {\n return i;\n }\n i += direction === LoadDirection.down ? 1 : -1;\n limit -= 1;\n }\n return i;\n}\n\ninterface QueryConfig {\n columns?: string;\n count?: \"exact\" | \"estimated\";\n limit?: number;\n offset?: number;\n order?: { key: string; ascending: boolean };\n after?: any;\n}\n\nfunction buildQuery<T>(\n client: PostgrestQueryBuilder<T, any>,\n config: QueryConfig\n) {\n const { columns = \"*\", count } = config;\n const opts = { count };\n\n let query = client.select(columns, opts);\n\n if (config.order != null) {\n query = query.order(config.order.key, {\n ascending: config.order.ascending,\n });\n if (config.after != null) {\n const op = config.order.ascending ? \"gt\" : \"lt\";\n query = query[op](config.order.key, config.after);\n }\n }\n if (config.limit != null) {\n if (config.offset != null) {\n query = query.range(config.offset, config.offset + config.limit - 1);\n console.log(`Random seek from ${config.offset}, this will be slow`);\n } else {\n query = query.limit(config.limit);\n }\n }\n return query;\n}\n\nfunction _loadMoreData<T>(\n client: PostgrestQueryBuilder<T, any>,\n config: QueryConfig & { chunkSize: number },\n state: LazyLoaderState<T>,\n dispatch: any\n) {\n const rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);\n if (state.loading || rowIndex == null) {\n if (state.initialized) {\n return;\n }\n }\n\n const { chunkSize = 100, ...rest } = config;\n\n const sortKey = config.order?.key ?? \"id\";\n\n let cfg: QueryConfig = {\n ...rest,\n limit: chunkSize,\n offset: null,\n };\n\n // Allows random seeking\n const isInitialQuery = !state.initialized;\n if (isInitialQuery) {\n cfg.count = \"exact\";\n }\n\n // This only works for forward queries\n if (!isInitialQuery) {\n cfg.after = state.data[rowIndex - 1]?.[sortKey];\n if (cfg.after == null) {\n cfg.offset = rowIndex;\n }\n }\n\n dispatch({ type: \"start-loading\" });\n\n const query = buildQuery(client, cfg);\n\n query.then((res) => {\n const { data, count } = res;\n dispatch({\n type: \"loaded\",\n data,\n offset: rowIndex,\n totalSize: count,\n });\n });\n}\n\n// Ensure only one data load is in progress at a time\nconst loadMoreData = debounce(_loadMoreData, 100);\n\ntype LazyLoaderOptions = Omit<QueryConfig, \"count\" | \"offset\" | \"limit\"> & {\n chunkSize?: number;\n sortKey?: string;\n};\n\nexport function usePostgRESTLazyLoader(\n endpoint: string,\n table: string,\n config: LazyLoaderOptions = {}\n) {\n const initialState: LazyLoaderState<any> = {\n data: [],\n loading: false,\n error: null,\n visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },\n initialized: false,\n };\n\n const client = useMemo(() => {\n return new PostgrestClient(endpoint).from(table);\n }, [endpoint, table]);\n\n const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);\n const { data, loading } = state;\n\n useAsyncEffect(async () => {\n loadMoreData(client, config, state, dispatch);\n }, [\n data,\n state.visibleRegion.rowIndexStart,\n state.visibleRegion.rowIndexEnd,\n ]);\n\n const onScroll = useCallback(\n debounce((visibleCells: RowRegion) => {\n dispatch({\n type: \"set-visible\",\n region: visibleCells,\n });\n }, 500),\n [dispatch]\n );\n\n return {\n data,\n loading,\n onScroll,\n dispatch,\n client,\n };\n}\n\nfunction getRowIndexToLoadFrom<T>(\n data: (T | null)[],\n visibleRegion: RowRegion,\n chunkSize: number\n) {\n return indexOfFirstNullInRegion(data, visibleRegion);\n}\n\nfunction indexOfFirstNullInRegion(\n data: any[],\n region: RowRegion\n): number | null {\n for (let i = region.rowIndexStart; i < region.rowIndexEnd; i++) {\n if (data[i] == null) {\n return i;\n }\n }\n return null;\n}\n"],"names":[],"version":3,"file":"data-loaders.5302dedd.js.map"}
|