impl #530, observe drag event and replay as mousemove

This commit is contained in:
Yanzhen Yu
2021-04-11 12:18:14 +08:00
parent e8925337d9
commit 693a8faf5e
2 changed files with 38 additions and 16 deletions

View File

@@ -142,19 +142,27 @@ function initMoveObserver(
let positions: mousePosition[] = []; let positions: mousePosition[] = [];
let timeBaseline: number | null; let timeBaseline: number | null;
const wrappedCb = throttle((isTouch: boolean) => { const wrappedCb = throttle(
const totalOffset = Date.now() - timeBaseline!; (
cb( source:
positions.map((p) => { | IncrementalSource.MouseMove
p.timeOffset -= totalOffset; | IncrementalSource.TouchMove
return p; | IncrementalSource.Drag,
}), ) => {
isTouch ? IncrementalSource.TouchMove : IncrementalSource.MouseMove, const totalOffset = Date.now() - timeBaseline!;
); cb(
positions = []; positions.map((p) => {
timeBaseline = null; p.timeOffset -= totalOffset;
}, callbackThreshold); return p;
const updatePosition = throttle<MouseEvent | TouchEvent>( }),
source,
);
positions = [];
timeBaseline = null;
},
callbackThreshold,
);
const updatePosition = throttle<MouseEvent | TouchEvent | DragEvent>(
(evt) => { (evt) => {
const { target } = evt; const { target } = evt;
const { clientX, clientY } = isTouchEvent(evt) const { clientX, clientY } = isTouchEvent(evt)
@@ -169,7 +177,13 @@ function initMoveObserver(
id: mirror.getId(target as INode), id: mirror.getId(target as INode),
timeOffset: Date.now() - timeBaseline, timeOffset: Date.now() - timeBaseline,
}); });
wrappedCb(isTouchEvent(evt)); wrappedCb(
evt instanceof MouseEvent
? IncrementalSource.MouseMove
: evt instanceof DragEvent
? IncrementalSource.Drag
: IncrementalSource.TouchMove,
);
}, },
threshold, threshold,
{ {
@@ -179,6 +193,7 @@ function initMoveObserver(
const handlers = [ const handlers = [
on('mousemove', updatePosition, doc), on('mousemove', updatePosition, doc),
on('touchmove', updatePosition, doc), on('touchmove', updatePosition, doc),
on('drag', updatePosition, doc),
]; ];
return () => { return () => {
handlers.forEach((h) => h()); handlers.forEach((h) => h());

View File

@@ -82,6 +82,7 @@ export enum IncrementalSource {
CanvasMutation, CanvasMutation,
Font, Font,
Log, Log,
Drag,
} }
export type mutationData = { export type mutationData = {
@@ -89,7 +90,10 @@ export type mutationData = {
} & mutationCallbackParam; } & mutationCallbackParam;
export type mousemoveData = { export type mousemoveData = {
source: IncrementalSource.MouseMove | IncrementalSource.TouchMove; source:
| IncrementalSource.MouseMove
| IncrementalSource.TouchMove
| IncrementalSource.Drag;
positions: mousePosition[]; positions: mousePosition[];
}; };
@@ -307,7 +311,10 @@ export type mutationCallBack = (m: mutationCallbackParam) => void;
export type mousemoveCallBack = ( export type mousemoveCallBack = (
p: mousePosition[], p: mousePosition[],
source: IncrementalSource.MouseMove | IncrementalSource.TouchMove, source:
| IncrementalSource.MouseMove
| IncrementalSource.TouchMove
| IncrementalSource.Drag,
) => void; ) => void;
export type mousePosition = { export type mousePosition = {