@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,15 @@
|
|
|
1
|
+
import { type RowData, type TableFeatures } from "./index";
|
|
2
|
+
import "./sv-grid-scrollbar";
|
|
3
|
+
export declare function createClipboard<TFeatures extends TableFeatures = TableFeatures, TData extends RowData = RowData>(ctx: any): {
|
|
4
|
+
readCellRaw: (rowIndex: number, columnId: string) => unknown;
|
|
5
|
+
writeCellRaw: (rowIndex: number, columnId: string, value: unknown) => void;
|
|
6
|
+
applyFillPattern: () => void;
|
|
7
|
+
clearSelectedCellValues: () => void;
|
|
8
|
+
startFillDrag: (event: PointerEvent, rowIndex: number, colIndex: number) => void;
|
|
9
|
+
onFillPointerMove: (event: PointerEvent) => void;
|
|
10
|
+
onFillPointerUp: () => void;
|
|
11
|
+
toggleBooleanCell: (rowIndex: number, colIndex: number) => void;
|
|
12
|
+
copySelectionToClipboard: () => void;
|
|
13
|
+
clearSelectedCells: () => boolean;
|
|
14
|
+
cutSelectionToClipboard: () => Promise<void>;
|
|
15
|
+
};
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
// clipboard handlers extracted from the controller. Imperative event handlers
|
|
2
|
+
// that read/write controller state via the `ctx` handle; the reactive core
|
|
3
|
+
// ($state/$derived/$effect) stays in the controller.
|
|
4
|
+
import { parseEditorValue, } from "./index";
|
|
5
|
+
import "./sv-grid-scrollbar";
|
|
6
|
+
import { buildFillPattern } from "./fill-patterns";
|
|
7
|
+
import { getCellKey, } from "./SvGrid.helpers";
|
|
8
|
+
import { getColumnBaseValue, isGroupRow, toolPanelHeaderLabel, } from "./cell-values";
|
|
9
|
+
export function createClipboard(ctx) {
|
|
10
|
+
/** Read the raw underlying value for the cell at (rowIndex, columnId)
|
|
11
|
+
* for pattern extraction. */
|
|
12
|
+
function readCellRaw(rowIndex, columnId) {
|
|
13
|
+
const row = ctx.internalData[rowIndex];
|
|
14
|
+
const column = ctx.findColumnById(columnId);
|
|
15
|
+
if (!row || !column?.columnDef.field)
|
|
16
|
+
return undefined;
|
|
17
|
+
return row[column.columnDef.field];
|
|
18
|
+
}
|
|
19
|
+
/** Write a value into (rowIndex, columnId) without going through the
|
|
20
|
+
* edit lifecycle. Fires `onCellValueChange` per write so consumers
|
|
21
|
+
* can react (formula recompute, autosave, etc.). */
|
|
22
|
+
function writeCellRaw(rowIndex, columnId, value) {
|
|
23
|
+
const row = ctx.internalData[rowIndex];
|
|
24
|
+
const column = ctx.findColumnById(columnId);
|
|
25
|
+
if (!row || !column?.columnDef.field)
|
|
26
|
+
return;
|
|
27
|
+
const field = column.columnDef.field;
|
|
28
|
+
const oldValue = row[field];
|
|
29
|
+
if (oldValue === value)
|
|
30
|
+
return;
|
|
31
|
+
// Resolve the row's id BEFORE swapping internalData - otherwise the
|
|
32
|
+
// recomputed `allRows` references the new row object and the
|
|
33
|
+
// `r.original === row` lookup fails, dropping our edit out of the
|
|
34
|
+
// `editedCellValues` map (which getCellDisplayValue consults first).
|
|
35
|
+
const rowId = ctx.allRows.find((r) => r.original === row)?.id;
|
|
36
|
+
const next = ctx.internalData.slice();
|
|
37
|
+
next[rowIndex] = { ...row, [field]: value };
|
|
38
|
+
ctx.internalData = next;
|
|
39
|
+
if (rowId) {
|
|
40
|
+
const key = getCellKey(rowId, columnId);
|
|
41
|
+
ctx.editedCellValues = { ...ctx.editedCellValues, [key]: value };
|
|
42
|
+
}
|
|
43
|
+
ctx.props.onCellValueChange?.({
|
|
44
|
+
rowIndex,
|
|
45
|
+
columnId,
|
|
46
|
+
oldValue,
|
|
47
|
+
newValue: value,
|
|
48
|
+
row: next[rowIndex],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Apply the pattern fill on pointerup. Each NEW row (or column) is
|
|
52
|
+
* filled from a pattern derived from the matching column (or row) of
|
|
53
|
+
* the source. Handles all four drag directions. */
|
|
54
|
+
function applyFillPattern() {
|
|
55
|
+
const d = ctx.fillDrag;
|
|
56
|
+
if (!d)
|
|
57
|
+
return;
|
|
58
|
+
// Clear fillDrag FIRST so a thrown error doesn't leave the grid
|
|
59
|
+
// stuck tracking the pointer.
|
|
60
|
+
ctx.fillDrag = null;
|
|
61
|
+
const newMinRow = Math.min(d.sourceMinRow, d.targetRow);
|
|
62
|
+
const newMaxRow = Math.max(d.sourceMaxRow, d.targetRow);
|
|
63
|
+
const newMinCol = Math.min(d.sourceMinCol, d.targetCol);
|
|
64
|
+
const newMaxCol = Math.max(d.sourceMaxCol, d.targetCol);
|
|
65
|
+
const verticalExtension = newMaxRow > d.sourceMaxRow || newMinRow < d.sourceMinRow;
|
|
66
|
+
const horizontalExtension = newMaxCol > d.sourceMaxCol || newMinCol < d.sourceMinCol;
|
|
67
|
+
if (verticalExtension) {
|
|
68
|
+
// For each column in the source range, build a pattern from the
|
|
69
|
+
// column's source values and apply to the new rows (above or below).
|
|
70
|
+
for (let c = d.sourceMinCol; c <= d.sourceMaxCol; c += 1) {
|
|
71
|
+
const column = ctx.allColumns[c];
|
|
72
|
+
if (!column?.columnDef.field)
|
|
73
|
+
continue;
|
|
74
|
+
if (column.columnDef.editable === false)
|
|
75
|
+
continue;
|
|
76
|
+
const sourceColValues = [];
|
|
77
|
+
for (let r = d.sourceMinRow; r <= d.sourceMaxRow; r += 1) {
|
|
78
|
+
sourceColValues.push(readCellRaw(r, column.id));
|
|
79
|
+
}
|
|
80
|
+
if (newMaxRow > d.sourceMaxRow) {
|
|
81
|
+
const targetRows = newMaxRow - d.sourceMaxRow;
|
|
82
|
+
const fills = buildFillPattern(sourceColValues, targetRows);
|
|
83
|
+
for (let i = 0; i < targetRows; i += 1) {
|
|
84
|
+
const targetRow = d.sourceMaxRow + 1 + i;
|
|
85
|
+
if (ctx.isCellEditableAt(targetRow, c))
|
|
86
|
+
writeCellRaw(targetRow, column.id, fills[i]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (newMinRow < d.sourceMinRow) {
|
|
90
|
+
// Filling upward - reverse-extrapolate.
|
|
91
|
+
const reversed = sourceColValues.slice().reverse();
|
|
92
|
+
const targetRows = d.sourceMinRow - newMinRow;
|
|
93
|
+
const fills = buildFillPattern(reversed, targetRows);
|
|
94
|
+
for (let i = 0; i < targetRows; i += 1) {
|
|
95
|
+
const targetRow = d.sourceMinRow - 1 - i;
|
|
96
|
+
if (ctx.isCellEditableAt(targetRow, c))
|
|
97
|
+
writeCellRaw(targetRow, column.id, fills[i]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (horizontalExtension) {
|
|
103
|
+
// For each row in source range, build pattern from the row's source
|
|
104
|
+
// values across columns and apply to new columns.
|
|
105
|
+
for (let r = d.sourceMinRow; r <= d.sourceMaxRow; r += 1) {
|
|
106
|
+
const sourceRowValues = [];
|
|
107
|
+
for (let c = d.sourceMinCol; c <= d.sourceMaxCol; c += 1) {
|
|
108
|
+
const col = ctx.allColumns[c];
|
|
109
|
+
if (!col)
|
|
110
|
+
continue;
|
|
111
|
+
sourceRowValues.push(readCellRaw(r, col.id));
|
|
112
|
+
}
|
|
113
|
+
if (newMaxCol > d.sourceMaxCol) {
|
|
114
|
+
const targetCols = newMaxCol - d.sourceMaxCol;
|
|
115
|
+
const fills = buildFillPattern(sourceRowValues, targetCols);
|
|
116
|
+
for (let i = 0; i < targetCols; i += 1) {
|
|
117
|
+
const targetCol = d.sourceMaxCol + 1 + i;
|
|
118
|
+
const col = ctx.allColumns[targetCol];
|
|
119
|
+
if (col && ctx.isCellEditableAt(r, targetCol))
|
|
120
|
+
writeCellRaw(r, col.id, fills[i]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (newMinCol < d.sourceMinCol) {
|
|
124
|
+
const reversed = sourceRowValues.slice().reverse();
|
|
125
|
+
const targetCols = d.sourceMinCol - newMinCol;
|
|
126
|
+
const fills = buildFillPattern(reversed, targetCols);
|
|
127
|
+
for (let i = 0; i < targetCols; i += 1) {
|
|
128
|
+
const targetCol = d.sourceMinCol - 1 - i;
|
|
129
|
+
const col = ctx.allColumns[targetCol];
|
|
130
|
+
if (col && ctx.isCellEditableAt(r, targetCol))
|
|
131
|
+
writeCellRaw(r, col.id, fills[i]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Extend the selection to the new range so the user can immediately
|
|
137
|
+
// see what got filled.
|
|
138
|
+
ctx.selectionRange = {
|
|
139
|
+
anchor: { rowIndex: newMinRow, colIndex: newMinCol },
|
|
140
|
+
focus: { rowIndex: newMaxRow, colIndex: newMaxCol },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** Clear the underlying value of every cell in the current selection
|
|
144
|
+
* range (or just the active cell when nothing is range-selected).
|
|
145
|
+
* Mirrors Excel's `Delete` key - values go to `null`, formatting and
|
|
146
|
+
* the row identity stay intact. */
|
|
147
|
+
function clearSelectedCellValues() {
|
|
148
|
+
const anchor = ctx.selectionRange.anchor;
|
|
149
|
+
const focus = ctx.selectionRange.focus;
|
|
150
|
+
if (anchor && focus) {
|
|
151
|
+
const minRow = Math.min(anchor.rowIndex, focus.rowIndex);
|
|
152
|
+
const maxRow = Math.max(anchor.rowIndex, focus.rowIndex);
|
|
153
|
+
const minCol = Math.min(anchor.colIndex, focus.colIndex);
|
|
154
|
+
const maxCol = Math.max(anchor.colIndex, focus.colIndex);
|
|
155
|
+
for (let r = minRow; r <= maxRow; r += 1) {
|
|
156
|
+
for (let c = minCol; c <= maxCol; c += 1) {
|
|
157
|
+
const col = ctx.allColumns[c];
|
|
158
|
+
if (col?.columnDef.field && ctx.isCellEditableAt(r, c)) {
|
|
159
|
+
writeCellRaw(r, col.id, null);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const a = ctx.grid.getState().activeCell;
|
|
166
|
+
if (a && ctx.userHasActivatedCell) {
|
|
167
|
+
const col = ctx.allColumns[a.colIndex];
|
|
168
|
+
if (col?.columnDef.field && ctx.isCellEditableAt(a.rowIndex, a.colIndex)) {
|
|
169
|
+
writeCellRaw(a.rowIndex, col.id, null);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Fill-handle pointerdown - seed the drag with the current selection
|
|
174
|
+
* range (or active cell as a 1x1) and start tracking the pointer. */
|
|
175
|
+
function startFillDrag(event, rowIndex, colIndex) {
|
|
176
|
+
event.stopPropagation();
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
const anchor = ctx.selectionRange.anchor;
|
|
179
|
+
const focus = ctx.selectionRange.focus;
|
|
180
|
+
if (anchor && focus) {
|
|
181
|
+
ctx.fillDrag = {
|
|
182
|
+
sourceMinRow: Math.min(anchor.rowIndex, focus.rowIndex),
|
|
183
|
+
sourceMaxRow: Math.max(anchor.rowIndex, focus.rowIndex),
|
|
184
|
+
sourceMinCol: Math.min(anchor.colIndex, focus.colIndex),
|
|
185
|
+
sourceMaxCol: Math.max(anchor.colIndex, focus.colIndex),
|
|
186
|
+
targetRow: rowIndex,
|
|
187
|
+
targetCol: colIndex,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
ctx.fillDrag = {
|
|
192
|
+
sourceMinRow: rowIndex,
|
|
193
|
+
sourceMaxRow: rowIndex,
|
|
194
|
+
sourceMinCol: colIndex,
|
|
195
|
+
sourceMaxCol: colIndex,
|
|
196
|
+
targetRow: rowIndex,
|
|
197
|
+
targetCol: colIndex,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
// Don't `setPointerCapture` - we use `elementFromPoint` during the
|
|
201
|
+
// drag to find the hovered cell, and capture would route the events
|
|
202
|
+
// back to the handle, breaking the lookup. Window-level handlers
|
|
203
|
+
// (`onWindowPointerMove` / `endDragSelection`) keep tracking.
|
|
204
|
+
}
|
|
205
|
+
/** Pointermove during fill-drag. We don't get cell coords from the
|
|
206
|
+
* event directly - find the td under the pointer via elementFromPoint
|
|
207
|
+
* and read its data-svgrid-row / data-svgrid-col attributes. */
|
|
208
|
+
function onFillPointerMove(event) {
|
|
209
|
+
if (!ctx.fillDrag)
|
|
210
|
+
return;
|
|
211
|
+
const el = document.elementFromPoint(event.clientX, event.clientY);
|
|
212
|
+
const cell = el?.closest("td[data-svgrid-row][data-svgrid-col]");
|
|
213
|
+
if (!cell)
|
|
214
|
+
return;
|
|
215
|
+
const r = Number(cell.dataset.svgridRow);
|
|
216
|
+
const c = Number(cell.dataset.svgridCol);
|
|
217
|
+
if (!Number.isFinite(r) || !Number.isFinite(c))
|
|
218
|
+
return;
|
|
219
|
+
if (r === ctx.fillDrag.targetRow && c === ctx.fillDrag.targetCol)
|
|
220
|
+
return;
|
|
221
|
+
ctx.fillDrag = { ...ctx.fillDrag, targetRow: r, targetCol: c };
|
|
222
|
+
}
|
|
223
|
+
function onFillPointerUp() {
|
|
224
|
+
if (!ctx.fillDrag)
|
|
225
|
+
return;
|
|
226
|
+
applyFillPattern();
|
|
227
|
+
}
|
|
228
|
+
function toggleBooleanCell(rowIndex, colIndex) {
|
|
229
|
+
const row = ctx.allRows[rowIndex];
|
|
230
|
+
const column = ctx.allColumns[colIndex];
|
|
231
|
+
if (!row || !column)
|
|
232
|
+
return;
|
|
233
|
+
if (!column.columnDef.field)
|
|
234
|
+
return;
|
|
235
|
+
const baseValue = getColumnBaseValue(row, column);
|
|
236
|
+
const currentValue = Boolean(ctx.getCellDisplayValue(row.id, column.id, baseValue));
|
|
237
|
+
const nextValue = !currentValue;
|
|
238
|
+
row.original[column.columnDef.field] =
|
|
239
|
+
nextValue;
|
|
240
|
+
const key = getCellKey(row.id, column.id);
|
|
241
|
+
ctx.editedCellValues = {
|
|
242
|
+
...ctx.editedCellValues,
|
|
243
|
+
[key]: nextValue,
|
|
244
|
+
};
|
|
245
|
+
ctx.grid.store.setState((prev) => ({ ...prev }));
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Write text to the OS clipboard, with a legacy fallback for insecure
|
|
249
|
+
* contexts. The async Clipboard API requires a secure context (HTTPS or
|
|
250
|
+
* localhost); on plain HTTP - e.g. a grid served by XAMPP/Apache over a LAN
|
|
251
|
+
* host - `navigator.clipboard` is undefined and copy/cut would silently do
|
|
252
|
+
* nothing. There we fall back to a temporary <textarea> + execCommand('copy'),
|
|
253
|
+
* which still works in an insecure context. This MUST be called synchronously
|
|
254
|
+
* from the user gesture (the keydown handler) so execCommand is allowed.
|
|
255
|
+
*/
|
|
256
|
+
function writeClipboardText(text) {
|
|
257
|
+
if (navigator.clipboard?.writeText) {
|
|
258
|
+
void navigator.clipboard.writeText(text).catch(() => legacyCopyText(text));
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
legacyCopyText(text);
|
|
262
|
+
}
|
|
263
|
+
function legacyCopyText(text) {
|
|
264
|
+
if (typeof document === "undefined")
|
|
265
|
+
return false;
|
|
266
|
+
const active = document.activeElement;
|
|
267
|
+
const ta = document.createElement("textarea");
|
|
268
|
+
ta.value = text;
|
|
269
|
+
ta.setAttribute("readonly", "");
|
|
270
|
+
// Offscreen but still selectable; opacity/0-size can suppress selection.
|
|
271
|
+
ta.style.cssText =
|
|
272
|
+
"position:fixed;top:0;left:-9999px;width:1px;height:1px;padding:0;border:0;";
|
|
273
|
+
document.body.appendChild(ta);
|
|
274
|
+
let ok = false;
|
|
275
|
+
try {
|
|
276
|
+
ta.select();
|
|
277
|
+
ta.setSelectionRange(0, text.length);
|
|
278
|
+
ok = document.execCommand("copy");
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
ok = false;
|
|
282
|
+
}
|
|
283
|
+
document.body.removeChild(ta);
|
|
284
|
+
// Restore focus to the grid root so keyboard nav keeps working afterward.
|
|
285
|
+
(active ?? ctx.gridRootEl)?.focus?.({
|
|
286
|
+
preventScroll: true,
|
|
287
|
+
});
|
|
288
|
+
return ok;
|
|
289
|
+
}
|
|
290
|
+
function copySelectionToClipboard() {
|
|
291
|
+
// Copy every selected rectangle. Each range becomes a TSV block; multiple
|
|
292
|
+
// ranges are stacked and separated by a blank line (Sheets-style).
|
|
293
|
+
const rects = ctx.getSelectionRects();
|
|
294
|
+
if (!rects.length)
|
|
295
|
+
return;
|
|
296
|
+
// Optional: prepend a header row (Excel "copy with headers") and/or run
|
|
297
|
+
// each value through a consumer hook before it hits the clipboard.
|
|
298
|
+
const withHeaders = ctx.props.copyHeadersToClipboard === true;
|
|
299
|
+
const processCell = ctx.props.processCellForClipboard;
|
|
300
|
+
const blocks = [];
|
|
301
|
+
for (const rect of rects) {
|
|
302
|
+
const lines = [];
|
|
303
|
+
if (withHeaders) {
|
|
304
|
+
const header = [];
|
|
305
|
+
for (let c = rect.minCol; c <= rect.maxCol; c += 1) {
|
|
306
|
+
const column = ctx.allColumns[c];
|
|
307
|
+
header.push(column ? toolPanelHeaderLabel(column) : "");
|
|
308
|
+
}
|
|
309
|
+
lines.push(header.join("\t"));
|
|
310
|
+
}
|
|
311
|
+
for (let r = rect.minRow; r <= rect.maxRow; r += 1) {
|
|
312
|
+
const row = ctx.allRows[r];
|
|
313
|
+
if (!row || isGroupRow(row))
|
|
314
|
+
continue;
|
|
315
|
+
const cells = [];
|
|
316
|
+
for (let c = rect.minCol; c <= rect.maxCol; c += 1) {
|
|
317
|
+
const column = ctx.allColumns[c];
|
|
318
|
+
if (!column) {
|
|
319
|
+
cells.push("");
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const base = getColumnBaseValue(row, column);
|
|
323
|
+
let value = ctx.getCellDisplayValue(row.id, column.id, base);
|
|
324
|
+
if (processCell) {
|
|
325
|
+
value = processCell({
|
|
326
|
+
value,
|
|
327
|
+
column,
|
|
328
|
+
row: row.original,
|
|
329
|
+
rowIndex: r,
|
|
330
|
+
columnId: column.id,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
cells.push(String(value ?? ""));
|
|
334
|
+
}
|
|
335
|
+
lines.push(cells.join("\t"));
|
|
336
|
+
}
|
|
337
|
+
blocks.push(lines.join("\n"));
|
|
338
|
+
}
|
|
339
|
+
writeClipboardText(blocks.join("\n\n"));
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Clear every editable cell in the current selection range. Used by
|
|
343
|
+
* Ctrl/Cmd+X (after a copy) and by the Delete / Backspace keys.
|
|
344
|
+
* Returns true if anything was changed - the caller uses that to
|
|
345
|
+
* decide whether to call `preventDefault()` and refresh the store.
|
|
346
|
+
*/
|
|
347
|
+
function clearSelectedCells() {
|
|
348
|
+
const anchor = ctx.selectionRange.anchor ?? ctx.grid.getState().activeCell;
|
|
349
|
+
if (!anchor)
|
|
350
|
+
return false;
|
|
351
|
+
const focus = ctx.selectionRange.focus ?? anchor;
|
|
352
|
+
const startRow = Math.min(anchor.rowIndex, focus.rowIndex);
|
|
353
|
+
const startCol = Math.min(anchor.colIndex, focus.colIndex);
|
|
354
|
+
const endRow = Math.max(anchor.rowIndex, focus.rowIndex);
|
|
355
|
+
const endCol = Math.max(anchor.colIndex, focus.colIndex);
|
|
356
|
+
const next = ctx.internalData.slice();
|
|
357
|
+
let mutated = false;
|
|
358
|
+
for (let r = startRow; r <= endRow; r += 1) {
|
|
359
|
+
const row = ctx.allRows[r];
|
|
360
|
+
if (!row || isGroupRow(row))
|
|
361
|
+
continue;
|
|
362
|
+
const dataIndex = Number(row.id);
|
|
363
|
+
if (!Number.isInteger(dataIndex) ||
|
|
364
|
+
dataIndex < 0 ||
|
|
365
|
+
dataIndex >= next.length)
|
|
366
|
+
continue;
|
|
367
|
+
const originalRow = next[dataIndex];
|
|
368
|
+
if (!originalRow)
|
|
369
|
+
continue;
|
|
370
|
+
const updated = {
|
|
371
|
+
...originalRow,
|
|
372
|
+
};
|
|
373
|
+
let rowChanged = false;
|
|
374
|
+
for (let c = startCol; c <= endCol; c += 1) {
|
|
375
|
+
const column = ctx.allColumns[c];
|
|
376
|
+
if (!column?.columnDef.field)
|
|
377
|
+
continue;
|
|
378
|
+
if (!ctx.isCellEditableAt(r, c))
|
|
379
|
+
continue;
|
|
380
|
+
// Clear means: empty string for text, undefined for everything
|
|
381
|
+
// else. parseEditorValue handles the per-type coercion.
|
|
382
|
+
const editorType = (column.columnDef.editorType ??
|
|
383
|
+
"text");
|
|
384
|
+
updated[column.columnDef.field] = parseEditorValue(editorType, "");
|
|
385
|
+
rowChanged = true;
|
|
386
|
+
}
|
|
387
|
+
if (rowChanged) {
|
|
388
|
+
next[dataIndex] = updated;
|
|
389
|
+
mutated = true;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (mutated) {
|
|
393
|
+
ctx.internalData = next;
|
|
394
|
+
ctx.grid.store.setState((prev) => ({ ...prev }));
|
|
395
|
+
}
|
|
396
|
+
return mutated;
|
|
397
|
+
}
|
|
398
|
+
async function cutSelectionToClipboard() {
|
|
399
|
+
// Cut = copy + clear. Wait for the copy to land on the clipboard so
|
|
400
|
+
// a slow writeText can't race the clear and leave the user with no
|
|
401
|
+
// way to undo via paste.
|
|
402
|
+
copySelectionToClipboard();
|
|
403
|
+
// Best-effort flush; clipboard.writeText is fire-and-forget but we
|
|
404
|
+
// already kicked it off above. The clear is synchronous.
|
|
405
|
+
clearSelectedCells();
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
readCellRaw,
|
|
409
|
+
writeCellRaw,
|
|
410
|
+
applyFillPattern,
|
|
411
|
+
clearSelectedCellValues,
|
|
412
|
+
startFillDrag,
|
|
413
|
+
onFillPointerMove,
|
|
414
|
+
onFillPointerUp,
|
|
415
|
+
toggleBooleanCell,
|
|
416
|
+
copySelectionToClipboard,
|
|
417
|
+
clearSelectedCells,
|
|
418
|
+
cutSelectionToClipboard,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|