@svgrid/grid 1.0.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 (213) hide show
  1. package/LICENSE +33 -0
  2. package/README.md +39 -0
  3. package/dist/FlexRender.svelte +96 -0
  4. package/dist/FlexRender.svelte.d.ts +49 -0
  5. package/dist/SvGrid.svelte +8742 -0
  6. package/dist/SvGrid.svelte.d.ts +381 -0
  7. package/dist/SvGridChart.svelte +653 -0
  8. package/dist/SvGridChart.svelte.d.ts +25 -0
  9. package/dist/SvGridChart.test.d.ts +1 -0
  10. package/dist/SvGridChart.test.js +62 -0
  11. package/dist/SvGridDropdown.svelte +675 -0
  12. package/dist/SvGridDropdown.svelte.d.ts +40 -0
  13. package/dist/a11y.contract.test.d.ts +1 -0
  14. package/dist/a11y.contract.test.js +39 -0
  15. package/dist/a11y.d.ts +38 -0
  16. package/dist/a11y.js +37 -0
  17. package/dist/a11y.test.d.ts +1 -0
  18. package/dist/a11y.test.js +44 -0
  19. package/dist/cell-formatting.d.ts +17 -0
  20. package/dist/cell-formatting.js +141 -0
  21. package/dist/cell-formatting.test.d.ts +1 -0
  22. package/dist/cell-formatting.test.js +234 -0
  23. package/dist/chart-export.d.ts +14 -0
  24. package/dist/chart-export.js +112 -0
  25. package/dist/chart.d.ts +226 -0
  26. package/dist/chart.js +732 -0
  27. package/dist/chart.test.d.ts +1 -0
  28. package/dist/chart.test.js +289 -0
  29. package/dist/collaboration.d.ts +74 -0
  30. package/dist/collaboration.js +98 -0
  31. package/dist/collaboration.test.d.ts +1 -0
  32. package/dist/collaboration.test.js +65 -0
  33. package/dist/conditional-formatting.d.ts +91 -0
  34. package/dist/conditional-formatting.js +170 -0
  35. package/dist/conditional-formatting.test.d.ts +1 -0
  36. package/dist/conditional-formatting.test.js +87 -0
  37. package/dist/core.coverage.test.d.ts +1 -0
  38. package/dist/core.coverage.test.js +186 -0
  39. package/dist/core.d.ts +370 -0
  40. package/dist/core.js +568 -0
  41. package/dist/core.performance.test.d.ts +1 -0
  42. package/dist/core.performance.test.js +29 -0
  43. package/dist/createGrid.svelte.d.ts +6 -0
  44. package/dist/createGrid.svelte.js +17 -0
  45. package/dist/createGrid.test.d.ts +1 -0
  46. package/dist/createGrid.test.js +9 -0
  47. package/dist/createGridState.svelte.d.ts +3 -0
  48. package/dist/createGridState.svelte.js +13 -0
  49. package/dist/editors/cell-editors.d.ts +23 -0
  50. package/dist/editors/cell-editors.js +97 -0
  51. package/dist/editors/cell-editors.test.d.ts +1 -0
  52. package/dist/editors/cell-editors.test.js +75 -0
  53. package/dist/fill-patterns.d.ts +30 -0
  54. package/dist/fill-patterns.js +207 -0
  55. package/dist/fill-patterns.test.d.ts +1 -0
  56. package/dist/fill-patterns.test.js +81 -0
  57. package/dist/filtering/excel-filters.d.ts +26 -0
  58. package/dist/filtering/excel-filters.js +64 -0
  59. package/dist/filtering/excel-filters.test.d.ts +1 -0
  60. package/dist/filtering/excel-filters.test.js +116 -0
  61. package/dist/filtering/locale-filter.test.d.ts +13 -0
  62. package/dist/filtering/locale-filter.test.js +189 -0
  63. package/dist/flex-render.d.ts +1 -0
  64. package/dist/flex-render.js +2 -0
  65. package/dist/flex-render.test.d.ts +1 -0
  66. package/dist/flex-render.test.js +142 -0
  67. package/dist/group-aggregate.test.d.ts +1 -0
  68. package/dist/group-aggregate.test.js +32 -0
  69. package/dist/index.d.ts +29 -0
  70. package/dist/index.js +27 -0
  71. package/dist/keyboard.d.ts +8 -0
  72. package/dist/keyboard.js +91 -0
  73. package/dist/keyboard.test.d.ts +1 -0
  74. package/dist/keyboard.test.js +27 -0
  75. package/dist/merge-objects.d.ts +9 -0
  76. package/dist/merge-objects.js +28 -0
  77. package/dist/named-views.d.ts +42 -0
  78. package/dist/named-views.js +80 -0
  79. package/dist/named-views.test.d.ts +1 -0
  80. package/dist/named-views.test.js +57 -0
  81. package/dist/new-features.test.d.ts +1 -0
  82. package/dist/new-features.test.js +183 -0
  83. package/dist/render-component.d.ts +13 -0
  84. package/dist/render-component.js +14 -0
  85. package/dist/render-component.test.d.ts +1 -0
  86. package/dist/render-component.test.js +41 -0
  87. package/dist/server-data-source.d.ts +76 -0
  88. package/dist/server-data-source.js +83 -0
  89. package/dist/server-data-source.test.d.ts +1 -0
  90. package/dist/server-data-source.test.js +81 -0
  91. package/dist/sparkline.d.ts +56 -0
  92. package/dist/sparkline.js +97 -0
  93. package/dist/sparkline.test.d.ts +1 -0
  94. package/dist/sparkline.test.js +52 -0
  95. package/dist/static-functions.d.ts +1 -0
  96. package/dist/static-functions.js +1 -0
  97. package/dist/subscribe.d.ts +9 -0
  98. package/dist/subscribe.js +31 -0
  99. package/dist/subscribe.test.d.ts +1 -0
  100. package/dist/subscribe.test.js +93 -0
  101. package/dist/sv-grid-scrollbar.d.ts +1 -0
  102. package/dist/sv-grid-scrollbar.js +310 -0
  103. package/dist/svgrid-wrapper.types.d.ts +341 -0
  104. package/dist/svgrid-wrapper.types.js +1 -0
  105. package/dist/svgrid.api-extensions.test.d.ts +14 -0
  106. package/dist/svgrid.api-extensions.test.js +334 -0
  107. package/dist/svgrid.api.test.d.ts +15 -0
  108. package/dist/svgrid.api.test.js +169 -0
  109. package/dist/svgrid.behavior.test.d.ts +15 -0
  110. package/dist/svgrid.behavior.test.js +605 -0
  111. package/dist/svgrid.column-reorder.test.d.ts +15 -0
  112. package/dist/svgrid.column-reorder.test.js +224 -0
  113. package/dist/svgrid.features.test.d.ts +1 -0
  114. package/dist/svgrid.features.test.js +129 -0
  115. package/dist/svgrid.interaction.test.d.ts +9 -0
  116. package/dist/svgrid.interaction.test.js +318 -0
  117. package/dist/svgrid.locale-filtering.test.d.ts +15 -0
  118. package/dist/svgrid.locale-filtering.test.js +261 -0
  119. package/dist/svgrid.new-features.wrapper.test.d.ts +1 -0
  120. package/dist/svgrid.new-features.wrapper.test.js +136 -0
  121. package/dist/svgrid.row-pinning.test.d.ts +14 -0
  122. package/dist/svgrid.row-pinning.test.js +250 -0
  123. package/dist/svgrid.set-filter.test.d.ts +17 -0
  124. package/dist/svgrid.set-filter.test.js +268 -0
  125. package/dist/svgrid.wrapper.test.d.ts +1 -0
  126. package/dist/svgrid.wrapper.test.js +33 -0
  127. package/dist/test-setup.d.ts +1 -0
  128. package/dist/test-setup.js +29 -0
  129. package/dist/transaction.test.d.ts +1 -0
  130. package/dist/transaction.test.js +89 -0
  131. package/dist/virtualization/column-virtualizer.d.ts +20 -0
  132. package/dist/virtualization/column-virtualizer.js +19 -0
  133. package/dist/virtualization/column-virtualizer.test.d.ts +1 -0
  134. package/dist/virtualization/column-virtualizer.test.js +25 -0
  135. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +11 -0
  136. package/dist/virtualization/svelte-virtualizer.svelte.js +20 -0
  137. package/dist/virtualization/types.d.ts +28 -0
  138. package/dist/virtualization/types.js +1 -0
  139. package/dist/virtualization/virtualizer.d.ts +13 -0
  140. package/dist/virtualization/virtualizer.js +232 -0
  141. package/dist/virtualization/virtualizer.test.d.ts +1 -0
  142. package/dist/virtualization/virtualizer.test.js +41 -0
  143. package/package.json +69 -0
  144. package/src/FlexRender.svelte +96 -0
  145. package/src/SvGrid.svelte +8742 -0
  146. package/src/SvGridChart.svelte +653 -0
  147. package/src/SvGridChart.test.ts +69 -0
  148. package/src/SvGridDropdown.svelte +675 -0
  149. package/src/a11y.contract.test.ts +49 -0
  150. package/src/a11y.test.ts +59 -0
  151. package/src/a11y.ts +59 -0
  152. package/src/cell-formatting.test.ts +273 -0
  153. package/src/cell-formatting.ts +169 -0
  154. package/src/chart-export.ts +144 -0
  155. package/src/chart.test.ts +311 -0
  156. package/src/chart.ts +934 -0
  157. package/src/collaboration.test.ts +74 -0
  158. package/src/collaboration.ts +161 -0
  159. package/src/conditional-formatting.test.ts +107 -0
  160. package/src/conditional-formatting.ts +239 -0
  161. package/src/core.coverage.test.ts +230 -0
  162. package/src/core.performance.test.ts +30 -0
  163. package/src/core.ts +991 -0
  164. package/src/createGrid.svelte.ts +42 -0
  165. package/src/createGrid.test.ts +10 -0
  166. package/src/createGridState.svelte.ts +17 -0
  167. package/src/editors/cell-editors.test.ts +80 -0
  168. package/src/editors/cell-editors.ts +127 -0
  169. package/src/fill-patterns.test.ts +93 -0
  170. package/src/fill-patterns.ts +229 -0
  171. package/src/filtering/excel-filters.test.ts +131 -0
  172. package/src/filtering/excel-filters.ts +101 -0
  173. package/src/filtering/locale-filter.test.ts +220 -0
  174. package/src/flex-render.test.ts +143 -0
  175. package/src/flex-render.ts +3 -0
  176. package/src/group-aggregate.test.ts +48 -0
  177. package/src/index.ts +159 -0
  178. package/src/keyboard.test.ts +59 -0
  179. package/src/keyboard.ts +97 -0
  180. package/src/merge-objects.ts +48 -0
  181. package/src/named-views.test.ts +66 -0
  182. package/src/named-views.ts +120 -0
  183. package/src/new-features.test.ts +217 -0
  184. package/src/render-component.test.ts +51 -0
  185. package/src/render-component.ts +28 -0
  186. package/src/server-data-source.test.ts +88 -0
  187. package/src/server-data-source.ts +163 -0
  188. package/src/sparkline.test.ts +59 -0
  189. package/src/sparkline.ts +164 -0
  190. package/src/static-functions.ts +11 -0
  191. package/src/subscribe.test.ts +103 -0
  192. package/src/subscribe.ts +38 -0
  193. package/src/sv-grid-scrollbar.ts +347 -0
  194. package/src/svgrid-wrapper.types.ts +382 -0
  195. package/src/svgrid.api-extensions.test.ts +362 -0
  196. package/src/svgrid.api.test.ts +192 -0
  197. package/src/svgrid.behavior.test.ts +657 -0
  198. package/src/svgrid.column-reorder.test.ts +234 -0
  199. package/src/svgrid.features.test.ts +157 -0
  200. package/src/svgrid.interaction.test.ts +355 -0
  201. package/src/svgrid.locale-filtering.test.ts +259 -0
  202. package/src/svgrid.new-features.wrapper.test.ts +164 -0
  203. package/src/svgrid.row-pinning.test.ts +266 -0
  204. package/src/svgrid.set-filter.test.ts +270 -0
  205. package/src/svgrid.wrapper.test.ts +35 -0
  206. package/src/test-setup.ts +37 -0
  207. package/src/transaction.test.ts +100 -0
  208. package/src/virtualization/column-virtualizer.test.ts +27 -0
  209. package/src/virtualization/column-virtualizer.ts +30 -0
  210. package/src/virtualization/svelte-virtualizer.svelte.ts +24 -0
  211. package/src/virtualization/types.ts +30 -0
  212. package/src/virtualization/virtualizer.test.ts +47 -0
  213. package/src/virtualization/virtualizer.ts +270 -0
