@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
@@ -0,0 +1,86 @@
1
+ import "./sv-grid-scrollbar";
2
+ export function createScrollSync(ctx) {
3
+ function showTooltipFor(el, text) {
4
+ if (!text)
5
+ return;
6
+ if (ctx.tooltipTimer)
7
+ window.clearTimeout(ctx.tooltipTimer);
8
+ ctx.tooltipTimer = window.setTimeout(() => {
9
+ const rect = el.getBoundingClientRect();
10
+ const vw = window.innerWidth;
11
+ const vh = window.innerHeight;
12
+ const below = rect.bottom + 64 < vh;
13
+ // Clamp x so the 280px-wide tooltip doesn't fall off the viewport.
14
+ const x = Math.min(Math.max(rect.left + 6, 10), vw - 290);
15
+ const y = below ? rect.bottom + 6 : rect.top - 6;
16
+ ctx.tooltip = { text, x, y, below };
17
+ }, 250);
18
+ }
19
+ function hideTooltip() {
20
+ if (ctx.tooltipTimer) {
21
+ window.clearTimeout(ctx.tooltipTimer);
22
+ ctx.tooltipTimer = null;
23
+ }
24
+ ctx.tooltip = null;
25
+ }
26
+ function flushScheduledScrollSync() {
27
+ ctx.scrollSyncRaf = null;
28
+ ctx.scrollVersion += 1;
29
+ if (ctx.rowVirtualizationEnabled)
30
+ ctx.virtualizer.setScrollOffset(ctx.domToLogicalRowOffset(ctx.pendingScrollTop));
31
+ if (ctx.columnVirtualizationEnabled)
32
+ ctx.columnVirtualizer.setHorizontalOffset(ctx.pendingScrollLeft);
33
+ }
34
+ /**
35
+ * Sync the virtualizer to the user's scroll position once per frame.
36
+ *
37
+ * The browser's smooth-scroll animation fires a `scroll` event on
38
+ * every frame of the ~250 ms ramp after each wheel notch - so a
39
+ * single wheel notch produces ~16 calls to `onBodyScroll`. Without
40
+ * batching, each one ran a full virtualizer recalc + Svelte re-render
41
+ * synchronously, which (a) made the work pile up on heavy demos and
42
+ * (b) read on screen as "the grid keeps scrolling after the wheel
43
+ * stopped" because each scroll event ticked the rendered window one
44
+ * row at a time over a quarter-second.
45
+ *
46
+ * Batching to a single rAF flush per frame means each animation
47
+ * frame produces ONE virtualizer update at the latest known scroll
48
+ * position. Smooth-scroll still feels smooth (the browser is still
49
+ * animating `scrollTop`), but the grid's per-frame cost is bounded
50
+ * and the visible rows match the current `scrollTop` exactly when
51
+ * the wheel stops - no trailing updates.
52
+ */
53
+ function scheduleScrollSync(scrollTop, scrollLeft) {
54
+ ctx.pendingScrollTop = scrollTop;
55
+ ctx.pendingScrollLeft = scrollLeft;
56
+ if (ctx.scrollSyncRaf !== null)
57
+ return;
58
+ ctx.scrollSyncRaf = requestAnimationFrame(flushScheduledScrollSync);
59
+ }
60
+ function onBodyScroll(event) {
61
+ const container = event.currentTarget;
62
+ if (!container)
63
+ return;
64
+ if (ctx.columnMenuFor || ctx.operatorMenuFor)
65
+ ctx.closeMenus();
66
+ scheduleScrollSync(container.scrollTop, container.scrollLeft);
67
+ if (ctx.props.onScrollBottomReached) {
68
+ const { scrollTop, scrollHeight, clientHeight } = container;
69
+ const atBottom = scrollTop + clientHeight >= scrollHeight - 32;
70
+ if (atBottom && ctx.scrollBottomArmed) {
71
+ ctx.scrollBottomArmed = false;
72
+ ctx.props.onScrollBottomReached({ scrollTop, scrollHeight, clientHeight });
73
+ }
74
+ else if (!atBottom && !ctx.scrollBottomArmed) {
75
+ ctx.scrollBottomArmed = true;
76
+ }
77
+ }
78
+ }
79
+ return {
80
+ showTooltipFor,
81
+ hideTooltip,
82
+ flushScheduledScrollSync,
83
+ scheduleScrollSync,
84
+ onBodyScroll,
85
+ };
86
+ }
@@ -0,0 +1,26 @@
1
+ import { type RowData, type TableFeatures } from "./index";
2
+ import "./sv-grid-scrollbar";
3
+ export declare function createSelection<TFeatures extends TableFeatures = TableFeatures, TData extends RowData = RowData>(ctx: any): {
4
+ isRowSelected: (rowId: string) => boolean;
5
+ toggleRowSelectionById: (rowId: string) => void;
6
+ toggleSelectAllRows: () => void;
7
+ setActiveCell: (rowIndex: number, colIndex: number) => void;
8
+ scrollActiveCellIntoView: (rowIndex: number, colIndex: number) => void;
9
+ setSelection: (rowIndex: number, colIndex: number) => void;
10
+ extendSelection: (rowIndex: number, colIndex: number) => void;
11
+ isCellInSelectedRange: (rowIndex: number, colIndex: number) => boolean;
12
+ getCellRangeEdges: (rowIndex: number, colIndex: number) => {
13
+ top: boolean;
14
+ bottom: boolean;
15
+ left: boolean;
16
+ right: boolean;
17
+ } | null;
18
+ isInFillPreview: (rowIndex: number, colIndex: number) => boolean;
19
+ findColumnById: (columnId: string) => any;
20
+ onCellPointerDown: (rowIndex: number, colIndex: number, event: PointerEvent) => void;
21
+ onCellPointerEnter: (rowIndex: number, colIndex: number) => void;
22
+ endDragSelection: () => void;
23
+ onWindowPointerMove: (event: PointerEvent) => void;
24
+ onCellClick: (rowIndex: number, colIndex: number) => void;
25
+ emitCellDoubleClick: (rowIndex: number, colIndex: number) => void;
26
+ };
@@ -0,0 +1,387 @@
1
+ // selection 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 { getGridCellDomId, } from "./index";
5
+ import "./sv-grid-scrollbar";
6
+ import { getColumnBaseValue, isGroupRow, } from "./cell-values";
7
+ export function createSelection(ctx) {
8
+ function isRowSelected(rowId) {
9
+ return Boolean(ctx.rowSelectionState[rowId]);
10
+ }
11
+ function toggleRowSelectionById(rowId) {
12
+ ctx.grid.setRowSelection((prev) => ({ ...prev, [rowId]: !prev[rowId] }));
13
+ }
14
+ function toggleSelectAllRows() {
15
+ const selectable = ctx.allRows.filter((row) => !isGroupRow(row));
16
+ const select = ctx.headerSelectionState !== "all";
17
+ ctx.grid.setRowSelection((prev) => {
18
+ const next = { ...prev };
19
+ for (const row of selectable) {
20
+ if (select)
21
+ next[row.id] = true;
22
+ else
23
+ delete next[row.id];
24
+ }
25
+ return next;
26
+ });
27
+ }
28
+ function setActiveCell(rowIndex, colIndex) {
29
+ ctx.userHasActivatedCell = true;
30
+ ctx.grid.setActiveCell({
31
+ rowIndex,
32
+ colIndex,
33
+ cellId: getGridCellDomId("svgrid", rowIndex, colIndex),
34
+ });
35
+ // Notify consumers (toolbars, ribbons) so they stay synced without
36
+ // having to listen on the DOM. Fired on EVERY active-cell move -
37
+ // click, arrow key, Tab, Enter, page-up/down, fill release.
38
+ if (ctx.props.onActiveCellChange) {
39
+ const column = ctx.allColumns[colIndex];
40
+ ctx.props.onActiveCellChange({
41
+ rowIndex,
42
+ colIndex,
43
+ columnId: column?.id ?? "",
44
+ });
45
+ }
46
+ }
47
+ function scrollActiveCellIntoView(rowIndex, colIndex) {
48
+ if (!ctx.scrollContainer)
49
+ return;
50
+ if (rowIndex < 0 || rowIndex >= ctx.allRows.length)
51
+ return;
52
+ if (colIndex < 0 || colIndex >= ctx.allColumns.length)
53
+ return;
54
+ if (ctx.rowVirtualizationEnabled) {
55
+ // Prefer the browser's native `scrollIntoView({ block: 'nearest' })`
56
+ // when the target row is already mounted. It does Excel-style
57
+ // minimum-scroll, respects the sticky thead via the
58
+ // `scroll-padding-top` we set on the scroll container, and runs
59
+ // on the compositor so it doesn't jump.
60
+ const root = ctx.scrollContainer;
61
+ const trEl = root.querySelector(`tr.sv-grid-row [data-svgrid-row="${rowIndex}"]`)?.closest('tr.sv-grid-row');
62
+ if (trEl) {
63
+ // `behavior: 'instant'` skips any user-agent smooth-scroll
64
+ // animation. Without it, fast key-repeat queues multiple
65
+ // overlapping animated scrolls and the viewport visibly
66
+ // overshoots / jumps as they collide.
67
+ trEl.scrollIntoView({
68
+ block: 'nearest',
69
+ inline: 'nearest',
70
+ behavior: 'instant',
71
+ });
72
+ }
73
+ else if (ctx.rowScrollScalingActive) {
74
+ // Huge-list scroll scaling: the logical row offset does NOT map 1:1
75
+ // to a DOM scrollTop (the DOM scroll range is capped), so the plain
76
+ // offset math below would clamp and never reach the target. Map the
77
+ // logical offset into DOM space and align the row just under the
78
+ // sticky header. Rows are sub-pixel tall in DOM space here, so exact
79
+ // minimum-scroll is meaningless; once the row mounts the native
80
+ // `scrollIntoView` path above fine-tunes on the next navigation.
81
+ const headerHeight = ctx.theadEl?.offsetHeight
82
+ ?? ctx.headerHeight ?? 0;
83
+ const rowLogicalTop = ctx.virtualizer.getOffsetForIndex(rowIndex);
84
+ const nextTop = Math.max(ctx.logicalToDomRowOffset(rowLogicalTop) - headerHeight, 0);
85
+ if (Math.abs(nextTop - root.scrollTop) > 0.5) {
86
+ root.scrollTop = nextTop;
87
+ }
88
+ }
89
+ else {
90
+ // Row outside the rendered virtualizer window. Compute the
91
+ // scroll-coordinate target manually and set scrollTop. On the
92
+ // next render the row will mount and subsequent navigations
93
+ // use the native path above.
94
+ const currentTop = root.scrollTop;
95
+ const clientHeight = root.clientHeight;
96
+ const headerHeight = ctx.theadEl?.offsetHeight
97
+ ?? ctx.headerHeight ?? 0;
98
+ const rowTopScroll = headerHeight + ctx.virtualizer.getOffsetForIndex(rowIndex);
99
+ const rowHeight = ctx.virtualizer.getSizeForIndex(rowIndex);
100
+ const rowBottom = rowTopScroll + rowHeight;
101
+ let nextTop = currentTop;
102
+ if (rowTopScroll < currentTop + headerHeight) {
103
+ nextTop = rowTopScroll - headerHeight;
104
+ }
105
+ else if (rowBottom > currentTop + clientHeight) {
106
+ nextTop = rowBottom - clientHeight;
107
+ }
108
+ nextTop = Math.max(nextTop, 0);
109
+ if (nextTop !== currentTop) {
110
+ root.scrollTop = nextTop;
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ // Non-virtualized mode: the cell's <td> is already in the DOM.
116
+ // Read its actual rect and bring it into view when it overlaps
117
+ // with the sticky header or is past the visible bottom. Without
118
+ // this branch, arrow keys move the active cell off-screen and
119
+ // the scrollbar never follows.
120
+ const cellEl = ctx.scrollContainer.querySelector(`td[data-svgrid-row="${rowIndex}"][data-svgrid-col="${colIndex}"]`);
121
+ if (cellEl) {
122
+ const headerHeight = ctx.theadEl?.offsetHeight ?? 0;
123
+ const containerRect = ctx.scrollContainer.getBoundingClientRect();
124
+ const cellRect = cellEl.getBoundingClientRect();
125
+ const cellTopInView = cellRect.top - containerRect.top;
126
+ const cellBotInView = cellRect.bottom - containerRect.top;
127
+ const clientHeight = ctx.scrollContainer.clientHeight;
128
+ let nextTop = ctx.scrollContainer.scrollTop;
129
+ if (cellTopInView < ctx.headerHeight) {
130
+ nextTop = ctx.scrollContainer.scrollTop + cellTopInView - ctx.headerHeight;
131
+ }
132
+ else if (cellBotInView > clientHeight) {
133
+ nextTop = ctx.scrollContainer.scrollTop + cellBotInView - clientHeight;
134
+ }
135
+ nextTop = Math.max(nextTop, 0);
136
+ if (nextTop !== ctx.scrollContainer.scrollTop) {
137
+ ctx.scrollContainer.scrollTop = nextTop;
138
+ }
139
+ }
140
+ }
141
+ // Prefer the rendered item (cached size from the layout pass).
142
+ // Fall back to the column virtualizer's offset helper - that
143
+ // handles per-column variable widths correctly, unlike the
144
+ // previous flat `colIndex * fallbackWidth` estimate.
145
+ const item = ctx.renderedColumnItems.find((entry) => entry.index === colIndex);
146
+ const fallbackWidth = ctx.props.columnWidth ?? 140;
147
+ const cellStart = item?.start ?? ctx.columnVirtualizer?.getOffsetForIndex?.(colIndex) ?? (colIndex * fallbackWidth);
148
+ const cellSize = item?.size ?? ctx.columnVirtualizer?.getSizeForIndex?.(colIndex) ?? fallbackWidth;
149
+ const cellEnd = cellStart + cellSize;
150
+ const viewStart = ctx.scrollContainer.scrollLeft;
151
+ const viewEnd = viewStart + ctx.scrollContainer.clientWidth;
152
+ if (cellStart < viewStart) {
153
+ ctx.scrollContainer.scrollLeft = cellStart;
154
+ }
155
+ else if (cellEnd > viewEnd) {
156
+ ctx.scrollContainer.scrollLeft = cellEnd - ctx.scrollContainer.clientWidth;
157
+ }
158
+ // No inline scrollVersion bump - the `scroll` event triggers
159
+ // onBodyScroll which flushes via rAF, doing one batched update.
160
+ }
161
+ function setSelection(rowIndex, colIndex) {
162
+ if (!ctx.enableCellSelectionEffective)
163
+ return;
164
+ const point = { rowIndex, colIndex };
165
+ ctx.selectionRange = { anchor: point, focus: point };
166
+ }
167
+ function extendSelection(rowIndex, colIndex) {
168
+ if (!ctx.enableCellSelectionEffective)
169
+ return;
170
+ const anchor = ctx.selectionRange.anchor ?? { rowIndex, colIndex };
171
+ ctx.selectionRange = { anchor, focus: { rowIndex, colIndex } };
172
+ }
173
+ function isCellInSelectedRange(rowIndex, colIndex) {
174
+ const anchor = ctx.selectionRange.anchor;
175
+ const focus = ctx.selectionRange.focus;
176
+ if (!anchor || !focus)
177
+ return false;
178
+ const minRow = Math.min(anchor.rowIndex, focus.rowIndex);
179
+ const maxRow = Math.max(anchor.rowIndex, focus.rowIndex);
180
+ const minCol = Math.min(anchor.colIndex, focus.colIndex);
181
+ const maxCol = Math.max(anchor.colIndex, focus.colIndex);
182
+ return (rowIndex >= minRow &&
183
+ rowIndex <= maxRow &&
184
+ colIndex >= minCol &&
185
+ colIndex <= maxCol);
186
+ }
187
+ /**
188
+ * Returns which sides of the selection rectangle a cell sits on, for the
189
+ * Excel-style outline. Returns null when the cell is outside the range.
190
+ */
191
+ function getCellRangeEdges(rowIndex, colIndex) {
192
+ const anchor = ctx.selectionRange.anchor;
193
+ const focus = ctx.selectionRange.focus;
194
+ if (!anchor || !focus)
195
+ return null;
196
+ const minRow = Math.min(anchor.rowIndex, focus.rowIndex);
197
+ const maxRow = Math.max(anchor.rowIndex, focus.rowIndex);
198
+ const minCol = Math.min(anchor.colIndex, focus.colIndex);
199
+ const maxCol = Math.max(anchor.colIndex, focus.colIndex);
200
+ if (rowIndex < minRow ||
201
+ rowIndex > maxRow ||
202
+ colIndex < minCol ||
203
+ colIndex > maxCol) {
204
+ return null;
205
+ }
206
+ return {
207
+ top: rowIndex === minRow,
208
+ bottom: rowIndex === maxRow,
209
+ left: colIndex === minCol,
210
+ right: colIndex === maxCol,
211
+ };
212
+ }
213
+ /** Returns true when the given cell is inside the fill-drag preview
214
+ * range BUT outside the original source range. Used to paint a
215
+ * dashed-outline preview while the user is dragging the handle. */
216
+ function isInFillPreview(rowIndex, colIndex) {
217
+ const d = ctx.fillDrag;
218
+ if (!d)
219
+ return false;
220
+ const minR = Math.min(d.sourceMinRow, d.targetRow);
221
+ const maxR = Math.max(d.sourceMaxRow, d.targetRow);
222
+ const minC = Math.min(d.sourceMinCol, d.targetCol);
223
+ const maxC = Math.max(d.sourceMaxCol, d.targetCol);
224
+ if (rowIndex < minR ||
225
+ rowIndex > maxR ||
226
+ colIndex < minC ||
227
+ colIndex > maxC)
228
+ return false;
229
+ const inSource = rowIndex >= d.sourceMinRow &&
230
+ rowIndex <= d.sourceMaxRow &&
231
+ colIndex >= d.sourceMinCol &&
232
+ colIndex <= d.sourceMaxCol;
233
+ return !inSource;
234
+ }
235
+ /** Look up a column by id without depending on `buildApi`'s private
236
+ * closure (those helpers don't exist at this scope). */
237
+ function findColumnById(columnId) {
238
+ return ctx.allColumns.find((c) => c.id === columnId);
239
+ }
240
+ function onCellPointerDown(rowIndex, colIndex, event) {
241
+ if (event.button !== 0)
242
+ return;
243
+ const row = ctx.allRows[rowIndex];
244
+ const column = ctx.allColumns[colIndex];
245
+ if (!row || !column || isGroupRow(row))
246
+ return;
247
+ const cellValue = ctx.getCellDisplayValue(row.id, column.id, getColumnBaseValue(row, column));
248
+ const isCheckboxColumn = column.columnDef.editorType === "checkbox" ||
249
+ typeof cellValue === "boolean";
250
+ if (isCheckboxColumn)
251
+ return; // let onCellClick toggle the checkbox
252
+ const active = ctx.grid.getState().activeCell;
253
+ ctx.activeAtPointerDown = active
254
+ ? { rowIndex: active.rowIndex, colIndex: active.colIndex }
255
+ : null;
256
+ if (event.shiftKey) {
257
+ extendSelection(rowIndex, colIndex);
258
+ setActiveCell(rowIndex, colIndex);
259
+ }
260
+ else {
261
+ setActiveCell(rowIndex, colIndex);
262
+ setSelection(rowIndex, colIndex);
263
+ ctx.isDraggingSelection = true;
264
+ }
265
+ }
266
+ function onCellPointerEnter(rowIndex, colIndex) {
267
+ if (!ctx.isDraggingSelection)
268
+ return;
269
+ const row = ctx.allRows[rowIndex];
270
+ if (!row || isGroupRow(row))
271
+ return;
272
+ extendSelection(rowIndex, colIndex);
273
+ setActiveCell(rowIndex, colIndex);
274
+ }
275
+ function endDragSelection() {
276
+ ctx.isDraggingSelection = false;
277
+ // Also commit a fill-handle drag if one was in progress - the user
278
+ // released the mouse, time to apply the pattern.
279
+ if (ctx.fillDrag)
280
+ ctx.onFillPointerUp();
281
+ }
282
+ function onWindowPointerMove(event) {
283
+ if (ctx.fillDrag) {
284
+ ctx.onFillPointerMove(event);
285
+ return;
286
+ }
287
+ if (!ctx.isDraggingSelection)
288
+ return;
289
+ // Safety: if no mouse button is held the drag is over (we may have
290
+ // missed pointerup because the user lifted outside the window).
291
+ if (event.buttons === 0) {
292
+ endDragSelection();
293
+ }
294
+ }
295
+ function onCellClick(rowIndex, colIndex) {
296
+ const row = ctx.allRows[rowIndex];
297
+ const column = ctx.allColumns[colIndex];
298
+ if (!row || !column)
299
+ return;
300
+ ctx.gridRootEl?.focus({ preventScroll: true });
301
+ if (isGroupRow(row)) {
302
+ setActiveCell(rowIndex, colIndex);
303
+ row.toggleExpanded?.();
304
+ return;
305
+ }
306
+ const baseValue = getColumnBaseValue(row, column);
307
+ const cellValue = ctx.getCellDisplayValue(row.id, column.id, baseValue);
308
+ // Emit the public click events for data cells/rows (before any
309
+ // checkbox-toggle / edit-entry side effects below).
310
+ ctx.props.onCellClick?.({
311
+ rowIndex,
312
+ colIndex,
313
+ columnId: column.id,
314
+ value: cellValue,
315
+ row: row.original,
316
+ });
317
+ ctx.props.onRowClick?.({
318
+ rowIndex,
319
+ columnId: column.id,
320
+ row: row.original,
321
+ });
322
+ const isCheckboxColumn = column.columnDef.editorType === "checkbox" ||
323
+ typeof cellValue === "boolean";
324
+ if (isCheckboxColumn) {
325
+ ctx.toggleBooleanCell(rowIndex, colIndex);
326
+ setActiveCell(rowIndex, colIndex);
327
+ setSelection(rowIndex, colIndex);
328
+ ctx.editingCell = null;
329
+ return;
330
+ }
331
+ // onCellPointerDown already set active+selection for data cells. Only
332
+ // decide whether to enter edit mode (click on the previously-active cell).
333
+ const wasActive = ctx.activeAtPointerDown !== null &&
334
+ ctx.activeAtPointerDown.rowIndex === rowIndex &&
335
+ ctx.activeAtPointerDown.colIndex === colIndex;
336
+ if (wasActive && ctx.editingEnabled) {
337
+ ctx.onCellDoubleClick(rowIndex, colIndex);
338
+ return;
339
+ }
340
+ ctx.editingCell = null;
341
+ }
342
+ /**
343
+ * Real double-click handler wired to the cell `ondblclick`. Emits the
344
+ * public double-click events (independent of editability) and then runs
345
+ * the edit-entry logic. Kept separate from `onCellDoubleClick` so the
346
+ * single-click-to-edit path in `onCellClick` does NOT emit a dblclick.
347
+ */
348
+ function emitCellDoubleClick(rowIndex, colIndex) {
349
+ const row = ctx.allRows[rowIndex];
350
+ const column = ctx.allColumns[colIndex];
351
+ if (row && column && !isGroupRow(row)) {
352
+ const value = ctx.getCellDisplayValue(row.id, column.id, getColumnBaseValue(row, column));
353
+ ctx.props.onCellDoubleClick?.({
354
+ rowIndex,
355
+ colIndex,
356
+ columnId: column.id,
357
+ value,
358
+ row: row.original,
359
+ });
360
+ ctx.props.onRowDoubleClick?.({
361
+ rowIndex,
362
+ columnId: column.id,
363
+ row: row.original,
364
+ });
365
+ }
366
+ ctx.onCellDoubleClick(rowIndex, colIndex);
367
+ }
368
+ return {
369
+ isRowSelected,
370
+ toggleRowSelectionById,
371
+ toggleSelectAllRows,
372
+ setActiveCell,
373
+ scrollActiveCellIntoView,
374
+ setSelection,
375
+ extendSelection,
376
+ isCellInSelectedRange,
377
+ getCellRangeEdges,
378
+ isInFillPreview,
379
+ findColumnById,
380
+ onCellPointerDown,
381
+ onCellPointerEnter,
382
+ endDragSelection,
383
+ onWindowPointerMove,
384
+ onCellClick,
385
+ emitCellDoubleClick,
386
+ };
387
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Spreadsheet-style layout helpers — cell merging + per-cell borders.
3
+ *
4
+ * These are *layout-only* augmentations on top of `<SvGrid>`. They don't
5
+ * change the grid's data model, sorting, or filtering — they just decorate
6
+ * the rendered body cells. Bind the action to the wrapper around your
7
+ * SvGrid and pass merge / border specs; the helper observes DOM mutations
8
+ * and re-applies the layout whenever the grid re-renders.
9
+ *
10
+ * Limitations (worth knowing up front):
11
+ * - Merges + borders index into the CURRENTLY-DISPLAYED rows (after
12
+ * sort / filter), via the `data-svgrid-row` index the grid sets on
13
+ * every body cell. If your data sorts or filters, recompute the
14
+ * specs against the new display order.
15
+ * - Merges use real `colspan` / `rowspan` plus `display:none` on
16
+ * covered TDs. Column widths come from the inline styles SvGrid
17
+ * emits on each TD; we don't fight those.
18
+ * - For long row-merges across a virtualised window, only rows that
19
+ * are actually rendered get the rowspan applied. Disable
20
+ * virtualisation on grids that need a continuous merge.
21
+ */
22
+ /** A single edge of a cell border. */
23
+ export type BorderSpec = {
24
+ /** Thickness in pixels. Default 2. */
25
+ width?: number;
26
+ /** CSS border-style. Default 'solid'. */
27
+ style?: 'solid' | 'dashed' | 'dotted' | 'double';
28
+ /** CSS color. Falls back to currentColor (i.e. text color). */
29
+ color?: string;
30
+ };
31
+ /** A merge declaration. The cell at (rowIndex, columnId) is the ORIGIN;
32
+ * it spans `colspan` columns to the right + `rowspan` rows downward.
33
+ * Covered cells are hidden so the origin visually fills the region. */
34
+ export type MergeSpec = {
35
+ /** Display-row index — the same value the grid puts on
36
+ * `data-svgrid-row`. After sorting/filtering, recompute the spec
37
+ * against the new display order. */
38
+ rowIndex: number;
39
+ columnId: string;
40
+ /** Default 1. */
41
+ rowspan?: number;
42
+ /** Default 1. */
43
+ colspan?: number;
44
+ };
45
+ /** Borders for one cell. Edges left unset render as the default
46
+ * cell border (i.e. no override). */
47
+ export type CellBorderSpec = {
48
+ rowIndex: number;
49
+ columnId: string;
50
+ top?: BorderSpec;
51
+ right?: BorderSpec;
52
+ bottom?: BorderSpec;
53
+ left?: BorderSpec;
54
+ };
55
+ /** What the Svelte action receives. Pass new values to update; pass
56
+ * `null` / empty arrays to clear. */
57
+ export type SpreadsheetActionOptions = {
58
+ merges?: ReadonlyArray<MergeSpec> | null;
59
+ borders?: ReadonlyArray<CellBorderSpec> | null;
60
+ /** Column id order the grid uses, in left-to-right order. Required
61
+ * to translate `colspan` into the right set of covered column ids.
62
+ * Pass `columns.map((c) => c.id)` from the consumer. */
63
+ columnOrder: ReadonlyArray<string>;
64
+ };
65
+ /** Svelte action. Attach to the element that hosts your `<SvGrid>` so
66
+ * the action can watch its DOM for re-renders.
67
+ *
68
+ * ```svelte
69
+ * <div use:spreadsheetLayout={{ merges, borders, columnOrder }}>
70
+ * <SvGrid {data} {columns} ... />
71
+ * </div>
72
+ * ```
73
+ *
74
+ * The action re-applies the layout whenever the grid's body mutates
75
+ * (new rows, column reorder, virtualization scroll) and whenever the
76
+ * options change. */
77
+ export declare function spreadsheetLayout(node: HTMLElement, opts: SpreadsheetActionOptions): {
78
+ update(next: SpreadsheetActionOptions): void;
79
+ destroy(): void;
80
+ };