Skip to content

Commit 8150141

Browse files
committed
feat: ✨ on cursor drag
1 parent ebd1812 commit 8150141

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

src/components/cursor/cursor.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,20 @@ export const Cursor: FC<CursorProps> = ({
3434
areaRef,
3535
deltaScrollLeft,
3636
onCursorDragStart,
37+
onCursorDrag,
3738
onCursorDragEnd,
3839
}) => {
3940
const rowRnd = useRef<RowRndApi>();
40-
const [draggingLeft, setDraggingLeft] = useState<undefined | number>();
41+
const draggingLeft = useRef<undefined | number>();
4142
const [width, setWidth] = useState(Number.MAX_SAFE_INTEGER);
4243

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+
4351
useEffect(() => {
4452
if (areaRef.current) {
4553
const resizeObserver = new ResizeObserver(() => {
@@ -56,8 +64,6 @@ export const Cursor: FC<CursorProps> = ({
5664
<RowDnd
5765
start={startLeft}
5866
ref={rowRnd}
59-
// dragging时,单向同步数据
60-
left={typeof draggingLeft === 'number' ? draggingLeft : parserTimeToPixel(cursorTime, { startLeft, scaleWidth, scale }) - scrollLeft}
6167
parentRef={areaRef}
6268
bounds={{
6369
left: 0,
@@ -77,28 +83,32 @@ export const Cursor: FC<CursorProps> = ({
7783
enableResizing={false}
7884
onDragStart={() => {
7985
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);
8188
}}
8289
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;
8694
}}
8795
onDrag={({ left }, scroll = 0) => {
8896
const scrollLeft = scrollSync.current.state.scrollLeft;
8997

9098
if (!scroll || scrollLeft === 0) {
9199
// 拖拽时,如果当前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;
94102
} else {
95103
// 自动滚动时,如果当前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;
98106
}
99107
}
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);
102112
return false;
103113
}}
104114
>

src/components/row_rnd/row_rnd.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export const RowDnd = React.forwardRef<RowRndApi, RowRndProps>(
4848

4949
//#region [rgba(100,120,156,0.08)] 赋值相关api
5050
useImperativeHandle(ref, () => ({
51-
updateLeft: handleUpdateLeft,
52-
updateWidth: handleUpdateWidth,
51+
updateLeft: (left) => handleUpdateLeft(left || 0, false),
52+
updateWidth: (width) => handleUpdateWidth(width, false),
5353
getLeft: handleGetLeft,
5454
getWidth: handleGetWidth,
5555
}));

src/components/timeline.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ export const Timeline = React.forwardRef<TimelineState, TimelineEditor>((props,
7979
const handleSetCursor = (param: { left?: number; time?: number; updateTime?: boolean }) => {
8080
let { left, time, updateTime = true } = param;
8181
if (typeof left === 'undefined' && typeof time === 'undefined') return;
82-
if (typeof left === 'undefined') left = parserTimeToPixel(time, { startLeft, scale, scaleWidth });
83-
if (typeof time === 'undefined') time = parserPixelToTime(left, { startLeft, scale, scaleWidth });
82+
83+
if (typeof time === 'undefined') {
84+
if (typeof left === 'undefined') left = parserTimeToPixel(time, { startLeft, scale, scaleWidth });
85+
time = parserPixelToTime(left, { startLeft, scale, scaleWidth });
86+
}
8487

8588
let result = true;
8689
if (updateTime) {

src/interface/timeline.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ export interface EditData {
184184
* @description cursor结束拖拽事件
185185
*/
186186
onCursorDragEnd?: (time: number) => void;
187+
/**
188+
* @description cursor拖拽事件
189+
*/
190+
onCursorDrag?: (time: number) => void;
187191
}
188192

189193
export interface TimelineState {

0 commit comments

Comments
 (0)