forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize-observer.ts
More file actions
187 lines (160 loc) · 5.16 KB
/
Copy pathresize-observer.ts
File metadata and controls
187 lines (160 loc) · 5.16 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Injectable, ElementRef, NgZone, OnDestroy } from "@angular/core";
import { Observable, Subject, BehaviorSubject } from "rxjs";
import { debounceTime, takeUntil, distinctUntilChanged } from "rxjs/operators";
export interface ResizeState {
width: number;
height: number;
isResizing: boolean;
}
@Injectable({
providedIn: "root",
})
export class ResizeObserverService implements OnDestroy {
private destroy$ = new Subject<void>();
private observers = new Map<HTMLElement, ResizeObserver>();
private resizeStates = new Map<HTMLElement, BehaviorSubject<ResizeState>>();
private resizeTimeouts = new Map<HTMLElement, number>();
constructor(private ngZone: NgZone) {}
/**
* Observe element resize with debouncing and resizing state
* @param element Element to observe
* @param debounceMs Debounce time (default 250ms)
* @param resizingDurationMs How long to show "isResizing" state (default 250ms)
*/
observeElement(
element: ElementRef<HTMLElement> | HTMLElement,
debounceMs: number = 0,
resizingDurationMs: number = 250,
): Observable<ResizeState> {
const el = element instanceof ElementRef ? element.nativeElement : element;
// Return existing observer if already observing
if (this.resizeStates.has(el)) {
return this.resizeStates.get(el)!.asObservable();
}
// Create new subject for this element
const resizeState$ = new BehaviorSubject<ResizeState>({
width: el.offsetWidth,
height: el.offsetHeight,
isResizing: false,
});
this.resizeStates.set(el, resizeState$);
// Create ResizeObserver
const resizeObserver = new ResizeObserver((entries) => {
if (entries.length === 0) return;
const entry = entries[0];
if (!entry) return;
const { width, height } = entry.contentRect;
this.ngZone.run(() => {
// Clear existing timeout
const existingTimeout = this.resizeTimeouts.get(el);
if (existingTimeout) {
clearTimeout(existingTimeout);
}
// Update state with isResizing = true
resizeState$.next({
width,
height,
isResizing: true,
});
// Set timeout to clear isResizing flag
if (resizingDurationMs > 0) {
const timeout = window.setTimeout(() => {
resizeState$.next({
width,
height,
isResizing: false,
});
this.resizeTimeouts.delete(el);
}, resizingDurationMs);
this.resizeTimeouts.set(el, timeout);
} else {
// If no duration, immediately set isResizing to false
resizeState$.next({
width,
height,
isResizing: false,
});
}
});
});
// Start observing
resizeObserver.observe(el);
this.observers.set(el, resizeObserver);
// Return observable with debouncing if specified
const observable = resizeState$.asObservable().pipe(
debounceMs > 0 ? debounceTime(debounceMs) : (source) => source,
distinctUntilChanged(
(a, b) =>
a.width === b.width &&
a.height === b.height &&
a.isResizing === b.isResizing,
),
takeUntil(this.destroy$),
);
return observable;
}
/**
* Stop observing an element
* @param element Element to stop observing
*/
unobserve(element: ElementRef<HTMLElement> | HTMLElement): void {
const el = element instanceof ElementRef ? element.nativeElement : element;
// Clear timeout if exists
const timeout = this.resizeTimeouts.get(el);
if (timeout) {
clearTimeout(timeout);
this.resizeTimeouts.delete(el);
}
// Disconnect observer
const observer = this.observers.get(el);
if (observer) {
observer.disconnect();
this.observers.delete(el);
}
// Complete and remove subject
const subject = this.resizeStates.get(el);
if (subject) {
subject.complete();
this.resizeStates.delete(el);
}
}
/**
* Get current size of element
* @param element Element to measure
*/
getCurrentSize(element: ElementRef<HTMLElement> | HTMLElement): {
width: number;
height: number;
} {
const el = element instanceof ElementRef ? element.nativeElement : element;
return {
width: el.offsetWidth,
height: el.offsetHeight,
};
}
/**
* Get current resize state of element
* @param element Element to check
*/
getCurrentState(
element: ElementRef<HTMLElement> | HTMLElement,
): ResizeState | null {
const el = element instanceof ElementRef ? element.nativeElement : element;
const subject = this.resizeStates.get(el);
return subject ? subject.value : null;
}
ngOnDestroy(): void {
// Clear all timeouts
this.resizeTimeouts.forEach((timeout) => clearTimeout(timeout));
this.resizeTimeouts.clear();
// Disconnect all observers
this.observers.forEach((observer) => observer.disconnect());
this.observers.clear();
// Complete all subjects
this.resizeStates.forEach((subject) => subject.complete());
this.resizeStates.clear();
// Complete destroy subject
this.destroy$.next();
this.destroy$.complete();
}
}