@svgrid/grid 1.1.0 → 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 (117) hide show
  1. package/dist/GridMenus.svelte +205 -127
  2. package/dist/SvGrid.controller.svelte.d.ts +44 -1
  3. package/dist/SvGrid.controller.svelte.js +223 -38
  4. package/dist/SvGrid.css +2012 -1709
  5. package/dist/SvGrid.helpers.test.d.ts +1 -0
  6. package/dist/SvGrid.helpers.test.js +298 -0
  7. package/dist/SvGrid.svelte +2346 -2043
  8. package/dist/SvGrid.types.d.ts +91 -4
  9. package/dist/aligned-grids.d.ts +6 -0
  10. package/dist/aligned-grids.js +84 -0
  11. package/dist/aligned-grids.test.d.ts +1 -0
  12. package/dist/aligned-grids.test.js +75 -0
  13. package/dist/build-api.coverage.test.d.ts +20 -0
  14. package/dist/build-api.coverage.test.js +505 -0
  15. package/dist/build-api.js +59 -29
  16. package/dist/cell-render.test.d.ts +1 -0
  17. package/dist/cell-render.test.js +338 -0
  18. package/dist/chart-export.test.d.ts +1 -0
  19. package/dist/chart-export.test.js +302 -0
  20. package/dist/chart.coverage.test.d.ts +1 -0
  21. package/dist/chart.coverage.test.js +748 -0
  22. package/dist/clipboard.js +88 -24
  23. package/dist/clipboard.test.d.ts +1 -0
  24. package/dist/clipboard.test.js +700 -0
  25. package/dist/collaboration.coverage.test.d.ts +1 -0
  26. package/dist/collaboration.coverage.test.js +200 -0
  27. package/dist/column-groups.d.ts +19 -0
  28. package/dist/column-groups.js +62 -0
  29. package/dist/column-groups.test.d.ts +1 -0
  30. package/dist/column-groups.test.js +56 -0
  31. package/dist/column-types.d.ts +10 -0
  32. package/dist/column-types.js +63 -0
  33. package/dist/column-types.test.d.ts +1 -0
  34. package/dist/column-types.test.js +62 -0
  35. package/dist/columns.test.d.ts +1 -0
  36. package/dist/columns.test.js +625 -0
  37. package/dist/core.d.ts +85 -0
  38. package/dist/editing.d.ts +7 -0
  39. package/dist/editing.js +191 -5
  40. package/dist/editing.test.d.ts +1 -0
  41. package/dist/editing.test.js +732 -0
  42. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  43. package/dist/editors/cell-editors.coverage.test.js +139 -0
  44. package/dist/facet-buckets.test.d.ts +1 -0
  45. package/dist/facet-buckets.test.js +296 -0
  46. package/dist/filter-operators.test.d.ts +1 -0
  47. package/dist/filter-operators.test.js +135 -0
  48. package/dist/hyperformula-adapter.test.d.ts +1 -0
  49. package/dist/hyperformula-adapter.test.js +205 -0
  50. package/dist/index.d.ts +5 -2
  51. package/dist/index.js +4 -1
  52. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  53. package/dist/keyboard-handlers.coverage.test.js +495 -0
  54. package/dist/keyboard-handlers.js +7 -2
  55. package/dist/menus.js +1 -0
  56. package/dist/menus.test.d.ts +1 -0
  57. package/dist/menus.test.js +560 -0
  58. package/dist/named-views.coverage.test.d.ts +1 -0
  59. package/dist/named-views.coverage.test.js +180 -0
  60. package/dist/row-drag.d.ts +49 -0
  61. package/dist/row-drag.js +221 -0
  62. package/dist/row-drag.test.d.ts +1 -0
  63. package/dist/row-drag.test.js +142 -0
  64. package/dist/row-resize.test.d.ts +1 -0
  65. package/dist/row-resize.test.js +329 -0
  66. package/dist/scroll-sync.js +3 -0
  67. package/dist/scroll-sync.test.d.ts +1 -0
  68. package/dist/scroll-sync.test.js +290 -0
  69. package/dist/selection.d.ts +7 -1
  70. package/dist/selection.js +76 -36
  71. package/dist/selection.multi-range.test.d.ts +1 -0
  72. package/dist/selection.multi-range.test.js +55 -0
  73. package/dist/selection.test.d.ts +1 -0
  74. package/dist/selection.test.js +647 -0
  75. package/dist/server-data-source.coverage.test.d.ts +1 -0
  76. package/dist/server-data-source.coverage.test.js +154 -0
  77. package/dist/spreadsheet.d.ts +30 -0
  78. package/dist/spreadsheet.js +48 -0
  79. package/dist/spreadsheet.test.d.ts +1 -0
  80. package/dist/spreadsheet.test.js +446 -0
  81. package/dist/sv-grid-scrollbar.js +13 -1
  82. package/dist/svgrid-wrapper.types.d.ts +19 -0
  83. package/dist/svgrid.behavior.test.js +20 -0
  84. package/dist/svgrid.interaction.test.js +31 -0
  85. package/dist/svgrid.new-features.wrapper.test.js +34 -2
  86. package/dist/test-setup.js +9 -3
  87. package/dist/virtualization/scroll-scaling.d.ts +17 -0
  88. package/dist/virtualization/scroll-scaling.js +35 -0
  89. package/dist/virtualization/scroll-scaling.test.js +42 -1
  90. package/package.json +2 -1
  91. package/src/GridMenus.svelte +205 -127
  92. package/src/SvGrid.controller.svelte.ts +2352 -2195
  93. package/src/SvGrid.css +2012 -1747
  94. package/src/SvGrid.svelte +2346 -2047
  95. package/src/SvGrid.types.ts +537 -456
  96. package/src/aligned-grids.test.ts +80 -0
  97. package/src/aligned-grids.ts +87 -0
  98. package/src/build-api.ts +683 -663
  99. package/src/clipboard.test.ts +49 -0
  100. package/src/clipboard.ts +51 -23
  101. package/src/column-groups.test.ts +59 -0
  102. package/src/column-groups.ts +80 -0
  103. package/src/column-types.test.ts +68 -0
  104. package/src/column-types.ts +82 -0
  105. package/src/core.ts +78 -0
  106. package/src/editing.ts +669 -513
  107. package/src/index.ts +12 -0
  108. package/src/menus.ts +1 -0
  109. package/src/row-drag.test.ts +168 -0
  110. package/src/row-drag.ts +255 -0
  111. package/src/scroll-sync.ts +2 -0
  112. package/src/selection.multi-range.test.ts +61 -0
  113. package/src/selection.ts +71 -37
  114. package/src/spreadsheet.test.ts +489 -445
  115. package/src/spreadsheet.ts +304 -246
  116. package/src/svgrid-wrapper.types.ts +19 -0
  117. package/src/svgrid.new-features.wrapper.test.ts +2 -2
