Skip to content

Commit f7d43f5

Browse files
authored
Add nested scroll support (#31)
1 parent 785ff39 commit f7d43f5

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/rebuild.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function buildNode(
102102
continue;
103103
}
104104
let value = n.attributes[name];
105-
value = typeof value === 'boolean' ? '' : value;
105+
value = typeof value === 'boolean' || typeof value === 'number' ? '' : value;
106106
// attribute names start with rr_ are internal attributes added by rrweb
107107
if (!name.startsWith('rr_')) {
108108
const isTextarea = tagName === 'textarea' && name === 'value';
@@ -220,6 +220,29 @@ export function buildNodeWithSN(
220220
return node as INode;
221221
}
222222

223+
function sideEffects(idNodeMap: idNodeMap) {
224+
for(let id in idNodeMap) {
225+
const node = idNodeMap[id];
226+
const n = node.__sn;
227+
if (n.type !== NodeType.Element) {
228+
continue;
229+
}
230+
const el = node as Node as HTMLElement;
231+
for(const name in n.attributes) {
232+
if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) {
233+
continue;
234+
}
235+
const value = n.attributes[name];
236+
if (name === 'rr_scrollLeft') {
237+
el.scrollLeft = value as number;
238+
}
239+
if (name === 'rr_scrollTop') {
240+
el.scrollTop = value as number;
241+
}
242+
}
243+
}
244+
}
245+
223246
function rebuild(
224247
n: serializedNodeWithId,
225248
doc: Document,
@@ -229,7 +252,9 @@ function rebuild(
229252
HACK_CSS: boolean = true,
230253
): [Node | null, idNodeMap] {
231254
const idNodeMap: idNodeMap = {};
232-
return [buildNodeWithSN(n, doc, idNodeMap, false, HACK_CSS), idNodeMap];
255+
const node = buildNodeWithSN(n, doc, idNodeMap, false, HACK_CSS);
256+
sideEffects(idNodeMap);
257+
return [node, idNodeMap];
233258
}
234259

235260
export default rebuild;

src/snapshot.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ function serializeNode(
269269
? 'paused'
270270
: 'played';
271271
}
272+
// scroll
273+
if ((n as HTMLElement).scrollLeft) {
274+
attributes.rr_scrollLeft = (n as HTMLElement).scrollLeft;
275+
}
276+
if ((n as HTMLElement).scrollTop) {
277+
attributes.rr_scrollTop = (n as HTMLElement).scrollTop;
278+
}
272279
if (needBlock) {
273280
const { width, height } = (n as HTMLElement).getBoundingClientRect();
274281
attributes.rr_width = `${width}px`;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type documentTypeNode = {
2020
};
2121

2222
export type attributes = {
23-
[key: string]: string | boolean;
23+
[key: string]: string | number | boolean;
2424
};
2525
export type elementNode = {
2626
type: NodeType.Element;

typings/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export declare type documentTypeNode = {
1717
systemId: string;
1818
};
1919
export declare type attributes = {
20-
[key: string]: string | boolean;
20+
[key: string]: string | number | boolean;
2121
};
2222
export declare type elementNode = {
2323
type: NodeType.Element;

0 commit comments

Comments
 (0)