add mouse interactions observer
This commit is contained in:
@@ -70,6 +70,16 @@ function record(options: recordOptions) {
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
mouseInteractionCb: d =>
|
||||||
|
emit(
|
||||||
|
wrapEvent({
|
||||||
|
type: EventType.IncrementalSnapshot,
|
||||||
|
data: {
|
||||||
|
source: IncrementalSource.MouseInteraction,
|
||||||
|
...d,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import {
|
|||||||
observerParam,
|
observerParam,
|
||||||
mousemoveCallBack,
|
mousemoveCallBack,
|
||||||
mousePosition,
|
mousePosition,
|
||||||
|
handlerMap,
|
||||||
|
mouseInteractionCallBack,
|
||||||
|
MouseInteractions,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
function initMutationObserver(cb: mutationCallBack): MutationObserver {
|
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) {
|
export default function initObservers(o: observerParam) {
|
||||||
const mutationObserver = initMutationObserver(o.mutationCb);
|
const mutationObserver = initMutationObserver(o.mutationCb);
|
||||||
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
|
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
|
||||||
|
const mouseInteractionHandler = initMouseInteractionObserver(
|
||||||
|
o.mouseInteractionCb,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
mutationObserver,
|
mutationObserver,
|
||||||
mousemoveHandler,
|
mousemoveHandler,
|
||||||
|
mouseInteractionHandler,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/types.ts
39
src/types.ts
@@ -34,6 +34,7 @@ export type incrementalSnapshotEvent = {
|
|||||||
export enum IncrementalSource {
|
export enum IncrementalSource {
|
||||||
Mutation,
|
Mutation,
|
||||||
MouseMove,
|
MouseMove,
|
||||||
|
MouseInteraction,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type mutationData = {
|
export type mutationData = {
|
||||||
@@ -45,7 +46,14 @@ export type mousemoveData = {
|
|||||||
positions: mousePosition[];
|
positions: mousePosition[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type incrementalData = mutationData | mousemoveData;
|
export type mouseInteractionData = {
|
||||||
|
source: IncrementalSource.MouseInteraction;
|
||||||
|
} & mouseInteractionParam;
|
||||||
|
|
||||||
|
export type incrementalData =
|
||||||
|
| mutationData
|
||||||
|
| mousemoveData
|
||||||
|
| mouseInteractionData;
|
||||||
|
|
||||||
export type event =
|
export type event =
|
||||||
| domContentLoadedEvent
|
| domContentLoadedEvent
|
||||||
@@ -64,6 +72,7 @@ export type recordOptions = {
|
|||||||
export type observerParam = {
|
export type observerParam = {
|
||||||
mutationCb: mutationCallBack;
|
mutationCb: mutationCallBack;
|
||||||
mousemoveCb: mousemoveCallBack;
|
mousemoveCb: mousemoveCallBack;
|
||||||
|
mouseInteractionCb: mouseInteractionCallBack;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type textMutation = {
|
export type textMutation = {
|
||||||
@@ -99,7 +108,7 @@ type mutationCallbackParam = {
|
|||||||
|
|
||||||
export type mutationCallBack = (m: mutationCallbackParam) => void;
|
export type mutationCallBack = (m: mutationCallbackParam) => void;
|
||||||
|
|
||||||
export type mousemoveCallBack = (m: mousePosition[]) => void;
|
export type mousemoveCallBack = (p: mousePosition[]) => void;
|
||||||
|
|
||||||
export type mousePosition = {
|
export type mousePosition = {
|
||||||
x: number;
|
x: number;
|
||||||
@@ -107,6 +116,32 @@ export type mousePosition = {
|
|||||||
timeOffset: number;
|
timeOffset: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type handlerMap = {
|
||||||
|
[key: string]: EventListener;
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum MouseInteractions {
|
||||||
|
MouseUp,
|
||||||
|
MouseDown,
|
||||||
|
Click,
|
||||||
|
ContextMenu,
|
||||||
|
DblClick,
|
||||||
|
Focus,
|
||||||
|
Blur,
|
||||||
|
TouchStart,
|
||||||
|
TouchMove,
|
||||||
|
TouchEnd,
|
||||||
|
}
|
||||||
|
|
||||||
|
type mouseInteractionParam = {
|
||||||
|
type: MouseInteractions;
|
||||||
|
id: number;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type mouseInteractionCallBack = (d: mouseInteractionParam) => void;
|
||||||
|
|
||||||
export type Mirror = {
|
export type Mirror = {
|
||||||
map: idNodeMap;
|
map: idNodeMap;
|
||||||
getId: (n: INode) => number;
|
getId: (n: INode) => number;
|
||||||
|
|||||||
Reference in New Issue
Block a user