feat: improve playhead
This commit is contained in:
@ -16,6 +16,8 @@ interface TimelinePlayheadProps {
|
|||||||
rulerRef: React.RefObject<HTMLDivElement>;
|
rulerRef: React.RefObject<HTMLDivElement>;
|
||||||
rulerScrollRef: React.RefObject<HTMLDivElement>;
|
rulerScrollRef: React.RefObject<HTMLDivElement>;
|
||||||
tracksScrollRef: React.RefObject<HTMLDivElement>;
|
tracksScrollRef: React.RefObject<HTMLDivElement>;
|
||||||
|
trackLabelsRef?: React.RefObject<HTMLDivElement>;
|
||||||
|
timelineRef: React.RefObject<HTMLDivElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TimelinePlayhead({
|
export function TimelinePlayhead({
|
||||||
@ -27,6 +29,8 @@ export function TimelinePlayhead({
|
|||||||
rulerRef,
|
rulerRef,
|
||||||
rulerScrollRef,
|
rulerScrollRef,
|
||||||
tracksScrollRef,
|
tracksScrollRef,
|
||||||
|
trackLabelsRef,
|
||||||
|
timelineRef,
|
||||||
}: TimelinePlayheadProps) {
|
}: TimelinePlayheadProps) {
|
||||||
const { playheadPosition, handlePlayheadMouseDown } = useTimelinePlayhead({
|
const { playheadPosition, handlePlayheadMouseDown } = useTimelinePlayhead({
|
||||||
currentTime,
|
currentTime,
|
||||||
@ -38,64 +42,36 @@ export function TimelinePlayhead({
|
|||||||
tracksScrollRef,
|
tracksScrollRef,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Use timeline container height minus a few pixels for breathing room
|
||||||
|
const timelineContainerHeight = timelineRef.current?.offsetHeight || 400;
|
||||||
|
const totalHeight = timelineContainerHeight - 8; // 8px padding from edges
|
||||||
|
|
||||||
|
// Get dynamic track labels width, fallback to 0 if no tracks or no ref
|
||||||
|
const trackLabelsWidth =
|
||||||
|
tracks.length > 0 && trackLabelsRef?.current
|
||||||
|
? trackLabelsRef.current.offsetWidth
|
||||||
|
: 0;
|
||||||
|
const leftPosition =
|
||||||
|
trackLabelsWidth +
|
||||||
|
playheadPosition * TIMELINE_CONSTANTS.PIXELS_PER_SECOND * zoomLevel;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
|
||||||
{/* Playhead in ruler (scrubbable) */}
|
|
||||||
<div
|
<div
|
||||||
className="playhead absolute top-0 bottom-0 w-0.5 bg-red-500 pointer-events-auto z-50 cursor-col-resize"
|
className="playhead absolute pointer-events-auto z-50"
|
||||||
style={{
|
style={{
|
||||||
left: `${playheadPosition * TIMELINE_CONSTANTS.PIXELS_PER_SECOND * zoomLevel}px`,
|
left: `${leftPosition}px`,
|
||||||
|
top: 0,
|
||||||
|
height: `${totalHeight}px`,
|
||||||
|
width: "2px", // Slightly wider for better click target
|
||||||
}}
|
}}
|
||||||
onMouseDown={handlePlayheadMouseDown}
|
onMouseDown={handlePlayheadMouseDown}
|
||||||
>
|
>
|
||||||
<div className="absolute top-1 left-1/2 transform -translate-x-1/2 w-3 h-3 bg-red-500 rounded-full border-2 border-white shadow-sm" />
|
{/* The red line spanning full height */}
|
||||||
|
<div className="absolute left-0 w-0.5 bg-foreground cursor-col-resize h-full" />
|
||||||
|
|
||||||
|
{/* Red dot indicator at the top (in ruler area) */}
|
||||||
|
<div className="absolute top-1 left-1/2 transform -translate-x-1/2 w-3 h-3 bg-foreground rounded-full border-2 border-foreground shadow-sm" />
|
||||||
</div>
|
</div>
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface TimelinePlayheadTracksProps {
|
|
||||||
currentTime: number;
|
|
||||||
duration: number;
|
|
||||||
zoomLevel: number;
|
|
||||||
tracks: TimelineTrack[];
|
|
||||||
seek: (time: number) => void;
|
|
||||||
rulerRef: React.RefObject<HTMLDivElement>;
|
|
||||||
rulerScrollRef: React.RefObject<HTMLDivElement>;
|
|
||||||
tracksScrollRef: React.RefObject<HTMLDivElement>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function TimelinePlayheadTracks({
|
|
||||||
currentTime,
|
|
||||||
duration,
|
|
||||||
zoomLevel,
|
|
||||||
tracks,
|
|
||||||
seek,
|
|
||||||
rulerRef,
|
|
||||||
rulerScrollRef,
|
|
||||||
tracksScrollRef,
|
|
||||||
}: TimelinePlayheadTracksProps) {
|
|
||||||
const { playheadPosition, handlePlayheadMouseDown } = useTimelinePlayhead({
|
|
||||||
currentTime,
|
|
||||||
duration,
|
|
||||||
zoomLevel,
|
|
||||||
seek,
|
|
||||||
rulerRef,
|
|
||||||
rulerScrollRef,
|
|
||||||
tracksScrollRef,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tracks.length === 0) return null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className="absolute top-0 w-0.5 bg-red-500 pointer-events-auto z-50 cursor-col-resize"
|
|
||||||
style={{
|
|
||||||
left: `${playheadPosition * TIMELINE_CONSTANTS.PIXELS_PER_SECOND * zoomLevel}px`,
|
|
||||||
height: `${getTotalTracksHeight(tracks)}px`,
|
|
||||||
}}
|
|
||||||
onMouseDown={handlePlayheadMouseDown}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +84,7 @@ export function useTimelinePlayheadRuler({
|
|||||||
rulerRef,
|
rulerRef,
|
||||||
rulerScrollRef,
|
rulerScrollRef,
|
||||||
tracksScrollRef,
|
tracksScrollRef,
|
||||||
}: Omit<TimelinePlayheadProps, "tracks" | "dynamicTimelineWidth">) {
|
}: Omit<TimelinePlayheadProps, "tracks" | "trackLabelsRef" | "timelineRef">) {
|
||||||
const { handleRulerMouseDown, isDraggingRuler } = useTimelinePlayhead({
|
const { handleRulerMouseDown, isDraggingRuler } = useTimelinePlayhead({
|
||||||
currentTime,
|
currentTime,
|
||||||
duration,
|
duration,
|
||||||
|
@ -46,7 +46,6 @@ import {
|
|||||||
import { TimelineTrackContent } from "./timeline-track";
|
import { TimelineTrackContent } from "./timeline-track";
|
||||||
import {
|
import {
|
||||||
TimelinePlayhead,
|
TimelinePlayhead,
|
||||||
TimelinePlayheadTracks,
|
|
||||||
useTimelinePlayheadRuler,
|
useTimelinePlayheadRuler,
|
||||||
} from "./timeline-playhead";
|
} from "./timeline-playhead";
|
||||||
import type { DragData, TimelineTrack } from "@/types/timeline";
|
import type { DragData, TimelineTrack } from "@/types/timeline";
|
||||||
@ -124,6 +123,7 @@ export function Timeline() {
|
|||||||
// Scroll synchronization and auto-scroll to playhead
|
// Scroll synchronization and auto-scroll to playhead
|
||||||
const rulerScrollRef = useRef<HTMLDivElement>(null);
|
const rulerScrollRef = useRef<HTMLDivElement>(null);
|
||||||
const tracksScrollRef = useRef<HTMLDivElement>(null);
|
const tracksScrollRef = useRef<HTMLDivElement>(null);
|
||||||
|
const trackLabelsRef = useRef<HTMLDivElement>(null);
|
||||||
const isUpdatingRef = useRef(false);
|
const isUpdatingRef = useRef(false);
|
||||||
const lastRulerSync = useRef(0);
|
const lastRulerSync = useRef(0);
|
||||||
const lastTracksSync = useRef(0);
|
const lastTracksSync = useRef(0);
|
||||||
@ -139,6 +139,77 @@ export function Timeline() {
|
|||||||
tracksScrollRef,
|
tracksScrollRef,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Timeline content click to seek handler
|
||||||
|
const handleTimelineContentClick = useCallback(
|
||||||
|
(e: React.MouseEvent) => {
|
||||||
|
// Don't seek if clicking on timeline elements, but still deselect
|
||||||
|
if ((e.target as HTMLElement).closest(".timeline-element")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't seek if clicking on playhead
|
||||||
|
if ((e.target as HTMLElement).closest(".playhead")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't seek if clicking on track labels
|
||||||
|
if ((e.target as HTMLElement).closest("[data-track-labels]")) {
|
||||||
|
clearSelectedElements();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear selected elements when clicking empty timeline area
|
||||||
|
clearSelectedElements();
|
||||||
|
|
||||||
|
// Determine if we're clicking in ruler or tracks area
|
||||||
|
const isRulerClick = (e.target as HTMLElement).closest(
|
||||||
|
"[data-ruler-area]"
|
||||||
|
);
|
||||||
|
|
||||||
|
let mouseX: number;
|
||||||
|
let scrollLeft = 0;
|
||||||
|
|
||||||
|
if (isRulerClick) {
|
||||||
|
// Calculate based on ruler position
|
||||||
|
const rulerContent = rulerScrollRef.current?.querySelector(
|
||||||
|
"[data-radix-scroll-area-viewport]"
|
||||||
|
) as HTMLElement;
|
||||||
|
if (!rulerContent) return;
|
||||||
|
const rect = rulerContent.getBoundingClientRect();
|
||||||
|
mouseX = e.clientX - rect.left;
|
||||||
|
scrollLeft = rulerContent.scrollLeft;
|
||||||
|
} else {
|
||||||
|
// Calculate based on tracks content position
|
||||||
|
const tracksContent = tracksScrollRef.current?.querySelector(
|
||||||
|
"[data-radix-scroll-area-viewport]"
|
||||||
|
) as HTMLElement;
|
||||||
|
if (!tracksContent) return;
|
||||||
|
const rect = tracksContent.getBoundingClientRect();
|
||||||
|
mouseX = e.clientX - rect.left;
|
||||||
|
scrollLeft = tracksContent.scrollLeft;
|
||||||
|
}
|
||||||
|
|
||||||
|
const time = Math.max(
|
||||||
|
0,
|
||||||
|
Math.min(
|
||||||
|
duration,
|
||||||
|
(mouseX + scrollLeft) /
|
||||||
|
(TIMELINE_CONSTANTS.PIXELS_PER_SECOND * zoomLevel)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
seek(time);
|
||||||
|
},
|
||||||
|
[
|
||||||
|
duration,
|
||||||
|
zoomLevel,
|
||||||
|
seek,
|
||||||
|
rulerScrollRef,
|
||||||
|
tracksScrollRef,
|
||||||
|
clearSelectedElements,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
// Update timeline duration when tracks change
|
// Update timeline duration when tracks change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const totalDuration = getTotalDuration();
|
const totalDuration = getTotalDuration();
|
||||||
@ -224,14 +295,6 @@ export function Timeline() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add new click handler for deselection
|
|
||||||
const handleTimelineClick = (e: React.MouseEvent) => {
|
|
||||||
// If clicking empty area (not on an element) and not starting marquee, deselect all elements
|
|
||||||
if (!(e.target as HTMLElement).closest(".timeline-element")) {
|
|
||||||
clearSelectedElements();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mouse move to update marquee
|
// Mouse move to update marquee
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!marquee || !marquee.active) return;
|
if (!marquee || !marquee.active) return;
|
||||||
@ -802,7 +865,22 @@ export function Timeline() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Timeline Container */}
|
{/* Timeline Container */}
|
||||||
<div className="flex-1 flex flex-col overflow-hidden" ref={timelineRef}>
|
<div
|
||||||
|
className="flex-1 flex flex-col overflow-hidden relative"
|
||||||
|
ref={timelineRef}
|
||||||
|
>
|
||||||
|
<TimelinePlayhead
|
||||||
|
currentTime={currentTime}
|
||||||
|
duration={duration}
|
||||||
|
zoomLevel={zoomLevel}
|
||||||
|
tracks={tracks}
|
||||||
|
seek={seek}
|
||||||
|
rulerRef={rulerRef}
|
||||||
|
rulerScrollRef={rulerScrollRef}
|
||||||
|
tracksScrollRef={tracksScrollRef}
|
||||||
|
trackLabelsRef={trackLabelsRef}
|
||||||
|
timelineRef={timelineRef}
|
||||||
|
/>
|
||||||
{/* Timeline Header with Ruler */}
|
{/* Timeline Header with Ruler */}
|
||||||
<div className="flex bg-panel sticky top-0 z-10">
|
<div className="flex bg-panel sticky top-0 z-10">
|
||||||
{/* Track Labels Header */}
|
{/* Track Labels Header */}
|
||||||
@ -817,17 +895,16 @@ export function Timeline() {
|
|||||||
<div
|
<div
|
||||||
className="flex-1 relative overflow-hidden h-4"
|
className="flex-1 relative overflow-hidden h-4"
|
||||||
onWheel={handleWheel}
|
onWheel={handleWheel}
|
||||||
|
onClick={handleTimelineContentClick}
|
||||||
|
data-ruler-area
|
||||||
>
|
>
|
||||||
<ScrollArea className="w-full" ref={rulerScrollRef}>
|
<ScrollArea className="w-full" ref={rulerScrollRef}>
|
||||||
<div
|
<div
|
||||||
ref={rulerRef}
|
ref={rulerRef}
|
||||||
className={`relative h-4 select-none ${
|
className="relative h-4 select-none cursor-pointer"
|
||||||
isDraggingRuler ? "cursor-grabbing" : "cursor-grab"
|
|
||||||
}`}
|
|
||||||
style={{
|
style={{
|
||||||
width: `${dynamicTimelineWidth}px`,
|
width: `${dynamicTimelineWidth}px`,
|
||||||
}}
|
}}
|
||||||
onMouseDown={handleRulerMouseDown}
|
|
||||||
>
|
>
|
||||||
{/* Time markers */}
|
{/* Time markers */}
|
||||||
{(() => {
|
{(() => {
|
||||||
@ -896,18 +973,6 @@ export function Timeline() {
|
|||||||
);
|
);
|
||||||
}).filter(Boolean);
|
}).filter(Boolean);
|
||||||
})()}
|
})()}
|
||||||
|
|
||||||
{/* Playhead in ruler */}
|
|
||||||
<TimelinePlayhead
|
|
||||||
currentTime={currentTime}
|
|
||||||
duration={duration}
|
|
||||||
zoomLevel={zoomLevel}
|
|
||||||
tracks={tracks}
|
|
||||||
seek={seek}
|
|
||||||
rulerRef={rulerRef}
|
|
||||||
rulerScrollRef={rulerScrollRef}
|
|
||||||
tracksScrollRef={tracksScrollRef}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
</div>
|
</div>
|
||||||
@ -917,7 +982,11 @@ export function Timeline() {
|
|||||||
<div className="flex-1 flex overflow-hidden">
|
<div className="flex-1 flex overflow-hidden">
|
||||||
{/* Track Labels */}
|
{/* Track Labels */}
|
||||||
{tracks.length > 0 && (
|
{tracks.length > 0 && (
|
||||||
<div className="w-48 flex-shrink-0 border-r bg-panel-accent overflow-y-auto">
|
<div
|
||||||
|
ref={trackLabelsRef}
|
||||||
|
className="w-48 flex-shrink-0 border-r bg-panel-accent overflow-y-auto"
|
||||||
|
data-track-labels
|
||||||
|
>
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
{tracks.map((track) => (
|
{tracks.map((track) => (
|
||||||
<div
|
<div
|
||||||
@ -943,6 +1012,7 @@ export function Timeline() {
|
|||||||
<div
|
<div
|
||||||
className="flex-1 relative overflow-hidden"
|
className="flex-1 relative overflow-hidden"
|
||||||
onWheel={handleWheel}
|
onWheel={handleWheel}
|
||||||
|
onClick={handleTimelineContentClick}
|
||||||
>
|
>
|
||||||
<ScrollArea className="w-full h-full" ref={tracksScrollRef}>
|
<ScrollArea className="w-full h-full" ref={tracksScrollRef}>
|
||||||
<div
|
<div
|
||||||
@ -951,7 +1021,6 @@ export function Timeline() {
|
|||||||
height: `${Math.max(200, Math.min(800, getTotalTracksHeight(tracks)))}px`,
|
height: `${Math.max(200, Math.min(800, getTotalTracksHeight(tracks)))}px`,
|
||||||
width: `${dynamicTimelineWidth}px`,
|
width: `${dynamicTimelineWidth}px`,
|
||||||
}}
|
}}
|
||||||
onClick={handleTimelineClick}
|
|
||||||
onMouseDown={handleTimelineMouseDown}
|
onMouseDown={handleTimelineMouseDown}
|
||||||
>
|
>
|
||||||
{tracks.length === 0 ? (
|
{tracks.length === 0 ? (
|
||||||
@ -996,18 +1065,6 @@ export function Timeline() {
|
|||||||
</ContextMenuContent>
|
</ContextMenuContent>
|
||||||
</ContextMenu>
|
</ContextMenu>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{/* Playhead for tracks area */}
|
|
||||||
<TimelinePlayheadTracks
|
|
||||||
currentTime={currentTime}
|
|
||||||
duration={duration}
|
|
||||||
zoomLevel={zoomLevel}
|
|
||||||
tracks={tracks}
|
|
||||||
seek={seek}
|
|
||||||
rulerRef={rulerRef}
|
|
||||||
rulerScrollRef={rulerScrollRef}
|
|
||||||
tracksScrollRef={tracksScrollRef}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user