@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.
Files changed (216) 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 +648 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +472 -0
  8. package/dist/SvGrid.controller.svelte.js +1917 -0
  9. package/dist/SvGrid.css +2012 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.helpers.test.d.ts +1 -0
  13. package/dist/SvGrid.helpers.test.js +298 -0
  14. package/dist/SvGrid.svelte +2346 -8742
  15. package/dist/SvGrid.svelte.d.ts +4 -357
  16. package/dist/SvGrid.types.d.ts +523 -0
  17. package/dist/SvGrid.types.js +1 -0
  18. package/dist/SvGridChart.svelte +1060 -23
  19. package/dist/SvGridChart.svelte.d.ts +17 -0
  20. package/dist/aligned-grids.d.ts +6 -0
  21. package/dist/aligned-grids.js +84 -0
  22. package/dist/aligned-grids.test.d.ts +1 -0
  23. package/dist/aligned-grids.test.js +75 -0
  24. package/dist/build-api.coverage.test.d.ts +20 -0
  25. package/dist/build-api.coverage.test.js +505 -0
  26. package/dist/build-api.d.ts +5 -0
  27. package/dist/build-api.js +557 -0
  28. package/dist/cell-render.d.ts +15 -0
  29. package/dist/cell-render.js +246 -0
  30. package/dist/cell-render.test.d.ts +1 -0
  31. package/dist/cell-render.test.js +338 -0
  32. package/dist/cell-values.d.ts +28 -0
  33. package/dist/cell-values.js +89 -0
  34. package/dist/chart-export.test.d.ts +1 -0
  35. package/dist/chart-export.test.js +302 -0
  36. package/dist/chart.coverage.test.d.ts +1 -0
  37. package/dist/chart.coverage.test.js +748 -0
  38. package/dist/chart.d.ts +370 -3
  39. package/dist/chart.js +1135 -42
  40. package/dist/clipboard.d.ts +15 -0
  41. package/dist/clipboard.js +420 -0
  42. package/dist/clipboard.test.d.ts +1 -0
  43. package/dist/clipboard.test.js +700 -0
  44. package/dist/collaboration.coverage.test.d.ts +1 -0
  45. package/dist/collaboration.coverage.test.js +200 -0
  46. package/dist/column-groups.d.ts +19 -0
  47. package/dist/column-groups.js +62 -0
  48. package/dist/column-groups.test.d.ts +1 -0
  49. package/dist/column-groups.test.js +56 -0
  50. package/dist/column-types.d.ts +10 -0
  51. package/dist/column-types.js +63 -0
  52. package/dist/column-types.test.d.ts +1 -0
  53. package/dist/column-types.test.js +62 -0
  54. package/dist/columns.d.ts +30 -0
  55. package/dist/columns.js +277 -0
  56. package/dist/columns.test.d.ts +1 -0
  57. package/dist/columns.test.js +625 -0
  58. package/dist/core.d.ts +86 -1
  59. package/dist/css.d.ts +3 -0
  60. package/dist/editing.d.ts +31 -0
  61. package/dist/editing.js +529 -0
  62. package/dist/editing.test.d.ts +1 -0
  63. package/dist/editing.test.js +732 -0
  64. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  65. package/dist/editors/cell-editors.coverage.test.js +139 -0
  66. package/dist/editors/cell-editors.d.ts +1 -1
  67. package/dist/facet-buckets.d.ts +13 -0
  68. package/dist/facet-buckets.js +54 -0
  69. package/dist/facet-buckets.test.d.ts +1 -0
  70. package/dist/facet-buckets.test.js +296 -0
  71. package/dist/features.d.ts +5 -0
  72. package/dist/features.js +30 -0
  73. package/dist/filter-operators.d.ts +16 -0
  74. package/dist/filter-operators.js +69 -0
  75. package/dist/filter-operators.test.d.ts +1 -0
  76. package/dist/filter-operators.test.js +135 -0
  77. package/dist/hyperformula-adapter.d.ts +82 -0
  78. package/dist/hyperformula-adapter.js +73 -0
  79. package/dist/hyperformula-adapter.test.d.ts +1 -0
  80. package/dist/hyperformula-adapter.test.js +205 -0
  81. package/dist/index.d.ts +9 -3
  82. package/dist/index.js +8 -2
  83. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  84. package/dist/keyboard-handlers.coverage.test.js +495 -0
  85. package/dist/keyboard-handlers.d.ts +7 -0
  86. package/dist/keyboard-handlers.js +202 -0
  87. package/dist/menus.d.ts +40 -0
  88. package/dist/menus.js +390 -0
  89. package/dist/menus.test.d.ts +1 -0
  90. package/dist/menus.test.js +560 -0
  91. package/dist/named-views.coverage.test.d.ts +1 -0
  92. package/dist/named-views.coverage.test.js +180 -0
  93. package/dist/named-views.d.ts +27 -0
  94. package/dist/named-views.js +39 -0
  95. package/dist/row-drag.d.ts +49 -0
  96. package/dist/row-drag.js +221 -0
  97. package/dist/row-drag.test.d.ts +1 -0
  98. package/dist/row-drag.test.js +142 -0
  99. package/dist/row-resize.d.ts +43 -0
  100. package/dist/row-resize.js +158 -0
  101. package/dist/row-resize.test.d.ts +1 -0
  102. package/dist/row-resize.test.js +329 -0
  103. package/dist/scroll-sync.d.ts +9 -0
  104. package/dist/scroll-sync.js +89 -0
  105. package/dist/scroll-sync.test.d.ts +1 -0
  106. package/dist/scroll-sync.test.js +290 -0
  107. package/dist/selection.d.ts +32 -0
  108. package/dist/selection.js +427 -0
  109. package/dist/selection.multi-range.test.d.ts +1 -0
  110. package/dist/selection.multi-range.test.js +55 -0
  111. package/dist/selection.test.d.ts +1 -0
  112. package/dist/selection.test.js +647 -0
  113. package/dist/server-data-source.coverage.test.d.ts +1 -0
  114. package/dist/server-data-source.coverage.test.js +154 -0
  115. package/dist/spreadsheet.d.ts +110 -0
  116. package/dist/spreadsheet.js +242 -0
  117. package/dist/spreadsheet.test.d.ts +1 -0
  118. package/dist/spreadsheet.test.js +446 -0
  119. package/dist/summaries.d.ts +12 -0
  120. package/dist/summaries.js +65 -0
  121. package/dist/sv-grid-scrollbar.js +13 -1
  122. package/dist/svgrid-wrapper.types.d.ts +31 -1
  123. package/dist/svgrid.behavior.test.js +20 -0
  124. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  125. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  126. package/dist/svgrid.context-menu.test.d.ts +1 -0
  127. package/dist/svgrid.context-menu.test.js +102 -0
  128. package/dist/svgrid.interaction.test.js +31 -0
  129. package/dist/svgrid.new-features.wrapper.test.js +64 -6
  130. package/dist/svgrid.wrapper.test.js +27 -1
  131. package/dist/test-setup.js +9 -3
  132. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  133. package/dist/virtualization/scroll-scaling.d.ts +45 -0
  134. package/dist/virtualization/scroll-scaling.js +99 -0
  135. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  136. package/dist/virtualization/scroll-scaling.test.js +127 -0
  137. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  138. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  139. package/dist/virtualization/virtualizer.d.ts +7 -0
  140. package/dist/virtualization/virtualizer.js +30 -0
  141. package/package.json +2 -1
  142. package/src/GridFooter.svelte +164 -0
  143. package/src/GridMenus.svelte +648 -0
  144. package/src/SvGrid.controller.svelte.ts +2352 -0
  145. package/src/SvGrid.css +2012 -0
  146. package/src/SvGrid.helpers.test.ts +415 -0
  147. package/src/SvGrid.helpers.ts +185 -0
  148. package/src/SvGrid.svelte +2346 -8742
  149. package/src/SvGrid.types.ts +537 -0
  150. package/src/SvGridChart.svelte +1060 -23
  151. package/src/aligned-grids.test.ts +80 -0
  152. package/src/aligned-grids.ts +87 -0
  153. package/src/build-api.coverage.test.ts +532 -0
  154. package/src/build-api.ts +683 -0
  155. package/src/cell-render.test.ts +451 -0
  156. package/src/cell-render.ts +426 -0
  157. package/src/cell-values.ts +114 -0
  158. package/src/chart-export.test.ts +370 -0
  159. package/src/chart.coverage.test.ts +814 -0
  160. package/src/chart.ts +1352 -47
  161. package/src/clipboard.test.ts +780 -0
  162. package/src/clipboard.ts +552 -0
  163. package/src/collaboration.coverage.test.ts +220 -0
  164. package/src/column-groups.test.ts +59 -0
  165. package/src/column-groups.ts +80 -0
  166. package/src/column-types.test.ts +68 -0
  167. package/src/column-types.ts +82 -0
  168. package/src/columns.test.ts +702 -0
  169. package/src/columns.ts +419 -0
  170. package/src/core.ts +86 -0
  171. package/src/css.d.ts +3 -0
  172. package/src/editing.test.ts +837 -0
  173. package/src/editing.ts +669 -0
  174. package/src/editors/cell-editors.coverage.test.ts +156 -0
  175. package/src/editors/cell-editors.ts +1 -0
  176. package/src/facet-buckets.test.ts +353 -0
  177. package/src/facet-buckets.ts +67 -0
  178. package/src/features.ts +128 -0
  179. package/src/filter-operators.test.ts +174 -0
  180. package/src/filter-operators.ts +87 -0
  181. package/src/hyperformula-adapter.test.ts +256 -0
  182. package/src/hyperformula-adapter.ts +124 -0
  183. package/src/index.ts +49 -0
  184. package/src/keyboard-handlers.coverage.test.ts +560 -0
  185. package/src/keyboard-handlers.ts +353 -0
  186. package/src/keyboard.ts +97 -97
  187. package/src/menus.test.ts +620 -0
  188. package/src/menus.ts +555 -0
  189. package/src/named-views.coverage.test.ts +210 -0
  190. package/src/named-views.ts +48 -0
  191. package/src/row-drag.test.ts +168 -0
  192. package/src/row-drag.ts +255 -0
  193. package/src/row-resize.test.ts +369 -0
  194. package/src/row-resize.ts +171 -0
  195. package/src/scroll-sync.test.ts +330 -0
  196. package/src/scroll-sync.ts +218 -0
  197. package/src/selection.multi-range.test.ts +61 -0
  198. package/src/selection.test.ts +722 -0
  199. package/src/selection.ts +579 -0
  200. package/src/server-data-source.coverage.test.ts +180 -0
  201. package/src/spreadsheet.test.ts +489 -0
  202. package/src/spreadsheet.ts +304 -0
  203. package/src/summaries.ts +204 -0
  204. package/src/sv-grid-scrollbar.ts +13 -1
  205. package/src/svgrid-wrapper.types.ts +31 -1
  206. package/src/svgrid.behavior.test.ts +22 -0
  207. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  208. package/src/svgrid.context-menu.test.ts +126 -0
  209. package/src/svgrid.interaction.test.ts +30 -0
  210. package/src/svgrid.new-features.wrapper.test.ts +67 -6
  211. package/src/svgrid.wrapper.test.ts +27 -1
  212. package/src/test-setup.ts +9 -6
  213. package/src/virtualization/scroll-scaling.test.ts +148 -0
  214. package/src/virtualization/scroll-scaling.ts +121 -0
  215. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  216. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,427 @@
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 { getGridCellDomId, } from "./index";
5
+ import "./sv-grid-scrollbar";
6
+ import { getColumnBaseValue, isGroupRow, } from "./cell-values";
7
+ export function createSelection(ctx) {
8
+ function isRowSelected(rowId) {
9
+ return Boolean(ctx.rowSelectionState[rowId]);
10
+ }
11
+ function toggleRowSelectionById(rowId) {
12
+ ctx.grid.setRowSelection((prev) => ({ ...prev, [rowId]: !prev[rowId] }));
13
+ }
14
+ function toggleSelectAllRows() {
15
+ const selectable = ctx.allRows.filter((row) => !isGroupRow(row));
16
+ const select = ctx.headerSelectionState !== "all";
17
+ ctx.grid.setRowSelection((prev) => {
18
+ const next = { ...prev };
19
+ for (const row of selectable) {
20
+ if (select)
21
+ next[row.id] = true;
22
+ else
23
+ delete next[row.id];
24
+ }
25
+ return next;
26
+ });
27
+ }
28
+ function setActiveCell(rowIndex, colIndex) {
29
+ ctx.userHasActivatedCell = true;
30
+ ctx.grid.setActiveCell({
31
+ rowIndex,
32
+ colIndex,
33
+ cellId: getGridCellDomId("svgrid", rowIndex, colIndex),
34
+ });
35
+ // Notify consumers (toolbars, ribbons) so they stay synced without
36
+ // having to listen on the DOM. Fired on EVERY active-cell move -
37
+ // click, arrow key, Tab, Enter, page-up/down, fill release.
38
+ if (ctx.props.onActiveCellChange) {
39
+ const column = ctx.allColumns[colIndex];
40
+ ctx.props.onActiveCellChange({
41
+ rowIndex,
42
+ colIndex,
43
+ columnId: column?.id ?? "",
44
+ });
45
+ }
46
+ }
47
+ function scrollActiveCellIntoView(rowIndex, colIndex) {
48
+ if (!ctx.scrollContainer)
49
+ return;
50
+ if (rowIndex < 0 || rowIndex >= ctx.allRows.length)
51
+ return;
52
+ if (colIndex < 0 || colIndex >= ctx.allColumns.length)
53
+ return;
54
+ if (ctx.rowVirtualizationEnabled) {
55
+ // Prefer the browser's native `scrollIntoView({ block: 'nearest' })`
56
+ // when the target row is already mounted. It does Excel-style
57
+ // minimum-scroll, respects the sticky thead via the
58
+ // `scroll-padding-top` we set on the scroll container, and runs
59
+ // on the compositor so it doesn't jump.
60
+ const root = ctx.scrollContainer;
61
+ const trEl = root.querySelector(`tr.sv-grid-row [data-svgrid-row="${rowIndex}"]`)?.closest('tr.sv-grid-row');
62
+ if (trEl) {
63
+ // `behavior: 'instant'` skips any user-agent smooth-scroll
64
+ // animation. Without it, fast key-repeat queues multiple
65
+ // overlapping animated scrolls and the viewport visibly
66
+ // overshoots / jumps as they collide.
67
+ trEl.scrollIntoView({
68
+ block: 'nearest',
69
+ inline: 'nearest',
70
+ behavior: 'instant',
71
+ });
72
+ }
73
+ else if (ctx.rowScrollScalingActive) {
74
+ // Huge-list scroll scaling: the logical row offset does NOT map 1:1
75
+ // to a DOM scrollTop (the DOM scroll range is capped), so the plain
76
+ // offset math below would clamp and never reach the target. Map the
77
+ // logical offset into DOM space and align the row just under the
78
+ // sticky header. Rows are sub-pixel tall in DOM space here, so exact
79
+ // minimum-scroll is meaningless; once the row mounts the native
80
+ // `scrollIntoView` path above fine-tunes on the next navigation.
81
+ const headerHeight = ctx.theadEl?.offsetHeight
82
+ ?? ctx.headerHeight ?? 0;
83
+ const rowLogicalTop = ctx.virtualizer.getOffsetForIndex(rowIndex);
84
+ const nextTop = Math.max(ctx.logicalToDomRowOffset(rowLogicalTop) - headerHeight, 0);
85
+ if (Math.abs(nextTop - root.scrollTop) > 0.5) {
86
+ root.scrollTop = nextTop;
87
+ }
88
+ }
89
+ else {
90
+ // Row outside the rendered virtualizer window. Compute the
91
+ // scroll-coordinate target manually and set scrollTop. On the
92
+ // next render the row will mount and subsequent navigations
93
+ // use the native path above.
94
+ const currentTop = root.scrollTop;
95
+ const clientHeight = root.clientHeight;
96
+ const headerHeight = ctx.theadEl?.offsetHeight
97
+ ?? ctx.headerHeight ?? 0;
98
+ const rowTopScroll = headerHeight + ctx.virtualizer.getOffsetForIndex(rowIndex);
99
+ const rowHeight = ctx.virtualizer.getSizeForIndex(rowIndex);
100
+ const rowBottom = rowTopScroll + rowHeight;
101
+ let nextTop = currentTop;
102
+ if (rowTopScroll < currentTop + headerHeight) {
103
+ nextTop = rowTopScroll - headerHeight;
104
+ }
105
+ else if (rowBottom > currentTop + clientHeight) {
106
+ nextTop = rowBottom - clientHeight;
107
+ }
108
+ nextTop = Math.max(nextTop, 0);
109
+ if (nextTop !== currentTop) {
110
+ root.scrollTop = nextTop;
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ // Non-virtualized mode: the cell's <td> is already in the DOM.
116
+ // Read its actual rect and bring it into view when it overlaps
117
+ // with the sticky header or is past the visible bottom. Without
118
+ // this branch, arrow keys move the active cell off-screen and
119
+ // the scrollbar never follows.
120
+ const cellEl = ctx.scrollContainer.querySelector(`td[data-svgrid-row="${rowIndex}"][data-svgrid-col="${colIndex}"]`);
121
+ if (cellEl) {
122
+ const headerHeight = ctx.theadEl?.offsetHeight ?? 0;
123
+ const containerRect = ctx.scrollContainer.getBoundingClientRect();
124
+ const cellRect = cellEl.getBoundingClientRect();
125
+ const cellTopInView = cellRect.top - containerRect.top;
126
+ const cellBotInView = cellRect.bottom - containerRect.top;
127
+ const clientHeight = ctx.scrollContainer.clientHeight;
128
+ let nextTop = ctx.scrollContainer.scrollTop;
129
+ if (cellTopInView < ctx.headerHeight) {
130
+ nextTop = ctx.scrollContainer.scrollTop + cellTopInView - ctx.headerHeight;
131
+ }
132
+ else if (cellBotInView > clientHeight) {
133
+ nextTop = ctx.scrollContainer.scrollTop + cellBotInView - clientHeight;
134
+ }
135
+ nextTop = Math.max(nextTop, 0);
136
+ if (nextTop !== ctx.scrollContainer.scrollTop) {
137
+ ctx.scrollContainer.scrollTop = nextTop;
138
+ }
139
+ }
140
+ }
141
+ // Prefer the rendered item (cached size from the layout pass).
142
+ // Fall back to the column virtualizer's offset helper - that
143
+ // handles per-column variable widths correctly, unlike the
144
+ // previous flat `colIndex * fallbackWidth` estimate.
145
+ const item = ctx.renderedColumnItems.find((entry) => entry.index === colIndex);
146
+ const fallbackWidth = ctx.props.columnWidth ?? 140;
147
+ const cellStart = item?.start ?? ctx.columnVirtualizer?.getOffsetForIndex?.(colIndex) ?? (colIndex * fallbackWidth);
148
+ const cellSize = item?.size ?? ctx.columnVirtualizer?.getSizeForIndex?.(colIndex) ?? fallbackWidth;
149
+ const cellEnd = cellStart + cellSize;
150
+ const viewStart = ctx.scrollContainer.scrollLeft;
151
+ const viewEnd = viewStart + ctx.scrollContainer.clientWidth;
152
+ if (cellStart < viewStart) {
153
+ ctx.scrollContainer.scrollLeft = cellStart;
154
+ }
155
+ else if (cellEnd > viewEnd) {
156
+ ctx.scrollContainer.scrollLeft = cellEnd - ctx.scrollContainer.clientWidth;
157
+ }
158
+ // No inline scrollVersion bump - the `scroll` event triggers
159
+ // onBodyScroll which flushes via rAF, doing one batched update.
160
+ }
161
+ // Normalize a range to a rectangle, or null when incomplete.
162
+ function rangeRect(range) {
163
+ const a = range?.anchor;
164
+ const f = range?.focus;
165
+ if (!a || !f)
166
+ return null;
167
+ return {
168
+ minRow: Math.min(a.rowIndex, f.rowIndex),
169
+ maxRow: Math.max(a.rowIndex, f.rowIndex),
170
+ minCol: Math.min(a.colIndex, f.colIndex),
171
+ maxCol: Math.max(a.colIndex, f.colIndex),
172
+ };
173
+ }
174
+ // Every selected rectangle: the committed extra ranges plus the active one.
175
+ // Order matters for copy (added-order); the active range comes last.
176
+ function getSelectionRects() {
177
+ const rects = [];
178
+ for (const r of ctx.selectionRanges ?? []) {
179
+ const rect = rangeRect(r);
180
+ if (rect)
181
+ rects.push(rect);
182
+ }
183
+ const active = rangeRect(ctx.selectionRange);
184
+ if (active)
185
+ rects.push(active);
186
+ return rects;
187
+ }
188
+ function setSelection(rowIndex, colIndex, additive = false) {
189
+ if (!ctx.enableCellSelectionEffective)
190
+ return;
191
+ if (additive) {
192
+ // Commit the current active range (if any) and start a fresh one, so the
193
+ // previous rectangle stays highlighted alongside the new Ctrl+drag.
194
+ const active = ctx.selectionRange;
195
+ if (active.anchor && active.focus) {
196
+ ctx.selectionRanges = [...ctx.selectionRanges, active];
197
+ }
198
+ }
199
+ else {
200
+ ctx.selectionRanges = [];
201
+ }
202
+ const point = { rowIndex, colIndex };
203
+ ctx.selectionRange = { anchor: point, focus: point };
204
+ }
205
+ function extendSelection(rowIndex, colIndex) {
206
+ if (!ctx.enableCellSelectionEffective)
207
+ return;
208
+ const anchor = ctx.selectionRange.anchor ?? { rowIndex, colIndex };
209
+ ctx.selectionRange = { anchor, focus: { rowIndex, colIndex } };
210
+ }
211
+ function isCellInSelectedRange(rowIndex, colIndex) {
212
+ for (const rect of getSelectionRects()) {
213
+ if (rowIndex >= rect.minRow &&
214
+ rowIndex <= rect.maxRow &&
215
+ colIndex >= rect.minCol &&
216
+ colIndex <= rect.maxCol) {
217
+ return true;
218
+ }
219
+ }
220
+ return false;
221
+ }
222
+ /**
223
+ * Returns which sides of a selection rectangle a cell sits on, for the
224
+ * Excel-style outline. With multiple ranges, the edges of the FIRST range
225
+ * that contains the cell are returned (active range checked last so its
226
+ * outline wins on overlap). Returns null when the cell is in no range.
227
+ */
228
+ function getCellRangeEdges(rowIndex, colIndex) {
229
+ for (const rect of getSelectionRects()) {
230
+ if (rowIndex < rect.minRow ||
231
+ rowIndex > rect.maxRow ||
232
+ colIndex < rect.minCol ||
233
+ colIndex > rect.maxCol) {
234
+ continue;
235
+ }
236
+ return {
237
+ top: rowIndex === rect.minRow,
238
+ bottom: rowIndex === rect.maxRow,
239
+ left: colIndex === rect.minCol,
240
+ right: colIndex === rect.maxCol,
241
+ };
242
+ }
243
+ return null;
244
+ }
245
+ /** Returns true when the given cell is inside the fill-drag preview
246
+ * range BUT outside the original source range. Used to paint a
247
+ * dashed-outline preview while the user is dragging the handle. */
248
+ function isInFillPreview(rowIndex, colIndex) {
249
+ const d = ctx.fillDrag;
250
+ if (!d)
251
+ return false;
252
+ const minR = Math.min(d.sourceMinRow, d.targetRow);
253
+ const maxR = Math.max(d.sourceMaxRow, d.targetRow);
254
+ const minC = Math.min(d.sourceMinCol, d.targetCol);
255
+ const maxC = Math.max(d.sourceMaxCol, d.targetCol);
256
+ if (rowIndex < minR ||
257
+ rowIndex > maxR ||
258
+ colIndex < minC ||
259
+ colIndex > maxC)
260
+ return false;
261
+ const inSource = rowIndex >= d.sourceMinRow &&
262
+ rowIndex <= d.sourceMaxRow &&
263
+ colIndex >= d.sourceMinCol &&
264
+ colIndex <= d.sourceMaxCol;
265
+ return !inSource;
266
+ }
267
+ /** Look up a column by id without depending on `buildApi`'s private
268
+ * closure (those helpers don't exist at this scope). */
269
+ function findColumnById(columnId) {
270
+ return ctx.allColumns.find((c) => c.id === columnId);
271
+ }
272
+ function onCellPointerDown(rowIndex, colIndex, event) {
273
+ if (event.button !== 0)
274
+ return;
275
+ const row = ctx.allRows[rowIndex];
276
+ const column = ctx.allColumns[colIndex];
277
+ if (!row || !column || isGroupRow(row))
278
+ return;
279
+ const cellValue = ctx.getCellDisplayValue(row.id, column.id, getColumnBaseValue(row, column));
280
+ const isCheckboxColumn = column.columnDef.editorType === "checkbox" ||
281
+ typeof cellValue === "boolean";
282
+ if (isCheckboxColumn)
283
+ return; // let onCellClick toggle the checkbox
284
+ const active = ctx.grid.getState().activeCell;
285
+ ctx.activeAtPointerDown = active
286
+ ? { rowIndex: active.rowIndex, colIndex: active.colIndex }
287
+ : null;
288
+ if (event.shiftKey) {
289
+ extendSelection(rowIndex, colIndex);
290
+ setActiveCell(rowIndex, colIndex);
291
+ }
292
+ else {
293
+ setActiveCell(rowIndex, colIndex);
294
+ // Ctrl/Cmd starts an ADDITIONAL range, keeping prior ranges highlighted.
295
+ setSelection(rowIndex, colIndex, event.ctrlKey || event.metaKey);
296
+ // Range drag-select is a mouse/pen affordance. On touch, starting a drag
297
+ // here would rubber-band a selection AND fight the browser's native
298
+ // scroll (we never preventDefault), so a finger-drag to scroll the grid
299
+ // instead selected cells. Tap still selects the single cell above; we
300
+ // just don't enter drag-select mode for touch. (issue #23)
301
+ if (event.pointerType !== "touch")
302
+ ctx.isDraggingSelection = true;
303
+ }
304
+ }
305
+ function onCellPointerEnter(rowIndex, colIndex) {
306
+ if (!ctx.isDraggingSelection)
307
+ return;
308
+ const row = ctx.allRows[rowIndex];
309
+ if (!row || isGroupRow(row))
310
+ return;
311
+ extendSelection(rowIndex, colIndex);
312
+ setActiveCell(rowIndex, colIndex);
313
+ }
314
+ function endDragSelection() {
315
+ ctx.isDraggingSelection = false;
316
+ // Also commit a fill-handle drag if one was in progress - the user
317
+ // released the mouse, time to apply the pattern.
318
+ if (ctx.fillDrag)
319
+ ctx.onFillPointerUp();
320
+ }
321
+ function onWindowPointerMove(event) {
322
+ if (ctx.fillDrag) {
323
+ ctx.onFillPointerMove(event);
324
+ return;
325
+ }
326
+ if (!ctx.isDraggingSelection)
327
+ return;
328
+ // Safety: if no mouse button is held the drag is over (we may have
329
+ // missed pointerup because the user lifted outside the window).
330
+ if (event.buttons === 0) {
331
+ endDragSelection();
332
+ }
333
+ }
334
+ function onCellClick(rowIndex, colIndex) {
335
+ const row = ctx.allRows[rowIndex];
336
+ const column = ctx.allColumns[colIndex];
337
+ if (!row || !column)
338
+ return;
339
+ ctx.gridRootEl?.focus({ preventScroll: true });
340
+ if (isGroupRow(row)) {
341
+ setActiveCell(rowIndex, colIndex);
342
+ row.toggleExpanded?.();
343
+ return;
344
+ }
345
+ const baseValue = getColumnBaseValue(row, column);
346
+ const cellValue = ctx.getCellDisplayValue(row.id, column.id, baseValue);
347
+ // Emit the public click events for data cells/rows (before any
348
+ // checkbox-toggle / edit-entry side effects below).
349
+ ctx.props.onCellClick?.({
350
+ rowIndex,
351
+ colIndex,
352
+ columnId: column.id,
353
+ value: cellValue,
354
+ row: row.original,
355
+ });
356
+ ctx.props.onRowClick?.({
357
+ rowIndex,
358
+ columnId: column.id,
359
+ row: row.original,
360
+ });
361
+ const isCheckboxColumn = column.columnDef.editorType === "checkbox" ||
362
+ typeof cellValue === "boolean";
363
+ if (isCheckboxColumn) {
364
+ ctx.toggleBooleanCell(rowIndex, colIndex);
365
+ setActiveCell(rowIndex, colIndex);
366
+ setSelection(rowIndex, colIndex);
367
+ ctx.editingCell = null;
368
+ return;
369
+ }
370
+ // onCellPointerDown already set active+selection for data cells. Only
371
+ // decide whether to enter edit mode (click on the previously-active cell).
372
+ const wasActive = ctx.activeAtPointerDown !== null &&
373
+ ctx.activeAtPointerDown.rowIndex === rowIndex &&
374
+ ctx.activeAtPointerDown.colIndex === colIndex;
375
+ if (wasActive && ctx.editingEnabled) {
376
+ ctx.onCellDoubleClick(rowIndex, colIndex);
377
+ return;
378
+ }
379
+ ctx.editingCell = null;
380
+ }
381
+ /**
382
+ * Real double-click handler wired to the cell `ondblclick`. Emits the
383
+ * public double-click events (independent of editability) and then runs
384
+ * the edit-entry logic. Kept separate from `onCellDoubleClick` so the
385
+ * single-click-to-edit path in `onCellClick` does NOT emit a dblclick.
386
+ */
387
+ function emitCellDoubleClick(rowIndex, colIndex) {
388
+ const row = ctx.allRows[rowIndex];
389
+ const column = ctx.allColumns[colIndex];
390
+ if (row && column && !isGroupRow(row)) {
391
+ const value = ctx.getCellDisplayValue(row.id, column.id, getColumnBaseValue(row, column));
392
+ ctx.props.onCellDoubleClick?.({
393
+ rowIndex,
394
+ colIndex,
395
+ columnId: column.id,
396
+ value,
397
+ row: row.original,
398
+ });
399
+ ctx.props.onRowDoubleClick?.({
400
+ rowIndex,
401
+ columnId: column.id,
402
+ row: row.original,
403
+ });
404
+ }
405
+ ctx.onCellDoubleClick(rowIndex, colIndex);
406
+ }
407
+ return {
408
+ isRowSelected,
409
+ toggleRowSelectionById,
410
+ toggleSelectAllRows,
411
+ setActiveCell,
412
+ scrollActiveCellIntoView,
413
+ setSelection,
414
+ extendSelection,
415
+ isCellInSelectedRange,
416
+ getCellRangeEdges,
417
+ getSelectionRects,
418
+ isInFillPreview,
419
+ findColumnById,
420
+ onCellPointerDown,
421
+ onCellPointerEnter,
422
+ endDragSelection,
423
+ onWindowPointerMove,
424
+ onCellClick,
425
+ emitCellDoubleClick,
426
+ };
427
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Unit tests for multiple-range (Ctrl+drag) cell selection, driven through the
3
+ * pure range helpers of createSelection(ctx) with a minimal fake ctx.
4
+ */
5
+ import { describe, expect, it } from 'vitest';
6
+ import { createSelection } from './selection';
7
+ function makeCtx() {
8
+ return {
9
+ enableCellSelectionEffective: true,
10
+ selectionRange: { anchor: null, focus: null },
11
+ selectionRanges: [],
12
+ };
13
+ }
14
+ describe('multiple range selection', () => {
15
+ it('a plain selection replaces any committed ranges', () => {
16
+ const ctx = makeCtx();
17
+ const s = createSelection(ctx);
18
+ s.setSelection(0, 0);
19
+ s.extendSelection(1, 1); // active range 0,0..1,1
20
+ s.setSelection(5, 5, true); // additive: commit prior, start new
21
+ expect(ctx.selectionRanges.length).toBe(1);
22
+ s.setSelection(9, 9, false); // plain: clears committed
23
+ expect(ctx.selectionRanges.length).toBe(0);
24
+ });
25
+ it('Ctrl+drag commits the prior range and keeps both highlighted', () => {
26
+ const ctx = makeCtx();
27
+ const s = createSelection(ctx);
28
+ s.setSelection(0, 0);
29
+ s.extendSelection(1, 1); // range A: rows 0-1, cols 0-1
30
+ s.setSelection(3, 3, true); // start range B additively
31
+ s.extendSelection(4, 4); // range B: rows 3-4, cols 3-4
32
+ const rects = s.getSelectionRects();
33
+ expect(rects.length).toBe(2);
34
+ // both ranges report cells as selected
35
+ expect(s.isCellInSelectedRange(0, 0)).toBe(true);
36
+ expect(s.isCellInSelectedRange(1, 1)).toBe(true);
37
+ expect(s.isCellInSelectedRange(4, 4)).toBe(true);
38
+ // a cell in neither is not selected
39
+ expect(s.isCellInSelectedRange(2, 2)).toBe(false);
40
+ });
41
+ it('getCellRangeEdges outlines each range independently', () => {
42
+ const ctx = makeCtx();
43
+ const s = createSelection(ctx);
44
+ s.setSelection(0, 0);
45
+ s.extendSelection(1, 1);
46
+ s.setSelection(3, 3, true);
47
+ s.extendSelection(4, 4);
48
+ // top-left corner of range A
49
+ expect(s.getCellRangeEdges(0, 0)).toEqual({ top: true, bottom: false, left: true, right: false });
50
+ // bottom-right corner of range B
51
+ expect(s.getCellRangeEdges(4, 4)).toEqual({ top: false, bottom: true, left: false, right: true });
52
+ // outside every range
53
+ expect(s.getCellRangeEdges(2, 2)).toBeNull();
54
+ });
55
+ });
@@ -0,0 +1 @@
1
+ export {};