@macrostrat/data-sheet 3.0.2 → 3.0.4
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 +27 -0
- package/dist/components/actions.cjs +2 -6
- package/dist/components/actions.cjs.map +1 -1
- package/dist/components/actions.js +1 -3
- package/dist/components/actions.js.map +1 -1
- package/dist/components/actions.module.sass.cjs +7 -2
- package/dist/components/actions.module.sass.cjs.map +1 -1
- package/dist/components/actions.module.sass.js +5 -2
- package/dist/components/actions.module.sass.js.map +1 -1
- package/dist/components/colors.cjs +4 -7
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.js +1 -3
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +15 -19
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.d.ts +1 -1
- package/dist/components/editor-popup.js +10 -12
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/main.module.sass.cjs +6 -1
- package/dist/components/main.module.sass.cjs.map +1 -1
- package/dist/components/main.module.sass.js +4 -1
- package/dist/components/main.module.sass.js.map +1 -1
- package/dist/components/text-areas.js +2 -2
- package/dist/components/text-areas.js.map +1 -1
- package/dist/core.cjs +48 -61
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +20 -32
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +6 -1
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +4 -1
- package/dist/main.module.sass.js.map +1 -1
- package/dist/postgrest-table/data-loaders.cjs +109 -0
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +6 -0
- package/dist/postgrest-table/data-loaders.js +111 -2
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +1 -0
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +1 -0
- package/dist/postgrest-table/index.js +4 -2
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +26 -0
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -0
- package/dist/postgrest-table/lazy-loader-table.d.ts +3 -0
- package/dist/postgrest-table/lazy-loader-table.js +24 -0
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -0
- package/dist/postgrest-table/main.module.sass.cjs +6 -1
- package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
- package/dist/postgrest-table/main.module.sass.js +4 -1
- package/dist/postgrest-table/main.module.sass.js.map +1 -1
- package/dist/provider.cjs +5 -2
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/provider.js +5 -2
- package/dist/provider.js.map +1 -1
- package/dist/utils/column-spec.cjs +6 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.js +6 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/package.json +7 -7
- package/src/components/actions.ts +1 -3
- package/src/components/colors.ts +1 -4
- package/src/components/editor-popup.ts +9 -12
- package/src/core.ts +38 -50
- package/src/postgrest-table/data-loaders.ts +151 -1
- package/src/postgrest-table/index.ts +1 -0
- package/src/postgrest-table/lazy-loader-table.ts +24 -0
- package/src/provider.ts +6 -3
- package/src/utils/column-spec.ts +6 -1
|
@@ -72,7 +72,12 @@ function generateDefaultColumnSpec(data, options = {}) {
|
|
|
72
72
|
return spec;
|
|
73
73
|
}
|
|
74
74
|
function generateColumnSpec(data, options) {
|
|
75
|
-
const {
|
|
75
|
+
const {
|
|
76
|
+
overrides = {},
|
|
77
|
+
nRows = 10,
|
|
78
|
+
omitColumns,
|
|
79
|
+
includeColumns
|
|
80
|
+
} = options ?? {};
|
|
76
81
|
if (data == null) return [];
|
|
77
82
|
const _nRows = Math.min(nRows, data.length);
|
|
78
83
|
let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-spec.cjs","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n required?: boolean;\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {
|
|
1
|
+
{"version":3,"file":"column-spec.cjs","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n required?: boolean;\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {\n overrides = {},\n nRows = 10,\n omitColumns,\n includeColumns,\n } = options ?? {};\n\n if (data == null) return [];\n\n const _nRows = Math.min(nRows, data.length);\n\n let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));\n let filteredSpec = columnSpec.filter((col) => {\n if (omitColumns != null && omitColumns.includes(col.key)) {\n return false;\n }\n if (includeColumns != null && !includeColumns.includes(col.key)) {\n return false;\n }\n return true;\n });\n\n // Apply overrides\n return filteredSpec.map((col) => {\n let ovr = overrides[col.key];\n if (ovr == null) return col;\n if (typeof ovr === \"string\") {\n return { ...col, name: ovr };\n }\n return { ...col, ...ovr };\n });\n}\n"],"names":[],"mappings":"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"}
|
|
@@ -70,7 +70,12 @@ function generateDefaultColumnSpec(data, options = {}) {
|
|
|
70
70
|
return spec;
|
|
71
71
|
}
|
|
72
72
|
function generateColumnSpec(data, options) {
|
|
73
|
-
const {
|
|
73
|
+
const {
|
|
74
|
+
overrides = {},
|
|
75
|
+
nRows = 10,
|
|
76
|
+
omitColumns,
|
|
77
|
+
includeColumns
|
|
78
|
+
} = options ?? {};
|
|
74
79
|
if (data == null) return [];
|
|
75
80
|
const _nRows = Math.min(nRows, data.length);
|
|
76
81
|
let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-spec.js","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n required?: boolean;\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {
|
|
1
|
+
{"version":3,"file":"column-spec.js","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n required?: boolean;\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {\n overrides = {},\n nRows = 10,\n omitColumns,\n includeColumns,\n } = options ?? {};\n\n if (data == null) return [];\n\n const _nRows = Math.min(nRows, data.length);\n\n let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));\n let filteredSpec = columnSpec.filter((col) => {\n if (omitColumns != null && omitColumns.includes(col.key)) {\n return false;\n }\n if (includeColumns != null && !includeColumns.includes(col.key)) {\n return false;\n }\n return true;\n });\n\n // Apply overrides\n return filteredSpec.map((col) => {\n let ovr = overrides[col.key];\n if (ovr == null) return col;\n if (typeof ovr === \"string\") {\n return { ...col, name: ovr };\n }\n return { ...col, ...ovr };\n });\n}\n"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macrostrat/data-sheet",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "Scalable data sheet with optional editing capabilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"react": "^18.0.0||^19.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@blueprintjs/core": "^6.
|
|
50
|
-
"@blueprintjs/table": "^6.0.
|
|
49
|
+
"@blueprintjs/core": "^6.11.3",
|
|
50
|
+
"@blueprintjs/table": "^6.0.23",
|
|
51
51
|
"@macrostrat/color-utils": "^1.2.1",
|
|
52
|
-
"@macrostrat/data-components": "^1.
|
|
53
|
-
"@macrostrat/hyper": "^3.0
|
|
54
|
-
"@macrostrat/ui-components": "^5.0.
|
|
52
|
+
"@macrostrat/data-components": "^1.1.5",
|
|
53
|
+
"@macrostrat/hyper": "^3.1.0",
|
|
54
|
+
"@macrostrat/ui-components": "^5.0.7",
|
|
55
55
|
"@supabase/postgrest-js": "^1.17.7",
|
|
56
56
|
"@types/underscore": "^1.13.0",
|
|
57
57
|
"classnames": "^2.5.1",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"zustand-computed": "^2.1.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@macrostrat/web-components-bundler": "1.0.
|
|
64
|
+
"@macrostrat/web-components-bundler": "1.0.1"
|
|
65
65
|
},
|
|
66
66
|
"publishConfig": {
|
|
67
67
|
"access": "public"
|
package/src/components/colors.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Cell } from "@blueprintjs/table";
|
|
2
2
|
import { useInDarkMode } from "@macrostrat/ui-components";
|
|
3
|
-
import
|
|
4
|
-
import styles from "./main.module.sass";
|
|
3
|
+
import h from "./main.module.sass";
|
|
5
4
|
import { getLuminanceAdjustedColorScheme } from "@macrostrat/color-utils";
|
|
6
5
|
import { memoize } from "underscore";
|
|
7
6
|
import classNames from "classnames";
|
|
@@ -9,8 +8,6 @@ import { ColorPicker2 } from "@macrostrat/ui-components";
|
|
|
9
8
|
|
|
10
9
|
export const ColorPicker = ColorPicker2;
|
|
11
10
|
|
|
12
|
-
const h = hyper.styled(styles);
|
|
13
|
-
|
|
14
11
|
export function ColorCell({
|
|
15
12
|
value,
|
|
16
13
|
children,
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import hyper from "@macrostrat/hyper";
|
|
2
1
|
import { ErrorBoundary } from "@macrostrat/ui-components";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
2
|
+
import { PopoverNext } from "@blueprintjs/core";
|
|
3
|
+
import h from "./main.module.sass";
|
|
5
4
|
import { useRef, useState } from "react";
|
|
6
5
|
import { useSelector } from "../provider";
|
|
7
6
|
|
|
8
|
-
const h = hyper.styled(styles);
|
|
9
|
-
|
|
10
7
|
export function EditorPopup(props) {
|
|
11
8
|
const {
|
|
12
9
|
children,
|
|
@@ -14,7 +11,6 @@ export function EditorPopup(props) {
|
|
|
14
11
|
autoFocus,
|
|
15
12
|
valueViewer,
|
|
16
13
|
placement = "right-start",
|
|
17
|
-
minimal = true,
|
|
18
14
|
} = props;
|
|
19
15
|
|
|
20
16
|
const [isOpen, setIsOpen] = useState(autoFocus);
|
|
@@ -25,14 +21,14 @@ export function EditorPopup(props) {
|
|
|
25
21
|
const inlineEditor = h([
|
|
26
22
|
h("span.editor-value.bp6-table-cell", valueViewer),
|
|
27
23
|
h("input.hidden-editor", {
|
|
28
|
-
|
|
24
|
+
defaultValue: "",
|
|
29
25
|
autoFocus: true,
|
|
30
26
|
onKeyDown: keyHandler,
|
|
31
27
|
}),
|
|
32
28
|
]);
|
|
33
29
|
|
|
34
30
|
return h(
|
|
35
|
-
|
|
31
|
+
PopoverNext,
|
|
36
32
|
{
|
|
37
33
|
content: h(
|
|
38
34
|
"div.interaction-barrier",
|
|
@@ -56,10 +52,11 @@ export function EditorPopup(props) {
|
|
|
56
52
|
),
|
|
57
53
|
enforceFocus: false,
|
|
58
54
|
autoFocus: false,
|
|
59
|
-
minimal,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
},
|
|
55
|
+
animation: "minimal",
|
|
56
|
+
lazy: true,
|
|
57
|
+
// modifiers: {
|
|
58
|
+
// offset: { enabled: true, options: { offset: [0, 8] } },
|
|
59
|
+
// },
|
|
63
60
|
placement,
|
|
64
61
|
interactionKind: "hover-target",
|
|
65
62
|
isOpen,
|
package/src/core.ts
CHANGED
|
@@ -7,11 +7,10 @@ import {
|
|
|
7
7
|
} from "@blueprintjs/core";
|
|
8
8
|
import { Cell, Column, Region, RowHeaderCell, Table } from "@blueprintjs/table";
|
|
9
9
|
import "@blueprintjs/table/lib/css/table.css";
|
|
10
|
-
import hyper from "@macrostrat/hyper";
|
|
11
10
|
import update from "immutability-helper";
|
|
12
11
|
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
|
|
13
12
|
import { EditorPopup, DataSheetAction } from "./components";
|
|
14
|
-
import
|
|
13
|
+
import h from "./main.module.sass";
|
|
15
14
|
import {
|
|
16
15
|
DataSheetProvider,
|
|
17
16
|
DataSheetProviderProps,
|
|
@@ -23,8 +22,6 @@ import {
|
|
|
23
22
|
} from "./provider";
|
|
24
23
|
import { ColumnSpec } from "./utils";
|
|
25
24
|
|
|
26
|
-
const h = hyper.styled(styles);
|
|
27
|
-
|
|
28
25
|
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args
|
|
29
26
|
|
|
30
27
|
// TODO: add a "copy to selection" tool (the little square in the bottom right corner of a cell)
|
|
@@ -87,6 +84,11 @@ export function DataSheet<T>(props: DataSheetProps<T>) {
|
|
|
87
84
|
);
|
|
88
85
|
}
|
|
89
86
|
|
|
87
|
+
const deletedRowHeaderStyle = {
|
|
88
|
+
opacity: 0.5,
|
|
89
|
+
textDecoration: "line-through",
|
|
90
|
+
};
|
|
91
|
+
|
|
90
92
|
function _DataSheet<T>({
|
|
91
93
|
onVisibleCellsChange,
|
|
92
94
|
enableColumnReordering,
|
|
@@ -207,25 +209,25 @@ function _DataSheet<T>({
|
|
|
207
209
|
};
|
|
208
210
|
}
|
|
209
211
|
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
);
|
|
212
|
+
const onColumnsReordered = useSelector((state) => state.onColumnsReordered);
|
|
213
|
+
|
|
214
|
+
const children = useMemo(() => {
|
|
215
|
+
return columnSpec.map((col, colIndex) => {
|
|
216
|
+
return h(Column, {
|
|
217
|
+
name: col.name,
|
|
218
|
+
cellRenderer: (rowIndex) => {
|
|
219
|
+
const state = storeAPI.getState();
|
|
220
|
+
return basicCellRenderer<T>(
|
|
221
|
+
rowIndex,
|
|
222
|
+
colIndex,
|
|
223
|
+
col,
|
|
224
|
+
state,
|
|
225
|
+
autoFocusEditor,
|
|
226
|
+
);
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}, [columnSpec, storeAPI, autoFocusEditor]);
|
|
229
231
|
|
|
230
232
|
const onColumnWidthChanged = useSelector(
|
|
231
233
|
(state) => state.onColumnWidthChanged,
|
|
@@ -233,14 +235,10 @@ function _DataSheet<T>({
|
|
|
233
235
|
|
|
234
236
|
const rowHeaderCellRenderer = useCallback(
|
|
235
237
|
(rowIndex: number) => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
style = {
|
|
239
|
-
opacity: 0.5,
|
|
240
|
-
textDecoration: "line-through",
|
|
241
|
-
};
|
|
242
|
-
}
|
|
238
|
+
const style = deletedRows.has(rowIndex) ? deletedRowHeaderStyle : null;
|
|
239
|
+
|
|
243
240
|
return h(RowHeaderCell, {
|
|
241
|
+
enableRowReordering: false,
|
|
244
242
|
index: rowIndex,
|
|
245
243
|
name: `${rowIndex + 1}`,
|
|
246
244
|
style,
|
|
@@ -251,8 +249,6 @@ function _DataSheet<T>({
|
|
|
251
249
|
|
|
252
250
|
const onKeyDown = useSelector((state) => state.tableKeyHandler);
|
|
253
251
|
|
|
254
|
-
if (data == null) return null;
|
|
255
|
-
|
|
256
252
|
return h("div.data-sheet-container", { className, style }, [
|
|
257
253
|
h.if(editable)(DataSheetEditToolbar, {
|
|
258
254
|
onSaveData: _onSaveData,
|
|
@@ -263,7 +259,6 @@ function _DataSheet<T>({
|
|
|
263
259
|
"div.data-sheet-holder",
|
|
264
260
|
{ onKeyDown },
|
|
265
261
|
h(
|
|
266
|
-
// @ts-expect-error
|
|
267
262
|
Table,
|
|
268
263
|
{
|
|
269
264
|
ref,
|
|
@@ -271,7 +266,7 @@ function _DataSheet<T>({
|
|
|
271
266
|
className: "data-sheet",
|
|
272
267
|
enableFocusedCell,
|
|
273
268
|
enableColumnReordering,
|
|
274
|
-
|
|
269
|
+
onColumnsReordered,
|
|
275
270
|
focusedCell,
|
|
276
271
|
selectedRegions,
|
|
277
272
|
defaultRowHeight: rowHeight,
|
|
@@ -279,6 +274,9 @@ function _DataSheet<T>({
|
|
|
279
274
|
columnWidths,
|
|
280
275
|
onColumnWidthChanged,
|
|
281
276
|
onSelection,
|
|
277
|
+
renderMode: "batch",
|
|
278
|
+
enableRowReordering: false,
|
|
279
|
+
enableRowResizing: false,
|
|
282
280
|
// The cell renderer is memoized internally based on these data dependencies
|
|
283
281
|
cellRendererDependencies: [
|
|
284
282
|
data,
|
|
@@ -312,10 +310,8 @@ function basicCellRenderer<T>(
|
|
|
312
310
|
const loading = row == null;
|
|
313
311
|
const col = columnSpec;
|
|
314
312
|
|
|
315
|
-
const focusedCell = state.focusedCell;
|
|
316
313
|
const _topLeftCell = state.topLeftCell;
|
|
317
314
|
const onCellEdited = state.onCellEdited;
|
|
318
|
-
const clearSelection = state.clearSelection;
|
|
319
315
|
|
|
320
316
|
const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
|
|
321
317
|
const _renderedValue = col.valueRenderer?.(value) ?? value;
|
|
@@ -329,8 +325,8 @@ function basicCellRenderer<T>(
|
|
|
329
325
|
};
|
|
330
326
|
}
|
|
331
327
|
|
|
332
|
-
const focused =
|
|
333
|
-
|
|
328
|
+
//const focused =
|
|
329
|
+
// focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
|
|
334
330
|
|
|
335
331
|
const editable = (col.editable ?? state.editable) && !isDeleted;
|
|
336
332
|
|
|
@@ -475,25 +471,17 @@ function basicCellRenderer<T>(
|
|
|
475
471
|
},
|
|
476
472
|
[
|
|
477
473
|
cellContents,
|
|
478
|
-
h.if(editable && isSingleCellSelection)(DragHandle,
|
|
479
|
-
focusedCell,
|
|
480
|
-
}),
|
|
474
|
+
h.if(editable && isSingleCellSelection)(DragHandle),
|
|
481
475
|
hiddenInput,
|
|
482
476
|
],
|
|
483
477
|
);
|
|
484
478
|
}
|
|
485
479
|
|
|
486
|
-
function DragHandle(
|
|
480
|
+
function DragHandle() {
|
|
487
481
|
// TODO: we might want to drag multiple columns in some cases
|
|
488
482
|
// This should be on the last cell of a selection
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
return h("div.corner-drag-handle", {
|
|
492
|
-
onMouseDown(e) {
|
|
493
|
-
onDragValue(focusedCell);
|
|
494
|
-
e.preventDefault();
|
|
495
|
-
},
|
|
496
|
-
});
|
|
483
|
+
const onMouseDown = useSelector((state) => state.onDragValue);
|
|
484
|
+
return h("div.corner-drag-handle", { onMouseDown });
|
|
497
485
|
}
|
|
498
486
|
|
|
499
487
|
function DataSheetEditToolbar({ onSaveData, onDeleteRows }) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Lazy loading of data from a PostgREST endpoint */
|
|
2
2
|
|
|
3
3
|
import { useAsyncEffect } from "@macrostrat/ui-components";
|
|
4
|
-
import { debounce } from "underscore";
|
|
4
|
+
import { debounce, range } from "underscore";
|
|
5
5
|
import { useCallback, useMemo, useReducer, useRef, useEffect } from "react";
|
|
6
6
|
import update, { Spec } from "immutability-helper";
|
|
7
7
|
|
|
@@ -337,3 +337,153 @@ function indexOfFirstNullInRegion(
|
|
|
337
337
|
}
|
|
338
338
|
return null;
|
|
339
339
|
}
|
|
340
|
+
|
|
341
|
+
export function useTestLazyLoader(config: LazyLoaderOptions = {}) {
|
|
342
|
+
const initialState: LazyLoaderState<any> = {
|
|
343
|
+
data: [],
|
|
344
|
+
loading: false,
|
|
345
|
+
error: null,
|
|
346
|
+
visibleRegion: { rowIndexStart: 0, rowIndexEnd: 0 },
|
|
347
|
+
initialized: false,
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const [state, dispatch] = useReducer(lazyLoadingReducer, initialState);
|
|
351
|
+
const { data, loading } = state;
|
|
352
|
+
|
|
353
|
+
useAsyncEffect(async () => {
|
|
354
|
+
testDataLoader(config, state, dispatch);
|
|
355
|
+
}, [
|
|
356
|
+
data,
|
|
357
|
+
state.visibleRegion.rowIndexStart,
|
|
358
|
+
state.visibleRegion.rowIndexEnd,
|
|
359
|
+
]);
|
|
360
|
+
|
|
361
|
+
// Reference to hold onto the scroll position
|
|
362
|
+
const ref = useRef(null);
|
|
363
|
+
|
|
364
|
+
const onScroll = useCallback(
|
|
365
|
+
debounce((visibleCells: RowRegion) => {
|
|
366
|
+
if (
|
|
367
|
+
visibleCells.rowIndexEnd == ref.current?.rowIndexEnd &&
|
|
368
|
+
visibleCells.rowIndexStart == ref.current?.rowIndexStart
|
|
369
|
+
) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
console.log("Visible cells changed", visibleCells);
|
|
373
|
+
dispatch({
|
|
374
|
+
type: "set-visible",
|
|
375
|
+
region: visibleCells,
|
|
376
|
+
});
|
|
377
|
+
ref.current = visibleCells;
|
|
378
|
+
}, 500),
|
|
379
|
+
[dispatch],
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
data,
|
|
384
|
+
loading,
|
|
385
|
+
onScroll,
|
|
386
|
+
dispatch,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function testDataLoader(
|
|
391
|
+
config: { chunkSize: number },
|
|
392
|
+
state: LazyLoaderState<any>,
|
|
393
|
+
dispatch,
|
|
394
|
+
) {
|
|
395
|
+
const sortKey = "id";
|
|
396
|
+
const rowIndex = indexOfFirstNullInRegion(state.data, state.visibleRegion);
|
|
397
|
+
if (state.loading || rowIndex == null) {
|
|
398
|
+
if (state.initialized) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const chunkSize = 10;
|
|
404
|
+
|
|
405
|
+
let cfg: QueryConfig = {
|
|
406
|
+
//...rest,
|
|
407
|
+
limit: chunkSize,
|
|
408
|
+
offset: null,
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// Allows random seeking
|
|
412
|
+
const isInitialQuery = !state.initialized;
|
|
413
|
+
if (isInitialQuery) {
|
|
414
|
+
cfg.count = "exact";
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// This only works for forward queries
|
|
418
|
+
if (!isInitialQuery) {
|
|
419
|
+
cfg.after = state.data[rowIndex - 1]?.[sortKey];
|
|
420
|
+
if (cfg.after == null) {
|
|
421
|
+
cfg.offset = rowIndex;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
dispatch({ type: "start-loading" });
|
|
426
|
+
|
|
427
|
+
const query = testQueryFunc(cfg);
|
|
428
|
+
query.then((res) => {
|
|
429
|
+
console.log(res);
|
|
430
|
+
const { data, count } = res;
|
|
431
|
+
dispatch({
|
|
432
|
+
type: "loaded",
|
|
433
|
+
data,
|
|
434
|
+
offset: rowIndex,
|
|
435
|
+
totalSize: count,
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
interface QueryParams {
|
|
441
|
+
count: "exact" | "estimated";
|
|
442
|
+
limit: number;
|
|
443
|
+
offset?: number;
|
|
444
|
+
after?: number;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const testQueryFunc = async (
|
|
448
|
+
cfg: QueryParams,
|
|
449
|
+
): Promise<{
|
|
450
|
+
data: any[];
|
|
451
|
+
count: number | null;
|
|
452
|
+
}> => {
|
|
453
|
+
console.log(cfg);
|
|
454
|
+
let offset = null;
|
|
455
|
+
if (cfg.after) {
|
|
456
|
+
offset = cfg.after;
|
|
457
|
+
} else if (cfg.offset) {
|
|
458
|
+
offset = cfg.offset;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let count = cfg.limit;
|
|
462
|
+
const totalCount = 50000;
|
|
463
|
+
count = Math.min(count, totalCount - offset);
|
|
464
|
+
|
|
465
|
+
// wait for a little bit before returning
|
|
466
|
+
await sleep(100);
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
data: buildSyntheticData(offset, count),
|
|
470
|
+
count: cfg.count != null ? 50000 : undefined,
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
async function sleep(ms: number) {
|
|
475
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function buildSyntheticData(offset: number, count: number | null) {
|
|
479
|
+
console.log(
|
|
480
|
+
`Getting data at indices between ${offset} and ${offset + count}`,
|
|
481
|
+
);
|
|
482
|
+
return range(offset, offset + count).map((i) => {
|
|
483
|
+
const id = i + 1;
|
|
484
|
+
return {
|
|
485
|
+
id,
|
|
486
|
+
name: `This is some long text content in row ${id}`,
|
|
487
|
+
};
|
|
488
|
+
});
|
|
489
|
+
}
|
|
@@ -7,6 +7,7 @@ import { PostgrestOrder, usePostgRESTLazyLoader } from "./data-loaders";
|
|
|
7
7
|
import { Spinner, InputGroup } from "@blueprintjs/core";
|
|
8
8
|
|
|
9
9
|
export * from "./data-loaders";
|
|
10
|
+
export * from "./lazy-loader-table";
|
|
10
11
|
import { useCallback, useState, useRef } from "react";
|
|
11
12
|
import {
|
|
12
13
|
ErrorBoundary,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import hyper from "@macrostrat/hyper";
|
|
2
|
+
import styles from "./main.module.sass";
|
|
3
|
+
import { DataSheet } from "../core";
|
|
4
|
+
import { useTestLazyLoader } from "./data-loaders";
|
|
5
|
+
import { Spinner } from "@blueprintjs/core";
|
|
6
|
+
|
|
7
|
+
const h = hyper.styled(styles);
|
|
8
|
+
|
|
9
|
+
export function TestLazyLoaderTableView<T>({ ...rest }) {
|
|
10
|
+
const { data, onScroll } = useTestLazyLoader();
|
|
11
|
+
|
|
12
|
+
if (data == null) {
|
|
13
|
+
return h(Spinner);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return h("div.data-sheet-outer", [
|
|
17
|
+
h(DataSheet, {
|
|
18
|
+
...rest,
|
|
19
|
+
data,
|
|
20
|
+
editable: false,
|
|
21
|
+
onVisibleCellsChange: onScroll,
|
|
22
|
+
}),
|
|
23
|
+
]);
|
|
24
|
+
}
|
package/src/provider.ts
CHANGED
|
@@ -44,7 +44,7 @@ type StateUpdater<T> = T[] | ((state: T[]) => T[]);
|
|
|
44
44
|
|
|
45
45
|
export interface DataSheetStoreMain<T> extends DataSheetVals<T> {
|
|
46
46
|
setSelection(selection: Region[]): void;
|
|
47
|
-
onDragValue(
|
|
47
|
+
onDragValue(event: MouseEvent): void;
|
|
48
48
|
setUpdatedData(data: StateUpdater<T>): void;
|
|
49
49
|
onCellEdited(rowIndex: number, columnName: string, value: any): void;
|
|
50
50
|
onSelectionEdited(value: any): void;
|
|
@@ -252,8 +252,11 @@ export function DataSheetProvider<T>({
|
|
|
252
252
|
// Right now we don't store this in the state
|
|
253
253
|
visibleCellsRef.current = visibleCells;
|
|
254
254
|
},
|
|
255
|
-
onDragValue(
|
|
256
|
-
set(
|
|
255
|
+
onDragValue(event: MouseEvent) {
|
|
256
|
+
set((state) => {
|
|
257
|
+
return { fillValueBaseCell: state.focusedCell };
|
|
258
|
+
});
|
|
259
|
+
event.preventDefault();
|
|
257
260
|
},
|
|
258
261
|
onCellEdited(rowIndex: number, columnName: string, value: any) {
|
|
259
262
|
set((state) => {
|
package/src/utils/column-spec.ts
CHANGED
|
@@ -133,7 +133,12 @@ export function generateColumnSpec<T>(
|
|
|
133
133
|
options: ColumnSpecOptions<T>,
|
|
134
134
|
): ColumnSpec[] {
|
|
135
135
|
/** Generate a column spec from a dataset */
|
|
136
|
-
const {
|
|
136
|
+
const {
|
|
137
|
+
overrides = {},
|
|
138
|
+
nRows = 10,
|
|
139
|
+
omitColumns,
|
|
140
|
+
includeColumns,
|
|
141
|
+
} = options ?? {};
|
|
137
142
|
|
|
138
143
|
if (data == null) return [];
|
|
139
144
|
|