@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
package/src/selection.ts
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
// selection 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 { createColumns } from "./columns";
|
|
95
|
+
import { createGridApi } from "./build-api";
|
|
96
|
+
import { createClipboard } from "./clipboard";
|
|
97
|
+
import {
|
|
98
|
+
filterOperatorOptions,
|
|
99
|
+
fallbackOperatorOption,
|
|
100
|
+
TEXT_OPERATORS,
|
|
101
|
+
NUMBER_OPERATORS,
|
|
102
|
+
DATE_OPERATORS,
|
|
103
|
+
CHECKBOX_OPERATORS,
|
|
104
|
+
operatorOption,
|
|
105
|
+
operatorsForColumn,
|
|
106
|
+
defaultOperatorFor,
|
|
107
|
+
operatorLabelFor,
|
|
108
|
+
} from "./filter-operators";
|
|
109
|
+
import {
|
|
110
|
+
type FacetBucket,
|
|
111
|
+
isBucketableColumn,
|
|
112
|
+
buildBuckets,
|
|
113
|
+
isInBucket,
|
|
114
|
+
} from "./facet-buckets";
|
|
115
|
+
import {
|
|
116
|
+
getColumnBaseValue,
|
|
117
|
+
isGroupRow,
|
|
118
|
+
toolPanelHeaderLabel,
|
|
119
|
+
formatSummaryNumeric,
|
|
120
|
+
getColumnAlign,
|
|
121
|
+
getPinnedCellValue,
|
|
122
|
+
getColumnAccessorValue,
|
|
123
|
+
columnDefMatchesId,
|
|
124
|
+
} from "./cell-values";
|
|
125
|
+
|
|
126
|
+
export function createSelection<
|
|
127
|
+
TFeatures extends TableFeatures = TableFeatures,
|
|
128
|
+
TData extends RowData = RowData,
|
|
129
|
+
>(ctx: any) {
|
|
130
|
+
function isRowSelected(rowId: string) {
|
|
131
|
+
return Boolean(ctx.rowSelectionState[rowId]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function toggleRowSelectionById(rowId: string) {
|
|
135
|
+
ctx.grid.setRowSelection((prev: any) => ({ ...prev, [rowId]: !prev[rowId] }));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function toggleSelectAllRows() {
|
|
139
|
+
const selectable = ctx.allRows.filter((row: any) => !isGroupRow(row));
|
|
140
|
+
const select = ctx.headerSelectionState !== "all";
|
|
141
|
+
ctx.grid.setRowSelection((prev: any) => {
|
|
142
|
+
const next = { ...prev };
|
|
143
|
+
for (const row of selectable) {
|
|
144
|
+
if (select) next[row.id] = true;
|
|
145
|
+
else delete next[row.id];
|
|
146
|
+
}
|
|
147
|
+
return next;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function setActiveCell(rowIndex: number, colIndex: number) {
|
|
152
|
+
ctx.userHasActivatedCell = true;
|
|
153
|
+
ctx.grid.setActiveCell({
|
|
154
|
+
rowIndex,
|
|
155
|
+
colIndex,
|
|
156
|
+
cellId: getGridCellDomId("svgrid", rowIndex, colIndex),
|
|
157
|
+
});
|
|
158
|
+
// Notify consumers (toolbars, ribbons) so they stay synced without
|
|
159
|
+
// having to listen on the DOM. Fired on EVERY active-cell move -
|
|
160
|
+
// click, arrow key, Tab, Enter, page-up/down, fill release.
|
|
161
|
+
if (ctx.props.onActiveCellChange) {
|
|
162
|
+
const column = ctx.allColumns[colIndex];
|
|
163
|
+
ctx.props.onActiveCellChange({
|
|
164
|
+
rowIndex,
|
|
165
|
+
colIndex,
|
|
166
|
+
columnId: column?.id ?? "",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function scrollActiveCellIntoView(rowIndex: number, colIndex: number) {
|
|
172
|
+
if (!ctx.scrollContainer) return;
|
|
173
|
+
if (rowIndex < 0 || rowIndex >= ctx.allRows.length) return;
|
|
174
|
+
if (colIndex < 0 || colIndex >= ctx.allColumns.length) return;
|
|
175
|
+
|
|
176
|
+
if (ctx.rowVirtualizationEnabled) {
|
|
177
|
+
// Prefer the browser's native `scrollIntoView({ block: 'nearest' })`
|
|
178
|
+
// when the target row is already mounted. It does Excel-style
|
|
179
|
+
// minimum-scroll, respects the sticky thead via the
|
|
180
|
+
// `scroll-padding-top` we set on the scroll container, and runs
|
|
181
|
+
// on the compositor so it doesn't jump.
|
|
182
|
+
const root = ctx.scrollContainer as HTMLElement
|
|
183
|
+
const trEl = root.querySelector<HTMLElement>(
|
|
184
|
+
`tr.sv-grid-row [data-svgrid-row="${rowIndex}"]`,
|
|
185
|
+
)?.closest('tr.sv-grid-row') as HTMLElement | null
|
|
186
|
+
if (trEl) {
|
|
187
|
+
// `behavior: 'instant'` skips any user-agent smooth-scroll
|
|
188
|
+
// animation. Without it, fast key-repeat queues multiple
|
|
189
|
+
// overlapping animated scrolls and the viewport visibly
|
|
190
|
+
// overshoots / jumps as they collide.
|
|
191
|
+
trEl.scrollIntoView({
|
|
192
|
+
block: 'nearest',
|
|
193
|
+
inline: 'nearest',
|
|
194
|
+
behavior: 'instant' as ScrollBehavior,
|
|
195
|
+
})
|
|
196
|
+
} else if (ctx.rowScrollScalingActive) {
|
|
197
|
+
// Huge-list scroll scaling: the logical row offset does NOT map 1:1
|
|
198
|
+
// to a DOM scrollTop (the DOM scroll range is capped), so the plain
|
|
199
|
+
// offset math below would clamp and never reach the target. Map the
|
|
200
|
+
// logical offset into DOM space and align the row just under the
|
|
201
|
+
// sticky header. Rows are sub-pixel tall in DOM space here, so exact
|
|
202
|
+
// minimum-scroll is meaningless; once the row mounts the native
|
|
203
|
+
// `scrollIntoView` path above fine-tunes on the next navigation.
|
|
204
|
+
const headerHeight = (ctx.theadEl as HTMLElement | null)?.offsetHeight
|
|
205
|
+
?? ctx.headerHeight ?? 0
|
|
206
|
+
const rowLogicalTop = ctx.virtualizer.getOffsetForIndex(rowIndex)
|
|
207
|
+
const nextTop = Math.max(
|
|
208
|
+
ctx.logicalToDomRowOffset(rowLogicalTop) - headerHeight,
|
|
209
|
+
0,
|
|
210
|
+
)
|
|
211
|
+
if (Math.abs(nextTop - root.scrollTop) > 0.5) {
|
|
212
|
+
root.scrollTop = nextTop
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
// Row outside the rendered virtualizer window. Compute the
|
|
216
|
+
// scroll-coordinate target manually and set scrollTop. On the
|
|
217
|
+
// next render the row will mount and subsequent navigations
|
|
218
|
+
// use the native path above.
|
|
219
|
+
const currentTop = root.scrollTop
|
|
220
|
+
const clientHeight = root.clientHeight
|
|
221
|
+
const headerHeight = (ctx.theadEl as HTMLElement | null)?.offsetHeight
|
|
222
|
+
?? ctx.headerHeight ?? 0
|
|
223
|
+
const rowTopScroll = headerHeight + ctx.virtualizer.getOffsetForIndex(rowIndex)
|
|
224
|
+
const rowHeight = ctx.virtualizer.getSizeForIndex(rowIndex)
|
|
225
|
+
const rowBottom = rowTopScroll + rowHeight
|
|
226
|
+
let nextTop = currentTop
|
|
227
|
+
if (rowTopScroll < currentTop + headerHeight) {
|
|
228
|
+
nextTop = rowTopScroll - headerHeight
|
|
229
|
+
} else if (rowBottom > currentTop + clientHeight) {
|
|
230
|
+
nextTop = rowBottom - clientHeight
|
|
231
|
+
}
|
|
232
|
+
nextTop = Math.max(nextTop, 0)
|
|
233
|
+
if (nextTop !== currentTop) {
|
|
234
|
+
root.scrollTop = nextTop
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
// Non-virtualized mode: the cell's <td> is already in the DOM.
|
|
239
|
+
// Read its actual rect and bring it into view when it overlaps
|
|
240
|
+
// with the sticky header or is past the visible bottom. Without
|
|
241
|
+
// this branch, arrow keys move the active cell off-screen and
|
|
242
|
+
// the scrollbar never follows.
|
|
243
|
+
const cellEl = (ctx.scrollContainer as HTMLElement).querySelector<HTMLElement>(
|
|
244
|
+
`td[data-svgrid-row="${rowIndex}"][data-svgrid-col="${colIndex}"]`,
|
|
245
|
+
);
|
|
246
|
+
if (cellEl) {
|
|
247
|
+
const headerHeight = ctx.theadEl?.offsetHeight ?? 0;
|
|
248
|
+
const containerRect = ctx.scrollContainer.getBoundingClientRect();
|
|
249
|
+
const cellRect = cellEl.getBoundingClientRect();
|
|
250
|
+
const cellTopInView = cellRect.top - containerRect.top;
|
|
251
|
+
const cellBotInView = cellRect.bottom - containerRect.top;
|
|
252
|
+
const clientHeight = ctx.scrollContainer.clientHeight;
|
|
253
|
+
let nextTop = ctx.scrollContainer.scrollTop;
|
|
254
|
+
if (cellTopInView < ctx.headerHeight) {
|
|
255
|
+
nextTop = ctx.scrollContainer.scrollTop + cellTopInView - ctx.headerHeight;
|
|
256
|
+
} else if (cellBotInView > clientHeight) {
|
|
257
|
+
nextTop = ctx.scrollContainer.scrollTop + cellBotInView - clientHeight;
|
|
258
|
+
}
|
|
259
|
+
nextTop = Math.max(nextTop, 0);
|
|
260
|
+
if (nextTop !== ctx.scrollContainer.scrollTop) {
|
|
261
|
+
ctx.scrollContainer.scrollTop = nextTop;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Prefer the rendered item (cached size from the layout pass).
|
|
267
|
+
// Fall back to the column virtualizer's offset helper - that
|
|
268
|
+
// handles per-column variable widths correctly, unlike the
|
|
269
|
+
// previous flat `colIndex * fallbackWidth` estimate.
|
|
270
|
+
const item = ctx.renderedColumnItems.find((entry: any) => entry.index === colIndex);
|
|
271
|
+
const fallbackWidth = ctx.props.columnWidth ?? 140;
|
|
272
|
+
const cellStart = item?.start ?? ctx.columnVirtualizer?.getOffsetForIndex?.(colIndex) ?? (colIndex * fallbackWidth);
|
|
273
|
+
const cellSize = item?.size ?? ctx.columnVirtualizer?.getSizeForIndex?.(colIndex) ?? fallbackWidth;
|
|
274
|
+
const cellEnd = cellStart + cellSize;
|
|
275
|
+
const viewStart = ctx.scrollContainer.scrollLeft;
|
|
276
|
+
const viewEnd = viewStart + ctx.scrollContainer.clientWidth;
|
|
277
|
+
if (cellStart < viewStart) {
|
|
278
|
+
ctx.scrollContainer.scrollLeft = cellStart;
|
|
279
|
+
} else if (cellEnd > viewEnd) {
|
|
280
|
+
ctx.scrollContainer.scrollLeft = cellEnd - ctx.scrollContainer.clientWidth;
|
|
281
|
+
}
|
|
282
|
+
// No inline scrollVersion bump - the `scroll` event triggers
|
|
283
|
+
// onBodyScroll which flushes via rAF, doing one batched update.
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Normalize a range to a rectangle, or null when incomplete.
|
|
287
|
+
function rangeRect(range: SelectionRange | null) {
|
|
288
|
+
const a = range?.anchor;
|
|
289
|
+
const f = range?.focus;
|
|
290
|
+
if (!a || !f) return null;
|
|
291
|
+
return {
|
|
292
|
+
minRow: Math.min(a.rowIndex, f.rowIndex),
|
|
293
|
+
maxRow: Math.max(a.rowIndex, f.rowIndex),
|
|
294
|
+
minCol: Math.min(a.colIndex, f.colIndex),
|
|
295
|
+
maxCol: Math.max(a.colIndex, f.colIndex),
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Every selected rectangle: the committed extra ranges plus the active one.
|
|
300
|
+
// Order matters for copy (added-order); the active range comes last.
|
|
301
|
+
function getSelectionRects() {
|
|
302
|
+
const rects = [] as Array<{ minRow: number; maxRow: number; minCol: number; maxCol: number }>;
|
|
303
|
+
for (const r of (ctx.selectionRanges as SelectionRange[]) ?? []) {
|
|
304
|
+
const rect = rangeRect(r);
|
|
305
|
+
if (rect) rects.push(rect);
|
|
306
|
+
}
|
|
307
|
+
const active = rangeRect(ctx.selectionRange);
|
|
308
|
+
if (active) rects.push(active);
|
|
309
|
+
return rects;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function setSelection(rowIndex: number, colIndex: number, additive = false) {
|
|
313
|
+
if (!ctx.enableCellSelectionEffective) return;
|
|
314
|
+
if (additive) {
|
|
315
|
+
// Commit the current active range (if any) and start a fresh one, so the
|
|
316
|
+
// previous rectangle stays highlighted alongside the new Ctrl+drag.
|
|
317
|
+
const active = ctx.selectionRange as SelectionRange;
|
|
318
|
+
if (active.anchor && active.focus) {
|
|
319
|
+
ctx.selectionRanges = [...(ctx.selectionRanges as SelectionRange[]), active];
|
|
320
|
+
}
|
|
321
|
+
} else {
|
|
322
|
+
ctx.selectionRanges = [];
|
|
323
|
+
}
|
|
324
|
+
const point = { rowIndex, colIndex };
|
|
325
|
+
ctx.selectionRange = { anchor: point, focus: point };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function extendSelection(rowIndex: number, colIndex: number) {
|
|
329
|
+
if (!ctx.enableCellSelectionEffective) return;
|
|
330
|
+
const anchor = ctx.selectionRange.anchor ?? { rowIndex, colIndex };
|
|
331
|
+
ctx.selectionRange = { anchor, focus: { rowIndex, colIndex } };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function isCellInSelectedRange(rowIndex: number, colIndex: number) {
|
|
335
|
+
for (const rect of getSelectionRects()) {
|
|
336
|
+
if (
|
|
337
|
+
rowIndex >= rect.minRow &&
|
|
338
|
+
rowIndex <= rect.maxRow &&
|
|
339
|
+
colIndex >= rect.minCol &&
|
|
340
|
+
colIndex <= rect.maxCol
|
|
341
|
+
) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Returns which sides of a selection rectangle a cell sits on, for the
|
|
350
|
+
* Excel-style outline. With multiple ranges, the edges of the FIRST range
|
|
351
|
+
* that contains the cell are returned (active range checked last so its
|
|
352
|
+
* outline wins on overlap). Returns null when the cell is in no range.
|
|
353
|
+
*/
|
|
354
|
+
function getCellRangeEdges(rowIndex: number, colIndex: number) {
|
|
355
|
+
for (const rect of getSelectionRects()) {
|
|
356
|
+
if (
|
|
357
|
+
rowIndex < rect.minRow ||
|
|
358
|
+
rowIndex > rect.maxRow ||
|
|
359
|
+
colIndex < rect.minCol ||
|
|
360
|
+
colIndex > rect.maxCol
|
|
361
|
+
) {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
top: rowIndex === rect.minRow,
|
|
366
|
+
bottom: rowIndex === rect.maxRow,
|
|
367
|
+
left: colIndex === rect.minCol,
|
|
368
|
+
right: colIndex === rect.maxCol,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Returns true when the given cell is inside the fill-drag preview
|
|
375
|
+
* range BUT outside the original source range. Used to paint a
|
|
376
|
+
* dashed-outline preview while the user is dragging the handle. */
|
|
377
|
+
function isInFillPreview(rowIndex: number, colIndex: number) {
|
|
378
|
+
const d = ctx.fillDrag;
|
|
379
|
+
if (!d) return false;
|
|
380
|
+
const minR = Math.min(d.sourceMinRow, d.targetRow);
|
|
381
|
+
const maxR = Math.max(d.sourceMaxRow, d.targetRow);
|
|
382
|
+
const minC = Math.min(d.sourceMinCol, d.targetCol);
|
|
383
|
+
const maxC = Math.max(d.sourceMaxCol, d.targetCol);
|
|
384
|
+
if (
|
|
385
|
+
rowIndex < minR ||
|
|
386
|
+
rowIndex > maxR ||
|
|
387
|
+
colIndex < minC ||
|
|
388
|
+
colIndex > maxC
|
|
389
|
+
)
|
|
390
|
+
return false;
|
|
391
|
+
const inSource =
|
|
392
|
+
rowIndex >= d.sourceMinRow &&
|
|
393
|
+
rowIndex <= d.sourceMaxRow &&
|
|
394
|
+
colIndex >= d.sourceMinCol &&
|
|
395
|
+
colIndex <= d.sourceMaxCol;
|
|
396
|
+
return !inSource;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Look up a column by id without depending on `buildApi`'s private
|
|
400
|
+
* closure (those helpers don't exist at this scope). */
|
|
401
|
+
function findColumnById(columnId: string) {
|
|
402
|
+
return ctx.allColumns.find((c: any) => c.id === columnId);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function onCellPointerDown(
|
|
406
|
+
rowIndex: number,
|
|
407
|
+
colIndex: number,
|
|
408
|
+
event: PointerEvent,
|
|
409
|
+
) {
|
|
410
|
+
if (event.button !== 0) return;
|
|
411
|
+
const row = ctx.allRows[rowIndex];
|
|
412
|
+
const column = ctx.allColumns[colIndex];
|
|
413
|
+
if (!row || !column || isGroupRow(row)) return;
|
|
414
|
+
const cellValue = ctx.getCellDisplayValue(
|
|
415
|
+
row.id,
|
|
416
|
+
column.id,
|
|
417
|
+
getColumnBaseValue(row, column),
|
|
418
|
+
);
|
|
419
|
+
const isCheckboxColumn =
|
|
420
|
+
column.columnDef.editorType === "checkbox" ||
|
|
421
|
+
typeof cellValue === "boolean";
|
|
422
|
+
if (isCheckboxColumn) return; // let onCellClick toggle the checkbox
|
|
423
|
+
|
|
424
|
+
const active = ctx.grid.getState().activeCell;
|
|
425
|
+
ctx.activeAtPointerDown = active
|
|
426
|
+
? { rowIndex: active.rowIndex, colIndex: active.colIndex }
|
|
427
|
+
: null;
|
|
428
|
+
if (event.shiftKey) {
|
|
429
|
+
extendSelection(rowIndex, colIndex);
|
|
430
|
+
setActiveCell(rowIndex, colIndex);
|
|
431
|
+
} else {
|
|
432
|
+
setActiveCell(rowIndex, colIndex);
|
|
433
|
+
// Ctrl/Cmd starts an ADDITIONAL range, keeping prior ranges highlighted.
|
|
434
|
+
setSelection(rowIndex, colIndex, event.ctrlKey || event.metaKey);
|
|
435
|
+
// Range drag-select is a mouse/pen affordance. On touch, starting a drag
|
|
436
|
+
// here would rubber-band a selection AND fight the browser's native
|
|
437
|
+
// scroll (we never preventDefault), so a finger-drag to scroll the grid
|
|
438
|
+
// instead selected cells. Tap still selects the single cell above; we
|
|
439
|
+
// just don't enter drag-select mode for touch. (issue #23)
|
|
440
|
+
if (event.pointerType !== "touch") ctx.isDraggingSelection = true;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function onCellPointerEnter(rowIndex: number, colIndex: number) {
|
|
445
|
+
if (!ctx.isDraggingSelection) return;
|
|
446
|
+
const row = ctx.allRows[rowIndex];
|
|
447
|
+
if (!row || isGroupRow(row)) return;
|
|
448
|
+
extendSelection(rowIndex, colIndex);
|
|
449
|
+
setActiveCell(rowIndex, colIndex);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function endDragSelection() {
|
|
453
|
+
ctx.isDraggingSelection = false;
|
|
454
|
+
// Also commit a fill-handle drag if one was in progress - the user
|
|
455
|
+
// released the mouse, time to apply the pattern.
|
|
456
|
+
if (ctx.fillDrag) ctx.onFillPointerUp();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function onWindowPointerMove(event: PointerEvent) {
|
|
460
|
+
if (ctx.fillDrag) {
|
|
461
|
+
ctx.onFillPointerMove(event);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (!ctx.isDraggingSelection) return;
|
|
465
|
+
// Safety: if no mouse button is held the drag is over (we may have
|
|
466
|
+
// missed pointerup because the user lifted outside the window).
|
|
467
|
+
if (event.buttons === 0) {
|
|
468
|
+
endDragSelection();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function onCellClick(rowIndex: number, colIndex: number) {
|
|
473
|
+
const row = ctx.allRows[rowIndex];
|
|
474
|
+
const column = ctx.allColumns[colIndex];
|
|
475
|
+
if (!row || !column) return;
|
|
476
|
+
|
|
477
|
+
ctx.gridRootEl?.focus({ preventScroll: true });
|
|
478
|
+
|
|
479
|
+
if (isGroupRow(row)) {
|
|
480
|
+
setActiveCell(rowIndex, colIndex);
|
|
481
|
+
row.toggleExpanded?.();
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const baseValue = getColumnBaseValue(row, column);
|
|
486
|
+
const cellValue = ctx.getCellDisplayValue(row.id, column.id, baseValue);
|
|
487
|
+
|
|
488
|
+
// Emit the public click events for data cells/rows (before any
|
|
489
|
+
// checkbox-toggle / edit-entry side effects below).
|
|
490
|
+
ctx.props.onCellClick?.({
|
|
491
|
+
rowIndex,
|
|
492
|
+
colIndex,
|
|
493
|
+
columnId: column.id,
|
|
494
|
+
value: cellValue,
|
|
495
|
+
row: row.original as TData,
|
|
496
|
+
});
|
|
497
|
+
ctx.props.onRowClick?.({
|
|
498
|
+
rowIndex,
|
|
499
|
+
columnId: column.id,
|
|
500
|
+
row: row.original as TData,
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const isCheckboxColumn =
|
|
504
|
+
column.columnDef.editorType === "checkbox" ||
|
|
505
|
+
typeof cellValue === "boolean";
|
|
506
|
+
|
|
507
|
+
if (isCheckboxColumn) {
|
|
508
|
+
ctx.toggleBooleanCell(rowIndex, colIndex);
|
|
509
|
+
setActiveCell(rowIndex, colIndex);
|
|
510
|
+
setSelection(rowIndex, colIndex);
|
|
511
|
+
ctx.editingCell = null;
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// onCellPointerDown already set active+selection for data cells. Only
|
|
516
|
+
// decide whether to enter edit mode (click on the previously-active cell).
|
|
517
|
+
const wasActive =
|
|
518
|
+
ctx.activeAtPointerDown !== null &&
|
|
519
|
+
ctx.activeAtPointerDown.rowIndex === rowIndex &&
|
|
520
|
+
ctx.activeAtPointerDown.colIndex === colIndex;
|
|
521
|
+
if (wasActive && ctx.editingEnabled) {
|
|
522
|
+
ctx.onCellDoubleClick(rowIndex, colIndex);
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
ctx.editingCell = null;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Real double-click handler wired to the cell `ondblclick`. Emits the
|
|
530
|
+
* public double-click events (independent of editability) and then runs
|
|
531
|
+
* the edit-entry logic. Kept separate from `onCellDoubleClick` so the
|
|
532
|
+
* single-click-to-edit path in `onCellClick` does NOT emit a dblclick.
|
|
533
|
+
*/
|
|
534
|
+
function emitCellDoubleClick(rowIndex: number, colIndex: number) {
|
|
535
|
+
const row = ctx.allRows[rowIndex];
|
|
536
|
+
const column = ctx.allColumns[colIndex];
|
|
537
|
+
if (row && column && !isGroupRow(row)) {
|
|
538
|
+
const value = ctx.getCellDisplayValue(
|
|
539
|
+
row.id,
|
|
540
|
+
column.id,
|
|
541
|
+
getColumnBaseValue(row, column),
|
|
542
|
+
);
|
|
543
|
+
ctx.props.onCellDoubleClick?.({
|
|
544
|
+
rowIndex,
|
|
545
|
+
colIndex,
|
|
546
|
+
columnId: column.id,
|
|
547
|
+
value,
|
|
548
|
+
row: row.original as TData,
|
|
549
|
+
});
|
|
550
|
+
ctx.props.onRowDoubleClick?.({
|
|
551
|
+
rowIndex,
|
|
552
|
+
columnId: column.id,
|
|
553
|
+
row: row.original as TData,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
ctx.onCellDoubleClick(rowIndex, colIndex);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
return {
|
|
560
|
+
isRowSelected,
|
|
561
|
+
toggleRowSelectionById,
|
|
562
|
+
toggleSelectAllRows,
|
|
563
|
+
setActiveCell,
|
|
564
|
+
scrollActiveCellIntoView,
|
|
565
|
+
setSelection,
|
|
566
|
+
extendSelection,
|
|
567
|
+
isCellInSelectedRange,
|
|
568
|
+
getCellRangeEdges,
|
|
569
|
+
getSelectionRects,
|
|
570
|
+
isInFillPreview,
|
|
571
|
+
findColumnById,
|
|
572
|
+
onCellPointerDown,
|
|
573
|
+
onCellPointerEnter,
|
|
574
|
+
endDragSelection,
|
|
575
|
+
onWindowPointerMove,
|
|
576
|
+
onCellClick,
|
|
577
|
+
emitCellDoubleClick,
|
|
578
|
+
};
|
|
579
|
+
}
|