@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,557 @@
1
+ import "./sv-grid-scrollbar";
2
+ import { isGroupRow, columnDefMatchesId, } from "./cell-values";
3
+ export function createGridApi(ctx) {
4
+ function buildApi() {
5
+ const findColumn = (columnId) => ctx.grid.getAllColumns().find((column) => column.id === columnId);
6
+ return {
7
+ getCellValue(rowIndex, columnId) {
8
+ const row = ctx.internalData[rowIndex];
9
+ const column = findColumn(columnId);
10
+ if (!row || !column)
11
+ return undefined;
12
+ if (column.columnDef.accessorFn)
13
+ return column.columnDef.accessorFn(row);
14
+ if (column.columnDef.field)
15
+ return row[column.columnDef.field];
16
+ return undefined;
17
+ },
18
+ setCellValue(rowIndex, columnId, value) {
19
+ const row = ctx.internalData[rowIndex];
20
+ const column = findColumn(columnId);
21
+ if (!row || !column?.columnDef.field)
22
+ return;
23
+ const next = ctx.internalData.slice();
24
+ next[rowIndex] = { ...row, [column.columnDef.field]: value };
25
+ ctx.internalData = next;
26
+ },
27
+ startEditing(rowIndex, columnId) {
28
+ return ctx.startEditing(rowIndex, columnId);
29
+ },
30
+ stopEditing(cancel) {
31
+ return ctx.stopEditing(cancel ?? false);
32
+ },
33
+ selectCells(ranges) {
34
+ // Multiple rectangles are supported. Empty array clears everything.
35
+ if (!ranges || ranges.length === 0) {
36
+ ctx.selectionRange = { anchor: null, focus: null };
37
+ ctx.selectionRanges = [];
38
+ return;
39
+ }
40
+ const rowCount = ctx.internalData.length;
41
+ const colCount = ctx.allColumns.length;
42
+ if (rowCount === 0 || colCount === 0)
43
+ return;
44
+ // Clamp to the visible grid bounds so callers can pass open-ended
45
+ // values like `[0, 0, Infinity, Infinity]` to mean "select all".
46
+ const toRange = ([r1, c1, r2, c2]) => ({
47
+ anchor: {
48
+ rowIndex: Math.max(0, Math.min(rowCount - 1, Math.min(r1, r2))),
49
+ colIndex: Math.max(0, Math.min(colCount - 1, Math.min(c1, c2))),
50
+ },
51
+ focus: {
52
+ rowIndex: Math.max(0, Math.min(rowCount - 1, Math.max(r1, r2))),
53
+ colIndex: Math.max(0, Math.min(colCount - 1, Math.max(c1, c2))),
54
+ },
55
+ });
56
+ const built = ranges.map(toRange);
57
+ // Last range is the active one; the rest are committed extras.
58
+ ctx.selectionRanges = built.slice(0, -1);
59
+ const active = built[built.length - 1];
60
+ ctx.selectionRange = active;
61
+ // Move the active cell to the last range's top-left so keyboard
62
+ // navigation continues from there.
63
+ ctx.setActiveCell(active.anchor.rowIndex, active.anchor.colIndex);
64
+ },
65
+ getSelected() {
66
+ return ctx
67
+ .getSelectionRects()
68
+ .map((r) => [r.minRow, r.minCol, r.maxRow, r.maxCol]);
69
+ },
70
+ addRow(row, position = "bottom") {
71
+ this.addRows([row], position);
72
+ },
73
+ addRows(rows, position = "bottom") {
74
+ const next = ctx.internalData.slice();
75
+ if (position === "top")
76
+ next.unshift(...rows);
77
+ else if (position === "bottom")
78
+ next.push(...rows);
79
+ else if (typeof position === "number")
80
+ next.splice(Math.max(0, Math.min(position, next.length)), 0, ...rows);
81
+ ctx.internalData = next;
82
+ },
83
+ removeRow(rowIndex) {
84
+ this.removeRows([rowIndex]);
85
+ },
86
+ removeRows(rowIndices) {
87
+ const drop = new Set(rowIndices);
88
+ ctx.internalData = ctx.internalData.filter((_, i) => !drop.has(i));
89
+ },
90
+ applyTransaction(tx) {
91
+ const getId = ctx.props.getRowId;
92
+ let next = ctx.internalData.slice();
93
+ let added = 0;
94
+ let updated = 0;
95
+ let removed = 0;
96
+ if (tx.remove?.length) {
97
+ const removeIds = new Set();
98
+ const removeRefs = new Set();
99
+ for (const r of tx.remove) {
100
+ if (typeof r === "string")
101
+ removeIds.add(r);
102
+ else
103
+ removeRefs.add(r);
104
+ }
105
+ next = next.filter((row, i) => {
106
+ const hit = removeRefs.has(row) ||
107
+ (getId ? removeIds.has(getId(row, i)) : false);
108
+ if (hit)
109
+ removed += 1;
110
+ return !hit;
111
+ });
112
+ }
113
+ if (tx.update?.length && getId) {
114
+ const byId = new Map();
115
+ for (const u of tx.update)
116
+ byId.set(getId(u, 0), u);
117
+ next = next.map((row, i) => {
118
+ const u = byId.get(getId(row, i));
119
+ if (u) {
120
+ updated += 1;
121
+ return u;
122
+ }
123
+ return row;
124
+ });
125
+ }
126
+ if (tx.add?.length) {
127
+ next.push(...tx.add);
128
+ added += tx.add.length;
129
+ }
130
+ ctx.internalData = next;
131
+ return { added, updated, removed };
132
+ },
133
+ addColumn(column, position = "right") {
134
+ this.addColumns([column], position);
135
+ },
136
+ addColumns(columns, position = "right") {
137
+ const next = ctx.internalColumns.slice();
138
+ if (position === "left")
139
+ next.unshift(...columns);
140
+ else if (position === "right")
141
+ next.push(...columns);
142
+ else if (typeof position === "number")
143
+ next.splice(Math.max(0, Math.min(position, next.length)), 0, ...columns);
144
+ ctx.internalColumns = next;
145
+ },
146
+ removeColumn(columnId) {
147
+ ctx.internalColumns = ctx.internalColumns.filter((def) => !columnDefMatchesId(def, columnId));
148
+ },
149
+ setColumnVisible(columnId, visible) {
150
+ if (visible) {
151
+ const next = { ...ctx.hiddenColumns };
152
+ delete next[columnId];
153
+ ctx.hiddenColumns = next;
154
+ }
155
+ else {
156
+ ctx.hiddenColumns = { ...ctx.hiddenColumns, [columnId]: true };
157
+ }
158
+ },
159
+ isColumnVisible(columnId) {
160
+ return !ctx.hiddenColumns[columnId];
161
+ },
162
+ setSort(columnId, direction) {
163
+ // Honour the column's `sortable` opt-out. Asking the engine
164
+ // whether the column can sort folds in both the feature toggle
165
+ // AND the per-column flag.
166
+ const col = ctx.allColumns.find((c) => c.id === columnId);
167
+ if (col && !col.getCanSort?.())
168
+ return;
169
+ const clauses = direction
170
+ ? [{ id: columnId, desc: direction === "desc" }]
171
+ : (ctx.grid.getState().sorting ?? []).filter((entry) => entry.id !== columnId);
172
+ ctx.grid.store.setState((prev) => ({ ...prev, sorting: clauses }));
173
+ },
174
+ clearSort() {
175
+ ctx.grid.store.setState((prev) => ({ ...prev, sorting: [] }));
176
+ },
177
+ setGroupBy(columnIds) {
178
+ ctx.grid.setGrouping([...columnIds]);
179
+ },
180
+ setFilter(columnId, filter) {
181
+ // Honour the column's `filterable` opt-out. Same approach as
182
+ // `setSort` - `getCanFilter()` already combines the feature flag
183
+ // and the per-column field.
184
+ const col = ctx.allColumns.find((c) => c.id === columnId);
185
+ if (col && !col.getCanFilter?.())
186
+ return;
187
+ if (!filter) {
188
+ this.clearFilter(columnId);
189
+ return;
190
+ }
191
+ ctx.filterMenuValues = {
192
+ ...ctx.filterMenuValues,
193
+ [columnId]: {
194
+ operator: filter.operator,
195
+ value: filter.value ?? "",
196
+ ...(filter.operator === "between"
197
+ ? { valueTo: filter.valueTo ?? "" }
198
+ : {}),
199
+ // Optional second condition + join (multi-condition filtering).
200
+ ...(filter.operator2
201
+ ? {
202
+ operator2: filter.operator2,
203
+ value2: filter.value2 ?? "",
204
+ join: filter.join ?? "AND",
205
+ ...(filter.operator2 === "between"
206
+ ? { valueTo2: filter.valueTo2 ?? "" }
207
+ : {}),
208
+ }
209
+ : {}),
210
+ },
211
+ };
212
+ },
213
+ setFacetFilter(columnId, values) {
214
+ // Empty array OR null both mean "clear this column's facet" -
215
+ // mirrors how the column menu treats unchecking the last value.
216
+ if (!values || values.length === 0) {
217
+ if (ctx.valueFilters[columnId]) {
218
+ const next = { ...ctx.valueFilters };
219
+ delete next[columnId];
220
+ ctx.valueFilters = next;
221
+ }
222
+ return;
223
+ }
224
+ ctx.valueFilters = {
225
+ ...ctx.valueFilters,
226
+ [columnId]: new Set(values),
227
+ };
228
+ },
229
+ clearFilter(columnId) {
230
+ ctx.clearColumnFilter(columnId);
231
+ },
232
+ clearAllFilters() {
233
+ // Wipe every filter surface in one go: column-menu, filter-row, set
234
+ // filters, and the global search box.
235
+ ctx.filterMenuValues = {};
236
+ ctx.filterRowValues = {};
237
+ ctx.valueFilters = {};
238
+ if (ctx.globalFilter !== "")
239
+ ctx.globalFilter = "";
240
+ },
241
+ getFilters() {
242
+ // Return a defensive copy so callers can't mutate internal state.
243
+ const out = {};
244
+ for (const [columnId, filter] of Object.entries(ctx.filterMenuValues)) {
245
+ out[columnId] = {
246
+ operator: filter.operator,
247
+ value: filter.value,
248
+ ...(filter.operator === "between" && filter.valueTo
249
+ ? { valueTo: filter.valueTo }
250
+ : {}),
251
+ };
252
+ }
253
+ return out;
254
+ },
255
+ getDisplayedRows() {
256
+ // `allRows` is the final, post-filter, post-sort, post-group,
257
+ // post-pagination list - exactly what the body renders. Skip group
258
+ // header rows (they wrap an aggregate, not a TData row).
259
+ const out = [];
260
+ for (const row of ctx.allRows) {
261
+ if (row.subRows && row.subRows.length > 0)
262
+ continue;
263
+ out.push(row.original);
264
+ }
265
+ return out;
266
+ },
267
+ getData() {
268
+ return ctx.internalData;
269
+ },
270
+ getColumns() {
271
+ // Snapshot every column with its human-readable label and
272
+ // visibility flag. Used by external code (exporters, column
273
+ // pickers) so they don't have to re-walk the columnDef tree.
274
+ // Hidden columns ARE included (with `visible: false`) so a column
275
+ // picker can re-enable them - callers that only want what's
276
+ // rendered filter by `.visible` (e.g. the exporter does).
277
+ const describe = (c, visible) => ({
278
+ id: c.id,
279
+ field: c.columnDef.field,
280
+ header: typeof c.columnDef.header === "string"
281
+ ? c.columnDef.header
282
+ : c.id,
283
+ visible,
284
+ });
285
+ // Visible columns first, in their current visual order...
286
+ const out = ctx.allColumns.map((c) => describe(c, true));
287
+ // ...then any column hidden via `visible: false` / setColumnVisible.
288
+ // Those aren't in `allColumns` (that list is visible-only), so pull
289
+ // them straight from the engine's leaf set.
290
+ const visibleIds = new Set(out.map((c) => c.id));
291
+ for (const c of ctx.grid.getAllColumns()) {
292
+ if (!visibleIds.has(c.id))
293
+ out.push(describe(c, false));
294
+ }
295
+ return out;
296
+ },
297
+ clearRowSelection() {
298
+ // Wipe the internal selection map AND emit the change so any
299
+ // consumer holding a derived `selectedRows` resets too.
300
+ ctx.grid.setRowSelection(() => ({}));
301
+ ctx.props.onRowSelectionChange?.({}, []);
302
+ },
303
+ setColumnWidth(columnId, width) {
304
+ const clamped = Math.max(ctx.MIN_COLUMN_WIDTH, Math.floor(width));
305
+ ctx.columnWidths = { ...ctx.columnWidths, [columnId]: clamped };
306
+ },
307
+ getColumnWidths() {
308
+ // Resolve every column's effective width by consulting both the
309
+ // resize overrides (`columnWidths`) AND the columnDef defaults,
310
+ // so the snapshot is round-trippable through setColumnWidth.
311
+ const out = {};
312
+ for (const c of ctx.allColumns)
313
+ out[c.id] = ctx.getColumnWidth(c.id);
314
+ return out;
315
+ },
316
+ autosizeColumn(columnId) {
317
+ ctx.autosizeColumn(columnId);
318
+ },
319
+ autosizeAllColumns() {
320
+ ctx.autosizeAllColumns();
321
+ },
322
+ setColumnPinning(pinning) {
323
+ // Defensive copy + dedupe so callers can't mutate our state.
324
+ const left = Array.from(new Set(pinning.left ?? []));
325
+ const right = Array.from(new Set(pinning.right ?? []));
326
+ ctx.columnPinning = { left, right };
327
+ },
328
+ getColumnPinning() {
329
+ return {
330
+ left: ctx.columnPinning.left.slice(),
331
+ right: ctx.columnPinning.right.slice(),
332
+ };
333
+ },
334
+ // ---- Column reorder
335
+ setColumnOrder(order) {
336
+ ctx.setColumnOrderInternal(order);
337
+ },
338
+ getColumnOrder() {
339
+ return ctx.getCurrentColumnOrder();
340
+ },
341
+ setRowExpanded(id, expanded) {
342
+ ctx.grid.setExpanded((prev) => ({ ...prev, [id]: !!expanded }));
343
+ },
344
+ expandAllGroups() {
345
+ // Walk the current grouped row model and flip every group node on.
346
+ const next = {};
347
+ const walk = (rows) => {
348
+ for (const row of rows) {
349
+ if (row.getCanExpand?.())
350
+ next[row.id] = true;
351
+ const sub = row.subRows;
352
+ if (sub && sub.length > 0)
353
+ walk(sub);
354
+ }
355
+ };
356
+ walk(ctx.grid.getRowModel().rows);
357
+ ctx.grid.setExpanded(() => next);
358
+ },
359
+ collapseAllGroups() {
360
+ ctx.grid.setExpanded(() => ({}));
361
+ },
362
+ // ---- Undo / redo (history + pointer)
363
+ undo() {
364
+ if (ctx.historyPtr < 0)
365
+ return false;
366
+ const step = ctx.history[ctx.historyPtr];
367
+ if (!step)
368
+ return false;
369
+ ctx.applyHistoryStep(step, 'undo');
370
+ ctx.historyPtr -= 1;
371
+ ctx.historyVersion += 1;
372
+ return true;
373
+ },
374
+ redo() {
375
+ if (ctx.historyPtr >= ctx.history.length - 1)
376
+ return false;
377
+ const step = ctx.history[ctx.historyPtr + 1];
378
+ if (!step)
379
+ return false;
380
+ ctx.applyHistoryStep(step, 'redo');
381
+ ctx.historyPtr += 1;
382
+ ctx.historyVersion += 1;
383
+ return true;
384
+ },
385
+ canUndo() { void ctx.historyVersion; return ctx.historyPtr >= 0; },
386
+ canRedo() { void ctx.historyVersion; return ctx.historyPtr < ctx.history.length - 1; },
387
+ clearHistory() { ctx.history = []; ctx.historyPtr = -1; ctx.historyVersion += 1; },
388
+ // ---- Find
389
+ openFind() { ctx.findOpen = true; },
390
+ closeFind() { ctx.findOpen = false; ctx.findQuery = ''; },
391
+ setFindQuery(q) { ctx.findQuery = q; ctx.findHitIndex = 0; },
392
+ getFindHits() { return ctx.findHits.slice(); },
393
+ // ---- Row selection (read + write)
394
+ getSelectedRows() {
395
+ const out = [];
396
+ for (const row of ctx.allRows) {
397
+ if (isGroupRow(row))
398
+ continue;
399
+ if (ctx.rowSelectionState[row.id])
400
+ out.push(row.original);
401
+ }
402
+ return out;
403
+ },
404
+ getSelectedRowIds() {
405
+ const out = [];
406
+ for (const row of ctx.allRows) {
407
+ if (!isGroupRow(row) && ctx.rowSelectionState[row.id])
408
+ out.push(row.id);
409
+ }
410
+ return out;
411
+ },
412
+ selectRows(ids, additive = false) {
413
+ const set = new Set(ids);
414
+ ctx.grid.setRowSelection((prev) => {
415
+ const next = additive ? { ...prev } : {};
416
+ for (const id of set)
417
+ next[id] = true;
418
+ return next;
419
+ });
420
+ },
421
+ selectAllRows() {
422
+ const next = {};
423
+ for (const row of ctx.allRows)
424
+ if (!isGroupRow(row))
425
+ next[row.id] = true;
426
+ ctx.grid.setRowSelection(() => next);
427
+ },
428
+ toggleRowSelected(id) {
429
+ ctx.toggleRowSelectionById(id);
430
+ },
431
+ // ---- Pagination
432
+ getPageInfo() {
433
+ const { pageIndex, pageSize } = ctx.paginationState;
434
+ const total = ctx.allRowsBeforePagination.length;
435
+ const pageCount = Math.max(1, Math.ceil(total / Math.max(1, pageSize)));
436
+ return { pageIndex, pageSize, pageCount, total };
437
+ },
438
+ setPage(pageIndex) {
439
+ const total = ctx.allRowsBeforePagination.length;
440
+ const pageCount = Math.max(1, Math.ceil(total / Math.max(1, ctx.paginationState.pageSize)));
441
+ ctx.goToPage(Math.max(0, Math.min(pageIndex, pageCount - 1)));
442
+ },
443
+ nextPage() {
444
+ this.setPage(ctx.paginationState.pageIndex + 1);
445
+ },
446
+ prevPage() {
447
+ this.setPage(ctx.paginationState.pageIndex - 1);
448
+ },
449
+ firstPage() {
450
+ this.setPage(0);
451
+ },
452
+ lastPage() {
453
+ const total = ctx.allRowsBeforePagination.length;
454
+ const pageCount = Math.max(1, Math.ceil(total / Math.max(1, ctx.paginationState.pageSize)));
455
+ this.setPage(pageCount - 1);
456
+ },
457
+ setPageSize(size) {
458
+ // Calls the component-level helper (lexical scope), not this method.
459
+ ctx.setPageSize(Math.max(1, Math.floor(size)));
460
+ },
461
+ // ---- Navigation / scrolling
462
+ scrollToRow(rowIndex) {
463
+ if (!ctx.scrollContainer)
464
+ return;
465
+ const maxIndex = Math.max(0, ctx.allRows.length - 1);
466
+ const clamped = Math.max(0, Math.min(rowIndex, maxIndex));
467
+ // Logical offset from the virtualizer (honors uniform OR per-row
468
+ // sizing), then mapped into the capped DOM scroll space so it lands
469
+ // correctly on huge grids where the DOM height is scaled down.
470
+ const logical = ctx.virtualizer.getOffsetForIndex(clamped);
471
+ ctx.scrollContainer.scrollTop = ctx.logicalToDomRowOffset(logical);
472
+ ctx.scheduleScrollSync(ctx.scrollContainer.scrollTop, ctx.scrollContainer.scrollLeft);
473
+ },
474
+ getActiveCell() {
475
+ const a = ctx.activeCell;
476
+ if (!a || a.rowIndex < 0 || a.colIndex < 0)
477
+ return null;
478
+ const col = ctx.allColumns[a.colIndex];
479
+ return {
480
+ rowIndex: a.rowIndex,
481
+ colIndex: a.colIndex,
482
+ columnId: col?.id ?? "",
483
+ };
484
+ },
485
+ setActiveCell(rowIndex, colIndex) {
486
+ const r = Math.max(0, Math.min(rowIndex, Math.max(0, ctx.allRows.length - 1)));
487
+ const c = Math.max(0, Math.min(colIndex, Math.max(0, ctx.allColumns.length - 1)));
488
+ // Calls the component-level helper (lexical scope), not this method.
489
+ ctx.setActiveCell(r, c);
490
+ },
491
+ // ---- View state (save / restore)
492
+ getState() {
493
+ return {
494
+ sorting: (ctx.grid.getState().sorting ?? []).map((s) => ({ id: s.id, desc: s.desc })),
495
+ grouping: [...(ctx.grid.getState().grouping ?? [])],
496
+ pagination: {
497
+ pageIndex: ctx.paginationState.pageIndex,
498
+ pageSize: ctx.paginationState.pageSize,
499
+ },
500
+ columnWidths: this.getColumnWidths(),
501
+ columnPinning: this.getColumnPinning(),
502
+ columnOrder: ctx.getCurrentColumnOrder(),
503
+ hiddenColumns: Object.keys(ctx.hiddenColumns).filter((k) => ctx.hiddenColumns[k]),
504
+ globalFilter: ctx.globalFilter,
505
+ columnFilters: this.getFilters(),
506
+ facetFilters: Object.fromEntries(Object.entries(ctx.valueFilters).map(([k, set]) => [
507
+ k,
508
+ Array.from(set),
509
+ ])),
510
+ };
511
+ },
512
+ setState(state) {
513
+ if (state.sorting) {
514
+ const sorting = state.sorting.map((s) => ({ id: s.id, desc: s.desc }));
515
+ ctx.grid.store.setState((prev) => ({ ...prev, sorting }));
516
+ }
517
+ if (state.grouping)
518
+ ctx.grid.setGrouping([...state.grouping]);
519
+ if (state.pagination) {
520
+ const p = state.pagination;
521
+ ctx.grid.setPagination((prev) => ({ ...prev, ...p }));
522
+ }
523
+ if (state.columnWidths)
524
+ ctx.columnWidths = { ...state.columnWidths };
525
+ if (state.columnPinning)
526
+ ctx.columnPinning = {
527
+ left: [...(state.columnPinning.left ?? [])],
528
+ right: [...(state.columnPinning.right ?? [])],
529
+ };
530
+ if (state.columnOrder)
531
+ ctx.setColumnOrderInternal(state.columnOrder);
532
+ if (state.hiddenColumns) {
533
+ const next = {};
534
+ for (const id of state.hiddenColumns)
535
+ next[id] = true;
536
+ ctx.hiddenColumns = next;
537
+ }
538
+ if (state.globalFilter !== undefined)
539
+ ctx.globalFilter = state.globalFilter;
540
+ if (state.columnFilters)
541
+ ctx.filterMenuValues = { ...state.columnFilters };
542
+ if (state.facetFilters) {
543
+ const next = {};
544
+ for (const [k, arr] of Object.entries(state.facetFilters))
545
+ next[k] = new Set(arr);
546
+ ctx.valueFilters = next;
547
+ }
548
+ },
549
+ refresh() {
550
+ ctx.grid.store.setState((prev) => ({ ...prev }));
551
+ },
552
+ };
553
+ }
554
+ return {
555
+ buildApi,
556
+ };
557
+ }
@@ -0,0 +1,15 @@
1
+ import { type CellEditorOption, type Column, type Row, type RowData, type TableFeatures } from "./index";
2
+ import "./sv-grid-scrollbar";
3
+ import { type ResolvedCellFormat } from "./conditional-formatting";
4
+ export declare function createCellRender<TFeatures extends TableFeatures = TableFeatures, TData extends RowData = RowData>(ctx: any): {
5
+ cellConditionalFormat: (row: Row<TData>, column: Column<TData>, value: unknown) => ResolvedCellFormat | null;
6
+ computeRowClass: (row: Row<TData>, rowIndex: number) => string;
7
+ computeCellClass: (row: Row<TData>, column: Column<TData>) => string;
8
+ computeCellTooltip: (row: Row<TData>, column: Column<TData>) => string | null;
9
+ computeCellNote: (row: Row<TData>, column: Column<TData>) => string | null;
10
+ getColumnEditorOptions: (column: Column<TData>, row?: Row<TData> | null) => CellEditorOption[];
11
+ formatListCellValue: (column: Column<TData>, value: unknown, row?: Row<TData> | null) => string;
12
+ formatCellValue: (column: Column<TData>, value: unknown, row: Row<TData>) => string;
13
+ formatPinnedValue: (column: Column<TData>, value: unknown) => string;
14
+ computePinnedCellClass: (rowData: TData, column: Column<TData>) => string;
15
+ };