@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,55 @@
|
|
|
1
|
+
import ReactDataSheet from "react-datasheet";
|
|
2
|
+
import "react-datasheet/lib/react-datasheet.css";
|
|
3
|
+
import h from "@macrostrat/hyper";
|
|
4
|
+
import { forwardRef } from "react";
|
|
5
|
+
import { useDataSheet } from "./provider";
|
|
6
|
+
import { Row, Sheet } from "./components";
|
|
7
|
+
import React from "react";
|
|
8
|
+
|
|
9
|
+
interface GridElement extends ReactDataSheet.Cell<GridElement, number> {
|
|
10
|
+
value: number | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface SheetContainerProps<T = any>
|
|
14
|
+
extends ReactDataSheet.DataSheetProps<T> {
|
|
15
|
+
height?: number;
|
|
16
|
+
width?: number;
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
className?: string;
|
|
19
|
+
scrollable?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const BaseSheet = forwardRef((props: SheetContainerProps, ref) => {
|
|
23
|
+
const {
|
|
24
|
+
scrollable = true,
|
|
25
|
+
height,
|
|
26
|
+
width,
|
|
27
|
+
children,
|
|
28
|
+
className,
|
|
29
|
+
...rest
|
|
30
|
+
} = props;
|
|
31
|
+
|
|
32
|
+
const { dispatch } = useDataSheet();
|
|
33
|
+
|
|
34
|
+
return h(
|
|
35
|
+
"div.data-sheet-container",
|
|
36
|
+
{ style: { height, width }, className, ref },
|
|
37
|
+
[
|
|
38
|
+
h("div.ui", { style: { height, width } }, [
|
|
39
|
+
h(ReactDataSheet, {
|
|
40
|
+
width,
|
|
41
|
+
height,
|
|
42
|
+
sheetRenderer: Sheet,
|
|
43
|
+
rowRenderer: Row,
|
|
44
|
+
onSelect(sel) {
|
|
45
|
+
dispatch({ type: "set-selection", value: sel });
|
|
46
|
+
},
|
|
47
|
+
...rest,
|
|
48
|
+
}),
|
|
49
|
+
]),
|
|
50
|
+
children,
|
|
51
|
+
]
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export { GridElement };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { sum } from "d3-array";
|
|
2
|
+
import { useDataSheet } from "../provider";
|
|
3
|
+
import { useDrop, DropTargetMonitor } from "react-dnd";
|
|
4
|
+
|
|
5
|
+
interface ColumnData {
|
|
6
|
+
name: string;
|
|
7
|
+
key: string;
|
|
8
|
+
width?: number;
|
|
9
|
+
idealWidth?: number;
|
|
10
|
+
editable?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ColumnWidthInfo {
|
|
14
|
+
key: string;
|
|
15
|
+
offset: number;
|
|
16
|
+
width: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type DragItem = {
|
|
20
|
+
type: "column";
|
|
21
|
+
id: string;
|
|
22
|
+
index: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/* Column width functionality */
|
|
26
|
+
function apportionWidths(
|
|
27
|
+
columns: ColumnData[],
|
|
28
|
+
desiredWidths: { [key: string]: number },
|
|
29
|
+
containerWidth: number
|
|
30
|
+
): ColumnData[] {
|
|
31
|
+
if (columns == null || desiredWidths == null || containerWidth == null)
|
|
32
|
+
return columns;
|
|
33
|
+
|
|
34
|
+
const totalSize = sum(Object.values(desiredWidths));
|
|
35
|
+
|
|
36
|
+
//console.log(maxContentWidth);
|
|
37
|
+
|
|
38
|
+
return columns.map((col) => {
|
|
39
|
+
return {
|
|
40
|
+
...col,
|
|
41
|
+
width: (desiredWidths[col.key] / totalSize) * (containerWidth - 50),
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function useColumnDropTarget(ref, index) {
|
|
47
|
+
const { dispatch } = useDataSheet();
|
|
48
|
+
return useDrop({
|
|
49
|
+
accept: "column",
|
|
50
|
+
hover(item: DragItem, monitor: DropTargetMonitor) {
|
|
51
|
+
if (!ref.current) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const dragIndex = item.index;
|
|
55
|
+
const hoverIndex = index;
|
|
56
|
+
|
|
57
|
+
// Don't replace items with themselves
|
|
58
|
+
if (dragIndex === hoverIndex) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Determine rectangle on screen
|
|
63
|
+
const hoverBoundingRect = ref.current?.getBoundingClientRect();
|
|
64
|
+
|
|
65
|
+
// Get horizontal middle
|
|
66
|
+
const hoverMiddleX =
|
|
67
|
+
(hoverBoundingRect.right - hoverBoundingRect.left) / 2;
|
|
68
|
+
|
|
69
|
+
// Determine mouse position
|
|
70
|
+
const clientOffset = monitor.getClientOffset();
|
|
71
|
+
|
|
72
|
+
// Get pixels to the left
|
|
73
|
+
const hoverClientX = clientOffset.x - hoverBoundingRect.left;
|
|
74
|
+
|
|
75
|
+
// Only perform the move when the mouse has crossed half of the items height
|
|
76
|
+
// When dragging downwards, only move when the cursor is below 50%
|
|
77
|
+
// When dragging upwards, only move when the cursor is above 50%
|
|
78
|
+
// Dragging downwards
|
|
79
|
+
if (dragIndex < hoverIndex && hoverClientX < hoverMiddleX) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Dragging upwards
|
|
84
|
+
if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Time to actually perform the action
|
|
89
|
+
dispatch({ type: "move-column", dragIndex, hoverIndex });
|
|
90
|
+
//moveCard(dragIndex, hoverIndex);
|
|
91
|
+
|
|
92
|
+
// Note: we're mutating the monitor item here!
|
|
93
|
+
// Generally it's better to avoid mutations,
|
|
94
|
+
// but it's good here for the sake of performance
|
|
95
|
+
// to avoid expensive index searches.
|
|
96
|
+
item.index = hoverIndex;
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function useColumnWidths(): ColumnWidthInfo[] {
|
|
102
|
+
const { columns, columnWidths, containerWidth } = useDataSheet();
|
|
103
|
+
|
|
104
|
+
let widthArray = [];
|
|
105
|
+
let offset = 50; // for index, we should probably not hard-code this...
|
|
106
|
+
for (const [i, col] of columns.entries()) {
|
|
107
|
+
let width =
|
|
108
|
+
columnWidths[col.name] ??
|
|
109
|
+
col.width ??
|
|
110
|
+
(containerWidth - 50) / columns.length;
|
|
111
|
+
if (i == columns.length - 1) {
|
|
112
|
+
// special case for last column to fill container
|
|
113
|
+
width = containerWidth - offset;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
widthArray.push({
|
|
117
|
+
key: col.name,
|
|
118
|
+
offset: offset,
|
|
119
|
+
width,
|
|
120
|
+
});
|
|
121
|
+
offset += width;
|
|
122
|
+
}
|
|
123
|
+
return widthArray;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export { ColumnData, apportionWidths, useColumnWidths, useColumnDropTarget };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { useRef, memo } from "react";
|
|
2
|
+
import { hyperStyled } from "@macrostrat/hyper";
|
|
3
|
+
import { ColumnInfo, useDataSheet } from "../provider";
|
|
4
|
+
import { useColumnDropTarget, useColumnWidths } from "./column-utils";
|
|
5
|
+
import { useDrag } from "react-dnd";
|
|
6
|
+
import Draggable from "react-draggable";
|
|
7
|
+
import styles from "../styles.module.scss";
|
|
8
|
+
|
|
9
|
+
const h = hyperStyled(styles);
|
|
10
|
+
|
|
11
|
+
function Row({ row, children, className }) {
|
|
12
|
+
const { rowHeight, rowOffset } = useDataSheet();
|
|
13
|
+
const style = { height: rowHeight };
|
|
14
|
+
return h("tr", { style, className }, [
|
|
15
|
+
h("td.index-cell.cell.read-only.index", row + rowOffset + 1),
|
|
16
|
+
children,
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function Columns() {
|
|
21
|
+
const columns = useColumnWidths();
|
|
22
|
+
return h("colgroup", [
|
|
23
|
+
h("col.index-column", { key: "index", style: { width: 50 } }),
|
|
24
|
+
columns.map(({ width, key }) => {
|
|
25
|
+
return h("col", {
|
|
26
|
+
key,
|
|
27
|
+
style: { width },
|
|
28
|
+
});
|
|
29
|
+
}),
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function ColumnResizeHandles() {
|
|
34
|
+
const { dispatch } = useDataSheet();
|
|
35
|
+
const columnWidths = useColumnWidths();
|
|
36
|
+
return h(
|
|
37
|
+
"td.column-resizers",
|
|
38
|
+
columnWidths.map((d, i) => {
|
|
39
|
+
if (i == columnWidths.length - 1) return null;
|
|
40
|
+
return h(
|
|
41
|
+
Draggable,
|
|
42
|
+
{
|
|
43
|
+
axis: "x",
|
|
44
|
+
position: { x: d.offset + d.width, y: 0 },
|
|
45
|
+
onStop(event, { x }) {
|
|
46
|
+
dispatch({
|
|
47
|
+
type: "update-column-width",
|
|
48
|
+
key: d.key,
|
|
49
|
+
value: x - d.offset,
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
h("span.cell-drag-handle", {
|
|
54
|
+
onMouseDown(e) {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
e.stopPropagation();
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function HeaderCell({ col, index }: { col: ColumnInfo; index: number }) {
|
|
65
|
+
const ref = useRef();
|
|
66
|
+
|
|
67
|
+
const [, drop] = useColumnDropTarget(ref, index);
|
|
68
|
+
|
|
69
|
+
const [{ isDragging }, drag] = useDrag({
|
|
70
|
+
item: { id: col.name, index },
|
|
71
|
+
type: "column",
|
|
72
|
+
collect: (monitor: any) => ({
|
|
73
|
+
isDragging: monitor.isDragging(),
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const opacity = isDragging ? 0 : 1;
|
|
78
|
+
|
|
79
|
+
drag(drop(ref));
|
|
80
|
+
return h(
|
|
81
|
+
"td.cell.header.read-only.header-cell",
|
|
82
|
+
{ style: { opacity }, key: col.name },
|
|
83
|
+
[h("div.cell-content", { ref }, col.name)]
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Header_({ width }) {
|
|
88
|
+
const style = { width };
|
|
89
|
+
const { columns } = useDataSheet();
|
|
90
|
+
|
|
91
|
+
return h("thead", { style }, [
|
|
92
|
+
h("tr.header", { style }, [
|
|
93
|
+
h("td.index-column.cell.read-only", ""),
|
|
94
|
+
columns.map((col, index) => {
|
|
95
|
+
return h(HeaderCell, { key: col.name, col, index });
|
|
96
|
+
}),
|
|
97
|
+
h(ColumnResizeHandles),
|
|
98
|
+
]),
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const Header = memo(Header_);
|
|
103
|
+
|
|
104
|
+
function Sheet({ className, children }) {
|
|
105
|
+
const { containerWidth: width } = useDataSheet();
|
|
106
|
+
return h("table", { className, style: { width } }, [
|
|
107
|
+
h(Columns),
|
|
108
|
+
h(Header, { width }),
|
|
109
|
+
h("tbody", children),
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export * from "./column-utils";
|
|
114
|
+
export * from "./toolbar";
|
|
115
|
+
export { Sheet, Row };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import h from "@macrostrat/hyper";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { Dialog, Classes, Button, ButtonGroup } from "@blueprintjs/core";
|
|
4
|
+
import { SaveButton, CancelButton } from "@macrostrat/ui-components";
|
|
5
|
+
|
|
6
|
+
function SubmitDialog({
|
|
7
|
+
onClick,
|
|
8
|
+
content,
|
|
9
|
+
className = null,
|
|
10
|
+
disabled,
|
|
11
|
+
...rest
|
|
12
|
+
}) {
|
|
13
|
+
const [open, setOpen] = useState(false);
|
|
14
|
+
return h("div", null, [
|
|
15
|
+
h(
|
|
16
|
+
SaveButton,
|
|
17
|
+
{
|
|
18
|
+
disabled: disabled,
|
|
19
|
+
...rest,
|
|
20
|
+
onClick: () => setOpen(true),
|
|
21
|
+
},
|
|
22
|
+
"Save Changes"
|
|
23
|
+
),
|
|
24
|
+
h(Dialog, { isOpen: open }, [
|
|
25
|
+
h("div", { className: Classes.DIALOG_HEADER }, h("h3", null, "WARNING")),
|
|
26
|
+
h("div", { className: Classes.DIALOG_BODY }, h("p", null, content)),
|
|
27
|
+
h("div", { className: Classes.DIALOG_FOOTER }, [
|
|
28
|
+
h(
|
|
29
|
+
SaveButton,
|
|
30
|
+
{
|
|
31
|
+
className: "save-btn",
|
|
32
|
+
small: true,
|
|
33
|
+
onClick: () => {
|
|
34
|
+
onClick();
|
|
35
|
+
setOpen(false);
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
"Save changes"
|
|
39
|
+
),
|
|
40
|
+
h(
|
|
41
|
+
CancelButton,
|
|
42
|
+
{
|
|
43
|
+
small: true,
|
|
44
|
+
onClick: () => setOpen(false),
|
|
45
|
+
},
|
|
46
|
+
"Cancel"
|
|
47
|
+
),
|
|
48
|
+
]),
|
|
49
|
+
]),
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function SheetToolbar(props) {
|
|
54
|
+
const { onSubmit, onUndo, hasChanges, children } = props;
|
|
55
|
+
const buttonProps = { disabled: !hasChanges, small: true };
|
|
56
|
+
var constant =
|
|
57
|
+
"Are you sure you want to save your edits? All changes will be final. If you do not want to submit, click Cancel.";
|
|
58
|
+
return h("div.sheet-header", [
|
|
59
|
+
h("h3.sheet-title", "Sample metadata"),
|
|
60
|
+
h(ButtonGroup, { className: "sheet-actions" }, [
|
|
61
|
+
h(SubmitDialog, {
|
|
62
|
+
className: "save-btn",
|
|
63
|
+
onClick: onSubmit,
|
|
64
|
+
content: constant,
|
|
65
|
+
...buttonProps,
|
|
66
|
+
}),
|
|
67
|
+
h(
|
|
68
|
+
Button,
|
|
69
|
+
{
|
|
70
|
+
onClick: onUndo,
|
|
71
|
+
...buttonProps,
|
|
72
|
+
},
|
|
73
|
+
"Reset changes"
|
|
74
|
+
),
|
|
75
|
+
]),
|
|
76
|
+
h("div.spacer"),
|
|
77
|
+
children,
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import h from "@macrostrat/hyper";
|
|
2
|
+
import { ErrorBoundary, useInDarkMode } from "@macrostrat/ui-components";
|
|
3
|
+
import { ChromePicker } from "react-color";
|
|
4
|
+
import { Popover } from "@blueprintjs/core";
|
|
5
|
+
import chroma from "chroma-js";
|
|
6
|
+
import "./styles.module.scss";
|
|
7
|
+
|
|
8
|
+
export function BasePopupEditor(props) {
|
|
9
|
+
const { children, valueRenderer, value, ...rest } = props;
|
|
10
|
+
|
|
11
|
+
return h(
|
|
12
|
+
Popover,
|
|
13
|
+
{
|
|
14
|
+
content: h(
|
|
15
|
+
"div.interaction-barrier",
|
|
16
|
+
{
|
|
17
|
+
onMouseDown(evt) {
|
|
18
|
+
//evt.nativeEvent.stopImmediatePropagation();
|
|
19
|
+
},
|
|
20
|
+
onKeyDown(evt) {
|
|
21
|
+
console.log(evt);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
h(ErrorBoundary, null, children)
|
|
25
|
+
),
|
|
26
|
+
enforceFocus: false,
|
|
27
|
+
autoFocus: false,
|
|
28
|
+
minimal: true,
|
|
29
|
+
modifiers: {
|
|
30
|
+
//offset: { enabled: true, options: { offset: [0, 8] } },
|
|
31
|
+
},
|
|
32
|
+
interactionKind: "hover-target",
|
|
33
|
+
isOpen: true,
|
|
34
|
+
onClose(evt) {
|
|
35
|
+
props.onKeyDown(evt);
|
|
36
|
+
},
|
|
37
|
+
usePortal: false,
|
|
38
|
+
},
|
|
39
|
+
children
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function ColorPicker({ value, onChange }) {
|
|
44
|
+
const darkMode = useInDarkMode();
|
|
45
|
+
const background = darkMode ? "#383e47;" : "#ffffff";
|
|
46
|
+
let color = "#aaaaaa";
|
|
47
|
+
try {
|
|
48
|
+
color = chroma(value).hex();
|
|
49
|
+
} catch {}
|
|
50
|
+
return h(ChromePicker, {
|
|
51
|
+
disableAlpha: true,
|
|
52
|
+
color,
|
|
53
|
+
styles: {
|
|
54
|
+
default: {
|
|
55
|
+
picker: {
|
|
56
|
+
background,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
onChange(color, evt) {
|
|
61
|
+
let c = "";
|
|
62
|
+
try {
|
|
63
|
+
c = chroma(color.hex);
|
|
64
|
+
} finally {
|
|
65
|
+
onChange(c);
|
|
66
|
+
evt.stopPropagation();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
onKeyDown(evt) {
|
|
70
|
+
console.log(evt);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function ColorEditor(props) {
|
|
76
|
+
const { value, onChange, ...rest } = props;
|
|
77
|
+
return h(BasePopupEditor, rest, h(ColorPicker, { value, onChange }));
|
|
78
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import ReactDataSheet from "react-datasheet";
|
|
2
|
+
//import classNames from "classnames";
|
|
3
|
+
interface GridElement extends ReactDataSheet.Cell<GridElement, number> {
|
|
4
|
+
value: number | null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
type SheetContent = GridElement[][];
|
|
8
|
+
|
|
9
|
+
export interface Field<Key> {
|
|
10
|
+
name: string;
|
|
11
|
+
key: Key;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
isValid?(k: any): boolean;
|
|
14
|
+
transform?(k: any): any;
|
|
15
|
+
valueViewer?: (k: any) => any;
|
|
16
|
+
dataEditor?: ReactDataSheet.DataEditor<ReactDataSheet.Cell<any, any>>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getFieldData<K>(field: Field<K>): Field<K> {
|
|
20
|
+
const {
|
|
21
|
+
transform = (d) => parseFloat(d),
|
|
22
|
+
isValid = (d) => !isNaN(d),
|
|
23
|
+
required = true,
|
|
24
|
+
...rest
|
|
25
|
+
} = field;
|
|
26
|
+
return { ...rest, transform, isValid, required };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function enhanceData(row: GridElement[], fields): any[] {
|
|
30
|
+
if (row == null) return [];
|
|
31
|
+
return row.map((cellData, i) => {
|
|
32
|
+
const { dataEditor, valueViewer, key } = getFieldData(fields[i]);
|
|
33
|
+
|
|
34
|
+
return { dataEditor, valueViewer, ...cellData };
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { DataSheetProvider } from "./provider";
|
|
2
|
+
import { VirtualizedSheet } from "./virtualized";
|
|
3
|
+
import { BaseSheet } from "./base";
|
|
4
|
+
import { ErrorBoundary, useElementSize } from "@macrostrat/ui-components";
|
|
5
|
+
import { enhanceData } from "./enhancers";
|
|
6
|
+
import { GridElement } from "./base";
|
|
7
|
+
import h from "@macrostrat/hyper";
|
|
8
|
+
import { useState, useEffect, useRef } from "react";
|
|
9
|
+
import { SheetToolbar } from "./components";
|
|
10
|
+
import classNames from "classnames";
|
|
11
|
+
import update from "immutability-helper";
|
|
12
|
+
|
|
13
|
+
export function DataSheet(props) {
|
|
14
|
+
const { data: baseData, columns, onSubmit, ...rest } = props;
|
|
15
|
+
const [data, setData] = useState<any[]>(baseData);
|
|
16
|
+
//const [edits, setEdits] = useState([]);
|
|
17
|
+
/**
|
|
18
|
+
* For edits, what I could do is create an array of indexes based on the row number.
|
|
19
|
+
* And at the end I can grab the whole row and send it to the backend.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
// Set data to start with the value of the initial data
|
|
24
|
+
if (baseData == null) return;
|
|
25
|
+
setData(baseData);
|
|
26
|
+
}, [baseData]);
|
|
27
|
+
|
|
28
|
+
const ref = useRef();
|
|
29
|
+
const size = useElementSize(ref);
|
|
30
|
+
|
|
31
|
+
const onCancel = () => {
|
|
32
|
+
setData(baseData);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//const [columns, setDesiredWidth] = useState(columnSpec);
|
|
36
|
+
|
|
37
|
+
//const desiredWidths =
|
|
38
|
+
// initialData != null ? calculateWidths(initialData, columns) : {};
|
|
39
|
+
|
|
40
|
+
// Change management
|
|
41
|
+
// const handleUndo = () => {
|
|
42
|
+
// setData(initialData);
|
|
43
|
+
// setEdits([]);
|
|
44
|
+
// };
|
|
45
|
+
// const handleSubmit = () => {
|
|
46
|
+
// //push method for sending data back to api
|
|
47
|
+
// if (data.length === 0) {
|
|
48
|
+
// return null;
|
|
49
|
+
// }
|
|
50
|
+
// setData(data);
|
|
51
|
+
// const post_data = BuildEdits();
|
|
52
|
+
// console.log(post_data);
|
|
53
|
+
// postData(post_data);
|
|
54
|
+
// setEdits([]);
|
|
55
|
+
// };
|
|
56
|
+
|
|
57
|
+
// function BuildEdits() {
|
|
58
|
+
// /** Grabs the edits from the data for POST.
|
|
59
|
+
// *
|
|
60
|
+
// * How can I group them by rows so I don't have to send a item per cell
|
|
61
|
+
// */
|
|
62
|
+
|
|
63
|
+
// const editsList = addNecesaryFields(edits, data);
|
|
64
|
+
// const finalEdits = combineLikeIds(editsList);
|
|
65
|
+
// return finalEdits;
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
function onCellsChanged(changes) {
|
|
69
|
+
/** Cell change function that uses immutability-helper */
|
|
70
|
+
const spec = {};
|
|
71
|
+
// const edits = []; // something like what's happening with spec
|
|
72
|
+
console.log(changes);
|
|
73
|
+
changes.forEach(({ cell, row, col, value }) => {
|
|
74
|
+
// Get the key that should be used to assign the value
|
|
75
|
+
const { key } = columns[col];
|
|
76
|
+
// Substitute empty strings for nulls
|
|
77
|
+
const $set = value == "" ? null : value;
|
|
78
|
+
// sets edits to the state index and the column name
|
|
79
|
+
//setEdits((prevEdits) => [{ row, key }, ...prevEdits]);
|
|
80
|
+
spec[row] = {
|
|
81
|
+
...(spec[row] || {}),
|
|
82
|
+
[key]: { $set },
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
setData(update(data, spec));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Builds the properties for the cell */
|
|
89
|
+
const finalizeCellProps = (
|
|
90
|
+
value: any,
|
|
91
|
+
row: number,
|
|
92
|
+
key: string,
|
|
93
|
+
col: number
|
|
94
|
+
) => {
|
|
95
|
+
/** Need to turn off submit on click away event for cell */
|
|
96
|
+
|
|
97
|
+
// Check if values is the same as the initial data key
|
|
98
|
+
//console.log(arguments);
|
|
99
|
+
|
|
100
|
+
const isChanged = value != baseData[row][key];
|
|
101
|
+
const readOnly = !(columns[col].editable ?? true);
|
|
102
|
+
const className = classNames(
|
|
103
|
+
{ edited: isChanged, "read-only": readOnly },
|
|
104
|
+
"cell",
|
|
105
|
+
"nowrap",
|
|
106
|
+
"clip"
|
|
107
|
+
);
|
|
108
|
+
return {
|
|
109
|
+
value,
|
|
110
|
+
className,
|
|
111
|
+
readOnly,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** We now build cell properties in the render function, rather than storing
|
|
116
|
+
them using a precomputed useEffect hook. We decide what cells have changed using
|
|
117
|
+
a direct comparison with the equivalent value of initialData. (That's the
|
|
118
|
+
power of immutable data.)
|
|
119
|
+
|
|
120
|
+
This is where the Components should be attached.
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
const sheetHeader = h(
|
|
124
|
+
SheetToolbar,
|
|
125
|
+
{
|
|
126
|
+
onSubmit,
|
|
127
|
+
onCancel,
|
|
128
|
+
hasChanges: baseData != data,
|
|
129
|
+
}
|
|
130
|
+
//h("div.right-actions", null, h(SettingsPopup))
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
return h(DataSheetUI, {
|
|
134
|
+
sheetHeader,
|
|
135
|
+
columns,
|
|
136
|
+
onCellsChanged,
|
|
137
|
+
finalizeCellProps,
|
|
138
|
+
data,
|
|
139
|
+
...rest,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function DataSheetUI({
|
|
144
|
+
columns,
|
|
145
|
+
data,
|
|
146
|
+
virtualized = false,
|
|
147
|
+
sheetHeader = null,
|
|
148
|
+
finalizeCellProps,
|
|
149
|
+
...rest
|
|
150
|
+
}) {
|
|
151
|
+
const component = virtualized ? VirtualizedSheet : BaseSheet;
|
|
152
|
+
let newData = data.map((d) => transformData(columns, d));
|
|
153
|
+
|
|
154
|
+
if (finalizeCellProps != null) {
|
|
155
|
+
newData = newData.map((obj, row) => {
|
|
156
|
+
return columns.map(({ key }, col) =>
|
|
157
|
+
finalizeCellProps(obj[col].value, row, key, col)
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return h(
|
|
162
|
+
ErrorBoundary,
|
|
163
|
+
null,
|
|
164
|
+
h(
|
|
165
|
+
DataSheetProvider,
|
|
166
|
+
{ columns },
|
|
167
|
+
h("div.data-sheet", [
|
|
168
|
+
sheetHeader,
|
|
169
|
+
h(component, { ...rest, data: newData }),
|
|
170
|
+
])
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function transformData(columns, data: object): GridElement[] {
|
|
176
|
+
const row1 = columns.map((d) => {
|
|
177
|
+
return { value: data[d.key] ?? null, className: "test" };
|
|
178
|
+
});
|
|
179
|
+
return enhanceData(row1, columns);
|
|
180
|
+
}
|