Skip to content

Commit

Permalink
Add nested scroll support (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzekang authored Jul 25, 2020
1 parent 785ff39 commit f7d43f5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function buildNode(
continue;
}
let value = n.attributes[name];
value = typeof value === 'boolean' ? '' : value;
value = typeof value === 'boolean' || typeof value === 'number' ? '' : value;
// attribute names start with rr_ are internal attributes added by rrweb
if (!name.startsWith('rr_')) {
const isTextarea = tagName === 'textarea' && name === 'value';
Expand Down Expand Up @@ -220,6 +220,29 @@ export function buildNodeWithSN(
return node as INode;
}

function sideEffects(idNodeMap: idNodeMap) {
for(let id in idNodeMap) {
const node = idNodeMap[id];
const n = node.__sn;
if (n.type !== NodeType.Element) {
continue;
}
const el = node as Node as HTMLElement;
for(const name in n.attributes) {
if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) {
continue;
}
const value = n.attributes[name];
if (name === 'rr_scrollLeft') {
el.scrollLeft = value as number;
}
if (name === 'rr_scrollTop') {
el.scrollTop = value as number;
}
}
}
}

function rebuild(
n: serializedNodeWithId,
doc: Document,
Expand All @@ -229,7 +252,9 @@ function rebuild(
HACK_CSS: boolean = true,
): [Node | null, idNodeMap] {
const idNodeMap: idNodeMap = {};
return [buildNodeWithSN(n, doc, idNodeMap, false, HACK_CSS), idNodeMap];
const node = buildNodeWithSN(n, doc, idNodeMap, false, HACK_CSS);
sideEffects(idNodeMap);
return [node, idNodeMap];
}

export default rebuild;
7 changes: 7 additions & 0 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ function serializeNode(
? 'paused'
: 'played';
}
// scroll
if ((n as HTMLElement).scrollLeft) {
attributes.rr_scrollLeft = (n as HTMLElement).scrollLeft;
}
if ((n as HTMLElement).scrollTop) {
attributes.rr_scrollTop = (n as HTMLElement).scrollTop;
}
if (needBlock) {
const { width, height } = (n as HTMLElement).getBoundingClientRect();
attributes.rr_width = `${width}px`;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type documentTypeNode = {
};

export type attributes = {
[key: string]: string | boolean;
[key: string]: string | number | boolean;
};
export type elementNode = {
type: NodeType.Element;
Expand Down
2 changes: 1 addition & 1 deletion typings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export declare type documentTypeNode = {
systemId: string;
};
export declare type attributes = {
[key: string]: string | boolean;
[key: string]: string | number | boolean;
};
export declare type elementNode = {
type: NodeType.Element;
Expand Down

0 comments on commit f7d43f5

Please sign in to comment.