@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,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row-resize Svelte action - drag the bottom edge of a row to grow /
|
|
3
|
+
* shrink it. Pair with a function-valued `rowHeight` prop so the grid
|
|
4
|
+
* reads per-row heights from a reactive store.
|
|
5
|
+
*
|
|
6
|
+
* The action is OFF until a consumer attaches it AND a row-header
|
|
7
|
+
* gutter cell exists in each row. A row-header cell is any TD with
|
|
8
|
+
* the `sv-row-gutter` class (typically the one rendering the row
|
|
9
|
+
* number 1, 2, 3, ...). Demos opt in by setting
|
|
10
|
+
* `cellClass: 'sv-row-gutter'` on the gutter column.
|
|
11
|
+
*
|
|
12
|
+
* <script>
|
|
13
|
+
* let heights = $state<Record<number, number>>({})
|
|
14
|
+
* let resizeOn = $state(false)
|
|
15
|
+
* const rowHeight = (i: number) => heights[i] ?? 32
|
|
16
|
+
* function onResize(i: number, h: number) { heights = { ...heights, [i]: h } }
|
|
17
|
+
* </script>
|
|
18
|
+
*
|
|
19
|
+
* <div use:rowResize={{ onResize, disabled: !resizeOn }}>
|
|
20
|
+
* <SvGrid columns={[{ field:'rn', cellClass:'sv-row-gutter', ... }, ...]}
|
|
21
|
+
* rowHeight={rowHeight} ... />
|
|
22
|
+
* </div>
|
|
23
|
+
*
|
|
24
|
+
* Visual: the strip sits along the bottom edge of the row-header
|
|
25
|
+
* cell, 5px tall, transparent by default, accent-tinted on hover -
|
|
26
|
+
* matching the column resize handle's look. Cursor is `row-resize`.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const STRIP_CLASS = 'sv-grid-row-resize-handle'
|
|
30
|
+
|
|
31
|
+
export type RowResizeOptions = {
|
|
32
|
+
/** Called when the user releases the drag with the final height. */
|
|
33
|
+
onResize: (rowIndex: number, height: number) => void
|
|
34
|
+
/** Optional callback fired during the drag (every pointermove). */
|
|
35
|
+
onResizeMove?: (rowIndex: number, height: number) => void
|
|
36
|
+
/** Min row height in px. Default 20. */
|
|
37
|
+
min?: number
|
|
38
|
+
/** Max row height in px. Default 320. */
|
|
39
|
+
max?: number
|
|
40
|
+
/** When true, the action removes its strips and ignores events. */
|
|
41
|
+
disabled?: boolean
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type ActiveDrag = {
|
|
45
|
+
rowIndex: number
|
|
46
|
+
startY: number
|
|
47
|
+
startHeight: number
|
|
48
|
+
trEl: HTMLTableRowElement
|
|
49
|
+
strip: HTMLElement
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function rowResize(node: HTMLElement, opts: RowResizeOptions) {
|
|
53
|
+
let current = opts
|
|
54
|
+
let drag: ActiveDrag | null = null
|
|
55
|
+
|
|
56
|
+
function rowIndexOf(tr: HTMLTableRowElement): number {
|
|
57
|
+
const dataRow = tr.querySelector<HTMLElement>('[data-svgrid-row]')
|
|
58
|
+
const val = dataRow?.dataset.svgridRow
|
|
59
|
+
return val != null ? Number(val) : NaN
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function onPointerMove(e: PointerEvent) {
|
|
63
|
+
if (!drag) return
|
|
64
|
+
const min = current.min ?? 20
|
|
65
|
+
const max = current.max ?? 320
|
|
66
|
+
const next = Math.max(min, Math.min(max, drag.startHeight + (e.clientY - drag.startY)))
|
|
67
|
+
drag.trEl.style.height = `${Math.round(next)}px`
|
|
68
|
+
current.onResizeMove?.(drag.rowIndex, Math.round(next))
|
|
69
|
+
}
|
|
70
|
+
function onPointerUp(e: PointerEvent) {
|
|
71
|
+
if (!drag) return
|
|
72
|
+
const min = current.min ?? 20
|
|
73
|
+
const max = current.max ?? 320
|
|
74
|
+
const next = Math.max(min, Math.min(max, drag.startHeight + (e.clientY - drag.startY)))
|
|
75
|
+
current.onResize(drag.rowIndex, Math.round(next))
|
|
76
|
+
drag.strip.classList.remove('is-resizing')
|
|
77
|
+
try { drag.trEl.releasePointerCapture(e.pointerId) } catch { /* ignore */ }
|
|
78
|
+
drag = null
|
|
79
|
+
window.removeEventListener('pointermove', onPointerMove)
|
|
80
|
+
window.removeEventListener('pointerup', onPointerUp)
|
|
81
|
+
document.body.style.cursor = ''
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function onPointerDown(e: PointerEvent) {
|
|
85
|
+
if (current.disabled) return
|
|
86
|
+
const t = e.target as HTMLElement | null
|
|
87
|
+
if (!t?.classList.contains(STRIP_CLASS)) return
|
|
88
|
+
const tr = t.closest<HTMLTableRowElement>('tr.sv-grid-row')
|
|
89
|
+
if (!tr) return
|
|
90
|
+
const rowIndex = rowIndexOf(tr)
|
|
91
|
+
if (!Number.isFinite(rowIndex)) return
|
|
92
|
+
e.preventDefault()
|
|
93
|
+
e.stopPropagation()
|
|
94
|
+
t.classList.add('is-resizing')
|
|
95
|
+
drag = {
|
|
96
|
+
rowIndex,
|
|
97
|
+
startY: e.clientY,
|
|
98
|
+
startHeight: tr.getBoundingClientRect().height,
|
|
99
|
+
trEl: tr,
|
|
100
|
+
strip: t,
|
|
101
|
+
}
|
|
102
|
+
try { tr.setPointerCapture(e.pointerId) } catch { /* ignore */ }
|
|
103
|
+
document.body.style.cursor = 'row-resize'
|
|
104
|
+
window.addEventListener('pointermove', onPointerMove)
|
|
105
|
+
window.addEventListener('pointerup', onPointerUp)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Inject one strip into every row's gutter cell. When disabled or
|
|
109
|
+
* when a row lacks a `.sv-row-gutter` cell, no strip is added. */
|
|
110
|
+
function decorate() {
|
|
111
|
+
if (current.disabled) {
|
|
112
|
+
removeAllStrips()
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const rows = node.querySelectorAll<HTMLTableRowElement>('tr.sv-grid-row')
|
|
116
|
+
for (const tr of rows) {
|
|
117
|
+
if (tr.classList.contains('sv-grid-header-row')) continue
|
|
118
|
+
if (tr.classList.contains('sv-grid-row-spacer')) continue
|
|
119
|
+
const gutter = tr.querySelector<HTMLTableCellElement>(
|
|
120
|
+
'.sv-grid-cell.sv-row-gutter',
|
|
121
|
+
)
|
|
122
|
+
if (!gutter) continue
|
|
123
|
+
if (gutter.querySelector(`:scope > .${STRIP_CLASS}`)) continue
|
|
124
|
+
// Anchor the strip to the gutter cell + allow it to overflow
|
|
125
|
+
// downward into the row gap so the hit-area straddles the
|
|
126
|
+
// row boundary the way Excel does.
|
|
127
|
+
if (getComputedStyle(gutter).position === 'static') gutter.style.position = 'relative'
|
|
128
|
+
gutter.style.overflow = 'visible'
|
|
129
|
+
const strip = document.createElement('div')
|
|
130
|
+
strip.className = STRIP_CLASS
|
|
131
|
+
strip.setAttribute('role', 'separator')
|
|
132
|
+
strip.setAttribute('aria-orientation', 'horizontal')
|
|
133
|
+
strip.setAttribute('aria-label', 'Resize row')
|
|
134
|
+
// Inline the geometry + pointer-events so the strip works even
|
|
135
|
+
// before SvGrid.css is parsed; the hover tint still comes from
|
|
136
|
+
// the stylesheet. We keep the strip fully inside the gutter
|
|
137
|
+
// cell (no negative offset) to avoid stacking-context surprises
|
|
138
|
+
// with pinned / virtualised neighbour cells.
|
|
139
|
+
// 5px tall + sitting flush with the row bottom matches the
|
|
140
|
+
// column resize handle's 5px width.
|
|
141
|
+
strip.style.cssText =
|
|
142
|
+
'position:absolute;left:0;right:0;bottom:0;height:5px;' +
|
|
143
|
+
'cursor:row-resize;user-select:none;z-index:60;pointer-events:auto;'
|
|
144
|
+
gutter.appendChild(strip)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function removeAllStrips() {
|
|
148
|
+
node.querySelectorAll(`.${STRIP_CLASS}`).forEach((el) => el.remove())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
node.addEventListener('pointerdown', onPointerDown, { capture: true })
|
|
152
|
+
const observer = new MutationObserver(() => decorate())
|
|
153
|
+
observer.observe(node, { childList: true, subtree: true })
|
|
154
|
+
decorate()
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
update(next: RowResizeOptions) {
|
|
158
|
+
const wasDisabled = current.disabled
|
|
159
|
+
current = next
|
|
160
|
+
// Toggle: when going from enabled <-> disabled, repaint immediately.
|
|
161
|
+
if (wasDisabled !== current.disabled) decorate()
|
|
162
|
+
},
|
|
163
|
+
destroy() {
|
|
164
|
+
node.removeEventListener('pointerdown', onPointerDown, { capture: true })
|
|
165
|
+
window.removeEventListener('pointermove', onPointerMove)
|
|
166
|
+
window.removeEventListener('pointerup', onPointerUp)
|
|
167
|
+
observer.disconnect()
|
|
168
|
+
removeAllStrips()
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createScrollSync } from './scroll-sync'
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Fake controller `ctx` for the scroll-sync handlers. As with the other
|
|
6
|
+
// controller-slice tests, the handlers read/write everything through `ctx`,
|
|
7
|
+
// so a hand-rolled stub drives every branch. Where the handler reads real DOM
|
|
8
|
+
// geometry (tooltip placement, scroll offsets) we build genuine elements and
|
|
9
|
+
// stub getBoundingClientRect / scroll metrics.
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
function makeCtx(overrides: Record<string, unknown> = {}) {
|
|
13
|
+
const ctx: any = {
|
|
14
|
+
tooltip: null,
|
|
15
|
+
tooltipTimer: null,
|
|
16
|
+
scrollSyncRaf: null,
|
|
17
|
+
scrollVersion: 0,
|
|
18
|
+
pendingScrollTop: 0,
|
|
19
|
+
pendingScrollLeft: 0,
|
|
20
|
+
rowVirtualizationEnabled: false,
|
|
21
|
+
columnVirtualizationEnabled: false,
|
|
22
|
+
scrollBottomArmed: true,
|
|
23
|
+
columnMenuFor: null,
|
|
24
|
+
operatorMenuFor: null,
|
|
25
|
+
closeMenus: vi.fn(),
|
|
26
|
+
domToLogicalRowOffset: (n: number) => n,
|
|
27
|
+
virtualizer: { setScrollOffset: vi.fn() },
|
|
28
|
+
columnVirtualizer: { setHorizontalOffset: vi.fn() },
|
|
29
|
+
props: {},
|
|
30
|
+
...overrides,
|
|
31
|
+
}
|
|
32
|
+
return ctx
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const syncFor = (overrides?: Record<string, unknown>) => {
|
|
36
|
+
const ctx = makeCtx(overrides)
|
|
37
|
+
return { ctx, ss: createScrollSync(ctx) }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('showTooltipFor / hideTooltip', () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.useFakeTimers()
|
|
43
|
+
})
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
vi.useRealTimers()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
function anchorEl(rect: Partial<DOMRect>) {
|
|
49
|
+
const el = document.createElement('div')
|
|
50
|
+
el.getBoundingClientRect = () =>
|
|
51
|
+
({ left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0, x: 0, y: 0, ...rect }) as DOMRect
|
|
52
|
+
return el
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
it('does nothing when there is no text', () => {
|
|
56
|
+
const { ctx, ss } = syncFor()
|
|
57
|
+
ss.showTooltipFor(anchorEl({}), '')
|
|
58
|
+
expect(ctx.tooltipTimer).toBeNull()
|
|
59
|
+
vi.runAllTimers()
|
|
60
|
+
expect(ctx.tooltip).toBeNull()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('schedules a tooltip after the debounce, positioned below the anchor', () => {
|
|
64
|
+
const { ctx, ss } = syncFor()
|
|
65
|
+
// Anchor near the top of a tall viewport -> tooltip appears below.
|
|
66
|
+
vi.stubGlobal('innerWidth', 1000)
|
|
67
|
+
vi.stubGlobal('innerHeight', 800)
|
|
68
|
+
const el = anchorEl({ left: 100, right: 140, top: 50, bottom: 70 })
|
|
69
|
+
ss.showTooltipFor(el, 'hello')
|
|
70
|
+
expect(ctx.tooltipTimer).not.toBeNull()
|
|
71
|
+
expect(ctx.tooltip).toBeNull() // not yet flushed
|
|
72
|
+
vi.advanceTimersByTime(250)
|
|
73
|
+
expect(ctx.tooltip).toEqual({
|
|
74
|
+
text: 'hello',
|
|
75
|
+
x: 106, // left + 6, within clamp range
|
|
76
|
+
y: 76, // bottom + 6 since "below"
|
|
77
|
+
below: true,
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('flips above the anchor when there is no room below', () => {
|
|
82
|
+
const { ctx, ss } = syncFor()
|
|
83
|
+
vi.stubGlobal('innerWidth', 1000)
|
|
84
|
+
vi.stubGlobal('innerHeight', 200)
|
|
85
|
+
// bottom + 64 is NOT < 200, so the tooltip renders above.
|
|
86
|
+
const el = anchorEl({ left: 100, right: 140, top: 150, bottom: 170 })
|
|
87
|
+
ss.showTooltipFor(el, 'world')
|
|
88
|
+
vi.advanceTimersByTime(250)
|
|
89
|
+
expect(ctx.tooltip.below).toBe(false)
|
|
90
|
+
expect(ctx.tooltip.y).toBe(144) // top - 6
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('clamps x so a wide tooltip stays inside the viewport', () => {
|
|
94
|
+
const { ctx, ss } = syncFor()
|
|
95
|
+
vi.stubGlobal('innerWidth', 300)
|
|
96
|
+
vi.stubGlobal('innerHeight', 800)
|
|
97
|
+
const el = anchorEl({ left: 280, right: 300, top: 10, bottom: 30 })
|
|
98
|
+
ss.showTooltipFor(el, 'edge')
|
|
99
|
+
vi.advanceTimersByTime(250)
|
|
100
|
+
// x clamped to vw - 290 = 10.
|
|
101
|
+
expect(ctx.tooltip.x).toBe(10)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('clears a pending timer before scheduling a new tooltip', () => {
|
|
105
|
+
const { ctx, ss } = syncFor()
|
|
106
|
+
const clearSpy = vi.spyOn(window, 'clearTimeout')
|
|
107
|
+
const el = anchorEl({ left: 10, right: 30, top: 10, bottom: 30 })
|
|
108
|
+
ss.showTooltipFor(el, 'first')
|
|
109
|
+
ss.showTooltipFor(el, 'second')
|
|
110
|
+
expect(clearSpy).toHaveBeenCalled()
|
|
111
|
+
clearSpy.mockRestore()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('hideTooltip clears the pending timer and the tooltip', () => {
|
|
115
|
+
const { ctx, ss } = syncFor()
|
|
116
|
+
const el = anchorEl({ left: 10, right: 30, top: 10, bottom: 30 })
|
|
117
|
+
ss.showTooltipFor(el, 'gone')
|
|
118
|
+
ctx.tooltip = { text: 'x', x: 0, y: 0, below: true }
|
|
119
|
+
ss.hideTooltip()
|
|
120
|
+
expect(ctx.tooltipTimer).toBeNull()
|
|
121
|
+
expect(ctx.tooltip).toBeNull()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('hideTooltip is safe when no timer is pending', () => {
|
|
125
|
+
const { ctx, ss } = syncFor()
|
|
126
|
+
ctx.tooltip = { text: 'x', x: 0, y: 0, below: true }
|
|
127
|
+
ss.hideTooltip()
|
|
128
|
+
expect(ctx.tooltip).toBeNull()
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
describe('scheduleScrollSync / flushScheduledScrollSync', () => {
|
|
133
|
+
let rafCb: FrameRequestCallback | null = null
|
|
134
|
+
let rafCalls = 0
|
|
135
|
+
beforeEach(() => {
|
|
136
|
+
rafCb = null
|
|
137
|
+
rafCalls = 0
|
|
138
|
+
vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {
|
|
139
|
+
rafCb = cb
|
|
140
|
+
rafCalls += 1
|
|
141
|
+
return 42
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
afterEach(() => {
|
|
145
|
+
vi.unstubAllGlobals()
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('records the pending offsets and schedules a single rAF', () => {
|
|
149
|
+
const { ctx, ss } = syncFor()
|
|
150
|
+
ss.scheduleScrollSync(120, 45)
|
|
151
|
+
expect(ctx.pendingScrollTop).toBe(120)
|
|
152
|
+
expect(ctx.pendingScrollLeft).toBe(45)
|
|
153
|
+
expect(ctx.scrollSyncRaf).toBe(42)
|
|
154
|
+
expect(rafCalls).toBe(1)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('coalesces back-to-back calls into one frame (latest position wins)', () => {
|
|
158
|
+
const { ctx, ss } = syncFor()
|
|
159
|
+
ss.scheduleScrollSync(10, 0)
|
|
160
|
+
ss.scheduleScrollSync(200, 5) // raf already pending, must not re-schedule
|
|
161
|
+
expect(rafCalls).toBe(1)
|
|
162
|
+
expect(ctx.pendingScrollTop).toBe(200)
|
|
163
|
+
expect(ctx.pendingScrollLeft).toBe(5)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('flush bumps scrollVersion and clears the raf handle', () => {
|
|
167
|
+
const { ctx, ss } = syncFor()
|
|
168
|
+
ss.scheduleScrollSync(50, 50)
|
|
169
|
+
rafCb?.(0)
|
|
170
|
+
expect(ctx.scrollVersion).toBe(1)
|
|
171
|
+
expect(ctx.scrollSyncRaf).toBeNull()
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('flush pushes the row offset through domToLogicalRowOffset when row virtualization is on', () => {
|
|
175
|
+
const setScrollOffset = vi.fn()
|
|
176
|
+
const { ss } = syncFor({
|
|
177
|
+
rowVirtualizationEnabled: true,
|
|
178
|
+
domToLogicalRowOffset: (n: number) => n * 2,
|
|
179
|
+
virtualizer: { setScrollOffset },
|
|
180
|
+
})
|
|
181
|
+
ss.scheduleScrollSync(30, 0)
|
|
182
|
+
rafCb?.(0)
|
|
183
|
+
expect(setScrollOffset).toHaveBeenCalledWith(60)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('flush pushes the horizontal offset when column virtualization is on', () => {
|
|
187
|
+
const setHorizontalOffset = vi.fn()
|
|
188
|
+
const { ss } = syncFor({
|
|
189
|
+
columnVirtualizationEnabled: true,
|
|
190
|
+
columnVirtualizer: { setHorizontalOffset },
|
|
191
|
+
})
|
|
192
|
+
ss.scheduleScrollSync(0, 88)
|
|
193
|
+
rafCb?.(0)
|
|
194
|
+
expect(setHorizontalOffset).toHaveBeenCalledWith(88)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('flush skips both virtualizers when neither is enabled', () => {
|
|
198
|
+
const { ctx, ss } = syncFor()
|
|
199
|
+
ss.scheduleScrollSync(5, 5)
|
|
200
|
+
rafCb?.(0)
|
|
201
|
+
expect(ctx.virtualizer.setScrollOffset).not.toHaveBeenCalled()
|
|
202
|
+
expect(ctx.columnVirtualizer.setHorizontalOffset).not.toHaveBeenCalled()
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
describe('onBodyScroll', () => {
|
|
207
|
+
let rafCb: FrameRequestCallback | null = null
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
rafCb = null
|
|
210
|
+
vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {
|
|
211
|
+
rafCb = cb
|
|
212
|
+
return 7
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
afterEach(() => {
|
|
216
|
+
vi.unstubAllGlobals()
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
function scrollEvent(container: HTMLElement | null) {
|
|
220
|
+
return { currentTarget: container } as unknown as Event
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function scroller(props: Partial<{
|
|
224
|
+
scrollTop: number
|
|
225
|
+
scrollLeft: number
|
|
226
|
+
scrollHeight: number
|
|
227
|
+
clientHeight: number
|
|
228
|
+
}>) {
|
|
229
|
+
const el = document.createElement('div')
|
|
230
|
+
Object.defineProperty(el, 'scrollTop', { value: props.scrollTop ?? 0, configurable: true })
|
|
231
|
+
Object.defineProperty(el, 'scrollLeft', { value: props.scrollLeft ?? 0, configurable: true })
|
|
232
|
+
Object.defineProperty(el, 'scrollHeight', { value: props.scrollHeight ?? 0, configurable: true })
|
|
233
|
+
Object.defineProperty(el, 'clientHeight', { value: props.clientHeight ?? 0, configurable: true })
|
|
234
|
+
return el
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
it('does nothing when there is no current target', () => {
|
|
238
|
+
const { ctx, ss } = syncFor()
|
|
239
|
+
ss.onBodyScroll(scrollEvent(null))
|
|
240
|
+
expect(ctx.pendingScrollTop).toBe(0)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('records the container scroll offsets via scheduleScrollSync', () => {
|
|
244
|
+
const { ctx, ss } = syncFor()
|
|
245
|
+
const el = scroller({ scrollTop: 90, scrollLeft: 12 })
|
|
246
|
+
ss.onBodyScroll(scrollEvent(el))
|
|
247
|
+
expect(ctx.pendingScrollTop).toBe(90)
|
|
248
|
+
expect(ctx.pendingScrollLeft).toBe(12)
|
|
249
|
+
expect(ctx.scrollSyncRaf).toBe(7)
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('closes open column/operator menus on scroll', () => {
|
|
253
|
+
const closeMenus = vi.fn()
|
|
254
|
+
const { ss } = syncFor({ columnMenuFor: 'x', closeMenus })
|
|
255
|
+
ss.onBodyScroll(scrollEvent(scroller({})))
|
|
256
|
+
expect(closeMenus).toHaveBeenCalled()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('closes menus when an operator menu is open', () => {
|
|
260
|
+
const closeMenus = vi.fn()
|
|
261
|
+
const { ss } = syncFor({ operatorMenuFor: 'op', closeMenus })
|
|
262
|
+
ss.onBodyScroll(scrollEvent(scroller({})))
|
|
263
|
+
expect(closeMenus).toHaveBeenCalled()
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('does not close menus when none are open', () => {
|
|
267
|
+
const closeMenus = vi.fn()
|
|
268
|
+
const { ss } = syncFor({ closeMenus })
|
|
269
|
+
ss.onBodyScroll(scrollEvent(scroller({})))
|
|
270
|
+
expect(closeMenus).not.toHaveBeenCalled()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('fires onScrollBottomReached once when the scroll reaches the bottom', () => {
|
|
274
|
+
const onScrollBottomReached = vi.fn()
|
|
275
|
+
const { ctx, ss } = syncFor({
|
|
276
|
+
props: { onScrollBottomReached },
|
|
277
|
+
scrollBottomArmed: true,
|
|
278
|
+
})
|
|
279
|
+
// scrollTop + clientHeight >= scrollHeight - 32 -> at bottom.
|
|
280
|
+
const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
|
|
281
|
+
ss.onBodyScroll(scrollEvent(el))
|
|
282
|
+
expect(onScrollBottomReached).toHaveBeenCalledWith({
|
|
283
|
+
scrollTop: 600,
|
|
284
|
+
scrollHeight: 1000,
|
|
285
|
+
clientHeight: 400,
|
|
286
|
+
})
|
|
287
|
+
expect(ctx.scrollBottomArmed).toBe(false) // disarmed so it won't refire
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('does not refire onScrollBottomReached while still at the bottom (disarmed)', () => {
|
|
291
|
+
const onScrollBottomReached = vi.fn()
|
|
292
|
+
const { ss } = syncFor({
|
|
293
|
+
props: { onScrollBottomReached },
|
|
294
|
+
scrollBottomArmed: false,
|
|
295
|
+
})
|
|
296
|
+
const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
|
|
297
|
+
ss.onBodyScroll(scrollEvent(el))
|
|
298
|
+
expect(onScrollBottomReached).not.toHaveBeenCalled()
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
it('re-arms the trigger after scrolling away from the bottom', () => {
|
|
302
|
+
const onScrollBottomReached = vi.fn()
|
|
303
|
+
const { ctx, ss } = syncFor({
|
|
304
|
+
props: { onScrollBottomReached },
|
|
305
|
+
scrollBottomArmed: false,
|
|
306
|
+
})
|
|
307
|
+
// Not at bottom: scrollTop + clientHeight < scrollHeight - 32.
|
|
308
|
+
const el = scroller({ scrollTop: 100, clientHeight: 400, scrollHeight: 1000 })
|
|
309
|
+
ss.onBodyScroll(scrollEvent(el))
|
|
310
|
+
expect(ctx.scrollBottomArmed).toBe(true)
|
|
311
|
+
expect(onScrollBottomReached).not.toHaveBeenCalled()
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
it('skips the bottom-reached logic entirely when no callback is registered', () => {
|
|
315
|
+
const { ctx, ss } = syncFor({ scrollBottomArmed: true })
|
|
316
|
+
const el = scroller({ scrollTop: 600, clientHeight: 400, scrollHeight: 1000 })
|
|
317
|
+
ss.onBodyScroll(scrollEvent(el))
|
|
318
|
+
// Armed flag is untouched when there is no callback.
|
|
319
|
+
expect(ctx.scrollBottomArmed).toBe(true)
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
describe('createScrollSync wiring', () => {
|
|
324
|
+
it('exposes the documented handler surface', () => {
|
|
325
|
+
const { ss } = syncFor()
|
|
326
|
+
expect(Object.keys(ss).sort()).toEqual(
|
|
327
|
+
['flushScheduledScrollSync', 'hideTooltip', 'onBodyScroll', 'scheduleScrollSync', 'showTooltipFor'],
|
|
328
|
+
)
|
|
329
|
+
})
|
|
330
|
+
})
|