@macrostrat/data-sheet 3.0.3 → 3.1.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 +29 -0
- package/dist/cell-renderer.cjs +162 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +8 -0
- package/dist/cell-renderer.js +162 -0
- package/dist/cell-renderer.js.map +1 -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/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/core.cjs +67 -207
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +4 -9
- package/dist/core.js +48 -185
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +9 -9
- package/dist/index.cjs +16 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +17 -8
- package/dist/index.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 +2 -1
- package/dist/postgrest-table/index.js +2 -0
- 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/provider.cjs +59 -336
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.ts +11 -65
- package/dist/provider.js +58 -336
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +54 -0
- 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/dist/utils/key-handling.cjs +26 -1
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -2
- package/dist/utils/key-handling.js +27 -2
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +320 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +318 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +7 -6
- package/src/cell-renderer.ts +217 -0
- 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 +82 -254
- package/src/index.ts +3 -0
- package/src/postgrest-table/data-loaders.ts +151 -1
- package/src/postgrest-table/index.ts +2 -1
- package/src/postgrest-table/lazy-loader-table.ts +24 -0
- package/src/provider.ts +66 -483
- package/src/types.ts +66 -0
- package/src/utils/column-spec.ts +6 -1
- package/src/utils/key-handling.ts +31 -5
- package/src/zustand-store.ts +377 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { range } from "./utils/index.js";
|
|
2
|
+
import update from "immutability-helper";
|
|
3
|
+
function createZustandStore(set, get) {
|
|
4
|
+
return {
|
|
5
|
+
data: [],
|
|
6
|
+
columnSpec: [],
|
|
7
|
+
defaultColumnWidth: 150,
|
|
8
|
+
editable: false,
|
|
9
|
+
deletedRows: /* @__PURE__ */ new Set(),
|
|
10
|
+
selection: [],
|
|
11
|
+
fillValueBaseCell: null,
|
|
12
|
+
updatedData: [],
|
|
13
|
+
focusedCell: null,
|
|
14
|
+
topLeftCell: null,
|
|
15
|
+
initialized: false,
|
|
16
|
+
tableRef: null,
|
|
17
|
+
visibleCellsRef: null,
|
|
18
|
+
// This is a placeholder
|
|
19
|
+
enableColumnReordering: false,
|
|
20
|
+
setSelection(selection) {
|
|
21
|
+
set(updateSelection(selection));
|
|
22
|
+
},
|
|
23
|
+
columnWidthsIndex: /* @__PURE__ */ new Map(),
|
|
24
|
+
moveFocusedCell(direction) {
|
|
25
|
+
set((state) => {
|
|
26
|
+
const { topLeftCell: topLeftCell2 } = state;
|
|
27
|
+
if (topLeftCell2 == null) return {};
|
|
28
|
+
let { col, row } = topLeftCell2;
|
|
29
|
+
switch (direction) {
|
|
30
|
+
case "up":
|
|
31
|
+
row = Math.max(0, row - 1);
|
|
32
|
+
break;
|
|
33
|
+
case "down":
|
|
34
|
+
row = Math.min(
|
|
35
|
+
row + 1,
|
|
36
|
+
Math.max(state.data.length, state.updatedData.length) - 1
|
|
37
|
+
);
|
|
38
|
+
break;
|
|
39
|
+
case "left":
|
|
40
|
+
col = Math.max(0, col - 1);
|
|
41
|
+
break;
|
|
42
|
+
case "right":
|
|
43
|
+
col = Math.min(col + 1, state.columnSpec.length - 1);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
const region = { cols: [col, col], rows: [row, row] };
|
|
47
|
+
return updateSelection([region]);
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
addRow(row = {}) {
|
|
51
|
+
set((state) => {
|
|
52
|
+
const { updatedData, data, selection } = state;
|
|
53
|
+
const lastRowIndex = getLastRowIndex(selection) ?? Math.max(data.length, updatedData.length) - 1;
|
|
54
|
+
const spec = {
|
|
55
|
+
$splice: [[lastRowIndex + 1, 0, row]]
|
|
56
|
+
};
|
|
57
|
+
console.log(spec);
|
|
58
|
+
return {
|
|
59
|
+
updatedData: update(updatedData, spec)
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
setUpdatedData(data) {
|
|
64
|
+
if (Array.isArray(data)) {
|
|
65
|
+
set({ updatedData: data });
|
|
66
|
+
} else {
|
|
67
|
+
set((state) => {
|
|
68
|
+
return { updatedData: data(state.updatedData) };
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
deleteSelectedRows() {
|
|
73
|
+
set((state) => {
|
|
74
|
+
const { selection, deletedRows } = state;
|
|
75
|
+
const rowIndices = getRowIndices(selection);
|
|
76
|
+
const newDeletedRows = new Set(deletedRows);
|
|
77
|
+
for (const rowIndex of rowIndices) {
|
|
78
|
+
newDeletedRows.add(rowIndex);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
deletedRows: newDeletedRows,
|
|
82
|
+
selection: [],
|
|
83
|
+
focusedCell: null,
|
|
84
|
+
topLeftCell: null,
|
|
85
|
+
fillValueBaseCell: null
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
resetChanges() {
|
|
90
|
+
set((state) => ({
|
|
91
|
+
updatedData: [],
|
|
92
|
+
deletedRows: /* @__PURE__ */ new Set(),
|
|
93
|
+
columnWidths: /* @__PURE__ */ new Map(),
|
|
94
|
+
selection: [],
|
|
95
|
+
focusedCell: null,
|
|
96
|
+
topLeftCell: null,
|
|
97
|
+
fillValueBaseCell: null
|
|
98
|
+
}));
|
|
99
|
+
},
|
|
100
|
+
onColumnWidthChanged(columnIx, newWidth) {
|
|
101
|
+
set((state) => {
|
|
102
|
+
const { columnSpec, columnWidthsIndex } = state;
|
|
103
|
+
const colKey = columnSpec[columnIx].key;
|
|
104
|
+
const newColumnWidths = new Map(columnWidthsIndex);
|
|
105
|
+
newColumnWidths.set(colKey, newWidth);
|
|
106
|
+
return { columnWidthsIndex: newColumnWidths };
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
setVisibleCells(visibleCells) {
|
|
110
|
+
const ref = get().visibleCellsRef;
|
|
111
|
+
if (ref == null) return;
|
|
112
|
+
ref.current = visibleCells;
|
|
113
|
+
},
|
|
114
|
+
onDragValue(event) {
|
|
115
|
+
set((state) => {
|
|
116
|
+
return { fillValueBaseCell: state.focusedCell };
|
|
117
|
+
});
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
},
|
|
120
|
+
onCellEdited(rowIndex, columnName, value) {
|
|
121
|
+
set((state) => {
|
|
122
|
+
const { editable, updatedData, data } = state;
|
|
123
|
+
if (!editable) return {};
|
|
124
|
+
let rowSpec;
|
|
125
|
+
if (value !== data[rowIndex]?.[columnName]) {
|
|
126
|
+
const rowOp = updatedData[rowIndex] != null ? "$merge" : "$set";
|
|
127
|
+
rowSpec = { [rowOp]: { [columnName]: value } };
|
|
128
|
+
} else {
|
|
129
|
+
rowSpec = { $unset: [columnName] };
|
|
130
|
+
}
|
|
131
|
+
const spec = { [rowIndex]: rowSpec };
|
|
132
|
+
return { updatedData: update(updatedData, spec) };
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
initialize(props) {
|
|
136
|
+
set({ ...props, initialized: true });
|
|
137
|
+
},
|
|
138
|
+
clearSelection() {
|
|
139
|
+
set((state) => {
|
|
140
|
+
const { selection, updatedData, columnSpec, data } = state;
|
|
141
|
+
let spec = {};
|
|
142
|
+
for (const region of selection) {
|
|
143
|
+
console.log("Clearing region", region);
|
|
144
|
+
const { cols, rows } = region;
|
|
145
|
+
const rowRange = range(rows ?? [0, updatedData.length - 1]);
|
|
146
|
+
const colRange = range(cols ?? [0, columnSpec.length - 1]);
|
|
147
|
+
for (const row of rowRange) {
|
|
148
|
+
let vals = {};
|
|
149
|
+
for (const col of colRange) {
|
|
150
|
+
const key = columnSpec[col].key;
|
|
151
|
+
const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];
|
|
152
|
+
if (currentValue != null && currentValue !== "") {
|
|
153
|
+
vals[key] = "";
|
|
154
|
+
}
|
|
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) {
|
|
166
|
+
set((state) => {
|
|
167
|
+
if (selectionEquals(selection, state.selection) && singleFocusedCell(selection) == null) {
|
|
168
|
+
return {
|
|
169
|
+
selection: [],
|
|
170
|
+
focusedCell: null,
|
|
171
|
+
topLeftCell: null,
|
|
172
|
+
fillValueBaseCell: null
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
let spec = updateSelection(selection);
|
|
176
|
+
if (state.fillValueBaseCell != null) {
|
|
177
|
+
spec.updatedData = fillValues(state, selection);
|
|
178
|
+
}
|
|
179
|
+
return spec;
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
onSelectionEdited(value) {
|
|
183
|
+
set((state) => {
|
|
184
|
+
const { selection, updatedData, columnSpec, editable } = state;
|
|
185
|
+
if (!editable) return {};
|
|
186
|
+
let spec = {};
|
|
187
|
+
for (const region of selection) {
|
|
188
|
+
const { cols, rows } = region;
|
|
189
|
+
const rowRange = range(rows ?? [0, updatedData.length - 1]);
|
|
190
|
+
const colRange = range(cols ?? [0, columnSpec.length - 1]);
|
|
191
|
+
for (const row of rowRange) {
|
|
192
|
+
let vals = {};
|
|
193
|
+
for (const col of colRange) {
|
|
194
|
+
const key = columnSpec[col].key;
|
|
195
|
+
vals[key] = value;
|
|
196
|
+
}
|
|
197
|
+
let op = updatedData[row] == null ? "$set" : "$merge";
|
|
198
|
+
spec[row] = { [op]: vals };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
updatedData: update(updatedData, spec)
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
},
|
|
206
|
+
onColumnsReordered(oldIndex, newIndex, length) {
|
|
207
|
+
set((state) => {
|
|
208
|
+
if (!state.enableColumnReordering) return {};
|
|
209
|
+
const { columnSpec } = state;
|
|
210
|
+
const newSpec = [...columnSpec];
|
|
211
|
+
const removed = newSpec.splice(oldIndex, length);
|
|
212
|
+
newSpec.splice(newIndex, 0, ...removed);
|
|
213
|
+
return { columnSpec: newSpec };
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
scrollToRow(rowIndex) {
|
|
217
|
+
const tableRef = get().tableRef;
|
|
218
|
+
if (tableRef.current == null) return;
|
|
219
|
+
tableRef.current.scrollToRegion({
|
|
220
|
+
rows: [rowIndex, rowIndex]
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function updateSelection(selection) {
|
|
226
|
+
const focusedCell = singleFocusedCell(selection);
|
|
227
|
+
let spec = {
|
|
228
|
+
selection,
|
|
229
|
+
focusedCell,
|
|
230
|
+
topLeftCell: topLeftCell(selection)
|
|
231
|
+
};
|
|
232
|
+
if (focusedCell != null) {
|
|
233
|
+
spec.fillValueBaseCell = null;
|
|
234
|
+
}
|
|
235
|
+
return spec;
|
|
236
|
+
}
|
|
237
|
+
function getLastRowIndex(regions) {
|
|
238
|
+
if (regions == null || regions.length === 0) return null;
|
|
239
|
+
let lastRow = -1;
|
|
240
|
+
for (const region of regions) {
|
|
241
|
+
const { rows } = region;
|
|
242
|
+
if (rows == null || rows.length !== 2) continue;
|
|
243
|
+
lastRow = Math.max(lastRow, rows[1]);
|
|
244
|
+
}
|
|
245
|
+
if (lastRow < 0) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
return lastRow;
|
|
249
|
+
}
|
|
250
|
+
function getRowIndices(regions) {
|
|
251
|
+
if (regions == null || regions.length === 0) return [];
|
|
252
|
+
const rowIndices = /* @__PURE__ */ new Set();
|
|
253
|
+
for (const region of regions) {
|
|
254
|
+
const { rows } = region;
|
|
255
|
+
if (rows == null || rows.length !== 2) continue;
|
|
256
|
+
for (let i = rows[0]; i <= rows[1]; i++) {
|
|
257
|
+
rowIndices.add(i);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return Array.from(rowIndices).sort((a, b) => a - b);
|
|
261
|
+
}
|
|
262
|
+
function topLeftCell(regions, requireSolitaryCell = false) {
|
|
263
|
+
if (regions == null) return null;
|
|
264
|
+
const [region] = regions;
|
|
265
|
+
if (region == null) return null;
|
|
266
|
+
const { cols, rows } = region;
|
|
267
|
+
if (cols == null || rows == null) return null;
|
|
268
|
+
if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))
|
|
269
|
+
return null;
|
|
270
|
+
return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };
|
|
271
|
+
}
|
|
272
|
+
function selectionEquals(a, b) {
|
|
273
|
+
if (a.length !== b.length) return false;
|
|
274
|
+
for (let i = 0; i < a.length; i++) {
|
|
275
|
+
const regionA = a[i];
|
|
276
|
+
const regionB = b[i];
|
|
277
|
+
const colsA = regionA.cols ?? [];
|
|
278
|
+
const colsB = regionB.cols ?? [];
|
|
279
|
+
const rowsA = regionA.rows ?? [];
|
|
280
|
+
const rowsB = regionB.rows ?? [];
|
|
281
|
+
if (colsA.length !== colsB.length || rowsA.length !== rowsB.length || colsA[0] !== colsB[0] || colsA[1] !== colsB[1] || rowsA[0] !== rowsB[0] || rowsA[1] !== rowsB[1]) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
function singleFocusedCell(sel) {
|
|
288
|
+
if (sel?.length !== 1) return null;
|
|
289
|
+
return topLeftCell(sel, true);
|
|
290
|
+
}
|
|
291
|
+
function fillValues(state, selection) {
|
|
292
|
+
const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
|
|
293
|
+
let regions = selection.map((region) => {
|
|
294
|
+
const { cols, rows } = region;
|
|
295
|
+
const [col2] = cols;
|
|
296
|
+
return { cols: [col2, col2], rows };
|
|
297
|
+
});
|
|
298
|
+
if (!editable || fillValueBaseCell == null) return updatedData;
|
|
299
|
+
const { col, row } = fillValueBaseCell;
|
|
300
|
+
const key = columnSpec[col].key;
|
|
301
|
+
const value = updatedData[row]?.[key] ?? data[row][key];
|
|
302
|
+
const spec = {};
|
|
303
|
+
for (const region of regions) {
|
|
304
|
+
const { cols, rows } = region;
|
|
305
|
+
for (const row2 of range(rows)) {
|
|
306
|
+
let op = updatedData[row2] == null ? "$set" : "$merge";
|
|
307
|
+
spec[row2] = { [op]: { [key]: value } };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return update(updatedData, spec);
|
|
311
|
+
}
|
|
312
|
+
export {
|
|
313
|
+
createZustandStore,
|
|
314
|
+
singleFocusedCell,
|
|
315
|
+
topLeftCell,
|
|
316
|
+
updateSelection
|
|
317
|
+
};
|
|
318
|
+
//# sourceMappingURL=zustand-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zustand-store.js","sources":["../src/zustand-store.ts"],"sourcesContent":["import {\n DataSheetState,\n DataSheetStore,\n DataSheetStoreMain,\n StateUpdater,\n VisibleCells,\n} from \"./types.ts\";\nimport type { FocusedCellCoordinates, Region } from \"@blueprintjs/table\";\nimport { range } from \"./utils\";\nimport update, { Spec } from \"immutability-helper\";\n\nexport function createZustandStore<T>(set, get): DataSheetStoreMain<T> {\n return {\n data: [],\n columnSpec: [],\n defaultColumnWidth: 150,\n editable: false,\n deletedRows: new Set<number>(),\n selection: [],\n fillValueBaseCell: null,\n updatedData: [],\n focusedCell: null,\n topLeftCell: null,\n initialized: false,\n tableRef: null,\n visibleCellsRef: null,\n // This is a placeholder\n enableColumnReordering: false,\n setSelection(selection: Region[]) {\n set(updateSelection(selection));\n },\n columnWidthsIndex: new Map<string, number>(),\n moveFocusedCell(direction: \"up\" | \"down\" | \"left\" | \"right\") {\n set((state) => {\n const { topLeftCell } = state;\n if (topLeftCell == null) return {};\n let { col, row } = topLeftCell;\n switch (direction) {\n case \"up\":\n row = Math.max(0, row - 1);\n break;\n case \"down\":\n row = Math.min(\n row + 1,\n Math.max(state.data.length, state.updatedData.length) - 1,\n );\n break;\n case \"left\":\n col = Math.max(0, col - 1);\n break;\n case \"right\":\n col = Math.min(col + 1, state.columnSpec.length - 1);\n break;\n }\n const region: Region = { cols: [col, col], rows: [row, row] };\n return updateSelection([region]);\n });\n },\n addRow(row: Partial<T> = {} as T) {\n /** Add a new row. If there is a selection, use the last row index to determine\n * where to insert the new row. Otherwise, append to the end of the data. */\n set((state) => {\n const { updatedData, data, selection } = state;\n const lastRowIndex =\n getLastRowIndex(selection) ??\n Math.max(data.length, updatedData.length) - 1;\n // If there is a selection, insert the new row after the last selected row\n const spec: Spec<any> = {\n $splice: [[lastRowIndex + 1, 0, row]],\n };\n console.log(spec);\n\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n setUpdatedData(data: StateUpdater<T>) {\n if (Array.isArray(data)) {\n set({ updatedData: data });\n } else {\n set((state) => {\n return { updatedData: data(state.updatedData) };\n });\n }\n },\n deleteSelectedRows() {\n // Remove selected rows from the data and updatedData arrays\n set((state) => {\n const { selection, deletedRows } = state;\n const rowIndices = getRowIndices(selection);\n\n // Delete rows from both updatedData and data\n const newDeletedRows = new Set(deletedRows);\n for (const rowIndex of rowIndices) {\n newDeletedRows.add(rowIndex);\n }\n\n // Remove selected rows and reset selection\n return {\n deletedRows: newDeletedRows,\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n };\n });\n },\n resetChanges() {\n // Reset the updated data to the initial data\n set((state) => ({\n updatedData: [],\n deletedRows: new Set<number>(),\n columnWidths: new Map<string, number>(),\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n }));\n },\n onColumnWidthChanged(columnIx: number, newWidth: number) {\n set((state) => {\n const { columnSpec, columnWidthsIndex } = state;\n const colKey = columnSpec[columnIx].key;\n const newColumnWidths = new Map(columnWidthsIndex);\n newColumnWidths.set(colKey, newWidth);\n return { columnWidthsIndex: newColumnWidths };\n });\n },\n setVisibleCells(visibleCells: VisibleCells) {\n // Visible cells are used for infinite scrolling\n // Right now we don't store this in the state\n const ref = get().visibleCellsRef;\n if (ref == null) return;\n ref.current = visibleCells;\n },\n onDragValue(event: MouseEvent) {\n set((state) => {\n return { fillValueBaseCell: state.focusedCell };\n });\n event.preventDefault();\n },\n onCellEdited(rowIndex: number, columnName: string, value: any) {\n set((state) => {\n const { editable, updatedData, data } = state;\n if (!editable) return {};\n let rowSpec: any;\n\n // Check to see if the new value is the same as the old one\n if (value !== data[rowIndex]?.[columnName]) {\n const rowOp = updatedData[rowIndex] != null ? \"$merge\" : \"$set\";\n rowSpec = { [rowOp]: { [columnName]: value } };\n } else {\n rowSpec = { $unset: [columnName] };\n }\n const spec: Spec<T[]> = { [rowIndex]: rowSpec };\n return { updatedData: update(updatedData, spec) };\n });\n },\n initialize(props: Partial<DataSheetStoreMain<T>>) {\n set({ ...props, initialized: true });\n },\n clearSelection() {\n set((state) => {\n // Delete all selected cells\n const { selection, updatedData, columnSpec, data } = state;\n let spec = {};\n for (const region of selection) {\n console.log(\"Clearing region\", region);\n const { cols, rows } = region;\n const rowRange = range(rows ?? [0, updatedData.length - 1]);\n const colRange = range(cols ?? [0, columnSpec.length - 1]);\n for (const row of rowRange) {\n let vals = {};\n for (const col of colRange) {\n const key = columnSpec[col].key;\n const currentValue = updatedData[row]?.[key] ?? data[row]?.[key];\n if (currentValue != null && currentValue !== \"\") {\n vals[key] = \"\";\n }\n }\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: vals };\n }\n }\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n onSelection(selection: Region[]) {\n set((state) => {\n if (\n selectionEquals(selection, state.selection) &&\n singleFocusedCell(selection) == null // Only if we're in a multi-cell selection mode\n ) {\n // If the selection is the same as the current selection, remove the selection.\n // In practice this only happens for whole-row and whole-column selections\n return {\n selection: [],\n focusedCell: null,\n topLeftCell: null,\n fillValueBaseCell: null,\n };\n }\n\n let spec = updateSelection(selection);\n if (state.fillValueBaseCell != null) {\n spec.updatedData = fillValues(state, selection);\n }\n return spec;\n });\n },\n onSelectionEdited(value: any) {\n // Apply the same value to all selected cells\n set((state) => {\n const { selection, updatedData, columnSpec, editable } = state;\n if (!editable) return {};\n let spec = {};\n for (const region of selection) {\n const { cols, rows } = region;\n const rowRange = range(rows ?? [0, updatedData.length - 1]);\n const colRange = range(cols ?? [0, columnSpec.length - 1]);\n for (const row of rowRange) {\n let vals = {};\n for (const col of colRange) {\n const key = columnSpec[col].key;\n vals[key] = value;\n }\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: vals };\n }\n }\n return {\n updatedData: update(updatedData, spec),\n };\n });\n },\n onColumnsReordered(oldIndex: number, newIndex: number, length: number) {\n set((state) => {\n if (!state.enableColumnReordering) return {};\n const { columnSpec } = state;\n const newSpec = [...columnSpec];\n const removed = newSpec.splice(oldIndex, length);\n newSpec.splice(newIndex, 0, ...removed);\n return { columnSpec: newSpec };\n });\n },\n scrollToRow(rowIndex: number) {\n const tableRef = get().tableRef;\n if (tableRef.current == null) return;\n tableRef.current.scrollToRegion({\n rows: [rowIndex, rowIndex],\n });\n },\n };\n}\n\nexport function updateSelection<T>(selection: Region[]) {\n const focusedCell = singleFocusedCell(selection);\n let spec: Partial<DataSheetState<T>> = {\n selection,\n focusedCell,\n topLeftCell: topLeftCell(selection),\n };\n if (focusedCell != null) {\n spec.fillValueBaseCell = null;\n }\n return spec;\n}\n\nfunction getLastRowIndex(regions: Region[]): number | null {\n /** Get the last row index from a selection of regions */\n if (regions == null || regions.length === 0) return null;\n let lastRow = -1;\n for (const region of regions) {\n const { rows } = region;\n if (rows == null || rows.length !== 2) continue;\n lastRow = Math.max(lastRow, rows[1]);\n }\n if (lastRow < 0) {\n /** If no valid rows are found, return null */\n return null;\n }\n return lastRow;\n}\n\nfunction getRowIndices(regions: Region[]): number[] {\n /** Get the row indices from a selection of regions */\n if (regions == null || regions.length === 0) return [];\n const rowIndices = new Set<number>();\n for (const region of regions) {\n const { rows } = region;\n if (rows == null || rows.length !== 2) continue;\n for (let i = rows[0]; i <= rows[1]; i++) {\n rowIndices.add(i);\n }\n }\n return Array.from(rowIndices).sort((a, b) => a - b);\n}\n\nexport function topLeftCell(\n regions: Region[],\n requireSolitaryCell: boolean = false,\n): FocusedCellCoordinates | null {\n /** Top left cell of a ranged selection */\n if (regions == null) return null;\n const [region] = regions;\n if (region == null) return null;\n const { cols, rows } = region;\n if (cols == null || rows == null) return null;\n if (requireSolitaryCell && (cols[0] !== cols[1] || rows[0] !== rows[1]))\n return null;\n return { col: cols[0], row: rows[0], focusSelectionIndex: 0 };\n}\n\nfunction selectionEquals(a: Region[], b: Region[]): boolean {\n /** Check if two selections are equal */\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n const regionA = a[i];\n const regionB = b[i];\n\n const colsA = regionA.cols ?? [];\n const colsB = regionB.cols ?? [];\n\n const rowsA = regionA.rows ?? [];\n const rowsB = regionB.rows ?? [];\n\n if (\n colsA.length !== colsB.length ||\n rowsA.length !== rowsB.length ||\n colsA[0] !== colsB[0] ||\n colsA[1] !== colsB[1] ||\n rowsA[0] !== rowsB[0] ||\n rowsA[1] !== rowsB[1]\n ) {\n return false;\n }\n }\n return true;\n}\n\nexport function singleFocusedCell(\n sel: Region[],\n): FocusedCellCoordinates | null {\n /** Derive a single focused cell from a selected region, if possible */\n if (sel?.length !== 1) return null;\n return topLeftCell(sel, true);\n}\n\nfunction fillValues<T>(state: DataSheetStore<T>, selection: Region[]) {\n const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;\n\n // Prepare regions by unnesting columns\n let regions = selection.map((region) => {\n const { cols, rows } = region;\n // Get the first column (maybe should be the last)\n const [col] = cols;\n return { cols: [col, col], rows };\n });\n\n // Fill values downwards\n if (!editable || fillValueBaseCell == null) return updatedData;\n const { col, row } = fillValueBaseCell;\n const key = columnSpec[col].key;\n const value = updatedData[row]?.[key] ?? data[row][key];\n const spec = {};\n for (const region of regions) {\n const { cols, rows } = region;\n for (const row of range(rows)) {\n let op = updatedData[row] == null ? \"$set\" : \"$merge\";\n spec[row] = { [op]: { [key]: value } };\n }\n }\n return update(updatedData, spec);\n}\n"],"names":["topLeftCell","col","row"],"mappings":";;AAWO,SAAS,mBAAsB,KAAK,KAA4B;AACrE,SAAO;AAAA,IACL,MAAM,CAAA;AAAA,IACN,YAAY,CAAA;AAAA,IACZ,oBAAoB;AAAA,IACpB,UAAU;AAAA,IACV,iCAAiB,IAAA;AAAA,IACjB,WAAW,CAAA;AAAA,IACX,mBAAmB;AAAA,IACnB,aAAa,CAAA;AAAA,IACb,aAAa;AAAA,IACb,aAAa;AAAA,IACb,aAAa;AAAA,IACb,UAAU;AAAA,IACV,iBAAiB;AAAA;AAAA,IAEjB,wBAAwB;AAAA,IACxB,aAAa,WAAqB;AAChC,UAAI,gBAAgB,SAAS,CAAC;AAAA,IAChC;AAAA,IACA,uCAAuB,IAAA;AAAA,IACvB,gBAAgB,WAA6C;AAC3D,UAAI,CAAC,UAAU;AACb,cAAM,EAAE,aAAAA,aAAAA,IAAgB;AACxB,YAAIA,gBAAe,KAAM,QAAO,CAAA;AAChC,YAAI,EAAE,KAAK,IAAA,IAAQA;AACnB,gBAAQ,WAAA;AAAA,UACN,KAAK;AACH,kBAAM,KAAK,IAAI,GAAG,MAAM,CAAC;AACzB;AAAA,UACF,KAAK;AACH,kBAAM,KAAK;AAAA,cACT,MAAM;AAAA,cACN,KAAK,IAAI,MAAM,KAAK,QAAQ,MAAM,YAAY,MAAM,IAAI;AAAA,YAAA;AAE1D;AAAA,UACF,KAAK;AACH,kBAAM,KAAK,IAAI,GAAG,MAAM,CAAC;AACzB;AAAA,UACF,KAAK;AACH,kBAAM,KAAK,IAAI,MAAM,GAAG,MAAM,WAAW,SAAS,CAAC;AACnD;AAAA,QAAA;AAEJ,cAAM,SAAiB,EAAE,MAAM,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,CAAA;AACb,aAAW,UAAU,SAAS;AAC5B,UAAM,EAAE,MAAM,KAAA,IAAS;AACvB,eAAWC,QAAO,MAAM,IAAI,GAAG;AAC7B,UAAI,KAAK,YAAYA,IAAG,KAAK,OAAO,SAAS;AAC7C,WAAKA,IAAG,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,GAAG,QAAM;AAAA,IACrC;AAAA,EACF;AACA,SAAO,OAAO,aAAa,IAAI;AACjC;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@macrostrat/data-sheet",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Scalable data sheet with optional editing capabilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -49,16 +49,17 @@
|
|
|
49
49
|
"@blueprintjs/core": "^6.11.3",
|
|
50
50
|
"@blueprintjs/table": "^6.0.23",
|
|
51
51
|
"@macrostrat/color-utils": "^1.2.1",
|
|
52
|
-
"@macrostrat/data-components": "^1.1.
|
|
53
|
-
"@macrostrat/hyper": "^3.0
|
|
54
|
-
"@macrostrat/ui-components": "^5.0.
|
|
52
|
+
"@macrostrat/data-components": "^1.1.6",
|
|
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",
|
|
58
58
|
"immutability-helper": "^3.1.1",
|
|
59
|
+
"jotai": "^2.19.1",
|
|
60
|
+
"jotai-zustand": "^0.6.0",
|
|
59
61
|
"underscore": "^1.13.7",
|
|
60
|
-
"zustand": "^5.0.2"
|
|
61
|
-
"zustand-computed": "^2.1.0"
|
|
62
|
+
"zustand": "^5.0.2"
|
|
62
63
|
},
|
|
63
64
|
"devDependencies": {
|
|
64
65
|
"@macrostrat/web-components-bundler": "1.0.1"
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { ColumnSpec, editorKeyHandlerAtom } from "./utils";
|
|
2
|
+
import { DataSheetStore } from "./types.ts";
|
|
3
|
+
import h from "./main.module.sass";
|
|
4
|
+
import { ReactNode } from "react";
|
|
5
|
+
import { EditorPopup } from "./components";
|
|
6
|
+
import { singleFocusedCell } from "./zustand-store.ts";
|
|
7
|
+
import { Cell } from "@blueprintjs/table";
|
|
8
|
+
import { useAtomValue, useSelector } from "./provider.ts";
|
|
9
|
+
|
|
10
|
+
export function basicCellRenderer<T>(
|
|
11
|
+
rowIndex: number,
|
|
12
|
+
colIndex: number,
|
|
13
|
+
columnSpec: ColumnSpec,
|
|
14
|
+
state: DataSheetStore<T>,
|
|
15
|
+
autoFocusEditor = true,
|
|
16
|
+
): any {
|
|
17
|
+
const data = state.data;
|
|
18
|
+
const updatedData = state.updatedData;
|
|
19
|
+
|
|
20
|
+
const isDeleted = state.deletedRows.has(rowIndex);
|
|
21
|
+
|
|
22
|
+
const row = data[rowIndex] ?? updatedData[rowIndex];
|
|
23
|
+
const loading = row == null;
|
|
24
|
+
const col = columnSpec;
|
|
25
|
+
|
|
26
|
+
const _topLeftCell = state.topLeftCell;
|
|
27
|
+
const onCellEdited = state.onCellEdited;
|
|
28
|
+
|
|
29
|
+
const value = updatedData[rowIndex]?.[col.key] ?? data[rowIndex]?.[col.key];
|
|
30
|
+
const _renderedValue = col.valueRenderer?.(value) ?? value;
|
|
31
|
+
|
|
32
|
+
let style = col.style ?? {};
|
|
33
|
+
if (isDeleted) {
|
|
34
|
+
style = {
|
|
35
|
+
...style,
|
|
36
|
+
opacity: 0.5,
|
|
37
|
+
textDecoration: "line-through",
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//const focused =
|
|
42
|
+
// focusedCell?.col === colIndex && focusedCell?.row === rowIndex;
|
|
43
|
+
|
|
44
|
+
const editable = (col.editable ?? state.editable) && !isDeleted;
|
|
45
|
+
|
|
46
|
+
// Top left cell of a ranged selection
|
|
47
|
+
const topLeft =
|
|
48
|
+
_topLeftCell?.col === colIndex && _topLeftCell?.row === rowIndex;
|
|
49
|
+
|
|
50
|
+
if (!editable && state.editable) {
|
|
51
|
+
// If the cell is not editable but the sheet is editable, we want to differentiate
|
|
52
|
+
// between editable and non-editable cells.
|
|
53
|
+
style.color = "var(--secondary-color)";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const edited = updatedData[rowIndex]?.[col.key] != null;
|
|
57
|
+
let intent = edited ? "success" : undefined;
|
|
58
|
+
if (isDeleted) {
|
|
59
|
+
intent = "danger";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const _Cell = col.cellComponent ?? BaseCell;
|
|
63
|
+
|
|
64
|
+
let inlineEditor = editable ? (col.inlineEditor ?? true) : false;
|
|
65
|
+
|
|
66
|
+
if (!topLeft) {
|
|
67
|
+
// This should be the case for every cell except the focused one
|
|
68
|
+
return h(
|
|
69
|
+
_Cell,
|
|
70
|
+
{
|
|
71
|
+
intent,
|
|
72
|
+
loading,
|
|
73
|
+
value,
|
|
74
|
+
style,
|
|
75
|
+
isDeleted,
|
|
76
|
+
},
|
|
77
|
+
_renderedValue,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The rest is for the top-left cell of a selection or the focused cell
|
|
82
|
+
|
|
83
|
+
// Hidden input to capture key events
|
|
84
|
+
let hiddenInput = h(EditorInput, {
|
|
85
|
+
className: "hidden-input",
|
|
86
|
+
autoFocus: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
let cellContents: ReactNode = _renderedValue;
|
|
90
|
+
|
|
91
|
+
let _dataEditor = null;
|
|
92
|
+
let className = null;
|
|
93
|
+
|
|
94
|
+
if (col.dataEditor != null) {
|
|
95
|
+
_dataEditor = h(
|
|
96
|
+
EditorPopup,
|
|
97
|
+
{
|
|
98
|
+
autoFocus: autoFocusEditor,
|
|
99
|
+
valueViewer: _renderedValue,
|
|
100
|
+
},
|
|
101
|
+
[
|
|
102
|
+
h(col.dataEditor, {
|
|
103
|
+
value,
|
|
104
|
+
editable,
|
|
105
|
+
onChange(value) {
|
|
106
|
+
if (!editable) return;
|
|
107
|
+
state.onSelectionEdited(value);
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!editable) {
|
|
115
|
+
// Most cells are not focused and don't need to be editable.
|
|
116
|
+
// This will be the rendering logic for almost all cells
|
|
117
|
+
|
|
118
|
+
if (_dataEditor != null) {
|
|
119
|
+
cellContents = _dataEditor;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return h(
|
|
123
|
+
_Cell,
|
|
124
|
+
{
|
|
125
|
+
intent,
|
|
126
|
+
value,
|
|
127
|
+
style,
|
|
128
|
+
},
|
|
129
|
+
cellContents,
|
|
130
|
+
);
|
|
131
|
+
// Could probably put the hidden input elsewhere,
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* The remaining logic covers cells that are focused and editable */
|
|
135
|
+
|
|
136
|
+
const onChange = (e) => {
|
|
137
|
+
if (!editable) return;
|
|
138
|
+
if (value === e.target.value) return;
|
|
139
|
+
onCellEdited(rowIndex, col.key, e.target.value);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const isSingleCellSelection = singleFocusedCell(state.selection) != null;
|
|
143
|
+
|
|
144
|
+
let _inlineEditor: ReactNode = null;
|
|
145
|
+
if (typeof inlineEditor == "boolean") {
|
|
146
|
+
let _value = value;
|
|
147
|
+
if (
|
|
148
|
+
typeof _renderedValue === "string" ||
|
|
149
|
+
typeof _renderedValue === "number" ||
|
|
150
|
+
_renderedValue == null
|
|
151
|
+
) {
|
|
152
|
+
_value = _renderedValue;
|
|
153
|
+
}
|
|
154
|
+
_inlineEditor = h(EditorInput, {
|
|
155
|
+
className: "main-editor",
|
|
156
|
+
value: _value ?? "",
|
|
157
|
+
autoFocus: autoFocusEditor,
|
|
158
|
+
onChange,
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
// If inlineEditor is a ReactNode, we use it directly
|
|
162
|
+
_inlineEditor = inlineEditor as ReactNode;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (_dataEditor != null) {
|
|
166
|
+
className = "editor-cell";
|
|
167
|
+
cellContents = _dataEditor;
|
|
168
|
+
hiddenInput = null;
|
|
169
|
+
} else if (_inlineEditor != null) {
|
|
170
|
+
cellContents = _inlineEditor;
|
|
171
|
+
className = "input-cell";
|
|
172
|
+
hiddenInput = null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Hidden html input
|
|
176
|
+
return h(
|
|
177
|
+
_Cell,
|
|
178
|
+
{
|
|
179
|
+
intent,
|
|
180
|
+
value,
|
|
181
|
+
className,
|
|
182
|
+
style,
|
|
183
|
+
//truncated: false,
|
|
184
|
+
},
|
|
185
|
+
[
|
|
186
|
+
cellContents,
|
|
187
|
+
h.if(editable && isSingleCellSelection)(DragHandle),
|
|
188
|
+
hiddenInput,
|
|
189
|
+
],
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function EditorInput(props) {
|
|
194
|
+
const onKeyDown = useAtomValue(editorKeyHandlerAtom);
|
|
195
|
+
return h("input", {
|
|
196
|
+
onKeyDown,
|
|
197
|
+
...props,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function DragHandle() {
|
|
202
|
+
// TODO: we might want to drag multiple columns in some cases
|
|
203
|
+
// This should be on the last cell of a selection
|
|
204
|
+
const onMouseDown = useSelector((state) => state.onDragValue);
|
|
205
|
+
return h("div.corner-drag-handle", { onMouseDown });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function BaseCell({ children, value, ...rest }) {
|
|
209
|
+
return h(
|
|
210
|
+
Cell,
|
|
211
|
+
{
|
|
212
|
+
interactive: true,
|
|
213
|
+
...rest,
|
|
214
|
+
},
|
|
215
|
+
children,
|
|
216
|
+
);
|
|
217
|
+
}
|
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,
|