@macrostrat/data-sheet 3.0.4 → 4.0.0-dev1
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 +20 -1
- package/dist/actions/builtin.cjs +56 -0
- package/dist/actions/builtin.cjs.map +1 -0
- package/dist/actions/builtin.d.ts +15 -0
- package/dist/actions/builtin.js +56 -0
- package/dist/actions/builtin.js.map +1 -0
- package/dist/actions/clipboard.cjs +250 -0
- package/dist/actions/clipboard.cjs.map +1 -0
- package/dist/actions/clipboard.d.ts +42 -0
- package/dist/actions/clipboard.js +250 -0
- package/dist/actions/clipboard.js.map +1 -0
- package/dist/actions/filters.cjs +123 -0
- package/dist/actions/filters.cjs.map +1 -0
- package/dist/actions/filters.d.ts +6 -0
- package/dist/actions/filters.js +123 -0
- package/dist/actions/filters.js.map +1 -0
- package/dist/actions/index.d.ts +6 -0
- package/dist/actions/selection.cjs +194 -0
- package/dist/actions/selection.cjs.map +1 -0
- package/dist/actions/selection.d.ts +40 -0
- package/dist/actions/selection.js +192 -0
- package/dist/actions/selection.js.map +1 -0
- package/dist/actions/toolbar.cjs +171 -0
- package/dist/actions/toolbar.cjs.map +1 -0
- package/dist/actions/toolbar.d.ts +10 -0
- package/dist/actions/toolbar.js +171 -0
- package/dist/actions/toolbar.js.map +1 -0
- package/dist/actions/toolbar.module.sass.cjs +22 -0
- package/dist/actions/toolbar.module.sass.cjs.map +1 -0
- package/dist/actions/toolbar.module.sass.js +20 -0
- package/dist/actions/toolbar.module.sass.js.map +1 -0
- package/dist/actions/types.d.ts +143 -0
- package/dist/cell-renderer.cjs +165 -0
- package/dist/cell-renderer.cjs.map +1 -0
- package/dist/cell-renderer.d.ts +3 -0
- package/dist/cell-renderer.js +165 -0
- package/dist/cell-renderer.js.map +1 -0
- package/dist/components/actions.module.sass.cjs +3 -3
- package/dist/components/actions.module.sass.js +2 -2
- package/dist/components/colors.cjs +20 -16
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.d.ts +4 -3
- package/dist/components/colors.js +20 -16
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +4 -3
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +4 -3
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/main.module.sass.cjs +12 -12
- package/dist/components/main.module.sass.js +11 -11
- package/dist/components/text-areas.cjs +23 -18
- package/dist/components/text-areas.cjs.map +1 -1
- package/dist/components/text-areas.d.ts +8 -4
- package/dist/components/text-areas.js +20 -13
- package/dist/components/text-areas.js.map +1 -1
- package/dist/components/view-info.cjs +18 -0
- package/dist/components/view-info.cjs.map +1 -0
- package/dist/components/view-info.d.ts +1 -0
- package/dist/components/view-info.js +18 -0
- package/dist/components/view-info.js.map +1 -0
- package/dist/core.cjs +201 -300
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +18 -10
- package/dist/core.js +204 -299
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +190 -52
- package/dist/index.cjs +47 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +48 -9
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +15 -14
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/notifications.cjs +19 -0
- package/dist/notifications.cjs.map +1 -0
- package/dist/notifications.d.ts +9 -0
- package/dist/notifications.js +19 -0
- package/dist/notifications.js.map +1 -0
- package/dist/postgrest-table/data-loaders.cjs +181 -104
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +52 -28
- package/dist/postgrest-table/data-loaders.js +181 -104
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +205 -79
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +14 -17
- package/dist/postgrest-table/index.js +186 -58
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
- package/dist/postgrest-table/lazy-loader-table.js +17 -14
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
- package/dist/postgrest-table/loading-utils.cjs +16 -0
- package/dist/postgrest-table/loading-utils.cjs.map +1 -0
- package/dist/postgrest-table/loading-utils.d.ts +13 -0
- package/dist/postgrest-table/loading-utils.js +16 -0
- package/dist/postgrest-table/loading-utils.js.map +1 -0
- package/dist/postgrest-table/main.module.sass.cjs +18 -13
- package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
- package/dist/postgrest-table/main.module.sass.js +18 -13
- package/dist/postgrest-table/main.module.sass.js.map +1 -1
- package/dist/provider.cjs +96 -344
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.js +96 -343
- package/dist/provider.js.map +1 -1
- package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
- package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
- package/dist/renderers/column-header-cell-postgrest.js +216 -0
- package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
- package/dist/renderers/column-header-cell.cjs +103 -0
- package/dist/renderers/column-header-cell.cjs.map +1 -0
- package/dist/renderers/column-header-cell.d.ts +30 -0
- package/dist/renderers/column-header-cell.js +101 -0
- package/dist/renderers/column-header-cell.js.map +1 -0
- package/dist/renderers/index.d.ts +2 -0
- package/dist/table-updates/data-parameter.d.ts +14 -0
- package/dist/table-updates/filter.d.ts +16 -0
- package/dist/table-updates/index.d.ts +19 -0
- package/dist/types.cjs +9 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/column-spec.cjs +9 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.d.ts +21 -0
- package/dist/utils/column-spec.js +9 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/dist/utils/key-handling.cjs +161 -47
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +3 -3
- package/dist/utils/key-handling.js +161 -47
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +485 -0
- package/dist/zustand-store.cjs.map +1 -0
- package/dist/zustand-store.d.ts +6 -0
- package/dist/zustand-store.js +483 -0
- package/dist/zustand-store.js.map +1 -0
- package/package.json +6 -5
- package/src/actions/builtin.ts +65 -0
- package/src/actions/clipboard.ts +350 -0
- package/src/actions/filters.ts +150 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/selection.ts +271 -0
- package/src/actions/toolbar.module.sass +30 -0
- package/src/actions/toolbar.ts +212 -0
- package/src/actions/types.ts +159 -0
- package/src/cell-renderer.ts +221 -0
- package/src/components/colors.ts +22 -19
- package/src/components/editor-popup.ts +3 -2
- package/src/components/index.ts +1 -0
- package/src/components/main.module.sass +1 -0
- package/src/components/text-areas.ts +34 -19
- package/src/components/view-info.ts +17 -0
- package/src/core.ts +273 -360
- package/src/index.ts +4 -0
- package/src/main.module.sass +14 -2
- package/src/notifications.ts +22 -0
- package/src/postgrest-table/data-loaders.ts +300 -201
- package/src/postgrest-table/index.ts +260 -86
- package/src/postgrest-table/lazy-loader-table.ts +18 -17
- package/src/postgrest-table/loading-utils.ts +49 -0
- package/src/postgrest-table/main.module.sass +16 -0
- package/src/provider.ts +110 -487
- package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
- package/src/renderers/column-header-cell-postgrest.ts +282 -0
- package/src/renderers/column-header-cell.ts +388 -0
- package/src/renderers/index.ts +2 -0
- package/src/table-updates/data-parameter.ts +44 -0
- package/src/table-updates/filter.ts +124 -0
- package/src/table-updates/index.ts +239 -0
- package/src/types.ts +121 -0
- package/src/utils/column-spec.ts +47 -0
- package/src/utils/key-handling.ts +201 -66
- package/src/zustand-store.ts +582 -0
- package/dist/provider.d.ts +0 -69
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { RegionCardinality } from "@blueprintjs/table";
|
|
2
|
+
import type { TableAction, TableActionContext, CellEdit } from "./types";
|
|
3
|
+
import type { ClipboardProxy } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
/** Serialize the current selection to tab-separated values.
|
|
6
|
+
* For full-row/column copies, includes a header row and produces
|
|
7
|
+
* a `ClipboardProxy` for potential backend-mediated paste. */
|
|
8
|
+
export function serializeSelectionToTSV<T>(ctx: TableActionContext<T>): {
|
|
9
|
+
text: string;
|
|
10
|
+
proxy?: ClipboardProxy;
|
|
11
|
+
} {
|
|
12
|
+
const { data, updatedData, columnSpec } = ctx;
|
|
13
|
+
const cardinality = ctx.selectionCardinality ?? RegionCardinality.FULL_TABLE;
|
|
14
|
+
const numRows = Math.max(data.length, updatedData.length);
|
|
15
|
+
|
|
16
|
+
let rowIndices: number[];
|
|
17
|
+
let columnKeys: string[];
|
|
18
|
+
let includeHeaders = false;
|
|
19
|
+
|
|
20
|
+
switch (cardinality) {
|
|
21
|
+
case RegionCardinality.CELLS:
|
|
22
|
+
rowIndices = ctx.getSelectedRowIndices();
|
|
23
|
+
columnKeys = ctx.getSelectedColumnKeys();
|
|
24
|
+
break;
|
|
25
|
+
case RegionCardinality.FULL_ROWS:
|
|
26
|
+
rowIndices = ctx.getSelectedRowIndices();
|
|
27
|
+
columnKeys = columnSpec.map((c) => c.key);
|
|
28
|
+
// TODO: include headers, but copy using proxy...
|
|
29
|
+
includeHeaders = false;
|
|
30
|
+
break;
|
|
31
|
+
case RegionCardinality.FULL_COLUMNS:
|
|
32
|
+
rowIndices = Array.from({ length: numRows }, (_, i) => i);
|
|
33
|
+
columnKeys = ctx.getSelectedColumnKeys();
|
|
34
|
+
includeHeaders = false;
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
rowIndices = Array.from({ length: numRows }, (_, i) => i);
|
|
38
|
+
columnKeys = columnSpec.map((c) => c.key);
|
|
39
|
+
includeHeaders = false;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const rows: string[] = [];
|
|
44
|
+
if (includeHeaders) {
|
|
45
|
+
const headers = columnKeys.map((key) => {
|
|
46
|
+
const col = columnSpec.find((c) => c.key === key);
|
|
47
|
+
return col?.name ?? key;
|
|
48
|
+
});
|
|
49
|
+
rows.push(headers.join("\t"));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const selectedColumns = columnSpec.filter((d) => columnKeys.includes(d.key));
|
|
53
|
+
|
|
54
|
+
for (const i of rowIndices) {
|
|
55
|
+
const row = { ...data[i], ...updatedData[i] };
|
|
56
|
+
rows.push(
|
|
57
|
+
selectedColumns
|
|
58
|
+
.map((col) => {
|
|
59
|
+
const value = row[col.key];
|
|
60
|
+
return String(col.valueRenderer?.(value) ?? value ?? "");
|
|
61
|
+
})
|
|
62
|
+
.join("\t"),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const text = rows.join("\n");
|
|
67
|
+
|
|
68
|
+
// For full rows/columns, also create a proxy for backend-mediated paste
|
|
69
|
+
let proxy: ClipboardProxy | undefined;
|
|
70
|
+
if (
|
|
71
|
+
cardinality === RegionCardinality.FULL_ROWS ||
|
|
72
|
+
cardinality === RegionCardinality.FULL_COLUMNS
|
|
73
|
+
) {
|
|
74
|
+
proxy = {
|
|
75
|
+
cardinality,
|
|
76
|
+
rowIndices:
|
|
77
|
+
cardinality === RegionCardinality.FULL_ROWS ? rowIndices : undefined,
|
|
78
|
+
columnKeys:
|
|
79
|
+
cardinality === RegionCardinality.FULL_COLUMNS ? columnKeys : undefined,
|
|
80
|
+
text,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return { text, proxy };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Parse tab-separated text into a 2D string array. */
|
|
88
|
+
function parseTSV(text: string): string[][] {
|
|
89
|
+
return text
|
|
90
|
+
.trim()
|
|
91
|
+
.split("\n")
|
|
92
|
+
.map((row) => row.split("\t"));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Copy the current selection to the clipboard as tab-separated text.
|
|
96
|
+
* For full-row/column copies, a `ClipboardProxy` is also stored in the
|
|
97
|
+
* table state, enabling backend-mediated paste in lazy-loaded tables. */
|
|
98
|
+
export const copyAction: TableAction = {
|
|
99
|
+
id: "copy",
|
|
100
|
+
name: "Copy",
|
|
101
|
+
icon: "clipboard",
|
|
102
|
+
targets: [
|
|
103
|
+
RegionCardinality.CELLS,
|
|
104
|
+
RegionCardinality.FULL_ROWS,
|
|
105
|
+
RegionCardinality.FULL_COLUMNS,
|
|
106
|
+
RegionCardinality.FULL_TABLE,
|
|
107
|
+
],
|
|
108
|
+
requiresEditable: false,
|
|
109
|
+
async run(ctx) {
|
|
110
|
+
const { text, proxy } = serializeSelectionToTSV(ctx);
|
|
111
|
+
console.log("Copying text:", text);
|
|
112
|
+
await navigator.clipboard.writeText(text);
|
|
113
|
+
ctx.setClipboardProxy(proxy ?? null);
|
|
114
|
+
},
|
|
115
|
+
hotkey: "mod+c",
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const cutAction: TableAction = {
|
|
119
|
+
id: "cut",
|
|
120
|
+
name: "Cut",
|
|
121
|
+
icon: "clipboard",
|
|
122
|
+
targets: [
|
|
123
|
+
RegionCardinality.CELLS,
|
|
124
|
+
RegionCardinality.FULL_ROWS,
|
|
125
|
+
RegionCardinality.FULL_COLUMNS,
|
|
126
|
+
RegionCardinality.FULL_TABLE,
|
|
127
|
+
],
|
|
128
|
+
requiresEditable: true,
|
|
129
|
+
async run(ctx) {
|
|
130
|
+
const { text, proxy } = serializeSelectionToTSV(ctx);
|
|
131
|
+
console.log("Cutting text:", text);
|
|
132
|
+
await navigator.clipboard.writeText(text);
|
|
133
|
+
ctx.setClipboardProxy(proxy ?? null);
|
|
134
|
+
ctx.clearSelection();
|
|
135
|
+
},
|
|
136
|
+
hotkey: "mod+x",
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** Describes the shape relationship between pasted data and the target region */
|
|
140
|
+
export enum PasteShape {
|
|
141
|
+
/** Data fits exactly into the target region */
|
|
142
|
+
EXACT = "exact",
|
|
143
|
+
/** Data is smaller than the target region in one or both dimensions */
|
|
144
|
+
SMALLER = "smaller",
|
|
145
|
+
/** Data is larger than the target region in one or both dimensions */
|
|
146
|
+
LARGER = "larger",
|
|
147
|
+
/** Pasting into a single cell — expand from that anchor */
|
|
148
|
+
SINGLE_CELL = "single-cell",
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Determine the relationship between clipboard data shape and target region */
|
|
152
|
+
function getPasteShape(
|
|
153
|
+
parsed: string[][],
|
|
154
|
+
targetRows: number,
|
|
155
|
+
targetCols: number,
|
|
156
|
+
): PasteShape {
|
|
157
|
+
if (targetRows === 1 && targetCols === 1) return PasteShape.SINGLE_CELL;
|
|
158
|
+
const dataRows = parsed.length;
|
|
159
|
+
const dataCols = Math.max(...parsed.map((r) => r.length));
|
|
160
|
+
if (dataRows === targetRows && dataCols === targetCols)
|
|
161
|
+
return PasteShape.EXACT;
|
|
162
|
+
if (dataRows <= targetRows && dataCols <= targetCols)
|
|
163
|
+
return PasteShape.SMALLER;
|
|
164
|
+
return PasteShape.LARGER;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Build cell edits for an Excel-style paste.
|
|
168
|
+
*
|
|
169
|
+
* Behavior by shape:
|
|
170
|
+
* - **SINGLE_CELL**: Paste the full clipboard grid anchored at the focused
|
|
171
|
+
* cell, expanding rightward and downward. Clips at table boundaries.
|
|
172
|
+
* - **EXACT**: Paste one-to-one into the selected region.
|
|
173
|
+
* - **SMALLER**: Tile (repeat) the clipboard data to fill the selection,
|
|
174
|
+
* matching Excel's behavior when the selection is an even multiple of the
|
|
175
|
+
* data. Otherwise, just paste once from the top-left of the selection.
|
|
176
|
+
* - **LARGER**: Truncate the data to fit the target selection. */
|
|
177
|
+
function buildPasteEdits(
|
|
178
|
+
parsed: string[][],
|
|
179
|
+
startRow: number,
|
|
180
|
+
startColIdx: number,
|
|
181
|
+
targetRowCount: number,
|
|
182
|
+
targetColCount: number,
|
|
183
|
+
shape: PasteShape,
|
|
184
|
+
ctx: TableActionContext,
|
|
185
|
+
): CellEdit[] {
|
|
186
|
+
const dataRows = parsed.length;
|
|
187
|
+
const dataCols = Math.max(...parsed.map((r) => r.length));
|
|
188
|
+
const numDataRows = Math.max(ctx.data.length, ctx.updatedData.length);
|
|
189
|
+
const numCols = ctx.columnSpec.length;
|
|
190
|
+
const edits: CellEdit[] = [];
|
|
191
|
+
|
|
192
|
+
let pasteRows: number;
|
|
193
|
+
let pasteCols: number;
|
|
194
|
+
|
|
195
|
+
switch (shape) {
|
|
196
|
+
case PasteShape.SINGLE_CELL:
|
|
197
|
+
// Expand from the anchor cell; clip at table boundaries
|
|
198
|
+
pasteRows = Math.min(dataRows, numDataRows - startRow);
|
|
199
|
+
pasteCols = Math.min(dataCols, numCols - startColIdx);
|
|
200
|
+
break;
|
|
201
|
+
case PasteShape.EXACT:
|
|
202
|
+
pasteRows = targetRowCount;
|
|
203
|
+
pasteCols = targetColCount;
|
|
204
|
+
break;
|
|
205
|
+
case PasteShape.SMALLER:
|
|
206
|
+
// Tile to fill the selection if it's an even multiple
|
|
207
|
+
pasteRows = targetRowCount;
|
|
208
|
+
pasteCols = targetColCount;
|
|
209
|
+
break;
|
|
210
|
+
case PasteShape.LARGER:
|
|
211
|
+
// Truncate to fit
|
|
212
|
+
pasteRows = Math.min(dataRows, targetRowCount);
|
|
213
|
+
pasteCols = Math.min(dataCols, targetColCount);
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
for (let r = 0; r < pasteRows; r++) {
|
|
218
|
+
const dataRow = startRow + r;
|
|
219
|
+
if (dataRow >= numDataRows) break;
|
|
220
|
+
// For SMALLER shape, wrap around the source data rows
|
|
221
|
+
const srcR = dataRows > 0 ? r % dataRows : 0;
|
|
222
|
+
const row = parsed[srcR];
|
|
223
|
+
if (row == null) continue;
|
|
224
|
+
|
|
225
|
+
for (let c = 0; c < pasteCols; c++) {
|
|
226
|
+
const colIdx = startColIdx + c;
|
|
227
|
+
if (colIdx >= numCols) break;
|
|
228
|
+
// Wrap around source columns for tiling
|
|
229
|
+
const srcC = dataCols > 0 ? c % dataCols : 0;
|
|
230
|
+
const value = row[srcC] ?? "";
|
|
231
|
+
edits.push({
|
|
232
|
+
rowIndex: dataRow,
|
|
233
|
+
columnKey: ctx.columnSpec[colIdx].key,
|
|
234
|
+
value,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return edits;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Paste tab-separated text from the clipboard into the table.
|
|
242
|
+
*
|
|
243
|
+
* Follows Excel-style paste semantics:
|
|
244
|
+
* - **Single-cell selection**: Anchors the paste at the focused cell and
|
|
245
|
+
* expands to accommodate the full clipboard data, clipping at table edges.
|
|
246
|
+
* - **Matching region**: Pastes one-to-one into the selected cells.
|
|
247
|
+
* - **Selection larger than data**: Tiles the data to fill the selection
|
|
248
|
+
* when the selection is an even multiple; otherwise pastes once from the
|
|
249
|
+
* top-left corner.
|
|
250
|
+
* - **Selection smaller than data**: Truncates the pasted data to fit.
|
|
251
|
+
*
|
|
252
|
+
* When a `ClipboardProxy` is stored and the clipboard text matches,
|
|
253
|
+
* the proxy metadata is available for consumers to detect same-table
|
|
254
|
+
* pastes and optionally delegate to the backend. */
|
|
255
|
+
export const pasteAction: TableAction = {
|
|
256
|
+
id: "paste",
|
|
257
|
+
name: "Paste",
|
|
258
|
+
icon: "bring-data",
|
|
259
|
+
hotkey: "mod+v",
|
|
260
|
+
targets: [
|
|
261
|
+
RegionCardinality.CELLS,
|
|
262
|
+
RegionCardinality.FULL_ROWS,
|
|
263
|
+
RegionCardinality.FULL_COLUMNS,
|
|
264
|
+
RegionCardinality.FULL_TABLE,
|
|
265
|
+
],
|
|
266
|
+
requiresEditable: true,
|
|
267
|
+
async run(ctx) {
|
|
268
|
+
const text = await navigator.clipboard.readText();
|
|
269
|
+
if (!text) return;
|
|
270
|
+
|
|
271
|
+
const parsed = parseTSV(text);
|
|
272
|
+
if (parsed.length === 0) return;
|
|
273
|
+
|
|
274
|
+
const cardinality =
|
|
275
|
+
ctx.selectionCardinality ?? RegionCardinality.FULL_TABLE;
|
|
276
|
+
const numDataRows = Math.max(ctx.data.length, ctx.updatedData.length);
|
|
277
|
+
const numCols = ctx.columnSpec.length;
|
|
278
|
+
|
|
279
|
+
// Determine the target region
|
|
280
|
+
const rowIndices = ctx.getSelectedRowIndices();
|
|
281
|
+
const columnKeys = ctx.getSelectedColumnKeys();
|
|
282
|
+
|
|
283
|
+
let startRow: number;
|
|
284
|
+
let startColIdx: number;
|
|
285
|
+
let targetRowCount: number;
|
|
286
|
+
let targetColCount: number;
|
|
287
|
+
|
|
288
|
+
switch (cardinality) {
|
|
289
|
+
case RegionCardinality.CELLS: {
|
|
290
|
+
startRow = rowIndices[0] ?? 0;
|
|
291
|
+
startColIdx =
|
|
292
|
+
columnKeys.length > 0
|
|
293
|
+
? ctx.columnSpec.findIndex((c) => c.key === columnKeys[0])
|
|
294
|
+
: 0;
|
|
295
|
+
targetRowCount = rowIndices.length;
|
|
296
|
+
targetColCount = columnKeys.length;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
case RegionCardinality.FULL_ROWS: {
|
|
300
|
+
startRow = rowIndices[0] ?? 0;
|
|
301
|
+
startColIdx = 0;
|
|
302
|
+
targetRowCount = rowIndices.length;
|
|
303
|
+
targetColCount = numCols;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case RegionCardinality.FULL_COLUMNS: {
|
|
307
|
+
startRow = 0;
|
|
308
|
+
startColIdx =
|
|
309
|
+
columnKeys.length > 0
|
|
310
|
+
? ctx.columnSpec.findIndex((c) => c.key === columnKeys[0])
|
|
311
|
+
: 0;
|
|
312
|
+
targetRowCount = numDataRows;
|
|
313
|
+
targetColCount = columnKeys.length;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
default: {
|
|
317
|
+
// FULL_TABLE
|
|
318
|
+
startRow = 0;
|
|
319
|
+
startColIdx = 0;
|
|
320
|
+
targetRowCount = numDataRows;
|
|
321
|
+
targetColCount = numCols;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (startColIdx < 0) startColIdx = 0;
|
|
327
|
+
|
|
328
|
+
const shape = getPasteShape(parsed, targetRowCount, targetColCount);
|
|
329
|
+
const edits = buildPasteEdits(
|
|
330
|
+
parsed,
|
|
331
|
+
startRow,
|
|
332
|
+
startColIdx,
|
|
333
|
+
targetRowCount,
|
|
334
|
+
targetColCount,
|
|
335
|
+
shape,
|
|
336
|
+
ctx,
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
if (edits.length > 0) {
|
|
340
|
+
ctx.editCells(edits);
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/** Convenience array of clipboard-related actions. */
|
|
346
|
+
export const clipboardActions: TableAction[] = [
|
|
347
|
+
copyAction,
|
|
348
|
+
cutAction,
|
|
349
|
+
pasteAction,
|
|
350
|
+
];
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Button,
|
|
3
|
+
ButtonGroup,
|
|
4
|
+
Menu,
|
|
5
|
+
MenuItem,
|
|
6
|
+
PopoverNext,
|
|
7
|
+
Tag,
|
|
8
|
+
} from "@blueprintjs/core";
|
|
9
|
+
import { useMemo, useState } from "react";
|
|
10
|
+
import h from "./toolbar.module.sass";
|
|
11
|
+
import { useSelector, useStoreAPI } from "../provider";
|
|
12
|
+
import { collectAllFilters } from "./selection";
|
|
13
|
+
import type { TableFilter } from "./types";
|
|
14
|
+
|
|
15
|
+
/** Bar showing active filters as interactive tags, with a button to
|
|
16
|
+
* add new filters from the available set (global + column-spec). */
|
|
17
|
+
export function FilterBar<T>({ filters = [] }: { filters?: TableFilter<T>[] }) {
|
|
18
|
+
const columnSpec = useSelector((state) => state.columnSpec);
|
|
19
|
+
const storeAPI = useStoreAPI();
|
|
20
|
+
const activeFilters = useSelector((state) => state.activeFilters);
|
|
21
|
+
|
|
22
|
+
const allFilters = useMemo(
|
|
23
|
+
() => collectAllFilters(filters, columnSpec),
|
|
24
|
+
[filters, columnSpec],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const activeIds = useMemo(() => new Set(activeFilters.keys()), [activeFilters]);
|
|
28
|
+
|
|
29
|
+
const availableFilters = useMemo(
|
|
30
|
+
() => allFilters.filter((f) => !activeIds.has(f.id)),
|
|
31
|
+
[allFilters, activeIds],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const hasActive = activeFilters.size > 0;
|
|
35
|
+
|
|
36
|
+
return h("div.filter-bar", [
|
|
37
|
+
h(
|
|
38
|
+
ButtonGroup,
|
|
39
|
+
{ minimal: true },
|
|
40
|
+
Array.from(activeFilters.entries()).map(([id, entry]) =>
|
|
41
|
+
h(ActiveFilterChip, { key: id, filterId: id, entry }),
|
|
42
|
+
),
|
|
43
|
+
),
|
|
44
|
+
h.if(availableFilters.length > 0)(AddFilterPopover, {
|
|
45
|
+
filters: availableFilters,
|
|
46
|
+
}),
|
|
47
|
+
h.if(hasActive)(
|
|
48
|
+
Button,
|
|
49
|
+
{
|
|
50
|
+
minimal: true,
|
|
51
|
+
small: true,
|
|
52
|
+
icon: "cross",
|
|
53
|
+
onClick() {
|
|
54
|
+
storeAPI.getState().clearFilters();
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
"Clear filters",
|
|
58
|
+
),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A single active filter displayed as a tag. Clicking opens a popover
|
|
63
|
+
* for reconfiguring the filter; the remove button deactivates it. */
|
|
64
|
+
function ActiveFilterChip<T>({
|
|
65
|
+
filterId,
|
|
66
|
+
entry,
|
|
67
|
+
}: {
|
|
68
|
+
filterId: string;
|
|
69
|
+
entry: { filter: TableFilter<T>; state: any };
|
|
70
|
+
}) {
|
|
71
|
+
const storeAPI = useStoreAPI();
|
|
72
|
+
const [configOpen, setConfigOpen] = useState(false);
|
|
73
|
+
const { filter, state: filterState } = entry;
|
|
74
|
+
|
|
75
|
+
const tag = h(
|
|
76
|
+
Tag,
|
|
77
|
+
{
|
|
78
|
+
icon: filter.icon ?? "filter",
|
|
79
|
+
onRemove() {
|
|
80
|
+
storeAPI.getState().removeFilter(filterId);
|
|
81
|
+
},
|
|
82
|
+
interactive: true,
|
|
83
|
+
intent: "primary",
|
|
84
|
+
onClick: filter.filterForm ? () => setConfigOpen(!configOpen) : undefined,
|
|
85
|
+
},
|
|
86
|
+
filter.name,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (filter.filterForm == null) return tag;
|
|
90
|
+
|
|
91
|
+
return h(
|
|
92
|
+
PopoverNext,
|
|
93
|
+
{
|
|
94
|
+
isOpen: configOpen,
|
|
95
|
+
onClose: () => setConfigOpen(false),
|
|
96
|
+
content: h("div.filter-config", [
|
|
97
|
+
h.if(filter.description != null)("p.description", filter.description),
|
|
98
|
+
h(filter.filterForm, {
|
|
99
|
+
state: filterState,
|
|
100
|
+
setState(newState) {
|
|
101
|
+
storeAPI.getState().setFilter(filterId, filter, newState);
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
]),
|
|
105
|
+
placement: "bottom-start",
|
|
106
|
+
enforceFocus: false,
|
|
107
|
+
autoFocus: false,
|
|
108
|
+
},
|
|
109
|
+
tag,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Popover menu listing available (not-yet-active) filters. */
|
|
114
|
+
function AddFilterPopover<T>({
|
|
115
|
+
filters,
|
|
116
|
+
}: {
|
|
117
|
+
filters: TableFilter<T>[];
|
|
118
|
+
}) {
|
|
119
|
+
const storeAPI = useStoreAPI();
|
|
120
|
+
|
|
121
|
+
return h(
|
|
122
|
+
PopoverNext,
|
|
123
|
+
{
|
|
124
|
+
content: h(
|
|
125
|
+
Menu,
|
|
126
|
+
filters.map((f) =>
|
|
127
|
+
h(MenuItem, {
|
|
128
|
+
key: f.id,
|
|
129
|
+
text: f.name,
|
|
130
|
+
icon: f.icon ?? "filter",
|
|
131
|
+
onClick() {
|
|
132
|
+
storeAPI.getState().setFilter(f.id, f, f.defaultState);
|
|
133
|
+
},
|
|
134
|
+
}),
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
placement: "bottom-start",
|
|
138
|
+
},
|
|
139
|
+
h(
|
|
140
|
+
Button,
|
|
141
|
+
{
|
|
142
|
+
minimal: true,
|
|
143
|
+
small: true,
|
|
144
|
+
icon: "filter",
|
|
145
|
+
},
|
|
146
|
+
"Add filter",
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|