improve prevent default event when replay

This commit is contained in:
Yanzhen Yu
2018-11-23 18:06:19 +08:00
parent 17fe17b452
commit eab30bd274
4 changed files with 42 additions and 11 deletions

23
index.d.ts vendored
View File

@@ -4,3 +4,26 @@ declare namespace mitt {
(all?: { [key: string]: Array<Handler> }): Emitter; (all?: { [key: string]: Array<Handler> }): Emitter;
} }
} }
declare module 'delegated-events' {
type EventHandler = (event: Event) => any;
type EventListenerOptions = {
capture?: boolean;
document?: Document;
};
export function on(
name: string,
selector: string,
handler: EventHandler,
options?: EventListenerOptions,
): void;
export function off(
name: string,
selector: string,
handler: EventHandler,
options?: EventListenerOptions,
): void;
export function fire(target: EventTarget, name: string, detail?: any): void;
}

View File

@@ -53,6 +53,7 @@
"typescript": "^3.1.6" "typescript": "^3.1.6"
}, },
"dependencies": { "dependencies": {
"delegated-events": "git+https://git@github.com/rrweb-io/delegated-events.git",
"mitt": "^1.1.3", "mitt": "^1.1.3",
"rrweb-snapshot": "^0.6.1" "rrweb-snapshot": "^0.6.1"
} }

View File

@@ -53,8 +53,8 @@ function record(options: recordOptions = {}) {
data: { data: {
node, node,
initialOffset: { initialOffset: {
left: document.documentElement.scrollLeft, left: document.documentElement!.scrollLeft,
top: document.documentElement.scrollTop, top: document.documentElement!.scrollTop,
}, },
}, },
}), }),

View File

@@ -1,5 +1,6 @@
import { rebuild, buildNodeWithSN } from 'rrweb-snapshot'; import { rebuild, buildNodeWithSN } from 'rrweb-snapshot';
import * as mittProxy from 'mitt'; import * as mittProxy from 'mitt';
import { on, off } from 'delegated-events';
import Timer from './timer'; import Timer from './timer';
import { import {
EventType, EventType,
@@ -155,7 +156,7 @@ export class Replayer {
const firstOffset = event.data.positions[0].timeOffset; const firstOffset = event.data.positions[0].timeOffset;
// timeOffset is a negative offset to event.timestamp // timeOffset is a negative offset to event.timestamp
const firstTimestamp = event.timestamp + firstOffset; const firstTimestamp = event.timestamp + firstOffset;
const delay = firstTimestamp - this.baselineTime const delay = firstTimestamp - this.baselineTime;
event.data.positions = event.data.positions.map(p => { event.data.positions = event.data.positions.map(p => {
return { return {
...p, ...p,
@@ -207,17 +208,23 @@ export class Replayer {
) { ) {
mirror.map = rebuild(event.data.node, this.iframe.contentDocument!)[1]; mirror.map = rebuild(event.data.node, this.iframe.contentDocument!)[1];
// avoid form submit to refresh the iframe // avoid form submit to refresh the iframe
this.iframe.contentDocument!.addEventListener('submit', evt => { off('submit', 'form', this.preventDefault, {
if (evt.target && (evt.target as Element).tagName === 'FORM') { document: this.iframe.contentDocument!,
evt.preventDefault(); });
} on('submit', 'form', this.preventDefault, {
document: this.iframe.contentDocument!,
}); });
// avoid a link click to refresh the iframe // avoid a link click to refresh the iframe
this.iframe.contentDocument!.addEventListener('click', evt => { off('click', 'a', this.preventDefault, {
if (evt.target && (evt.target as Element).tagName === 'A') { document: this.iframe.contentDocument!,
evt.preventDefault();
}
}); });
on('click', 'a', this.preventDefault, {
document: this.iframe.contentDocument!,
});
}
private preventDefault(evt: Event) {
evt.preventDefault();
} }
private applyIncremental(d: incrementalData, isSync: boolean) { private applyIncremental(d: incrementalData, isSync: boolean) {