From 5eb78ff19068b5f0c7d4ccbe95ff0954c8f3b072 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Wed, 15 Jul 2026 21:33:29 -0400 Subject: [PATCH] feat(studio): timeline clip selection seeks off-playhead clips into view --- .../studio/src/player/components/Timeline.tsx | 1 + .../player/components/TimelineLanes.test.tsx | 91 ++++++++++++++++++- .../src/player/components/TimelineLanes.tsx | 5 + 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/packages/studio/src/player/components/Timeline.tsx b/packages/studio/src/player/components/Timeline.tsx index a5b404d76f..54af2513a8 100644 --- a/packages/studio/src/player/components/Timeline.tsx +++ b/packages/studio/src/player/components/Timeline.tsx @@ -519,6 +519,7 @@ export const Timeline = memo(function Timeline({ keyframeCache={keyframeCache} selectedKeyframes={selectedKeyframes} currentTime={currentTime} + onSeek={onSeek} beatAnalysis={adjustedBeatAnalysis} onClickKeyframe={(el, pct) => { usePlayerStore.getState().clearSelectedKeyframes(); diff --git a/packages/studio/src/player/components/TimelineLanes.test.tsx b/packages/studio/src/player/components/TimelineLanes.test.tsx index a1eeecf67e..9cd1f4b6e2 100644 --- a/packages/studio/src/player/components/TimelineLanes.test.tsx +++ b/packages/studio/src/player/components/TimelineLanes.test.tsx @@ -33,6 +33,14 @@ const secondClip: TimelineElement = { track: 0, }; +const laterClip: TimelineElement = { + id: "clip-later", + tag: "div", + start: 5, + duration: 2, + track: 0, +}; + beforeEach(() => { document.body.innerHTML = ""; usePlayerStore.getState().reset(); @@ -43,12 +51,16 @@ function renderLanes({ selectedElementId, selectedElementIds = new Set(), keyframeCache, + currentTime = 0, + onSeek = vi.fn(), onMoveKeyframe, }: { elements?: TimelineElement[]; selectedElementId: string | null; selectedElementIds?: Set; keyframeCache?: Map; + currentTime?: number; + onSeek?: (time: number) => void; onMoveKeyframe?: (elementId: string, fromClipPct: number, toClipPct: number) => void; }) { const host = document.createElement("div"); @@ -90,7 +102,8 @@ function renderLanes({ getTrackStyle={getTrackStyle} keyframeCache={keyframeCache} selectedKeyframes={new Set()} - currentTime={0} + currentTime={currentTime} + onSeek={onSeek} onMoveKeyframe={onMoveKeyframe} onToggleTrackHidden={undefined} onResizeElement={undefined} @@ -101,7 +114,7 @@ function renderLanes({ ); }); - return { host, root, setSelectedElementId }; + return { host, root, setSelectedElementId, onSeek }; } function pointerEvent(type: string, init: PointerEventInit): Event { @@ -118,6 +131,80 @@ function clickClip(host: HTMLElement, id: string): void { } describe("TimelineLanes selection", () => { + it("seeks to a clicked clip when the playhead is before its window", () => { + const harness = renderLanes({ + elements: [laterClip], + selectedElementId: null, + currentTime: 0, + }); + + clickClip(harness.host, laterClip.id); + + expect(harness.onSeek).toHaveBeenCalledWith(laterClip.start); + expect(harness.setSelectedElementId).toHaveBeenCalledWith(laterClip.id); + act(() => harness.root.unmount()); + }); + + it("does not seek when the playhead is inside the clicked clip window", () => { + const harness = renderLanes({ + selectedElementId: null, + currentTime: 0.5, + }); + + clickClip(harness.host, firstClip.id); + + expect(harness.onSeek).not.toHaveBeenCalled(); + expect(harness.setSelectedElementId).toHaveBeenCalledWith(firstClip.id); + act(() => harness.root.unmount()); + }); + + it("does not seek when reclicking a selected clip that contains the playhead", () => { + usePlayerStore.getState().setSelectedElementId(firstClip.id); + const harness = renderLanes({ + selectedElementId: firstClip.id, + currentTime: 0.5, + }); + + clickClip(harness.host, firstClip.id); + + expect(harness.onSeek).not.toHaveBeenCalled(); + expect(harness.setSelectedElementId).toHaveBeenCalledWith(firstClip.id); + act(() => harness.root.unmount()); + }); + + it("seeks to an off-window clip when selecting its keyframe diamond", () => { + const harness = renderLanes({ + elements: [laterClip], + selectedElementId: null, + currentTime: 0, + onMoveKeyframe: vi.fn(), + keyframeCache: new Map([ + [ + laterClip.id, + { + format: "percentage", + keyframes: [ + { percentage: 0, properties: { x: 0 } }, + { percentage: 50, properties: { x: 100 } }, + ], + }, + ], + ]), + }); + const diamond = harness.host.querySelector('button[title="50%"]'); + expect(diamond).not.toBeNull(); + + act(() => { + diamond!.dispatchEvent( + pointerEvent("pointerdown", { bubbles: true, button: 0, clientX: 50 }), + ); + }); + + expect(harness.onSeek).toHaveBeenCalledWith(laterClip.start); + expect(harness.setSelectedElementId).toHaveBeenCalledWith(laterClip.id); + act(() => harness.root.unmount()); + }); + it("writes a plain clip click to store selection only", () => { usePlayerStore.getState().setSelectedElementId(firstClip.id); const harness = renderLanes({ selectedElementId: firstClip.id }); diff --git a/packages/studio/src/player/components/TimelineLanes.tsx b/packages/studio/src/player/components/TimelineLanes.tsx index 2383d222cd..e467ce8ed7 100644 --- a/packages/studio/src/player/components/TimelineLanes.tsx +++ b/packages/studio/src/player/components/TimelineLanes.tsx @@ -70,6 +70,7 @@ export interface TimelineLaneBaseProps { keyframeCache?: Map; selectedKeyframes: Set; currentTime: number; + onSeek?: (time: number) => void; onClickKeyframe?: (element: TimelineElement, percentage: number) => void; onShiftClickKeyframe?: (elementId: string, percentage: number) => void; onContextMenuKeyframe?: (e: React.MouseEvent, elementId: string, percentage: number) => void; @@ -126,6 +127,7 @@ export function TimelineLanes({ keyframeCache, selectedKeyframes, currentTime, + onSeek, onClickKeyframe, onShiftClickKeyframe, onContextMenuKeyframe, @@ -264,6 +266,9 @@ export function TimelineLanes({ const selectClip = () => { usePlayerStore.getState().clearSelectedElementIds(); setSelectedElementId(elementKey); + const playheadInWindow = + currentTime >= el.start && currentTime < el.start + el.duration; + if (!playheadInWindow) onSeek?.(el.start); }; const isComposition = !!el.compositionSrc; // elementKey (el.key ?? el.id) is already unique per clip; do NOT