@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
package/src/provider.ts
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useRef, useState } from "react";
|
|
2
|
+
import h from "@macrostrat/hyper";
|
|
3
|
+
import { createStore, StoreApi, useStore } from "zustand";
|
|
4
|
+
import type {
|
|
5
|
+
FocusedCellCoordinates,
|
|
6
|
+
Region,
|
|
7
|
+
Table2,
|
|
8
|
+
} from "@blueprintjs/table";
|
|
9
|
+
import { generateColumnSpec } from "./utils";
|
|
10
|
+
import update, { Spec } from "immutability-helper";
|
|
11
|
+
import { range } from "./utils";
|
|
12
|
+
import React from "react";
|
|
13
|
+
|
|
14
|
+
export interface ColumnSpec {
|
|
15
|
+
name: string;
|
|
16
|
+
key: string;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
isValid?: (d: any) => boolean;
|
|
19
|
+
transformValue?: (d: any) => any;
|
|
20
|
+
valueRenderer?: (d: any) => string | React.ReactNode;
|
|
21
|
+
dataEditor?: any;
|
|
22
|
+
cellComponent?: any;
|
|
23
|
+
category?: string;
|
|
24
|
+
editable?: boolean;
|
|
25
|
+
inlineEditor?: boolean | React.ComponentType<any> | string | null;
|
|
26
|
+
style?: React.CSSProperties;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ColumnSpecOptions {
|
|
30
|
+
overrides: Record<string, Partial<ColumnSpec> | string>;
|
|
31
|
+
data?: any[]; // Data to use for type inference
|
|
32
|
+
nRows?: number; // Number of rows to use for type inference
|
|
33
|
+
omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.
|
|
34
|
+
includeColumns?: string[]; // Columns to include.
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DataSheetCoreProps<T> {
|
|
38
|
+
data: T[];
|
|
39
|
+
columnSpec?: ColumnSpec[];
|
|
40
|
+
editable?: boolean;
|
|
41
|
+
enableColumnReordering: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface DataSheetState<T> {
|
|
45
|
+
selection: Region[];
|
|
46
|
+
columnSpec: ColumnSpec[];
|
|
47
|
+
fillValueBaseCell: FocusedCellCoordinates | null;
|
|
48
|
+
focusedCell: FocusedCellCoordinates | null;
|
|
49
|
+
topLeftCell: FocusedCellCoordinates | null;
|
|
50
|
+
updatedData: T[];
|
|
51
|
+
initialized: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type DataSheetVals<T> = DataSheetState<T> & DataSheetCoreProps<T>;
|
|
55
|
+
|
|
56
|
+
export interface DataSheetStore<T> extends DataSheetVals<T> {
|
|
57
|
+
setSelection(selection: Region[]): void;
|
|
58
|
+
onDragValue(cell: FocusedCellCoordinates | null): void;
|
|
59
|
+
setUpdatedData(data: T[]): void;
|
|
60
|
+
onCellEdited(rowIndex: number, columnName: string, value: any): void;
|
|
61
|
+
clearSelection(): void;
|
|
62
|
+
initialize(props: DataSheetCoreProps<T>): void;
|
|
63
|
+
onSelection(selection: Region[]): void;
|
|
64
|
+
// Internal method used for infinite scrolling
|
|
65
|
+
setVisibleCells(visibleCells: VisibleCells): void;
|
|
66
|
+
scrollToRow(rowIndex: number): void;
|
|
67
|
+
tableRef: React.MutableRefObject<Table2>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type DataSheetProviderProps<T> = DataSheetCoreProps<T> & {
|
|
71
|
+
children: React.ReactNode;
|
|
72
|
+
columnSpecOptions?: ColumnSpecOptions;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const DataSheetContext = createContext<StoreApi<DataSheetStore<any>>>(null);
|
|
76
|
+
|
|
77
|
+
export interface VisibleCells {
|
|
78
|
+
rowIndexStart: number;
|
|
79
|
+
rowIndexEnd: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function DataSheetProvider<T>({
|
|
83
|
+
children,
|
|
84
|
+
data,
|
|
85
|
+
columnSpec,
|
|
86
|
+
columnSpecOptions,
|
|
87
|
+
editable,
|
|
88
|
+
enableColumnReordering,
|
|
89
|
+
}: DataSheetProviderProps<T>) {
|
|
90
|
+
const visibleCellsRef = useRef<VisibleCells>({
|
|
91
|
+
rowIndexStart: 0,
|
|
92
|
+
rowIndexEnd: 0,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const tableRef = useRef<Table2>(null);
|
|
96
|
+
|
|
97
|
+
const [store] = useState(() => {
|
|
98
|
+
return createStore<DataSheetStore<T>>((set) => {
|
|
99
|
+
return {
|
|
100
|
+
data,
|
|
101
|
+
columnSpec: columnSpec ?? generateColumnSpec(data, columnSpecOptions),
|
|
102
|
+
editable,
|
|
103
|
+
selection: [],
|
|
104
|
+
fillValueBaseCell: null,
|
|
105
|
+
updatedData: [],
|
|
106
|
+
focusedCell: null,
|
|
107
|
+
topLeftCell: null,
|
|
108
|
+
initialized: false,
|
|
109
|
+
tableRef,
|
|
110
|
+
setSelection(selection: Region[]) {
|
|
111
|
+
set(updateSelection(selection));
|
|
112
|
+
},
|
|
113
|
+
setUpdatedData(data: T[]) {
|
|
114
|
+
set({ updatedData: data });
|
|
115
|
+
},
|
|
116
|
+
setVisibleCells(visibleCells: VisibleCells) {
|
|
117
|
+
// Visible cells are used for infinite scrolling
|
|
118
|
+
// Right now we don't store this in the state
|
|
119
|
+
visibleCellsRef.current = visibleCells;
|
|
120
|
+
},
|
|
121
|
+
onDragValue(cell: FocusedCellCoordinates | null) {
|
|
122
|
+
set({ fillValueBaseCell: cell });
|
|
123
|
+
},
|
|
124
|
+
onCellEdited(rowIndex: number, columnName: string, value: any) {
|
|
125
|
+
set((state) => {
|
|
126
|
+
const { editable, updatedData, data } = state;
|
|
127
|
+
if (!editable) return {};
|
|
128
|
+
let rowSpec: any;
|
|
129
|
+
// Check to see if the new value is the same as the old one
|
|
130
|
+
if (value !== data[rowIndex]?.[columnName]) {
|
|
131
|
+
const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
|
|
132
|
+
rowSpec = { [rowOp]: { [columnName]: value } };
|
|
133
|
+
} else {
|
|
134
|
+
rowSpec = { $unset: [columnName] };
|
|
135
|
+
}
|
|
136
|
+
const spec: Spec<T[]> = { [rowIndex]: rowSpec };
|
|
137
|
+
return { updatedData: update(updatedData, spec) };
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
initialize(props: DataSheetCoreProps<T>) {
|
|
141
|
+
set({ ...props, initialized: true });
|
|
142
|
+
},
|
|
143
|
+
clearSelection() {
|
|
144
|
+
set((state) => {
|
|
145
|
+
// Delete all selected cells
|
|
146
|
+
const { selection, updatedData, columnSpec } = state;
|
|
147
|
+
let spec = {};
|
|
148
|
+
for (const region of selection) {
|
|
149
|
+
const { cols, rows } = region;
|
|
150
|
+
for (const row of range(rows)) {
|
|
151
|
+
let vals = {};
|
|
152
|
+
for (const col of range(cols)) {
|
|
153
|
+
const key = columnSpec[col].key;
|
|
154
|
+
vals[key] = "";
|
|
155
|
+
}
|
|
156
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
157
|
+
spec[row] = { [op]: vals };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
updatedData: update(updatedData, spec),
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
onSelection(selection: Region[]) {
|
|
166
|
+
set((state) => {
|
|
167
|
+
let spec = updateSelection(selection);
|
|
168
|
+
if (state.fillValueBaseCell != null) {
|
|
169
|
+
spec.updatedData = fillValues(state, selection);
|
|
170
|
+
}
|
|
171
|
+
return spec;
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
onColumnsReordered(oldIndex: number, newIndex: number, length: number) {
|
|
175
|
+
set((state) => {
|
|
176
|
+
if (!state.enableColumnReordering) return {};
|
|
177
|
+
const { columnSpec } = state;
|
|
178
|
+
const newSpec = [...columnSpec];
|
|
179
|
+
const removed = newSpec.splice(oldIndex, length);
|
|
180
|
+
newSpec.splice(newIndex, 0, ...removed);
|
|
181
|
+
return { columnSpec: newSpec };
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
scrollToRow(rowIndex: number, columnIndex: number) {
|
|
185
|
+
if (tableRef.current == null) return;
|
|
186
|
+
tableRef.current.scrollToRegion({
|
|
187
|
+
rows: [rowIndex, rowIndex],
|
|
188
|
+
});
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Not sure how required this initialization is
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
const { initialize } = store.getState();
|
|
197
|
+
initialize({
|
|
198
|
+
data,
|
|
199
|
+
columnSpec: columnSpec ?? generateColumnSpec(data, columnSpecOptions),
|
|
200
|
+
editable,
|
|
201
|
+
enableColumnReordering,
|
|
202
|
+
});
|
|
203
|
+
}, [data, editable, columnSpec, columnSpecOptions, enableColumnReordering]);
|
|
204
|
+
|
|
205
|
+
return h(DataSheetContext.Provider, { value: store }, children);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function useStoreAPI<T>(): StoreApi<DataSheetStore<T>> {
|
|
209
|
+
const store = useContext(DataSheetContext);
|
|
210
|
+
if (!store) {
|
|
211
|
+
throw new Error("Missing DataSheetProvider");
|
|
212
|
+
}
|
|
213
|
+
return store;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function useSelector<T = any, A = any>(
|
|
217
|
+
selector: (state: DataSheetStore<T>) => A
|
|
218
|
+
): A {
|
|
219
|
+
const store = useStoreAPI<T>();
|
|
220
|
+
return useStore(store, selector);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function updateSelection(selection: Region[]) {
|
|
224
|
+
const focusedCell = singleFocusedCell(selection);
|
|
225
|
+
let spec: Partial<DataSheetState<T>> = {
|
|
226
|
+
selection,
|
|
227
|
+
focusedCell,
|
|
228
|
+
topLeftCell: topLeftCell(selection),
|
|
229
|
+
};
|
|
230
|
+
if (focusedCell != null) {
|
|
231
|
+
spec.fillValueBaseCell = null;
|
|
232
|
+
}
|
|
233
|
+
return spec;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function topLeftCell(
|
|
237
|
+
regions: Region[],
|
|
238
|
+
requireSolitaryCell: boolean = false
|
|
239
|
+
): FocusedCellCoordinates | null {
|
|
240
|
+
/** Top left cell of a ranged selection */
|
|
241
|
+
if (regions == null) return null;
|
|
242
|
+
const [region] = regions;
|
|
243
|
+
if (region == null) return null;
|
|
244
|
+
const { cols, rows } = region;
|
|
245
|
+
if (cols == null || rows == null) return null;
|
|
246
|
+
if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
|
|
247
|
+
return null;
|
|
248
|
+
return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function singleFocusedCell(sel: Region[]): FocusedCellCoordinates | null {
|
|
252
|
+
/** Derive a single focused cell from a selected region, if possible */
|
|
253
|
+
if (sel?.length !== 1) return null;
|
|
254
|
+
return topLeftCell(sel, true);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {
|
|
258
|
+
const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
|
|
259
|
+
|
|
260
|
+
// Prepare regions by unnesting columns
|
|
261
|
+
let regions = selection.map((region) => {
|
|
262
|
+
const { cols, rows } = region;
|
|
263
|
+
// Get the first column (maybe should be the last)
|
|
264
|
+
const [col] = cols;
|
|
265
|
+
return { cols: [col, col], rows };
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Fill values downwards
|
|
269
|
+
if (!editable || fillValueBaseCell == null) return updatedData;
|
|
270
|
+
const { col, row } = fillValueBaseCell;
|
|
271
|
+
const key = columnSpec[col].key;
|
|
272
|
+
const value = updatedData[row]?.[key] ?? data[row][key];
|
|
273
|
+
const spec = {};
|
|
274
|
+
for (const region of regions) {
|
|
275
|
+
const { cols, rows } = region;
|
|
276
|
+
for (const row of range(rows)) {
|
|
277
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
278
|
+
spec[row] = { [op]: { [key]: value } };
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return update(updatedData, spec);
|
|
282
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const defaultRenderers = {
|
|
2
|
+
string: (d) => d,
|
|
3
|
+
number: (d) => d?.toFixed(2),
|
|
4
|
+
boolean: (d) => (d ? "T" : "F"),
|
|
5
|
+
object: (d) => JSON.stringify(d),
|
|
6
|
+
integer: (d) => d?.toFixed(0),
|
|
7
|
+
array: (d) => d?.join(", "),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function generateDefaultColumnSpec<T>(data: Array<T>): ColumnSpec[] {
|
|
11
|
+
/** Build a default column spec from a dataset based on the first n rows */
|
|
12
|
+
if (data == null) return [];
|
|
13
|
+
// Get the keys
|
|
14
|
+
const keys = new Set();
|
|
15
|
+
const types = new Map();
|
|
16
|
+
for (const row of data) {
|
|
17
|
+
if (row == null) continue;
|
|
18
|
+
for (const key of Object.keys(row)) {
|
|
19
|
+
const val = row[key];
|
|
20
|
+
keys.add(key);
|
|
21
|
+
|
|
22
|
+
if (val == null) continue;
|
|
23
|
+
let type: string = typeof val;
|
|
24
|
+
// Special 'type' for integers
|
|
25
|
+
if (Number.isInteger(val)) {
|
|
26
|
+
type = "integer";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Special 'type' for arrays of simple values
|
|
30
|
+
if (
|
|
31
|
+
Array.isArray(val) &&
|
|
32
|
+
val.every((d) => typeof d === "string" || typeof d === "number")
|
|
33
|
+
) {
|
|
34
|
+
type = "array";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (types.has(key)) {
|
|
38
|
+
if (types.get(key) !== type) {
|
|
39
|
+
if (type === "number" && types.get(key) === "integer") {
|
|
40
|
+
types.set(key, "number");
|
|
41
|
+
}
|
|
42
|
+
if (type === "object" && types.get(key) === "array") {
|
|
43
|
+
types.set(key, "object");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
types.set(key, "string");
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
types.set(key, type);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Build a column spec
|
|
55
|
+
const spec = [];
|
|
56
|
+
for (const key of keys) {
|
|
57
|
+
spec.push({
|
|
58
|
+
name: key,
|
|
59
|
+
key,
|
|
60
|
+
valueRenderer: defaultRenderers[types.get(key)],
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return spec;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function generateColumnSpec<T>(
|
|
67
|
+
data: T[],
|
|
68
|
+
options: ColumnSpecOptions
|
|
69
|
+
): ColumnSpec[] {
|
|
70
|
+
/** Generate a column spec from a dataset */
|
|
71
|
+
const { overrides = {}, nRows = 10, omitColumns, includeColumns } = options;
|
|
72
|
+
|
|
73
|
+
if (data == null) return [];
|
|
74
|
+
|
|
75
|
+
const _nRows = Math.min(nRows, data.length);
|
|
76
|
+
|
|
77
|
+
let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));
|
|
78
|
+
let filteredSpec = columnSpec.filter((col) => {
|
|
79
|
+
if (omitColumns != null && omitColumns.includes(col.key)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (includeColumns != null && !includeColumns.includes(col.key)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Apply overrides
|
|
89
|
+
return filteredSpec.map((col) => {
|
|
90
|
+
let ovr = overrides[col.key];
|
|
91
|
+
if (ovr == null) return col;
|
|
92
|
+
if (typeof ovr === "string") {
|
|
93
|
+
return { ...col, name: ovr };
|
|
94
|
+
}
|
|
95
|
+
return { ...col, ...ovr };
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { generateColumnSpec } from "./column-spec";
|
|
2
|
+
|
|
3
|
+
export function range(arr: number[]) {
|
|
4
|
+
if (arr.length != 2) throw new Error("Range must have two elements");
|
|
5
|
+
const [start, end] = arr;
|
|
6
|
+
return Array.from({ length: end - start + 1 }, (_, i) => i + start);
|
|
7
|
+
}
|