new utility, less duplicated code

This commit is contained in:
Maze Winther 2026-01-18 09:16:47 +01:00
parent d260212b12
commit 7117f17ece
4 changed files with 33 additions and 4 deletions

View File

@ -619,6 +619,15 @@ time-utils.ts
time: number;
fps: number;
}): number
export function getSnappedSeekTime({
rawTime,
duration,
fps,
}: {
rawTime: number;
duration: number;
fps: number;
}): number
transcription-utils.ts
export function isTranscriptionConfigured()

View File

@ -1,7 +1,7 @@
import { useCallback, useRef } from "react";
import type { MutableRefObject, RefObject } from "react";
import { TIMELINE_CONSTANTS } from "@/constants/timeline-constants";
import { snapTimeToFrame } from "@/lib/time-utils";
import { getSnappedSeekTime } from "@/lib/time-utils";
import { useEditor } from "../use-editor";
interface UseTimelineInteractionsProps {
@ -139,7 +139,11 @@ export function useTimelineInteractions({
);
const projectFps = activeProject?.settings.fps || 30;
const time = snapTimeToFrame({ time: rawTime, fps: projectFps });
const time = getSnappedSeekTime({
rawTime,
duration,
fps: projectFps,
});
seek(time);
},
[

View File

@ -1,4 +1,4 @@
import { snapTimeToFrame } from "@/lib/time-utils";
import { getSnappedSeekTime } from "@/lib/time-utils";
import { useState, useEffect, useCallback, useRef } from "react";
import { useEdgeAutoScroll } from "@/hooks/timeline/use-edge-auto-scroll";
import { useEditor } from "../use-editor";
@ -89,7 +89,7 @@ export function useTimelinePlayhead({
);
// use frame snapping for playhead scrubbing
const fps = activeProject.settings.fps;
const time = snapTimeToFrame({ time: rawTime, fps });
const time = getSnappedSeekTime({ rawTime, duration, fps });
setScrubTime(time);
seek(time); // update video preview in real time

View File

@ -182,3 +182,19 @@ export function snapTimeToFrame({
const frame = timeToFrame({ time, fps });
return frameToTime({ frame, fps });
}
export function getSnappedSeekTime({
rawTime,
duration,
fps,
}: {
rawTime: number;
duration: number;
fps: number;
}): number {
const snappedTime = snapTimeToFrame({ time: rawTime, fps });
const frameOffset = fps > 0 ? 1 / fps : 0;
const maxSeekTime =
frameOffset > 0 ? Math.max(0, duration - frameOffset) : duration;
return Math.max(0, Math.min(maxSeekTime, snappedTime));
}