@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,330 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { createScrollSync } from './scroll-sync'
3
+
4
+ // ---------------------------------------------------------------------------
5
+ // Fake controller `ctx` for the scroll-sync handlers. As with the other
6
+ // controller-slice tests, the handlers read/write everything through `ctx`,
7
+ // so a hand-rolled stub drives every branch. Where the handler reads real DOM
8
+ // geometry (tooltip placement, scroll offsets) we build genuine elements and
9
+ // stub getBoundingClientRect / scroll metrics.
10
+ // ---------------------------------------------------------------------------
11
+
12
+ function makeCtx(overrides: Record<string, unknown> = {}) {
13
+ const ctx: any = {
14
+ tooltip: null,
15
+ tooltipTimer: null,
16
+ scrollSyncRaf: null,
17
+ scrollVersion: 0,
18
+ pendingScrollTop: 0,
19
+ pendingScrollLeft: 0,
20
+ rowVirtualizationEnabled: false,
21
+ columnVirtualizationEnabled: false,
22
+ scrollBottomArmed: true,
23
+ columnMenuFor: null,
24
+ operatorMenuFor: null,
25
+ closeMenus: vi.fn(),
26
+ domToLogicalRowOffset: (n: number) => n,
27
+ virtualizer: { setScrollOffset: vi.fn() },
28
+ columnVirtualizer: { setHorizontalOffset: vi.fn() },
29
+ props: {},
30
+ ...overrides,
31
+ }
32
+ return ctx
33
+ }
34
+
35
+ const syncFor = (overrides?: Record<string, unknown>) => {
36
+ const ctx = makeCtx(overrides)
37
+ return { ctx, ss: createScrollSync(ctx) }
38
+ }
39
+
40
+ describe('showTooltipFor / hideTooltip', () => {
41
+ beforeEach(() => {
42
+ vi.useFakeTimers()
43
+ })
44
+ afterEach(() => {
45
+ vi.useRealTimers()
46
+ })
47
+
48
+ function anchorEl(rect: Partial<DOMRect>) {
49
+ const el = document.createElement('div')
50
+ el.getBoundingClientRect = () =>
51
+ ({ left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0, x: 0, y: 0, ...rect }) as DOMRect
52
+ return el
53
+ }
54
+
55
+ it('does nothing when there is no text', () => {
56
+ const { ctx, ss } = syncFor()
57
+ ss.showTooltipFor(anchorEl({}), '')
58
+ expect(ctx.tooltipTimer).toBeNull()
59
+ vi.runAllTimers()
60
+ expect(ctx.tooltip).toBeNull()
61
+ })
62
+
63
+ it('schedules a tooltip after the debounce, positioned below the anchor', () => {
64
+ const { ctx, ss } = syncFor()
65
+ // Anchor near the top of a tall viewport -> tooltip appears below.
66
+ vi.stubGlobal('innerWidth', 1000)
67
+ vi.stubGlobal('innerHeight', 800)
68
+ const el = anchorEl({ left: 100, right: 140, top: 50, bottom: 70 })
69
+ ss.showTooltipFor(el, 'hello')
70
+ expect(ctx.tooltipTimer).not.toBeNull()
71
+ expect(ctx.tooltip).toBeNull() // not yet flushed
72
+ vi.advanceTimersByTime(250)
73
+ expect(ctx.tooltip).toEqual({
74
+ text: 'hello',
75
+ x: 106, // left + 6, within clamp range
76
+ y: 76, // bottom + 6 since "below"
77
+ below: true,
78
+ })
79
+ })
80
+
81
+ it('flips above the anchor when there is no room below', () => {
82
+ const { ctx, ss } = syncFor()
83
+ vi.stubGlobal('innerWidth', 1000)
84
+ vi.stubGlobal('innerHeight', 200)
85
+ // bottom + 64 is NOT < 200, so the tooltip renders above.
86
+ const el = anchorEl({ left: 100, right: 140, top: 150, bottom: 170 })
87
+ ss.showTooltipFor(el, 'world')
88
+ vi.advanceTimersByTime(250)
89
+ expect(ctx.tooltip.below).toBe(false)
90
+ expect(ctx.tooltip.y).toBe(144) // top - 6
91
+ })
92
+
93
+ it('clamps x so a wide tooltip stays inside the viewport', () => {
94
+ const { ctx, ss } = syncFor()
95
+ vi.stubGlobal('innerWidth', 300)
96
+ vi.stubGlobal('innerHeight', 800)
97
+ const el = anchorEl({ left: 280, right: 300, top: 10, bottom: 30 })
98
+ ss.showTooltipFor(el, 'edge')
99
+ vi.advanceTimersByTime(250)
100
+ // x clamped to vw - 290 = 10.
101
+ expect(ctx.tooltip.x).toBe(10)
102
+ })
103
+
104
+ it('clears a pending timer before scheduling a new tooltip', () => {
105
+ const { ctx, ss } = syncFor()
106
+ const clearSpy = vi.spyOn(window, 'clearTimeout')
107
+ const el = anchorEl({ left: 10, right: 30, top: 10, bottom: 30 })
108
+ ss.showTooltipFor(el, 'first')
109
+ ss.showTooltipFor(el, 'second')
110
+ expect(clearSpy).toHaveBeenCalled()
111
+ clearSpy.mockRestore()
112
+ })
113
+
114
+ it('hideTooltip clears the pending timer and the tooltip', () => {
115
+ const { ctx, ss } = syncFor()
116
+ const el = anchorEl({ left: 10, right: 30, top: 10, bottom: 30 })
117
+ ss.showTooltipFor(el, 'gone')
118
+ ctx.tooltip = { text: 'x', x: 0, y: 0, below: true }
119
+ ss.hideTooltip()
120
+ expect(ctx.tooltipTimer).toBeNull()
121
+ expect(ctx.tooltip).toBeNull()
122
+ })
123
+
124
+ it('hideTooltip is safe when no timer is pending', () => {
125
+ const { ctx, ss } = syncFor()
126
+ ctx.tooltip = { text: 'x', x: 0, y: 0, below: true }
127
+ ss.hideTooltip()
128
+ expect(ctx.tooltip).toBeNull()
129
+ })
130
+ })
131
+
132
+ describe('scheduleScrollSync / flushScheduledScrollSync', () => {
133
+ let rafCb: FrameRequestCallback | null = null
134
+ let rafCalls = 0
135
+ beforeEach(() => {
136
+ rafCb = null
137
+ rafCalls = 0
138
+ vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {
139
+ rafCb = cb
140
+ rafCalls += 1
141
+ return 42
142
+ })
143
+ })
144
+ afterEach(() => {
145
+ vi.unstubAllGlobals()
146
+ })
147
+
148
+ it('records the pending offsets and schedules a single rAF', () => {
149
+ const { ctx, ss } = syncFor()
150
+ ss.scheduleScrollSync(120, 45)
151
+ expect(ctx.pendingScrollTop).toBe(120)
152
+ expect(ctx.pendingScrollLeft).toBe(45)
153
+ expect(ctx.scrollSyncRaf).toBe(42)
154
+ expect(rafCalls).toBe(1)
155
+ })
156
+
157
+ it('coalesces back-to-back calls into one frame (latest position wins)', () => {
158
+ const { ctx, ss } = syncFor()
159
+ ss.scheduleScrollSync(10, 0)
160
+ ss.scheduleScrollSync(200, 5) // raf already pending, must not re-schedule
161
+ expect(rafCalls).toBe(1)
162
+ expect(ctx.pendingScrollTop).toBe(200)
163
+ expect(ctx.pendingScrollLeft).toBe(5)
164
+ })
165
+
166
+ it('flush bumps scrollVersion and clears the raf handle', () => {
167
+ const { ctx, ss } = syncFor()
168
+ ss.scheduleScrollSync(50, 50)
169
+ rafCb?.(0)
170
+ expect(ctx.scrollVersion).toBe(1)
171
+ expect(ctx.scrollSyncRaf).toBeNull()
172
+ })
173
+
174
+ it('flush pushes the row offset through domToLogicalRowOffset when row virtualization is on', () => {
175
+ const setScrollOffset = vi.fn()
176
+ const { ss } = syncFor({
177
+ rowVirtualizationEnabled: true,
178
+ domToLogicalRowOffset: (n: number) => n * 2,
179
+ virtualizer: { setScrollOffset },
180
+ })
181
+ ss.scheduleScrollSync(30, 0)
182
+ rafCb?.(0)
183
+ expect(setScrollOffset).toHaveBeenCalledWith(60)
184
+ })
185
+
186
+ it('flush pushes the horizontal offset when column virtualization is on', () => {
187
+ const setHorizontalOffset = vi.fn()
188
+ const { ss } = syncFor({
189
+ columnVirtualizationEnabled: true,
190
+ columnVirtualizer: { setHorizontalOffset },
191
+ })
192
+ ss.scheduleScrollSync(0, 88)
193
+ rafCb?.(0)
194
+ expect(setHorizontalOffset).toHaveBeenCalledWith(88)
195
+ })
196
+
197
+ it('flush skips both virtualizers when neither is enabled', () => {
198
+ const { ctx, ss } = syncFor()
199
+ ss.scheduleScrollSync(5, 5)
200
+ rafCb?.(0)
201
+ expect(ctx.virtualizer.setScrollOffset).not.toHaveBeenCalled()
202
+ expect(ctx.columnVirtualizer.setHorizontalOffset).not.toHaveBeenCalled()
203
+ })
204
+ })
205
+
206
+ describe('onBodyScroll', () => {
207
+ let rafCb: FrameRequestCallback | null = null
208
+ beforeEach(() => {
209
+ rafCb = null
210
+ vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {
211
+ rafCb = cb
212
+ return 7
213
+ })
214
+ })
215
+ afterEach(() => {
216
+ vi.unstubAllGlobals()
217
+ })
218
+
219
+ function scrollEvent(container: HTMLElement | null) {
220
+ return { currentTarget: container } as unknown as Event
221
+ }
222
+
223
+ function scroller(props: Partial<{
224
+ scrollTop: number
225
+ scrollLeft: number
226
+ scrollHeight: number
227
+ clientHeight: number
228
+ }>) {
229
+ const el = document.createElement('div')
230
+ Object.defineProperty(el, 'scrollTop', { value: props.scrollTop ?? 0, configurable: true })
231
+ Object.defineProperty(el, 'scrollLeft', { value: props.scrollLeft ?? 0, configurable: true })
232
+ Object.defineProperty(el, 'scrollHeight', { value: props.scrollHeight ?? 0, configurable: true })
233
+ Object.defineProperty(el, 'clientHeight', { value: props.clientHeight ?? 0, configurable: true })
234
+ return el
235
+ }
236
+
237
+ it('does nothing when there is no current target', () => {
238
+ const { ctx, ss } = syncFor()
239
+ ss.onBodyScroll(scrollEvent(null))
240
+ expect(ctx.pendingScrollTop).toBe(0)
241
+ })
242
+
243
+ it('records the container scroll offsets via scheduleScrollSync', () => {
244
+ const { ctx, ss } = syncFor()
245
+ const el = scroller({ scrollTop: 90, scrollLeft: 12 })
246
+ ss.onBodyScroll(scrollEvent(el))
247
+ expect(ctx.pendingScrollTop).toBe(90)
248
+ expect(ctx.pendingScrollLeft).toBe(12)
249
+ expect(ctx.scrollSyncRaf).toBe(7)
250
+ })
251
+
252
+ it('closes open column/operator menus on scroll', () => {
253
+ const closeMenus = vi.fn()
254
+ const { ss } = syncFor({ columnMenuFor: 'x', closeMenus })
255
+ ss.onBodyScroll(scrollEvent(scroller({})))
256
+ expect(closeMenus).toHaveBeenCalled()
257
+ })
258
+
259
+ it('closes menus when an operator menu is open', () => {
260
+ const closeMenus = vi.fn()
261
+ const { ss } = syncFor({ operatorMenuFor: 'op', closeMenus })
262
+ ss.onBodyScroll(scrollEvent(scroller({})))
263
+ expect(closeMenus).toHaveBeenCalled()
264
+ })
265
+
266
+ it('does not close menus when none are open', () => {
267
+ const closeMenus = vi.fn()
268
+ const { ss } = syncFor({ closeMenus })
269
+ ss.onBodyScroll(scrollEvent(scroller({})))
270
+ expect(closeMenus).not.toHaveBeenCalled()
271
+ })
272
+
273
+ it('fires onScrollBottomReached once when the scroll reaches the bottom', () => {
274
+ const onScrollBottomReached = vi.fn()
275
+ const { ctx, ss } = syncFor({
276
+ props: { onScrollBottomReached },
277
+ scrollBottomArmed: true,
278
+ })
279
+ // scrollTop + clientHeight >= scrollHeight - 32 -> at bottom.
280
+ const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
281
+ ss.onBodyScroll(scrollEvent(el))
282
+ expect(onScrollBottomReached).toHaveBeenCalledWith({
283
+ scrollTop: 600,
284
+ scrollHeight: 1000,
285
+ clientHeight: 400,
286
+ })
287
+ expect(ctx.scrollBottomArmed).toBe(false) // disarmed so it won't refire
288
+ })
289
+
290
+ it('does not refire onScrollBottomReached while still at the bottom (disarmed)', () => {
291
+ const onScrollBottomReached = vi.fn()
292
+ const { ss } = syncFor({
293
+ props: { onScrollBottomReached },
294
+ scrollBottomArmed: false,
295
+ })
296
+ const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
297
+ ss.onBodyScroll(scrollEvent(el))
298
+ expect(onScrollBottomReached).not.toHaveBeenCalled()
299
+ })
300
+
301
+ it('re-arms the trigger after scrolling away from the bottom', () => {
302
+ const onScrollBottomReached = vi.fn()
303
+ const { ctx, ss } = syncFor({
304
+ props: { onScrollBottomReached },
305
+ scrollBottomArmed: false,
306
+ })
307
+ // Not at bottom: scrollTop + clientHeight < scrollHeight - 32.
308
+ const el = scroller({ scrollTop: 100, clientHeight: 400, scrollHeight: 1000 })
309
+ ss.onBodyScroll(scrollEvent(el))
310
+ expect(ctx.scrollBottomArmed).toBe(true)
311
+ expect(onScrollBottomReached).not.toHaveBeenCalled()
312
+ })
313
+
314
+ it('skips the bottom-reached logic entirely when no callback is registered', () => {
315
+ const { ctx, ss } = syncFor({ scrollBottomArmed: true })
316
+ const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
317
+ ss.onBodyScroll(scrollEvent(el))
318
+ // Armed flag is untouched when there is no callback.
319
+ expect(ctx.scrollBottomArmed).toBe(true)
320
+ })
321
+ })
322
+
323
+ describe('createScrollSync wiring', () => {
324
+ it('exposes the documented handler surface', () => {
325
+ const { ss } = syncFor()
326
+ expect(Object.keys(ss).sort()).toEqual(
327
+ ['flushScheduledScrollSync', 'hideTooltip', 'onBodyScroll', 'scheduleScrollSync', 'showTooltipFor'],
328
+ )
329
+ })
330
+ })
@@ -0,0 +1,218 @@
1
+ // scroll-sync handlers extracted from the controller. Imperative event handlers
2
+ // reading/writing controller state via the `ctx` handle; the reactive core
3
+ // ($state/$derived/$effect) stays in the controller.
4
+ import {
5
+ applyExcelFilter,
6
+ normalizeForFilter,
7
+ createColumnVirtualizer,
8
+ createCoreRowModel,
9
+ createExpandedRowModel,
10
+ createFilteredRowModel,
11
+ createGroupedRowModel,
12
+ createPaginatedRowModel,
13
+ createSvelteVirtualizer,
14
+ createSortedRowModel,
15
+ createSvGrid,
16
+ filterFns,
17
+ getGridCellA11yProps,
18
+ getGridCellDomId,
19
+ getGridHeaderA11yProps,
20
+ getGridRootA11yProps,
21
+ getGridRowA11yProps,
22
+ parseEditorValue,
23
+ normalizeEditorOptions,
24
+ sortFns,
25
+ tableFeatures,
26
+ rowSortingFeature,
27
+ columnFilteringFeature,
28
+ columnGroupingFeature,
29
+ type CellContext,
30
+ type EditorContext,
31
+ type CellEditorOption,
32
+ type CellEditorType,
33
+ type CellFormatter,
34
+ type CellFormatConfig,
35
+ type Column,
36
+ type ColumnDef,
37
+ type Row,
38
+ type RowData,
39
+ type SvGridApi,
40
+ type TableFeatures,
41
+ } from "./index";
42
+ import "./sv-grid-scrollbar";
43
+ import type { Snippet } from "svelte";
44
+ import { getKeyboardIntent, getNextActiveCell } from "./keyboard";
45
+ import {
46
+ formatNumericWithConfig,
47
+ getDateFormatter,
48
+ resolveDatePattern,
49
+ } from "./cell-formatting";
50
+ import {
51
+ RenderSnippetConfig,
52
+ RenderComponentConfig,
53
+ } from "./render-component";
54
+ import { buildFillPattern } from "./fill-patterns";
55
+ import { buildSparkline, toSparklineValues } from "./sparkline";
56
+ import {
57
+ resolveCellFormat,
58
+ computeColumnStat,
59
+ formatsNeedingStats,
60
+ type ColumnStat,
61
+ type ResolvedCellFormat,
62
+ } from "./conditional-formatting";
63
+ import SvGridDropdown from "./SvGridDropdown.svelte";
64
+ import type {
65
+ Props,
66
+ SelectionPoint,
67
+ SelectionRange,
68
+ CellEditState,
69
+ FilterOperator,
70
+ FilterOption,
71
+ MenuPosition,
72
+ } from "./SvGrid.types";
73
+ import {
74
+ cfTextStyle,
75
+ fmtStat,
76
+ getCellKey,
77
+ resolveClassList,
78
+ toDateInputValue,
79
+ toDateTimeLocalInputValue,
80
+ getEditableInputValue,
81
+ getEditorInputType,
82
+ toValueArray,
83
+ getOptionLabel,
84
+ getOptionColor,
85
+ colorfulChipStyle,
86
+ getEditorClass,
87
+ asDate,
88
+ clampMenuX,
89
+ cssEscape,
90
+ rawToNumber,
91
+ formatFacetNumber,
92
+ formatFacetDate,
93
+ } from "./SvGrid.helpers";
94
+ import { createKeyboard } from "./keyboard-handlers";
95
+ import { createSummaries } from "./summaries";
96
+ import { createMenus } from "./menus";
97
+ import { createCellRender } from "./cell-render";
98
+ import { createEditing } from "./editing";
99
+ import { createSelection } from "./selection";
100
+ import { createColumns } from "./columns";
101
+ import { createGridApi } from "./build-api";
102
+ import { createClipboard } from "./clipboard";
103
+ import {
104
+ filterOperatorOptions,
105
+ fallbackOperatorOption,
106
+ TEXT_OPERATORS,
107
+ NUMBER_OPERATORS,
108
+ DATE_OPERATORS,
109
+ CHECKBOX_OPERATORS,
110
+ operatorOption,
111
+ operatorsForColumn,
112
+ defaultOperatorFor,
113
+ operatorLabelFor,
114
+ } from "./filter-operators";
115
+ import {
116
+ type FacetBucket,
117
+ isBucketableColumn,
118
+ buildBuckets,
119
+ isInBucket,
120
+ } from "./facet-buckets";
121
+ import {
122
+ getColumnBaseValue,
123
+ isGroupRow,
124
+ toolPanelHeaderLabel,
125
+ formatSummaryNumeric,
126
+ getColumnAlign,
127
+ getPinnedCellValue,
128
+ getColumnAccessorValue,
129
+ columnDefMatchesId,
130
+ } from "./cell-values";
131
+
132
+ export function createScrollSync<
133
+ TFeatures extends TableFeatures = TableFeatures,
134
+ TData extends RowData = RowData,
135
+ >(ctx: any) {
136
+ function showTooltipFor(el: HTMLElement, text: string) {
137
+ if (!text) return
138
+ if (ctx.tooltipTimer) window.clearTimeout(ctx.tooltipTimer)
139
+ ctx.tooltipTimer = window.setTimeout(() => {
140
+ const rect = el.getBoundingClientRect()
141
+ const vw = window.innerWidth
142
+ const vh = window.innerHeight
143
+ const below = rect.bottom + 64 < vh
144
+ // Clamp x so the 280px-wide tooltip doesn't fall off the viewport.
145
+ const x = Math.min(Math.max(rect.left + 6, 10), vw - 290)
146
+ const y = below ? rect.bottom + 6 : rect.top - 6
147
+ ctx.tooltip = { text, x, y, below }
148
+ }, 250)
149
+ }
150
+
151
+ function hideTooltip() {
152
+ if (ctx.tooltipTimer) { window.clearTimeout(ctx.tooltipTimer); ctx.tooltipTimer = null }
153
+ ctx.tooltip = null
154
+ }
155
+
156
+ function flushScheduledScrollSync() {
157
+ ctx.scrollSyncRaf = null;
158
+ ctx.scrollVersion += 1;
159
+ if (ctx.rowVirtualizationEnabled)
160
+ ctx.virtualizer.setScrollOffset(ctx.domToLogicalRowOffset(ctx.pendingScrollTop));
161
+ if (ctx.columnVirtualizationEnabled)
162
+ ctx.columnVirtualizer.setHorizontalOffset(ctx.pendingScrollLeft);
163
+ }
164
+
165
+ /**
166
+ * Sync the virtualizer to the user's scroll position once per frame.
167
+ *
168
+ * The browser's smooth-scroll animation fires a `scroll` event on
169
+ * every frame of the ~250 ms ramp after each wheel notch - so a
170
+ * single wheel notch produces ~16 calls to `onBodyScroll`. Without
171
+ * batching, each one ran a full virtualizer recalc + Svelte re-render
172
+ * synchronously, which (a) made the work pile up on heavy demos and
173
+ * (b) read on screen as "the grid keeps scrolling after the wheel
174
+ * stopped" because each scroll event ticked the rendered window one
175
+ * row at a time over a quarter-second.
176
+ *
177
+ * Batching to a single rAF flush per frame means each animation
178
+ * frame produces ONE virtualizer update at the latest known scroll
179
+ * position. Smooth-scroll still feels smooth (the browser is still
180
+ * animating `scrollTop`), but the grid's per-frame cost is bounded
181
+ * and the visible rows match the current `scrollTop` exactly when
182
+ * the wheel stops - no trailing updates.
183
+ */
184
+ function scheduleScrollSync(scrollTop: number, scrollLeft: number) {
185
+ ctx.pendingScrollTop = scrollTop;
186
+ ctx.pendingScrollLeft = scrollLeft;
187
+ if (ctx.scrollSyncRaf !== null) return;
188
+ ctx.scrollSyncRaf = requestAnimationFrame(flushScheduledScrollSync);
189
+ }
190
+
191
+ function onBodyScroll(event: Event) {
192
+ const container = event.currentTarget as HTMLDivElement | null;
193
+ if (!container) return;
194
+ if (ctx.columnMenuFor || ctx.operatorMenuFor) ctx.closeMenus();
195
+ scheduleScrollSync(container.scrollTop, container.scrollLeft);
196
+ // Mirror horizontal scroll to any aligned grids in the same group.
197
+ if (ctx.props.alignedGridGroup != null) ctx.broadcastAlignedScroll(container.scrollLeft);
198
+
199
+ if (ctx.props.onScrollBottomReached) {
200
+ const { scrollTop, scrollHeight, clientHeight } = container;
201
+ const atBottom = scrollTop + clientHeight >= scrollHeight - 32;
202
+ if (atBottom && ctx.scrollBottomArmed) {
203
+ ctx.scrollBottomArmed = false;
204
+ ctx.props.onScrollBottomReached({ scrollTop, scrollHeight, clientHeight });
205
+ } else if (!atBottom && !ctx.scrollBottomArmed) {
206
+ ctx.scrollBottomArmed = true;
207
+ }
208
+ }
209
+ }
210
+
211
+ return {
212
+ showTooltipFor,
213
+ hideTooltip,
214
+ flushScheduledScrollSync,
215
+ scheduleScrollSync,
216
+ onBodyScroll,
217
+ };
218
+ }
@@ -0,0 +1,61 @@
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
+
8
+ function makeCtx() {
9
+ return {
10
+ enableCellSelectionEffective: true,
11
+ selectionRange: { anchor: null, focus: null },
12
+ selectionRanges: [],
13
+ } as Record<string, any>
14
+ }
15
+
16
+ describe('multiple range selection', () => {
17
+ it('a plain selection replaces any committed ranges', () => {
18
+ const ctx = makeCtx()
19
+ const s = createSelection(ctx)
20
+ s.setSelection(0, 0)
21
+ s.extendSelection(1, 1) // active range 0,0..1,1
22
+ s.setSelection(5, 5, true) // additive: commit prior, start new
23
+ expect(ctx.selectionRanges.length).toBe(1)
24
+ s.setSelection(9, 9, false) // plain: clears committed
25
+ expect(ctx.selectionRanges.length).toBe(0)
26
+ })
27
+
28
+ it('Ctrl+drag commits the prior range and keeps both highlighted', () => {
29
+ const ctx = makeCtx()
30
+ const s = createSelection(ctx)
31
+ s.setSelection(0, 0)
32
+ s.extendSelection(1, 1) // range A: rows 0-1, cols 0-1
33
+ s.setSelection(3, 3, true) // start range B additively
34
+ s.extendSelection(4, 4) // range B: rows 3-4, cols 3-4
35
+
36
+ const rects = s.getSelectionRects()
37
+ expect(rects.length).toBe(2)
38
+ // both ranges report cells as selected
39
+ expect(s.isCellInSelectedRange(0, 0)).toBe(true)
40
+ expect(s.isCellInSelectedRange(1, 1)).toBe(true)
41
+ expect(s.isCellInSelectedRange(4, 4)).toBe(true)
42
+ // a cell in neither is not selected
43
+ expect(s.isCellInSelectedRange(2, 2)).toBe(false)
44
+ })
45
+
46
+ it('getCellRangeEdges outlines each range independently', () => {
47
+ const ctx = makeCtx()
48
+ const s = createSelection(ctx)
49
+ s.setSelection(0, 0)
50
+ s.extendSelection(1, 1)
51
+ s.setSelection(3, 3, true)
52
+ s.extendSelection(4, 4)
53
+
54
+ // top-left corner of range A
55
+ expect(s.getCellRangeEdges(0, 0)).toEqual({ top: true, bottom: false, left: true, right: false })
56
+ // bottom-right corner of range B
57
+ expect(s.getCellRangeEdges(4, 4)).toEqual({ top: false, bottom: true, left: false, right: true })
58
+ // outside every range
59
+ expect(s.getCellRangeEdges(2, 2)).toBeNull()
60
+ })
61
+ })