@svgrid/grid 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) 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 +570 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +429 -0
  8. package/dist/SvGrid.controller.svelte.js +1732 -0
  9. package/dist/SvGrid.css +1709 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.svelte +344 -7043
  13. package/dist/SvGrid.svelte.d.ts +4 -357
  14. package/dist/SvGrid.types.d.ts +436 -0
  15. package/dist/SvGrid.types.js +1 -0
  16. package/dist/SvGridChart.svelte +1060 -23
  17. package/dist/SvGridChart.svelte.d.ts +17 -0
  18. package/dist/build-api.d.ts +5 -0
  19. package/dist/build-api.js +527 -0
  20. package/dist/cell-render.d.ts +15 -0
  21. package/dist/cell-render.js +246 -0
  22. package/dist/cell-values.d.ts +28 -0
  23. package/dist/cell-values.js +89 -0
  24. package/dist/chart.d.ts +370 -3
  25. package/dist/chart.js +1135 -42
  26. package/dist/clipboard.d.ts +15 -0
  27. package/dist/clipboard.js +356 -0
  28. package/dist/columns.d.ts +30 -0
  29. package/dist/columns.js +277 -0
  30. package/dist/core.d.ts +1 -1
  31. package/dist/css.d.ts +3 -0
  32. package/dist/editing.d.ts +24 -0
  33. package/dist/editing.js +343 -0
  34. package/dist/editors/cell-editors.d.ts +1 -1
  35. package/dist/facet-buckets.d.ts +13 -0
  36. package/dist/facet-buckets.js +54 -0
  37. package/dist/features.d.ts +5 -0
  38. package/dist/features.js +30 -0
  39. package/dist/filter-operators.d.ts +16 -0
  40. package/dist/filter-operators.js +69 -0
  41. package/dist/hyperformula-adapter.d.ts +82 -0
  42. package/dist/hyperformula-adapter.js +73 -0
  43. package/dist/index.d.ts +5 -2
  44. package/dist/index.js +5 -2
  45. package/dist/keyboard-handlers.d.ts +7 -0
  46. package/dist/keyboard-handlers.js +197 -0
  47. package/dist/menus.d.ts +40 -0
  48. package/dist/menus.js +389 -0
  49. package/dist/named-views.d.ts +27 -0
  50. package/dist/named-views.js +39 -0
  51. package/dist/row-resize.d.ts +43 -0
  52. package/dist/row-resize.js +158 -0
  53. package/dist/scroll-sync.d.ts +9 -0
  54. package/dist/scroll-sync.js +86 -0
  55. package/dist/selection.d.ts +26 -0
  56. package/dist/selection.js +387 -0
  57. package/dist/spreadsheet.d.ts +80 -0
  58. package/dist/spreadsheet.js +194 -0
  59. package/dist/summaries.d.ts +12 -0
  60. package/dist/summaries.js +65 -0
  61. package/dist/svgrid-wrapper.types.d.ts +12 -1
  62. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  63. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  64. package/dist/svgrid.context-menu.test.d.ts +1 -0
  65. package/dist/svgrid.context-menu.test.js +102 -0
  66. package/dist/svgrid.new-features.wrapper.test.js +30 -4
  67. package/dist/svgrid.wrapper.test.js +27 -1
  68. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  69. package/dist/virtualization/scroll-scaling.d.ts +28 -0
  70. package/dist/virtualization/scroll-scaling.js +64 -0
  71. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  72. package/dist/virtualization/scroll-scaling.test.js +86 -0
  73. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  74. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  75. package/dist/virtualization/virtualizer.d.ts +7 -0
  76. package/dist/virtualization/virtualizer.js +30 -0
  77. package/package.json +1 -1
  78. package/src/GridFooter.svelte +164 -0
  79. package/src/GridMenus.svelte +570 -0
  80. package/src/SvGrid.controller.svelte.ts +2195 -0
  81. package/src/SvGrid.css +1747 -0
  82. package/src/SvGrid.helpers.test.ts +415 -0
  83. package/src/SvGrid.helpers.ts +185 -0
  84. package/src/SvGrid.svelte +348 -7043
  85. package/src/SvGrid.types.ts +456 -0
  86. package/src/SvGridChart.svelte +1060 -23
  87. package/src/build-api.coverage.test.ts +532 -0
  88. package/src/build-api.ts +663 -0
  89. package/src/cell-render.test.ts +451 -0
  90. package/src/cell-render.ts +426 -0
  91. package/src/cell-values.ts +114 -0
  92. package/src/chart-export.test.ts +370 -0
  93. package/src/chart.coverage.test.ts +814 -0
  94. package/src/chart.ts +1352 -47
  95. package/src/clipboard.test.ts +731 -0
  96. package/src/clipboard.ts +524 -0
  97. package/src/collaboration.coverage.test.ts +220 -0
  98. package/src/columns.test.ts +702 -0
  99. package/src/columns.ts +419 -0
  100. package/src/core.ts +8 -0
  101. package/src/css.d.ts +3 -0
  102. package/src/editing.test.ts +837 -0
  103. package/src/editing.ts +513 -0
  104. package/src/editors/cell-editors.coverage.test.ts +156 -0
  105. package/src/editors/cell-editors.ts +1 -0
  106. package/src/facet-buckets.test.ts +353 -0
  107. package/src/facet-buckets.ts +67 -0
  108. package/src/features.ts +128 -0
  109. package/src/filter-operators.test.ts +174 -0
  110. package/src/filter-operators.ts +87 -0
  111. package/src/hyperformula-adapter.test.ts +256 -0
  112. package/src/hyperformula-adapter.ts +124 -0
  113. package/src/index.ts +37 -0
  114. package/src/keyboard-handlers.coverage.test.ts +560 -0
  115. package/src/keyboard-handlers.ts +353 -0
  116. package/src/keyboard.ts +97 -97
  117. package/src/menus.test.ts +620 -0
  118. package/src/menus.ts +554 -0
  119. package/src/named-views.coverage.test.ts +210 -0
  120. package/src/named-views.ts +48 -0
  121. package/src/row-resize.test.ts +369 -0
  122. package/src/row-resize.ts +171 -0
  123. package/src/scroll-sync.test.ts +330 -0
  124. package/src/scroll-sync.ts +216 -0
  125. package/src/selection.test.ts +722 -0
  126. package/src/selection.ts +545 -0
  127. package/src/server-data-source.coverage.test.ts +180 -0
  128. package/src/spreadsheet.test.ts +445 -0
  129. package/src/spreadsheet.ts +246 -0
  130. package/src/summaries.ts +204 -0
  131. package/src/sv-grid-scrollbar.ts +13 -1
  132. package/src/svgrid-wrapper.types.ts +12 -1
  133. package/src/svgrid.behavior.test.ts +22 -0
  134. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  135. package/src/svgrid.context-menu.test.ts +126 -0
  136. package/src/svgrid.interaction.test.ts +30 -0
  137. package/src/svgrid.new-features.wrapper.test.ts +65 -4
  138. package/src/svgrid.wrapper.test.ts +27 -1
  139. package/src/test-setup.ts +9 -6
  140. package/src/virtualization/scroll-scaling.test.ts +148 -0
  141. package/src/virtualization/scroll-scaling.ts +121 -0
  142. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  143. package/src/virtualization/virtualizer.ts +26 -0
