diff options
Diffstat (limited to 'apps/web/src/hooks')
| -rw-r--r-- | apps/web/src/hooks/use-editor-actions.ts | 280 | ||||
| -rw-r--r-- | apps/web/src/hooks/use-keybindings.ts | 9 | ||||
| -rw-r--r-- | apps/web/src/hooks/use-timeline-playhead.ts | 153 |
3 files changed, 255 insertions, 187 deletions
diff --git a/apps/web/src/hooks/use-editor-actions.ts b/apps/web/src/hooks/use-editor-actions.ts index bfe9a7b9..b2fad55f 100644 --- a/apps/web/src/hooks/use-editor-actions.ts +++ b/apps/web/src/hooks/use-editor-actions.ts @@ -26,204 +26,136 @@ export function useEditorActions() { const { activeProject } = useProjectStore(); // Playback actions - useActionHandler( - "toggle-play", - () => { - toggle(); - }, - undefined - ); + useActionHandler("toggle-play", () => { + toggle(); + }, undefined); - useActionHandler( - "stop-playback", - () => { - if (isPlaying) { - toggle(); - } - seek(0); - }, - undefined - ); + useActionHandler("stop-playback", () => { + if (isPlaying) { + toggle(); + } + seek(0); + }, undefined); - useActionHandler( - "seek-forward", - (args) => { - const seconds = args?.seconds ?? 1; - seek(Math.min(duration, currentTime + seconds)); - }, - undefined - ); + useActionHandler("seek-forward", (args) => { + const seconds = args?.seconds ?? 1; + seek(Math.min(duration, currentTime + seconds)); + }, undefined); - useActionHandler( - "seek-backward", - (args) => { - const seconds = args?.seconds ?? 1; - seek(Math.max(0, currentTime - seconds)); - }, - undefined - ); + useActionHandler("seek-backward", (args) => { + const seconds = args?.seconds ?? 1; + seek(Math.max(0, currentTime - seconds)); + }, undefined); - useActionHandler( - "frame-step-forward", - () => { - const projectFps = activeProject?.fps || 30; - seek(Math.min(duration, currentTime + 1 / projectFps)); - }, - undefined - ); + useActionHandler("frame-step-forward", () => { + const projectFps = activeProject?.fps || 30; + seek(Math.min(duration, currentTime + 1 / projectFps)); + }, undefined); - useActionHandler( - "frame-step-backward", - () => { - const projectFps = activeProject?.fps || 30; - seek(Math.max(0, currentTime - 1 / projectFps)); - }, - undefined - ); + useActionHandler("frame-step-backward", () => { + const projectFps = activeProject?.fps || 30; + seek(Math.max(0, currentTime - 1 / projectFps)); + }, undefined); - useActionHandler( - "jump-forward", - (args) => { - const seconds = args?.seconds ?? 5; - seek(Math.min(duration, currentTime + seconds)); - }, - undefined - ); + useActionHandler("jump-forward", (args) => { + const seconds = args?.seconds ?? 5; + seek(Math.min(duration, currentTime + seconds)); + }, undefined); - useActionHandler( - "jump-backward", - (args) => { - const seconds = args?.seconds ?? 5; - seek(Math.max(0, currentTime - seconds)); - }, - undefined - ); + useActionHandler("jump-backward", (args) => { + const seconds = args?.seconds ?? 5; + seek(Math.max(0, currentTime - seconds)); + }, undefined); - useActionHandler( - "goto-start", - () => { - seek(0); - }, - undefined - ); + useActionHandler("goto-start", () => { + seek(0); + }, undefined); - useActionHandler( - "goto-end", - () => { - seek(duration); - }, - undefined - ); + useActionHandler("goto-end", () => { + seek(duration); + }, undefined); // Timeline editing actions - useActionHandler( - "split-element", - () => { - if (selectedElements.length !== 1) { - toast.error("Select exactly one element to split"); - return; - } + useActionHandler("split-element", () => { + if (selectedElements.length !== 1) { + toast.error("Select exactly one element to split"); + return; + } - const { trackId, elementId } = selectedElements[0]; - const track = tracks.find((t: any) => t.id === trackId); - const element = track?.elements.find((el: any) => el.id === elementId); + const { trackId, elementId } = selectedElements[0]; + const track = tracks.find((t: any) => t.id === trackId); + const element = track?.elements.find((el: any) => el.id === elementId); - if (element) { - const effectiveStart = element.startTime; - const effectiveEnd = - element.startTime + - (element.duration - element.trimStart - element.trimEnd); + if (element) { + const effectiveStart = element.startTime; + const effectiveEnd = + element.startTime + + (element.duration - element.trimStart - element.trimEnd); - if (currentTime > effectiveStart && currentTime < effectiveEnd) { - splitElement(trackId, elementId, currentTime); - } else { - toast.error("Playhead must be within selected element"); - } + if (currentTime > effectiveStart && currentTime < effectiveEnd) { + splitElement(trackId, elementId, currentTime); + } else { + toast.error("Playhead must be within selected element"); } - }, - undefined - ); + } + }, undefined); - useActionHandler( - "delete-selected", - () => { - if (selectedElements.length === 0) { - return; + useActionHandler("delete-selected", () => { + if (selectedElements.length === 0) { + return; + } + selectedElements.forEach( + ({ trackId, elementId }: { trackId: string; elementId: string }) => { + removeElementFromTrack(trackId, elementId); } - selectedElements.forEach( - ({ trackId, elementId }: { trackId: string; elementId: string }) => { - removeElementFromTrack(trackId, elementId); - } - ); - clearSelectedElements(); - }, - undefined - ); + ); + clearSelectedElements(); + }, undefined); - useActionHandler( - "select-all", - () => { - const allElements = tracks.flatMap((track: any) => - track.elements.map((element: any) => ({ - trackId: track.id, - elementId: element.id, - })) - ); - setSelectedElements(allElements); - }, - undefined - ); + useActionHandler("select-all", () => { + const allElements = tracks.flatMap((track: any) => + track.elements.map((element: any) => ({ + trackId: track.id, + elementId: element.id, + })) + ); + setSelectedElements(allElements); + }, undefined); - useActionHandler( - "duplicate-selected", - () => { - if (selectedElements.length !== 1) { - toast.error("Select exactly one element to duplicate"); - return; - } + useActionHandler("duplicate-selected", () => { + if (selectedElements.length !== 1) { + toast.error("Select exactly one element to duplicate"); + return; + } - const { trackId, elementId } = selectedElements[0]; - const track = tracks.find((t: any) => t.id === trackId); - const element = track?.elements.find((el: any) => el.id === elementId); + const { trackId, elementId } = selectedElements[0]; + const track = tracks.find((t: any) => t.id === trackId); + const element = track?.elements.find((el: any) => el.id === elementId); - if (element) { - const newStartTime = - element.startTime + - (element.duration - element.trimStart - element.trimEnd) + - 0.1; - const { id, ...elementWithoutId } = element; + if (element) { + const newStartTime = + element.startTime + + (element.duration - element.trimStart - element.trimEnd) + + 0.1; + const { id, ...elementWithoutId } = element; - addElementToTrack(trackId, { - ...elementWithoutId, - startTime: newStartTime, - }); - } - }, - undefined - ); + addElementToTrack(trackId, { + ...elementWithoutId, + startTime: newStartTime, + }); + } + }, undefined); - useActionHandler( - "toggle-snapping", - () => { - toggleSnapping(); - }, - undefined - ); + useActionHandler("toggle-snapping", () => { + toggleSnapping(); + }, undefined); // History actions - useActionHandler( - "undo", - () => { - undo(); - }, - undefined - ); + useActionHandler("undo", () => { + undo(); + }, undefined); - useActionHandler( - "redo", - () => { - redo(); - }, - undefined - ); + useActionHandler("redo", () => { + redo(); + }, undefined); } diff --git a/apps/web/src/hooks/use-keybindings.ts b/apps/web/src/hooks/use-keybindings.ts index 10616900..7f6505b0 100644 --- a/apps/web/src/hooks/use-keybindings.ts +++ b/apps/web/src/hooks/use-keybindings.ts @@ -24,6 +24,15 @@ export function useKeybindingsListener() { const boundAction = keybindings[binding]; if (!boundAction) return; + const activeElement = document.activeElement; + const isTextInput = + activeElement && + (activeElement.tagName === "INPUT" || + activeElement.tagName === "TEXTAREA" || + (activeElement as HTMLElement).isContentEditable); + + if (isTextInput) return; + ev.preventDefault(); // Handle actions with default arguments diff --git a/apps/web/src/hooks/use-timeline-playhead.ts b/apps/web/src/hooks/use-timeline-playhead.ts index 008f61a4..ac59a8cd 100644 --- a/apps/web/src/hooks/use-timeline-playhead.ts +++ b/apps/web/src/hooks/use-timeline-playhead.ts @@ -1,6 +1,7 @@ import { snapTimeToFrame } from "@/constants/timeline-constants"; import { useProjectStore } from "@/stores/project-store"; -import { useState, useEffect, useCallback } from "react"; +import { usePlaybackStore } from "@/stores/playback-store"; +import { useState, useEffect, useCallback, useRef } from "react"; interface UseTimelinePlayheadProps { currentTime: number; @@ -31,6 +32,10 @@ export function useTimelinePlayhead({ const [isDraggingRuler, setIsDraggingRuler] = useState(false); const [hasDraggedRuler, setHasDraggedRuler] = useState(false); + // Auto-scroll state during dragging + const autoScrollRef = useRef<number | null>(null); + const lastMouseXRef = useRef<number>(0); + const playheadPosition = isScrubbing && scrubTime !== null ? scrubTime : currentTime; @@ -70,21 +75,110 @@ export function useTimelinePlayhead({ const ruler = rulerRef.current; if (!ruler) return; const rect = ruler.getBoundingClientRect(); - const x = e.clientX - rect.left; + const rawX = e.clientX - rect.left; + + // Get the timeline content width based on duration and zoom + const timelineContentWidth = duration * 50 * zoomLevel; // TIMELINE_CONSTANTS.PIXELS_PER_SECOND = 50 + + // Constrain x to be within the timeline content bounds + const x = Math.max(0, Math.min(timelineContentWidth, rawX)); + const rawTime = Math.max(0, Math.min(duration, x / (50 * zoomLevel))); // Use frame snapping for playhead scrubbing const projectStore = useProjectStore.getState(); const projectFps = projectStore.activeProject?.fps || 30; const time = snapTimeToFrame(rawTime, projectFps); + + // Debug logging + if (rawX < 0 || x !== rawX) { + console.log( + "PLAYHEAD DEBUG:", + JSON.stringify({ + mouseX: e.clientX, + rulerLeft: rect.left, + rawX, + constrainedX: x, + timelineContentWidth, + rawTime, + finalTime: time, + duration, + zoomLevel, + playheadPx: time * 50 * zoomLevel, + }) + ); + } + setScrubTime(time); seek(time); // update video preview in real time + + // Store mouse position for auto-scrolling + lastMouseXRef.current = e.clientX; }, [duration, zoomLevel, seek, rulerRef] ); + // Auto-scroll function during dragging + const performAutoScroll = useCallback(() => { + const rulerViewport = rulerScrollRef.current?.querySelector( + "[data-radix-scroll-area-viewport]" + ) as HTMLElement; + const tracksViewport = tracksScrollRef.current?.querySelector( + "[data-radix-scroll-area-viewport]" + ) as HTMLElement; + + if (!rulerViewport || !tracksViewport || !isScrubbing) return; + + const viewportRect = rulerViewport.getBoundingClientRect(); + const mouseX = lastMouseXRef.current; + const mouseXRelative = mouseX - viewportRect.left; + + const edgeThreshold = 100; // pixels from edge to start scrolling + const maxScrollSpeed = 15; // max pixels per frame + const viewportWidth = rulerViewport.clientWidth; + + // Calculate timeline content boundaries + const timelineContentWidth = duration * 50 * zoomLevel; // TIMELINE_CONSTANTS.PIXELS_PER_SECOND = 50 + const scrollMax = Math.max(0, timelineContentWidth - viewportWidth); + + let scrollSpeed = 0; + + // Check if near left edge (and can scroll left) + if (mouseXRelative < edgeThreshold && rulerViewport.scrollLeft > 0) { + const edgeDistance = Math.max(0, mouseXRelative); + const intensity = 1 - edgeDistance / edgeThreshold; + scrollSpeed = -maxScrollSpeed * intensity; + } + // Check if near right edge (and can scroll right, and haven't reached timeline end) + else if ( + mouseXRelative > viewportWidth - edgeThreshold && + rulerViewport.scrollLeft < scrollMax + ) { + const edgeDistance = Math.max( + 0, + viewportWidth - edgeThreshold - mouseXRelative + ); + const intensity = 1 - edgeDistance / edgeThreshold; + scrollSpeed = maxScrollSpeed * intensity; + } + + if (scrollSpeed !== 0) { + const newScrollLeft = Math.max( + 0, + Math.min(scrollMax, rulerViewport.scrollLeft + scrollSpeed) + ); + rulerViewport.scrollLeft = newScrollLeft; + tracksViewport.scrollLeft = newScrollLeft; + } + + if (isScrubbing) { + autoScrollRef.current = requestAnimationFrame(performAutoScroll); + } + }, [isScrubbing, rulerScrollRef, tracksScrollRef, duration, zoomLevel]); + // Mouse move/up event handlers useEffect(() => { if (!isScrubbing) return; + const onMouseMove = (e: MouseEvent) => { handleScrub(e); // Mark that we've dragged if ruler drag is active @@ -92,11 +186,18 @@ export function useTimelinePlayhead({ setHasDraggedRuler(true); } }; + const onMouseUp = (e: MouseEvent) => { setIsScrubbing(false); if (scrubTime !== null) seek(scrubTime); // finalize seek setScrubTime(null); + // Stop auto-scrolling + if (autoScrollRef.current) { + cancelAnimationFrame(autoScrollRef.current); + autoScrollRef.current = null; + } + // Handle ruler click vs drag if (isDraggingRuler) { setIsDraggingRuler(false); @@ -107,11 +208,20 @@ export function useTimelinePlayhead({ setHasDraggedRuler(false); } }; + window.addEventListener("mousemove", onMouseMove); window.addEventListener("mouseup", onMouseUp); + + // Start auto-scrolling + autoScrollRef.current = requestAnimationFrame(performAutoScroll); + return () => { window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("mouseup", onMouseUp); + if (autoScrollRef.current) { + cancelAnimationFrame(autoScrollRef.current); + autoScrollRef.current = null; + } }; }, [ isScrubbing, @@ -120,10 +230,16 @@ export function useTimelinePlayhead({ handleScrub, isDraggingRuler, hasDraggedRuler, + performAutoScroll, ]); - // --- Playhead auto-scroll effect --- + // --- Playhead auto-scroll effect (only during playback) --- useEffect(() => { + const { isPlaying } = usePlaybackStore.getState(); + + // Only auto-scroll during playback, not during manual interactions + if (!isPlaying || isScrubbing) return; + const rulerViewport = rulerScrollRef.current?.querySelector( "[data-radix-scroll-area-viewport]" ) as HTMLElement; @@ -131,22 +247,33 @@ export function useTimelinePlayhead({ "[data-radix-scroll-area-viewport]" ) as HTMLElement; if (!rulerViewport || !tracksViewport) return; + const playheadPx = playheadPosition * 50 * zoomLevel; // TIMELINE_CONSTANTS.PIXELS_PER_SECOND = 50 const viewportWidth = rulerViewport.clientWidth; const scrollMin = 0; const scrollMax = rulerViewport.scrollWidth - viewportWidth; - // Center the playhead if it's not visible (100px buffer) - const desiredScroll = Math.max( - scrollMin, - Math.min(scrollMax, playheadPx - viewportWidth / 2) - ); - if ( - playheadPx < rulerViewport.scrollLeft + 100 || - playheadPx > rulerViewport.scrollLeft + viewportWidth - 100 - ) { + + // Only auto-scroll if playhead is completely out of view (no buffer) + const needsScroll = + playheadPx < rulerViewport.scrollLeft || + playheadPx > rulerViewport.scrollLeft + viewportWidth; + + if (needsScroll) { + // Center the playhead in the viewport + const desiredScroll = Math.max( + scrollMin, + Math.min(scrollMax, playheadPx - viewportWidth / 2) + ); rulerViewport.scrollLeft = tracksViewport.scrollLeft = desiredScroll; } - }, [playheadPosition, duration, zoomLevel, rulerScrollRef, tracksScrollRef]); + }, [ + playheadPosition, + duration, + zoomLevel, + rulerScrollRef, + tracksScrollRef, + isScrubbing, + ]); return { playheadPosition, |
