From f555baa4ac4986f9f90e932c5f5a9a80c6d481eb Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] impl #530, observe drag event and replay as mousemove --- src/record/observer.ts | 43 ++++++++++++++++++++++++++++-------------- src/types.ts | 11 +++++++++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/record/observer.ts b/src/record/observer.ts index ac86d0d5..c4fa81d1 100644 --- a/src/record/observer.ts +++ b/src/record/observer.ts @@ -142,19 +142,27 @@ function initMoveObserver( let positions: mousePosition[] = []; let timeBaseline: number | null; - const wrappedCb = throttle((isTouch: boolean) => { - const totalOffset = Date.now() - timeBaseline!; - cb( - positions.map((p) => { - p.timeOffset -= totalOffset; - return p; - }), - isTouch ? IncrementalSource.TouchMove : IncrementalSource.MouseMove, - ); - positions = []; - timeBaseline = null; - }, callbackThreshold); - const updatePosition = throttle( + const wrappedCb = throttle( + ( + source: + | IncrementalSource.MouseMove + | IncrementalSource.TouchMove + | IncrementalSource.Drag, + ) => { + const totalOffset = Date.now() - timeBaseline!; + cb( + positions.map((p) => { + p.timeOffset -= totalOffset; + return p; + }), + source, + ); + positions = []; + timeBaseline = null; + }, + callbackThreshold, + ); + const updatePosition = throttle( (evt) => { const { target } = evt; const { clientX, clientY } = isTouchEvent(evt) @@ -169,7 +177,13 @@ function initMoveObserver( id: mirror.getId(target as INode), timeOffset: Date.now() - timeBaseline, }); - wrappedCb(isTouchEvent(evt)); + wrappedCb( + evt instanceof MouseEvent + ? IncrementalSource.MouseMove + : evt instanceof DragEvent + ? IncrementalSource.Drag + : IncrementalSource.TouchMove, + ); }, threshold, { @@ -179,6 +193,7 @@ function initMoveObserver( const handlers = [ on('mousemove', updatePosition, doc), on('touchmove', updatePosition, doc), + on('drag', updatePosition, doc), ]; return () => { handlers.forEach((h) => h()); diff --git a/src/types.ts b/src/types.ts index 171ec7f8..a7b3d7c9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -82,6 +82,7 @@ export enum IncrementalSource { CanvasMutation, Font, Log, + Drag, } export type mutationData = { @@ -89,7 +90,10 @@ export type mutationData = { } & mutationCallbackParam; export type mousemoveData = { - source: IncrementalSource.MouseMove | IncrementalSource.TouchMove; + source: + | IncrementalSource.MouseMove + | IncrementalSource.TouchMove + | IncrementalSource.Drag; positions: mousePosition[]; }; @@ -307,7 +311,10 @@ export type mutationCallBack = (m: mutationCallbackParam) => void; export type mousemoveCallBack = ( p: mousePosition[], - source: IncrementalSource.MouseMove | IncrementalSource.TouchMove, + source: + | IncrementalSource.MouseMove + | IncrementalSource.TouchMove + | IncrementalSource.Drag, ) => void; export type mousePosition = {