fix scroll value and record viewport when loaded

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 41b9861fbf
commit 4446e27899
4 changed files with 41 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
import { snapshot } from 'rrweb-snapshot'; import { snapshot } from 'rrweb-snapshot';
import initObservers from './observer'; import initObservers from './observer';
import { mirror, on } from '../utils'; import { mirror, on, getWindowWidth, getWindowHeight } from '../utils';
import { import {
EventType, EventType,
event, event,
@@ -36,7 +36,12 @@ function record(options: recordOptions = {}) {
on( on(
'load', 'load',
() => { () => {
emit(wrapEvent({ type: EventType.Load, data: {} })); emit(
wrapEvent({
type: EventType.Load,
data: { width: getWindowWidth(), height: getWindowHeight() },
}),
);
const [node, idNodeMap] = snapshot(document); const [node, idNodeMap] = snapshot(document);
if (!node) { if (!node) {
return console.warn('Failed to snapshot the document'); return console.warn('Failed to snapshot the document');

View File

@@ -1,5 +1,12 @@
import { INode, serializeNodeWithId } from 'rrweb-snapshot'; import { INode, serializeNodeWithId } from 'rrweb-snapshot';
import { mirror, throttle, on, hookSetter } from '../utils'; import {
mirror,
throttle,
on,
hookSetter,
getWindowHeight,
getWindowWidth,
} from '../utils';
import { import {
mutationCallBack, mutationCallBack,
textMutation, textMutation,
@@ -183,14 +190,14 @@ function initScrollObserver(cb: scrollCallback): listenerHandler {
if (evt.target === document) { if (evt.target === document) {
cb({ cb({
id, id,
x: document.documentElement.scrollTop, x: document.documentElement.scrollLeft,
y: document.documentElement.scrollLeft, y: document.documentElement.scrollTop,
}); });
} else { } else {
cb({ cb({
id, id,
x: (evt.target as HTMLElement).scrollTop, x: (evt.target as HTMLElement).scrollLeft,
y: (evt.target as HTMLElement).scrollLeft, y: (evt.target as HTMLElement).scrollTop,
}); });
} }
}, 100); }, 100);
@@ -201,14 +208,8 @@ function initViewportResizeObserver(
cb: viewportResizeCallback, cb: viewportResizeCallback,
): listenerHandler { ): listenerHandler {
const updateDimension = throttle(() => { const updateDimension = throttle(() => {
const height = const height = getWindowHeight();
window.innerHeight || const width = getWindowWidth();
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight);
const width =
window.innerWidth ||
(document.documentElement && document.documentElement.clientWidth) ||
(document.body && document.body.clientWidth);
cb({ cb({
width: Number(width), width: Number(width),
height: Number(height), height: Number(height),

View File

@@ -16,7 +16,10 @@ export type domContentLoadedEvent = {
export type loadedEvent = { export type loadedEvent = {
type: EventType.Load; type: EventType.Load;
data: {}; data: {
width: number;
height: number;
};
}; };
export type fullSnapshotEvent = { export type fullSnapshotEvent = {

View File

@@ -79,3 +79,19 @@ export function hookSetter<T>(
}); });
return () => hookSetter(target, key, original || {}); return () => hookSetter(target, key, original || {});
} }
export function getWindowHeight(): number {
return (
window.innerHeight ||
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight)
);
}
export function getWindowWidth(): number {
return (
window.innerWidth ||
(document.documentElement && document.documentElement.clientWidth) ||
(document.body && document.body.clientWidth)
);
}