-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathindex.ts
More file actions
150 lines (139 loc) · 4.21 KB
/
index.ts
File metadata and controls
150 lines (139 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
DestroyRef,
afterNextRender,
computed,
effect,
inject,
signal,
untracked,
} from '@angular/core'
import {
Virtualizer,
elementScroll,
observeElementOffset,
observeElementRect,
observeWindowOffset,
observeWindowRect,
windowScroll,
} from '@tanstack/virtual-core'
import { proxyVirtualizer } from './proxy'
import type { ElementRef, Signal } from '@angular/core'
import type {
Key,
PartialKeys,
VirtualizerInputOptions,
VirtualizerOptions,
} from '@tanstack/virtual-core'
import type { AngularVirtualizer } from './types'
export * from '@tanstack/virtual-core'
export * from './types'
function createVirtualizerBase<
TScrollElement extends Element | Window,
TItemElement extends Element,
TKey extends Key = Key,
>(
options: Signal<VirtualizerOptions<TScrollElement, TItemElement, TKey>>,
): AngularVirtualizer<TScrollElement, TItemElement, TKey> {
let virtualizer: Virtualizer<TScrollElement, TItemElement, TKey>
function lazyInit() {
virtualizer ??= new Virtualizer(
options() as VirtualizerInputOptions<TScrollElement, TItemElement, TKey>,
)
return virtualizer
}
const virtualizerSignal = signal(virtualizer!, { equal: () => false })
// two-way sync options
effect(
() => {
const _options = options()
lazyInit()
virtualizerSignal.set(virtualizer)
virtualizer.setOptions({
..._options,
onChange: (instance, sync) => {
// update virtualizerSignal so that dependent computeds recompute.
virtualizerSignal.set(instance)
_options.onChange?.(instance, sync)
},
})
// update virtualizerSignal so that dependent computeds recompute.
virtualizerSignal.set(virtualizer)
},
{ allowSignalWrites: true },
)
const scrollElement = computed(() => options().getScrollElement())
// let the virtualizer know when the scroll element is changed
effect(
() => {
const el = scrollElement()
if (el) {
untracked(virtualizerSignal)._willUpdate()
}
},
{ allowSignalWrites: true },
)
let cleanup: () => void | undefined
afterNextRender({ read: () => (virtualizer ?? lazyInit())._didMount() })
inject(DestroyRef).onDestroy(() => cleanup?.())
return proxyVirtualizer(virtualizerSignal, lazyInit)
}
export function injectVirtualizer<
TScrollElement extends Element,
TItemElement extends Element,
TKey extends Key = Key,
>(
options: () => PartialKeys<
Omit<VirtualizerOptions<TScrollElement, TItemElement, TKey>, 'getScrollElement'>,
'observeElementRect' | 'observeElementOffset' | 'scrollToFn'
> & {
scrollElement: ElementRef<TScrollElement> | TScrollElement | undefined
},
): AngularVirtualizer<TScrollElement, TItemElement, TKey> {
const resolvedOptions = computed(() => {
return {
observeElementRect: observeElementRect,
observeElementOffset: observeElementOffset,
scrollToFn: elementScroll,
getScrollElement: () => {
const elementOrRef = options().scrollElement
return (
(isElementRef(elementOrRef)
? elementOrRef.nativeElement
: elementOrRef) ?? null
)
},
...options(),
}
})
return createVirtualizerBase<TScrollElement, TItemElement, TKey>(resolvedOptions)
}
function isElementRef<T extends Element>(
elementOrRef: ElementRef<T> | T | undefined,
): elementOrRef is ElementRef<T> {
return elementOrRef != null && 'nativeElement' in elementOrRef
}
export function injectWindowVirtualizer<
TItemElement extends Element,
TKey extends Key = Key,
>(
options: () => PartialKeys<
VirtualizerOptions<Window, TItemElement, TKey>,
| 'getScrollElement'
| 'observeElementRect'
| 'observeElementOffset'
| 'scrollToFn'
>,
): AngularVirtualizer<Window, TItemElement, TKey> {
const resolvedOptions = computed(() => {
return {
getScrollElement: () => (typeof document !== 'undefined' ? window : null),
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset: () =>
typeof document !== 'undefined' ? window.scrollY : 0,
...options(),
}
})
return createVirtualizerBase<Window, TItemElement, TKey>(resolvedOptions)
}