@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.
Files changed (143) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +137 -39
  3. package/dist/GridFooter.svelte +164 -0
  4. package/dist/GridFooter.svelte.d.ts +29 -0
  5. package/dist/GridMenus.svelte +570 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +429 -0
  8. package/dist/SvGrid.controller.svelte.js +1732 -0
  9. package/dist/SvGrid.css +1709 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.svelte +344 -7043
  13. package/dist/SvGrid.svelte.d.ts +4 -357
  14. package/dist/SvGrid.types.d.ts +436 -0
  15. package/dist/SvGrid.types.js +1 -0
  16. package/dist/SvGridChart.svelte +1060 -23
  17. package/dist/SvGridChart.svelte.d.ts +17 -0
  18. package/dist/build-api.d.ts +5 -0
  19. package/dist/build-api.js +527 -0
  20. package/dist/cell-render.d.ts +15 -0
  21. package/dist/cell-render.js +246 -0
  22. package/dist/cell-values.d.ts +28 -0
  23. package/dist/cell-values.js +89 -0
  24. package/dist/chart.d.ts +370 -3
  25. package/dist/chart.js +1135 -42
  26. package/dist/clipboard.d.ts +15 -0
  27. package/dist/clipboard.js +356 -0
  28. package/dist/columns.d.ts +30 -0
  29. package/dist/columns.js +277 -0
  30. package/dist/core.d.ts +1 -1
  31. package/dist/css.d.ts +3 -0
  32. package/dist/editing.d.ts +24 -0
  33. package/dist/editing.js +343 -0
  34. package/dist/editors/cell-editors.d.ts +1 -1
  35. package/dist/facet-buckets.d.ts +13 -0
  36. package/dist/facet-buckets.js +54 -0
  37. package/dist/features.d.ts +5 -0
  38. package/dist/features.js +30 -0
  39. package/dist/filter-operators.d.ts +16 -0
  40. package/dist/filter-operators.js +69 -0
  41. package/dist/hyperformula-adapter.d.ts +82 -0
  42. package/dist/hyperformula-adapter.js +73 -0
  43. package/dist/index.d.ts +5 -2
  44. package/dist/index.js +5 -2
  45. package/dist/keyboard-handlers.d.ts +7 -0
  46. package/dist/keyboard-handlers.js +197 -0
  47. package/dist/menus.d.ts +40 -0
  48. package/dist/menus.js +389 -0
  49. package/dist/named-views.d.ts +27 -0
  50. package/dist/named-views.js +39 -0
  51. package/dist/row-resize.d.ts +43 -0
  52. package/dist/row-resize.js +158 -0
  53. package/dist/scroll-sync.d.ts +9 -0
  54. package/dist/scroll-sync.js +86 -0
  55. package/dist/selection.d.ts +26 -0
  56. package/dist/selection.js +387 -0
  57. package/dist/spreadsheet.d.ts +80 -0
  58. package/dist/spreadsheet.js +194 -0
  59. package/dist/summaries.d.ts +12 -0
  60. package/dist/summaries.js +65 -0
  61. package/dist/svgrid-wrapper.types.d.ts +12 -1
  62. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  63. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  64. package/dist/svgrid.context-menu.test.d.ts +1 -0
  65. package/dist/svgrid.context-menu.test.js +102 -0
  66. package/dist/svgrid.new-features.wrapper.test.js +30 -4
  67. package/dist/svgrid.wrapper.test.js +27 -1
  68. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  69. package/dist/virtualization/scroll-scaling.d.ts +28 -0
  70. package/dist/virtualization/scroll-scaling.js +64 -0
  71. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  72. package/dist/virtualization/scroll-scaling.test.js +86 -0
  73. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  74. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  75. package/dist/virtualization/virtualizer.d.ts +7 -0
  76. package/dist/virtualization/virtualizer.js +30 -0
  77. package/package.json +1 -1
  78. package/src/GridFooter.svelte +164 -0
  79. package/src/GridMenus.svelte +570 -0
  80. package/src/SvGrid.controller.svelte.ts +2195 -0
  81. package/src/SvGrid.css +1747 -0
  82. package/src/SvGrid.helpers.test.ts +415 -0
  83. package/src/SvGrid.helpers.ts +185 -0
  84. package/src/SvGrid.svelte +348 -7043
  85. package/src/SvGrid.types.ts +456 -0
  86. package/src/SvGridChart.svelte +1060 -23
  87. package/src/build-api.coverage.test.ts +532 -0
  88. package/src/build-api.ts +663 -0
  89. package/src/cell-render.test.ts +451 -0
  90. package/src/cell-render.ts +426 -0
  91. package/src/cell-values.ts +114 -0
  92. package/src/chart-export.test.ts +370 -0
  93. package/src/chart.coverage.test.ts +814 -0
  94. package/src/chart.ts +1352 -47
  95. package/src/clipboard.test.ts +731 -0
  96. package/src/clipboard.ts +524 -0
  97. package/src/collaboration.coverage.test.ts +220 -0
  98. package/src/columns.test.ts +702 -0
  99. package/src/columns.ts +419 -0
  100. package/src/core.ts +8 -0
  101. package/src/css.d.ts +3 -0
  102. package/src/editing.test.ts +837 -0
  103. package/src/editing.ts +513 -0
  104. package/src/editors/cell-editors.coverage.test.ts +156 -0
  105. package/src/editors/cell-editors.ts +1 -0
  106. package/src/facet-buckets.test.ts +353 -0
  107. package/src/facet-buckets.ts +67 -0
  108. package/src/features.ts +128 -0
  109. package/src/filter-operators.test.ts +174 -0
  110. package/src/filter-operators.ts +87 -0
  111. package/src/hyperformula-adapter.test.ts +256 -0
  112. package/src/hyperformula-adapter.ts +124 -0
  113. package/src/index.ts +37 -0
  114. package/src/keyboard-handlers.coverage.test.ts +560 -0
  115. package/src/keyboard-handlers.ts +353 -0
  116. package/src/keyboard.ts +97 -97
  117. package/src/menus.test.ts +620 -0
  118. package/src/menus.ts +554 -0
  119. package/src/named-views.coverage.test.ts +210 -0
  120. package/src/named-views.ts +48 -0
  121. package/src/row-resize.test.ts +369 -0
  122. package/src/row-resize.ts +171 -0
  123. package/src/scroll-sync.test.ts +330 -0
  124. package/src/scroll-sync.ts +216 -0
  125. package/src/selection.test.ts +722 -0
  126. package/src/selection.ts +545 -0
  127. package/src/server-data-source.coverage.test.ts +180 -0
  128. package/src/spreadsheet.test.ts +445 -0
  129. package/src/spreadsheet.ts +246 -0
  130. package/src/summaries.ts +204 -0
  131. package/src/sv-grid-scrollbar.ts +13 -1
  132. package/src/svgrid-wrapper.types.ts +12 -1
  133. package/src/svgrid.behavior.test.ts +22 -0
  134. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  135. package/src/svgrid.context-menu.test.ts +126 -0
  136. package/src/svgrid.interaction.test.ts +30 -0
  137. package/src/svgrid.new-features.wrapper.test.ts +65 -4
  138. package/src/svgrid.wrapper.test.ts +27 -1
  139. package/src/test-setup.ts +9 -6
  140. package/src/virtualization/scroll-scaling.test.ts +148 -0
  141. package/src/virtualization/scroll-scaling.ts +121 -0
  142. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  143. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,545 @@
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
+ function setSelection(rowIndex: number, colIndex: number) {
287
+ if (!ctx.enableCellSelectionEffective) return;
288
+ const point = { rowIndex, colIndex };
289
+ ctx.selectionRange = { anchor: point, focus: point };
290
+ }
291
+
292
+ function extendSelection(rowIndex: number, colIndex: number) {
293
+ if (!ctx.enableCellSelectionEffective) return;
294
+ const anchor = ctx.selectionRange.anchor ?? { rowIndex, colIndex };
295
+ ctx.selectionRange = { anchor, focus: { rowIndex, colIndex } };
296
+ }
297
+
298
+ function isCellInSelectedRange(rowIndex: number, colIndex: number) {
299
+ const anchor = ctx.selectionRange.anchor;
300
+ const focus = ctx.selectionRange.focus;
301
+ if (!anchor || !focus) return false;
302
+ const minRow = Math.min(anchor.rowIndex, focus.rowIndex);
303
+ const maxRow = Math.max(anchor.rowIndex, focus.rowIndex);
304
+ const minCol = Math.min(anchor.colIndex, focus.colIndex);
305
+ const maxCol = Math.max(anchor.colIndex, focus.colIndex);
306
+ return (
307
+ rowIndex >= minRow &&
308
+ rowIndex <= maxRow &&
309
+ colIndex >= minCol &&
310
+ colIndex <= maxCol
311
+ );
312
+ }
313
+
314
+ /**
315
+ * Returns which sides of the selection rectangle a cell sits on, for the
316
+ * Excel-style outline. Returns null when the cell is outside the range.
317
+ */
318
+ function getCellRangeEdges(rowIndex: number, colIndex: number) {
319
+ const anchor = ctx.selectionRange.anchor;
320
+ const focus = ctx.selectionRange.focus;
321
+ if (!anchor || !focus) return null;
322
+ const minRow = Math.min(anchor.rowIndex, focus.rowIndex);
323
+ const maxRow = Math.max(anchor.rowIndex, focus.rowIndex);
324
+ const minCol = Math.min(anchor.colIndex, focus.colIndex);
325
+ const maxCol = Math.max(anchor.colIndex, focus.colIndex);
326
+ if (
327
+ rowIndex < minRow ||
328
+ rowIndex > maxRow ||
329
+ colIndex < minCol ||
330
+ colIndex > maxCol
331
+ ) {
332
+ return null;
333
+ }
334
+ return {
335
+ top: rowIndex === minRow,
336
+ bottom: rowIndex === maxRow,
337
+ left: colIndex === minCol,
338
+ right: colIndex === maxCol,
339
+ };
340
+ }
341
+
342
+ /** Returns true when the given cell is inside the fill-drag preview
343
+ * range BUT outside the original source range. Used to paint a
344
+ * dashed-outline preview while the user is dragging the handle. */
345
+ function isInFillPreview(rowIndex: number, colIndex: number) {
346
+ const d = ctx.fillDrag;
347
+ if (!d) return false;
348
+ const minR = Math.min(d.sourceMinRow, d.targetRow);
349
+ const maxR = Math.max(d.sourceMaxRow, d.targetRow);
350
+ const minC = Math.min(d.sourceMinCol, d.targetCol);
351
+ const maxC = Math.max(d.sourceMaxCol, d.targetCol);
352
+ if (
353
+ rowIndex < minR ||
354
+ rowIndex > maxR ||
355
+ colIndex < minC ||
356
+ colIndex > maxC
357
+ )
358
+ return false;
359
+ const inSource =
360
+ rowIndex >= d.sourceMinRow &&
361
+ rowIndex <= d.sourceMaxRow &&
362
+ colIndex >= d.sourceMinCol &&
363
+ colIndex <= d.sourceMaxCol;
364
+ return !inSource;
365
+ }
366
+
367
+ /** Look up a column by id without depending on `buildApi`'s private
368
+ * closure (those helpers don't exist at this scope). */
369
+ function findColumnById(columnId: string) {
370
+ return ctx.allColumns.find((c: any) => c.id === columnId);
371
+ }
372
+
373
+ function onCellPointerDown(
374
+ rowIndex: number,
375
+ colIndex: number,
376
+ event: PointerEvent,
377
+ ) {
378
+ if (event.button !== 0) return;
379
+ const row = ctx.allRows[rowIndex];
380
+ const column = ctx.allColumns[colIndex];
381
+ if (!row || !column || isGroupRow(row)) return;
382
+ const cellValue = ctx.getCellDisplayValue(
383
+ row.id,
384
+ column.id,
385
+ getColumnBaseValue(row, column),
386
+ );
387
+ const isCheckboxColumn =
388
+ column.columnDef.editorType === "checkbox" ||
389
+ typeof cellValue === "boolean";
390
+ if (isCheckboxColumn) return; // let onCellClick toggle the checkbox
391
+
392
+ const active = ctx.grid.getState().activeCell;
393
+ ctx.activeAtPointerDown = active
394
+ ? { rowIndex: active.rowIndex, colIndex: active.colIndex }
395
+ : null;
396
+ if (event.shiftKey) {
397
+ extendSelection(rowIndex, colIndex);
398
+ setActiveCell(rowIndex, colIndex);
399
+ } else {
400
+ setActiveCell(rowIndex, colIndex);
401
+ setSelection(rowIndex, colIndex);
402
+ // Range drag-select is a mouse/pen affordance. On touch, starting a drag
403
+ // here would rubber-band a selection AND fight the browser's native
404
+ // scroll (we never preventDefault), so a finger-drag to scroll the grid
405
+ // instead selected cells. Tap still selects the single cell above; we
406
+ // just don't enter drag-select mode for touch. (issue #23)
407
+ if (event.pointerType !== "touch") ctx.isDraggingSelection = true;
408
+ }
409
+ }
410
+
411
+ function onCellPointerEnter(rowIndex: number, colIndex: number) {
412
+ if (!ctx.isDraggingSelection) return;
413
+ const row = ctx.allRows[rowIndex];
414
+ if (!row || isGroupRow(row)) return;
415
+ extendSelection(rowIndex, colIndex);
416
+ setActiveCell(rowIndex, colIndex);
417
+ }
418
+
419
+ function endDragSelection() {
420
+ ctx.isDraggingSelection = false;
421
+ // Also commit a fill-handle drag if one was in progress - the user
422
+ // released the mouse, time to apply the pattern.
423
+ if (ctx.fillDrag) ctx.onFillPointerUp();
424
+ }
425
+
426
+ function onWindowPointerMove(event: PointerEvent) {
427
+ if (ctx.fillDrag) {
428
+ ctx.onFillPointerMove(event);
429
+ return;
430
+ }
431
+ if (!ctx.isDraggingSelection) return;
432
+ // Safety: if no mouse button is held the drag is over (we may have
433
+ // missed pointerup because the user lifted outside the window).
434
+ if (event.buttons === 0) {
435
+ endDragSelection();
436
+ }
437
+ }
438
+
439
+ function onCellClick(rowIndex: number, colIndex: number) {
440
+ const row = ctx.allRows[rowIndex];
441
+ const column = ctx.allColumns[colIndex];
442
+ if (!row || !column) return;
443
+
444
+ ctx.gridRootEl?.focus({ preventScroll: true });
445
+
446
+ if (isGroupRow(row)) {
447
+ setActiveCell(rowIndex, colIndex);
448
+ row.toggleExpanded?.();
449
+ return;
450
+ }
451
+
452
+ const baseValue = getColumnBaseValue(row, column);
453
+ const cellValue = ctx.getCellDisplayValue(row.id, column.id, baseValue);
454
+
455
+ // Emit the public click events for data cells/rows (before any
456
+ // checkbox-toggle / edit-entry side effects below).
457
+ ctx.props.onCellClick?.({
458
+ rowIndex,
459
+ colIndex,
460
+ columnId: column.id,
461
+ value: cellValue,
462
+ row: row.original as TData,
463
+ });
464
+ ctx.props.onRowClick?.({
465
+ rowIndex,
466
+ columnId: column.id,
467
+ row: row.original as TData,
468
+ });
469
+
470
+ const isCheckboxColumn =
471
+ column.columnDef.editorType === "checkbox" ||
472
+ typeof cellValue === "boolean";
473
+
474
+ if (isCheckboxColumn) {
475
+ ctx.toggleBooleanCell(rowIndex, colIndex);
476
+ setActiveCell(rowIndex, colIndex);
477
+ setSelection(rowIndex, colIndex);
478
+ ctx.editingCell = null;
479
+ return;
480
+ }
481
+
482
+ // onCellPointerDown already set active+selection for data cells. Only
483
+ // decide whether to enter edit mode (click on the previously-active cell).
484
+ const wasActive =
485
+ ctx.activeAtPointerDown !== null &&
486
+ ctx.activeAtPointerDown.rowIndex === rowIndex &&
487
+ ctx.activeAtPointerDown.colIndex === colIndex;
488
+ if (wasActive && ctx.editingEnabled) {
489
+ ctx.onCellDoubleClick(rowIndex, colIndex);
490
+ return;
491
+ }
492
+ ctx.editingCell = null;
493
+ }
494
+
495
+ /**
496
+ * Real double-click handler wired to the cell `ondblclick`. Emits the
497
+ * public double-click events (independent of editability) and then runs
498
+ * the edit-entry logic. Kept separate from `onCellDoubleClick` so the
499
+ * single-click-to-edit path in `onCellClick` does NOT emit a dblclick.
500
+ */
501
+ function emitCellDoubleClick(rowIndex: number, colIndex: number) {
502
+ const row = ctx.allRows[rowIndex];
503
+ const column = ctx.allColumns[colIndex];
504
+ if (row && column && !isGroupRow(row)) {
505
+ const value = ctx.getCellDisplayValue(
506
+ row.id,
507
+ column.id,
508
+ getColumnBaseValue(row, column),
509
+ );
510
+ ctx.props.onCellDoubleClick?.({
511
+ rowIndex,
512
+ colIndex,
513
+ columnId: column.id,
514
+ value,
515
+ row: row.original as TData,
516
+ });
517
+ ctx.props.onRowDoubleClick?.({
518
+ rowIndex,
519
+ columnId: column.id,
520
+ row: row.original as TData,
521
+ });
522
+ }
523
+ ctx.onCellDoubleClick(rowIndex, colIndex);
524
+ }
525
+
526
+ return {
527
+ isRowSelected,
528
+ toggleRowSelectionById,
529
+ toggleSelectAllRows,
530
+ setActiveCell,
531
+ scrollActiveCellIntoView,
532
+ setSelection,
533
+ extendSelection,
534
+ isCellInSelectedRange,
535
+ getCellRangeEdges,
536
+ isInFillPreview,
537
+ findColumnById,
538
+ onCellPointerDown,
539
+ onCellPointerEnter,
540
+ endDragSelection,
541
+ onWindowPointerMove,
542
+ onCellClick,
543
+ emitCellDoubleClick,
544
+ };
545
+ }