forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-position.ts
More file actions
173 lines (154 loc) · 4.54 KB
/
Copy pathscroll-position.ts
File metadata and controls
173 lines (154 loc) · 4.54 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Injectable, ElementRef, NgZone, OnDestroy } from "@angular/core";
import { ScrollDispatcher, ViewportRuler } from "@angular/cdk/scrolling";
import {
Observable,
Subject,
BehaviorSubject,
fromEvent,
merge,
animationFrameScheduler,
} from "rxjs";
import {
takeUntil,
debounceTime,
throttleTime,
distinctUntilChanged,
map,
startWith,
} from "rxjs/operators";
export interface ScrollState {
isAtBottom: boolean;
scrollTop: number;
scrollHeight: number;
clientHeight: number;
}
@Injectable({
providedIn: "root",
})
export class ScrollPosition implements OnDestroy {
private destroy$ = new Subject<void>();
private scrollStateSubject = new BehaviorSubject<ScrollState>({
isAtBottom: true,
scrollTop: 0,
scrollHeight: 0,
clientHeight: 0,
});
public scrollState$ = this.scrollStateSubject.asObservable();
constructor(
private scrollDispatcher: ScrollDispatcher,
private viewportRuler: ViewportRuler,
private ngZone: NgZone,
) {}
/**
* Monitor scroll position of an element
* @param element The element to monitor
* @param threshold Pixels from bottom to consider "at bottom" (default 10)
*/
monitorScrollPosition(
element: ElementRef<HTMLElement> | HTMLElement,
threshold: number = 10,
): Observable<ScrollState> {
const el = element instanceof ElementRef ? element.nativeElement : element;
// Create scroll observable
const scroll$ = merge(
fromEvent(el, "scroll"),
this.viewportRuler.change(150), // Monitor viewport changes
).pipe(
startWith(null), // Emit initial state
throttleTime(16, animationFrameScheduler, { trailing: true }), // ~60fps
map(() => this.getScrollState(el, threshold)),
distinctUntilChanged(
(a, b) =>
a.isAtBottom === b.isAtBottom &&
a.scrollTop === b.scrollTop &&
a.scrollHeight === b.scrollHeight,
),
takeUntil(this.destroy$),
);
// Subscribe and update subject
scroll$.subscribe((state) => {
this.scrollStateSubject.next(state);
});
return scroll$;
}
/**
* Scroll element to bottom with smooth animation
* @param element The element to scroll
* @param smooth Whether to use smooth scrolling
*/
scrollToBottom(
element: ElementRef<HTMLElement> | HTMLElement,
smooth: boolean = true,
): void {
const el = element instanceof ElementRef ? element.nativeElement : element;
this.ngZone.runOutsideAngular(() => {
if (smooth && "scrollBehavior" in document.documentElement.style) {
el.scrollTo({
top: el.scrollHeight,
behavior: "smooth",
});
} else {
el.scrollTop = el.scrollHeight;
}
});
}
/**
* Check if element is at bottom
* @param element The element to check
* @param threshold Pixels from bottom to consider "at bottom"
*/
isAtBottom(
element: ElementRef<HTMLElement> | HTMLElement,
threshold: number = 10,
): boolean {
const el = element instanceof ElementRef ? element.nativeElement : element;
return this.getScrollState(el, threshold).isAtBottom;
}
/**
* Get current scroll state of element
*/
public getScrollState(element: HTMLElement, threshold: number): ScrollState {
const scrollTop = element.scrollTop;
const scrollHeight = element.scrollHeight;
const clientHeight = element.clientHeight;
const distanceFromBottom = scrollHeight - scrollTop - clientHeight;
const isAtBottom = distanceFromBottom <= threshold;
return {
isAtBottom,
scrollTop,
scrollHeight,
clientHeight,
};
}
/**
* Create a ResizeObserver for element size changes
* @param element The element to observe
* @param debounceMs Debounce time in milliseconds
*/
observeResize(
element: ElementRef<HTMLElement> | HTMLElement,
debounceMs: number = 250,
): Observable<ResizeObserverEntry> {
const el = element instanceof ElementRef ? element.nativeElement : element;
const resize$ = new Subject<ResizeObserverEntry>();
const resizeObserver = new ResizeObserver((entries) => {
const entry = entries[0];
if (entry) {
this.ngZone.run(() => {
resize$.next(entry);
});
}
});
resizeObserver.observe(el);
// Cleanup on destroy
this.destroy$.subscribe(() => {
resizeObserver.disconnect();
});
return resize$.pipe(debounceTime(debounceMs), takeUntil(this.destroy$));
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
this.scrollStateSubject.complete();
}
}