Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 4c27f26

Browse files
fix: DEV-2706: Fix the misalignment between frame numbers (#1022)
* Fix the misalignment between frame numbers * Remove console.log * Fix seeker position clamping
1 parent c15c84d commit 4c27f26

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/components/Timeline/Seeker.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const Seeker: FC<SeekerProps> = ({
6969
document.addEventListener("mouseup", onMouseUp);
7070
}, [length]);
7171

72-
const onSeekerDrag = useCallback((e) => {
72+
const onSeekerDrag = useCallback((e: globalThis.MouseEvent) => {
7373
const indicator = seekerRef.current!;
7474
const dimensions = rootRef.current!.getBoundingClientRect();
7575
const indicatorWidth = indicator.clientWidth;
@@ -78,15 +78,21 @@ export const Seeker: FC<SeekerProps> = ({
7878
const startOffset = startDrag - dimensions.left - (indicatorWidth / 2);
7979
const parentWidth = dimensions.width;
8080

81-
onSeek?.(clamp(Math.ceil(length * (startOffset / parentWidth)), 0, parentWidth));
82-
83-
const onMouseMove = (e: globalThis.MouseEvent) => {
81+
const jump = (e: globalThis.MouseEvent) => {
8482
const limit = parentWidth - indicator.clientWidth;
8583
const newOffset = clamp(startOffset + (e.pageX - startDrag), 0, limit);
8684
const percent = newOffset / parentWidth;
85+
const newPosition = Math.ceil(length * percent);
8786

88-
onSeek?.(Math.ceil(length * percent));
87+
onSeek?.(newPosition);
8988
};
89+
90+
jump(e);
91+
92+
const onMouseMove = (e: globalThis.MouseEvent) => {
93+
jump(e);
94+
};
95+
9096
const onMouseUp = () => {
9197
document.removeEventListener("mousemove", onMouseMove);
9298
document.removeEventListener("mouseup", onMouseUp);

src/components/Timeline/SideControls/FramesControl.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export const FramesControl: FC<TimelineSideControlProps> = ({
2121
length={duration}
2222
position={position}
2323
onChange={(value) => {
24-
onPositionChange?.(value > 0 ? value + 1 : value);
24+
onPositionChange?.(clamp(value, 0, length));
2525
}}
2626
onFinishEditing={() => {
2727
setInputMode(false);
2828
}}
2929
/>
3030
) : (
31-
<>{Math.round(position)} <span>of {Math.round(duration)}</span></>
31+
<>{Math.round(position + 1)} <span>of {Math.round(duration)}</span></>
3232
)}
3333
</Block>
3434
);
@@ -61,7 +61,7 @@ const FrameInput: FC<FrameInputProps> = ({ length, position, onChange, onFinishE
6161
<input
6262
type="text"
6363
ref={input}
64-
defaultValue={position}
64+
defaultValue={position + 1}
6565
autoFocus
6666
onFocus={() => input.current?.select()}
6767
onKeyDown={(e) => {

src/components/Timeline/Timeline.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ const TimelineComponent: FC<TimelineProps> = ({
6464
});
6565

6666
const setInternalPosition = (newPosition: number) => {
67-
const clampedValue = clamp(newPosition, 1, length);
67+
setCurrentPosition((currentPosition) => {
68+
const clampedValue = clamp(newPosition, 1, length);
6869

69-
if (clampedValue !== currentPosition) {
70-
setCurrentPosition(clampedValue);
71-
handlers.onPositionChange?.(clampedValue);
72-
}
70+
if (clampedValue !== currentPosition) {
71+
handlers.onPositionChange?.(clampedValue);
72+
return clampedValue;
73+
}
74+
75+
return currentPosition;
76+
});
7377
};
7478

7579
const increasePosition: TimelineControlsStepHandler = (_, stepSize) => {

src/components/Timeline/Views/Frames/Frames.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const Frames: FC<TimelineViewProps> = ({
272272
<Elem
273273
name="hover"
274274
style={{ left: roundToStep(hoverOffset, step), marginLeft: timelineStartOffset }}
275-
data-frame={toSteps(currentOffsetX + hoverOffset, step)}
275+
data-frame={toSteps(currentOffsetX + hoverOffset, step) + 1}
276276
/>
277277
)}
278278
</Elem>

0 commit comments

Comments
 (0)