@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,179 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useReducer,
|
|
4
|
+
useContext,
|
|
5
|
+
Dispatch,
|
|
6
|
+
useEffect,
|
|
7
|
+
} from "react";
|
|
8
|
+
import h, { compose, C } from "@macrostrat/hyper";
|
|
9
|
+
import { DndProvider } from "react-dnd";
|
|
10
|
+
import { HTML5Backend } from "react-dnd-html5-backend";
|
|
11
|
+
import update from "immutability-helper";
|
|
12
|
+
import ReactDataSheet from "react-datasheet";
|
|
13
|
+
import { offsetSelection } from "./virtualized";
|
|
14
|
+
import { ColumnData, apportionWidths } from "./components/column-utils";
|
|
15
|
+
|
|
16
|
+
const defaultContext = {
|
|
17
|
+
columns: [],
|
|
18
|
+
rowHeight: 20,
|
|
19
|
+
containerWidth: 500,
|
|
20
|
+
rowOffset: 0,
|
|
21
|
+
actions: null,
|
|
22
|
+
selection: null,
|
|
23
|
+
columnWidths: {},
|
|
24
|
+
dispatch: () => {},
|
|
25
|
+
virtualized: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type ColumnInfo = {
|
|
29
|
+
name: string;
|
|
30
|
+
width: number | null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type WidthSpec = {
|
|
34
|
+
[key: string]: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface DataSheetState {
|
|
38
|
+
columnWidths: WidthSpec;
|
|
39
|
+
selection: ReactDataSheet.Selection | null;
|
|
40
|
+
rowOffset: number;
|
|
41
|
+
columns: ColumnData[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface DataSheetParams {
|
|
45
|
+
rowHeight: number;
|
|
46
|
+
containerWidth: number;
|
|
47
|
+
virtualized: boolean;
|
|
48
|
+
columns: ColumnData[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type DataSheetCtx = DataSheetState &
|
|
52
|
+
DataSheetParams & {
|
|
53
|
+
dispatch: Dispatch<DataSheetAction>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
type SetColumns = {
|
|
57
|
+
type: "set-columns";
|
|
58
|
+
value: ColumnData[];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type UpdateColumnWidth = {
|
|
62
|
+
type: "update-column-width";
|
|
63
|
+
key: string;
|
|
64
|
+
value: number;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type SetSelection = {
|
|
68
|
+
type: "set-selection";
|
|
69
|
+
value: ReactDataSheet.Selection;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
type MoveColumn = {
|
|
73
|
+
type: "move-column";
|
|
74
|
+
dragIndex: number;
|
|
75
|
+
hoverIndex: number;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
type SetRowOffset = {
|
|
79
|
+
type: "set-row-offset";
|
|
80
|
+
value: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
type DataSheetProps = DataSheetParams & {
|
|
84
|
+
// This is too complicated by half...
|
|
85
|
+
desiredWidths: WidthSpec;
|
|
86
|
+
children: React.ReactNode;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
type DataSheetAction =
|
|
90
|
+
| UpdateColumnWidth
|
|
91
|
+
| SetSelection
|
|
92
|
+
| SetRowOffset
|
|
93
|
+
| SetColumns
|
|
94
|
+
| MoveColumn;
|
|
95
|
+
|
|
96
|
+
const DataSheetContext = createContext<DataSheetCtx>(defaultContext);
|
|
97
|
+
|
|
98
|
+
function dataSheetReducer(state: DataSheetState, action: DataSheetAction) {
|
|
99
|
+
switch (action.type) {
|
|
100
|
+
case "update-column-width":
|
|
101
|
+
return update(state, {
|
|
102
|
+
columnWidths: { [action.key]: { $set: action.value } },
|
|
103
|
+
});
|
|
104
|
+
case "set-columns":
|
|
105
|
+
return { ...state, columns: action.value };
|
|
106
|
+
case "move-column":
|
|
107
|
+
// Move a column in response ot a drag
|
|
108
|
+
const { dragIndex, hoverIndex } = action;
|
|
109
|
+
return update(state, {
|
|
110
|
+
columns: {
|
|
111
|
+
$splice: [
|
|
112
|
+
[dragIndex, 1], // remove column from drag index
|
|
113
|
+
[hoverIndex, 0, state.columns[dragIndex]], // ...and insert at hover index
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
case "set-selection":
|
|
118
|
+
return update(state, {
|
|
119
|
+
selection: { $set: offsetSelection(action.value, state.rowOffset) },
|
|
120
|
+
});
|
|
121
|
+
case "set-row-offset":
|
|
122
|
+
return update(state, { rowOffset: { $set: action.value } });
|
|
123
|
+
}
|
|
124
|
+
return state;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function DataSheetProviderBase(props: DataSheetProps) {
|
|
128
|
+
/** This context/context provider don't do much right now, but they will
|
|
129
|
+
take an increasing role in state management going forward */
|
|
130
|
+
const {
|
|
131
|
+
columns,
|
|
132
|
+
rowHeight,
|
|
133
|
+
children,
|
|
134
|
+
virtualized = true,
|
|
135
|
+
desiredWidths,
|
|
136
|
+
containerWidth,
|
|
137
|
+
} = props;
|
|
138
|
+
|
|
139
|
+
const [state, dispatch] = useReducer(dataSheetReducer, {
|
|
140
|
+
columnWidths: {},
|
|
141
|
+
selection: null,
|
|
142
|
+
rowOffset: 0,
|
|
143
|
+
columns,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
// If we don't have this we'll get an infinite loop
|
|
148
|
+
const col = apportionWidths(columns, desiredWidths, containerWidth);
|
|
149
|
+
dispatch({ type: "set-columns", value: col });
|
|
150
|
+
}, [containerWidth, desiredWidths, columns]);
|
|
151
|
+
|
|
152
|
+
const value = {
|
|
153
|
+
virtualized,
|
|
154
|
+
rowHeight,
|
|
155
|
+
rowOffset: state.rowOffset,
|
|
156
|
+
dispatch,
|
|
157
|
+
containerWidth,
|
|
158
|
+
...state,
|
|
159
|
+
};
|
|
160
|
+
return h(DataSheetContext.Provider, { value }, children);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
DataSheetProviderBase.defaultProps = defaultContext;
|
|
164
|
+
|
|
165
|
+
const DataSheetProvider = compose(
|
|
166
|
+
C(DndProvider, { backend: HTML5Backend }),
|
|
167
|
+
DataSheetProviderBase
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const useDataSheet = () => useContext(DataSheetContext);
|
|
171
|
+
const useDispatch = () => useDataSheet().dispatch;
|
|
172
|
+
|
|
173
|
+
export {
|
|
174
|
+
DataSheetContext,
|
|
175
|
+
DataSheetProvider,
|
|
176
|
+
ColumnInfo,
|
|
177
|
+
useDispatch,
|
|
178
|
+
useDataSheet,
|
|
179
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
.header-cell {
|
|
2
|
+
position: relative;
|
|
3
|
+
overflow: visible !important;
|
|
4
|
+
border-right: 1px solid #888;
|
|
5
|
+
}
|
|
6
|
+
.header-cell .cell-content {
|
|
7
|
+
width: 100%;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
thead {
|
|
11
|
+
position: relative;
|
|
12
|
+
.cell-drag-handle {
|
|
13
|
+
display: none;
|
|
14
|
+
}
|
|
15
|
+
&:hover .cell-drag-handle {
|
|
16
|
+
display: block;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.cell-drag-handle {
|
|
21
|
+
top: 0;
|
|
22
|
+
left: -2px;
|
|
23
|
+
bottom: 0;
|
|
24
|
+
width: 4px;
|
|
25
|
+
display: block;
|
|
26
|
+
position: absolute;
|
|
27
|
+
background: var(--border-color);
|
|
28
|
+
cursor: ew-resize;
|
|
29
|
+
z-index: 50;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.virtualized-sheet {
|
|
33
|
+
overflow-y: scroll;
|
|
34
|
+
overflow-x: hidden;
|
|
35
|
+
position: relative;
|
|
36
|
+
.ui {
|
|
37
|
+
position: sticky;
|
|
38
|
+
top: 0px;
|
|
39
|
+
bottom: 0px;
|
|
40
|
+
left: 0px;
|
|
41
|
+
right: 0px;
|
|
42
|
+
}
|
|
43
|
+
:global {
|
|
44
|
+
.data-grid-container {
|
|
45
|
+
width: 100%;
|
|
46
|
+
height: 100%;
|
|
47
|
+
}
|
|
48
|
+
.data-grid {
|
|
49
|
+
width: 100%;
|
|
50
|
+
height: 100%;
|
|
51
|
+
.cell.updated {
|
|
52
|
+
background-color: inherit;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
tr
|
|
2
|
+
height 20px
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
col
|
|
6
|
+
width: 100px
|
|
7
|
+
&.color
|
|
8
|
+
width 120px
|
|
9
|
+
&.index-column
|
|
10
|
+
width 40px
|
|
11
|
+
|
|
12
|
+
.data-grid-container .data-grid .cell
|
|
13
|
+
height 20px
|
|
14
|
+
&> input
|
|
15
|
+
margin 0
|
|
16
|
+
padding 0 4px
|
|
17
|
+
outline: none !important
|
|
18
|
+
height: 19px
|
|
19
|
+
width: calc(100% + 2px)
|
|
20
|
+
&>.value-viewer
|
|
21
|
+
padding 0 4px
|
|
22
|
+
&[data-status="invalid"]
|
|
23
|
+
box-shadow: inset 0 0 0 1px salmon;
|
|
24
|
+
|
|
25
|
+
.neatline
|
|
26
|
+
fill: none
|
|
27
|
+
stroke: black
|
|
28
|
+
stroke-width: 2px
|
|
29
|
+
|
|
30
|
+
.left-panel
|
|
31
|
+
max-width: 600px
|
|
32
|
+
|
|
33
|
+
.data-area-main
|
|
34
|
+
display: flex
|
|
35
|
+
flex-direction row
|
|
36
|
+
&>*
|
|
37
|
+
margin-right 4px
|
|
38
|
+
&:last-child
|
|
39
|
+
margin-right 0
|
|
40
|
+
|
|
41
|
+
.popover-container
|
|
42
|
+
span.popover-target, span.bp5-popover-target
|
|
43
|
+
display inline !important
|
|
44
|
+
pointer-events none
|
|
45
|
+
width 10px !important
|
|
46
|
+
height: 10px !important
|
|
47
|
+
|
|
48
|
+
.interaction-barrier
|
|
49
|
+
pointer-events: all
|
|
50
|
+
|
|
51
|
+
p.description
|
|
52
|
+
color #aaa
|
|
53
|
+
font-style: italic
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
tr {
|
|
2
|
+
height: 20px;
|
|
3
|
+
}
|
|
4
|
+
col {
|
|
5
|
+
width: 100px;
|
|
6
|
+
}
|
|
7
|
+
col.color {
|
|
8
|
+
width: 120px;
|
|
9
|
+
}
|
|
10
|
+
col.index-column {
|
|
11
|
+
width: 40px;
|
|
12
|
+
}
|
|
13
|
+
.data-grid-container .data-grid .cell {
|
|
14
|
+
height: 20px;
|
|
15
|
+
}
|
|
16
|
+
.data-grid-container .data-grid .cell > input {
|
|
17
|
+
margin: 0;
|
|
18
|
+
padding: 0 4px;
|
|
19
|
+
outline: none !important;
|
|
20
|
+
height: 19px;
|
|
21
|
+
width: calc(100% + 2px);
|
|
22
|
+
}
|
|
23
|
+
.data-grid-container .data-grid .cell > .value-viewer {
|
|
24
|
+
padding: 0 4px;
|
|
25
|
+
}
|
|
26
|
+
.data-grid-container .data-grid .cell[data-status="invalid"] {
|
|
27
|
+
box-shadow: inset 0 0 0 1px #fa8072;
|
|
28
|
+
}
|
|
29
|
+
.neatline {
|
|
30
|
+
fill: none;
|
|
31
|
+
stroke: #000;
|
|
32
|
+
stroke-width: 2px;
|
|
33
|
+
}
|
|
34
|
+
.left-panel {
|
|
35
|
+
max-width: 600px;
|
|
36
|
+
}
|
|
37
|
+
.data-area-main {
|
|
38
|
+
display: flex;
|
|
39
|
+
flex-direction: row;
|
|
40
|
+
}
|
|
41
|
+
.data-area-main > * {
|
|
42
|
+
margin-right: 4px;
|
|
43
|
+
}
|
|
44
|
+
.data-area-main > *:last-child {
|
|
45
|
+
margin-right: 0;
|
|
46
|
+
}
|
|
47
|
+
.popover-container span.popover-target,
|
|
48
|
+
.popover-container span.bp5-popover2-target {
|
|
49
|
+
display: inline !important;
|
|
50
|
+
pointer-events: none;
|
|
51
|
+
width: 10px !important;
|
|
52
|
+
height: 10px !important;
|
|
53
|
+
}
|
|
54
|
+
.interaction-barrier {
|
|
55
|
+
pointer-events: all;
|
|
56
|
+
}
|
|
57
|
+
p.description {
|
|
58
|
+
color: #aaa;
|
|
59
|
+
font-style: italic;
|
|
60
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import h from "@macrostrat/hyper";
|
|
2
|
+
import ReactDataSheet from "react-datasheet";
|
|
3
|
+
import "react-datasheet/lib/react-datasheet.css";
|
|
4
|
+
import update, { Spec } from "immutability-helper";
|
|
5
|
+
import { Button, ButtonGroup } from "@blueprintjs/core";
|
|
6
|
+
import { ErrorBoundary } from "@macrostrat/ui-components";
|
|
7
|
+
import "./attitude-sheet.scss";
|
|
8
|
+
import { ColorEditor } from "../editors";
|
|
9
|
+
|
|
10
|
+
//import classNames from "classnames";
|
|
11
|
+
|
|
12
|
+
type OrientationRow = Partial<Orientation> | null;
|
|
13
|
+
type OrientationData = OrientationRow[];
|
|
14
|
+
|
|
15
|
+
class OrientationDataSheet extends ReactDataSheet<GridElement, number> {}
|
|
16
|
+
type SheetContent = GridElement[][];
|
|
17
|
+
|
|
18
|
+
export interface Field<Key> {
|
|
19
|
+
name: string;
|
|
20
|
+
key: Key;
|
|
21
|
+
required?: boolean;
|
|
22
|
+
isValid?(k: any): boolean;
|
|
23
|
+
transform?(k: any): any;
|
|
24
|
+
dataEditor?: ReactDataSheet.DataEditor<ReactDataSheet.Cell<any, any>>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type OrientationKey =
|
|
28
|
+
| "strike"
|
|
29
|
+
| "dip"
|
|
30
|
+
| "rake"
|
|
31
|
+
| "maxError"
|
|
32
|
+
| "minError"
|
|
33
|
+
| "color";
|
|
34
|
+
|
|
35
|
+
export const orientationFields: Field<OrientationKey>[] = [
|
|
36
|
+
{ name: "Strike", key: "strike" },
|
|
37
|
+
{ name: "Dip", key: "dip" },
|
|
38
|
+
{ name: "Rake", key: "rake" },
|
|
39
|
+
{ name: "Max.", key: "maxError", category: "Errors" },
|
|
40
|
+
{ name: "Min.", key: "minError", category: "Errors" },
|
|
41
|
+
{
|
|
42
|
+
name: "Color",
|
|
43
|
+
key: "color",
|
|
44
|
+
required: false,
|
|
45
|
+
isValid: (d) => true, //getColor(d) != null,
|
|
46
|
+
transform: (d) => d,
|
|
47
|
+
dataEditor: ColorEditor,
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
export function addEmptyRows(
|
|
52
|
+
data: SheetContent,
|
|
53
|
+
modulus: number = 10,
|
|
54
|
+
targetN = 0
|
|
55
|
+
): SheetContent {
|
|
56
|
+
const nToAdd =
|
|
57
|
+
Math.ceil((data.length + targetN) / modulus) * modulus - data.length;
|
|
58
|
+
if (nToAdd <= 0) return data;
|
|
59
|
+
const emptyData = Array(orientationFields.length).fill({ value: null });
|
|
60
|
+
const addedRows = Array(nToAdd).fill(emptyData);
|
|
61
|
+
return [...data, ...addedRows];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function Columns() {
|
|
65
|
+
return h("colgroup", [
|
|
66
|
+
h("col.index-column", { key: "index" }),
|
|
67
|
+
orientationFields.map(({ key }) => {
|
|
68
|
+
return h("col", {
|
|
69
|
+
className: key,
|
|
70
|
+
key,
|
|
71
|
+
});
|
|
72
|
+
}),
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function Row(props) {
|
|
77
|
+
const { children, row } = props;
|
|
78
|
+
return h("tr", [h("td.index-cell.cell.read-only.index", row + 1), children]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function Header() {
|
|
82
|
+
return h("thead", [
|
|
83
|
+
h("tr.header", [
|
|
84
|
+
h("td.index-column.cell.read-only", ""),
|
|
85
|
+
orientationFields.map((col, index) => {
|
|
86
|
+
return h(
|
|
87
|
+
"td.cell.header.read-only.header-cell",
|
|
88
|
+
{
|
|
89
|
+
key: col.key,
|
|
90
|
+
index,
|
|
91
|
+
},
|
|
92
|
+
col.name
|
|
93
|
+
);
|
|
94
|
+
}),
|
|
95
|
+
]),
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function Sheet({ className, children }) {
|
|
100
|
+
return h("table", { className }, [
|
|
101
|
+
h(Columns),
|
|
102
|
+
h(Header),
|
|
103
|
+
h("tbody", children),
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function getFieldData<K>(field: Field<K>): Field<K> {
|
|
108
|
+
const {
|
|
109
|
+
transform = (d) => parseFloat(d),
|
|
110
|
+
isValid = (d) => !isNaN(d),
|
|
111
|
+
required = true,
|
|
112
|
+
...rest
|
|
113
|
+
} = field;
|
|
114
|
+
return { ...rest, transform, isValid, required };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function Controls({ data, updateData, resetData }) {
|
|
118
|
+
return h("div.controls", [
|
|
119
|
+
h(ButtonGroup, [
|
|
120
|
+
h(
|
|
121
|
+
Button,
|
|
122
|
+
{
|
|
123
|
+
onClick() {
|
|
124
|
+
updateData(addEmptyRows(data, 10, 10));
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
"Add more rows"
|
|
128
|
+
),
|
|
129
|
+
h(
|
|
130
|
+
Button,
|
|
131
|
+
{
|
|
132
|
+
onClick: resetData,
|
|
133
|
+
},
|
|
134
|
+
"Reset data"
|
|
135
|
+
),
|
|
136
|
+
]),
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function enhanceData(row: GridElement[]): any[] {
|
|
141
|
+
if (row == null) return [];
|
|
142
|
+
return row.map((cellData, i) => {
|
|
143
|
+
const { dataEditor, key } = getFieldData(orientationFields[i]);
|
|
144
|
+
|
|
145
|
+
let addedProps = {};
|
|
146
|
+
// if (key == "color") {
|
|
147
|
+
// addedProps.valueViewer = cell =>
|
|
148
|
+
// h(
|
|
149
|
+
// "span.value-viewer",
|
|
150
|
+
// { style: { color: getColor(cell.value), bacbac } },
|
|
151
|
+
// cell.value
|
|
152
|
+
// );
|
|
153
|
+
// }
|
|
154
|
+
|
|
155
|
+
return { dataEditor, ...cellData, ...addedProps };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function finalizeRow(row: GridElement[]) {
|
|
160
|
+
// Validation function to ensure that max angular error is always greater than min angular error
|
|
161
|
+
if (parseFloat(row[3]?.value) < parseFloat(row[4]?.value)) {
|
|
162
|
+
console.log("Row has errors");
|
|
163
|
+
let newRow = [...row];
|
|
164
|
+
newRow[3] = row[4];
|
|
165
|
+
newRow[4] = row[3];
|
|
166
|
+
return newRow;
|
|
167
|
+
}
|
|
168
|
+
return row;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function DataSheet({ data, updateData }) {
|
|
172
|
+
return h(ReactDataSheet, {
|
|
173
|
+
data: data.map(enhanceData),
|
|
174
|
+
valueRenderer: (cell, row, col) => {
|
|
175
|
+
return cell.value;
|
|
176
|
+
},
|
|
177
|
+
rowRenderer: Row,
|
|
178
|
+
sheetRenderer: Sheet,
|
|
179
|
+
attributesRenderer(cell, row, col) {
|
|
180
|
+
if (cell.value == null || cell.value == "")
|
|
181
|
+
return { "data-status": "empty" };
|
|
182
|
+
const { isValid } = getFieldData(orientationFields[col]);
|
|
183
|
+
const status = isValid(cell.value) ? "ok" : "invalid";
|
|
184
|
+
return { "data-status": status };
|
|
185
|
+
},
|
|
186
|
+
onContextMenu: (...args) => {
|
|
187
|
+
console.log("Context menu");
|
|
188
|
+
console.log(args);
|
|
189
|
+
},
|
|
190
|
+
onCellsChanged: (changes) => {
|
|
191
|
+
let spec: Spec<SheetContent> = {};
|
|
192
|
+
changes.forEach(({ cell, row, col, value }) => {
|
|
193
|
+
if (!(row in spec)) spec[row] = {};
|
|
194
|
+
spec[row][col] = { $set: { value } };
|
|
195
|
+
});
|
|
196
|
+
updateData(update(data, spec).map(finalizeRow));
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function DataArea({ data, updateData, resetData }) {
|
|
202
|
+
return h("div.data-area", [
|
|
203
|
+
h(
|
|
204
|
+
ErrorBoundary,
|
|
205
|
+
null,
|
|
206
|
+
h(DataSheet, { data, updateData, style: { width: 800 } })
|
|
207
|
+
),
|
|
208
|
+
h(Controls, { data, updateData, resetData }),
|
|
209
|
+
]);
|
|
210
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import ReactDataSheet, { DataEditor } from "react-datasheet/lib";
|
|
2
|
+
import { useDataSheet } from "./provider";
|
|
3
|
+
import { hyperStyled } from "@macrostrat/hyper";
|
|
4
|
+
import { useRef, useEffect } from "react";
|
|
5
|
+
import { useScrollOffset } from "@macrostrat/ui-components";
|
|
6
|
+
import { BaseSheet } from "./base";
|
|
7
|
+
import styles from "./styles.module.scss";
|
|
8
|
+
|
|
9
|
+
const h = hyperStyled(styles);
|
|
10
|
+
|
|
11
|
+
function VirtualizedDataEditor({ row, ...rest }) {
|
|
12
|
+
const { rowOffset } = useDataSheet();
|
|
13
|
+
return h(DataEditor, {
|
|
14
|
+
row: row + rowOffset,
|
|
15
|
+
...rest,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type OptionalSelection = ReactDataSheet.Selection | null;
|
|
20
|
+
|
|
21
|
+
function offsetSelection(
|
|
22
|
+
sel: OptionalSelection,
|
|
23
|
+
offset: number
|
|
24
|
+
): OptionalSelection {
|
|
25
|
+
if (sel == null) return null;
|
|
26
|
+
const { start, end } = sel;
|
|
27
|
+
return {
|
|
28
|
+
start: { i: start.i + offset, j: start.j },
|
|
29
|
+
end: { i: end.i + offset, j: end.j },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function VirtualizedSheet(props) {
|
|
34
|
+
const {
|
|
35
|
+
data,
|
|
36
|
+
dataEditor,
|
|
37
|
+
onCellsChanged,
|
|
38
|
+
scrollBuffer = 50,
|
|
39
|
+
height,
|
|
40
|
+
width,
|
|
41
|
+
...rest
|
|
42
|
+
} = props;
|
|
43
|
+
|
|
44
|
+
const { rowHeight, selection, dispatch } = useDataSheet();
|
|
45
|
+
|
|
46
|
+
const ref = useRef<HTMLDivElement>();
|
|
47
|
+
|
|
48
|
+
const scrollOffset = useScrollOffset(ref);
|
|
49
|
+
const scrollerHeight = data.length * rowHeight;
|
|
50
|
+
const percentage = scrollOffset / scrollerHeight;
|
|
51
|
+
const rowsToDisplay = Math.ceil((height + scrollBuffer) / rowHeight);
|
|
52
|
+
const rowOffset = Math.floor(percentage * (data.length - rowsToDisplay + 5));
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
dispatch({ type: "set-row-offset", value: rowOffset });
|
|
56
|
+
}, [rowOffset]);
|
|
57
|
+
|
|
58
|
+
const lastRow = Math.min(rowOffset + rowsToDisplay, data.length - 1);
|
|
59
|
+
|
|
60
|
+
return h(
|
|
61
|
+
BaseSheet,
|
|
62
|
+
{
|
|
63
|
+
ref,
|
|
64
|
+
height,
|
|
65
|
+
width,
|
|
66
|
+
className: "virtualized-sheet",
|
|
67
|
+
...rest,
|
|
68
|
+
data: data.slice(rowOffset, lastRow),
|
|
69
|
+
selected: offsetSelection(selection, -rowOffset),
|
|
70
|
+
dataEditor: VirtualizedDataEditor,
|
|
71
|
+
onCellsChanged(changes) {
|
|
72
|
+
changes.forEach((d) => (d.row += rowOffset));
|
|
73
|
+
onCellsChanged?.(changes);
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
h("div.scroll-panel", {
|
|
77
|
+
style: { height: scrollerHeight },
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { VirtualizedSheet, VirtualizedDataEditor, offsetSelection };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import hyper from "@macrostrat/hyper";
|
|
2
|
+
import style from "./actions.module.sass";
|
|
3
|
+
const h = hyper.styled(style);
|
|
4
|
+
|
|
5
|
+
export function DataSheetActionsRow(props) {
|
|
6
|
+
return h("div.table-actions", props.children);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function DataSheetAction(props) {
|
|
10
|
+
return h("div.table-action", props.children);
|
|
11
|
+
}
|