package/src/menus.ts ADDED
@@ -0,0 +1,554 @@
1
+ // menus 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 { createCellRender } from "./cell-render";
95
+ import { createEditing } from "./editing";
96
+ import { createSelection } from "./selection";
97
+ import { createColumns } from "./columns";
98
+ import { createGridApi } from "./build-api";
99
+ import { createClipboard } from "./clipboard";
100
+ import {
101
+ filterOperatorOptions,
102
+ fallbackOperatorOption,
103
+ TEXT_OPERATORS,
104
+ NUMBER_OPERATORS,
105
+ DATE_OPERATORS,
106
+ CHECKBOX_OPERATORS,
107
+ operatorOption,
108
+ operatorsForColumn,
109
+ defaultOperatorFor,
110
+ operatorLabelFor,
111
+ } from "./filter-operators";
112
+ import {
113
+ type FacetBucket,
114
+ isBucketableColumn,
115
+ buildBuckets,
116
+ isInBucket,
117
+ } from "./facet-buckets";
118
+ import {
119
+ getColumnBaseValue,
120
+ isGroupRow,
121
+ toolPanelHeaderLabel,
122
+ formatSummaryNumeric,
123
+ getColumnAlign,
124
+ getPinnedCellValue,
125
+ getColumnAccessorValue,
126
+ columnDefMatchesId,
127
+ } from "./cell-values";
128
+
129
+ export function createMenus<
130
+ TFeatures extends TableFeatures = TableFeatures,
131
+ TData extends RowData = RowData,
132
+ >(ctx: any) {
133
+ function updateFilterRow(columnId: string, value: string) {
134
+ ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
135
+ const current = ctx.filterMenuValues[columnId] ?? {
136
+ operator: "contains" as const,
137
+ value: "",
138
+ };
139
+ ctx.filterMenuValues = {
140
+ ...ctx.filterMenuValues,
141
+ [columnId]: { ...current, value },
142
+ };
143
+ }
144
+
145
+ function updateFilterOperator(columnId: string, operator: FilterOperator) {
146
+ const current = ctx.filterMenuValues[columnId] ?? {
147
+ operator: "contains" as const,
148
+ value: "",
149
+ };
150
+ ctx.filterMenuValues = {
151
+ ...ctx.filterMenuValues,
152
+ [columnId]: { ...current, operator },
153
+ };
154
+ }
155
+
156
+ function updateFilterMenuValue(columnId: string, value: string) {
157
+ const current = ctx.filterMenuValues[columnId] ?? {
158
+ operator: "contains" as const,
159
+ value: "",
160
+ };
161
+ ctx.filterMenuValues = {
162
+ ...ctx.filterMenuValues,
163
+ [columnId]: { ...current, value },
164
+ };
165
+ ctx.filterRowValues = { ...ctx.filterRowValues, [columnId]: value };
166
+ }
167
+
168
+ /** Upper-bound input for the `between` operator. Only relevant when
169
+ * the column's current operator is `between`; harmless to call
170
+ * otherwise. */
171
+ function updateFilterMenuValueTo(columnId: string, valueTo: string) {
172
+ const current = ctx.filterMenuValues[columnId] ?? {
173
+ operator: "between" as const,
174
+ value: "",
175
+ };
176
+ ctx.filterMenuValues = {
177
+ ...ctx.filterMenuValues,
178
+ [columnId]: { ...current, valueTo },
179
+ };
180
+ }
181
+
182
+ function toggleCheckboxWithKeyboard(
183
+ event: KeyboardEvent,
184
+ toggle: () => void,
185
+ ) {
186
+ if (event.key !== "Enter" && event.key !== " ") return;
187
+ event.preventDefault();
188
+ toggle();
189
+ }
190
+
191
+ function isColumnFiltered(columnId: string) {
192
+ if (ctx.valueFilters[columnId]) return true;
193
+ const menuFilter = ctx.filterMenuValues[columnId];
194
+ return Boolean(
195
+ menuFilter &&
196
+ (menuFilter.operator === "isBlank" || menuFilter.value.trim()),
197
+ );
198
+ }
199
+
200
+ function closeMenus() {
201
+ ctx.columnMenuFor = null;
202
+ ctx.filterMenuFor = null;
203
+ ctx.operatorMenuFor = null;
204
+ ctx.chooseColumnsPos = null;
205
+ }
206
+
207
+ function openChooseColumns(event: MouseEvent) {
208
+ event.stopPropagation();
209
+ // Submenu behavior: keep the parent operations menu open and float the
210
+ // Choose Columns panel out to the right of it (AG-Grid style). Falls
211
+ // back to below the item if the popover would clip the viewport.
212
+ const trigger = event.currentTarget as HTMLElement;
213
+ const menuEl = trigger.closest(".sv-grid-menu") as HTMLElement | null;
214
+ const anchor = (menuEl ?? trigger).getBoundingClientRect();
215
+ const panelWidth = 240;
216
+ const wantRight = anchor.right + 4;
217
+ const x =
218
+ wantRight + panelWidth + 8 > window.innerWidth
219
+ ? clampMenuX(anchor.left - panelWidth - 4, panelWidth)
220
+ : clampMenuX(wantRight, panelWidth);
221
+ ctx.chooseColumnsPos = { x, y: anchor.top };
222
+ }
223
+
224
+ function openColumnMenu(event: MouseEvent, columnId: string) {
225
+ event.stopPropagation();
226
+ const wasOpen = ctx.columnMenuFor === columnId;
227
+ closeMenus();
228
+ if (wasOpen) return;
229
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
230
+ ctx.columnMenuPos = {
231
+ x: clampMenuX(rect.right - 240, 240),
232
+ y: rect.bottom + 4,
233
+ };
234
+ ctx.columnMenuFor = columnId;
235
+ }
236
+
237
+ function openFilterMenu(event: MouseEvent, columnId: string) {
238
+ event.stopPropagation();
239
+ const wasOpen = ctx.filterMenuFor === columnId;
240
+ closeMenus();
241
+ if (wasOpen) return;
242
+
243
+ // If the consumer is running in row-mode (or any mode where the
244
+ // funnel popover wouldn't render), redirect the click to the
245
+ // inline filter input for this column. Without this the funnel
246
+ // would be a dead button - it visibly clicks but nothing happens
247
+ // because the menu is gated behind `showColumnFiltersEffective`.
248
+ if (!ctx.showColumnFiltersEffective && ctx.showFilterRowEffective) {
249
+ // Find the row-mode filter input and focus it. The input is
250
+ // tagged with `data-svgrid-filter-col` so we can target by id
251
+ // without depending on column position.
252
+ const input = (ctx.scrollContainer as HTMLElement | null)?.querySelector<HTMLInputElement>(
253
+ `[data-svgrid-filter-col="${cssEscape(columnId)}"]`,
254
+ );
255
+ if (input) {
256
+ input.focus();
257
+ input.select();
258
+ // Brief pulse so the user sees where focus landed.
259
+ input.classList.add("sv-grid-filter-value-pulse");
260
+ setTimeout(() => input.classList.remove("sv-grid-filter-value-pulse"), 700);
261
+ }
262
+ return;
263
+ }
264
+
265
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
266
+ ctx.filterMenuPos = {
267
+ x: clampMenuX(rect.right - 260, 260),
268
+ y: rect.bottom + 4,
269
+ };
270
+ ctx.columnMenuSearch = "";
271
+ ctx.filterMenuFor = columnId;
272
+ }
273
+
274
+ function openOperatorMenu(event: MouseEvent, columnId: string) {
275
+ event.stopPropagation();
276
+ const wasOpen = ctx.operatorMenuFor === columnId;
277
+ closeMenus();
278
+ if (wasOpen) return;
279
+ const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
280
+ ctx.operatorMenuPos = { x: clampMenuX(rect.left, 184), y: rect.bottom + 4 };
281
+ ctx.operatorMenuFor = columnId;
282
+ }
283
+
284
+ function sortColumnFromMenu(columnId: string, desc: boolean) {
285
+ ctx.grid.store.setState((prev: any) => ({
286
+ ...prev,
287
+ sorting: [{ id: columnId, desc }],
288
+ }));
289
+ closeMenus();
290
+ }
291
+
292
+ function clearColumnSort(columnId: string) {
293
+ ctx.grid.store.setState((prev: any) => ({
294
+ ...prev,
295
+ sorting: (prev.sorting ?? []).filter(
296
+ (entry: { id: string }) => entry.id !== columnId,
297
+ ),
298
+ }));
299
+ closeMenus();
300
+ }
301
+
302
+ function groupByColumnFromMenu(columnId: string) {
303
+ const current = ctx.grid.getState().grouping ?? [];
304
+ if (current.includes(columnId)) {
305
+ closeMenus();
306
+ return;
307
+ }
308
+ ctx.grid.setGrouping([...current, columnId]);
309
+ closeMenus();
310
+ }
311
+
312
+ function clearGroupingFromMenu(columnId: string) {
313
+ const current = ctx.grid.getState().grouping ?? [];
314
+ ctx.grid.setGrouping(current.filter((id: string) => id !== columnId));
315
+ closeMenus();
316
+ }
317
+
318
+ function isFacetChecked(columnId: string, value: string) {
319
+ const selected = ctx.valueFilters[columnId];
320
+ return selected ? selected.has(value) : true;
321
+ }
322
+
323
+ function toggleFacetValue(columnId: string, value: string) {
324
+ const allValues = ctx.columnMenuFacetValues;
325
+ const next = new Set(ctx.valueFilters[columnId] ?? allValues);
326
+ if (next.has(value)) next.delete(value);
327
+ else next.add(value);
328
+ if (next.size === allValues.length) {
329
+ const copy = { ...ctx.valueFilters };
330
+ delete copy[columnId];
331
+ ctx.valueFilters = copy;
332
+ } else {
333
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: next };
334
+ }
335
+ }
336
+
337
+ function isAllFacetsChecked(columnId: string) {
338
+ const selected = ctx.valueFilters[columnId];
339
+ return !selected || selected.size >= ctx.columnMenuFacetValues.length;
340
+ }
341
+
342
+ function toggleAllFacets(columnId: string) {
343
+ if (isAllFacetsChecked(columnId)) {
344
+ ctx.valueFilters = { ...ctx.valueFilters, [columnId]: new Set<string>() };
345
+ } else {
346
+ const copy = { ...ctx.valueFilters };
347
+ delete copy[columnId];
348
+ ctx.valueFilters = copy;
349
+ }
350
+ }
351
+
352
+ function clearColumnFilter(columnId: string) {
353
+ if (ctx.valueFilters[columnId]) {
354
+ const next = { ...ctx.valueFilters };
355
+ delete next[columnId];
356
+ ctx.valueFilters = next;
357
+ }
358
+ if (ctx.filterMenuValues[columnId]) {
359
+ const next = { ...ctx.filterMenuValues };
360
+ delete next[columnId];
361
+ ctx.filterMenuValues = next;
362
+ }
363
+ if (ctx.filterRowValues[columnId]) {
364
+ const next = { ...ctx.filterRowValues };
365
+ delete next[columnId];
366
+ ctx.filterRowValues = next;
367
+ }
368
+ }
369
+
370
+ function changePage(delta: number) {
371
+ ctx.grid.setPagination((prev: any) => ({
372
+ ...prev,
373
+ pageIndex: Math.max((prev?.pageIndex ?? 0) + delta, 0),
374
+ }));
375
+ }
376
+
377
+ function goToPage(pageIndex: number) {
378
+ ctx.grid.setPagination((prev: any) => ({
379
+ ...prev,
380
+ pageIndex: Math.max(0, pageIndex),
381
+ }));
382
+ }
383
+
384
+ function setPageSize(pageSize: number) {
385
+ ctx.grid.setPagination((prev: any) => {
386
+ const oldSize = prev?.pageSize ?? 10;
387
+ const oldIndex = prev?.pageIndex ?? 0;
388
+ // Keep the first visible row in view when the page size changes.
389
+ const firstVisibleRow = oldIndex * oldSize;
390
+ return {
391
+ ...prev,
392
+ pageSize,
393
+ pageIndex: Math.max(0, Math.floor(firstVisibleRow / pageSize)),
394
+ };
395
+ });
396
+ }
397
+
398
+ // ---- Right-click context menu --------------------------------------
399
+ type CtxTarget = { rowIndex: number; colIndex: number; columnId: string; rowId: string; row: TData | null };
400
+ type ResolvedItem = { key: string; label: string; separator?: boolean; disabled?: boolean; run?: () => void };
401
+
402
+ function insertRowAt(t: CtxTarget, offset: number) {
403
+ const data = ctx.internalData.slice() as TData[];
404
+ const idx = t.row ? data.indexOf(t.row) : -1;
405
+ const at = idx < 0 ? data.length : idx + offset;
406
+ data.splice(at, 0, {} as TData);
407
+ ctx.internalData = data;
408
+ }
409
+ function removeRowAt(t: CtxTarget) {
410
+ if (!t.row) return;
411
+ ctx.internalData = (ctx.internalData as TData[]).filter((r) => r !== t.row);
412
+ }
413
+ function removeColAt(t: CtxTarget) {
414
+ ctx.internalColumns = ctx.internalColumns.filter(
415
+ (d: any) => (d.id ?? d.field) !== t.columnId,
416
+ );
417
+ }
418
+
419
+ // Built-in items, keyed. `run` reads the live target passed at click time.
420
+ const builtins: Record<string, { label: string; run: (t: CtxTarget) => void }> = {
421
+ copy: { label: "Copy", run: () => ctx.copySelectionToClipboard() },
422
+ cut: { label: "Cut", run: () => ctx.cutSelectionToClipboard() },
423
+ paste: { label: "Paste", run: () => void ctx.pasteFromClipboard() },
424
+ clear: { label: "Clear", run: () => ctx.clearSelectedCells() },
425
+ row_above: { label: "Insert row above", run: (t) => insertRowAt(t, 0) },
426
+ row_below: { label: "Insert row below", run: (t) => insertRowAt(t, 1) },
427
+ remove_row: { label: "Remove row", run: removeRowAt },
428
+ remove_col: { label: "Remove column", run: removeColAt },
429
+ comment: { label: "Edit comment", run: (t) => openCommentEditor(t) },
430
+ };
431
+ const DEFAULT_ITEMS = [
432
+ "copy", "cut", "paste", "clear", "separator",
433
+ "row_above", "row_below", "remove_row", "separator", "remove_col",
434
+ ];
435
+
436
+ function openContextMenu(
437
+ event: MouseEvent,
438
+ rowIndex: number,
439
+ colIndex: number,
440
+ columnId: string,
441
+ ) {
442
+ if (!ctx.props.contextMenu) return;
443
+ event.preventDefault();
444
+ closeMenus();
445
+ ctx.contextMenuFor = null;
446
+ const rowModel = ctx.allRows[rowIndex];
447
+ const row = rowModel?.original ?? null;
448
+ const rowId = rowModel?.id ?? String(rowIndex);
449
+ ctx.contextMenuPos = {
450
+ x: clampMenuX(event.clientX, 220),
451
+ y: Math.max(8, Math.min(event.clientY, window.innerHeight - 16)),
452
+ };
453
+ ctx.contextMenuFor = { rowIndex, colIndex, columnId, rowId, row };
454
+ }
455
+
456
+ function closeContextMenu() {
457
+ ctx.contextMenuFor = null;
458
+ }
459
+
460
+ // ---- Editable comments ---------------------------------------------
461
+ function openCommentEditor(t: CtxTarget) {
462
+ const cur =
463
+ ctx.noteOverrides?.[t.rowId]?.[t.columnId] ??
464
+ ctx.props.notes?.[t.rowId]?.[t.columnId] ??
465
+ "";
466
+ ctx.commentDraft = cur;
467
+ ctx.commentEditFor = { rowId: t.rowId, columnId: t.columnId, x: ctx.contextMenuPos.x, y: ctx.contextMenuPos.y };
468
+ ctx.contextMenuFor = null;
469
+ }
470
+ function writeNote(rowId: string, columnId: string, note: string) {
471
+ const next = { ...ctx.noteOverrides };
472
+ next[rowId] = { ...(next[rowId] ?? {}), [columnId]: note };
473
+ ctx.noteOverrides = next;
474
+ ctx.props.onNoteChange?.({ rowId, columnId, note });
475
+ }
476
+ function saveComment() {
477
+ const e = ctx.commentEditFor;
478
+ if (!e) return;
479
+ writeNote(e.rowId, e.columnId, ctx.commentDraft);
480
+ ctx.commentEditFor = null;
481
+ }
482
+ function removeComment() {
483
+ const e = ctx.commentEditFor;
484
+ if (!e) return;
485
+ writeNote(e.rowId, e.columnId, "");
486
+ ctx.commentEditFor = null;
487
+ }
488
+ function closeCommentEditor() {
489
+ ctx.commentEditFor = null;
490
+ }
491
+
492
+ /** Resolve the configured items against the current target for rendering. */
493
+ function contextMenuItems(): ResolvedItem[] {
494
+ const target = ctx.contextMenuFor as CtxTarget | null;
495
+ if (!target) return [];
496
+ const cfg = ctx.props.contextMenu;
497
+ const entries: any[] = Array.isArray(cfg)
498
+ ? cfg
499
+ : [...DEFAULT_ITEMS, ...(ctx.props.editableComments ? ["separator", "comment"] : [])];
500
+ const out: ResolvedItem[] = [];
501
+ for (const entry of entries) {
502
+ if (entry === "separator") { out.push({ key: "sep" + out.length, label: "", separator: true }); continue; }
503
+ if (typeof entry === "string") {
504
+ const b = builtins[entry];
505
+ if (!b) continue;
506
+ out.push({ key: entry, label: b.label, run: () => b.run(target) });
507
+ continue;
508
+ }
509
+ if (entry.hidden?.(target)) continue;
510
+ out.push({
511
+ key: entry.key,
512
+ label: entry.label,
513
+ disabled: entry.disabled?.(target) ?? false,
514
+ run: () => entry.action(target),
515
+ });
516
+ }
517
+ // Drop leading/trailing/double separators.
518
+ return out.filter((it, i, a) =>
519
+ !it.separator || (i > 0 && i < a.length - 1 && !a[i - 1]?.separator),
520
+ );
521
+ }
522
+
523
+ return {
524
+ updateFilterRow,
525
+ updateFilterOperator,
526
+ updateFilterMenuValue,
527
+ updateFilterMenuValueTo,
528
+ toggleCheckboxWithKeyboard,
529
+ isColumnFiltered,
530
+ closeMenus,
531
+ openChooseColumns,
532
+ openColumnMenu,
533
+ openFilterMenu,
534
+ openOperatorMenu,
535
+ sortColumnFromMenu,
536
+ clearColumnSort,
537
+ groupByColumnFromMenu,
538
+ clearGroupingFromMenu,
539
+ isFacetChecked,
540
+ toggleFacetValue,
541
+ isAllFacetsChecked,
542
+ toggleAllFacets,
543
+ clearColumnFilter,
544
+ changePage,
545
+ goToPage,
546
+ setPageSize,
547
+ openContextMenu,
548
+ closeContextMenu,
549
+ contextMenuItems,
550
+ saveComment,
551
+ removeComment,
552
+ closeCommentEditor,
553
+ };
554
+ }