feat: integrate snapping functionality into timeline track component

This commit is contained in:
Anwarul Islam 2025-07-15 04:27:19 +06:00
parent af632d8cc5
commit 1106aeeb60
1 changed files with 37 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import {
TIMELINE_CONSTANTS,
} from "@/constants/timeline-constants";
import { useProjectStore } from "@/stores/project-store";
import { useTimelineSnapping } from "@/hooks/use-timeline-snapping";
export function TimelineTrackContent({
track,
@ -45,8 +46,25 @@ export function TimelineTrackContent({
endDrag: endDragAction,
clearSelectedElements,
insertTrackAt,
snappingEnabled,
gridSnappingEnabled,
elementSnappingEnabled,
playheadSnappingEnabled,
snapThreshold,
gridInterval,
} = useTimelineStore();
const { currentTime } = usePlaybackStore();
// Initialize snapping hook
const { snapElementPosition } = useTimelineSnapping({
snapThreshold,
gridInterval,
enableGridSnapping: snappingEnabled && gridSnappingEnabled,
enableElementSnapping: snappingEnabled && elementSnappingEnabled,
enablePlayheadSnapping: snappingEnabled && playheadSnappingEnabled,
});
const timelineRef = useRef<HTMLDivElement>(null);
const [isDropping, setIsDropping] = useState(false);
const [dropPosition, setDropPosition] = useState<number | null>(null);
@ -85,12 +103,26 @@ export function TimelineTrackContent({
mouseX / (TIMELINE_CONSTANTS.PIXELS_PER_SECOND * zoomLevel)
);
const adjustedTime = Math.max(0, mouseTime - dragState.clickOffsetTime);
// Use frame snapping if project has FPS, otherwise use decimal snapping
const projectStore = useProjectStore.getState();
const projectFps = projectStore.activeProject?.fps || 30;
const snappedTime = snapTimeToFrame(adjustedTime, projectFps);
updateDragTime(snappedTime);
// Apply snapping if enabled
let finalTime = adjustedTime;
if (snappingEnabled) {
const snapResult = snapElementPosition(
adjustedTime,
tracks,
currentTime,
zoomLevel,
dragState.elementId || undefined
);
finalTime = snapResult.snappedTime;
} else {
// Use frame snapping if project has FPS, otherwise use decimal snapping
const projectStore = useProjectStore.getState();
const projectFps = projectStore.activeProject?.fps || 30;
finalTime = snapTimeToFrame(adjustedTime, projectFps);
}
updateDragTime(finalTime);
};
const handleMouseUp = (e: MouseEvent) => {