add scroll and viewport resize observers

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 7078ce2f2a
commit ee4fc3ce7e
4 changed files with 121 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
import { snapshot } from 'rrweb-snapshot';
import initObservers from './observer';
import { mirror } from '../utils';
import { mirror, on } from '../utils';
import {
EventType,
event,
@@ -9,14 +9,6 @@ import {
IncrementalSource,
} from '../types';
function on(
type: string,
fn: EventListenerOrEventListenerObject,
target = document,
) {
target.addEventListener(type, fn, { capture: true, passive: true });
}
function wrapEvent(e: event): eventWithTime {
return {
...e,
@@ -80,6 +72,26 @@ function record(options: recordOptions) {
},
}),
),
scrollCb: p =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Scroll,
...p,
},
}),
),
viewportResizeCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.ViewportResize,
...d,
},
}),
),
});
});
} catch (error) {