fix #62 accept RegExp type block class config

This commit is contained in:
Yanzhen Yu
2019-04-14 16:11:37 +08:00
parent faed623986
commit 2d8d4b0c19
6 changed files with 28 additions and 16 deletions

View File

@@ -26,6 +26,7 @@ import {
hookResetter,
textCursor,
attributeCursor,
blockClass,
} from '../types';
import { deepDelete, isParentRemoved, isParentDropped } from './collection';
@@ -48,7 +49,7 @@ import { deepDelete, isParentRemoved, isParentDropped } from './collection';
*/
function initMutationObserver(
cb: mutationCallBack,
blockClass: string,
blockClass: blockClass,
): MutationObserver {
const observer = new MutationObserver(mutations => {
const texts: textCursor[] = [];
@@ -242,7 +243,7 @@ function initMousemoveObserver(cb: mousemoveCallBack): listenerHandler {
function initMouseInteractionObserver(
cb: mouseInteractionCallBack,
blockClass: string,
blockClass: blockClass,
): listenerHandler {
const handlers: listenerHandler[] = [];
const getHandler = (eventKey: keyof typeof MouseInteractions) => {
@@ -274,7 +275,7 @@ function initMouseInteractionObserver(
function initScrollObserver(
cb: scrollCallback,
blockClass: string,
blockClass: blockClass,
): listenerHandler {
const updatePosition = throttle<UIEvent>(evt => {
if (!evt.target || isBlocked(evt.target as Node, blockClass)) {
@@ -317,7 +318,7 @@ const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
const lastInputValueMap: WeakMap<EventTarget, inputValue> = new WeakMap();
function initInputObserver(
cb: inputCallback,
blockClass: string,
blockClass: blockClass,
ignoreClass: string,
): listenerHandler {
function eventHandler(event: Event) {

View File

@@ -98,11 +98,13 @@ export type eventWithTime = event & {
delay?: number;
};
export type blockClass = string | RegExp;
export type recordOptions = {
emit?: (e: eventWithTime, isCheckout?: boolean) => void;
checkoutEveryNth?: number;
checkoutEveryNms?: number;
blockClass?: string;
blockClass?: blockClass;
ignoreClass?: string;
};
@@ -113,7 +115,7 @@ export type observerParam = {
scrollCb: scrollCallback;
viewportResizeCb: viewportResizeCallback;
inputCb: inputCallback;
blockClass: string;
blockClass: blockClass;
ignoreClass: string;
};

View File

@@ -3,6 +3,7 @@ import {
throttleOptions,
listenerHandler,
hookResetter,
blockClass,
} from './types';
import { INode } from 'rrweb-snapshot';
@@ -113,15 +114,22 @@ export function getWindowWidth(): number {
);
}
export function isBlocked(node: Node | null, blockClass: string): boolean {
export function isBlocked(node: Node | null, blockClass: blockClass): boolean {
if (!node) {
return false;
}
if (node.nodeType === node.ELEMENT_NODE) {
return (
(node as HTMLElement).classList.contains(blockClass) ||
isBlocked(node.parentNode, blockClass)
);
let needBlock = false;
if (typeof blockClass === 'string') {
needBlock = (node as HTMLElement).classList.contains(blockClass);
} else {
(node as HTMLElement).classList.forEach(className => {
if (blockClass.test(className)) {
needBlock = true;
}
});
}
return needBlock || isBlocked(node.parentNode, blockClass);
}
return isBlocked(node.parentNode, blockClass);
}