Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore classname & block config #25

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 68 additions & 64 deletions src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function wrapEvent(e: event): eventWithTime {
}

function record(options: recordOptions = {}): listenerHandler | undefined {
const { emit } = options;
const { emit, blockClass, ignoreClass } = options;
// runtime checks for user options
if (!emit) {
throw new Error('emit function is required');
Expand Down Expand Up @@ -46,7 +46,7 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
},
}),
);
const [node, idNodeMap] = snapshot(document);
const [node, idNodeMap] = snapshot(document, blockClass);
if (!node) {
return console.warn('Failed to snapshot the document');
}
Expand All @@ -64,68 +64,72 @@ function record(options: recordOptions = {}): listenerHandler | undefined {
}),
);
handlers.push(
initObservers({
mutationCb: m =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Mutation,
...m,
},
}),
),
mousemoveCb: positions =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseMove,
positions,
},
}),
),
mouseInteractionCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
...d,
},
}),
),
scrollCb: p =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Scroll,
...p,
},
}),
),
viewportResizeCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.ViewportResize,
...d,
},
}),
),
inputCb: v =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Input,
...v,
},
}),
),
}),
initObservers(
{
mutationCb: m =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Mutation,
...m,
},
}),
),
mousemoveCb: positions =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseMove,
positions,
},
}),
),
mouseInteractionCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
...d,
},
}),
),
scrollCb: p =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Scroll,
...p,
},
}),
),
viewportResizeCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.ViewportResize,
...d,
},
}),
),
inputCb: v =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Input,
...v,
},
}),
),
},
ignoreClass,
blockClass,
),
);
};
if (
Expand Down
26 changes: 18 additions & 8 deletions src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import {
* which means all the id related calculation should be lazy too.
* @param cb mutationCallBack
*/
function initMutationObserver(cb: mutationCallBack): MutationObserver {
function initMutationObserver(
cb: mutationCallBack,
blockClass: string,
): MutationObserver {
const observer = new MutationObserver(mutations => {
const texts: textCursor[] = [];
const attributes: attributeCursor[] = [];
Expand Down Expand Up @@ -172,7 +175,7 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver {
nextId: !n.nextSibling
? n.nextSibling
: mirror.getId(n.nextSibling as INode),
node: serializeNodeWithId(n, document, mirror.map, true)!,
node: serializeNodeWithId(n, document, mirror.map, blockClass, true)!,
});
} else {
dropped.push(n);
Expand Down Expand Up @@ -331,9 +334,12 @@ const HOOK_PROPERTIES: Array<[HTMLElement, string]> = [
[HTMLSelectElement.prototype, 'value'],
[HTMLTextAreaElement.prototype, 'value'],
];
const IGNORE_CLASS = 'rr-ignore';

const lastInputValueMap: WeakMap<EventTarget, inputValue> = new WeakMap();
function initInputObserver(cb: inputCallback): listenerHandler {
function initInputObserver(
cb: inputCallback,
ignoreClass = 'rr-ignore',
): listenerHandler {
function eventHandler(event: Event) {
const { target } = event;
if (
Expand All @@ -347,7 +353,7 @@ function initInputObserver(cb: inputCallback): listenerHandler {
const type: string | undefined = (target as HTMLInputElement).type;
if (
type === 'password' ||
(target as HTMLElement).classList.contains(IGNORE_CLASS)
(target as HTMLElement).classList.contains(ignoreClass)
) {
return;
}
Expand Down Expand Up @@ -413,15 +419,19 @@ function initInputObserver(cb: inputCallback): listenerHandler {
};
}

export default function initObservers(o: observerParam): listenerHandler {
const mutationObserver = initMutationObserver(o.mutationCb);
export default function initObservers(
o: observerParam,
ignoreClass?: string,
blockClass?: string,
): listenerHandler {
const mutationObserver = initMutationObserver(o.mutationCb, blockClass);
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
const mouseInteractionHandler = initMouseInteractionObserver(
o.mouseInteractionCb,
);
const scrollHandler = initScrollObserver(o.scrollCb);
const viewportResizeHandler = initViewportResizeObserver(o.viewportResizeCb);
const inputHandler = initInputObserver(o.inputCb);
const inputHandler = initInputObserver(o.inputCb, ignoreClass);
return () => {
mutationObserver.disconnect();
mousemoveHandler();
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export type eventWithTime = event & {

export type recordOptions = {
emit?: (e: eventWithTime) => void;
blockClass?: string;
ignoreClass?: string;
};

export type observerParam = {
Expand Down