@svgrid/grid 2.6.21 → 2.7.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 (358) hide show
  1. package/CHANGELOG.md +150 -88
  2. package/README.md +221 -199
  3. package/dist/FlexRender.svelte +96 -96
  4. package/dist/GridFooter.svelte +181 -181
  5. package/dist/GridMenus.svelte +201 -18
  6. package/dist/SvAutoComplete.svelte +169 -165
  7. package/dist/SvAvatar.svelte +75 -75
  8. package/dist/SvCalendar.svelte +503 -503
  9. package/dist/SvCarousel.svelte +141 -141
  10. package/dist/SvCheckBox.svelte +102 -102
  11. package/dist/SvCircularProgress.svelte +109 -109
  12. package/dist/SvColorInput.svelte +181 -181
  13. package/dist/SvComboBox.svelte +279 -275
  14. package/dist/SvContextMenu.svelte +116 -116
  15. package/dist/SvCountryInput.svelte +163 -163
  16. package/dist/SvDateRangeInput.svelte +25 -3
  17. package/dist/SvDrawer.svelte +254 -254
  18. package/dist/SvDropDownList.svelte +378 -374
  19. package/dist/SvDurationInput.svelte +126 -126
  20. package/dist/SvField.svelte +293 -293
  21. package/dist/SvForm.svelte +437 -437
  22. package/dist/SvGrid.controller.svelte.d.ts +61 -9
  23. package/dist/SvGrid.controller.svelte.js +363 -123
  24. package/dist/SvGrid.css +222 -1
  25. package/dist/SvGrid.helpers.d.ts +55 -0
  26. package/dist/SvGrid.helpers.js +83 -0
  27. package/dist/SvGrid.svelte +255 -56
  28. package/dist/SvGrid.types.d.ts +146 -1
  29. package/dist/SvGridCellEditor.svelte +744 -662
  30. package/dist/SvGridChart.svelte +1716 -1716
  31. package/dist/SvGridChartPanel.svelte +485 -485
  32. package/dist/SvGridChartView.svelte +70 -70
  33. package/dist/SvGridDropdown.svelte +728 -728
  34. package/dist/SvGridSelect.svelte +270 -266
  35. package/dist/SvGridSelect.svelte.d.ts +1 -1
  36. package/dist/SvGroupCell.svelte +116 -116
  37. package/dist/SvListBox.svelte +334 -326
  38. package/dist/SvMaskedInput.svelte +122 -122
  39. package/dist/SvMenu.svelte +124 -124
  40. package/dist/SvMenuList.svelte +146 -146
  41. package/dist/SvMultiSelect.svelte +293 -289
  42. package/dist/SvMultiSelect.svelte.d.ts +1 -1
  43. package/dist/SvNumberInput.svelte +172 -168
  44. package/dist/SvOtpInput.svelte +158 -158
  45. package/dist/SvPasswordInput.svelte +151 -151
  46. package/dist/SvPhoneInput.svelte +133 -133
  47. package/dist/SvPopover.svelte +197 -197
  48. package/dist/SvProgress.svelte +116 -116
  49. package/dist/SvRadioGroup.svelte +107 -107
  50. package/dist/SvRating.svelte +112 -112
  51. package/dist/SvResult.svelte +73 -73
  52. package/dist/SvRichCell.svelte +125 -0
  53. package/dist/SvRichCell.svelte.d.ts +15 -0
  54. package/dist/SvRichText.svelte +211 -211
  55. package/dist/SvRowGroupPanel.svelte +170 -170
  56. package/dist/SvScrollArea.svelte +61 -61
  57. package/dist/SvSlider.svelte +203 -203
  58. package/dist/SvSwitchButton.svelte +108 -108
  59. package/dist/SvTagsInput.svelte +115 -115
  60. package/dist/SvTextArea.svelte +5 -1
  61. package/dist/SvTextInput.svelte +147 -147
  62. package/dist/SvTimePicker.svelte +245 -245
  63. package/dist/SvToaster.svelte +159 -159
  64. package/dist/SvToggleButton.svelte +85 -85
  65. package/dist/SvTooltip.svelte +161 -161
  66. package/dist/SvTour.svelte +208 -208
  67. package/dist/SvTree.svelte +444 -444
  68. package/dist/SvTreeSelect.svelte +239 -235
  69. package/dist/SvTreeSelect.svelte.d.ts +1 -1
  70. package/dist/builtin-editors.d.ts +2 -2
  71. package/dist/builtin-editors.js +19 -3
  72. package/dist/cdn/GridMenus-C4oLsJyj.js +600 -0
  73. package/dist/cdn/GridMenus-yEivjNwl.js +601 -0
  74. package/dist/cdn/SvDateRangeInput-Bh0A0JkF.js +199 -0
  75. package/dist/cdn/SvDateRangeInput-DflbiP7N.js +198 -0
  76. package/dist/cdn/SvDateTimePicker-BWwpfB_o.js +812 -0
  77. package/dist/cdn/SvDateTimePicker-Bivn8dAP.js +816 -0
  78. package/dist/cdn/SvGridCellEditor-AXL8cHGO.js +550 -0
  79. package/dist/cdn/SvGridCellEditor-Ba7rY3eu.js +549 -0
  80. package/dist/cdn/{SvGridChart-BwVos976.js → SvGridChart-Bs2GIR2Q.js} +1 -1
  81. package/dist/cdn/{SvGridChart-BEJmNNx9.js → SvGridChart-CwhFz7GV.js} +1 -1
  82. package/dist/cdn/{SvGridChartPanel-BOknOkrP.js → SvGridChartPanel-1WXuStzM.js} +1 -1
  83. package/dist/cdn/{SvGridChartPanel-D2PjII4O.js → SvGridChartPanel-e5_nqNP9.js} +1 -1
  84. package/dist/cdn/{SvGridChartView-00LPUHL1.js → SvGridChartView-SmPW10dI.js} +1 -1
  85. package/dist/cdn/{SvGridChartView-BhUEJ5ki.js → SvGridChartView-eSPuJE6g.js} +1 -1
  86. package/dist/cdn/{SvGridDropdown-D0VdjeR8.js → SvGridDropdown-B1ZcLpgs.js} +1 -1
  87. package/dist/cdn/{SvGridDropdown-D13MtJ2j.js → SvGridDropdown-CtdbHcIS.js} +1 -1
  88. package/dist/cdn/column-resize-DsfNXMom.js +102 -0
  89. package/dist/cdn/date-format-CcP1tafP.js +1395 -0
  90. package/dist/cdn/date-format-DZT7T1wf.js +1393 -0
  91. package/dist/cdn/{editor-registry-CiI0xlKg.js → editor-registry-BhgE3S84.js} +51 -25
  92. package/dist/cdn/row-resize-BRcimkUT.js +95 -0
  93. package/dist/cdn/{src-BYq-qyrp.js → src-COC0vnpg.js} +7846 -7317
  94. package/dist/cdn/{src-DBel9wRZ.js → src-pYGV2rMR.js} +10033 -9504
  95. package/dist/cdn/svgrid.js +15 -11
  96. package/dist/cdn/svgrid.svelte-external.js +15 -11
  97. package/dist/cell-values.d.ts +16 -0
  98. package/dist/cell-values.js +28 -0
  99. package/dist/chart-export.js +8 -8
  100. package/dist/clipboard.d.ts +22 -0
  101. package/dist/clipboard.js +405 -27
  102. package/dist/column-groups.js +1 -1
  103. package/dist/column-resize.d.ts +46 -0
  104. package/dist/column-resize.js +205 -0
  105. package/dist/columns.d.ts +0 -3
  106. package/dist/columns.js +0 -57
  107. package/dist/core.d.ts +19 -4
  108. package/dist/core.js +460 -119
  109. package/dist/editing.js +12 -7
  110. package/dist/editors/cell-editors.d.ts +11 -0
  111. package/dist/editors/cell-editors.js +18 -1
  112. package/dist/filtering/excel-filters.js +28 -0
  113. package/dist/grid-messages.d.ts +15 -0
  114. package/dist/grid-messages.js +15 -0
  115. package/dist/group-display.d.ts +1 -1
  116. package/dist/index.d.ts +6 -1
  117. package/dist/index.js +10 -0
  118. package/dist/list-option.d.ts +6 -0
  119. package/dist/menus.js +41 -8
  120. package/dist/row-resize.d.ts +11 -0
  121. package/dist/row-resize.js +7 -1
  122. package/dist/sanitize-html.d.ts +37 -0
  123. package/dist/sanitize-html.js +234 -0
  124. package/dist/selection-bar-view.svelte.d.ts +25 -0
  125. package/dist/selection-bar-view.svelte.js +13 -0
  126. package/dist/selection.d.ts +2 -1
  127. package/dist/selection.js +133 -8
  128. package/dist/spreadsheet.d.ts +1 -1
  129. package/dist/spreadsheet.js +1 -1
  130. package/package.json +11 -11
  131. package/src/FlexRender.svelte +96 -96
  132. package/src/GridFooter.svelte +181 -181
  133. package/src/GridMenus.svelte +201 -18
  134. package/src/SvAutoComplete.svelte +169 -165
  135. package/src/SvAvatar.svelte +75 -75
  136. package/src/SvCalendar.svelte +503 -503
  137. package/src/SvCalendar.test.ts +226 -226
  138. package/src/SvCarousel.svelte +141 -141
  139. package/src/SvCheckBox.svelte +102 -102
  140. package/src/SvCircularProgress.svelte +109 -109
  141. package/src/SvColorInput.svelte +181 -181
  142. package/src/SvComboBox.svelte +279 -275
  143. package/src/SvContextMenu.svelte +116 -116
  144. package/src/SvCountryInput.svelte +163 -163
  145. package/src/SvDateRangeInput.svelte +25 -3
  146. package/src/SvDrawer.svelte +254 -254
  147. package/src/SvDropDownList.svelte +378 -374
  148. package/src/SvDurationInput.svelte +126 -126
  149. package/src/SvField.svelte +293 -293
  150. package/src/SvForm.svelte +437 -437
  151. package/src/SvForm.test.ts +411 -411
  152. package/src/SvGrid.controller.svelte.ts +404 -133
  153. package/src/SvGrid.css +222 -1
  154. package/src/SvGrid.helpers.ts +87 -0
  155. package/src/SvGrid.svelte +255 -56
  156. package/src/SvGrid.types.ts +2092 -1940
  157. package/src/SvGridCellEditor.svelte +744 -662
  158. package/src/SvGridChart.svelte +1716 -1716
  159. package/src/SvGridChartPanel.svelte +485 -485
  160. package/src/SvGridChartView.svelte +70 -70
  161. package/src/SvGridDropdown.svelte +728 -728
  162. package/src/SvGridSelect.svelte +270 -266
  163. package/src/SvGroupCell.svelte +116 -116
  164. package/src/SvListBox.svelte +334 -326
  165. package/src/SvMaskedInput.svelte +122 -122
  166. package/src/SvMenu.svelte +124 -124
  167. package/src/SvMenu.test.ts +97 -97
  168. package/src/SvMenuList.svelte +146 -146
  169. package/src/SvMultiSelect.svelte +293 -289
  170. package/src/SvNumberInput.svelte +172 -168
  171. package/src/SvOtpInput.svelte +158 -158
  172. package/src/SvPasswordInput.svelte +151 -151
  173. package/src/SvPhoneInput.svelte +133 -133
  174. package/src/SvPopover.svelte +197 -197
  175. package/src/SvProgress.svelte +116 -116
  176. package/src/SvRadioGroup.svelte +107 -107
  177. package/src/SvRating.svelte +112 -112
  178. package/src/SvResult.svelte +73 -73
  179. package/src/SvRichCell.svelte +125 -0
  180. package/src/SvRichText.svelte +211 -211
  181. package/src/SvRowGroupPanel.svelte +170 -170
  182. package/src/SvScrollArea.svelte +61 -61
  183. package/src/SvSlider.svelte +203 -203
  184. package/src/SvSwitchButton.svelte +108 -108
  185. package/src/SvTagsInput.svelte +115 -115
  186. package/src/SvTextArea.svelte +5 -1
  187. package/src/SvTextInput.svelte +147 -147
  188. package/src/SvTimePicker.svelte +245 -245
  189. package/src/SvToaster.svelte +159 -159
  190. package/src/SvToaster.test.ts +95 -95
  191. package/src/SvToggleButton.svelte +85 -85
  192. package/src/SvTooltip.svelte +161 -161
  193. package/src/SvTour.svelte +208 -208
  194. package/src/SvTree.svelte +444 -444
  195. package/src/SvTreeSelect.svelte +239 -235
  196. package/src/a11y/dismissable.test.ts +119 -119
  197. package/src/a11y/dismissable.ts +114 -114
  198. package/src/a11y.contract.test.ts +49 -49
  199. package/src/a11y.test.ts +59 -59
  200. package/src/a11y.ts +61 -61
  201. package/src/ai.test.ts +502 -502
  202. package/src/ai.ts +1419 -1419
  203. package/src/build-api.coverage.test.ts +633 -633
  204. package/src/build-api.ts +846 -846
  205. package/src/builtin-editors.grid.test.ts +83 -83
  206. package/src/builtin-editors.test.ts +18 -1
  207. package/src/builtin-editors.ts +19 -3
  208. package/src/cell-formatting.ts +171 -171
  209. package/src/cell-render.test.ts +513 -513
  210. package/src/cell-render.ts +496 -496
  211. package/src/cell-values.ts +148 -118
  212. package/src/chart-export.ts +202 -202
  213. package/src/chart-view.svelte.ts +36 -36
  214. package/src/chart.ts +2321 -2321
  215. package/src/clipboard.test.ts +93 -20
  216. package/src/clipboard.ts +433 -33
  217. package/src/collaboration.test.ts +104 -104
  218. package/src/collaboration.ts +167 -167
  219. package/src/column-groups.ts +78 -78
  220. package/src/column-resize.test.ts +381 -0
  221. package/src/column-resize.ts +227 -0
  222. package/src/columns.test.ts +0 -103
  223. package/src/columns.ts +0 -58
  224. package/src/core.aggregate.test.ts +134 -0
  225. package/src/core.filter.test.ts +156 -0
  226. package/src/core.grouping.test.ts +146 -0
  227. package/src/core.performance.test.ts +30 -30
  228. package/src/core.row-shape.test.ts +119 -0
  229. package/src/core.rowmodel-cache.test.ts +121 -0
  230. package/src/core.sort.test.ts +293 -0
  231. package/src/core.ts +1865 -1468
  232. package/src/createAutocomplete.svelte.ts +132 -132
  233. package/src/createCombobox.svelte.ts +191 -191
  234. package/src/createCountryInput.svelte.ts +157 -157
  235. package/src/createDropdownList.svelte.ts +168 -168
  236. package/src/createForm.svelte.ts +386 -386
  237. package/src/createGrid.svelte.ts +42 -42
  238. package/src/createGrid.test.ts +10 -10
  239. package/src/createGridState.svelte.ts +17 -17
  240. package/src/createListbox.svelte.ts +250 -250
  241. package/src/createMenu.svelte.ts +224 -224
  242. package/src/createPopoverSelect.svelte.ts +213 -213
  243. package/src/createSlider.svelte.ts +191 -191
  244. package/src/createTooltip.svelte.ts +144 -144
  245. package/src/createTree.svelte.ts +322 -322
  246. package/src/date-string-column.test.ts +87 -0
  247. package/src/datetime/date-core.ts +206 -206
  248. package/src/datetime/date-restrict.ts +61 -61
  249. package/src/datetime/timezone.ts +135 -135
  250. package/src/dock-manager-model.ts +596 -596
  251. package/src/dock-model.ts +374 -374
  252. package/src/editing.test.ts +974 -949
  253. package/src/editing.ts +609 -606
  254. package/src/editor-block-prop.test.ts +132 -0
  255. package/src/editor-contract.ts +171 -171
  256. package/src/editor-registry.grid.test.ts +144 -144
  257. package/src/editor-registry.ts +122 -122
  258. package/src/editors/cell-editors.ts +27 -1
  259. package/src/export-data-api.test.ts +126 -126
  260. package/src/export-format.test.ts +107 -107
  261. package/src/export-format.ts +601 -601
  262. package/src/filter-operators.ts +160 -160
  263. package/src/filtering/excel-filters.ts +325 -295
  264. package/src/filtering/normalize-fast-path.test.ts +104 -0
  265. package/src/flex-render.ts +3 -3
  266. package/src/form-field.ts +127 -127
  267. package/src/grid-messages.ts +34 -0
  268. package/src/group-display.test.ts +167 -167
  269. package/src/group-display.ts +200 -200
  270. package/src/headless.ts +87 -87
  271. package/src/index.ts +25 -1
  272. package/src/js-scroller.svelte.ts +173 -173
  273. package/src/keyboard-handlers.ts +270 -270
  274. package/src/keyboard.test.ts +59 -59
  275. package/src/keyboard.ts +97 -97
  276. package/src/list-nav.test.ts +49 -49
  277. package/src/list-nav.ts +29 -29
  278. package/src/list-option.test.ts +56 -56
  279. package/src/list-option.ts +179 -173
  280. package/src/menus.test.ts +43 -0
  281. package/src/menus.ts +597 -567
  282. package/src/merge-objects.ts +48 -48
  283. package/src/move-cells.test.ts +850 -0
  284. package/src/number-editor-literal.test.ts +84 -0
  285. package/src/number-editor.grid.test.ts +160 -0
  286. package/src/overlays.test.ts +90 -90
  287. package/src/positioning.ts +268 -268
  288. package/src/render-component.ts +28 -28
  289. package/src/resize-props.test.ts +361 -0
  290. package/src/row-drag-touch.ts +211 -211
  291. package/src/row-drag.test.ts +353 -353
  292. package/src/row-drag.ts +415 -415
  293. package/src/row-resize.test.ts +524 -493
  294. package/src/row-resize.ts +228 -210
  295. package/src/sanitize-html.test.ts +182 -0
  296. package/src/sanitize-html.ts +235 -0
  297. package/src/scheduler-ical.ts +181 -181
  298. package/src/scheduler-model.test.ts +562 -562
  299. package/src/scheduler-model.ts +873 -873
  300. package/src/selection-bar-view.svelte.ts +36 -0
  301. package/src/selection.test.ts +900 -743
  302. package/src/selection.ts +135 -8
  303. package/src/server-data-source.test.ts +383 -383
  304. package/src/server-data-source.ts +469 -469
  305. package/src/sparkline.test.ts +68 -68
  306. package/src/sparkline.ts +169 -169
  307. package/src/spreadsheet.test.ts +488 -488
  308. package/src/spreadsheet.ts +312 -312
  309. package/src/static-functions.ts +11 -11
  310. package/src/subscribe.ts +38 -38
  311. package/src/summaries.ts +113 -113
  312. package/src/svgrid-wrapper.types.ts +563 -563
  313. package/src/svgrid.async-editor-options.test.ts +273 -273
  314. package/src/svgrid.auto-row-height.test.ts +204 -204
  315. package/src/svgrid.behavior.test.ts +910 -910
  316. package/src/svgrid.charting.test.ts +534 -534
  317. package/src/svgrid.comments-autocomplete.test.ts +127 -127
  318. package/src/svgrid.context-menu.test.ts +147 -147
  319. package/src/svgrid.features.test.ts +157 -157
  320. package/src/svgrid.filter-depth.test.ts +163 -163
  321. package/src/svgrid.filter-menu-listbox.svelte.test.ts +377 -331
  322. package/src/svgrid.filter-menu-scroll.test.ts +112 -112
  323. package/src/svgrid.grand-total.test.ts +188 -188
  324. package/src/svgrid.group-display-mode.test.ts +171 -171
  325. package/src/svgrid.group-footers.test.ts +121 -121
  326. package/src/svgrid.group-pagination.test.ts +153 -153
  327. package/src/svgrid.in-suggest-snapshot.svelte.test.ts +91 -0
  328. package/src/svgrid.menu-scroll-close.test.ts +204 -0
  329. package/src/svgrid.new-features.wrapper.test.ts +251 -251
  330. package/src/svgrid.selection-bar-seam.test.ts +185 -0
  331. package/src/svgrid.tree-data.test.ts +186 -186
  332. package/src/svgrid.upsell-license.test.ts +117 -0
  333. package/src/svgrid.wrapper.test.ts +63 -63
  334. package/src/svgriddropdown.async-panel.svelte.test.ts +195 -195
  335. package/src/test-setup.ts +62 -62
  336. package/src/themes/index.ts +288 -288
  337. package/src/toast-store.svelte.ts +250 -250
  338. package/src/toast-store.test.ts +147 -147
  339. package/src/tree-row-model.test.ts +168 -168
  340. package/src/ui-buttons.test.ts +144 -144
  341. package/src/ui-inputs.test.ts +118 -118
  342. package/src/ui-localization.test.ts +113 -113
  343. package/src/ui-range.test.ts +70 -70
  344. package/src/ui-selection.test.ts +155 -155
  345. package/src/ui-tier1.test.ts +142 -142
  346. package/src/virtual.test.ts +88 -88
  347. package/src/virtualization/column-virtualizer.test.ts +27 -27
  348. package/src/virtualization/column-virtualizer.ts +30 -30
  349. package/src/virtualization/svelte-virtualizer.svelte.ts +26 -26
  350. package/src/virtualization/types.ts +30 -30
  351. package/src/virtualization/virtualizer.test.ts +47 -47
  352. package/src/virtualization/virtualizer.ts +322 -322
  353. package/dist/cdn/GridMenus-BfTAKn84.js +0 -488
  354. package/dist/cdn/GridMenus-C3bJd7w8.js +0 -489
  355. package/dist/cdn/SvDateTimePicker-CHnUkWcH.js +0 -2206
  356. package/dist/cdn/SvDateTimePicker-CRQH_U7E.js +0 -2201
  357. package/dist/cdn/SvGridCellEditor-BGUCzuPB.js +0 -523
  358. package/dist/cdn/SvGridCellEditor-G8IMWIws.js +0 -522