package/src/index.ts ADDED
@@ -0,0 +1,159 @@
1
+ export { formatNumericWithConfig, resolveDatePattern } from './cell-formatting'
2
+
3
+ export {
4
+ columnFilteringFeature,
5
+ columnGroupingFeature,
6
+ createCoreRowModel,
7
+ createExpandedRowModel,
8
+ createFilteredRowModel,
9
+ createGroupedRowModel,
10
+ createPaginatedRowModel,
11
+ createSortedRowModel,
12
+ applyGroupAggregate,
13
+ filterFns,
14
+ rowExpandingFeature,
15
+ rowPaginationFeature,
16
+ rowSelectionFeature,
17
+ rowSortingFeature,
18
+ sortFns,
19
+ tableFeatures,
20
+ type Cell,
21
+ type CellContext,
22
+ type EditorContext,
23
+ type CellData,
24
+ type ActiveCellState,
25
+ type CellFormatConfig,
26
+ type CellFormatter,
27
+ type Column,
28
+ type ColumnDef,
29
+ type ColumnDefTemplate,
30
+ type GroupAggregator,
31
+ type Header,
32
+ type HeaderContext,
33
+ type HeaderGroup,
34
+ type Row,
35
+ type RowData,
36
+ type SortingState,
37
+ type SvGrid as SvGridInstance,
38
+ type SvGridOptions,
39
+ type TableFeatures,
40
+ type Updater,
41
+ } from './core'
42
+
43
+ export { createGrid, createSvGrid, type SvelteGrid } from './createGrid.svelte'
44
+ export { createGridState, createSvGridState } from './createGridState.svelte'
45
+ export { subscribeGrid, subscribeSvGrid } from './subscribe'
46
+ export { default as SvGrid } from './SvGrid.svelte'
47
+ export { default as FlexRender } from './FlexRender.svelte'
48
+ export { renderComponent, renderSnippet } from './render-component'
49
+ export { default as SvGridChart } from './SvGridChart.svelte'
50
+ export {
51
+ buildChart,
52
+ rowsToChartSpec,
53
+ niceScale,
54
+ DEFAULT_PALETTE,
55
+ type ChartType,
56
+ type ChartSpec,
57
+ type ChartSeries,
58
+ type ChartGeometry,
59
+ type ChartBar,
60
+ type ChartLine,
61
+ type ChartLinePoint,
62
+ type ChartPieSlice,
63
+ type ChartSelection,
64
+ type ChartReferenceLine,
65
+ type ChartRefLineGeo,
66
+ type ChartRefLineGeoV,
67
+ type ChartScatterDot,
68
+ type ScatterPoint,
69
+ type NiceScale,
70
+ } from './chart'
71
+ export {
72
+ chartToSvgString,
73
+ downloadChartSvg,
74
+ chartToPngBlob,
75
+ downloadChartPng,
76
+ type ChartExportOptions,
77
+ } from './chart-export'
78
+ export {
79
+ buildSparkline,
80
+ toSparklineValues,
81
+ type SparklineConfig,
82
+ type SparklineType,
83
+ type SparklineGeometry,
84
+ } from './sparkline'
85
+ export {
86
+ createCollaboration,
87
+ broadcastChannelTransport,
88
+ type CollabUser,
89
+ type CollabCell,
90
+ type CollabPresence,
91
+ type CollabMessage,
92
+ type CollabTransport,
93
+ type Collaboration,
94
+ } from './collaboration'
95
+ export {
96
+ createServerDataSource,
97
+ type ServerDataSource,
98
+ type ServerRequest,
99
+ type ServerResult,
100
+ type ServerController,
101
+ type ServerState,
102
+ type ServerSortModel,
103
+ type ServerFilterModel,
104
+ } from './server-data-source'
105
+ export {
106
+ createNamedViews,
107
+ memoryViews,
108
+ localStorageViews,
109
+ type SavedView,
110
+ type ViewStorage,
111
+ type NamedViews,
112
+ } from './named-views'
113
+ export {
114
+ resolveCellFormat,
115
+ computeColumnStat,
116
+ lerpColor,
117
+ contrastText,
118
+ type ConditionalFormat,
119
+ type ConditionalFormatSpec,
120
+ type ColorScaleFormat,
121
+ type DataBarFormat,
122
+ type IconSetFormat,
123
+ type RuleFormat,
124
+ type ResolvedCellFormat,
125
+ } from './conditional-formatting'
126
+ export { getKeyboardIntent, getNextActiveCell, type GridKeyboardIntent } from './keyboard'
127
+ export { createVirtualizer } from './virtualization/virtualizer'
128
+ export { createSvelteVirtualizer } from './virtualization/svelte-virtualizer.svelte'
129
+ export { createColumnVirtualizer } from './virtualization/column-virtualizer'
130
+ export type { VirtualItem, VirtualizerOptions, VirtualizerState } from './virtualization/types'
131
+ export type { SvGridApi, SvGridFilterOperator, SvGridWrapperProps } from './svgrid-wrapper.types'
132
+ export {
133
+ parseEditorValue,
134
+ normalizeEditorOptions,
135
+ type CellEditorType,
136
+ type CellEditorOption,
137
+ } from './editors/cell-editors'
138
+ export {
139
+ applyExcelFilter,
140
+ normalizeForFilter,
141
+ type ExcelFilter,
142
+ type ExcelFilterOperator,
143
+ type ExcelFilterOptions,
144
+ } from './filtering/excel-filters'
145
+ export {
146
+ getGridCellDomId,
147
+ getGridCellA11yProps,
148
+ getGridHeaderA11yProps,
149
+ getGridRootA11yProps,
150
+ getGridRowA11yProps,
151
+ type GridCellA11yInput,
152
+ type GridColumnA11yInput,
153
+ type GridSortDirection,
154
+ } from './a11y'
155
+
156
+ /**
157
+ * Compatibility alias for teams migrating from `createTable`.
158
+ */
159
+ export { createSvGrid as createTable } from './createGrid.svelte'
@@ -0,0 +1,59 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { getKeyboardIntent, getNextActiveCell } from './keyboard'
3
+
4
+ describe('keyboard navigation engine', () => {
5
+ it('maps keys to navigation intents', () => {
6
+ expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: 'ArrowLeft' }))).toBe(
7
+ 'moveLeft',
8
+ )
9
+ expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: 'Home', ctrlKey: true }))).toBe(
10
+ 'gridStart',
11
+ )
12
+ expect(getKeyboardIntent(new KeyboardEvent('keydown', { key: ' ' }))).toBe(
13
+ 'activate',
14
+ )
15
+ })
16
+
17
+ it('computes next active cell within bounds', () => {
18
+ const next = getNextActiveCell(
19
+ { rowIndex: 1, colIndex: 1, cellId: null },
20
+ 'moveDown',
21
+ { maxRow: 2, maxCol: 3, pageSize: 10 },
22
+ )
23
+ expect(next.rowIndex).toBe(2)
24
+ expect(next.colIndex).toBe(1)
25
+ })
26
+
27
+ it('handles row and grid boundary intents', () => {
28
+ const rowEnd = getNextActiveCell(
29
+ { rowIndex: 2, colIndex: 1, cellId: null },
30
+ 'rowEnd',
31
+ { maxRow: 2, maxCol: 4, pageSize: 2 },
32
+ )
33
+ expect(rowEnd.colIndex).toBe(4)
34
+
35
+ const gridStart = getNextActiveCell(
36
+ { rowIndex: 2, colIndex: 4, cellId: null },
37
+ 'gridStart',
38
+ { maxRow: 2, maxCol: 4, pageSize: 2 },
39
+ )
40
+ expect(gridStart.rowIndex).toBe(0)
41
+ expect(gridStart.colIndex).toBe(0)
42
+ })
43
+
44
+ it('clamps page navigation to valid bounds', () => {
45
+ const pageUp = getNextActiveCell(
46
+ { rowIndex: 1, colIndex: 0, cellId: null },
47
+ 'pageUp',
48
+ { maxRow: 20, maxCol: 2, pageSize: 10 },
49
+ )
50
+ const pageDown = getNextActiveCell(
51
+ { rowIndex: 18, colIndex: 0, cellId: null },
52
+ 'pageDown',
53
+ { maxRow: 20, maxCol: 2, pageSize: 10 },
54
+ )
55
+
56
+ expect(pageUp.rowIndex).toBe(0)
57
+ expect(pageDown.rowIndex).toBe(20)
58
+ })
59
+ })
@@ -0,0 +1,97 @@
1
+ import type { ActiveCellState } from './core'
2
+
3
+ export type GridKeyboardIntent =
4
+ | 'moveLeft'
5
+ | 'moveRight'
6
+ | 'moveUp'
7
+ | 'moveDown'
8
+ | 'tabNext'
9
+ | 'tabPrev'
10
+ | 'rowStart'
11
+ | 'rowEnd'
12
+ | 'gridStart'
13
+ | 'gridEnd'
14
+ | 'pageUp'
15
+ | 'pageDown'
16
+ | 'activate'
17
+ | 'clearCells'
18
+ | 'noop'
19
+
20
+ export function getKeyboardIntent(event: KeyboardEvent): GridKeyboardIntent {
21
+ // Ctrl+Home / Ctrl+End come before plain Home / End so the modifier wins.
22
+ if (event.ctrlKey && event.key === 'Home') return 'gridStart'
23
+ if (event.ctrlKey && event.key === 'End') return 'gridEnd'
24
+
25
+ if (event.key === 'ArrowLeft') return 'moveLeft'
26
+ if (event.key === 'ArrowRight') return 'moveRight'
27
+ if (event.key === 'ArrowUp') return 'moveUp'
28
+ if (event.key === 'ArrowDown') return 'moveDown'
29
+ if (event.key === 'Home') return 'rowStart'
30
+ if (event.key === 'End') return 'rowEnd'
31
+ if (event.key === 'PageUp') return 'pageUp'
32
+ if (event.key === 'PageDown') return 'pageDown'
33
+
34
+ // Excel-style data-entry navigation:
35
+ // Enter → move down (Shift+Enter → up)
36
+ // Tab → move right (Shift+Tab → left), wraps at row boundaries
37
+ // Delete → clear contents of the selected cells
38
+ // F2 / Space → start editing the active cell
39
+ if (event.key === 'Enter') return event.shiftKey ? 'moveUp' : 'moveDown'
40
+ if (event.key === 'Tab') return event.shiftKey ? 'tabPrev' : 'tabNext'
41
+ if (event.key === 'F2') return 'activate'
42
+ if (event.key === ' ') return 'activate'
43
+ if (event.key === 'Delete') return 'clearCells'
44
+
45
+ return 'noop'
46
+ }
47
+
48
+ export function getNextActiveCell(
49
+ current: ActiveCellState,
50
+ intent: GridKeyboardIntent,
51
+ bounds: { maxRow: number; maxCol: number; pageSize?: number },
52
+ ): ActiveCellState {
53
+ const pageSize = bounds.pageSize ?? 10
54
+ let rowIndex = current.rowIndex
55
+ let colIndex = current.colIndex
56
+
57
+ if (intent === 'moveLeft') colIndex -= 1
58
+ if (intent === 'moveRight') colIndex += 1
59
+ if (intent === 'moveUp') rowIndex -= 1
60
+ if (intent === 'moveDown') rowIndex += 1
61
+ if (intent === 'tabNext') {
62
+ // Right one column; if we fall off the right edge, wrap to the
63
+ // first column of the next row - Excel's "data entry" behavior.
64
+ colIndex += 1
65
+ if (colIndex > bounds.maxCol) {
66
+ colIndex = 0
67
+ rowIndex += 1
68
+ }
69
+ }
70
+ if (intent === 'tabPrev') {
71
+ colIndex -= 1
72
+ if (colIndex < 0) {
73
+ colIndex = bounds.maxCol
74
+ rowIndex -= 1
75
+ }
76
+ }
77
+ if (intent === 'rowStart') colIndex = 0
78
+ if (intent === 'rowEnd') colIndex = bounds.maxCol
79
+ if (intent === 'gridStart') {
80
+ rowIndex = 0
81
+ colIndex = 0
82
+ }
83
+ if (intent === 'gridEnd') {
84
+ rowIndex = bounds.maxRow
85
+ colIndex = bounds.maxCol
86
+ }
87
+ if (intent === 'pageUp') rowIndex -= pageSize
88
+ if (intent === 'pageDown') rowIndex += pageSize
89
+
90
+ rowIndex = Math.min(Math.max(rowIndex, 0), Math.max(bounds.maxRow, 0))
91
+ colIndex = Math.min(Math.max(colIndex, 0), Math.max(bounds.maxCol, 0))
92
+ return {
93
+ rowIndex,
94
+ colIndex,
95
+ cellId: current.cellId,
96
+ }
97
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Merges objects together while keeping their getters alive.
3
+ * Taken from SolidJS:
4
+ * https://github.com/solidjs/solid/blob/24abc825c0996fd2bc8c1de1491efe9a7e743aff/packages/solid/src/server/rendering.ts#L82-L115
5
+ */
6
+ export function mergeObjects<T>(source: T): T
7
+ export function mergeObjects<T, U>(source: T, source1: U): T & U
8
+ export function mergeObjects<T, U, V>(
9
+ source: T,
10
+ source1: U,
11
+ source2: V,
12
+ ): T & U & V
13
+ export function mergeObjects<T, U, V, W>(
14
+ source: T,
15
+ source1: U,
16
+ source2: V,
17
+ source3: W,
18
+ ): T & U & V & W
19
+ export function mergeObjects(...sources: any): any {
20
+ const target = {}
21
+
22
+ for (let source of sources) {
23
+ if (typeof source === 'function') source = source()
24
+
25
+ if (source) {
26
+ const descriptors = Object.getOwnPropertyDescriptors(source)
27
+
28
+ for (const key in descriptors) {
29
+ if (key in target) continue
30
+
31
+ Object.defineProperty(target, key, {
32
+ enumerable: true,
33
+ get() {
34
+ for (let i = sources.length - 1; i >= 0; i--) {
35
+ let scopedSource = sources[i]
36
+ if (typeof scopedSource === 'function') scopedSource = scopedSource()
37
+
38
+ const value = (scopedSource || {})[key]
39
+ if (value !== undefined) return value
40
+ }
41
+ },
42
+ })
43
+ }
44
+ }
45
+ }
46
+
47
+ return target
48
+ }
@@ -0,0 +1,66 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { createNamedViews, memoryViews } from './named-views'
3
+ import type { SvGridViewState } from './svgrid-wrapper.types'
4
+
5
+ function fakeHost(initial: Partial<SvGridViewState> = {}) {
6
+ let state = { sorting: [], columnFilters: [], ...initial } as SvGridViewState
7
+ return {
8
+ getState: () => state,
9
+ setState: vi.fn((s: Partial<SvGridViewState>) => {
10
+ state = { ...state, ...s }
11
+ }),
12
+ _peek: () => state,
13
+ }
14
+ }
15
+
16
+ describe('createNamedViews', () => {
17
+ it('saves the current state and lists it', () => {
18
+ const host = fakeHost({ sorting: [{ id: 'name', desc: true }] })
19
+ const views = createNamedViews(host, { storage: memoryViews() })
20
+ views.save('By name')
21
+ expect(views.list().map((v) => v.name)).toEqual(['By name'])
22
+ expect(views.has('By name')).toBe(true)
23
+ })
24
+
25
+ it('load applies the saved state via setState', () => {
26
+ const host = fakeHost({ sorting: [{ id: 'age', desc: false }] })
27
+ const views = createNamedViews(host, { storage: memoryViews() })
28
+ views.save('snap')
29
+ host.setState({ sorting: [] }) // mutate away
30
+ host.setState.mockClear()
31
+ expect(views.load('snap')).toBe(true)
32
+ expect(host.setState).toHaveBeenCalledWith(
33
+ expect.objectContaining({ sorting: [{ id: 'age', desc: false }] }),
34
+ )
35
+ expect(views.load('missing')).toBe(false)
36
+ })
37
+
38
+ it('save overwrites a duplicate name but keeps its createdAt', () => {
39
+ const host = fakeHost()
40
+ const views = createNamedViews(host, { storage: memoryViews() })
41
+ const a = views.save('v')
42
+ const b = views.save('v')
43
+ expect(views.list()).toHaveLength(1)
44
+ expect(b.createdAt).toBe(a.createdAt)
45
+ })
46
+
47
+ it('remove and rename', () => {
48
+ const host = fakeHost()
49
+ const views = createNamedViews(host, { storage: memoryViews() })
50
+ views.save('a')
51
+ views.save('b')
52
+ expect(views.rename('a', 'c')).toBe(true)
53
+ expect(views.rename('c', 'b')).toBe(false) // target exists
54
+ expect(views.remove('c')).toBe(true)
55
+ expect(views.remove('c')).toBe(false)
56
+ expect(views.list().map((v) => v.name)).toEqual(['b'])
57
+ })
58
+
59
+ it('persists through a storage adapter', () => {
60
+ const store = memoryViews()
61
+ createNamedViews(fakeHost({ sorting: [{ id: 'x', desc: true }] }), { storage: store }).save('keep')
62
+ // A fresh manager over the same storage sees it.
63
+ const reopened = createNamedViews(fakeHost(), { storage: store })
64
+ expect(reopened.has('keep')).toBe(true)
65
+ })
66
+ })
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Named views. A thin, packaged manager on top of `api.getState()` /
3
+ * `api.setState()` so the "save / restore named layouts" feature every
4
+ * enterprise grid ships is one import instead of hand-rolled glue.
5
+ *
6
+ * Storage is pluggable: `localStorageViews(key)` persists per browser, or
7
+ * pass your own adapter to sync views to a server / per-user account. The
8
+ * manager itself is pure and synchronous, so it is trivially testable.
9
+ */
10
+ import type { SvGridViewState } from './svgrid-wrapper.types'
11
+
12
+ export type SavedView = {
13
+ name: string
14
+ state: Partial<SvGridViewState>
15
+ createdAt: number
16
+ }
17
+
18
+ export type ViewStorage = {
19
+ read(): SavedView[]
20
+ write(views: SavedView[]): void
21
+ }
22
+
23
+ /** Minimal grid handle the manager needs - `SvGridApi` satisfies it. */
24
+ export type ViewStateHost = {
25
+ getState(): SvGridViewState
26
+ setState(state: Partial<SvGridViewState>): void
27
+ }
28
+
29
+ export type NamedViews = {
30
+ list(): SavedView[]
31
+ /** Capture the grid's current state under `name` (overwrites a duplicate). */
32
+ save(name: string): SavedView
33
+ /** Apply a saved view to the grid. Returns false if the name is unknown. */
34
+ load(name: string): boolean
35
+ /** Delete a saved view. Returns false if the name is unknown. */
36
+ remove(name: string): boolean
37
+ rename(from: string, to: string): boolean
38
+ has(name: string): boolean
39
+ }
40
+
41
+ /** In-memory storage (default). State lives only as long as the manager. */
42
+ export function memoryViews(initial: SavedView[] = []): ViewStorage {
43
+ let views = [...initial]
44
+ return {
45
+ read: () => views,
46
+ write: (next) => {
47
+ views = next
48
+ },
49
+ }
50
+ }
51
+
52
+ /** localStorage-backed storage. Safe to construct in SSR (no-ops there). */
53
+ export function localStorageViews(key: string): ViewStorage {
54
+ const available = typeof localStorage !== 'undefined'
55
+ return {
56
+ read() {
57
+ if (!available) return []
58
+ try {
59
+ const raw = localStorage.getItem(key)
60
+ const parsed = raw ? JSON.parse(raw) : []
61
+ return Array.isArray(parsed) ? (parsed as SavedView[]) : []
62
+ } catch {
63
+ return []
64
+ }
65
+ },
66
+ write(views) {
67
+ if (!available) return
68
+ try {
69
+ localStorage.setItem(key, JSON.stringify(views))
70
+ } catch {
71
+ /* quota / private mode - ignore */
72
+ }
73
+ },
74
+ }
75
+ }
76
+
77
+ export function createNamedViews(
78
+ host: ViewStateHost,
79
+ options: { storage?: ViewStorage } = {},
80
+ ): NamedViews {
81
+ const storage = options.storage ?? memoryViews()
82
+
83
+ const all = () => storage.read()
84
+ const find = (name: string) => all().find((v) => v.name === name)
85
+
86
+ return {
87
+ list: () => all().slice().sort((a, b) => a.createdAt - b.createdAt),
88
+ has: (name) => Boolean(find(name)),
89
+ save(name) {
90
+ const view: SavedView = {
91
+ name,
92
+ state: host.getState(),
93
+ createdAt: find(name)?.createdAt ?? Date.now(),
94
+ }
95
+ storage.write([...all().filter((v) => v.name !== name), view])
96
+ return view
97
+ },
98
+ load(name) {
99
+ const view = find(name)
100
+ if (!view) return false
101
+ host.setState(view.state)
102
+ return true
103
+ },
104
+ remove(name) {
105
+ const next = all().filter((v) => v.name !== name)
106
+ if (next.length === all().length) return false
107
+ storage.write(next)
108
+ return true
109
+ },
110
+ rename(from, to) {
111
+ const view = find(from)
112
+ if (!view || find(to)) return false
113
+ storage.write([
114
+ ...all().filter((v) => v.name !== from),
115
+ { ...view, name: to },
116
+ ])
117
+ return true
118
+ },
119
+ }
120
+ }