@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.
- package/LICENSE +1 -1
- package/README.md +137 -39
- package/dist/GridFooter.svelte +164 -0
- package/dist/GridFooter.svelte.d.ts +29 -0
- package/dist/GridMenus.svelte +570 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +429 -0
- package/dist/SvGrid.controller.svelte.js +1732 -0
- package/dist/SvGrid.css +1709 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.svelte +344 -7043
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +436 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +527 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -0
- package/dist/chart.d.ts +370 -3
- package/dist/chart.js +1135 -42
- package/dist/clipboard.d.ts +15 -0
- package/dist/clipboard.js +356 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/core.d.ts +1 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +24 -0
- package/dist/editing.js +343 -0
- package/dist/editors/cell-editors.d.ts +1 -1
- package/dist/facet-buckets.d.ts +13 -0
- package/dist/facet-buckets.js +54 -0
- package/dist/features.d.ts +5 -0
- package/dist/features.js +30 -0
- package/dist/filter-operators.d.ts +16 -0
- package/dist/filter-operators.js +69 -0
- package/dist/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +197 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +389 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +86 -0
- package/dist/selection.d.ts +26 -0
- package/dist/selection.js +387 -0
- package/dist/spreadsheet.d.ts +80 -0
- package/dist/spreadsheet.js +194 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/svgrid-wrapper.types.d.ts +12 -1
- package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
- package/dist/svgrid.comments-autocomplete.test.js +96 -0
- package/dist/svgrid.context-menu.test.d.ts +1 -0
- package/dist/svgrid.context-menu.test.js +102 -0
- package/dist/svgrid.new-features.wrapper.test.js +30 -4
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +28 -0
- package/dist/virtualization/scroll-scaling.js +64 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +86 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
- package/dist/virtualization/virtualizer.d.ts +7 -0
- package/dist/virtualization/virtualizer.js +30 -0
- package/package.json +1 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +570 -0
- package/src/SvGrid.controller.svelte.ts +2195 -0
- package/src/SvGrid.css +1747 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +348 -7043
- package/src/SvGrid.types.ts +456 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +663 -0
- package/src/cell-render.test.ts +451 -0
- package/src/cell-render.ts +426 -0
- package/src/cell-values.ts +114 -0
- package/src/chart-export.test.ts +370 -0
- package/src/chart.coverage.test.ts +814 -0
- package/src/chart.ts +1352 -47
- package/src/clipboard.test.ts +731 -0
- package/src/clipboard.ts +524 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +8 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +513 -0
- package/src/editors/cell-editors.coverage.test.ts +156 -0
- package/src/editors/cell-editors.ts +1 -0
- package/src/facet-buckets.test.ts +353 -0
- package/src/facet-buckets.ts +67 -0
- package/src/features.ts +128 -0
- package/src/filter-operators.test.ts +174 -0
- package/src/filter-operators.ts +87 -0
- package/src/hyperformula-adapter.test.ts +256 -0
- package/src/hyperformula-adapter.ts +124 -0
- package/src/index.ts +37 -0
- package/src/keyboard-handlers.coverage.test.ts +560 -0
- package/src/keyboard-handlers.ts +353 -0
- package/src/keyboard.ts +97 -97
- package/src/menus.test.ts +620 -0
- package/src/menus.ts +554 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -0
- package/src/row-resize.test.ts +369 -0
- package/src/row-resize.ts +171 -0
- package/src/scroll-sync.test.ts +330 -0
- package/src/scroll-sync.ts +216 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +545 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +445 -0
- package/src/spreadsheet.ts +246 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +12 -1
- package/src/svgrid.behavior.test.ts +22 -0
- package/src/svgrid.comments-autocomplete.test.ts +112 -0
- package/src/svgrid.context-menu.test.ts +126 -0
- package/src/svgrid.interaction.test.ts +30 -0
- package/src/svgrid.new-features.wrapper.test.ts +65 -4
- package/src/svgrid.wrapper.test.ts +27 -1
- package/src/test-setup.ts +9 -6
- package/src/virtualization/scroll-scaling.test.ts +148 -0
- package/src/virtualization/scroll-scaling.ts +121 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
- package/src/virtualization/virtualizer.ts +26 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
attachAutoSavedView,
|
|
4
|
+
createNamedViews,
|
|
5
|
+
localStorageViews,
|
|
6
|
+
memoryViews,
|
|
7
|
+
type ViewStorage,
|
|
8
|
+
} from './named-views'
|
|
9
|
+
import type { SvGridViewState } from './svgrid-wrapper.types'
|
|
10
|
+
|
|
11
|
+
function fakeHost(initial: Partial<SvGridViewState> = {}) {
|
|
12
|
+
let state = { sorting: [], columnFilters: [], ...initial } as SvGridViewState
|
|
13
|
+
return {
|
|
14
|
+
getState: () => state,
|
|
15
|
+
setState: vi.fn((s: Partial<SvGridViewState>) => {
|
|
16
|
+
state = { ...state, ...s }
|
|
17
|
+
}),
|
|
18
|
+
set: (s: SvGridViewState) => {
|
|
19
|
+
state = s
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('createNamedViews - list ordering and edge cases', () => {
|
|
25
|
+
it('lists views sorted by createdAt ascending', () => {
|
|
26
|
+
const store = memoryViews([
|
|
27
|
+
{ name: 'late', state: {}, createdAt: 200 },
|
|
28
|
+
{ name: 'early', state: {}, createdAt: 100 },
|
|
29
|
+
])
|
|
30
|
+
const views = createNamedViews(fakeHost(), { storage: store })
|
|
31
|
+
expect(views.list().map((v) => v.name)).toEqual(['early', 'late'])
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('defaults to memory storage when none is passed', () => {
|
|
35
|
+
const views = createNamedViews(fakeHost({ sorting: [{ id: 'a', desc: true }] }))
|
|
36
|
+
views.save('only')
|
|
37
|
+
expect(views.has('only')).toBe(true)
|
|
38
|
+
expect(views.list()).toHaveLength(1)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('rename returns false when the source name is unknown', () => {
|
|
42
|
+
const views = createNamedViews(fakeHost(), { storage: memoryViews() })
|
|
43
|
+
views.save('a')
|
|
44
|
+
expect(views.rename('missing', 'b')).toBe(false)
|
|
45
|
+
expect(views.list().map((v) => v.name)).toEqual(['a'])
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('load returns false and does not call setState for unknown name', () => {
|
|
49
|
+
const host = fakeHost()
|
|
50
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
51
|
+
host.setState.mockClear()
|
|
52
|
+
expect(views.load('nope')).toBe(false)
|
|
53
|
+
expect(host.setState).not.toHaveBeenCalled()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('save captures the current host state snapshot', () => {
|
|
57
|
+
const host = fakeHost({ sorting: [{ id: 'z', desc: false }] })
|
|
58
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
59
|
+
const v = views.save('snap')
|
|
60
|
+
expect(v.state).toMatchObject({ sorting: [{ id: 'z', desc: false }] })
|
|
61
|
+
expect(typeof v.createdAt).toBe('number')
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('localStorageViews', () => {
|
|
66
|
+
beforeEach(() => localStorage.clear())
|
|
67
|
+
afterEach(() => localStorage.clear())
|
|
68
|
+
|
|
69
|
+
it('reads [] when the key is absent', () => {
|
|
70
|
+
const store = localStorageViews('svgrid:absent')
|
|
71
|
+
expect(store.read()).toEqual([])
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('round-trips views through localStorage', () => {
|
|
75
|
+
const store = localStorageViews('svgrid:rt')
|
|
76
|
+
const views = [{ name: 'x', state: {}, createdAt: 1 }]
|
|
77
|
+
store.write(views)
|
|
78
|
+
expect(store.read()).toEqual(views)
|
|
79
|
+
// raw JSON is actually present
|
|
80
|
+
expect(JSON.parse(localStorage.getItem('svgrid:rt')!)).toEqual(views)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('returns [] for corrupt JSON', () => {
|
|
84
|
+
localStorage.setItem('svgrid:bad', '{not valid json')
|
|
85
|
+
expect(localStorageViews('svgrid:bad').read()).toEqual([])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('returns [] when the stored value is not an array', () => {
|
|
89
|
+
localStorage.setItem('svgrid:obj', JSON.stringify({ foo: 'bar' }))
|
|
90
|
+
expect(localStorageViews('svgrid:obj').read()).toEqual([])
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('swallows write errors (quota / private mode)', () => {
|
|
94
|
+
const store = localStorageViews('svgrid:quota')
|
|
95
|
+
const spy = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
|
96
|
+
throw new DOMException('quota', 'QuotaExceededError')
|
|
97
|
+
})
|
|
98
|
+
expect(() => store.write([{ name: 'a', state: {}, createdAt: 1 }])).not.toThrow()
|
|
99
|
+
spy.mockRestore()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('works end-to-end as a NamedViews adapter', () => {
|
|
103
|
+
const views = createNamedViews(fakeHost({ sorting: [{ id: 'p', desc: true }] }), {
|
|
104
|
+
storage: localStorageViews('svgrid:adapter'),
|
|
105
|
+
})
|
|
106
|
+
views.save('persisted')
|
|
107
|
+
const reopened = createNamedViews(fakeHost(), {
|
|
108
|
+
storage: localStorageViews('svgrid:adapter'),
|
|
109
|
+
})
|
|
110
|
+
expect(reopened.has('persisted')).toBe(true)
|
|
111
|
+
expect(reopened.load('persisted')).toBe(true)
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe('attachAutoSavedView', () => {
|
|
116
|
+
beforeEach(() => vi.useFakeTimers())
|
|
117
|
+
afterEach(() => vi.useRealTimers())
|
|
118
|
+
|
|
119
|
+
it('restores an existing autosave slot on attach', () => {
|
|
120
|
+
const host = fakeHost()
|
|
121
|
+
const store: ViewStorage = memoryViews([
|
|
122
|
+
{ name: '__autosave', state: { sorting: [{ id: 'restored', desc: true }] }, createdAt: 1 },
|
|
123
|
+
])
|
|
124
|
+
const views = createNamedViews(host, { storage: store })
|
|
125
|
+
const off = attachAutoSavedView(host, views)
|
|
126
|
+
expect(host.setState).toHaveBeenCalledWith(
|
|
127
|
+
expect.objectContaining({ sorting: [{ id: 'restored', desc: true }] }),
|
|
128
|
+
)
|
|
129
|
+
off()
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('skips restore when skipRestore is set', () => {
|
|
133
|
+
const host = fakeHost()
|
|
134
|
+
const store = memoryViews([{ name: '__autosave', state: { sorting: [] }, createdAt: 1 }])
|
|
135
|
+
const views = createNamedViews(host, { storage: store })
|
|
136
|
+
const off = attachAutoSavedView(host, views, { skipRestore: true })
|
|
137
|
+
expect(host.setState).not.toHaveBeenCalled()
|
|
138
|
+
off()
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('saves when the state snapshot changes after an interval', () => {
|
|
142
|
+
const host = fakeHost({ sorting: [] })
|
|
143
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
144
|
+
const off = attachAutoSavedView(host, views, { intervalMs: 500 })
|
|
145
|
+
expect(views.has('__autosave')).toBe(false)
|
|
146
|
+
// mutate the host state, then let the poller tick
|
|
147
|
+
host.set({ sorting: [{ id: 'new', desc: false }], columnFilters: [] } as SvGridViewState)
|
|
148
|
+
vi.advanceTimersByTime(500)
|
|
149
|
+
expect(views.has('__autosave')).toBe(true)
|
|
150
|
+
off()
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('does not save when the snapshot is unchanged', () => {
|
|
154
|
+
const host = fakeHost({ sorting: [] })
|
|
155
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
156
|
+
const saveSpy = vi.spyOn(views, 'save')
|
|
157
|
+
const off = attachAutoSavedView(host, views, { intervalMs: 300 })
|
|
158
|
+
vi.advanceTimersByTime(900) // several ticks, no state change
|
|
159
|
+
expect(saveSpy).not.toHaveBeenCalled()
|
|
160
|
+
off()
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('uses a custom slot name', () => {
|
|
164
|
+
const host = fakeHost({ sorting: [] })
|
|
165
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
166
|
+
const off = attachAutoSavedView(host, views, { name: 'mySlot', intervalMs: 200 })
|
|
167
|
+
host.set({ sorting: [{ id: 'q', desc: true }], columnFilters: [] } as SvGridViewState)
|
|
168
|
+
vi.advanceTimersByTime(200)
|
|
169
|
+
expect(views.has('mySlot')).toBe(true)
|
|
170
|
+
off()
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('clamps the interval to a minimum of 100ms', () => {
|
|
174
|
+
const host = fakeHost({ sorting: [] })
|
|
175
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
176
|
+
const setIntervalSpy = vi.spyOn(globalThis, 'setInterval')
|
|
177
|
+
const off = attachAutoSavedView(host, views, { intervalMs: 5 })
|
|
178
|
+
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 100)
|
|
179
|
+
off()
|
|
180
|
+
setIntervalSpy.mockRestore()
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('recovers when getState throws inside the poll loop', () => {
|
|
184
|
+
let throwing = false
|
|
185
|
+
const host = {
|
|
186
|
+
getState: () => {
|
|
187
|
+
if (throwing) throw new Error('boom')
|
|
188
|
+
return { sorting: [], columnFilters: [] } as SvGridViewState
|
|
189
|
+
},
|
|
190
|
+
setState: vi.fn(),
|
|
191
|
+
}
|
|
192
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
193
|
+
const saveSpy = vi.spyOn(views, 'save')
|
|
194
|
+
const off = attachAutoSavedView(host, views, { intervalMs: 200 })
|
|
195
|
+
throwing = true
|
|
196
|
+
expect(() => vi.advanceTimersByTime(400)).not.toThrow()
|
|
197
|
+
expect(saveSpy).not.toHaveBeenCalled()
|
|
198
|
+
off()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('detach stops the polling interval', () => {
|
|
202
|
+
const host = fakeHost({ sorting: [] })
|
|
203
|
+
const views = createNamedViews(host, { storage: memoryViews() })
|
|
204
|
+
const clearSpy = vi.spyOn(globalThis, 'clearInterval')
|
|
205
|
+
const off = attachAutoSavedView(host, views, { intervalMs: 200 })
|
|
206
|
+
off()
|
|
207
|
+
expect(clearSpy).toHaveBeenCalled()
|
|
208
|
+
clearSpy.mockRestore()
|
|
209
|
+
})
|
|
210
|
+
})
|
package/src/named-views.ts
CHANGED
|
@@ -118,3 +118,51 @@ export function createNamedViews(
|
|
|
118
118
|
},
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
|
+
|
|
122
|
+
export type AutoSavedViewOptions = {
|
|
123
|
+
/** Slot name inside the NamedViews store. Default `'__autosave'`. */
|
|
124
|
+
name?: string
|
|
125
|
+
/** Sample interval in ms. Default 800. */
|
|
126
|
+
intervalMs?: number
|
|
127
|
+
/** Skip restore-on-mount (e.g. you already loaded a URL view first). */
|
|
128
|
+
skipRestore?: boolean
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Attach an "always-save-current-layout" slot to a `NamedViews` manager.
|
|
133
|
+
* Restores once on attach (if a saved view exists under `name`) and
|
|
134
|
+
* polls `host.getState()` thereafter, saving when the JSON snapshot
|
|
135
|
+
* changes.
|
|
136
|
+
*
|
|
137
|
+
* Returns a `detach()` that stops polling - call it from `onDestroy`.
|
|
138
|
+
*
|
|
139
|
+
* ```ts
|
|
140
|
+
* const views = createNamedViews(api, { storage: localStorageViews('myapp:views') })
|
|
141
|
+
* const off = attachAutoSavedView(api, views)
|
|
142
|
+
* onDestroy(off)
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* Distinct from a user-saved named view: the slot is a single, fixed
|
|
146
|
+
* name reserved for "what the user left the page looking at." The user
|
|
147
|
+
* can still call `views.save('Q3 review')` etc. through the same store.
|
|
148
|
+
*/
|
|
149
|
+
export function attachAutoSavedView(
|
|
150
|
+
host: ViewStateHost,
|
|
151
|
+
views: NamedViews,
|
|
152
|
+
opts: AutoSavedViewOptions = {},
|
|
153
|
+
): () => void {
|
|
154
|
+
const name = opts.name ?? '__autosave'
|
|
155
|
+
if (!opts.skipRestore && views.has(name)) views.load(name)
|
|
156
|
+
|
|
157
|
+
const interval = Math.max(100, opts.intervalMs ?? 800)
|
|
158
|
+
let last = JSON.stringify(host.getState())
|
|
159
|
+
const handle = setInterval(() => {
|
|
160
|
+
let next: string
|
|
161
|
+
try { next = JSON.stringify(host.getState()) } catch { return }
|
|
162
|
+
if (next === last) return
|
|
163
|
+
last = next
|
|
164
|
+
views.save(name)
|
|
165
|
+
}, interval)
|
|
166
|
+
|
|
167
|
+
return () => clearInterval(handle)
|
|
168
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { rowResize } from './row-resize'
|
|
3
|
+
import type { RowResizeOptions } from './row-resize'
|
|
4
|
+
|
|
5
|
+
const STRIP_CLASS = 'sv-grid-row-resize-handle'
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Helpers
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
/** Build a host `<table>` with `rows` body rows. Each row carries the
|
|
12
|
+
* `tr.sv-grid-row` class + a `.sv-row-gutter` cell holding a
|
|
13
|
+
* `data-svgrid-row` marker, matching what SvGrid renders. Extra classes
|
|
14
|
+
* let individual tests opt rows out (header / spacer / no-gutter). */
|
|
15
|
+
function buildGrid(
|
|
16
|
+
specs: Array<{ rowClasses?: string[]; gutter?: boolean; rowIndex?: number }>,
|
|
17
|
+
): { host: HTMLElement; rows: HTMLTableRowElement[] } {
|
|
18
|
+
const host = document.createElement('div')
|
|
19
|
+
const table = document.createElement('table')
|
|
20
|
+
const tbody = document.createElement('tbody')
|
|
21
|
+
const rows: HTMLTableRowElement[] = []
|
|
22
|
+
specs.forEach((spec, i) => {
|
|
23
|
+
const tr = document.createElement('tr')
|
|
24
|
+
tr.className = ['sv-grid-row', ...(spec.rowClasses ?? [])].join(' ')
|
|
25
|
+
const cell = document.createElement('td')
|
|
26
|
+
cell.className = 'sv-grid-cell'
|
|
27
|
+
if (spec.gutter !== false) cell.classList.add('sv-row-gutter')
|
|
28
|
+
const marker = document.createElement('span')
|
|
29
|
+
marker.setAttribute('data-svgrid-row', String(spec.rowIndex ?? i))
|
|
30
|
+
cell.appendChild(marker)
|
|
31
|
+
tr.appendChild(cell)
|
|
32
|
+
tbody.appendChild(tr)
|
|
33
|
+
rows.push(tr)
|
|
34
|
+
})
|
|
35
|
+
table.appendChild(tbody)
|
|
36
|
+
host.appendChild(table)
|
|
37
|
+
document.body.appendChild(host)
|
|
38
|
+
return { host, rows }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function gutterOf(tr: HTMLTableRowElement): HTMLTableCellElement {
|
|
42
|
+
return tr.querySelector<HTMLTableCellElement>('.sv-row-gutter')!
|
|
43
|
+
}
|
|
44
|
+
function stripOf(tr: HTMLTableRowElement): HTMLElement | null {
|
|
45
|
+
return tr.querySelector<HTMLElement>(`.${STRIP_CLASS}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Mock getBoundingClientRect so a TR reports a real height in jsdom
|
|
49
|
+
* (which otherwise returns all-zero rects). */
|
|
50
|
+
function mockRowHeight(tr: HTMLTableRowElement, height: number): void {
|
|
51
|
+
tr.getBoundingClientRect = () =>
|
|
52
|
+
({ height, width: 100, top: 0, left: 0, right: 100, bottom: height, x: 0, y: 0, toJSON() {} }) as DOMRect
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function pointer(type: string, init: Partial<PointerEvent> = {}): PointerEvent {
|
|
56
|
+
return new PointerEvent(type, { bubbles: true, cancelable: true, pointerId: 1, ...init } as PointerEventInit)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const hosts: HTMLElement[] = []
|
|
60
|
+
function track(host: HTMLElement): HTMLElement {
|
|
61
|
+
hosts.push(host)
|
|
62
|
+
return host
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
// jsdom lacks pointer-capture methods on elements; stub them so the action's
|
|
67
|
+
// try/catch wrapped calls have something to invoke.
|
|
68
|
+
if (!HTMLElement.prototype.setPointerCapture) {
|
|
69
|
+
HTMLElement.prototype.setPointerCapture = function () {}
|
|
70
|
+
}
|
|
71
|
+
if (!HTMLElement.prototype.releasePointerCapture) {
|
|
72
|
+
HTMLElement.prototype.releasePointerCapture = function () {}
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
while (hosts.length) hosts.pop()!.remove()
|
|
78
|
+
document.body.innerHTML = ''
|
|
79
|
+
document.body.style.cursor = ''
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
// decorate()
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
describe('rowResize - strip injection', () => {
|
|
87
|
+
it('injects a resize strip into every gutter cell', () => {
|
|
88
|
+
const { host, rows } = buildGrid([{}, {}, {}])
|
|
89
|
+
track(host)
|
|
90
|
+
const action = rowResize(host, { onResize: vi.fn() })
|
|
91
|
+
for (const tr of rows) {
|
|
92
|
+
const strip = stripOf(tr)
|
|
93
|
+
expect(strip).not.toBeNull()
|
|
94
|
+
expect(strip!.getAttribute('role')).toBe('separator')
|
|
95
|
+
expect(strip!.getAttribute('aria-orientation')).toBe('horizontal')
|
|
96
|
+
expect(strip!.getAttribute('aria-label')).toBe('Resize row')
|
|
97
|
+
expect(strip!.style.cursor).toBe('row-resize')
|
|
98
|
+
}
|
|
99
|
+
// Gutter cells become a positioning context with visible overflow.
|
|
100
|
+
expect(gutterOf(rows[0]).style.position).toBe('relative')
|
|
101
|
+
expect(gutterOf(rows[0]).style.overflow).toBe('visible')
|
|
102
|
+
action.destroy()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('does not double-inject a strip on re-decorate', async () => {
|
|
106
|
+
const { host, rows } = buildGrid([{}])
|
|
107
|
+
track(host)
|
|
108
|
+
const action = rowResize(host, { onResize: vi.fn() })
|
|
109
|
+
expect(rows[0].querySelectorAll(`.${STRIP_CLASS}`).length).toBe(1)
|
|
110
|
+
// Trigger the MutationObserver by appending an unrelated node.
|
|
111
|
+
gutterOf(rows[0]).appendChild(document.createElement('span'))
|
|
112
|
+
await Promise.resolve()
|
|
113
|
+
await new Promise((r) => setTimeout(r, 0))
|
|
114
|
+
expect(rows[0].querySelectorAll(`.${STRIP_CLASS}`).length).toBe(1)
|
|
115
|
+
action.destroy()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('skips header rows, spacer rows, and rows without a gutter cell', () => {
|
|
119
|
+
const { host, rows } = buildGrid([
|
|
120
|
+
{ rowClasses: ['sv-grid-header-row'] },
|
|
121
|
+
{ rowClasses: ['sv-grid-row-spacer'] },
|
|
122
|
+
{ gutter: false },
|
|
123
|
+
{}, // normal row gets a strip
|
|
124
|
+
])
|
|
125
|
+
track(host)
|
|
126
|
+
const action = rowResize(host, { onResize: vi.fn() })
|
|
127
|
+
expect(stripOf(rows[0])).toBeNull()
|
|
128
|
+
expect(stripOf(rows[1])).toBeNull()
|
|
129
|
+
expect(stripOf(rows[2])).toBeNull()
|
|
130
|
+
expect(stripOf(rows[3])).not.toBeNull()
|
|
131
|
+
action.destroy()
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('does not override an already-positioned gutter cell', () => {
|
|
135
|
+
const { host, rows } = buildGrid([{}])
|
|
136
|
+
track(host)
|
|
137
|
+
gutterOf(rows[0]).style.position = 'sticky'
|
|
138
|
+
const action = rowResize(host, { onResize: vi.fn() })
|
|
139
|
+
expect(gutterOf(rows[0]).style.position).toBe('sticky')
|
|
140
|
+
action.destroy()
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('injects no strips when created disabled', () => {
|
|
144
|
+
const { host, rows } = buildGrid([{}, {}])
|
|
145
|
+
track(host)
|
|
146
|
+
const action = rowResize(host, { onResize: vi.fn(), disabled: true })
|
|
147
|
+
expect(stripOf(rows[0])).toBeNull()
|
|
148
|
+
expect(stripOf(rows[1])).toBeNull()
|
|
149
|
+
action.destroy()
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Drag lifecycle
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
describe('rowResize - drag', () => {
|
|
158
|
+
it('reports the new height on pointerup and fires move callbacks', () => {
|
|
159
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
160
|
+
track(host)
|
|
161
|
+
mockRowHeight(rows[0], 32)
|
|
162
|
+
const onResize = vi.fn()
|
|
163
|
+
const onResizeMove = vi.fn()
|
|
164
|
+
const action = rowResize(host, { onResize, onResizeMove })
|
|
165
|
+
const strip = stripOf(rows[0])!
|
|
166
|
+
|
|
167
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
|
|
168
|
+
expect(strip.classList.contains('is-resizing')).toBe(true)
|
|
169
|
+
expect(document.body.style.cursor).toBe('row-resize')
|
|
170
|
+
|
|
171
|
+
// Drag down 20px -> 52.
|
|
172
|
+
window.dispatchEvent(pointer('pointermove', { clientY: 120 }))
|
|
173
|
+
expect(onResizeMove).toHaveBeenLastCalledWith(0, 52)
|
|
174
|
+
expect(rows[0].style.height).toBe('52px')
|
|
175
|
+
|
|
176
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 120 }))
|
|
177
|
+
expect(onResize).toHaveBeenCalledWith(0, 52)
|
|
178
|
+
expect(strip.classList.contains('is-resizing')).toBe(false)
|
|
179
|
+
expect(document.body.style.cursor).toBe('')
|
|
180
|
+
action.destroy()
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('clamps the height to the min bound', () => {
|
|
184
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
185
|
+
track(host)
|
|
186
|
+
mockRowHeight(rows[0], 32)
|
|
187
|
+
const onResize = vi.fn()
|
|
188
|
+
const action = rowResize(host, { onResize, min: 24 })
|
|
189
|
+
const strip = stripOf(rows[0])!
|
|
190
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
|
|
191
|
+
// Drag far up -> would be negative, clamps to 24.
|
|
192
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 0 }))
|
|
193
|
+
expect(onResize).toHaveBeenCalledWith(0, 24)
|
|
194
|
+
action.destroy()
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('clamps the height to the max bound', () => {
|
|
198
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
199
|
+
track(host)
|
|
200
|
+
mockRowHeight(rows[0], 32)
|
|
201
|
+
const onResize = vi.fn()
|
|
202
|
+
const action = rowResize(host, { onResize, max: 50 })
|
|
203
|
+
const strip = stripOf(rows[0])!
|
|
204
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 100 }))
|
|
205
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 1000 }))
|
|
206
|
+
expect(onResize).toHaveBeenCalledWith(0, 50)
|
|
207
|
+
action.destroy()
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('works without an onResizeMove callback (optional)', () => {
|
|
211
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
212
|
+
track(host)
|
|
213
|
+
mockRowHeight(rows[0], 30)
|
|
214
|
+
const onResize = vi.fn()
|
|
215
|
+
const action = rowResize(host, { onResize })
|
|
216
|
+
const strip = stripOf(rows[0])!
|
|
217
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 50 }))
|
|
218
|
+
expect(() => window.dispatchEvent(pointer('pointermove', { clientY: 60 }))).not.toThrow()
|
|
219
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 60 }))
|
|
220
|
+
expect(onResize).toHaveBeenCalledWith(0, 40)
|
|
221
|
+
action.destroy()
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
it('ignores pointermove / pointerup when no drag is active', () => {
|
|
225
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
226
|
+
track(host)
|
|
227
|
+
const onResize = vi.fn()
|
|
228
|
+
const onResizeMove = vi.fn()
|
|
229
|
+
const action = rowResize(host, { onResize, onResizeMove })
|
|
230
|
+
// No pointerdown first - listeners were only attached on down, so these
|
|
231
|
+
// shouldn't do anything even if dispatched.
|
|
232
|
+
window.dispatchEvent(pointer('pointermove', { clientY: 10 }))
|
|
233
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 10 }))
|
|
234
|
+
expect(onResize).not.toHaveBeenCalled()
|
|
235
|
+
expect(onResizeMove).not.toHaveBeenCalled()
|
|
236
|
+
action.destroy()
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// pointerdown guards
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
describe('rowResize - pointerdown guards', () => {
|
|
245
|
+
it('ignores pointerdown when disabled', () => {
|
|
246
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
247
|
+
track(host)
|
|
248
|
+
mockRowHeight(rows[0], 32)
|
|
249
|
+
const onResize = vi.fn()
|
|
250
|
+
// Start enabled so a strip exists, then disable via update.
|
|
251
|
+
const action = rowResize(host, { onResize })
|
|
252
|
+
const strip = stripOf(rows[0])!
|
|
253
|
+
action.update({ onResize, disabled: true })
|
|
254
|
+
// update removes strips, but dispatch on the (now-detached) strip anyway:
|
|
255
|
+
// even if it were still in the DOM, current.disabled short-circuits.
|
|
256
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
|
|
257
|
+
window.dispatchEvent(pointer('pointerup', { clientY: 50 }))
|
|
258
|
+
expect(onResize).not.toHaveBeenCalled()
|
|
259
|
+
action.destroy()
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('ignores pointerdown on a non-strip target', () => {
|
|
263
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
264
|
+
track(host)
|
|
265
|
+
const onResize = vi.fn()
|
|
266
|
+
const action = rowResize(host, { onResize })
|
|
267
|
+
gutterOf(rows[0]).dispatchEvent(pointer('pointerdown', { clientY: 10 }))
|
|
268
|
+
expect(document.body.style.cursor).toBe('')
|
|
269
|
+
action.destroy()
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('ignores a strip whose row has a non-finite row index', () => {
|
|
273
|
+
const { host, rows } = buildGrid([{}])
|
|
274
|
+
track(host)
|
|
275
|
+
// Remove the data-svgrid-row marker so rowIndexOf returns NaN.
|
|
276
|
+
rows[0].querySelector('[data-svgrid-row]')!.removeAttribute('data-svgrid-row')
|
|
277
|
+
const onResize = vi.fn()
|
|
278
|
+
const action = rowResize(host, { onResize })
|
|
279
|
+
const strip = stripOf(rows[0])!
|
|
280
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
|
|
281
|
+
expect(document.body.style.cursor).toBe('')
|
|
282
|
+
expect(strip.classList.contains('is-resizing')).toBe(false)
|
|
283
|
+
action.destroy()
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
it('ignores a strip not inside a tr.sv-grid-row', () => {
|
|
287
|
+
const host = track(document.createElement('div'))
|
|
288
|
+
document.body.appendChild(host)
|
|
289
|
+
// A loose strip with no matching ancestor row.
|
|
290
|
+
const strip = document.createElement('div')
|
|
291
|
+
strip.className = STRIP_CLASS
|
|
292
|
+
host.appendChild(strip)
|
|
293
|
+
const onResize = vi.fn()
|
|
294
|
+
const action = rowResize(host, { onResize })
|
|
295
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
|
|
296
|
+
expect(document.body.style.cursor).toBe('')
|
|
297
|
+
action.destroy()
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
// ---------------------------------------------------------------------------
|
|
302
|
+
// update() + destroy()
|
|
303
|
+
// ---------------------------------------------------------------------------
|
|
304
|
+
|
|
305
|
+
describe('rowResize - update + destroy', () => {
|
|
306
|
+
it('removes strips when toggled to disabled and restores when re-enabled', () => {
|
|
307
|
+
const { host, rows } = buildGrid([{}, {}])
|
|
308
|
+
track(host)
|
|
309
|
+
const onResize = vi.fn()
|
|
310
|
+
const action = rowResize(host, { onResize, disabled: false })
|
|
311
|
+
expect(stripOf(rows[0])).not.toBeNull()
|
|
312
|
+
|
|
313
|
+
action.update({ onResize, disabled: true })
|
|
314
|
+
expect(stripOf(rows[0])).toBeNull()
|
|
315
|
+
expect(stripOf(rows[1])).toBeNull()
|
|
316
|
+
|
|
317
|
+
action.update({ onResize, disabled: false })
|
|
318
|
+
expect(stripOf(rows[0])).not.toBeNull()
|
|
319
|
+
action.destroy()
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
it('does not repaint when disabled flag is unchanged on update', () => {
|
|
323
|
+
const { host, rows } = buildGrid([{}])
|
|
324
|
+
track(host)
|
|
325
|
+
const action = rowResize(host, { onResize: vi.fn() })
|
|
326
|
+
const firstStrip = stripOf(rows[0])
|
|
327
|
+
// Same disabled value -> decorate is NOT called, strip stays the same node.
|
|
328
|
+
action.update({ onResize: vi.fn() })
|
|
329
|
+
expect(stripOf(rows[0])).toBe(firstStrip)
|
|
330
|
+
action.destroy()
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
it('destroy removes all strips and detaches listeners', () => {
|
|
334
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
335
|
+
track(host)
|
|
336
|
+
mockRowHeight(rows[0], 32)
|
|
337
|
+
const onResize = vi.fn()
|
|
338
|
+
const action = rowResize(host, { onResize })
|
|
339
|
+
expect(stripOf(rows[0])).not.toBeNull()
|
|
340
|
+
|
|
341
|
+
action.destroy()
|
|
342
|
+
expect(stripOf(rows[0])).toBeNull()
|
|
343
|
+
|
|
344
|
+
// After destroy the pointerdown listener is gone; re-create a strip
|
|
345
|
+
// manually and confirm no drag starts.
|
|
346
|
+
const strip = document.createElement('div')
|
|
347
|
+
strip.className = STRIP_CLASS
|
|
348
|
+
gutterOf(rows[0]).appendChild(strip)
|
|
349
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 10 }))
|
|
350
|
+
expect(document.body.style.cursor).toBe('')
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
it('uses default min=20 / max=320 when bounds are omitted', () => {
|
|
354
|
+
const { host, rows } = buildGrid([{ rowIndex: 0 }])
|
|
355
|
+
track(host)
|
|
356
|
+
mockRowHeight(rows[0], 100)
|
|
357
|
+
const onResize = vi.fn()
|
|
358
|
+
const action = rowResize(host, { onResize })
|
|
359
|
+
const strip = stripOf(rows[0])!
|
|
360
|
+
strip.dispatchEvent(pointer('pointerdown', { clientY: 0 }))
|
|
361
|
+
// Drag way past max -> clamps to 320.
|
|
362
|
+
window.dispatchEvent(pointer('pointermove', { clientY: 10000 }))
|
|
363
|
+
expect(rows[0].style.height).toBe('320px')
|
|
364
|
+
// Drag way below min -> clamps to 20.
|
|
365
|
+
window.dispatchEvent(pointer('pointerup', { clientY: -10000 }))
|
|
366
|
+
expect(onResize).toHaveBeenCalledWith(0, 20)
|
|
367
|
+
action.destroy()
|
|
368
|
+
})
|
|
369
|
+
})
|