@svgrid/grid 1.0.2 → 1.1.1
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/LICENSE +1 -1
- package/README.md +137 -39
- package/dist/GridFooter.svelte +164 -0
- package/dist/GridFooter.svelte.d.ts +29 -0
- package/dist/GridMenus.svelte +648 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +472 -0
- package/dist/SvGrid.controller.svelte.js +1917 -0
- package/dist/SvGrid.css +2012 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.helpers.test.d.ts +1 -0
- package/dist/SvGrid.helpers.test.js +298 -0
- package/dist/SvGrid.svelte +2346 -8742
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +523 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/aligned-grids.d.ts +6 -0
- package/dist/aligned-grids.js +84 -0
- package/dist/aligned-grids.test.d.ts +1 -0
- package/dist/aligned-grids.test.js +75 -0
- package/dist/build-api.coverage.test.d.ts +20 -0
- package/dist/build-api.coverage.test.js +505 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +557 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-render.test.d.ts +1 -0
- package/dist/cell-render.test.js +338 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -0
- package/dist/chart-export.test.d.ts +1 -0
- package/dist/chart-export.test.js +302 -0
- package/dist/chart.coverage.test.d.ts +1 -0
- package/dist/chart.coverage.test.js +748 -0
- package/dist/chart.d.ts +370 -3
- package/dist/chart.js +1135 -42
- package/dist/clipboard.d.ts +15 -0
- package/dist/clipboard.js +420 -0
- package/dist/clipboard.test.d.ts +1 -0
- package/dist/clipboard.test.js +700 -0
- package/dist/collaboration.coverage.test.d.ts +1 -0
- package/dist/collaboration.coverage.test.js +200 -0
- package/dist/column-groups.d.ts +19 -0
- package/dist/column-groups.js +62 -0
- package/dist/column-groups.test.d.ts +1 -0
- package/dist/column-groups.test.js +56 -0
- package/dist/column-types.d.ts +10 -0
- package/dist/column-types.js +63 -0
- package/dist/column-types.test.d.ts +1 -0
- package/dist/column-types.test.js +62 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/columns.test.d.ts +1 -0
- package/dist/columns.test.js +625 -0
- package/dist/core.d.ts +86 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +31 -0
- package/dist/editing.js +529 -0
- package/dist/editing.test.d.ts +1 -0
- package/dist/editing.test.js +732 -0
- package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
- package/dist/editors/cell-editors.coverage.test.js +139 -0
- package/dist/editors/cell-editors.d.ts +1 -1
- package/dist/facet-buckets.d.ts +13 -0
- package/dist/facet-buckets.js +54 -0
- package/dist/facet-buckets.test.d.ts +1 -0
- package/dist/facet-buckets.test.js +296 -0
- package/dist/features.d.ts +5 -0
- package/dist/features.js +30 -0
- package/dist/filter-operators.d.ts +16 -0
- package/dist/filter-operators.js +69 -0
- package/dist/filter-operators.test.d.ts +1 -0
- package/dist/filter-operators.test.js +135 -0
- package/dist/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/hyperformula-adapter.test.d.ts +1 -0
- package/dist/hyperformula-adapter.test.js +205 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.js +8 -2
- package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
- package/dist/keyboard-handlers.coverage.test.js +495 -0
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +202 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +390 -0
- package/dist/menus.test.d.ts +1 -0
- package/dist/menus.test.js +560 -0
- package/dist/named-views.coverage.test.d.ts +1 -0
- package/dist/named-views.coverage.test.js +180 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-drag.d.ts +49 -0
- package/dist/row-drag.js +221 -0
- package/dist/row-drag.test.d.ts +1 -0
- package/dist/row-drag.test.js +142 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/row-resize.test.d.ts +1 -0
- package/dist/row-resize.test.js +329 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +89 -0
- package/dist/scroll-sync.test.d.ts +1 -0
- package/dist/scroll-sync.test.js +290 -0
- package/dist/selection.d.ts +32 -0
- package/dist/selection.js +427 -0
- package/dist/selection.multi-range.test.d.ts +1 -0
- package/dist/selection.multi-range.test.js +55 -0
- package/dist/selection.test.d.ts +1 -0
- package/dist/selection.test.js +647 -0
- package/dist/server-data-source.coverage.test.d.ts +1 -0
- package/dist/server-data-source.coverage.test.js +154 -0
- package/dist/spreadsheet.d.ts +110 -0
- package/dist/spreadsheet.js +242 -0
- package/dist/spreadsheet.test.d.ts +1 -0
- package/dist/spreadsheet.test.js +446 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/sv-grid-scrollbar.js +13 -1
- package/dist/svgrid-wrapper.types.d.ts +31 -1
- package/dist/svgrid.behavior.test.js +20 -0
- package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
- package/dist/svgrid.comments-autocomplete.test.js +96 -0
- package/dist/svgrid.context-menu.test.d.ts +1 -0
- package/dist/svgrid.context-menu.test.js +102 -0
- package/dist/svgrid.interaction.test.js +31 -0
- package/dist/svgrid.new-features.wrapper.test.js +64 -6
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/test-setup.js +9 -3
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +45 -0
- package/dist/virtualization/scroll-scaling.js +99 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +127 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
- package/dist/virtualization/virtualizer.d.ts +7 -0
- package/dist/virtualization/virtualizer.js +30 -0
- package/package.json +2 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +648 -0
- package/src/SvGrid.controller.svelte.ts +2352 -0
- package/src/SvGrid.css +2012 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +2346 -8742
- package/src/SvGrid.types.ts +537 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/aligned-grids.test.ts +80 -0
- package/src/aligned-grids.ts +87 -0
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +683 -0
- package/src/cell-render.test.ts +451 -0
- package/src/cell-render.ts +426 -0
- package/src/cell-values.ts +114 -0
- package/src/chart-export.test.ts +370 -0
- package/src/chart.coverage.test.ts +814 -0
- package/src/chart.ts +1352 -47
- package/src/clipboard.test.ts +780 -0
- package/src/clipboard.ts +552 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/column-groups.test.ts +59 -0
- package/src/column-groups.ts +80 -0
- package/src/column-types.test.ts +68 -0
- package/src/column-types.ts +82 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +86 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +669 -0
- package/src/editors/cell-editors.coverage.test.ts +156 -0
- package/src/editors/cell-editors.ts +1 -0
- package/src/facet-buckets.test.ts +353 -0
- package/src/facet-buckets.ts +67 -0
- package/src/features.ts +128 -0
- package/src/filter-operators.test.ts +174 -0
- package/src/filter-operators.ts +87 -0
- package/src/hyperformula-adapter.test.ts +256 -0
- package/src/hyperformula-adapter.ts +124 -0
- package/src/index.ts +49 -0
- package/src/keyboard-handlers.coverage.test.ts +560 -0
- package/src/keyboard-handlers.ts +353 -0
- package/src/keyboard.ts +97 -97
- package/src/menus.test.ts +620 -0
- package/src/menus.ts +555 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -0
- package/src/row-drag.test.ts +168 -0
- package/src/row-drag.ts +255 -0
- package/src/row-resize.test.ts +369 -0
- package/src/row-resize.ts +171 -0
- package/src/scroll-sync.test.ts +330 -0
- package/src/scroll-sync.ts +218 -0
- package/src/selection.multi-range.test.ts +61 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +579 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +489 -0
- package/src/spreadsheet.ts +304 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +31 -1
- package/src/svgrid.behavior.test.ts +22 -0
- package/src/svgrid.comments-autocomplete.test.ts +112 -0
- package/src/svgrid.context-menu.test.ts +126 -0
- package/src/svgrid.interaction.test.ts +30 -0
- package/src/svgrid.new-features.wrapper.test.ts +67 -6
- package/src/svgrid.wrapper.test.ts +27 -1
- package/src/test-setup.ts +9 -6
- package/src/virtualization/scroll-scaling.test.ts +148 -0
- package/src/virtualization/scroll-scaling.ts +121 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
- package/src/virtualization/virtualizer.ts +26 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import "./sv-grid-scrollbar";
|
|
2
|
+
import { getKeyboardIntent, getNextActiveCell } from "./keyboard";
|
|
3
|
+
export function createKeyboard(ctx) {
|
|
4
|
+
function onGridKeyDown(event) {
|
|
5
|
+
// Only the grid root drives navigation - keys on header buttons, menus,
|
|
6
|
+
// or the cell editor are handled by those controls themselves.
|
|
7
|
+
if (event.target !== event.currentTarget)
|
|
8
|
+
return;
|
|
9
|
+
if (ctx.editingCell)
|
|
10
|
+
return;
|
|
11
|
+
if ((event.ctrlKey || event.metaKey) && !event.altKey) {
|
|
12
|
+
const lower = event.key.toLowerCase();
|
|
13
|
+
if (lower === "c") {
|
|
14
|
+
event.preventDefault();
|
|
15
|
+
ctx.copySelectionToClipboard();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (lower === "v") {
|
|
19
|
+
// Secure context: read via the async Clipboard API and swallow the
|
|
20
|
+
// key. Insecure context (no navigator.clipboard): DON'T preventDefault
|
|
21
|
+
// so the browser delivers a native `paste` event to `onGridPaste`.
|
|
22
|
+
if (typeof navigator.clipboard?.readText === "function") {
|
|
23
|
+
event.preventDefault();
|
|
24
|
+
void ctx.pasteFromClipboard();
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (lower === "x") {
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
void ctx.cutSelectionToClipboard();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
// Ctrl+Z (Cmd+Z) undoes the most recent cell edit. Ctrl+Shift+Z
|
|
34
|
+
// and Ctrl+Y both redo. Mirrors VSCode / Sheets / Excel.
|
|
35
|
+
if (lower === "z" && !event.shiftKey) {
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
if (ctx.historyPtr >= 0) {
|
|
38
|
+
const step = ctx.history[ctx.historyPtr];
|
|
39
|
+
if (step) {
|
|
40
|
+
ctx.applyHistoryStep(step, 'undo');
|
|
41
|
+
ctx.historyPtr -= 1;
|
|
42
|
+
ctx.historyVersion += 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if ((lower === "z" && event.shiftKey) || lower === "y") {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
if (ctx.historyPtr < ctx.history.length - 1) {
|
|
50
|
+
const step = ctx.history[ctx.historyPtr + 1];
|
|
51
|
+
if (step) {
|
|
52
|
+
ctx.applyHistoryStep(step, 'redo');
|
|
53
|
+
ctx.historyPtr += 1;
|
|
54
|
+
ctx.historyVersion += 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// Ctrl+F opens the find overlay.
|
|
60
|
+
if (lower === "f") {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
ctx.findOpen = true;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Esc closes find when nothing else owns the key.
|
|
67
|
+
if (event.key === "Escape" && ctx.findOpen) {
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
ctx.findOpen = false;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// Delete / Backspace clears every editable cell in the selection
|
|
73
|
+
// range. No clipboard interaction - this is the "blank the cells I
|
|
74
|
+
// have selected" gesture, distinct from Ctrl/Cmd+X.
|
|
75
|
+
if ((event.key === "Delete" || event.key === "Backspace") &&
|
|
76
|
+
!event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
77
|
+
if (ctx.clearSelectedCells()) {
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const current = ctx.grid.getState().activeCell ?? {
|
|
83
|
+
rowIndex: 0,
|
|
84
|
+
colIndex: 0,
|
|
85
|
+
cellId: null,
|
|
86
|
+
};
|
|
87
|
+
if (event.key === "F2") {
|
|
88
|
+
event.preventDefault();
|
|
89
|
+
ctx.onCellDoubleClick(current.rowIndex, current.colIndex);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const intent = getKeyboardIntent(event);
|
|
93
|
+
if (intent === "noop") {
|
|
94
|
+
// A printable character on the active cell starts editing seeded with it.
|
|
95
|
+
if (event.key.length === 1 &&
|
|
96
|
+
!event.ctrlKey &&
|
|
97
|
+
!event.metaKey &&
|
|
98
|
+
!event.altKey) {
|
|
99
|
+
if (ctx.startEditingWithChar(current.rowIndex, current.colIndex, event.key)) {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
if (intent === "clearCells") {
|
|
107
|
+
// Excel `Delete` - clear contents of every cell in the selection
|
|
108
|
+
// (or the active cell if no range). Formatting is left alone, only
|
|
109
|
+
// the underlying value is wiped.
|
|
110
|
+
ctx.clearSelectedCellValues();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (intent === "activate") {
|
|
114
|
+
const row = ctx.allRows[current.rowIndex];
|
|
115
|
+
const column = ctx.allColumns[current.colIndex];
|
|
116
|
+
if (event.key === " ") {
|
|
117
|
+
row?.toggleSelected?.();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (event.ctrlKey && row?.getCanExpand?.()) {
|
|
121
|
+
row.toggleExpanded?.();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (column?.columnDef.editorType === "checkbox") {
|
|
125
|
+
ctx.toggleBooleanCell(current.rowIndex, current.colIndex);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
ctx.onCellDoubleClick(current.rowIndex, current.colIndex);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
// Excel-style page step: PageUp/PageDown jump by ~one visible
|
|
132
|
+
// page minus one row of overlap. Falls back to grid pagination's
|
|
133
|
+
// pageSize when pagination is on, or 10 as a final fallback.
|
|
134
|
+
function pageStep() {
|
|
135
|
+
const pageSize = ctx.grid.getState().pagination?.pageSize;
|
|
136
|
+
if (pageSize && pageSize > 0)
|
|
137
|
+
return pageSize;
|
|
138
|
+
const clientHeight = ctx.scrollContainer?.clientHeight ?? 0;
|
|
139
|
+
const headerHeight = ctx.headerHeight ?? 0;
|
|
140
|
+
const rowHeight = typeof ctx.props.rowHeight === 'function'
|
|
141
|
+
? (ctx.props.rowHeight(current.rowIndex) ?? 30)
|
|
142
|
+
: (ctx.props.rowHeight ?? 30);
|
|
143
|
+
const usable = Math.max(0, clientHeight - headerHeight);
|
|
144
|
+
return Math.max(1, Math.floor(usable / Math.max(rowHeight, 1)) - 1);
|
|
145
|
+
}
|
|
146
|
+
const next = getNextActiveCell(current, intent, {
|
|
147
|
+
maxRow: Math.max(ctx.allRows.length - 1, 0),
|
|
148
|
+
maxCol: Math.max(ctx.allColumns.length - 1, 0),
|
|
149
|
+
pageSize: pageStep(),
|
|
150
|
+
});
|
|
151
|
+
ctx.setActiveCell(next.rowIndex, next.colIndex);
|
|
152
|
+
ctx.scrollActiveCellIntoView(next.rowIndex, next.colIndex);
|
|
153
|
+
// Shift extends the selection ONLY for arrow keys (Excel-style).
|
|
154
|
+
// Shift+Enter / Shift+Tab just change direction; they don't grow the
|
|
155
|
+
// range, otherwise hammering Shift+Tab to backspace through a row
|
|
156
|
+
// would paint a creeping rectangle behind the cursor.
|
|
157
|
+
const shouldExtend = event.shiftKey &&
|
|
158
|
+
(intent === "moveLeft" ||
|
|
159
|
+
intent === "moveRight" ||
|
|
160
|
+
intent === "moveUp" ||
|
|
161
|
+
intent === "moveDown");
|
|
162
|
+
if (shouldExtend)
|
|
163
|
+
ctx.extendSelection(next.rowIndex, next.colIndex);
|
|
164
|
+
else
|
|
165
|
+
ctx.setSelection(next.rowIndex, next.colIndex);
|
|
166
|
+
}
|
|
167
|
+
function onWindowKeydown(event) {
|
|
168
|
+
if (event.key === "Escape" && (ctx.columnMenuFor || ctx.operatorMenuFor)) {
|
|
169
|
+
ctx.closeMenus();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function onHeaderSortClick(event, columnId) {
|
|
173
|
+
const column = ctx.allColumns.find((entry) => entry.id === columnId);
|
|
174
|
+
if (!column?.getCanSort?.())
|
|
175
|
+
return;
|
|
176
|
+
const clauses = ctx.grid.getState().sorting ?? [];
|
|
177
|
+
const current = clauses.find((entry) => entry.id === columnId);
|
|
178
|
+
if (!event.shiftKey) {
|
|
179
|
+
// Single-sort: cycle this column's direction and clear all other
|
|
180
|
+
// sorts. Cycle order: none → asc → desc → none.
|
|
181
|
+
const nextClause = !current
|
|
182
|
+
? [{ id: columnId, desc: false }]
|
|
183
|
+
: current.desc
|
|
184
|
+
? []
|
|
185
|
+
: [{ id: columnId, desc: true }];
|
|
186
|
+
ctx.grid.store.setState((prev) => ({ ...prev, sorting: nextClause }));
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// Shift-click: append/toggle as part of a multi-sort.
|
|
190
|
+
const nextClause = !current
|
|
191
|
+
? [...clauses, { id: columnId, desc: false }]
|
|
192
|
+
: current.desc
|
|
193
|
+
? clauses.filter((entry) => entry.id !== columnId)
|
|
194
|
+
: clauses.map((entry) => entry.id === columnId ? { ...entry, desc: true } : entry);
|
|
195
|
+
ctx.grid.store.setState((prev) => ({ ...prev, sorting: nextClause }));
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
onGridKeyDown,
|
|
199
|
+
onWindowKeydown,
|
|
200
|
+
onHeaderSortClick,
|
|
201
|
+
};
|
|
202
|
+
}
|
package/dist/menus.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type RowData, type TableFeatures } from "./index";
|
|
2
|
+
import "./sv-grid-scrollbar";
|
|
3
|
+
import type { FilterOperator } from "./SvGrid.types";
|
|
4
|
+
export declare function createMenus<TFeatures extends TableFeatures = TableFeatures, TData extends RowData = RowData>(ctx: any): {
|
|
5
|
+
updateFilterRow: (columnId: string, value: string) => void;
|
|
6
|
+
updateFilterOperator: (columnId: string, operator: FilterOperator) => void;
|
|
7
|
+
updateFilterMenuValue: (columnId: string, value: string) => void;
|
|
8
|
+
updateFilterMenuValueTo: (columnId: string, valueTo: string) => void;
|
|
9
|
+
toggleCheckboxWithKeyboard: (event: KeyboardEvent, toggle: () => void) => void;
|
|
10
|
+
isColumnFiltered: (columnId: string) => boolean;
|
|
11
|
+
closeMenus: () => void;
|
|
12
|
+
openChooseColumns: (event: MouseEvent) => void;
|
|
13
|
+
openColumnMenu: (event: MouseEvent, columnId: string) => void;
|
|
14
|
+
openFilterMenu: (event: MouseEvent, columnId: string) => void;
|
|
15
|
+
openOperatorMenu: (event: MouseEvent, columnId: string) => void;
|
|
16
|
+
sortColumnFromMenu: (columnId: string, desc: boolean) => void;
|
|
17
|
+
clearColumnSort: (columnId: string) => void;
|
|
18
|
+
groupByColumnFromMenu: (columnId: string) => void;
|
|
19
|
+
clearGroupingFromMenu: (columnId: string) => void;
|
|
20
|
+
isFacetChecked: (columnId: string, value: string) => any;
|
|
21
|
+
toggleFacetValue: (columnId: string, value: string) => void;
|
|
22
|
+
isAllFacetsChecked: (columnId: string) => boolean;
|
|
23
|
+
toggleAllFacets: (columnId: string) => void;
|
|
24
|
+
clearColumnFilter: (columnId: string) => void;
|
|
25
|
+
changePage: (delta: number) => void;
|
|
26
|
+
goToPage: (pageIndex: number) => void;
|
|
27
|
+
setPageSize: (pageSize: number) => void;
|
|
28
|
+
openContextMenu: (event: MouseEvent, rowIndex: number, colIndex: number, columnId: string) => void;
|
|
29
|
+
closeContextMenu: () => void;
|
|
30
|
+
contextMenuItems: () => {
|
|
31
|
+
key: string;
|
|
32
|
+
label: string;
|
|
33
|
+
separator?: boolean;
|
|
34
|
+
disabled?: boolean;
|
|
35
|
+
run?: () => void;
|
|
36
|
+
}[];
|
|
37
|
+
saveComment: () => void;
|
|
38
|
+
removeComment: () => void;
|
|
39
|
+
closeCommentEditor: () => void;
|
|
40
|
+
};
|
package/dist/menus.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import "./sv-grid-scrollbar";
|
|
2
|
+
import { clampMenuX, cssEscape, } from "./SvGrid.helpers";
|
|
3
|
+
export function createMenus(ctx) {
|
|
4
|
+
function updateFilterRow(columnId, value) {
|
|
5
|
+
ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
|
|
6
|
+
const current = ctx.filterMenuValues[columnId] ?? {
|
|
7
|
+
operator: "contains",
|
|
8
|
+
value: "",
|
|
9
|
+
};
|
|
10
|
+
ctx.filterMenuValues = {
|
|
11
|
+
...ctx.filterMenuValues,
|
|
12
|
+
[columnId]: { ...current, value },
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function updateFilterOperator(columnId, operator) {
|
|
16
|
+
const current = ctx.filterMenuValues[columnId] ?? {
|
|
17
|
+
operator: "contains",
|
|
18
|
+
value: "",
|
|
19
|
+
};
|
|
20
|
+
ctx.filterMenuValues = {
|
|
21
|
+
...ctx.filterMenuValues,
|
|
22
|
+
[columnId]: { ...current, operator },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function updateFilterMenuValue(columnId, value) {
|
|
26
|
+
const current = ctx.filterMenuValues[columnId] ?? {
|
|
27
|
+
operator: "contains",
|
|
28
|
+
value: "",
|
|
29
|
+
};
|
|
30
|
+
ctx.filterMenuValues = {
|
|
31
|
+
...ctx.filterMenuValues,
|
|
32
|
+
[columnId]: { ...current, value },
|
|
33
|
+
};
|
|
34
|
+
ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
|
|
35
|
+
}
|
|
36
|
+
/** Upper-bound input for the `between` operator. Only relevant when
|
|
37
|
+
* the column's current operator is `between`; harmless to call
|
|
38
|
+
* otherwise. */
|
|
39
|
+
function updateFilterMenuValueTo(columnId, valueTo) {
|
|
40
|
+
const current = ctx.filterMenuValues[columnId] ?? {
|
|
41
|
+
operator: "between",
|
|
42
|
+
value: "",
|
|
43
|
+
};
|
|
44
|
+
ctx.filterMenuValues = {
|
|
45
|
+
...ctx.filterMenuValues,
|
|
46
|
+
[columnId]: { ...current, valueTo },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function toggleCheckboxWithKeyboard(event, toggle) {
|
|
50
|
+
if (event.key !== "Enter" && event.key !== " ")
|
|
51
|
+
return;
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
toggle();
|
|
54
|
+
}
|
|
55
|
+
function isColumnFiltered(columnId) {
|
|
56
|
+
if (ctx.valueFilters[columnId])
|
|
57
|
+
return true;
|
|
58
|
+
const menuFilter = ctx.filterMenuValues[columnId];
|
|
59
|
+
return Boolean(menuFilter &&
|
|
60
|
+
(menuFilter.operator === "isBlank" || menuFilter.value.trim()));
|
|
61
|
+
}
|
|
62
|
+
function closeMenus() {
|
|
63
|
+
ctx.columnMenuFor = null;
|
|
64
|
+
ctx.filterMenuFor = null;
|
|
65
|
+
ctx.operatorMenuFor = null;
|
|
66
|
+
ctx.chooseColumnsPos = null;
|
|
67
|
+
}
|
|
68
|
+
function openChooseColumns(event) {
|
|
69
|
+
event.stopPropagation();
|
|
70
|
+
// Submenu behavior: keep the parent operations menu open and float the
|
|
71
|
+
// Choose Columns panel out to the right of it (AG-Grid style). Falls
|
|
72
|
+
// back to below the item if the popover would clip the viewport.
|
|
73
|
+
const trigger = event.currentTarget;
|
|
74
|
+
const menuEl = trigger.closest(".sv-grid-menu");
|
|
75
|
+
const anchor = (menuEl ?? trigger).getBoundingClientRect();
|
|
76
|
+
const panelWidth = 240;
|
|
77
|
+
const wantRight = anchor.right + 4;
|
|
78
|
+
const x = wantRight + panelWidth + 8 > window.innerWidth
|
|
79
|
+
? clampMenuX(anchor.left - panelWidth - 4, panelWidth)
|
|
80
|
+
: clampMenuX(wantRight, panelWidth);
|
|
81
|
+
ctx.chooseColumnsPos = { x, y: anchor.top };
|
|
82
|
+
}
|
|
83
|
+
function openColumnMenu(event, columnId) {
|
|
84
|
+
event.stopPropagation();
|
|
85
|
+
const wasOpen = ctx.columnMenuFor === columnId;
|
|
86
|
+
closeMenus();
|
|
87
|
+
if (wasOpen)
|
|
88
|
+
return;
|
|
89
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
90
|
+
ctx.columnMenuPos = {
|
|
91
|
+
x: clampMenuX(rect.right - 240, 240),
|
|
92
|
+
y: rect.bottom + 4,
|
|
93
|
+
};
|
|
94
|
+
ctx.columnMenuTab = "general";
|
|
95
|
+
ctx.columnMenuFor = columnId;
|
|
96
|
+
}
|
|
97
|
+
function openFilterMenu(event, columnId) {
|
|
98
|
+
event.stopPropagation();
|
|
99
|
+
const wasOpen = ctx.filterMenuFor === columnId;
|
|
100
|
+
closeMenus();
|
|
101
|
+
if (wasOpen)
|
|
102
|
+
return;
|
|
103
|
+
// If the consumer is running in row-mode (or any mode where the
|
|
104
|
+
// funnel popover wouldn't render), redirect the click to the
|
|
105
|
+
// inline filter input for this column. Without this the funnel
|
|
106
|
+
// would be a dead button - it visibly clicks but nothing happens
|
|
107
|
+
// because the menu is gated behind `showColumnFiltersEffective`.
|
|
108
|
+
if (!ctx.showColumnFiltersEffective && ctx.showFilterRowEffective) {
|
|
109
|
+
// Find the row-mode filter input and focus it. The input is
|
|
110
|
+
// tagged with `data-svgrid-filter-col` so we can target by id
|
|
111
|
+
// without depending on column position.
|
|
112
|
+
const input = ctx.scrollContainer?.querySelector(`[data-svgrid-filter-col="${cssEscape(columnId)}"]`);
|
|
113
|
+
if (input) {
|
|
114
|
+
input.focus();
|
|
115
|
+
input.select();
|
|
116
|
+
// Brief pulse so the user sees where focus landed.
|
|
117
|
+
input.classList.add("sv-grid-filter-value-pulse");
|
|
118
|
+
setTimeout(() => input.classList.remove("sv-grid-filter-value-pulse"), 700);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
123
|
+
ctx.filterMenuPos = {
|
|
124
|
+
x: clampMenuX(rect.right - 260, 260),
|
|
125
|
+
y: rect.bottom + 4,
|
|
126
|
+
};
|
|
127
|
+
ctx.columnMenuSearch = "";
|
|
128
|
+
ctx.filterMenuFor = columnId;
|
|
129
|
+
}
|
|
130
|
+
function openOperatorMenu(event, columnId) {
|
|
131
|
+
event.stopPropagation();
|
|
132
|
+
const wasOpen = ctx.operatorMenuFor === columnId;
|
|
133
|
+
closeMenus();
|
|
134
|
+
if (wasOpen)
|
|
135
|
+
return;
|
|
136
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
137
|
+
ctx.operatorMenuPos = { x: clampMenuX(rect.left, 184), y: rect.bottom + 4 };
|
|
138
|
+
ctx.operatorMenuFor = columnId;
|
|
139
|
+
}
|
|
140
|
+
function sortColumnFromMenu(columnId, desc) {
|
|
141
|
+
ctx.grid.store.setState((prev) => ({
|
|
142
|
+
...prev,
|
|
143
|
+
sorting: [{ id: columnId, desc }],
|
|
144
|
+
}));
|
|
145
|
+
closeMenus();
|
|
146
|
+
}
|
|
147
|
+
function clearColumnSort(columnId) {
|
|
148
|
+
ctx.grid.store.setState((prev) => ({
|
|
149
|
+
...prev,
|
|
150
|
+
sorting: (prev.sorting ?? []).filter((entry) => entry.id !== columnId),
|
|
151
|
+
}));
|
|
152
|
+
closeMenus();
|
|
153
|
+
}
|
|
154
|
+
function groupByColumnFromMenu(columnId) {
|
|
155
|
+
const current = ctx.grid.getState().grouping ?? [];
|
|
156
|
+
if (current.includes(columnId)) {
|
|
157
|
+
closeMenus();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
ctx.grid.setGrouping([...current, columnId]);
|
|
161
|
+
closeMenus();
|
|
162
|
+
}
|
|
163
|
+
function clearGroupingFromMenu(columnId) {
|
|
164
|
+
const current = ctx.grid.getState().grouping ?? [];
|
|
165
|
+
ctx.grid.setGrouping(current.filter((id) => id !== columnId));
|
|
166
|
+
closeMenus();
|
|
167
|
+
}
|
|
168
|
+
function isFacetChecked(columnId, value) {
|
|
169
|
+
const selected = ctx.valueFilters[columnId];
|
|
170
|
+
return selected ? selected.has(value) : true;
|
|
171
|
+
}
|
|
172
|
+
function toggleFacetValue(columnId, value) {
|
|
173
|
+
const allValues = ctx.columnMenuFacetValues;
|
|
174
|
+
const next = new Set(ctx.valueFilters[columnId] ?? allValues);
|
|
175
|
+
if (next.has(value))
|
|
176
|
+
next.delete(value);
|
|
177
|
+
else
|
|
178
|
+
next.add(value);
|
|
179
|
+
if (next.size === allValues.length) {
|
|
180
|
+
const copy = { ...ctx.valueFilters };
|
|
181
|
+
delete copy[columnId];
|
|
182
|
+
ctx.valueFilters = copy;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
ctx.valueFilters = { ...ctx.valueFilters, [columnId]: next };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function isAllFacetsChecked(columnId) {
|
|
189
|
+
const selected = ctx.valueFilters[columnId];
|
|
190
|
+
return !selected || selected.size >= ctx.columnMenuFacetValues.length;
|
|
191
|
+
}
|
|
192
|
+
function toggleAllFacets(columnId) {
|
|
193
|
+
if (isAllFacetsChecked(columnId)) {
|
|
194
|
+
ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set() };
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const copy = { ...ctx.valueFilters };
|
|
198
|
+
delete copy[columnId];
|
|
199
|
+
ctx.valueFilters = copy;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function clearColumnFilter(columnId) {
|
|
203
|
+
if (ctx.valueFilters[columnId]) {
|
|
204
|
+
const next = { ...ctx.valueFilters };
|
|
205
|
+
delete next[columnId];
|
|
206
|
+
ctx.valueFilters = next;
|
|
207
|
+
}
|
|
208
|
+
if (ctx.filterMenuValues[columnId]) {
|
|
209
|
+
const next = { ...ctx.filterMenuValues };
|
|
210
|
+
delete next[columnId];
|
|
211
|
+
ctx.filterMenuValues = next;
|
|
212
|
+
}
|
|
213
|
+
if (ctx.filterRowValues[columnId]) {
|
|
214
|
+
const next = { ...ctx.filterRowValues };
|
|
215
|
+
delete next[columnId];
|
|
216
|
+
ctx.filterRowValues = next;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function changePage(delta) {
|
|
220
|
+
ctx.grid.setPagination((prev) => ({
|
|
221
|
+
...prev,
|
|
222
|
+
pageIndex: Math.max((prev?.pageIndex ?? 0) + delta, 0),
|
|
223
|
+
}));
|
|
224
|
+
}
|
|
225
|
+
function goToPage(pageIndex) {
|
|
226
|
+
ctx.grid.setPagination((prev) => ({
|
|
227
|
+
...prev,
|
|
228
|
+
pageIndex: Math.max(0, pageIndex),
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
function setPageSize(pageSize) {
|
|
232
|
+
ctx.grid.setPagination((prev) => {
|
|
233
|
+
const oldSize = prev?.pageSize ?? 10;
|
|
234
|
+
const oldIndex = prev?.pageIndex ?? 0;
|
|
235
|
+
// Keep the first visible row in view when the page size changes.
|
|
236
|
+
const firstVisibleRow = oldIndex * oldSize;
|
|
237
|
+
return {
|
|
238
|
+
...prev,
|
|
239
|
+
pageSize,
|
|
240
|
+
pageIndex: Math.max(0, Math.floor(firstVisibleRow / pageSize)),
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
function insertRowAt(t, offset) {
|
|
245
|
+
const data = ctx.internalData.slice();
|
|
246
|
+
const idx = t.row ? data.indexOf(t.row) : -1;
|
|
247
|
+
const at = idx < 0 ? data.length : idx + offset;
|
|
248
|
+
data.splice(at, 0, {});
|
|
249
|
+
ctx.internalData = data;
|
|
250
|
+
}
|
|
251
|
+
function removeRowAt(t) {
|
|
252
|
+
if (!t.row)
|
|
253
|
+
return;
|
|
254
|
+
ctx.internalData = ctx.internalData.filter((r) => r !== t.row);
|
|
255
|
+
}
|
|
256
|
+
function removeColAt(t) {
|
|
257
|
+
ctx.internalColumns = ctx.internalColumns.filter((d) => (d.id ?? d.field) !== t.columnId);
|
|
258
|
+
}
|
|
259
|
+
// Built-in items, keyed. `run` reads the live target passed at click time.
|
|
260
|
+
const builtins = {
|
|
261
|
+
copy: { label: "Copy", run: () => ctx.copySelectionToClipboard() },
|
|
262
|
+
cut: { label: "Cut", run: () => ctx.cutSelectionToClipboard() },
|
|
263
|
+
paste: { label: "Paste", run: () => void ctx.pasteFromClipboard() },
|
|
264
|
+
clear: { label: "Clear", run: () => ctx.clearSelectedCells() },
|
|
265
|
+
row_above: { label: "Insert row above", run: (t) => insertRowAt(t, 0) },
|
|
266
|
+
row_below: { label: "Insert row below", run: (t) => insertRowAt(t, 1) },
|
|
267
|
+
remove_row: { label: "Remove row", run: removeRowAt },
|
|
268
|
+
remove_col: { label: "Remove column", run: removeColAt },
|
|
269
|
+
comment: { label: "Edit comment", run: (t) => openCommentEditor(t) },
|
|
270
|
+
};
|
|
271
|
+
const DEFAULT_ITEMS = [
|
|
272
|
+
"copy", "cut", "paste", "clear", "separator",
|
|
273
|
+
"row_above", "row_below", "remove_row", "separator", "remove_col",
|
|
274
|
+
];
|
|
275
|
+
function openContextMenu(event, rowIndex, colIndex, columnId) {
|
|
276
|
+
if (!ctx.props.contextMenu)
|
|
277
|
+
return;
|
|
278
|
+
event.preventDefault();
|
|
279
|
+
closeMenus();
|
|
280
|
+
ctx.contextMenuFor = null;
|
|
281
|
+
const rowModel = ctx.allRows[rowIndex];
|
|
282
|
+
const row = rowModel?.original ?? null;
|
|
283
|
+
const rowId = rowModel?.id ?? String(rowIndex);
|
|
284
|
+
ctx.contextMenuPos = {
|
|
285
|
+
x: clampMenuX(event.clientX, 220),
|
|
286
|
+
y: Math.max(8, Math.min(event.clientY, window.innerHeight - 16)),
|
|
287
|
+
};
|
|
288
|
+
ctx.contextMenuFor = { rowIndex, colIndex, columnId, rowId, row };
|
|
289
|
+
}
|
|
290
|
+
function closeContextMenu() {
|
|
291
|
+
ctx.contextMenuFor = null;
|
|
292
|
+
}
|
|
293
|
+
// ---- Editable comments ---------------------------------------------
|
|
294
|
+
function openCommentEditor(t) {
|
|
295
|
+
const cur = ctx.noteOverrides?.[t.rowId]?.[t.columnId] ??
|
|
296
|
+
ctx.props.notes?.[t.rowId]?.[t.columnId] ??
|
|
297
|
+
"";
|
|
298
|
+
ctx.commentDraft = cur;
|
|
299
|
+
ctx.commentEditFor = { rowId: t.rowId, columnId: t.columnId, x: ctx.contextMenuPos.x, y: ctx.contextMenuPos.y };
|
|
300
|
+
ctx.contextMenuFor = null;
|
|
301
|
+
}
|
|
302
|
+
function writeNote(rowId, columnId, note) {
|
|
303
|
+
const next = { ...ctx.noteOverrides };
|
|
304
|
+
next[rowId] = { ...(next[rowId] ?? {}), [columnId]: note };
|
|
305
|
+
ctx.noteOverrides = next;
|
|
306
|
+
ctx.props.onNoteChange?.({ rowId, columnId, note });
|
|
307
|
+
}
|
|
308
|
+
function saveComment() {
|
|
309
|
+
const e = ctx.commentEditFor;
|
|
310
|
+
if (!e)
|
|
311
|
+
return;
|
|
312
|
+
writeNote(e.rowId, e.columnId, ctx.commentDraft);
|
|
313
|
+
ctx.commentEditFor = null;
|
|
314
|
+
}
|
|
315
|
+
function removeComment() {
|
|
316
|
+
const e = ctx.commentEditFor;
|
|
317
|
+
if (!e)
|
|
318
|
+
return;
|
|
319
|
+
writeNote(e.rowId, e.columnId, "");
|
|
320
|
+
ctx.commentEditFor = null;
|
|
321
|
+
}
|
|
322
|
+
function closeCommentEditor() {
|
|
323
|
+
ctx.commentEditFor = null;
|
|
324
|
+
}
|
|
325
|
+
/** Resolve the configured items against the current target for rendering. */
|
|
326
|
+
function contextMenuItems() {
|
|
327
|
+
const target = ctx.contextMenuFor;
|
|
328
|
+
if (!target)
|
|
329
|
+
return [];
|
|
330
|
+
const cfg = ctx.props.contextMenu;
|
|
331
|
+
const entries = Array.isArray(cfg)
|
|
332
|
+
? cfg
|
|
333
|
+
: [...DEFAULT_ITEMS, ...(ctx.props.editableComments ? ["separator", "comment"] : [])];
|
|
334
|
+
const out = [];
|
|
335
|
+
for (const entry of entries) {
|
|
336
|
+
if (entry === "separator") {
|
|
337
|
+
out.push({ key: "sep" + out.length, label: "", separator: true });
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (typeof entry === "string") {
|
|
341
|
+
const b = builtins[entry];
|
|
342
|
+
if (!b)
|
|
343
|
+
continue;
|
|
344
|
+
out.push({ key: entry, label: b.label, run: () => b.run(target) });
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (entry.hidden?.(target))
|
|
348
|
+
continue;
|
|
349
|
+
out.push({
|
|
350
|
+
key: entry.key,
|
|
351
|
+
label: entry.label,
|
|
352
|
+
disabled: entry.disabled?.(target) ?? false,
|
|
353
|
+
run: () => entry.action(target),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
// Drop leading/trailing/double separators.
|
|
357
|
+
return out.filter((it, i, a) => !it.separator || (i > 0 && i < a.length - 1 && !a[i - 1]?.separator));
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
updateFilterRow,
|
|
361
|
+
updateFilterOperator,
|
|
362
|
+
updateFilterMenuValue,
|
|
363
|
+
updateFilterMenuValueTo,
|
|
364
|
+
toggleCheckboxWithKeyboard,
|
|
365
|
+
isColumnFiltered,
|
|
366
|
+
closeMenus,
|
|
367
|
+
openChooseColumns,
|
|
368
|
+
openColumnMenu,
|
|
369
|
+
openFilterMenu,
|
|
370
|
+
openOperatorMenu,
|
|
371
|
+
sortColumnFromMenu,
|
|
372
|
+
clearColumnSort,
|
|
373
|
+
groupByColumnFromMenu,
|
|
374
|
+
clearGroupingFromMenu,
|
|
375
|
+
isFacetChecked,
|
|
376
|
+
toggleFacetValue,
|
|
377
|
+
isAllFacetsChecked,
|
|
378
|
+
toggleAllFacets,
|
|
379
|
+
clearColumnFilter,
|
|
380
|
+
changePage,
|
|
381
|
+
goToPage,
|
|
382
|
+
setPageSize,
|
|
383
|
+
openContextMenu,
|
|
384
|
+
closeContextMenu,
|
|
385
|
+
contextMenuItems,
|
|
386
|
+
saveComment,
|
|
387
|
+
removeComment,
|
|
388
|
+
closeCommentEditor,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|