add mouse interactions observer

This commit is contained in:
Yanzhen Yu
2018-10-08 18:37:40 +08:00
parent a33b24a676
commit 64d4c541ee
3 changed files with 85 additions and 2 deletions

View File

@@ -70,6 +70,16 @@ function record(options: recordOptions) {
},
}),
),
mouseInteractionCb: d =>
emit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.MouseInteraction,
...d,
},
}),
),
});
});
} catch (error) {

View File

@@ -9,6 +9,9 @@ import {
observerParam,
mousemoveCallBack,
mousePosition,
handlerMap,
mouseInteractionCallBack,
MouseInteractions,
} from '../types';
function initMutationObserver(cb: mutationCallBack): MutationObserver {
@@ -139,11 +142,46 @@ function initMousemoveObserver(cb: mousemoveCallBack): () => void {
};
}
function initMouseInteractionObserver(
cb: mouseInteractionCallBack,
): () => void {
const handlers: handlerMap = {};
const getHandler = (eventKey: keyof typeof MouseInteractions) => {
return (event: MouseEvent) => {
const id = mirror.getId(event.target as INode);
const { clientX, clientY } = event;
cb({
type: MouseInteractions[eventKey],
id,
x: clientX,
y: clientY,
});
};
};
Object.keys(MouseInteractions)
.filter(key => Number.isNaN(Number(key)))
.forEach((eventKey: keyof typeof MouseInteractions) => {
const eventName = eventKey.toLowerCase();
const handler = getHandler(eventKey);
handlers[eventName] = handler;
document.addEventListener(eventName, handler);
});
return () => {
Object.keys(handlers).forEach(eventName => {
document.removeEventListener(eventName, handlers[eventName]);
});
};
}
export default function initObservers(o: observerParam) {
const mutationObserver = initMutationObserver(o.mutationCb);
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
const mouseInteractionHandler = initMouseInteractionObserver(
o.mouseInteractionCb,
);
return {
mutationObserver,
mousemoveHandler,
mouseInteractionHandler,
};
}