@@ -34,12 +34,20 @@ export const Cursor: FC<CursorProps> = ({
34
34
areaRef,
35
35
deltaScrollLeft,
36
36
onCursorDragStart,
37
+ onCursorDrag,
37
38
onCursorDragEnd,
38
39
} ) => {
39
40
const rowRnd = useRef < RowRndApi > ( ) ;
40
- const [ draggingLeft , setDraggingLeft ] = useState < undefined | number > ( ) ;
41
+ const draggingLeft = useRef < undefined | number > ( ) ;
41
42
const [ width , setWidth ] = useState ( Number . MAX_SAFE_INTEGER ) ;
42
43
44
+ useEffect ( ( ) => {
45
+ if ( typeof draggingLeft . current === 'undefined' ) {
46
+ // 非dragging时,根据穿参更新cursor刻度
47
+ rowRnd . current . updateLeft ( parserTimeToPixel ( cursorTime , { startLeft, scaleWidth, scale } ) - scrollLeft ) ;
48
+ }
49
+ } , [ cursorTime , startLeft , scaleWidth , scale ] ) ;
50
+
43
51
useEffect ( ( ) => {
44
52
if ( areaRef . current ) {
45
53
const resizeObserver = new ResizeObserver ( ( ) => {
@@ -56,8 +64,6 @@ export const Cursor: FC<CursorProps> = ({
56
64
< RowDnd
57
65
start = { startLeft }
58
66
ref = { rowRnd }
59
- // dragging时,单向同步数据
60
- left = { typeof draggingLeft === 'number' ? draggingLeft : parserTimeToPixel ( cursorTime , { startLeft, scaleWidth, scale } ) - scrollLeft }
61
67
parentRef = { areaRef }
62
68
bounds = { {
63
69
left : 0 ,
@@ -77,28 +83,32 @@ export const Cursor: FC<CursorProps> = ({
77
83
enableResizing = { false }
78
84
onDragStart = { ( ) => {
79
85
onCursorDragStart && onCursorDragStart ( cursorTime ) ;
80
- setDraggingLeft ( parserTimeToPixel ( cursorTime , { startLeft, scaleWidth, scale } ) - scrollLeft ) ;
86
+ draggingLeft . current = parserTimeToPixel ( cursorTime , { startLeft, scaleWidth, scale } ) - scrollLeft ;
87
+ rowRnd . current . updateLeft ( draggingLeft . current ) ;
81
88
} }
82
89
onDragEnd = { ( ) => {
83
- setCursor ( { left : draggingLeft + scrollLeft } ) ;
84
- setDraggingLeft ( undefined ) ;
85
- onCursorDragEnd && onCursorDragEnd ( parserPixelToTime ( draggingLeft + scrollLeft , { startLeft, scale, scaleWidth } ) ) ;
90
+ const time = parserPixelToTime ( draggingLeft . current + scrollLeft , { startLeft, scale, scaleWidth } ) ;
91
+ setCursor ( { time } ) ;
92
+ onCursorDragEnd && onCursorDragEnd ( time ) ;
93
+ draggingLeft . current = undefined ;
86
94
} }
87
95
onDrag = { ( { left } , scroll = 0 ) => {
88
96
const scrollLeft = scrollSync . current . state . scrollLeft ;
89
97
90
98
if ( ! scroll || scrollLeft === 0 ) {
91
99
// 拖拽时,如果当前left < left min,将数值设置为 left min
92
- if ( left < startLeft - scrollLeft ) setDraggingLeft ( startLeft - scrollLeft ) ;
93
- else setDraggingLeft ( left ) ;
100
+ if ( left < startLeft - scrollLeft ) draggingLeft . current = startLeft - scrollLeft ;
101
+ else draggingLeft . current = left ;
94
102
} else {
95
103
// 自动滚动时,如果当前left < left min,将数值设置为 left min
96
- if ( draggingLeft < startLeft - scrollLeft - scroll ) {
97
- setDraggingLeft ( startLeft - scrollLeft - scroll ) ;
104
+ if ( draggingLeft . current < startLeft - scrollLeft - scroll ) {
105
+ draggingLeft . current = startLeft - scrollLeft - scroll ;
98
106
}
99
107
}
100
-
101
- setCursor ( { left : draggingLeft + scrollLeft } ) ;
108
+ rowRnd . current . updateLeft ( draggingLeft . current ) ;
109
+ const time = parserPixelToTime ( draggingLeft . current + scrollLeft , { startLeft, scale, scaleWidth } ) ;
110
+ setCursor ( { time } ) ;
111
+ onCursorDrag && onCursorDrag ( time ) ;
102
112
return false ;
103
113
} }
104
114
>
0 commit comments