@@ -0,0 +1,647 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { createSelection } from './selection';
3
+ // ---------------------------------------------------------------------------
4
+ // Fake controller (`ctx`) helpers. The selection factory reads/writes plain
5
+ // state on `ctx` and calls a small set of `ctx.grid.*` methods. We build the
6
+ // minimal surface each tested path touches.
7
+ // ---------------------------------------------------------------------------
8
+ function makeGroupRow(id) {
9
+ // isGroupRow() checks `typeof row.getCanExpand === 'function' && row.getCanExpand()`
10
+ return { id, getCanExpand: () => true, toggleExpanded: vi.fn(), original: {} };
11
+ }
12
+ function makeDataRow(id, original = {}) {
13
+ return {
14
+ id,
15
+ getCanExpand: () => false,
16
+ original,
17
+ getCellValueByColumnId: (colId) => original?.[colId],
18
+ };
19
+ }
20
+ function makeColumn(id, columnDef = {}) {
21
+ return { id, columnDef };
22
+ }
23
+ function makeCtx(overrides = {}) {
24
+ let rowSelection = overrides.rowSelectionState ?? {};
25
+ let activeCell = overrides.activeCell ?? null;
26
+ const setRowSelection = vi.fn((updater) => {
27
+ rowSelection =
28
+ typeof updater === 'function' ? updater(rowSelection) : updater;
29
+ ctx.rowSelectionState = rowSelection;
30
+ });
31
+ const setActiveCell = vi.fn((cell) => {
32
+ activeCell = cell;
33
+ });
34
+ const ctx = {
35
+ rowSelectionState: rowSelection,
36
+ allRows: [],
37
+ allColumns: [],
38
+ headerSelectionState: 'none',
39
+ userHasActivatedCell: false,
40
+ enableCellSelectionEffective: true,
41
+ selectionRange: { anchor: null, focus: null },
42
+ isDraggingSelection: false,
43
+ fillDrag: null,
44
+ activeAtPointerDown: null,
45
+ editingEnabled: false,
46
+ editingCell: undefined,
47
+ scrollContainer: null,
48
+ rowVirtualizationEnabled: false,
49
+ rowScrollScalingActive: false,
50
+ renderedColumnItems: [],
51
+ columnVirtualizer: null,
52
+ theadEl: null,
53
+ headerHeight: 0,
54
+ props: {},
55
+ gridRootEl: null,
56
+ getCellDisplayValue: vi.fn((_r, _c, base) => base),
57
+ toggleBooleanCell: vi.fn(),
58
+ onCellDoubleClick: vi.fn(),
59
+ onFillPointerUp: vi.fn(),
60
+ onFillPointerMove: vi.fn(),
61
+ grid: {
62
+ setRowSelection,
63
+ setActiveCell,
64
+ getState: () => ({ activeCell }),
65
+ },
66
+ ...overrides,
67
+ };
68
+ ctx.rowSelectionState = rowSelection;
69
+ return ctx;
70
+ }
71
+ describe('createSelection - row selection', () => {
72
+ it('isRowSelected reflects rowSelectionState', () => {
73
+ const ctx = makeCtx({ rowSelectionState: { r1: true, r2: false } });
74
+ const sel = createSelection(ctx);
75
+ expect(sel.isRowSelected('r1')).toBe(true);
76
+ expect(sel.isRowSelected('r2')).toBe(false);
77
+ expect(sel.isRowSelected('missing')).toBe(false);
78
+ });
79
+ it('toggleRowSelectionById flips the single row', () => {
80
+ const ctx = makeCtx({ rowSelectionState: { r1: false } });
81
+ const sel = createSelection(ctx);
82
+ sel.toggleRowSelectionById('r1');
83
+ expect(ctx.rowSelectionState.r1).toBe(true);
84
+ sel.toggleRowSelectionById('r1');
85
+ expect(ctx.rowSelectionState.r1).toBe(false);
86
+ });
87
+ it('toggleSelectAllRows selects every non-group row when not all selected', () => {
88
+ const ctx = makeCtx({
89
+ headerSelectionState: 'none',
90
+ allRows: [makeDataRow('r1'), makeGroupRow('g1'), makeDataRow('r2')],
91
+ });
92
+ const sel = createSelection(ctx);
93
+ sel.toggleSelectAllRows();
94
+ expect(ctx.rowSelectionState.r1).toBe(true);
95
+ expect(ctx.rowSelectionState.r2).toBe(true);
96
+ // group row excluded
97
+ expect(ctx.rowSelectionState.g1).toBeUndefined();
98
+ });
99
+ it('toggleSelectAllRows deselects all when header state is "all"', () => {
100
+ const ctx = makeCtx({
101
+ headerSelectionState: 'all',
102
+ rowSelectionState: { r1: true, r2: true },
103
+ allRows: [makeDataRow('r1'), makeDataRow('r2')],
104
+ });
105
+ const sel = createSelection(ctx);
106
+ sel.toggleSelectAllRows();
107
+ expect(ctx.rowSelectionState.r1).toBeUndefined();
108
+ expect(ctx.rowSelectionState.r2).toBeUndefined();
109
+ });
110
+ });
111
+ describe('createSelection - setActiveCell', () => {
112
+ it('sets active cell and flags userHasActivatedCell', () => {
113
+ const ctx = makeCtx({ allColumns: [makeColumn('a'), makeColumn('b')] });
114
+ const sel = createSelection(ctx);
115
+ sel.setActiveCell(2, 1);
116
+ expect(ctx.userHasActivatedCell).toBe(true);
117
+ expect(ctx.grid.setActiveCell).toHaveBeenCalledWith(expect.objectContaining({ rowIndex: 2, colIndex: 1 }));
118
+ });
119
+ it('fires onActiveCellChange with resolved columnId', () => {
120
+ const onActiveCellChange = vi.fn();
121
+ const ctx = makeCtx({
122
+ allColumns: [makeColumn('a'), makeColumn('b')],
123
+ props: { onActiveCellChange },
124
+ });
125
+ const sel = createSelection(ctx);
126
+ sel.setActiveCell(0, 1);
127
+ expect(onActiveCellChange).toHaveBeenCalledWith({
128
+ rowIndex: 0,
129
+ colIndex: 1,
130
+ columnId: 'b',
131
+ });
132
+ });
133
+ it('onActiveCellChange columnId falls back to "" when column missing', () => {
134
+ const onActiveCellChange = vi.fn();
135
+ const ctx = makeCtx({ allColumns: [], props: { onActiveCellChange } });
136
+ const sel = createSelection(ctx);
137
+ sel.setActiveCell(0, 5);
138
+ expect(onActiveCellChange).toHaveBeenCalledWith(expect.objectContaining({ columnId: '' }));
139
+ });
140
+ });
141
+ describe('createSelection - range math', () => {
142
+ it('setSelection sets anchor and focus to the same point', () => {
143
+ const ctx = makeCtx();
144
+ const sel = createSelection(ctx);
145
+ sel.setSelection(3, 4);
146
+ expect(ctx.selectionRange).toEqual({
147
+ anchor: { rowIndex: 3, colIndex: 4 },
148
+ focus: { rowIndex: 3, colIndex: 4 },
149
+ });
150
+ });
151
+ it('setSelection is a no-op when cell selection disabled', () => {
152
+ const ctx = makeCtx({ enableCellSelectionEffective: false });
153
+ const before = ctx.selectionRange;
154
+ const sel = createSelection(ctx);
155
+ sel.setSelection(1, 1);
156
+ expect(ctx.selectionRange).toBe(before);
157
+ });
158
+ it('extendSelection keeps the existing anchor and moves focus', () => {
159
+ const ctx = makeCtx({
160
+ selectionRange: { anchor: { rowIndex: 1, colIndex: 1 }, focus: { rowIndex: 1, colIndex: 1 } },
161
+ });
162
+ const sel = createSelection(ctx);
163
+ sel.extendSelection(4, 5);
164
+ expect(ctx.selectionRange.anchor).toEqual({ rowIndex: 1, colIndex: 1 });
165
+ expect(ctx.selectionRange.focus).toEqual({ rowIndex: 4, colIndex: 5 });
166
+ });
167
+ it('extendSelection uses the new point as anchor when none exists', () => {
168
+ const ctx = makeCtx({ selectionRange: { anchor: null, focus: null } });
169
+ const sel = createSelection(ctx);
170
+ sel.extendSelection(2, 3);
171
+ expect(ctx.selectionRange.anchor).toEqual({ rowIndex: 2, colIndex: 3 });
172
+ expect(ctx.selectionRange.focus).toEqual({ rowIndex: 2, colIndex: 3 });
173
+ });
174
+ it('extendSelection is a no-op when cell selection disabled', () => {
175
+ const ctx = makeCtx({ enableCellSelectionEffective: false });
176
+ const before = ctx.selectionRange;
177
+ const sel = createSelection(ctx);
178
+ sel.extendSelection(1, 1);
179
+ expect(ctx.selectionRange).toBe(before);
180
+ });
181
+ it('isCellInSelectedRange covers normalized ranges (anchor > focus)', () => {
182
+ const ctx = makeCtx({
183
+ selectionRange: { anchor: { rowIndex: 4, colIndex: 4 }, focus: { rowIndex: 1, colIndex: 1 } },
184
+ });
185
+ const sel = createSelection(ctx);
186
+ expect(sel.isCellInSelectedRange(2, 3)).toBe(true);
187
+ expect(sel.isCellInSelectedRange(1, 1)).toBe(true);
188
+ expect(sel.isCellInSelectedRange(4, 4)).toBe(true);
189
+ expect(sel.isCellInSelectedRange(0, 0)).toBe(false);
190
+ expect(sel.isCellInSelectedRange(5, 5)).toBe(false);
191
+ });
192
+ it('isCellInSelectedRange returns false when no range', () => {
193
+ const ctx = makeCtx({ selectionRange: { anchor: null, focus: null } });
194
+ const sel = createSelection(ctx);
195
+ expect(sel.isCellInSelectedRange(0, 0)).toBe(false);
196
+ });
197
+ });
198
+ describe('createSelection - getCellRangeEdges', () => {
199
+ it('returns null outside the range', () => {
200
+ const ctx = makeCtx({
201
+ selectionRange: { anchor: { rowIndex: 1, colIndex: 1 }, focus: { rowIndex: 3, colIndex: 3 } },
202
+ });
203
+ const sel = createSelection(ctx);
204
+ expect(sel.getCellRangeEdges(0, 0)).toBeNull();
205
+ expect(sel.getCellRangeEdges(5, 5)).toBeNull();
206
+ });
207
+ it('returns null when range unset', () => {
208
+ const ctx = makeCtx();
209
+ const sel = createSelection(ctx);
210
+ expect(sel.getCellRangeEdges(0, 0)).toBeNull();
211
+ });
212
+ it('marks all four edges for a single-cell range', () => {
213
+ const ctx = makeCtx({
214
+ selectionRange: { anchor: { rowIndex: 2, colIndex: 2 }, focus: { rowIndex: 2, colIndex: 2 } },
215
+ });
216
+ const sel = createSelection(ctx);
217
+ expect(sel.getCellRangeEdges(2, 2)).toEqual({ top: true, bottom: true, left: true, right: true });
218
+ });
219
+ it('flags only the corner edges for a corner cell of a wider range', () => {
220
+ const ctx = makeCtx({
221
+ selectionRange: { anchor: { rowIndex: 1, colIndex: 1 }, focus: { rowIndex: 3, colIndex: 3 } },
222
+ });
223
+ const sel = createSelection(ctx);
224
+ expect(sel.getCellRangeEdges(1, 1)).toEqual({ top: true, bottom: false, left: true, right: false });
225
+ expect(sel.getCellRangeEdges(2, 2)).toEqual({ top: false, bottom: false, left: false, right: false });
226
+ expect(sel.getCellRangeEdges(3, 3)).toEqual({ top: false, bottom: true, left: false, right: true });
227
+ });
228
+ });
229
+ describe('createSelection - isInFillPreview', () => {
230
+ it('returns false when no fill drag active', () => {
231
+ const ctx = makeCtx({ fillDrag: null });
232
+ const sel = createSelection(ctx);
233
+ expect(sel.isInFillPreview(0, 0)).toBe(false);
234
+ });
235
+ it('returns false for cells outside the preview rectangle', () => {
236
+ const ctx = makeCtx({
237
+ fillDrag: { sourceMinRow: 0, sourceMaxRow: 1, sourceMinCol: 0, sourceMaxCol: 0, targetRow: 3, targetCol: 0 },
238
+ });
239
+ const sel = createSelection(ctx);
240
+ expect(sel.isInFillPreview(5, 0)).toBe(false);
241
+ expect(sel.isInFillPreview(0, 5)).toBe(false);
242
+ });
243
+ it('returns true for cells in the preview but outside the source', () => {
244
+ const ctx = makeCtx({
245
+ fillDrag: { sourceMinRow: 0, sourceMaxRow: 1, sourceMinCol: 0, sourceMaxCol: 0, targetRow: 3, targetCol: 0 },
246
+ });
247
+ const sel = createSelection(ctx);
248
+ expect(sel.isInFillPreview(2, 0)).toBe(true); // below source, within preview
249
+ expect(sel.isInFillPreview(0, 0)).toBe(false); // inside the source range
250
+ });
251
+ });
252
+ describe('createSelection - findColumnById', () => {
253
+ it('finds a column by id or returns undefined', () => {
254
+ const ctx = makeCtx({ allColumns: [makeColumn('a'), makeColumn('b')] });
255
+ const sel = createSelection(ctx);
256
+ expect(sel.findColumnById('b')?.id).toBe('b');
257
+ expect(sel.findColumnById('zzz')).toBeUndefined();
258
+ });
259
+ });
260
+ describe('createSelection - onCellPointerDown', () => {
261
+ function baseCtx(over = {}) {
262
+ return makeCtx({
263
+ allRows: [makeDataRow('r0'), makeDataRow('r1')],
264
+ allColumns: [makeColumn('c0', { editorType: 'text' }), makeColumn('c1', { editorType: 'text' })],
265
+ ...over,
266
+ });
267
+ }
268
+ it('ignores non-left button', () => {
269
+ const ctx = baseCtx();
270
+ const sel = createSelection(ctx);
271
+ sel.onCellPointerDown(0, 0, { button: 2 });
272
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
273
+ });
274
+ it('ignores group rows', () => {
275
+ const ctx = baseCtx({ allRows: [makeGroupRow('g0')] });
276
+ const sel = createSelection(ctx);
277
+ sel.onCellPointerDown(0, 0, { button: 0, shiftKey: false });
278
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
279
+ });
280
+ it('ignores checkbox columns (by editorType)', () => {
281
+ const ctx = baseCtx({
282
+ allColumns: [makeColumn('c0', { editorType: 'checkbox' })],
283
+ });
284
+ const sel = createSelection(ctx);
285
+ sel.onCellPointerDown(0, 0, { button: 0, shiftKey: false });
286
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
287
+ });
288
+ it('ignores boolean-valued cells', () => {
289
+ const ctx = baseCtx({
290
+ getCellDisplayValue: vi.fn(() => true),
291
+ });
292
+ const sel = createSelection(ctx);
293
+ sel.onCellPointerDown(0, 0, { button: 0, shiftKey: false });
294
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
295
+ });
296
+ it('plain click sets active + selection and starts a drag', () => {
297
+ const ctx = baseCtx();
298
+ const sel = createSelection(ctx);
299
+ sel.onCellPointerDown(1, 1, { button: 0, shiftKey: false });
300
+ expect(ctx.isDraggingSelection).toBe(true);
301
+ expect(ctx.selectionRange.anchor).toEqual({ rowIndex: 1, colIndex: 1 });
302
+ expect(ctx.grid.setActiveCell).toHaveBeenCalled();
303
+ expect(ctx.activeAtPointerDown).toBeNull();
304
+ });
305
+ it('shift click extends the existing range without starting a drag', () => {
306
+ const ctx = baseCtx({
307
+ selectionRange: { anchor: { rowIndex: 0, colIndex: 0 }, focus: { rowIndex: 0, colIndex: 0 } },
308
+ activeCell: { rowIndex: 0, colIndex: 0 },
309
+ });
310
+ const sel = createSelection(ctx);
311
+ sel.onCellPointerDown(1, 1, { button: 0, shiftKey: true });
312
+ expect(ctx.isDraggingSelection).toBe(false);
313
+ expect(ctx.selectionRange.anchor).toEqual({ rowIndex: 0, colIndex: 0 });
314
+ expect(ctx.selectionRange.focus).toEqual({ rowIndex: 1, colIndex: 1 });
315
+ expect(ctx.activeAtPointerDown).toEqual({ rowIndex: 0, colIndex: 0 });
316
+ });
317
+ });
318
+ describe('createSelection - onCellPointerEnter / drag', () => {
319
+ it('does nothing when not dragging', () => {
320
+ const ctx = makeCtx({ isDraggingSelection: false, allRows: [makeDataRow('r0')] });
321
+ const sel = createSelection(ctx);
322
+ sel.onCellPointerEnter(0, 0);
323
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
324
+ });
325
+ it('extends selection and moves active cell while dragging', () => {
326
+ const ctx = makeCtx({
327
+ isDraggingSelection: true,
328
+ selectionRange: { anchor: { rowIndex: 0, colIndex: 0 }, focus: { rowIndex: 0, colIndex: 0 } },
329
+ allRows: [makeDataRow('r0'), makeDataRow('r1')],
330
+ allColumns: [makeColumn('c0'), makeColumn('c1')],
331
+ });
332
+ const sel = createSelection(ctx);
333
+ sel.onCellPointerEnter(1, 1);
334
+ expect(ctx.selectionRange.focus).toEqual({ rowIndex: 1, colIndex: 1 });
335
+ expect(ctx.grid.setActiveCell).toHaveBeenCalled();
336
+ });
337
+ it('ignores group rows while dragging', () => {
338
+ const ctx = makeCtx({ isDraggingSelection: true, allRows: [makeGroupRow('g0')] });
339
+ const sel = createSelection(ctx);
340
+ sel.onCellPointerEnter(0, 0);
341
+ expect(ctx.grid.setActiveCell).not.toHaveBeenCalled();
342
+ });
343
+ });
344
+ describe('createSelection - endDragSelection / window move', () => {
345
+ it('endDragSelection clears the drag flag', () => {
346
+ const ctx = makeCtx({ isDraggingSelection: true });
347
+ const sel = createSelection(ctx);
348
+ sel.endDragSelection();
349
+ expect(ctx.isDraggingSelection).toBe(false);
350
+ });
351
+ it('endDragSelection commits a pending fill drag', () => {
352
+ const ctx = makeCtx({ isDraggingSelection: true, fillDrag: { x: 1 } });
353
+ const sel = createSelection(ctx);
354
+ sel.endDragSelection();
355
+ expect(ctx.onFillPointerUp).toHaveBeenCalled();
356
+ });
357
+ it('onWindowPointerMove delegates to fill move when fill active', () => {
358
+ const ctx = makeCtx({ fillDrag: { x: 1 } });
359
+ const sel = createSelection(ctx);
360
+ const ev = { buttons: 1 };
361
+ sel.onWindowPointerMove(ev);
362
+ expect(ctx.onFillPointerMove).toHaveBeenCalledWith(ev);
363
+ });
364
+ it('onWindowPointerMove ignores when not dragging and no fill', () => {
365
+ const ctx = makeCtx({ fillDrag: null, isDraggingSelection: false });
366
+ const sel = createSelection(ctx);
367
+ sel.onWindowPointerMove({ buttons: 0 });
368
+ expect(ctx.onFillPointerUp).not.toHaveBeenCalled();
369
+ });
370
+ it('onWindowPointerMove ends the drag when no buttons held', () => {
371
+ const ctx = makeCtx({ fillDrag: null, isDraggingSelection: true });
372
+ const sel = createSelection(ctx);
373
+ sel.onWindowPointerMove({ buttons: 0 });
374
+ expect(ctx.isDraggingSelection).toBe(false);
375
+ });
376
+ it('onWindowPointerMove keeps the drag while a button is held', () => {
377
+ const ctx = makeCtx({ fillDrag: null, isDraggingSelection: true });
378
+ const sel = createSelection(ctx);
379
+ sel.onWindowPointerMove({ buttons: 1 });
380
+ expect(ctx.isDraggingSelection).toBe(true);
381
+ });
382
+ });
383
+ describe('createSelection - onCellClick', () => {
384
+ function clickCtx(over = {}) {
385
+ return makeCtx({
386
+ allRows: [makeDataRow('r0', { id: 0 }), makeDataRow('r1', { id: 1 })],
387
+ allColumns: [makeColumn('c0', { editorType: 'text' }), makeColumn('c1', { editorType: 'text' })],
388
+ gridRootEl: { focus: vi.fn() },
389
+ ...over,
390
+ });
391
+ }
392
+ it('returns early when row or column missing', () => {
393
+ const ctx = clickCtx({ allRows: [] });
394
+ const sel = createSelection(ctx);
395
+ sel.onCellClick(0, 0);
396
+ expect(ctx.gridRootEl.focus).not.toHaveBeenCalled();
397
+ });
398
+ it('toggles expansion for a group row', () => {
399
+ const group = makeGroupRow('g0');
400
+ const ctx = clickCtx({ allRows: [group] });
401
+ const sel = createSelection(ctx);
402
+ sel.onCellClick(0, 0);
403
+ expect(group.toggleExpanded).toHaveBeenCalled();
404
+ expect(ctx.grid.setActiveCell).toHaveBeenCalled();
405
+ });
406
+ it('emits onCellClick and onRowClick for data cells', () => {
407
+ const onCellClick = vi.fn();
408
+ const onRowClick = vi.fn();
409
+ const ctx = clickCtx({ props: { onCellClick, onRowClick } });
410
+ const sel = createSelection(ctx);
411
+ sel.onCellClick(1, 0);
412
+ expect(onCellClick).toHaveBeenCalledWith(expect.objectContaining({ rowIndex: 1, colIndex: 0, columnId: 'c0' }));
413
+ expect(onRowClick).toHaveBeenCalledWith(expect.objectContaining({ rowIndex: 1, columnId: 'c0' }));
414
+ });
415
+ it('toggles a checkbox cell and clears editing', () => {
416
+ const ctx = clickCtx({
417
+ allColumns: [makeColumn('c0', { editorType: 'checkbox' })],
418
+ editingCell: { rowIndex: 0, colIndex: 0 },
419
+ });
420
+ const sel = createSelection(ctx);
421
+ sel.onCellClick(0, 0);
422
+ expect(ctx.toggleBooleanCell).toHaveBeenCalledWith(0, 0);
423
+ expect(ctx.editingCell).toBeNull();
424
+ expect(ctx.selectionRange.anchor).toEqual({ rowIndex: 0, colIndex: 0 });
425
+ });
426
+ it('enters edit mode when clicking the previously-active editable cell', () => {
427
+ const ctx = clickCtx({
428
+ editingEnabled: true,
429
+ activeAtPointerDown: { rowIndex: 0, colIndex: 0 },
430
+ });
431
+ const sel = createSelection(ctx);
432
+ sel.onCellClick(0, 0);
433
+ expect(ctx.onCellDoubleClick).toHaveBeenCalledWith(0, 0);
434
+ });
435
+ it('clears editing when not re-clicking the active cell', () => {
436
+ const ctx = clickCtx({
437
+ editingEnabled: true,
438
+ activeAtPointerDown: { rowIndex: 1, colIndex: 1 },
439
+ editingCell: { rowIndex: 0, colIndex: 0 },
440
+ });
441
+ const sel = createSelection(ctx);
442
+ sel.onCellClick(0, 0);
443
+ expect(ctx.onCellDoubleClick).not.toHaveBeenCalled();
444
+ expect(ctx.editingCell).toBeNull();
445
+ });
446
+ });
447
+ describe('createSelection - emitCellDoubleClick', () => {
448
+ it('emits the public double-click events and runs edit entry', () => {
449
+ const onCellDoubleClick = vi.fn();
450
+ const onRowDoubleClick = vi.fn();
451
+ const ctx = makeCtx({
452
+ allRows: [makeDataRow('r0', { id: 0 })],
453
+ allColumns: [makeColumn('c0', { editorType: 'text' })],
454
+ props: { onCellDoubleClick, onRowDoubleClick },
455
+ });
456
+ const sel = createSelection(ctx);
457
+ sel.emitCellDoubleClick(0, 0);
458
+ expect(onCellDoubleClick).toHaveBeenCalledWith(expect.objectContaining({ rowIndex: 0, colIndex: 0, columnId: 'c0' }));
459
+ expect(onRowDoubleClick).toHaveBeenCalled();
460
+ expect(ctx.onCellDoubleClick).toHaveBeenCalledWith(0, 0);
461
+ });
462
+ it('skips the public events for group rows but still runs edit entry', () => {
463
+ const onCellDoubleClick = vi.fn();
464
+ const ctx = makeCtx({
465
+ allRows: [makeGroupRow('g0')],
466
+ allColumns: [makeColumn('c0')],
467
+ props: { onCellDoubleClick },
468
+ });
469
+ const sel = createSelection(ctx);
470
+ sel.emitCellDoubleClick(0, 0);
471
+ expect(onCellDoubleClick).not.toHaveBeenCalled();
472
+ expect(ctx.onCellDoubleClick).toHaveBeenCalledWith(0, 0);
473
+ });
474
+ });
475
+ describe('createSelection - scrollActiveCellIntoView', () => {
476
+ it('returns early without a scroll container', () => {
477
+ const ctx = makeCtx({ scrollContainer: null });
478
+ const sel = createSelection(ctx);
479
+ expect(() => sel.scrollActiveCellIntoView(0, 0)).not.toThrow();
480
+ });
481
+ it('returns early for out-of-bounds indices', () => {
482
+ const container = document.createElement('div');
483
+ const ctx = makeCtx({
484
+ scrollContainer: container,
485
+ allRows: [makeDataRow('r0')],
486
+ allColumns: [makeColumn('c0')],
487
+ });
488
+ const sel = createSelection(ctx);
489
+ // rowIndex out of range -> early return, no throw
490
+ expect(() => sel.scrollActiveCellIntoView(5, 0)).not.toThrow();
491
+ expect(() => sel.scrollActiveCellIntoView(0, 5)).not.toThrow();
492
+ });
493
+ it('adjusts scrollLeft in non-virtualized mode using column virtualizer offsets', () => {
494
+ const container = document.createElement('div');
495
+ Object.defineProperty(container, 'clientWidth', { value: 100, configurable: true });
496
+ Object.defineProperty(container, 'clientHeight', { value: 100, configurable: true });
497
+ container.scrollLeft = 500;
498
+ const ctx = makeCtx({
499
+ scrollContainer: container,
500
+ rowVirtualizationEnabled: false,
501
+ allRows: [makeDataRow('r0'), makeDataRow('r1')],
502
+ allColumns: [makeColumn('c0'), makeColumn('c1'), makeColumn('c2')],
503
+ renderedColumnItems: [],
504
+ columnVirtualizer: {
505
+ getOffsetForIndex: (i) => i * 140,
506
+ getSizeForIndex: () => 140,
507
+ },
508
+ theadEl: { offsetHeight: 0 },
509
+ });
510
+ const sel = createSelection(ctx);
511
+ // target column 0 is left of the current scrollLeft -> scroll left to it
512
+ sel.scrollActiveCellIntoView(0, 0);
513
+ expect(container.scrollLeft).toBe(0);
514
+ });
515
+ it('virtualized: uses native scrollIntoView when the row is mounted', () => {
516
+ const container = document.createElement('div');
517
+ Object.defineProperty(container, 'clientWidth', { value: 100, configurable: true });
518
+ Object.defineProperty(container, 'clientHeight', { value: 100, configurable: true });
519
+ const tr = document.createElement('tr');
520
+ tr.className = 'sv-grid-row';
521
+ const cell = document.createElement('td');
522
+ cell.setAttribute('data-svgrid-row', '0');
523
+ tr.appendChild(cell);
524
+ container.appendChild(tr);
525
+ const scrollIntoView = vi.fn();
526
+ tr.scrollIntoView = scrollIntoView;
527
+ const ctx = makeCtx({
528
+ scrollContainer: container,
529
+ rowVirtualizationEnabled: true,
530
+ allRows: [makeDataRow('r0')],
531
+ allColumns: [makeColumn('c0')],
532
+ renderedColumnItems: [{ index: 0, start: 0, size: 140 }],
533
+ });
534
+ const sel = createSelection(ctx);
535
+ sel.scrollActiveCellIntoView(0, 0);
536
+ expect(scrollIntoView).toHaveBeenCalledWith(expect.objectContaining({ block: 'nearest' }));
537
+ });
538
+ it('virtualized + scroll scaling: maps the logical offset into DOM space', () => {
539
+ const container = document.createElement('div');
540
+ Object.defineProperty(container, 'clientHeight', { value: 100, configurable: true });
541
+ container.scrollTop = 0;
542
+ const ctx = makeCtx({
543
+ scrollContainer: container,
544
+ rowVirtualizationEnabled: true,
545
+ rowScrollScalingActive: true,
546
+ allRows: [makeDataRow('r0')],
547
+ allColumns: [makeColumn('c0')],
548
+ theadEl: { offsetHeight: 10 },
549
+ virtualizer: { getOffsetForIndex: () => 1000, getSizeForIndex: () => 20 },
550
+ logicalToDomRowOffset: (n) => n / 2, // 1000 -> 500
551
+ columnVirtualizer: { getOffsetForIndex: () => 0, getSizeForIndex: () => 140 },
552
+ });
553
+ const sel = createSelection(ctx);
554
+ sel.scrollActiveCellIntoView(0, 0);
555
+ // nextTop = max(500 - 10, 0) = 490
556
+ expect(container.scrollTop).toBe(490);
557
+ });
558
+ it('virtualized: computes scrollTop manually when the row is offscreen', () => {
559
+ const container = document.createElement('div');
560
+ Object.defineProperty(container, 'clientHeight', { value: 100, configurable: true });
561
+ container.scrollTop = 0;
562
+ const ctx = makeCtx({
563
+ scrollContainer: container,
564
+ rowVirtualizationEnabled: true,
565
+ rowScrollScalingActive: false,
566
+ allRows: [makeDataRow('r0')],
567
+ allColumns: [makeColumn('c0')],
568
+ theadEl: { offsetHeight: 10 },
569
+ virtualizer: { getOffsetForIndex: () => 500, getSizeForIndex: () => 30 },
570
+ columnVirtualizer: { getOffsetForIndex: () => 0, getSizeForIndex: () => 140 },
571
+ });
572
+ const sel = createSelection(ctx);
573
+ sel.scrollActiveCellIntoView(0, 0);
574
+ // rowBottom = 10 + 500 + 30 = 540 > 0 + 100 -> nextTop = 540 - 100 = 440
575
+ expect(container.scrollTop).toBe(440);
576
+ });
577
+ it('non-virtualized: scrolls up when the cell overlaps the sticky header', () => {
578
+ const container = document.createElement('div');
579
+ Object.defineProperty(container, 'clientHeight', { value: 200, configurable: true });
580
+ Object.defineProperty(container, 'clientWidth', { value: 500, configurable: true });
581
+ container.scrollTop = 100;
582
+ container.scrollLeft = 0;
583
+ container.getBoundingClientRect = () => ({ top: 0, left: 0, bottom: 200, right: 500 });
584
+ const td = document.createElement('td');
585
+ td.setAttribute('data-svgrid-row', '0');
586
+ td.setAttribute('data-svgrid-col', '0');
587
+ td.getBoundingClientRect = () => ({ top: 5, bottom: 35, left: 0, right: 140 });
588
+ container.appendChild(td);
589
+ const ctx = makeCtx({
590
+ scrollContainer: container,
591
+ rowVirtualizationEnabled: false,
592
+ allRows: [makeDataRow('r0')],
593
+ allColumns: [makeColumn('c0')],
594
+ headerHeight: 30,
595
+ theadEl: { offsetHeight: 30 },
596
+ renderedColumnItems: [{ index: 0, start: 0, size: 140 }],
597
+ });
598
+ const sel = createSelection(ctx);
599
+ sel.scrollActiveCellIntoView(0, 0);
600
+ // cellTopInView (5) < headerHeight (30) -> nextTop = 100 + 5 - 30 = 75
601
+ expect(container.scrollTop).toBe(75);
602
+ });
603
+ it('non-virtualized: scrolls down when the cell is past the bottom', () => {
604
+ const container = document.createElement('div');
605
+ Object.defineProperty(container, 'clientHeight', { value: 200, configurable: true });
606
+ Object.defineProperty(container, 'clientWidth', { value: 500, configurable: true });
607
+ container.scrollTop = 0;
608
+ container.scrollLeft = 0;
609
+ container.getBoundingClientRect = () => ({ top: 0, left: 0, bottom: 200, right: 500 });
610
+ const td = document.createElement('td');
611
+ td.setAttribute('data-svgrid-row', '0');
612
+ td.setAttribute('data-svgrid-col', '0');
613
+ td.getBoundingClientRect = () => ({ top: 250, bottom: 290, left: 0, right: 140 });
614
+ container.appendChild(td);
615
+ const ctx = makeCtx({
616
+ scrollContainer: container,
617
+ rowVirtualizationEnabled: false,
618
+ allRows: [makeDataRow('r0')],
619
+ allColumns: [makeColumn('c0')],
620
+ headerHeight: 30,
621
+ theadEl: { offsetHeight: 30 },
622
+ renderedColumnItems: [{ index: 0, start: 0, size: 140 }],
623
+ });
624
+ const sel = createSelection(ctx);
625
+ sel.scrollActiveCellIntoView(0, 0);
626
+ // cellBotInView (290) > clientHeight (200) -> nextTop = 0 + 290 - 200 = 90
627
+ expect(container.scrollTop).toBe(90);
628
+ });
629
+ it('scrolls right when the target column is past the viewport', () => {
630
+ const container = document.createElement('div');
631
+ Object.defineProperty(container, 'clientWidth', { value: 100, configurable: true });
632
+ Object.defineProperty(container, 'clientHeight', { value: 100, configurable: true });
633
+ container.scrollLeft = 0;
634
+ const ctx = makeCtx({
635
+ scrollContainer: container,
636
+ rowVirtualizationEnabled: false,
637
+ allRows: [makeDataRow('r0')],
638
+ allColumns: [makeColumn('c0'), makeColumn('c1'), makeColumn('c2')],
639
+ renderedColumnItems: [{ index: 2, start: 280, size: 140 }],
640
+ theadEl: { offsetHeight: 0 },
641
+ });
642
+ const sel = createSelection(ctx);
643
+ sel.scrollActiveCellIntoView(0, 2);
644
+ // cellEnd (420) - clientWidth (100) = 320
645
+ expect(container.scrollLeft).toBe(320);
646
+ });
647
+ });
@@ -0,0 +1 @@
1
+ export {};