@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,369 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { rowResize } from './row-resize'
3
+ import type { RowResizeOptions } from './row-resize'
4
+
5
+ const STRIP_CLASS = 'sv-grid-row-resize-handle'
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Helpers
9
+ // ---------------------------------------------------------------------------
10
+
11
+ /** Build a host `<table>` with `rows` body rows. Each row carries the
12
+ * `tr.sv-grid-row` class + a `.sv-row-gutter` cell holding a
13
+ * `data-svgrid-row` marker, matching what SvGrid renders. Extra classes
14
+ * let individual tests opt rows out (header / spacer / no-gutter). */
15
+ function buildGrid(
16
+ specs: Array<{ rowClasses?: string[]; gutter?: boolean; rowIndex?: number }>,
17
+ ): { host: HTMLElement; rows: HTMLTableRowElement[] } {
18
+ const host = document.createElement('div')
19
+ const table = document.createElement('table')
20
+ const tbody = document.createElement('tbody')
21
+ const rows: HTMLTableRowElement[] = []
22
+ specs.forEach((spec, i) => {
23
+ const tr = document.createElement('tr')
24
+ tr.className = ['sv-grid-row', ...(spec.rowClasses ?? [])].join(' ')
25
+ const cell = document.createElement('td')
26
+ cell.className = 'sv-grid-cell'
27
+ if (spec.gutter !== false) cell.classList.add('sv-row-gutter')
28
+ const marker = document.createElement('span')
29
+ marker.setAttribute('data-svgrid-row', String(spec.rowIndex ?? i))
30
+ cell.appendChild(marker)
31
+ tr.appendChild(cell)
32
+ tbody.appendChild(tr)
33
+ rows.push(tr)
34
+ })
35
+ table.appendChild(tbody)
36
+ host.appendChild(table)
37
+ document.body.appendChild(host)
38
+ return { host, rows }
39
+ }
40
+
41
+ function gutterOf(tr: HTMLTableRowElement): HTMLTableCellElement {
42
+ return tr.querySelector<HTMLTableCellElement>('.sv-row-gutter')!
43
+ }
44
+ function stripOf(tr: HTMLTableRowElement): HTMLElement | null {
45
+ return tr.querySelector<HTMLElement>(`.${STRIP_CLASS}`)
46
+ }
47
+
48
+ /** Mock getBoundingClientRect so a TR reports a real height in jsdom
49
+ * (which otherwise returns all-zero rects). */
50
+ function mockRowHeight(tr: HTMLTableRowElement, height: number): void {
51
+ tr.getBoundingClientRect = () =>
52
+ ({ height, width: 100, top: 0, left: 0, right: 100, bottom: height, x: 0, y: 0, toJSON() {} }) as DOMRect
53
+ }
54
+
55
+ function pointer(type: string, init: Partial<PointerEvent> = {}): PointerEvent {
56
+ return new PointerEvent(type, { bubbles: true, cancelable: true, pointerId: 1, ...init } as PointerEventInit)
57
+ }
58
+
59
+ const hosts: HTMLElement[] = []
60
+ function track(host: HTMLElement): HTMLElement {
61
+ hosts.push(host)
62
+ return host
63
+ }
64
+
65
+ beforeEach(() => {
66
+ // jsdom lacks pointer-capture methods on elements; stub them so the action's
67
+ // try/catch wrapped calls have something to invoke.
68
+ if (!HTMLElement.prototype.setPointerCapture) {
69
+ HTMLElement.prototype.setPointerCapture = function () {}
70
+ }
71
+ if (!HTMLElement.prototype.releasePointerCapture) {
72
+ HTMLElement.prototype.releasePointerCapture = function () {}
73
+ }
74
+ })
75
+
76
+ afterEach(() => {
77
+ while (hosts.length) hosts.pop()!.remove()
78
+ document.body.innerHTML = ''
79
+ document.body.style.cursor = ''
80
+ })
81
+
82
+ // ---------------------------------------------------------------------------
83
+ // decorate()
84
+ // ---------------------------------------------------------------------------
85
+
86
+ describe('rowResize - strip injection', () => {
87
+ it('injects a resize strip into every gutter cell', () => {
88
+ const { host, rows } = buildGrid([{}, {}, {}])
89
+ track(host)
90
+ const action = rowResize(host, { onResize: vi.fn() })
91
+ for (const tr of rows) {
92
+ const strip = stripOf(tr)
93
+ expect(strip).not.toBeNull()
94
+ expect(strip!.getAttribute('role')).toBe('separator')
95
+ expect(strip!.getAttribute('aria-orientation')).toBe('horizontal')
96
+ expect(strip!.getAttribute('aria-label')).toBe('Resize row')
97
+ expect(strip!.style.cursor).toBe('row-resize')
98
+ }
99
+ // Gutter cells become a positioning context with visible overflow.
100
+ expect(gutterOf(rows[0]).style.position).toBe('relative')
101
+ expect(gutterOf(rows[0]).style.overflow).toBe('visible')
102
+ action.destroy()
103
+ })
104
+
105
+ it('does not double-inject a strip on re-decorate', async () => {
106
+ const { host, rows } = buildGrid([{}])
107
+ track(host)
108
+ const action = rowResize(host, { onResize: vi.fn() })
109
+ expect(rows[0].querySelectorAll(`.${STRIP_CLASS}`).length).toBe(1)
110
+ // Trigger the MutationObserver by appending an unrelated node.
111
+ gutterOf(rows[0]).appendChild(document.createElement('span'))
112
+ await Promise.resolve()
113
+ await new Promise((r) => setTimeout(r, 0))
114
+ expect(rows[0].querySelectorAll(`.${STRIP_CLASS}`).length).toBe(1)
115
+ action.destroy()
116
+ })
117
+
118
+ it('skips header rows, spacer rows, and rows without a gutter cell', () => {
119
+ const { host, rows } = buildGrid([
120
+ { rowClasses: ['sv-grid-header-row'] },
121
+ { rowClasses: ['sv-grid-row-spacer'] },
122
+ { gutter: false },
123
+ {}, // normal row gets a strip
124
+ ])
125
+ track(host)
126
+ const action = rowResize(host, { onResize: vi.fn() })
127
+ expect(stripOf(rows[0])).toBeNull()
128
+ expect(stripOf(rows[1])).toBeNull()
129
+ expect(stripOf(rows[2])).toBeNull()
130
+ expect(stripOf(rows[3])).not.toBeNull()
131
+ action.destroy()
132
+ })
133
+
134
+ it('does not override an already-positioned gutter cell', () => {
135
+ const { host, rows } = buildGrid([{}])
136
+ track(host)
137
+ gutterOf(rows[0]).style.position = 'sticky'
138
+ const action = rowResize(host, { onResize: vi.fn() })
139
+ expect(gutterOf(rows[0]).style.position).toBe('sticky')
140
+ action.destroy()
141
+ })
142
+
143
+ it('injects no strips when created disabled', () => {
144
+ const { host, rows } = buildGrid([{}, {}])
145
+ track(host)
146
+ const action = rowResize(host, { onResize: vi.fn(), disabled: true })
147
+ expect(stripOf(rows[0])).toBeNull()
148
+ expect(stripOf(rows[1])).toBeNull()
149
+ action.destroy()
150
+ })
151
+ })
152
+
153
+ // ---------------------------------------------------------------------------
154
+ // Drag lifecycle
155
+ // ---------------------------------------------------------------------------
156
+
157
+ describe('rowResize - drag', () => {
158
+ it('reports the new height on pointerup and fires move callbacks', () => {
159
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
160
+ track(host)
161
+ mockRowHeight(rows[0], 32)
162
+ const onResize = vi.fn()
163
+ const onResizeMove = vi.fn()
164
+ const action = rowResize(host, { onResize, onResizeMove })
165
+ const strip = stripOf(rows[0])!
166
+
167
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
168
+ expect(strip.classList.contains('is-resizing')).toBe(true)
169
+ expect(document.body.style.cursor).toBe('row-resize')
170
+
171
+ // Drag down 20px -> 52.
172
+ window.dispatchEvent(pointer('pointermove', { clientY: 120 }))
173
+ expect(onResizeMove).toHaveBeenLastCalledWith(0, 52)
174
+ expect(rows[0].style.height).toBe('52px')
175
+
176
+ window.dispatchEvent(pointer('pointerup', { clientY: 120 }))
177
+ expect(onResize).toHaveBeenCalledWith(0, 52)
178
+ expect(strip.classList.contains('is-resizing')).toBe(false)
179
+ expect(document.body.style.cursor).toBe('')
180
+ action.destroy()
181
+ })
182
+
183
+ it('clamps the height to the min bound', () => {
184
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
185
+ track(host)
186
+ mockRowHeight(rows[0], 32)
187
+ const onResize = vi.fn()
188
+ const action = rowResize(host, { onResize, min: 24 })
189
+ const strip = stripOf(rows[0])!
190
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
191
+ // Drag far up -> would be negative, clamps to 24.
192
+ window.dispatchEvent(pointer('pointerup', { clientY: 0 }))
193
+ expect(onResize).toHaveBeenCalledWith(0, 24)
194
+ action.destroy()
195
+ })
196
+
197
+ it('clamps the height to the max bound', () => {
198
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
199
+ track(host)
200
+ mockRowHeight(rows[0], 32)
201
+ const onResize = vi.fn()
202
+ const action = rowResize(host, { onResize, max: 50 })
203
+ const strip = stripOf(rows[0])!
204
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
205
+ window.dispatchEvent(pointer('pointerup', { clientY: 1000 }))
206
+ expect(onResize).toHaveBeenCalledWith(0, 50)
207
+ action.destroy()
208
+ })
209
+
210
+ it('works without an onResizeMove callback (optional)', () => {
211
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
212
+ track(host)
213
+ mockRowHeight(rows[0], 30)
214
+ const onResize = vi.fn()
215
+ const action = rowResize(host, { onResize })
216
+ const strip = stripOf(rows[0])!
217
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 50 }))
218
+ expect(() => window.dispatchEvent(pointer('pointermove', { clientY: 60 }))).not.toThrow()
219
+ window.dispatchEvent(pointer('pointerup', { clientY: 60 }))
220
+ expect(onResize).toHaveBeenCalledWith(0, 40)
221
+ action.destroy()
222
+ })
223
+
224
+ it('ignores pointermove / pointerup when no drag is active', () => {
225
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
226
+ track(host)
227
+ const onResize = vi.fn()
228
+ const onResizeMove = vi.fn()
229
+ const action = rowResize(host, { onResize, onResizeMove })
230
+ // No pointerdown first - listeners were only attached on down, so these
231
+ // shouldn't do anything even if dispatched.
232
+ window.dispatchEvent(pointer('pointermove', { clientY: 10 }))
233
+ window.dispatchEvent(pointer('pointerup', { clientY: 10 }))
234
+ expect(onResize).not.toHaveBeenCalled()
235
+ expect(onResizeMove).not.toHaveBeenCalled()
236
+ action.destroy()
237
+ })
238
+ })
239
+
240
+ // ---------------------------------------------------------------------------
241
+ // pointerdown guards
242
+ // ---------------------------------------------------------------------------
243
+
244
+ describe('rowResize - pointerdown guards', () => {
245
+ it('ignores pointerdown when disabled', () => {
246
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
247
+ track(host)
248
+ mockRowHeight(rows[0], 32)
249
+ const onResize = vi.fn()
250
+ // Start enabled so a strip exists, then disable via update.
251
+ const action = rowResize(host, { onResize })
252
+ const strip = stripOf(rows[0])!
253
+ action.update({ onResize, disabled: true })
254
+ // update removes strips, but dispatch on the (now-detached) strip anyway:
255
+ // even if it were still in the DOM, current.disabled short-circuits.
256
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
257
+ window.dispatchEvent(pointer('pointerup', { clientY: 50 }))
258
+ expect(onResize).not.toHaveBeenCalled()
259
+ action.destroy()
260
+ })
261
+
262
+ it('ignores pointerdown on a non-strip target', () => {
263
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
264
+ track(host)
265
+ const onResize = vi.fn()
266
+ const action = rowResize(host, { onResize })
267
+ gutterOf(rows[0]).dispatchEvent(pointer('pointerdown', { clientY: 10 }))
268
+ expect(document.body.style.cursor).toBe('')
269
+ action.destroy()
270
+ })
271
+
272
+ it('ignores a strip whose row has a non-finite row index', () => {
273
+ const { host, rows } = buildGrid([{}])
274
+ track(host)
275
+ // Remove the data-svgrid-row marker so rowIndexOf returns NaN.
276
+ rows[0].querySelector('[data-svgrid-row]')!.removeAttribute('data-svgrid-row')
277
+ const onResize = vi.fn()
278
+ const action = rowResize(host, { onResize })
279
+ const strip = stripOf(rows[0])!
280
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
281
+ expect(document.body.style.cursor).toBe('')
282
+ expect(strip.classList.contains('is-resizing')).toBe(false)
283
+ action.destroy()
284
+ })
285
+
286
+ it('ignores a strip not inside a tr.sv-grid-row', () => {
287
+ const host = track(document.createElement('div'))
288
+ document.body.appendChild(host)
289
+ // A loose strip with no matching ancestor row.
290
+ const strip = document.createElement('div')
291
+ strip.className = STRIP_CLASS
292
+ host.appendChild(strip)
293
+ const onResize = vi.fn()
294
+ const action = rowResize(host, { onResize })
295
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
296
+ expect(document.body.style.cursor).toBe('')
297
+ action.destroy()
298
+ })
299
+ })
300
+
301
+ // ---------------------------------------------------------------------------
302
+ // update() + destroy()
303
+ // ---------------------------------------------------------------------------
304
+
305
+ describe('rowResize - update + destroy', () => {
306
+ it('removes strips when toggled to disabled and restores when re-enabled', () => {
307
+ const { host, rows } = buildGrid([{}, {}])
308
+ track(host)
309
+ const onResize = vi.fn()
310
+ const action = rowResize(host, { onResize, disabled: false })
311
+ expect(stripOf(rows[0])).not.toBeNull()
312
+
313
+ action.update({ onResize, disabled: true })
314
+ expect(stripOf(rows[0])).toBeNull()
315
+ expect(stripOf(rows[1])).toBeNull()
316
+
317
+ action.update({ onResize, disabled: false })
318
+ expect(stripOf(rows[0])).not.toBeNull()
319
+ action.destroy()
320
+ })
321
+
322
+ it('does not repaint when disabled flag is unchanged on update', () => {
323
+ const { host, rows } = buildGrid([{}])
324
+ track(host)
325
+ const action = rowResize(host, { onResize: vi.fn() })
326
+ const firstStrip = stripOf(rows[0])
327
+ // Same disabled value -> decorate is NOT called, strip stays the same node.
328
+ action.update({ onResize: vi.fn() })
329
+ expect(stripOf(rows[0])).toBe(firstStrip)
330
+ action.destroy()
331
+ })
332
+
333
+ it('destroy removes all strips and detaches listeners', () => {
334
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
335
+ track(host)
336
+ mockRowHeight(rows[0], 32)
337
+ const onResize = vi.fn()
338
+ const action = rowResize(host, { onResize })
339
+ expect(stripOf(rows[0])).not.toBeNull()
340
+
341
+ action.destroy()
342
+ expect(stripOf(rows[0])).toBeNull()
343
+
344
+ // After destroy the pointerdown listener is gone; re-create a strip
345
+ // manually and confirm no drag starts.
346
+ const strip = document.createElement('div')
347
+ strip.className = STRIP_CLASS
348
+ gutterOf(rows[0]).appendChild(strip)
349
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
350
+ expect(document.body.style.cursor).toBe('')
351
+ })
352
+
353
+ it('uses default min=20 / max=320 when bounds are omitted', () => {
354
+ const { host, rows } = buildGrid([{ rowIndex: 0 }])
355
+ track(host)
356
+ mockRowHeight(rows[0], 100)
357
+ const onResize = vi.fn()
358
+ const action = rowResize(host, { onResize })
359
+ const strip = stripOf(rows[0])!
360
+ strip.dispatchEvent(pointer('pointerdown', { clientY: 0 }))
361
+ // Drag way past max -> clamps to 320.
362
+ window.dispatchEvent(pointer('pointermove', { clientY: 10000 }))
363
+ expect(rows[0].style.height).toBe('320px')
364
+ // Drag way below min -> clamps to 20.
365
+ window.dispatchEvent(pointer('pointerup', { clientY: -10000 }))
366
+ expect(onResize).toHaveBeenCalledWith(0, 20)
367
+ action.destroy()
368
+ })
369
+ })
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Row-resize Svelte action - drag the bottom edge of a row to grow /
3
+ * shrink it. Pair with a function-valued `rowHeight` prop so the grid
4
+ * reads per-row heights from a reactive store.
5
+ *
6
+ * The action is OFF until a consumer attaches it AND a row-header
7
+ * gutter cell exists in each row. A row-header cell is any TD with
8
+ * the `sv-row-gutter` class (typically the one rendering the row
9
+ * number 1, 2, 3, ...). Demos opt in by setting
10
+ * `cellClass: 'sv-row-gutter'` on the gutter column.
11
+ *
12
+ * <script>
13
+ * let heights = $state<Record<number, number>>({})
14
+ * let resizeOn = $state(false)
15
+ * const rowHeight = (i: number) => heights[i] ?? 32
16
+ * function onResize(i: number, h: number) { heights = { ...heights, [i]: h } }
17
+ * </script>
18
+ *
19
+ * <div use:rowResize={{ onResize, disabled: !resizeOn }}>
20
+ * <SvGrid columns={[{ field:'rn', cellClass:'sv-row-gutter', ... }, ...]}
21
+ * rowHeight={rowHeight} ... />
22
+ * </div>
23
+ *
24
+ * Visual: the strip sits along the bottom edge of the row-header
25
+ * cell, 5px tall, transparent by default, accent-tinted on hover -
26
+ * matching the column resize handle's look. Cursor is `row-resize`.
27
+ */
28
+
29
+ const STRIP_CLASS = 'sv-grid-row-resize-handle'
30
+
31
+ export type RowResizeOptions = {
32
+ /** Called when the user releases the drag with the final height. */
33
+ onResize: (rowIndex: number, height: number) => void
34
+ /** Optional callback fired during the drag (every pointermove). */
35
+ onResizeMove?: (rowIndex: number, height: number) => void
36
+ /** Min row height in px. Default 20. */
37
+ min?: number
38
+ /** Max row height in px. Default 320. */
39
+ max?: number
40
+ /** When true, the action removes its strips and ignores events. */
41
+ disabled?: boolean
42
+ }
43
+
44
+ type ActiveDrag = {
45
+ rowIndex: number
46
+ startY: number
47
+ startHeight: number
48
+ trEl: HTMLTableRowElement
49
+ strip: HTMLElement
50
+ }
51
+
52
+ export function rowResize(node: HTMLElement, opts: RowResizeOptions) {
53
+ let current = opts
54
+ let drag: ActiveDrag | null = null
55
+
56
+ function rowIndexOf(tr: HTMLTableRowElement): number {
57
+ const dataRow = tr.querySelector<HTMLElement>('[data-svgrid-row]')
58
+ const val = dataRow?.dataset.svgridRow
59
+ return val != null ? Number(val) : NaN
60
+ }
61
+
62
+ function onPointerMove(e: PointerEvent) {
63
+ if (!drag) return
64
+ const min = current.min ?? 20
65
+ const max = current.max ?? 320
66
+ const next = Math.max(min, Math.min(max, drag.startHeight + (e.clientY - drag.startY)))
67
+ drag.trEl.style.height = `${Math.round(next)}px`
68
+ current.onResizeMove?.(drag.rowIndex, Math.round(next))
69
+ }
70
+ function onPointerUp(e: PointerEvent) {
71
+ if (!drag) return
72
+ const min = current.min ?? 20
73
+ const max = current.max ?? 320
74
+ const next = Math.max(min, Math.min(max, drag.startHeight + (e.clientY - drag.startY)))
75
+ current.onResize(drag.rowIndex, Math.round(next))
76
+ drag.strip.classList.remove('is-resizing')
77
+ try { drag.trEl.releasePointerCapture(e.pointerId) } catch { /* ignore */ }
78
+ drag = null
79
+ window.removeEventListener('pointermove', onPointerMove)
80
+ window.removeEventListener('pointerup', onPointerUp)
81
+ document.body.style.cursor = ''
82
+ }
83
+
84
+ function onPointerDown(e: PointerEvent) {
85
+ if (current.disabled) return
86
+ const t = e.target as HTMLElement | null
87
+ if (!t?.classList.contains(STRIP_CLASS)) return
88
+ const tr = t.closest<HTMLTableRowElement>('tr.sv-grid-row')
89
+ if (!tr) return
90
+ const rowIndex = rowIndexOf(tr)
91
+ if (!Number.isFinite(rowIndex)) return
92
+ e.preventDefault()
93
+ e.stopPropagation()
94
+ t.classList.add('is-resizing')
95
+ drag = {
96
+ rowIndex,
97
+ startY: e.clientY,
98
+ startHeight: tr.getBoundingClientRect().height,
99
+ trEl: tr,
100
+ strip: t,
101
+ }
102
+ try { tr.setPointerCapture(e.pointerId) } catch { /* ignore */ }
103
+ document.body.style.cursor = 'row-resize'
104
+ window.addEventListener('pointermove', onPointerMove)
105
+ window.addEventListener('pointerup', onPointerUp)
106
+ }
107
+
108
+ /** Inject one strip into every row's gutter cell. When disabled or
109
+ * when a row lacks a `.sv-row-gutter` cell, no strip is added. */
110
+ function decorate() {
111
+ if (current.disabled) {
112
+ removeAllStrips()
113
+ return
114
+ }
115
+ const rows = node.querySelectorAll<HTMLTableRowElement>('tr.sv-grid-row')
116
+ for (const tr of rows) {
117
+ if (tr.classList.contains('sv-grid-header-row')) continue
118
+ if (tr.classList.contains('sv-grid-row-spacer')) continue
119
+ const gutter = tr.querySelector<HTMLTableCellElement>(
120
+ '.sv-grid-cell.sv-row-gutter',
121
+ )
122
+ if (!gutter) continue
123
+ if (gutter.querySelector(`:scope > .${STRIP_CLASS}`)) continue
124
+ // Anchor the strip to the gutter cell + allow it to overflow
125
+ // downward into the row gap so the hit-area straddles the
126
+ // row boundary the way Excel does.
127
+ if (getComputedStyle(gutter).position === 'static') gutter.style.position = 'relative'
128
+ gutter.style.overflow = 'visible'
129
+ const strip = document.createElement('div')
130
+ strip.className = STRIP_CLASS
131
+ strip.setAttribute('role', 'separator')
132
+ strip.setAttribute('aria-orientation', 'horizontal')
133
+ strip.setAttribute('aria-label', 'Resize row')
134
+ // Inline the geometry + pointer-events so the strip works even
135
+ // before SvGrid.css is parsed; the hover tint still comes from
136
+ // the stylesheet. We keep the strip fully inside the gutter
137
+ // cell (no negative offset) to avoid stacking-context surprises
138
+ // with pinned / virtualised neighbour cells.
139
+ // 5px tall + sitting flush with the row bottom matches the
140
+ // column resize handle's 5px width.
141
+ strip.style.cssText =
142
+ 'position:absolute;left:0;right:0;bottom:0;height:5px;' +
143
+ 'cursor:row-resize;user-select:none;z-index:60;pointer-events:auto;'
144
+ gutter.appendChild(strip)
145
+ }
146
+ }
147
+ function removeAllStrips() {
148
+ node.querySelectorAll(`.${STRIP_CLASS}`).forEach((el) => el.remove())
149
+ }
150
+
151
+ node.addEventListener('pointerdown', onPointerDown, { capture: true })
152
+ const observer = new MutationObserver(() => decorate())
153
+ observer.observe(node, { childList: true, subtree: true })
154
+ decorate()
155
+
156
+ return {
157
+ update(next: RowResizeOptions) {
158
+ const wasDisabled = current.disabled
159
+ current = next
160
+ // Toggle: when going from enabled <-> disabled, repaint immediately.
161
+ if (wasDisabled !== current.disabled) decorate()
162
+ },
163
+ destroy() {
164
+ node.removeEventListener('pointerdown', onPointerDown, { capture: true })
165
+ window.removeEventListener('pointermove', onPointerMove)
166
+ window.removeEventListener('pointerup', onPointerUp)
167
+ observer.disconnect()
168
+ removeAllStrips()
169
+ },
170
+ }
171
+ }