@svgrid/grid 1.0.2 → 1.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/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 +570 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +429 -0
- package/dist/SvGrid.controller.svelte.js +1732 -0
- package/dist/SvGrid.css +1709 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.svelte +344 -7043
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +436 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +527 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -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 +356 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/core.d.ts +1 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +24 -0
- package/dist/editing.js +343 -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/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/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +197 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +389 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +86 -0
- package/dist/selection.d.ts +26 -0
- package/dist/selection.js +387 -0
- package/dist/spreadsheet.d.ts +80 -0
- package/dist/spreadsheet.js +194 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/svgrid-wrapper.types.d.ts +12 -1
- 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.new-features.wrapper.test.js +30 -4
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +28 -0
- package/dist/virtualization/scroll-scaling.js +64 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +86 -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 +1 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +570 -0
- package/src/SvGrid.controller.svelte.ts +2195 -0
- package/src/SvGrid.css +1747 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +348 -7043
- package/src/SvGrid.types.ts +456 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +663 -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 +731 -0
- package/src/clipboard.ts +524 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +8 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +513 -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 +37 -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 +554 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -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 +216 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +545 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +445 -0
- package/src/spreadsheet.ts +246 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +12 -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 +65 -4
- 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
package/src/editing.ts
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
// editing handlers extracted from the controller. Imperative event handlers
|
|
2
|
+
// reading/writing controller state via the `ctx` handle; the reactive core
|
|
3
|
+
// ($state/$derived/$effect) stays in the controller.
|
|
4
|
+
import {
|
|
5
|
+
applyExcelFilter,
|
|
6
|
+
normalizeForFilter,
|
|
7
|
+
createColumnVirtualizer,
|
|
8
|
+
createCoreRowModel,
|
|
9
|
+
createExpandedRowModel,
|
|
10
|
+
createFilteredRowModel,
|
|
11
|
+
createGroupedRowModel,
|
|
12
|
+
createPaginatedRowModel,
|
|
13
|
+
createSvelteVirtualizer,
|
|
14
|
+
createSortedRowModel,
|
|
15
|
+
createSvGrid,
|
|
16
|
+
filterFns,
|
|
17
|
+
getGridCellA11yProps,
|
|
18
|
+
getGridCellDomId,
|
|
19
|
+
getGridHeaderA11yProps,
|
|
20
|
+
getGridRootA11yProps,
|
|
21
|
+
getGridRowA11yProps,
|
|
22
|
+
parseEditorValue,
|
|
23
|
+
normalizeEditorOptions,
|
|
24
|
+
sortFns,
|
|
25
|
+
tableFeatures,
|
|
26
|
+
rowSortingFeature,
|
|
27
|
+
columnFilteringFeature,
|
|
28
|
+
columnGroupingFeature,
|
|
29
|
+
type CellContext,
|
|
30
|
+
type EditorContext,
|
|
31
|
+
type CellEditorOption,
|
|
32
|
+
type CellEditorType,
|
|
33
|
+
type CellFormatter,
|
|
34
|
+
type CellFormatConfig,
|
|
35
|
+
type Column,
|
|
36
|
+
type ColumnDef,
|
|
37
|
+
type Row,
|
|
38
|
+
type RowData,
|
|
39
|
+
type SvGridApi,
|
|
40
|
+
type TableFeatures,
|
|
41
|
+
} from "./index";
|
|
42
|
+
import "./sv-grid-scrollbar";
|
|
43
|
+
import type { Snippet } from "svelte";
|
|
44
|
+
import { getKeyboardIntent, getNextActiveCell } from "./keyboard";
|
|
45
|
+
import {
|
|
46
|
+
formatNumericWithConfig,
|
|
47
|
+
getDateFormatter,
|
|
48
|
+
resolveDatePattern,
|
|
49
|
+
} from "./cell-formatting";
|
|
50
|
+
import {
|
|
51
|
+
RenderSnippetConfig,
|
|
52
|
+
RenderComponentConfig,
|
|
53
|
+
} from "./render-component";
|
|
54
|
+
import { buildFillPattern } from "./fill-patterns";
|
|
55
|
+
import { buildSparkline, toSparklineValues } from "./sparkline";
|
|
56
|
+
import {
|
|
57
|
+
resolveCellFormat,
|
|
58
|
+
computeColumnStat,
|
|
59
|
+
formatsNeedingStats,
|
|
60
|
+
type ColumnStat,
|
|
61
|
+
type ResolvedCellFormat,
|
|
62
|
+
} from "./conditional-formatting";
|
|
63
|
+
import SvGridDropdown from "./SvGridDropdown.svelte";
|
|
64
|
+
import type {
|
|
65
|
+
Props,
|
|
66
|
+
SelectionPoint,
|
|
67
|
+
SelectionRange,
|
|
68
|
+
CellEditState,
|
|
69
|
+
FilterOperator,
|
|
70
|
+
FilterOption,
|
|
71
|
+
MenuPosition,
|
|
72
|
+
} from "./SvGrid.types";
|
|
73
|
+
import {
|
|
74
|
+
cfTextStyle,
|
|
75
|
+
fmtStat,
|
|
76
|
+
getCellKey,
|
|
77
|
+
resolveClassList,
|
|
78
|
+
toDateInputValue,
|
|
79
|
+
toDateTimeLocalInputValue,
|
|
80
|
+
getEditableInputValue,
|
|
81
|
+
getEditorInputType,
|
|
82
|
+
toValueArray,
|
|
83
|
+
getOptionLabel,
|
|
84
|
+
getOptionColor,
|
|
85
|
+
colorfulChipStyle,
|
|
86
|
+
getEditorClass,
|
|
87
|
+
asDate,
|
|
88
|
+
clampMenuX,
|
|
89
|
+
cssEscape,
|
|
90
|
+
rawToNumber,
|
|
91
|
+
formatFacetNumber,
|
|
92
|
+
formatFacetDate,
|
|
93
|
+
} from "./SvGrid.helpers";
|
|
94
|
+
import { createSelection } from "./selection";
|
|
95
|
+
import { createColumns } from "./columns";
|
|
96
|
+
import { createGridApi } from "./build-api";
|
|
97
|
+
import { createClipboard } from "./clipboard";
|
|
98
|
+
import {
|
|
99
|
+
filterOperatorOptions,
|
|
100
|
+
fallbackOperatorOption,
|
|
101
|
+
TEXT_OPERATORS,
|
|
102
|
+
NUMBER_OPERATORS,
|
|
103
|
+
DATE_OPERATORS,
|
|
104
|
+
CHECKBOX_OPERATORS,
|
|
105
|
+
operatorOption,
|
|
106
|
+
operatorsForColumn,
|
|
107
|
+
defaultOperatorFor,
|
|
108
|
+
operatorLabelFor,
|
|
109
|
+
} from "./filter-operators";
|
|
110
|
+
import {
|
|
111
|
+
type FacetBucket,
|
|
112
|
+
isBucketableColumn,
|
|
113
|
+
buildBuckets,
|
|
114
|
+
isInBucket,
|
|
115
|
+
} from "./facet-buckets";
|
|
116
|
+
import {
|
|
117
|
+
getColumnBaseValue,
|
|
118
|
+
isGroupRow,
|
|
119
|
+
toolPanelHeaderLabel,
|
|
120
|
+
formatSummaryNumeric,
|
|
121
|
+
getColumnAlign,
|
|
122
|
+
getPinnedCellValue,
|
|
123
|
+
getColumnAccessorValue,
|
|
124
|
+
columnDefMatchesId,
|
|
125
|
+
} from "./cell-values";
|
|
126
|
+
|
|
127
|
+
type HistoryStep = {
|
|
128
|
+
rowId: string;
|
|
129
|
+
columnId: string;
|
|
130
|
+
field: string;
|
|
131
|
+
before: unknown;
|
|
132
|
+
after: unknown;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export function createEditing<
|
|
136
|
+
TFeatures extends TableFeatures = TableFeatures,
|
|
137
|
+
TData extends RowData = RowData,
|
|
138
|
+
>(ctx: any) {
|
|
139
|
+
/**
|
|
140
|
+
* Resolve `columnDef.editable` for one row × column pair.
|
|
141
|
+
*
|
|
142
|
+
* - `undefined` / `true` → editable
|
|
143
|
+
* - `false` → not editable (column-level lockdown, fastest path)
|
|
144
|
+
* - function → call it with the cell's context and honour the
|
|
145
|
+
* boolean it returns (cell-level lockdown - used for things like
|
|
146
|
+
* "this field is owned by the server, this one isn't, by row")
|
|
147
|
+
*
|
|
148
|
+
* Called from every editing entry point: double-click, type-to-edit,
|
|
149
|
+
* fill-handle drag, Delete-clear, clipboard paste. A central helper
|
|
150
|
+
* keeps the cell-level callback signature consistent and saves a
|
|
151
|
+
* lookup of CellContext at each site.
|
|
152
|
+
*/
|
|
153
|
+
function isCellEditable(column: Column<TData>, row?: Row<TData>): boolean {
|
|
154
|
+
const editable = column.columnDef.editable;
|
|
155
|
+
if (editable === false) return false;
|
|
156
|
+
if (typeof editable !== "function") return true;
|
|
157
|
+
if (!row) return true;
|
|
158
|
+
const cellCtx: CellContext<TData> = {
|
|
159
|
+
cell: {
|
|
160
|
+
id: `${row.id}_${column.id}`,
|
|
161
|
+
row,
|
|
162
|
+
column,
|
|
163
|
+
getValue: () => getColumnBaseValue(row, column),
|
|
164
|
+
getContext: () => cellCtx,
|
|
165
|
+
},
|
|
166
|
+
row,
|
|
167
|
+
column,
|
|
168
|
+
table: ctx.grid,
|
|
169
|
+
getValue: () => getColumnBaseValue(row, column),
|
|
170
|
+
};
|
|
171
|
+
try {
|
|
172
|
+
return editable(cellCtx) !== false;
|
|
173
|
+
} catch {
|
|
174
|
+
// A throwing predicate shouldn't crash the grid - log and treat
|
|
175
|
+
// as not-editable so any error biases toward safety.
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Variant that takes raw row + column indices for the rare callers
|
|
181
|
+
* (fill-handle, paste, Delete) that already work in index-space. */
|
|
182
|
+
function isCellEditableAt(rowIndex: number, colIndex: number): boolean {
|
|
183
|
+
const row = ctx.allRows[rowIndex];
|
|
184
|
+
const column = ctx.allColumns[colIndex];
|
|
185
|
+
if (!column) return false;
|
|
186
|
+
return isCellEditable(column, row);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function getRowColumnValue(row: Row<TData>, columnId: string) {
|
|
190
|
+
const column = ctx.allColumns.find((entry: any) => entry.id === columnId);
|
|
191
|
+
return column
|
|
192
|
+
? getColumnBaseValue(row, column)
|
|
193
|
+
: row.getCellValueByColumnId(columnId);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function getCellDisplayValue(
|
|
197
|
+
rowId: string,
|
|
198
|
+
columnId: string,
|
|
199
|
+
baseValue: unknown,
|
|
200
|
+
) {
|
|
201
|
+
const key = getCellKey(rowId, columnId);
|
|
202
|
+
return key in ctx.editedCellValues ? ctx.editedCellValues[key] : baseValue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Start editing seeded with a typed character (Excel-style type-to-edit). */
|
|
206
|
+
function startEditingWithChar(
|
|
207
|
+
rowIndex: number,
|
|
208
|
+
colIndex: number,
|
|
209
|
+
char: string,
|
|
210
|
+
): boolean {
|
|
211
|
+
if (!ctx.editingEnabled) return false;
|
|
212
|
+
const row = ctx.allRows[rowIndex];
|
|
213
|
+
const column = ctx.allColumns[colIndex];
|
|
214
|
+
if (!row || !column || isGroupRow(row)) return false;
|
|
215
|
+
if (!isCellEditable(column, row)) return false;
|
|
216
|
+
const editorType = (column.columnDef.editorType ??
|
|
217
|
+
"text") as CellEditorType;
|
|
218
|
+
if (
|
|
219
|
+
editorType === "checkbox" ||
|
|
220
|
+
editorType === "date" ||
|
|
221
|
+
editorType === "datetime" ||
|
|
222
|
+
editorType === "list" ||
|
|
223
|
+
editorType === "chips"
|
|
224
|
+
) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (editorType === "number" && !/[0-9.+\-]/.test(char)) return false;
|
|
228
|
+
ctx.editorSelectAll = false;
|
|
229
|
+
ctx.editingCell = {
|
|
230
|
+
rowId: row.id,
|
|
231
|
+
columnId: column.id,
|
|
232
|
+
editorType,
|
|
233
|
+
value: char,
|
|
234
|
+
};
|
|
235
|
+
ctx.setActiveCell(rowIndex, colIndex);
|
|
236
|
+
ctx.setSelection(rowIndex, colIndex);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function saveEditingCell() {
|
|
241
|
+
if (!ctx.editingCell) return;
|
|
242
|
+
const row = ctx.allRows.find((entry: any) => entry.id === ctx.editingCell?.rowId);
|
|
243
|
+
const column = ctx.allColumns.find(
|
|
244
|
+
(entry: any) => entry.id === ctx.editingCell?.columnId,
|
|
245
|
+
);
|
|
246
|
+
const parsedValue = parseEditorValue(
|
|
247
|
+
ctx.editingCell.editorType,
|
|
248
|
+
ctx.editingCell.value,
|
|
249
|
+
{
|
|
250
|
+
multiple: column?.columnDef.editorMultiple === true,
|
|
251
|
+
},
|
|
252
|
+
);
|
|
253
|
+
let oldValue: unknown = undefined;
|
|
254
|
+
if (row?.original && column?.columnDef.field) {
|
|
255
|
+
oldValue = (row.original as Record<string, unknown>)[
|
|
256
|
+
column.columnDef.field
|
|
257
|
+
];
|
|
258
|
+
(row.original as Record<string, unknown>)[column.columnDef.field] =
|
|
259
|
+
parsedValue;
|
|
260
|
+
}
|
|
261
|
+
const key = getCellKey(ctx.editingCell.rowId, ctx.editingCell.columnId);
|
|
262
|
+
ctx.editedCellValues = {
|
|
263
|
+
...ctx.editedCellValues,
|
|
264
|
+
[key]: parsedValue,
|
|
265
|
+
};
|
|
266
|
+
ctx.grid.store.setState((prev: any) => ({ ...prev }));
|
|
267
|
+
// Record into the history at the current pointer. Any forward
|
|
268
|
+
// history (steps the user could have redone) is truncated - this
|
|
269
|
+
// is the standard "edit invalidates redo" rule.
|
|
270
|
+
if (oldValue !== parsedValue && row?.original && column?.columnDef.field) {
|
|
271
|
+
const step: HistoryStep = {
|
|
272
|
+
rowId: ctx.editingCell.rowId,
|
|
273
|
+
columnId: ctx.editingCell.columnId,
|
|
274
|
+
field: column.columnDef.field as string,
|
|
275
|
+
before: oldValue,
|
|
276
|
+
after: parsedValue,
|
|
277
|
+
}
|
|
278
|
+
const truncated = ctx.history.slice(0, ctx.historyPtr + 1)
|
|
279
|
+
truncated.push(step)
|
|
280
|
+
// Cap the buffer at UNDO_LIMIT; drop the OLDEST entries so the
|
|
281
|
+
// pointer stays valid relative to the newest steps.
|
|
282
|
+
if (truncated.length > ctx.UNDO_LIMIT) {
|
|
283
|
+
const drop = truncated.length - ctx.UNDO_LIMIT
|
|
284
|
+
ctx.history = truncated.slice(drop)
|
|
285
|
+
ctx.historyPtr = ctx.history.length - 1
|
|
286
|
+
} else {
|
|
287
|
+
ctx.history = truncated
|
|
288
|
+
ctx.historyPtr = ctx.history.length - 1
|
|
289
|
+
}
|
|
290
|
+
ctx.historyVersion += 1
|
|
291
|
+
}
|
|
292
|
+
// Notify the consumer AFTER the row has been updated so any callback-
|
|
293
|
+
// driven recompute (cascade totals, server save, undo stack) sees the
|
|
294
|
+
// post-write state. `rowIndex` matches the position in `props.data`.
|
|
295
|
+
if (ctx.props.onCellValueChange && row?.original && column) {
|
|
296
|
+
const rowIndex = ctx.internalData.indexOf(row.original as TData);
|
|
297
|
+
ctx.props.onCellValueChange({
|
|
298
|
+
rowIndex,
|
|
299
|
+
columnId: column.id,
|
|
300
|
+
oldValue,
|
|
301
|
+
newValue: parsedValue,
|
|
302
|
+
row: row.original as TData,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
ctx.editingCell = null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Apply an undo / redo step directly to the underlying row, bypassing
|
|
309
|
+
* the editor pipeline so we don't accidentally re-push to the stack. */
|
|
310
|
+
function applyHistoryStep(step: HistoryStep, direction: 'undo' | 'redo') {
|
|
311
|
+
const row = ctx.allRows.find((r: any) => r.id === step.rowId)
|
|
312
|
+
const col = ctx.allColumns.find((c: any) => c.id === step.columnId)
|
|
313
|
+
if (!row?.original || !col) return
|
|
314
|
+
const value = direction === 'undo' ? step.before : step.after
|
|
315
|
+
;(row.original as Record<string, unknown>)[step.field] = value
|
|
316
|
+
const key = getCellKey(step.rowId, step.columnId)
|
|
317
|
+
ctx.editedCellValues = { ...ctx.editedCellValues, [key]: value }
|
|
318
|
+
ctx.grid.store.setState((prev: any) => ({ ...prev }))
|
|
319
|
+
if (ctx.props.onCellValueChange) {
|
|
320
|
+
const rowIndex = ctx.internalData.indexOf(row.original as TData)
|
|
321
|
+
ctx.props.onCellValueChange({
|
|
322
|
+
rowIndex,
|
|
323
|
+
columnId: step.columnId,
|
|
324
|
+
oldValue: direction === 'undo' ? step.after : step.before,
|
|
325
|
+
newValue: value,
|
|
326
|
+
row: row.original as TData,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function updateEditingCellValue(value: string) {
|
|
332
|
+
ctx.editingCell = ctx.editingCell ? { ...ctx.editingCell, value } : ctx.editingCell;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function onEditorKeyDown(event: KeyboardEvent) {
|
|
336
|
+
event.stopPropagation();
|
|
337
|
+
if (event.key === "Enter") {
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
saveEditingCell();
|
|
340
|
+
ctx.gridRootEl?.focus({ preventScroll: true });
|
|
341
|
+
} else if (event.key === "Escape") {
|
|
342
|
+
event.preventDefault();
|
|
343
|
+
ctx.editingCell = null;
|
|
344
|
+
ctx.gridRootEl?.focus({ preventScroll: true });
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function focusOnMount(node: HTMLInputElement | HTMLTextAreaElement) {
|
|
349
|
+
const selectAll = ctx.editorSelectAll;
|
|
350
|
+
requestAnimationFrame(() => {
|
|
351
|
+
node.focus({ preventScroll: true });
|
|
352
|
+
try {
|
|
353
|
+
if (selectAll) {
|
|
354
|
+
node.select();
|
|
355
|
+
} else {
|
|
356
|
+
const end = node.value.length;
|
|
357
|
+
node.setSelectionRange(end, end);
|
|
358
|
+
}
|
|
359
|
+
} catch {
|
|
360
|
+
/* date/number inputs may not support text selection */
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function onCellDoubleClick(rowIndex: number, colIndex: number) {
|
|
366
|
+
if (!ctx.editingEnabled) return;
|
|
367
|
+
const row = ctx.allRows[rowIndex];
|
|
368
|
+
const column = ctx.allColumns[colIndex];
|
|
369
|
+
if (!row || !column) return;
|
|
370
|
+
if (isGroupRow(row)) return;
|
|
371
|
+
if (!isCellEditable(column, row)) return;
|
|
372
|
+
if (ctx.editingCell?.rowId === row.id && ctx.editingCell?.columnId === column.id)
|
|
373
|
+
return;
|
|
374
|
+
const editorType = (column.columnDef.editorType ??
|
|
375
|
+
"text") as CellEditorType;
|
|
376
|
+
ctx.editorSelectAll = true;
|
|
377
|
+
let initialValue = getCellDisplayValue(
|
|
378
|
+
row.id,
|
|
379
|
+
column.id,
|
|
380
|
+
row.getCellValueByColumnId(column.id),
|
|
381
|
+
);
|
|
382
|
+
if (editorType === "list" || editorType === "chips") {
|
|
383
|
+
if (column.columnDef.editorMultiple) {
|
|
384
|
+
// Seed with an array so the editor can mutate slot-by-slot.
|
|
385
|
+
initialValue = toValueArray(initialValue);
|
|
386
|
+
} else if (Array.isArray(initialValue)) {
|
|
387
|
+
initialValue = initialValue[0] ?? "";
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
ctx.editingCell = {
|
|
391
|
+
rowId: row.id,
|
|
392
|
+
columnId: column.id,
|
|
393
|
+
editorType,
|
|
394
|
+
value: initialValue,
|
|
395
|
+
};
|
|
396
|
+
ctx.setActiveCell(rowIndex, colIndex);
|
|
397
|
+
ctx.setSelection(rowIndex, colIndex);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function pasteFromClipboard() {
|
|
401
|
+
// Preferred path: the async Clipboard API. Requires a secure context
|
|
402
|
+
// (HTTPS or localhost) AND read permission; it also works on Firefox,
|
|
403
|
+
// where the native `paste` event does NOT fire on a non-editable element.
|
|
404
|
+
// On plain HTTP (a XAMPP/Apache LAN host) `navigator.clipboard` is
|
|
405
|
+
// undefined - there the Ctrl+V keydown handler skips preventDefault and
|
|
406
|
+
// lets the browser deliver a native `paste` event to `onGridPaste`.
|
|
407
|
+
if (!navigator.clipboard?.readText) return;
|
|
408
|
+
let text: string;
|
|
409
|
+
try {
|
|
410
|
+
text = await navigator.clipboard.readText();
|
|
411
|
+
} catch {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
applyPastedText(text);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Native `paste` ClipboardEvent fallback (insecure-context path). A real
|
|
419
|
+
* Ctrl/Cmd+V on the focused grid root fires this with `clipboardData`
|
|
420
|
+
* readable even over plain HTTP and with no permission prompt. Wired on the
|
|
421
|
+
* grid root in SvGrid.svelte; the keydown handler lets the gesture through
|
|
422
|
+
* (no preventDefault) whenever the async API is unavailable.
|
|
423
|
+
*/
|
|
424
|
+
function onGridPaste(event: ClipboardEvent) {
|
|
425
|
+
// When the async API is available the keydown handler already pasted via
|
|
426
|
+
// pasteFromClipboard(); ignore the (suppressed) native event so we don't
|
|
427
|
+
// paste twice.
|
|
428
|
+
if (typeof navigator.clipboard?.readText === "function") return;
|
|
429
|
+
const text = event.clipboardData?.getData("text/plain") ?? "";
|
|
430
|
+
if (!text) return;
|
|
431
|
+
event.preventDefault();
|
|
432
|
+
applyPastedText(text);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function applyPastedText(text: string) {
|
|
436
|
+
const anchor = ctx.selectionRange.anchor ?? ctx.grid.getState().activeCell;
|
|
437
|
+
if (!anchor) return;
|
|
438
|
+
const focus = ctx.selectionRange.focus ?? anchor;
|
|
439
|
+
const startRow = Math.min(anchor.rowIndex, focus.rowIndex);
|
|
440
|
+
const startCol = Math.min(anchor.colIndex, focus.colIndex);
|
|
441
|
+
const endRow = Math.max(anchor.rowIndex, focus.rowIndex);
|
|
442
|
+
const endCol = Math.max(anchor.colIndex, focus.colIndex);
|
|
443
|
+
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
|
444
|
+
if (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
445
|
+
if (!lines.length) return;
|
|
446
|
+
|
|
447
|
+
// Excel/Sheets behaviour: when the clipboard holds a single cell and
|
|
448
|
+
// the selection is a range, fill the entire range with that value.
|
|
449
|
+
// For larger clipboards (multi-row/col TSV), paste at the top-left
|
|
450
|
+
// corner and let the clipboard determine the span (the original
|
|
451
|
+
// behaviour).
|
|
452
|
+
const clipboardIsSingleCell =
|
|
453
|
+
lines.length === 1 && (lines[0]?.split("\t").length ?? 0) === 1;
|
|
454
|
+
const selectionIsRange = startRow !== endRow || startCol !== endCol;
|
|
455
|
+
const fillRange = clipboardIsSingleCell && selectionIsRange;
|
|
456
|
+
|
|
457
|
+
const rowSpan = fillRange ? endRow - startRow + 1 : lines.length;
|
|
458
|
+
const colSpan = fillRange
|
|
459
|
+
? endCol - startCol + 1
|
|
460
|
+
: Math.max(...lines.map((l) => l.split("\t").length));
|
|
461
|
+
|
|
462
|
+
const next = ctx.internalData.slice() as Array<TData>;
|
|
463
|
+
for (let i = 0; i < rowSpan; i += 1) {
|
|
464
|
+
const targetRowIndex = startRow + i;
|
|
465
|
+
const row = ctx.allRows[targetRowIndex];
|
|
466
|
+
if (!row || isGroupRow(row)) continue;
|
|
467
|
+
// Map the visible row back to its index in the data array via row.id (default = String(index)).
|
|
468
|
+
const dataIndex = Number(row.id);
|
|
469
|
+
if (
|
|
470
|
+
!Number.isInteger(dataIndex) ||
|
|
471
|
+
dataIndex < 0 ||
|
|
472
|
+
dataIndex >= next.length
|
|
473
|
+
)
|
|
474
|
+
continue;
|
|
475
|
+
const originalRow = next[dataIndex];
|
|
476
|
+
if (!originalRow) continue;
|
|
477
|
+
const sourceCells = fillRange
|
|
478
|
+
? null
|
|
479
|
+
: lines[i]?.split("\t") ?? [];
|
|
480
|
+
const updated: Record<string, unknown> = {
|
|
481
|
+
...(originalRow as Record<string, unknown>),
|
|
482
|
+
};
|
|
483
|
+
for (let j = 0; j < colSpan; j += 1) {
|
|
484
|
+
const column = ctx.allColumns[startCol + j];
|
|
485
|
+
if (!column?.columnDef.field) continue;
|
|
486
|
+
if (!isCellEditableAt(targetRowIndex, startCol + j)) continue;
|
|
487
|
+
const editorType = (column.columnDef.editorType ??
|
|
488
|
+
"text") as CellEditorType;
|
|
489
|
+
const raw = fillRange ? lines[0]! : sourceCells?.[j] ?? "";
|
|
490
|
+
updated[column.columnDef.field] = parseEditorValue(editorType, raw);
|
|
491
|
+
}
|
|
492
|
+
next[dataIndex] = updated as TData;
|
|
493
|
+
}
|
|
494
|
+
ctx.internalData = next;
|
|
495
|
+
ctx.grid.store.setState((prev: any) => ({ ...prev }));
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
isCellEditable,
|
|
500
|
+
isCellEditableAt,
|
|
501
|
+
getRowColumnValue,
|
|
502
|
+
getCellDisplayValue,
|
|
503
|
+
startEditingWithChar,
|
|
504
|
+
saveEditingCell,
|
|
505
|
+
applyHistoryStep,
|
|
506
|
+
updateEditingCellValue,
|
|
507
|
+
onEditorKeyDown,
|
|
508
|
+
focusOnMount,
|
|
509
|
+
onCellDoubleClick,
|
|
510
|
+
pasteFromClipboard,
|
|
511
|
+
onGridPaste,
|
|
512
|
+
};
|
|
513
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { normalizeEditorOptions, parseEditorValue } from './cell-editors'
|
|
3
|
+
|
|
4
|
+
describe('normalizeEditorOptions', () => {
|
|
5
|
+
it('returns [] for undefined', () => {
|
|
6
|
+
expect(normalizeEditorOptions(undefined)).toEqual([])
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('treats a function (mistakenly passed) as empty', () => {
|
|
10
|
+
// The type says functions are resolved earlier, but the guard handles it.
|
|
11
|
+
const fn = (() => []) as unknown as undefined
|
|
12
|
+
expect(normalizeEditorOptions(fn)).toEqual([])
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('maps bare strings to { value, label }', () => {
|
|
16
|
+
expect(normalizeEditorOptions(['a', 'b'])).toEqual([
|
|
17
|
+
{ value: 'a', label: 'a' },
|
|
18
|
+
{ value: 'b', label: 'b' },
|
|
19
|
+
])
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('maps bare numbers to stringified labels', () => {
|
|
23
|
+
expect(normalizeEditorOptions([1, 2])).toEqual([
|
|
24
|
+
{ value: 1, label: '1' },
|
|
25
|
+
{ value: 2, label: '2' },
|
|
26
|
+
])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('keeps an explicit label and color', () => {
|
|
30
|
+
expect(
|
|
31
|
+
normalizeEditorOptions([{ value: 'x', label: 'Ex', color: '#f00' }]),
|
|
32
|
+
).toEqual([{ value: 'x', label: 'Ex', color: '#f00' }])
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('defaults a missing label to String(value)', () => {
|
|
36
|
+
expect(normalizeEditorOptions([{ value: 7 }])).toEqual([
|
|
37
|
+
{ value: 7, label: '7', color: undefined },
|
|
38
|
+
])
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('parseEditorValue - rating', () => {
|
|
43
|
+
it('rounds and clamps into 0..5', () => {
|
|
44
|
+
expect(parseEditorValue('rating', 3.4)).toBe(3)
|
|
45
|
+
expect(parseEditorValue('rating', 4.6)).toBe(5)
|
|
46
|
+
expect(parseEditorValue('rating', 9)).toBe(5)
|
|
47
|
+
expect(parseEditorValue('rating', -2)).toBe(0)
|
|
48
|
+
})
|
|
49
|
+
it('returns 0 for non-finite input', () => {
|
|
50
|
+
expect(parseEditorValue('rating', 'nope')).toBe(0)
|
|
51
|
+
expect(parseEditorValue('rating', Infinity)).toBe(0)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('parseEditorValue - color', () => {
|
|
56
|
+
it('returns the trimmed color string', () => {
|
|
57
|
+
expect(parseEditorValue('color', ' #abcdef ')).toBe('#abcdef')
|
|
58
|
+
})
|
|
59
|
+
it('passes non-hex color strings through', () => {
|
|
60
|
+
expect(parseEditorValue('color', 'rebeccapurple')).toBe('rebeccapurple')
|
|
61
|
+
})
|
|
62
|
+
it('falls back to #000000 for empty/nullish', () => {
|
|
63
|
+
expect(parseEditorValue('color', '')).toBe('#000000')
|
|
64
|
+
expect(parseEditorValue('color', null)).toBe('#000000')
|
|
65
|
+
expect(parseEditorValue('color', undefined)).toBe('#000000')
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('parseEditorValue - time', () => {
|
|
70
|
+
it('returns null for empty input', () => {
|
|
71
|
+
expect(parseEditorValue('time', '')).toBe(null)
|
|
72
|
+
expect(parseEditorValue('time', null)).toBe(null)
|
|
73
|
+
})
|
|
74
|
+
it('returns null for malformed strings', () => {
|
|
75
|
+
expect(parseEditorValue('time', 'noon')).toBe(null)
|
|
76
|
+
expect(parseEditorValue('time', '12')).toBe(null)
|
|
77
|
+
})
|
|
78
|
+
it('normalizes HH:MM', () => {
|
|
79
|
+
expect(parseEditorValue('time', '9:05')).toBe('09:05')
|
|
80
|
+
})
|
|
81
|
+
it('keeps seconds when present', () => {
|
|
82
|
+
expect(parseEditorValue('time', '23:59:59')).toBe('23:59:59')
|
|
83
|
+
})
|
|
84
|
+
it('clamps out-of-range components', () => {
|
|
85
|
+
expect(parseEditorValue('time', '99:99:99')).toBe('23:59:59')
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
describe('parseEditorValue - password', () => {
|
|
90
|
+
it('keeps an empty string instead of nulling it', () => {
|
|
91
|
+
expect(parseEditorValue('password', '')).toBe('')
|
|
92
|
+
})
|
|
93
|
+
it('stringifies and preserves content', () => {
|
|
94
|
+
expect(parseEditorValue('password', 'hunter2')).toBe('hunter2')
|
|
95
|
+
expect(parseEditorValue('password', null)).toBe('')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('parseEditorValue - select / rich-select', () => {
|
|
100
|
+
it('returns scalar value as-is', () => {
|
|
101
|
+
expect(parseEditorValue('select', 'one')).toBe('one')
|
|
102
|
+
expect(parseEditorValue('rich-select', 5)).toBe(5)
|
|
103
|
+
})
|
|
104
|
+
it('takes the first element of an array', () => {
|
|
105
|
+
expect(parseEditorValue('select', ['a', 'b'])).toBe('a')
|
|
106
|
+
})
|
|
107
|
+
it('coerces nullish to null', () => {
|
|
108
|
+
expect(parseEditorValue('select', null)).toBe(null)
|
|
109
|
+
expect(parseEditorValue('select', undefined)).toBe(null)
|
|
110
|
+
expect(parseEditorValue('rich-select', [])).toBe(null)
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('parseEditorValue - textarea', () => {
|
|
115
|
+
it('stringifies, nullish to empty', () => {
|
|
116
|
+
expect(parseEditorValue('textarea', 'multi\nline')).toBe('multi\nline')
|
|
117
|
+
expect(parseEditorValue('textarea', null)).toBe('')
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('parseEditorValue - list / chips (single)', () => {
|
|
122
|
+
it('returns scalar as-is', () => {
|
|
123
|
+
expect(parseEditorValue('list', 'x')).toBe('x')
|
|
124
|
+
expect(parseEditorValue('chips', 7)).toBe(7)
|
|
125
|
+
})
|
|
126
|
+
it('takes first element of array', () => {
|
|
127
|
+
expect(parseEditorValue('list', ['a', 'b'])).toBe('a')
|
|
128
|
+
expect(parseEditorValue('chips', [])).toBe(null)
|
|
129
|
+
})
|
|
130
|
+
it('coerces nullish to null', () => {
|
|
131
|
+
expect(parseEditorValue('list', null)).toBe(null)
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('parseEditorValue - list / chips (multiple)', () => {
|
|
136
|
+
it('clones an array', () => {
|
|
137
|
+
const input = ['a', 'b']
|
|
138
|
+
const out = parseEditorValue('list', input, { multiple: true }) as string[]
|
|
139
|
+
expect(out).toEqual(['a', 'b'])
|
|
140
|
+
expect(out).not.toBe(input)
|
|
141
|
+
})
|
|
142
|
+
it('returns [] for empty/nullish', () => {
|
|
143
|
+
expect(parseEditorValue('chips', null, { multiple: true })).toEqual([])
|
|
144
|
+
expect(parseEditorValue('chips', '', { multiple: true })).toEqual([])
|
|
145
|
+
})
|
|
146
|
+
it('wraps a scalar into a single-element array', () => {
|
|
147
|
+
expect(parseEditorValue('list', 'solo', { multiple: true })).toEqual(['solo'])
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('parseEditorValue - unknown type fallback', () => {
|
|
152
|
+
it('stringifies, nullish to empty', () => {
|
|
153
|
+
expect(parseEditorValue('autocomplete', 'hey')).toBe('hey')
|
|
154
|
+
expect(parseEditorValue('autocomplete', undefined)).toBe('')
|
|
155
|
+
})
|
|
156
|
+
})
|