refactor(studio): own timeline composition context

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-20 12:23:29 -04:00
parent bf615bca58
commit 154a61e2ea
6 changed files with 184 additions and 108 deletions
@@ -26,7 +26,7 @@ const DROP_PREVIEW_SECONDS = 3;
export const TimelineCanvas = memo(function TimelineCanvas() {
const { state } = useTimelineContext();
const props = state.canvasProps;
const props = state.canvas;
const { draggedClip, resizingClip, scrollRef, selectedElementIds, displayTrackOrder } = props;
const snapGuide = resolveSnapGuide(draggedClip, resizingClip);
const draggedRowIndex =
@@ -6,6 +6,11 @@ import type { TimelineElement } from "../store/playerStore";
import { usePlayerStore } from "../store/playerStore";
import { type KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu";
import { TimelineOverlays, resolveTimelineContextElement } from "./TimelineOverlays";
import {
TimelineContextProvider,
type TimelineContextValue,
type TimelineOverlaysState,
} from "./TimelineProvider";
import { defaultTimelineTheme } from "./timelineTheme";
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -90,6 +95,42 @@ function renderKeyframeOverlay(options: {
percentage: 50,
animationId: "child-position",
};
const overlays = {
elements,
elementsRef: { current: elements },
theme: defaultTimelineTheme,
showShortcutHint: false,
showPopover: false,
rangeSelection: null,
setShowPopover: vi.fn(),
setRangeSelection: vi.fn(),
kfContextMenu: menu,
setKfContextMenu,
onDeleteKeyframe: vi.fn(),
onDeleteAllKeyframes,
onMoveKeyframeToPlayhead: vi.fn(),
clipContextMenu: null,
setClipContextMenu: vi.fn(),
currentTime: 0,
onSplitElement: vi.fn(),
pinZoomBeforeEdit: vi.fn(),
onDeleteElement: vi.fn(),
gapContextMenu: null,
onDismissGapContextMenu: vi.fn(),
onCloseTrackGap: vi.fn(),
onCloseAllTrackGaps: vi.fn(),
onHoverGapAction: vi.fn(),
} satisfies TimelineOverlaysState;
const contextValue = {
state: {
timelineReady: true,
elements,
canvas: {} as TimelineContextValue["state"]["canvas"],
overlays,
},
actions: {},
meta: {} as TimelineContextValue["meta"],
} satisfies TimelineContextValue;
act(() => {
usePlayerStore.setState({
@@ -97,32 +138,11 @@ function renderKeyframeOverlay(options: {
timelineSessionEpoch: 2,
});
root.render(
createElement(TimelineOverlays, {
elements,
elementsRef: { current: elements },
theme: defaultTimelineTheme,
showShortcutHint: false,
showPopover: false,
rangeSelection: null,
setShowPopover: vi.fn(),
setRangeSelection: vi.fn(),
kfContextMenu: menu,
setKfContextMenu,
onDeleteKeyframe: vi.fn(),
onDeleteAllKeyframes,
onMoveKeyframeToPlayhead: vi.fn(),
clipContextMenu: null,
setClipContextMenu: vi.fn(),
currentTime: 0,
onSplitElement: vi.fn(),
pinZoomBeforeEdit: vi.fn(),
onDeleteElement: vi.fn(),
gapContextMenu: null,
onDismissGapContextMenu: vi.fn(),
onCloseTrackGap: vi.fn(),
onCloseAllTrackGaps: vi.fn(),
onHoverGapAction: vi.fn(),
}),
createElement(
TimelineContextProvider,
{ value: contextValue },
createElement(TimelineOverlays),
),
);
});
return { setKfContextMenu, onDeleteAllKeyframes };
@@ -1,9 +1,6 @@
import { useEffect, type MutableRefObject } from "react";
import { useEffect } from "react";
import type { TimelineElement } from "../store/playerStore";
import { usePlayerStore } from "../store/playerStore";
import type { TimelineTheme } from "./timelineTheme";
import type { TimelineRangeSelection } from "./timelineEditing";
import type { TimelineEditCallbacks } from "./timelineCallbacks";
import { EditPopover } from "./EditModal";
import {
KeyframeDiamondContextMenu,
@@ -14,55 +11,8 @@ import { TrackGapContextMenu } from "./TrackGapContextMenu";
import { TimelineShortcutHint } from "./TimelineShortcutHint";
import { copyTextToClipboard } from "../../utils/clipboard";
import { trackStudioSegmentEaseEdit } from "../../telemetry/events";
import { useTimelineContextOptional } from "./TimelineProvider";
export interface ClipContextMenuState {
x: number;
y: number;
element: TimelineElement;
sessionEpoch: number;
}
/** Resolved model for the empty-lane-space (track gap) context menu. */
interface TrackGapContextMenuState {
x: number;
y: number;
gapWidth: number | null;
canCloseGap: boolean;
canCloseAllGaps: boolean;
hasAnyGaps: boolean;
}
export interface TimelineOverlaysProps {
elements: readonly TimelineElement[];
elementsRef: MutableRefObject<readonly TimelineElement[]>;
theme: TimelineTheme;
showShortcutHint: boolean;
showPopover: boolean;
rangeSelection: TimelineRangeSelection | null;
setShowPopover: (value: boolean) => void;
setRangeSelection: (value: TimelineRangeSelection | null) => void;
kfContextMenu: KeyframeDiamondContextMenuState | null;
setKfContextMenu: (value: KeyframeDiamondContextMenuState | null) => void;
onDeleteKeyframe: TimelineEditCallbacks["onDeleteKeyframe"];
onDeleteAllKeyframes: TimelineEditCallbacks["onDeleteAllKeyframes"];
onMoveKeyframeToPlayhead: TimelineEditCallbacks["onMoveKeyframeToPlayhead"];
clipContextMenu: ClipContextMenuState | null;
setClipContextMenu: (value: ClipContextMenuState | null) => void;
currentTime: number;
onSplitElement: TimelineEditCallbacks["onSplitElement"];
pinZoomBeforeEdit: () => void;
onDeleteElement?: (element: TimelineElement) => Promise<void> | void;
onCopyClip?: () => boolean;
onPasteClip?: () => Promise<void>;
onDuplicateClip?: () => Promise<boolean>;
canPasteClip?: () => boolean;
gapContextMenu: TrackGapContextMenuState | null;
onDismissGapContextMenu: () => void;
onCloseTrackGap: () => void;
onCloseAllTrackGaps: () => void;
onHoverGapAction: (action: "close-gap" | "close-all" | null) => void;
}
import { useTimelineContext } from "./TimelineProvider";
import type { ClipContextMenuState } from "./TimelineProvider";
interface TimelineContextTargetInput {
capturedElement: TimelineElement;
@@ -104,12 +54,9 @@ function readTimelineContextElement(
// The timeline's floating overlays, rendered as siblings above the scroll area:
// the shortcut hint, the range-edit popover, the keyframe-diamond context menu,
// and the clip context menu.
export function TimelineOverlays(props?: TimelineOverlaysProps) {
const context = useTimelineContextOptional();
const overlayProps = context?.state.overlaysProps ?? props;
if (overlayProps === undefined) {
throw new Error("TimelineOverlays requires TimelineProvider or props");
}
export function TimelineOverlays() {
const { state } = useTimelineContext();
const overlayProps = state.overlays;
const {
elements,
elementsRef,
@@ -1,5 +1,14 @@
import { createContext, useContext, type ComponentProps, type ReactNode } from "react";
import type { TimelineOverlaysProps } from "./TimelineOverlays";
import {
createContext,
useContext,
type ComponentProps,
type CSSProperties,
type MouseEvent,
type PointerEvent,
type ReactNode,
type RefCallback,
type UIEvent,
} from "react";
import type { TimelineEmptyState } from "./TimelineEmptyState";
import type { TimelineProps } from "./TimelineTypes";
import type { TimelineRangeSelection } from "./timelineEditing";
@@ -8,6 +17,10 @@ import type { Rect } from "../../utils/marqueeGeometry";
import type { ResizingClipState } from "./useTimelineClipDrag";
import type { TimelineLaneBaseProps } from "./timelineLaneProps";
import type { TimelineLaneGapStrips } from "./useTimelineGapHighlights";
import type { TimelineElement } from "../store/playerStore";
import type { TimelineTheme } from "./timelineTheme";
import type { TimelineEditCallbacks } from "./timelineCallbacks";
import type { KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu";
import { useTimelineProviderState } from "./useTimelineProviderState";
export {
@@ -28,7 +41,7 @@ export {
getTimelineVisibleTimeRange,
} from "./timelineViewportGeometry";
type TimelineCanvasProps = Omit<
export type TimelineCanvasState = Omit<
TimelineLaneBaseProps,
"setRangeSelection" | "setResizingClip" | "setDraggedClip"
> & {
@@ -49,22 +62,104 @@ type TimelineCanvasProps = Omit<
setDraggedClip: (value: TimelineLaneBaseProps["draggedClip"]) => void;
};
export interface ClipContextMenuState {
x: number;
y: number;
element: TimelineElement;
sessionEpoch: number;
}
export interface TrackGapContextMenuState {
x: number;
y: number;
gapWidth: number | null;
canCloseGap: boolean;
canCloseAllGaps: boolean;
hasAnyGaps: boolean;
}
export interface TimelineOverlaysState {
elements: readonly TimelineElement[];
elementsRef: { current: readonly TimelineElement[] };
theme: TimelineTheme;
showShortcutHint: boolean;
showPopover: boolean;
rangeSelection: TimelineRangeSelection | null;
setShowPopover: (value: boolean) => void;
setRangeSelection: (value: TimelineRangeSelection | null) => void;
kfContextMenu: KeyframeDiamondContextMenuState | null;
setKfContextMenu: (value: KeyframeDiamondContextMenuState | null) => void;
onDeleteKeyframe: TimelineEditCallbacks["onDeleteKeyframe"];
onDeleteAllKeyframes: TimelineEditCallbacks["onDeleteAllKeyframes"];
onMoveKeyframeToPlayhead: TimelineEditCallbacks["onMoveKeyframeToPlayhead"];
clipContextMenu: ClipContextMenuState | null;
setClipContextMenu: (value: ClipContextMenuState | null) => void;
currentTime: number;
onSplitElement: TimelineEditCallbacks["onSplitElement"];
pinZoomBeforeEdit: () => void;
onDeleteElement?: (element: TimelineElement) => Promise<void> | void;
onCopyClip?: () => boolean;
onPasteClip?: () => Promise<void>;
onDuplicateClip?: () => Promise<boolean>;
canPasteClip?: () => boolean;
gapContextMenu: TrackGapContextMenuState | null;
onDismissGapContextMenu: () => void;
onCloseTrackGap: () => void;
onCloseAllTrackGaps: () => void;
onHoverGapAction: (action: "close-gap" | "close-all" | null) => void;
}
export interface TimelineContainerProps {
ref: RefCallback<HTMLDivElement>;
"aria-label": string;
"data-timeline-element-count": number;
className: string;
onMouseMove: (event: MouseEvent<HTMLDivElement>) => void;
onMouseLeave: () => void;
style: CSSProperties;
}
export interface TimelineViewportProps {
ref: RefCallback<HTMLDivElement>;
"data-timeline-scroll-viewport": boolean;
"data-timeline-auto-scroll-left-inset": number;
tabIndex: number;
className: string;
onScroll: (event: UIEvent<HTMLDivElement>) => void;
onFocus: (event: React.FocusEvent<HTMLDivElement>) => void;
onBlur: (event: React.FocusEvent<HTMLDivElement>) => void;
onDragOver: (event: React.DragEvent<HTMLDivElement>) => void;
onDragLeave: (event: React.DragEvent<HTMLDivElement>) => void;
onDrop: (event: React.DragEvent<HTMLDivElement>) => void;
onPointerDown: (event: PointerEvent<HTMLDivElement>) => void;
onPointerMove: (event: PointerEvent<HTMLDivElement>) => void;
onPointerUp: (event: PointerEvent<HTMLDivElement>) => void;
onPointerCancel: (event: PointerEvent<HTMLDivElement>) => void;
onLostPointerCapture: (event: PointerEvent<HTMLDivElement>) => void;
}
export interface TimelineState {
timelineReady: boolean;
elements: readonly TimelineElement[];
canvas: TimelineCanvasState;
overlays: TimelineOverlaysState;
}
export interface TimelineActions {}
export interface TimelineMeta {
renderClipContent: TimelineCanvasState["renderClipContent"];
renderClipOverlay: TimelineCanvasState["renderClipOverlay"];
emptyState: ComponentProps<typeof TimelineEmptyState>;
containerProps: TimelineContainerProps;
viewportProps: TimelineViewportProps;
razorGuide: ReactNode;
}
export interface TimelineContextValue {
state: {
timelineReady: boolean;
elements: readonly unknown[];
canvasProps: TimelineCanvasProps;
overlaysProps: TimelineOverlaysProps;
};
actions: Record<string, never>;
meta: {
renderClipContent: TimelineCanvasProps["renderClipContent"];
renderClipOverlay: TimelineCanvasProps["renderClipOverlay"];
emptyState: ComponentProps<typeof TimelineEmptyState>;
containerProps: ComponentProps<"div">;
viewportProps: ComponentProps<"div">;
razorGuide: ReactNode;
};
state: TimelineState;
actions: TimelineActions;
meta: TimelineMeta;
}
const TimelineContext = createContext<TimelineContextValue | null>(null);
@@ -74,6 +169,16 @@ export function TimelineProvider({ children, ...props }: TimelineProps & { child
return <TimelineContext.Provider value={value}>{children}</TimelineContext.Provider>;
}
export function TimelineContextProvider({
value,
children,
}: {
value: TimelineContextValue;
children: ReactNode;
}) {
return <TimelineContext.Provider value={value}>{children}</TimelineContext.Provider>;
}
export function useTimelineContext(): TimelineContextValue {
const value = useTimelineContextOptional();
if (value === null) throw new Error("useTimelineContext must be used inside TimelineProvider");
@@ -1,6 +1,6 @@
import { useCallback } from "react";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import type { ClipContextMenuState } from "./TimelineOverlays";
import type { ClipContextMenuState } from "./TimelineProvider";
/** A clicked member of an active multi-selection keeps the whole group
* selected, so the context menu it opens acts on all of it. */
@@ -9,7 +9,7 @@ import { useTimelineZoom } from "./useTimelineZoom";
import { useTimelineAssetDrop } from "./timelineDragDrop";
import { type KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu";
import { useTimelineClipDrag } from "./useTimelineClipDrag";
import { type ClipContextMenuState } from "./TimelineOverlays";
import type { ClipContextMenuState, TimelineContextValue } from "./TimelineProvider";
import { useTimelineEditPinning } from "./useTimelineEditPinning";
import { useTimelineStackingSync } from "./useTimelineStackingSync";
import { useTimelineGeometry } from "./useTimelineGeometry";
@@ -42,7 +42,6 @@ import { useTimelineActiveClips } from "./useTimelineActiveClips";
import { useTimelineLaneMoveRefresh } from "./useTimelineLaneMoveRefresh";
import { useTimelineLogicalFocus } from "./useTimelineLogicalFocus";
import { useClipContextMenu } from "./useTimelineClipContextMenu";
import type { TimelineContextValue } from "./TimelineProvider";
export function useTimelineProviderState({
onSeek,
@@ -519,7 +518,12 @@ export function useTimelineProviderState({
onHoverGapAction: setHoveredGapAction,
};
const contextValue: TimelineContextValue = {
state: { timelineReady, elements: timelineElements, canvasProps, overlaysProps },
state: {
timelineReady,
elements: timelineElements,
canvas: canvasProps,
overlays: overlaysProps,
},
actions: {},
meta: {
renderClipContent,