Skip to content

Commit 7c5531e

Browse files
committed
Revert "feat: Remove plugins related code, which is not used (#123)" (#133)
This reverts commit 42ffd92. We need plugins to enable our custom canvas playback method.
1 parent e950bfa commit 7c5531e

File tree

4 files changed

+364
-12
lines changed

4 files changed

+364
-12
lines changed

packages/rrweb/src/record/index.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function record<T = eventWithTime>(
104104
userTriggeredOnInput = false,
105105
collectFonts = false,
106106
inlineImages = false,
107+
plugins,
107108
keepIframeSrcFn = () => false,
108109
ignoreCSSAttributes = new Set([]),
109110
errorHandler,
@@ -192,7 +193,11 @@ function record<T = eventWithTime>(
192193
let incrementalSnapshotCount = 0;
193194

194195
const eventProcessor = (e: eventWithTime): T => {
195-
// We ignore plugins here, as we do not have any
196+
for (const plugin of plugins || []) {
197+
if (plugin.eventProcessor) {
198+
e = plugin.eventProcessor(e);
199+
}
200+
}
196201
if (
197202
packFn &&
198203
// Disable packing events which will be emitted to parent frames.
@@ -308,8 +313,16 @@ function record<T = eventWithTime>(
308313

309314
/**
310315
* Exposes mirror to the plugins
311-
* We ignore plugins here, as we don't use any
312316
*/
317+
for (const plugin of plugins || []) {
318+
if (plugin.getMirror)
319+
plugin.getMirror({
320+
nodeMirror: mirror,
321+
crossOriginIframeMirror: iframeManager.crossOriginIframeMirror,
322+
crossOriginIframeStyleMirror:
323+
iframeManager.crossOriginIframeStyleMirror,
324+
});
325+
}
313326

314327
const processedNodeManager = new ProcessedNodeManager();
315328

@@ -570,7 +583,21 @@ function record<T = eventWithTime>(
570583
processedNodeManager,
571584
canvasManager,
572585
ignoreCSSAttributes,
573-
plugins: [],
586+
plugins:
587+
plugins
588+
?.filter((p) => p.observer)
589+
?.map((p) => ({
590+
observer: p.observer!,
591+
options: p.options,
592+
callback: (payload: object) =>
593+
wrappedEmit({
594+
type: EventType.Plugin,
595+
data: {
596+
plugin: p.name,
597+
payload,
598+
},
599+
}),
600+
})) || [],
574601
});
575602
};
576603

packages/rrweb/src/record/observer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,12 @@ export function initObservers(
13231323
const customElementObserver = initCustomElementObserver(o);
13241324

13251325
// plugins
1326-
// we ignore plugins here, as we don't have any
1326+
const pluginHandlers: listenerHandler[] = [];
1327+
for (const plugin of o.plugins) {
1328+
pluginHandlers.push(
1329+
plugin.observer(plugin.callback, currentWindow, plugin.options),
1330+
);
1331+
}
13271332

13281333
return callbackWrapper(() => {
13291334
mutationBuffers.forEach((b) => b.reset());
@@ -1340,6 +1345,7 @@ export function initObservers(
13401345
fontObserver();
13411346
selectionObserver();
13421347
customElementObserver();
1348+
pluginHandlers.forEach((h) => h());
13431349
});
13441350
}
13451351

packages/rrweb/src/replay/index.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ export class Replayer {
202202

203203
/**
204204
* Exposes mirror to the plugins
205-
* We ignore plugins here, as we don't have any
206205
*/
206+
for (const plugin of this.config.plugins || []) {
207+
if (plugin.getMirror) plugin.getMirror({ nodeMirror: this.mirror });
208+
}
207209

208210
this.emitter.on(ReplayerEvents.Flush, () => {
209211
if (this.usingVirtualDom) {
@@ -234,7 +236,11 @@ export class Replayer {
234236
else if (data.source === IncrementalSource.StyleDeclaration)
235237
this.applyStyleDeclaration(data, styleSheet);
236238
},
237-
// we ignore plugins here, as we don't have any
239+
afterAppend: (node: Node, id: number) => {
240+
for (const plugin of this.config.plugins || []) {
241+
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
242+
}
243+
},
238244
};
239245
if (this.iframe.contentDocument)
240246
try {
@@ -717,7 +723,9 @@ export class Replayer {
717723
castFn();
718724
}
719725

720-
// we ignore plugins here, as we don't have any
726+
for (const plugin of this.config.plugins || []) {
727+
if (plugin.handler) plugin.handler(event, isSync, { replayer: this });
728+
}
721729

722730
this.service.send({ type: 'CAST_EVENT', payload: { event } });
723731

@@ -770,7 +778,13 @@ export class Replayer {
770778
const collected: AppendedIframe[] = [];
771779
const afterAppend = (builtNode: Node, id: number) => {
772780
this.collectIframeAndAttachDocument(collected, builtNode);
773-
// we ignore plugins here, as we don't have any
781+
for (const plugin of this.config.plugins || []) {
782+
if (plugin.onBuild)
783+
plugin.onBuild(builtNode, {
784+
id,
785+
replayer: this,
786+
});
787+
}
774788
};
775789

776790
/**
@@ -863,7 +877,7 @@ export class Replayer {
863877
type TMirror = typeof mirror extends Mirror ? Mirror : RRDOMMirror;
864878

865879
const collected: AppendedIframe[] = [];
866-
const afterAppend = (builtNode: Node, _id: number) => {
880+
const afterAppend = (builtNode: Node, id: number) => {
867881
this.collectIframeAndAttachDocument(collected, builtNode);
868882
const sn = (mirror as TMirror).getMeta(builtNode as unknown as TNode);
869883
if (
@@ -878,7 +892,14 @@ export class Replayer {
878892
}
879893

880894
// Skip the plugin onBuild callback in the virtual dom mode
881-
// we ignore plugins here, as we don't have any
895+
if (this.usingVirtualDom) return;
896+
for (const plugin of this.config.plugins || []) {
897+
if (plugin.onBuild)
898+
plugin.onBuild(builtNode, {
899+
id,
900+
replayer: this,
901+
});
902+
}
882903
};
883904

884905
buildNodeWithSN(mutation.node, {
@@ -1498,7 +1519,13 @@ export class Replayer {
14981519
);
14991520
return;
15001521
}
1501-
// we ignore plugins here, as we don't have any
1522+
const afterAppend = (node: Node | RRNode, id: number) => {
1523+
// Skip the plugin onBuild callback for virtual dom
1524+
if (this.usingVirtualDom) return;
1525+
for (const plugin of this.config.plugins || []) {
1526+
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
1527+
}
1528+
};
15021529

15031530
const target = buildNodeWithSN(mutation.node, {
15041531
doc: targetDoc as Document, // can be Document or RRDocument
@@ -1510,6 +1537,7 @@ export class Replayer {
15101537
* caveat: `afterAppend` only gets called on child nodes of target
15111538
* we have to call it again below when this target was added to the DOM
15121539
*/
1540+
afterAppend,
15131541
}) as Node | RRNode;
15141542

15151543
// legacy data, we should not have -1 siblings any more
@@ -1584,7 +1612,10 @@ export class Replayer {
15841612
} else {
15851613
(parent as TNode).appendChild(target as TNode);
15861614
}
1587-
// we ignore plugins here, as we don't have any
1615+
/**
1616+
* target was added, execute plugin hooks
1617+
*/
1618+
afterAppend(target, mutation.node.id);
15881619

15891620
/**
15901621
* https://github.com/rrweb-io/rrweb/pull/887

0 commit comments

Comments
 (0)