@@ -0,0 +1,850 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { createClipboard } from './clipboard'
3
+
4
+ // ---------------------------------------------------------------------------
5
+ // Excel-style range move / copy (the `moveCells` prop): grab a selection
6
+ // border, drag it somewhere else, drop.
7
+ //
8
+ // Same hand-rolled `ctx` approach as clipboard.test.ts - the handlers talk to
9
+ // the controller only through this handle - but with the extra surface the
10
+ // move path needs: history, an editability predicate that can differ per cell,
11
+ // and a selection-rect view that can hold more than one range.
12
+ // ---------------------------------------------------------------------------
13
+
14
+ interface FakeOptions {
15
+ columns?: Array<{ id: string; field?: string }>
16
+ data?: Array<Record<string, unknown>>
17
+ editableAt?: (r: number, c: number) => boolean
18
+ moveCells?: boolean
19
+ }
20
+
21
+ function makeCtx(opts: FakeOptions = {}) {
22
+ const columns = opts.columns ?? [
23
+ { id: 'a', field: 'a' },
24
+ { id: 'b', field: 'b' },
25
+ { id: 'c', field: 'c' },
26
+ ]
27
+ let backing = (
28
+ opts.data ?? [
29
+ { a: 'a0', b: 'b0', c: 'c0' },
30
+ { a: 'a1', b: 'b1', c: 'c1' },
31
+ { a: 'a2', b: 'b2', c: 'c2' },
32
+ { a: 'a3', b: 'b3', c: 'c3' },
33
+ ]
34
+ ).map((d) => ({ ...d }))
35
+
36
+ const allColumns = columns.map((c) => ({
37
+ id: c.id,
38
+ columnDef: { field: c.field },
39
+ }))
40
+
41
+ const buildRows = (rows: Array<Record<string, unknown>>) =>
42
+ rows.map((row, index) => ({
43
+ id: String(index),
44
+ original: row,
45
+ getCanExpand: () => false,
46
+ getCellValueByColumnId: (colId: string) => row[colId],
47
+ }))
48
+
49
+ let rows = buildRows(backing)
50
+
51
+ const ctx: any = {
52
+ allColumns,
53
+ selectionRange: { anchor: null, focus: null } as any,
54
+ selectionRanges: [] as any[],
55
+ fillDrag: null,
56
+ moveDrag: null,
57
+ moveGrabHover: false,
58
+ moveCellsEffective: opts.moveCells ?? true,
59
+ editedCellValues: {} as Record<string, unknown>,
60
+ userHasActivatedCell: false,
61
+ gridRootEl: null,
62
+ props: {} as Record<string, unknown>,
63
+ history: [] as any[],
64
+ historyPtr: -1,
65
+ historyVersion: 0,
66
+ UNDO_LIMIT: 200,
67
+ activeCell: null as { rowIndex: number; colIndex: number } | null,
68
+ setActiveCell(rowIndex: number, colIndex: number) {
69
+ ctx.activeCell = { rowIndex, colIndex }
70
+ },
71
+ findColumnById: (id: string) => allColumns.find((c) => c.id === id),
72
+ isCellEditableAt: opts.editableAt ?? (() => true),
73
+ getCellDisplayValue: (_r: string, _c: string, base: unknown) => base,
74
+ grid: { getState: () => ({ activeCell: ctx.activeCell }), store: { setState: vi.fn() } },
75
+ getSelectionRects() {
76
+ const norm = (r: any) =>
77
+ r?.anchor && r?.focus
78
+ ? {
79
+ minRow: Math.min(r.anchor.rowIndex, r.focus.rowIndex),
80
+ maxRow: Math.max(r.anchor.rowIndex, r.focus.rowIndex),
81
+ minCol: Math.min(r.anchor.colIndex, r.focus.colIndex),
82
+ maxCol: Math.max(r.anchor.colIndex, r.focus.colIndex),
83
+ }
84
+ : null
85
+ const rects: any[] = []
86
+ for (const r of ctx.selectionRanges) {
87
+ const n = norm(r)
88
+ if (n) rects.push(n)
89
+ }
90
+ const active = norm(ctx.selectionRange)
91
+ if (active) rects.push(active)
92
+ return rects
93
+ },
94
+ // Mirrors the real getCellRangeEdges: first containing rect wins.
95
+ getCellRangeEdges(rowIndex: number, colIndex: number) {
96
+ for (const rect of ctx.getSelectionRects()) {
97
+ if (
98
+ rowIndex < rect.minRow ||
99
+ rowIndex > rect.maxRow ||
100
+ colIndex < rect.minCol ||
101
+ colIndex > rect.maxCol
102
+ )
103
+ continue
104
+ return {
105
+ top: rowIndex === rect.minRow,
106
+ bottom: rowIndex === rect.maxRow,
107
+ left: colIndex === rect.minCol,
108
+ right: colIndex === rect.maxCol,
109
+ }
110
+ }
111
+ return null
112
+ },
113
+ }
114
+
115
+ Object.defineProperty(ctx, 'internalData', {
116
+ get: () => backing,
117
+ set: (next: Array<Record<string, unknown>>) => {
118
+ backing = next
119
+ rows = buildRows(next)
120
+ },
121
+ })
122
+ Object.defineProperty(ctx, 'allRows', { get: () => rows })
123
+
124
+ return ctx
125
+ }
126
+
127
+ /** Snapshot the grid as a plain matrix so assertions read like the screen. */
128
+ function grid(ctx: any) {
129
+ return ctx.internalData.map((row: Record<string, unknown>) =>
130
+ ctx.allColumns.map((col: any) => row[col.columnDef.field as string]),
131
+ )
132
+ }
133
+
134
+ /** Select the rectangle (r0,c0)-(r1,c1) as the ACTIVE range. */
135
+ function select(ctx: any, r0: number, c0: number, r1: number, c1: number) {
136
+ ctx.selectionRange = {
137
+ anchor: { rowIndex: r0, colIndex: c0 },
138
+ focus: { rowIndex: r1, colIndex: c1 },
139
+ }
140
+ }
141
+
142
+ /** Run a whole drag: grab (grabRow,grabCol), drop on (targetRow,targetCol). */
143
+ function drop(
144
+ cb: ReturnType<typeof createClipboard>,
145
+ ctx: any,
146
+ grab: [number, number],
147
+ target: [number, number],
148
+ copy = false,
149
+ ) {
150
+ ctx.moveDrag = {
151
+ sourceMinRow: Math.min(ctx.selectionRange.anchor.rowIndex, ctx.selectionRange.focus.rowIndex),
152
+ sourceMaxRow: Math.max(ctx.selectionRange.anchor.rowIndex, ctx.selectionRange.focus.rowIndex),
153
+ sourceMinCol: Math.min(ctx.selectionRange.anchor.colIndex, ctx.selectionRange.focus.colIndex),
154
+ sourceMaxCol: Math.max(ctx.selectionRange.anchor.colIndex, ctx.selectionRange.focus.colIndex),
155
+ grabRow: grab[0],
156
+ grabCol: grab[1],
157
+ targetRow: target[0],
158
+ targetCol: target[1],
159
+ copy,
160
+ }
161
+ cb.onMovePointerUp()
162
+ }
163
+
164
+ describe('moveDestRect', () => {
165
+ const base = {
166
+ sourceMinRow: 1,
167
+ sourceMaxRow: 2,
168
+ sourceMinCol: 0,
169
+ sourceMaxCol: 1,
170
+ grabRow: 1,
171
+ grabCol: 0,
172
+ }
173
+
174
+ it('offsets the source rectangle by how far the pointer travelled', () => {
175
+ const cb = createClipboard(makeCtx())
176
+ expect(cb.moveDestRect({ ...base, targetRow: 2, targetCol: 1 })).toEqual({
177
+ minRow: 2,
178
+ maxRow: 3,
179
+ minCol: 1,
180
+ maxCol: 2,
181
+ })
182
+ })
183
+
184
+ it('measures the offset from the GRAB point, not the rectangle corner', () => {
185
+ const cb = createClipboard(makeCtx())
186
+ // Grabbed the bottom-right cell of the range and dropped it one row down:
187
+ // the whole rectangle shifts by one, it does not jump to the pointer.
188
+ expect(
189
+ cb.moveDestRect({ ...base, grabRow: 2, grabCol: 1, targetRow: 3, targetCol: 1 }),
190
+ ).toEqual({ minRow: 2, maxRow: 3, minCol: 0, maxCol: 1 })
191
+ })
192
+
193
+ it('is null when the drag never left the cell it started on', () => {
194
+ const cb = createClipboard(makeCtx())
195
+ expect(cb.moveDestRect({ ...base, targetRow: 1, targetCol: 0 })).toBeNull()
196
+ })
197
+
198
+ it('is null past every edge of the grid rather than clamping', () => {
199
+ // 4 rows, 3 columns. The source is rows 1-2 x cols 0-1, so each of these
200
+ // is a drag a user could really make - the destination just does not fit.
201
+ const cb = createClipboard(makeCtx())
202
+ // Grab the bottom row, drop it on the top row: the rectangle starts at -1.
203
+ expect(cb.moveDestRect({ ...base, grabRow: 2, targetRow: 0, targetCol: 0 })).toBeNull()
204
+ // Grab the right column, drop it on the left one: starts at column -1.
205
+ expect(cb.moveDestRect({ ...base, grabCol: 1, targetRow: 1, targetCol: 0 })).toBeNull()
206
+ expect(cb.moveDestRect({ ...base, targetRow: 3, targetCol: 0 })).toBeNull() // past row 3
207
+ expect(cb.moveDestRect({ ...base, targetRow: 1, targetCol: 2 })).toBeNull() // past column 2
208
+ })
209
+
210
+ it('allows a shift that lands exactly on the first row', () => {
211
+ // The guard is "outside the grid", not "close to the edge" - one row up
212
+ // from row 1 is row 0, which is a perfectly good destination.
213
+ const cb = createClipboard(makeCtx())
214
+ expect(cb.moveDestRect({ ...base, targetRow: 0, targetCol: 0 })).toEqual({
215
+ minRow: 0,
216
+ maxRow: 1,
217
+ minCol: 0,
218
+ maxCol: 1,
219
+ })
220
+ })
221
+ })
222
+
223
+ describe('applyMoveRange - moving', () => {
224
+ it('moves the values and blanks the cells they came from', () => {
225
+ const ctx = makeCtx()
226
+ const cb = createClipboard(ctx)
227
+ select(ctx, 0, 0, 1, 0)
228
+ drop(cb, ctx, [0, 0], [0, 1])
229
+ expect(grid(ctx)).toEqual([
230
+ [null, 'a0', 'c0'],
231
+ [null, 'a1', 'c1'],
232
+ ['a2', 'b2', 'c2'],
233
+ ['a3', 'b3', 'c3'],
234
+ ])
235
+ })
236
+
237
+ it('leaves no ghost when the source and destination overlap', () => {
238
+ const ctx = makeCtx()
239
+ const cb = createClipboard(ctx)
240
+ // Three rows of column a, nudged down by one: rows 1-3 take the old
241
+ // 0-2 values and row 0 is emptied. If the source were read lazily instead
242
+ // of snapshotted, row 2 would receive the value row 1 had just been given.
243
+ select(ctx, 0, 0, 2, 0)
244
+ drop(cb, ctx, [0, 0], [1, 0])
245
+ expect(grid(ctx).map((r: unknown[]) => r[0])).toEqual([null, 'a0', 'a1', 'a2'])
246
+ })
247
+
248
+ it('carries a 2x2 block across intact', () => {
249
+ const ctx = makeCtx()
250
+ const cb = createClipboard(ctx)
251
+ select(ctx, 0, 0, 1, 1)
252
+ drop(cb, ctx, [0, 0], [2, 1])
253
+ expect(grid(ctx)).toEqual([
254
+ [null, null, 'c0'],
255
+ [null, null, 'c1'],
256
+ ['a2', 'a0', 'b0'],
257
+ ['a3', 'a1', 'b1'],
258
+ ])
259
+ })
260
+
261
+ it('moves the range whose border was grabbed, not the active one', () => {
262
+ const ctx = makeCtx()
263
+ const cb = createClipboard(ctx)
264
+ // A committed range on column a, plus an active one on column c.
265
+ ctx.selectionRanges = [
266
+ { anchor: { rowIndex: 0, colIndex: 0 }, focus: { rowIndex: 0, colIndex: 0 } },
267
+ ]
268
+ select(ctx, 3, 2, 3, 2)
269
+ // startMoveDrag resolves the source through getSelectionRects, so seed the
270
+ // drag the same way the pointer path would for the COMMITTED rectangle.
271
+ ctx.moveDrag = {
272
+ sourceMinRow: 0,
273
+ sourceMaxRow: 0,
274
+ sourceMinCol: 0,
275
+ sourceMaxCol: 0,
276
+ grabRow: 0,
277
+ grabCol: 0,
278
+ targetRow: 1,
279
+ targetCol: 0,
280
+ copy: false,
281
+ }
282
+ cb.onMovePointerUp()
283
+ expect(grid(ctx)[0]![0]).toBeNull()
284
+ expect(grid(ctx)[1]![0]).toBe('a0')
285
+ // The active range's own cell is untouched.
286
+ expect(grid(ctx)[3]![2]).toBe('c3')
287
+ })
288
+ })
289
+
290
+ describe('applyMoveRange - copying', () => {
291
+ it('leaves the source in place when copy is held', () => {
292
+ const ctx = makeCtx()
293
+ const cb = createClipboard(ctx)
294
+ select(ctx, 0, 0, 1, 0)
295
+ drop(cb, ctx, [0, 0], [0, 1], true)
296
+ expect(grid(ctx)).toEqual([
297
+ ['a0', 'a0', 'c0'],
298
+ ['a1', 'a1', 'c1'],
299
+ ['a2', 'b2', 'c2'],
300
+ ['a3', 'b3', 'c3'],
301
+ ])
302
+ })
303
+
304
+ it('copies even out of read-only source cells', () => {
305
+ // Nothing is written to the source, so its editability is irrelevant -
306
+ // a read-only column is still copyable, which is the point of copy.
307
+ const ctx = makeCtx({ editableAt: (_r, c) => c !== 0 })
308
+ const cb = createClipboard(ctx)
309
+ select(ctx, 0, 0, 0, 0)
310
+ drop(cb, ctx, [0, 0], [0, 1], true)
311
+ expect(grid(ctx)[0]).toEqual(['a0', 'a0', 'c0'])
312
+ })
313
+ })
314
+
315
+ describe('applyMoveRange - refusals', () => {
316
+ it('changes nothing when the destination runs off the grid', () => {
317
+ const ctx = makeCtx()
318
+ const cb = createClipboard(ctx)
319
+ const before = grid(ctx)
320
+ select(ctx, 2, 0, 3, 0)
321
+ drop(cb, ctx, [2, 0], [3, 0]) // would need rows 3-4; there is no row 4
322
+ expect(grid(ctx)).toEqual(before)
323
+ })
324
+
325
+ it('refuses the whole move when ANY destination cell is read-only', () => {
326
+ // Half-applying would split the range across two places with nothing on
327
+ // screen to say which half landed.
328
+ const ctx = makeCtx({ editableAt: (r, c) => !(c === 1 && r === 1) })
329
+ const cb = createClipboard(ctx)
330
+ const before = grid(ctx)
331
+ select(ctx, 0, 0, 1, 0)
332
+ drop(cb, ctx, [0, 0], [0, 1])
333
+ expect(grid(ctx)).toEqual(before)
334
+ })
335
+
336
+ it('refuses a move out of a read-only source, which would have to be blanked', () => {
337
+ const ctx = makeCtx({ editableAt: (_r, c) => c !== 0 })
338
+ const cb = createClipboard(ctx)
339
+ const before = grid(ctx)
340
+ select(ctx, 0, 0, 0, 0)
341
+ drop(cb, ctx, [0, 0], [0, 1])
342
+ expect(grid(ctx)).toEqual(before)
343
+ })
344
+
345
+ it('refuses when a column on either side has no field to write back to', () => {
346
+ const ctx = makeCtx({
347
+ columns: [{ id: 'a', field: 'a' }, { id: 'computed' }, { id: 'c', field: 'c' }],
348
+ })
349
+ const cb = createClipboard(ctx)
350
+ const before = grid(ctx)
351
+ select(ctx, 0, 0, 0, 0)
352
+ drop(cb, ctx, [0, 0], [0, 1])
353
+ expect(grid(ctx)).toEqual(before)
354
+ })
355
+
356
+ it('clears the drag even when it refuses, so the pointer stops being tracked', () => {
357
+ const ctx = makeCtx()
358
+ const cb = createClipboard(ctx)
359
+ select(ctx, 2, 0, 3, 0)
360
+ drop(cb, ctx, [2, 0], [3, 0])
361
+ expect(ctx.moveDrag).toBeNull()
362
+ })
363
+ })
364
+
365
+ describe('applyMoveRange - aftermath', () => {
366
+ it('records one undo step per changed cell', () => {
367
+ const ctx = makeCtx()
368
+ const cb = createClipboard(ctx)
369
+ select(ctx, 0, 0, 1, 0)
370
+ drop(cb, ctx, [0, 0], [0, 1])
371
+ // 2 source cells blanked + 2 destination cells written.
372
+ expect(ctx.history).toHaveLength(4)
373
+ expect(ctx.historyPtr).toBe(3)
374
+ expect(ctx.history[0]).toMatchObject({ rowId: '0', columnId: 'a', before: 'a0', after: null })
375
+ expect(ctx.history[2]).toMatchObject({ rowId: '0', columnId: 'b', before: 'b0', after: 'a0' })
376
+ })
377
+
378
+ it('truncates any redo history the way a normal edit does', () => {
379
+ const ctx = makeCtx()
380
+ ctx.history = [{ rowId: 'x', columnId: 'a', field: 'a', before: 1, after: 2 }]
381
+ ctx.historyPtr = -1 // an undone step, still redoable
382
+ const cb = createClipboard(ctx)
383
+ select(ctx, 0, 0, 0, 0)
384
+ drop(cb, ctx, [0, 0], [0, 1])
385
+ expect(ctx.history.map((s: any) => s.rowId)).not.toContain('x')
386
+ })
387
+
388
+ it('leaves the moved range selected where it landed', () => {
389
+ const ctx = makeCtx()
390
+ const cb = createClipboard(ctx)
391
+ select(ctx, 0, 0, 1, 1)
392
+ drop(cb, ctx, [0, 0], [2, 1])
393
+ expect(ctx.selectionRange).toEqual({
394
+ anchor: { rowIndex: 2, colIndex: 1 },
395
+ focus: { rowIndex: 3, colIndex: 2 },
396
+ })
397
+ expect(ctx.selectionRanges).toEqual([])
398
+ expect(ctx.activeCell).toEqual({ rowIndex: 2, colIndex: 1 })
399
+ })
400
+
401
+ it('reports every write through onCellValueChange', () => {
402
+ const ctx = makeCtx()
403
+ const onCellValueChange = vi.fn()
404
+ ctx.props.onCellValueChange = onCellValueChange
405
+ const cb = createClipboard(ctx)
406
+ select(ctx, 0, 0, 0, 0)
407
+ drop(cb, ctx, [0, 0], [0, 1])
408
+ expect(onCellValueChange).toHaveBeenCalledTimes(2)
409
+ expect(onCellValueChange).toHaveBeenCalledWith(
410
+ expect.objectContaining({ columnId: 'a', oldValue: 'a0', newValue: null }),
411
+ )
412
+ expect(onCellValueChange).toHaveBeenCalledWith(
413
+ expect.objectContaining({ columnId: 'b', oldValue: 'b0', newValue: 'a0' }),
414
+ )
415
+ })
416
+ })
417
+
418
+ // ---------------------------------------------------------------------------
419
+ // The grab strip. jsdom gives every element a zero rect, so the cell's
420
+ // geometry is stubbed - these assertions are about the 4px arithmetic, not
421
+ // about layout.
422
+ // ---------------------------------------------------------------------------
423
+
424
+ function cellEl(width = 100, height = 24, left = 0, top = 0) {
425
+ return {
426
+ getBoundingClientRect: () => ({ left, top, width, height, right: left + width, bottom: top + height }),
427
+ } as unknown as HTMLElement
428
+ }
429
+
430
+ describe('isOnMoveGrabStrip', () => {
431
+ it('hits the outer 4px of each edge the cell sits on', () => {
432
+ const ctx = makeCtx()
433
+ const cb = createClipboard(ctx)
434
+ select(ctx, 1, 1, 2, 2) // the cell at (1,1) is on the top and left edges
435
+ const el = cellEl()
436
+ expect(cb.isOnMoveGrabStrip(el, 1, 1, 50, 2)).toBe(true) // top strip
437
+ expect(cb.isOnMoveGrabStrip(el, 1, 1, 3, 12)).toBe(true) // left strip
438
+ expect(cb.isOnMoveGrabStrip(el, 2, 2, 50, 22)).toBe(true) // bottom strip
439
+ expect(cb.isOnMoveGrabStrip(el, 2, 2, 98, 12)).toBe(true) // right strip
440
+ })
441
+
442
+ it('misses the interior, so a plain click still starts a new selection', () => {
443
+ const ctx = makeCtx()
444
+ const cb = createClipboard(ctx)
445
+ select(ctx, 1, 1, 2, 2)
446
+ expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 12)).toBe(false)
447
+ })
448
+
449
+ it('misses the edge a cell is NOT on', () => {
450
+ const ctx = makeCtx()
451
+ const cb = createClipboard(ctx)
452
+ select(ctx, 1, 1, 2, 2)
453
+ // (1,1) is the top-LEFT corner: its bottom and right sides are interior.
454
+ expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 22)).toBe(false)
455
+ expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 98, 12)).toBe(false)
456
+ })
457
+
458
+ it('misses every cell outside the selection', () => {
459
+ const ctx = makeCtx()
460
+ const cb = createClipboard(ctx)
461
+ select(ctx, 1, 1, 2, 2)
462
+ expect(cb.isOnMoveGrabStrip(cellEl(), 0, 0, 1, 1)).toBe(false)
463
+ })
464
+
465
+ it('is off entirely when moveCells is disabled', () => {
466
+ const ctx = makeCtx({ moveCells: false })
467
+ const cb = createClipboard(ctx)
468
+ select(ctx, 1, 1, 2, 2)
469
+ expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 2)).toBe(false)
470
+ })
471
+
472
+ it('accounts for the cell offset, not just its size', () => {
473
+ const ctx = makeCtx()
474
+ const cb = createClipboard(ctx)
475
+ select(ctx, 1, 1, 1, 1)
476
+ const el = cellEl(100, 24, 300, 500)
477
+ expect(cb.isOnMoveGrabStrip(el, 1, 1, 350, 502)).toBe(true)
478
+ expect(cb.isOnMoveGrabStrip(el, 1, 1, 350, 512)).toBe(false)
479
+ })
480
+ })
481
+
482
+ describe('startMoveDrag', () => {
483
+ function pointerEvent(x: number, y: number, target: unknown, extra: Record<string, unknown> = {}) {
484
+ return {
485
+ button: 0,
486
+ clientX: x,
487
+ clientY: y,
488
+ target,
489
+ ctrlKey: false,
490
+ metaKey: false,
491
+ stopPropagation: vi.fn(),
492
+ preventDefault: vi.fn(),
493
+ ...extra,
494
+ } as unknown as PointerEvent
495
+ }
496
+
497
+ /** A stub `td` that answers `closest()` with itself, as the real one would. */
498
+ function td(width = 100, height = 24) {
499
+ const el: any = cellEl(width, height)
500
+ el.closest = () => el
501
+ return el as HTMLElement
502
+ }
503
+
504
+ it('starts a drag on the border and reports that it claimed the event', () => {
505
+ const ctx = makeCtx()
506
+ const cb = createClipboard(ctx)
507
+ select(ctx, 1, 0, 2, 1)
508
+ const el = td()
509
+ expect(cb.startMoveDrag(pointerEvent(50, 2, el), 1, 0)).toBe(true)
510
+ expect(ctx.moveDrag).toMatchObject({
511
+ sourceMinRow: 1,
512
+ sourceMaxRow: 2,
513
+ sourceMinCol: 0,
514
+ sourceMaxCol: 1,
515
+ grabRow: 1,
516
+ grabCol: 0,
517
+ targetRow: 1,
518
+ targetCol: 0,
519
+ copy: false,
520
+ })
521
+ })
522
+
523
+ it('declines an interior pointerdown and leaves the selection path alone', () => {
524
+ const ctx = makeCtx()
525
+ const cb = createClipboard(ctx)
526
+ select(ctx, 1, 0, 2, 1)
527
+ const event = pointerEvent(50, 12, td())
528
+ expect(cb.startMoveDrag(event, 1, 0)).toBe(false)
529
+ expect(ctx.moveDrag).toBeNull()
530
+ expect(event.preventDefault).not.toHaveBeenCalled()
531
+ })
532
+
533
+ it('declines touch, which would otherwise block the page from scrolling', () => {
534
+ // This path calls preventDefault, so a finger landing within 4px of a
535
+ // selection border would start a move AND kill native scroll for that
536
+ // gesture. Drag-select already refuses touch for a milder version of the
537
+ // same problem (issue #23).
538
+ const ctx = makeCtx()
539
+ const cb = createClipboard(ctx)
540
+ select(ctx, 1, 0, 2, 1)
541
+ const event = pointerEvent(50, 2, td(), { pointerType: 'touch' })
542
+ expect(cb.startMoveDrag(event, 1, 0)).toBe(false)
543
+ expect(ctx.moveDrag).toBeNull()
544
+ expect(event.preventDefault).not.toHaveBeenCalled()
545
+ })
546
+
547
+ it('still accepts a pen, which has hover and does not scroll the page', () => {
548
+ const ctx = makeCtx()
549
+ const cb = createClipboard(ctx)
550
+ select(ctx, 1, 0, 2, 1)
551
+ expect(cb.startMoveDrag(pointerEvent(50, 2, td(), { pointerType: 'pen' }), 1, 0)).toBe(true)
552
+ })
553
+
554
+ it('declines anything but the primary button', () => {
555
+ const ctx = makeCtx()
556
+ const cb = createClipboard(ctx)
557
+ select(ctx, 1, 0, 2, 1)
558
+ expect(cb.startMoveDrag(pointerEvent(50, 2, td(), { button: 2 }), 1, 0)).toBe(false)
559
+ })
560
+
561
+ it('reads Ctrl (and Cmd) as copy', () => {
562
+ const ctx = makeCtx()
563
+ const cb = createClipboard(ctx)
564
+ select(ctx, 1, 0, 1, 0)
565
+ cb.startMoveDrag(pointerEvent(50, 2, td(), { ctrlKey: true }), 1, 0)
566
+ expect(ctx.moveDrag.copy).toBe(true)
567
+ ctx.moveDrag = null
568
+ cb.startMoveDrag(pointerEvent(50, 2, td(), { metaKey: true }), 1, 0)
569
+ expect(ctx.moveDrag.copy).toBe(true)
570
+ })
571
+ })
572
+
573
+ describe('onMovePointerMove', () => {
574
+ function moveEvent(extra: Record<string, unknown> = {}) {
575
+ return { clientX: 10, clientY: 10, ctrlKey: false, metaKey: false, ...extra } as PointerEvent
576
+ }
577
+
578
+ /** jsdom does not implement `document.elementFromPoint` at all - it is not
579
+ * a stub returning null, the property is absent - so `vi.spyOn` has
580
+ * nothing to wrap. Install it for the duration of one test. */
581
+ function stubElementFromPoint(result: Element | null) {
582
+ const calls = { count: 0 }
583
+ Object.defineProperty(document, 'elementFromPoint', {
584
+ configurable: true,
585
+ writable: true,
586
+ value: () => {
587
+ calls.count += 1
588
+ return result
589
+ },
590
+ })
591
+ return calls
592
+ }
593
+
594
+ afterEach(() => {
595
+ delete (document as unknown as Record<string, unknown>).elementFromPoint
596
+ })
597
+
598
+ function seed(ctx: any) {
599
+ ctx.moveDrag = {
600
+ sourceMinRow: 0,
601
+ sourceMaxRow: 0,
602
+ sourceMinCol: 0,
603
+ sourceMaxCol: 0,
604
+ grabRow: 0,
605
+ grabCol: 0,
606
+ targetRow: 0,
607
+ targetCol: 0,
608
+ copy: false,
609
+ }
610
+ }
611
+
612
+ it('follows the cell under the pointer', () => {
613
+ const ctx = makeCtx()
614
+ const cb = createClipboard(ctx)
615
+ seed(ctx)
616
+ const cell: any = { dataset: { svgridRow: '2', svgridCol: '1' } }
617
+ cell.closest = () => cell
618
+ stubElementFromPoint(cell)
619
+ cb.onMovePointerMove(moveEvent())
620
+ expect(ctx.moveDrag).toMatchObject({ targetRow: 2, targetCol: 1 })
621
+ })
622
+
623
+ it('picks up the copy modifier mid-drag, even between cells', () => {
624
+ const ctx = makeCtx()
625
+ const cb = createClipboard(ctx)
626
+ seed(ctx)
627
+ stubElementFromPoint(null)
628
+ cb.onMovePointerMove(moveEvent({ ctrlKey: true }))
629
+ expect(ctx.moveDrag.copy).toBe(true)
630
+ cb.onMovePointerMove(moveEvent({ ctrlKey: false }))
631
+ expect(ctx.moveDrag.copy).toBe(false)
632
+ })
633
+
634
+ it('takes the copy modifier from the release, not just the last move', () => {
635
+ // 'drag, then press Ctrl, then let go' is the natural way to do it, and
636
+ // pointermove has stopped firing by then.
637
+ const ctx = makeCtx()
638
+ const cb = createClipboard(ctx)
639
+ select(ctx, 0, 0, 0, 0)
640
+ ctx.moveDrag = {
641
+ sourceMinRow: 0, sourceMaxRow: 0, sourceMinCol: 0, sourceMaxCol: 0,
642
+ grabRow: 0, grabCol: 0, targetRow: 0, targetCol: 1, copy: false,
643
+ }
644
+ cb.onMovePointerUp({ ctrlKey: true, metaKey: false } as PointerEvent)
645
+ expect(grid(ctx)[0]).toEqual(['a0', 'a0', 'c0'])
646
+ })
647
+
648
+ it('does nothing at all when no move is in flight', () => {
649
+ const ctx = makeCtx()
650
+ const cb = createClipboard(ctx)
651
+ const calls = stubElementFromPoint(null)
652
+ cb.onMovePointerMove(moveEvent())
653
+ expect(calls.count).toBe(0)
654
+ })
655
+ })
656
+
657
+ // ---------------------------------------------------------------------------
658
+ // Edge auto-scroll. Shared by the fill drag, the range move and plain
659
+ // drag-select: without it a drag can only ever reach cells that were already
660
+ // on screen when it started.
661
+ // ---------------------------------------------------------------------------
662
+
663
+ describe('edge auto-scroll', () => {
664
+ const HOVERED = { row: 3, col: 2 }
665
+
666
+ beforeEach(() => {
667
+ const cell: any = { dataset: { svgridRow: String(HOVERED.row), svgridCol: String(HOVERED.col) } }
668
+ cell.closest = () => cell
669
+ Object.defineProperty(document, 'elementFromPoint', {
670
+ configurable: true,
671
+ writable: true,
672
+ value: () => cell,
673
+ })
674
+ })
675
+ afterEach(() => {
676
+ delete (document as unknown as Record<string, unknown>).elementFromPoint
677
+ })
678
+
679
+ /** A stand-in scroll container with a real rect and settable scroll offsets. */
680
+ function scroller(over: Record<string, unknown> = {}): any {
681
+ return {
682
+ scrollTop: 100,
683
+ scrollLeft: 100,
684
+ getBoundingClientRect: () => ({ left: 0, top: 0, right: 500, bottom: 300, width: 500, height: 300 }),
685
+ ...over,
686
+ }
687
+ }
688
+
689
+ /** Drive N animation frames synchronously. */
690
+ type Flush = (n?: number) => void
691
+ function withFrames<T>(run: (flush: Flush, queue: Array<FrameRequestCallback | null>) => T): T {
692
+ const queue: Array<FrameRequestCallback | null> = []
693
+ const raf = globalThis.requestAnimationFrame
694
+ const caf = globalThis.cancelAnimationFrame
695
+ globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => { queue.push(cb); return queue.length }
696
+ globalThis.cancelAnimationFrame = (id: number) => { queue[id - 1] = null }
697
+ const flush: Flush = (n = 1) => {
698
+ for (let i = 0; i < n; i += 1) {
699
+ const cb = queue.shift()
700
+ if (cb) cb(0)
701
+ }
702
+ }
703
+ try { return run(flush, queue) } finally {
704
+ globalThis.requestAnimationFrame = raf
705
+ globalThis.cancelAnimationFrame = caf
706
+ }
707
+ }
708
+
709
+ function movingCtx(over: Record<string, unknown> = {}) {
710
+ const ctx = makeCtx()
711
+ ctx.scrollContainer = scroller(over)
712
+ ctx.isDraggingSelection = false
713
+ ctx.extendSelection = vi.fn()
714
+ ctx.moveDrag = {
715
+ sourceMinRow: 0, sourceMaxRow: 0, sourceMinCol: 0, sourceMaxCol: 0,
716
+ grabRow: 0, grabCol: 0, targetRow: 0, targetCol: 0, copy: false,
717
+ }
718
+ return ctx
719
+ }
720
+
721
+ it('scrolls down while the pointer sits near the bottom edge', () => {
722
+ const ctx = movingCtx()
723
+ const cb = createClipboard(ctx)
724
+ withFrames((flush) => {
725
+ // Container bottom is 300; 295 is well inside the 40px band.
726
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
727
+ flush()
728
+ expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(100)
729
+ })
730
+ })
731
+
732
+ it('scrolls up, left and right from the matching edges', () => {
733
+ const cases: Array<[number, number, 'scrollTop' | 'scrollLeft', number]> = [
734
+ [250, 5, 'scrollTop', -1],
735
+ [5, 150, 'scrollLeft', -1],
736
+ [495, 150, 'scrollLeft', 1],
737
+ ]
738
+ for (const [x, y, axis, dir] of cases) {
739
+ const ctx = movingCtx()
740
+ const cb = createClipboard(ctx)
741
+ withFrames((flush) => {
742
+ cb.trackEdgeScroll({ clientX: x, clientY: y } as PointerEvent)
743
+ flush()
744
+ const moved = ctx.scrollContainer[axis] - 100
745
+ expect(Math.sign(moved), `${axis} at (${x},${y})`).toBe(dir)
746
+ })
747
+ }
748
+ })
749
+
750
+ it('does nothing while the pointer is in the middle', () => {
751
+ const ctx = movingCtx()
752
+ const cb = createClipboard(ctx)
753
+ withFrames((flush) => {
754
+ cb.trackEdgeScroll({ clientX: 250, clientY: 150 } as PointerEvent)
755
+ flush()
756
+ expect(ctx.scrollContainer.scrollTop).toBe(100)
757
+ expect(ctx.scrollContainer.scrollLeft).toBe(100)
758
+ })
759
+ })
760
+
761
+ it('ramps: deeper into the band scrolls faster', () => {
762
+ const at = (y: number) => {
763
+ const ctx = movingCtx()
764
+ const cb = createClipboard(ctx)
765
+ return withFrames((flush) => {
766
+ cb.trackEdgeScroll({ clientX: 250, clientY: y } as PointerEvent)
767
+ flush()
768
+ return ctx.scrollContainer.scrollTop - 100
769
+ })
770
+ }
771
+ // 270 is 10px into the 40px band, 299 is at the very edge.
772
+ expect(at(299)).toBeGreaterThan(at(270))
773
+ })
774
+
775
+ it('keeps scrolling frame after frame with the pointer parked still', () => {
776
+ const ctx = movingCtx()
777
+ const cb = createClipboard(ctx)
778
+ withFrames((flush) => {
779
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
780
+ flush()
781
+ const after1 = ctx.scrollContainer.scrollTop
782
+ flush()
783
+ expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(after1)
784
+ })
785
+ })
786
+
787
+ it('stops as soon as the drag ends', () => {
788
+ const ctx = movingCtx()
789
+ const cb = createClipboard(ctx)
790
+ withFrames((flush) => {
791
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
792
+ flush()
793
+ const parked = ctx.scrollContainer.scrollTop
794
+ ctx.moveDrag = null
795
+ flush()
796
+ flush()
797
+ expect(ctx.scrollContainer.scrollTop).toBe(parked)
798
+ })
799
+ })
800
+
801
+ it('re-targets the drag at whatever cell the scroll brought under the pointer', () => {
802
+ const ctx = movingCtx()
803
+ const cb = createClipboard(ctx)
804
+ withFrames((flush) => {
805
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
806
+ flush()
807
+ })
808
+ expect(ctx.moveDrag).toMatchObject({ targetRow: HOVERED.row, targetCol: HOVERED.col })
809
+ })
810
+
811
+ it('does not re-target when the scroll is already clamped at the end', () => {
812
+ // scrollTop never moves, so the pointer is parked over one cell and
813
+ // re-targeting every frame would flicker the selection.
814
+ const stuck = scroller()
815
+ Object.defineProperty(stuck, 'scrollTop', { get: () => 0, set: () => {} })
816
+ const ctx = movingCtx()
817
+ ctx.scrollContainer = stuck
818
+ const cb = createClipboard(ctx)
819
+ const before = { ...ctx.moveDrag }
820
+ withFrames((flush) => {
821
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
822
+ flush()
823
+ flush()
824
+ })
825
+ expect(ctx.moveDrag).toEqual(before)
826
+ })
827
+
828
+ it('drives a plain drag-select too, not just fill and move', () => {
829
+ const ctx = movingCtx()
830
+ ctx.moveDrag = null
831
+ ctx.isDraggingSelection = true
832
+ const cb = createClipboard(ctx)
833
+ withFrames((flush) => {
834
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
835
+ flush()
836
+ expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(100)
837
+ })
838
+ })
839
+
840
+ it('never starts when nothing is being dragged', () => {
841
+ const ctx = movingCtx()
842
+ ctx.moveDrag = null
843
+ const cb = createClipboard(ctx)
844
+ withFrames((flush) => {
845
+ cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
846
+ flush()
847
+ expect(ctx.scrollContainer.scrollTop).toBe(100)
848
+ })
849
+ })
850
+ })