Skip to content

🎥 Make videos programmatically with React

59.5 KB
Raw Blame
import type {
	CanvasSelectionInteraction,
	CanvasSelectionItem,
	CanvasSelectionSnapshot,
} from '@remotion/sdk';
import {
	CanvasInternals,
	getCanvasSelectionItemKey,
	useCanvasSelection,
} from '@remotion/sdk';
import {
	stringifySequenceExpandedRowKey,
	type SequenceNodePathMutation,
	type SequenceNodePathRemapping,
} from '@remotion/studio-shared';
import React, {
	createContext,
	useCallback,
	useContext,
	useEffect,
	useLayoutEffect,
	useMemo,
	useRef,
	useState,
	type CSSProperties,
} from 'react';
import {
	Internals,
	type GetDragOverrides,
	type GetEffectDragOverrides,
	type PropStatuses,
} from 'remotion';
import {
	canUseEffectOperations,
	canUseKeyframeOperations,
} from '../../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {
	BACKGROUND,
	BLACK,
	TIMELINE_BACKGROUND_COLOR,
	TIMELINE_SELECTED_BACKGROUND_COLOR,
	TIMELINE_SELECTED_LABEL_BACKGROUND_COLOR,
	WHITE_ALPHA_05,
	WHITE_ALPHA_10,
	WHITE_ALPHA_80,
} from '../../helpers/colors';
import type {
	SequenceNodePathInfo,
	TimelineTrackData,
} from '../../helpers/get-timeline-sequence-sort-key';
import {
	isStudioInteractivityEnabled,
	isStudioSelectionEnabled,
} from '../../helpers/interactivity-enabled';
import {
	isPointerSessionRelease,
	startCapturedPointerSession,
} from '../../helpers/pointer-session';
import {
	buildTimelineTree,
	flattenVisibleTreeNodes,
	TIMELINE_PADDING,
} from '../../helpers/timeline-layout';
import {timelineNodePathInfoToKey} from '../../helpers/timeline-node-path-key';
import {useBreakpoint} from '../../helpers/use-breakpoint';
import {useKeybinding} from '../../helpers/use-keybinding';
import {SIDEBAR_RESPONSIVE_BREAKPOINTS} from '../../helpers/use-responsive-sidebar-status';
import {useSyncExternalStore} from '../../helpers/use-sync-external-store';
import {SidebarContext} from '../../state/sidebar';
import {useZIndex} from '../../state/z-index';
import {
	ExpandedTracksGetterContext,
	ExpandedTracksSetterContext,
	type GetIsExpanded,
} from '../ExpandedTracksProvider';
import {selectOptionsSidebarInspectorPanel} from '../options-sidebar-tabs';
import {
	getNodeCanEditEasing,
	getNodeHasKeyframes,
	getNodeKeyframes,
} from './get-node-keyframes';
import {getCurrentFrame} from './imperative-state';
import {parseKeyframeFieldFromNodePath} from './parse-keyframe-field-from-node-path';
import {
	filterTimelineExpandedTree,
	getSelectedTimelineExpandedRowKeys,
	isTimelineExpandedNodeSelected,
} from './timeline-expanded-filter';
import {scrollableRef, timelineVerticalScroll} from './timeline-refs';
import {
	EDGE_SCROLL_VERTICAL_INCREMENT,
	SCROLL_INCREMENT,
	startTimelineEdgeAutoScroll,
} from './timeline-scroll-logic';
import {TimelineClipboardKeybindings} from './TimelineClipboardKeybindings';
import {TimelineDeleteKeybindings} from './TimelineDeleteKeybindings';
import {TimelineSceneRangeContext} from './TimelineSceneRangeContext';
import {TimelineWidthContext} from './TimelineWidthProvider';

const {
	EMPTY_CANVAS_SELECTION,
	getCanvasSelectionAfterInteraction,
	getCanvasSequenceSelectionKey,
	getKeyframeSegments,
	useCanvasSelectionController,
} = CanvasInternals;

const emptyDragOverrideSnapshot = {};
const getDragOverrideSnapshot = (overrides: Record<string, unknown>): object =>
	Object.keys(overrides).length === 0 ? emptyDragOverrideSnapshot : overrides;

type SelectableTrackCache = {
	readonly track: TimelineTrackData;
	readonly expanded: boolean;
	readonly dragOverride: object | null;
	readonly effectOverrides: readonly object[];
	readonly items: TimelineSelection[];
};

export const TIMELINE_SELECTED_BACKGROUND = TIMELINE_SELECTED_BACKGROUND_COLOR;
export const TIMELINE_EXPANDED_SELECTED_BACKGROUND = BACKGROUND;
export const TIMELINE_HOVER_BACKGROUND = WHITE_ALPHA_05;
export const TIMELINE_SELECTED_LABEL_BACKGROUND =
	TIMELINE_SELECTED_LABEL_BACKGROUND_COLOR;
export const TIMELINE_SELECTED_LABEL_TEXT = BLACK;
export const TIMELINE_SELECTED_LABEL_HORIZONTAL_PADDING = 2;

export const getTimelineSelectedLabelStyle = (
	selected: boolean,
	subcategory: boolean,
): CSSProperties => {
	return {
		paddingLeft: TIMELINE_SELECTED_LABEL_HORIZONTAL_PADDING,
		paddingRight: TIMELINE_SELECTED_LABEL_HORIZONTAL_PADDING,
		...(selected
			? {
					backgroundColor: subcategory
						? WHITE_ALPHA_10
						: TIMELINE_SELECTED_LABEL_BACKGROUND,
				}
			: {}),
	};
};

export const getTimelineColor = (selected: boolean, subcategory: boolean) => {
	return selected && !subcategory
		? TIMELINE_SELECTED_LABEL_TEXT
		: WHITE_ALPHA_80;
};

export const getTimelineSelectedTrackHighlightStyle = (
	timelineWidth: number,
	backgroundColor: string = TIMELINE_SELECTED_BACKGROUND,
): CSSProperties => ({
	backgroundColor,
	bottom: 0,
	left: -TIMELINE_PADDING,
	pointerEvents: 'none',
	position: 'absolute',
	top: 0,
	width: timelineWidth,
	minWidth: `calc(100% + ${TIMELINE_PADDING * 2}px)`,
});

export const getTimelineRowHighlightBackground = ({
	showSelectedBackground,
	selected,
	containsSelection,
	hovered,
	selectedBackground,
}: {
	readonly showSelectedBackground: boolean;
	readonly selected: boolean;
	readonly containsSelection: boolean;
	readonly hovered: boolean;
	readonly selectedBackground: string;
}): string | undefined => {
	if (showSelectedBackground && (selected || containsSelection)) {
		return selectedBackground;
	}

	return hovered ? TIMELINE_HOVER_BACKGROUND : undefined;
};

export const TIMELINE_BACKGROUND = TIMELINE_BACKGROUND_COLOR;
export const TIMELINE_TICKS_BACKGROUND = BACKGROUND;

export type TimelineSelection = CanvasSelectionItem;

export type TimelineEasingSelection = Extract<
	TimelineSelection,
	{type: 'easing'}
>;

export type TimelineSelectionInteraction = CanvasSelectionInteraction;

export const isTimelineSelectionModifierEvent = ({
	shiftKey,
	metaKey,
	ctrlKey,
}: {
	readonly shiftKey: boolean;
	readonly metaKey: boolean;
	readonly ctrlKey: boolean;
}) => {
	return shiftKey || metaKey || ctrlKey;
};

export const shouldSelectTimelineRowOnPointerDown = ({
	selected,
	shiftKey,
	metaKey,
	ctrlKey,
}: {
	readonly selected: boolean;
	readonly shiftKey: boolean;
	readonly metaKey: boolean;
	readonly ctrlKey: boolean;
}) => {
	return (
		!selected || isTimelineSelectionModifierEvent({shiftKey, metaKey, ctrlKey})
	);
};

export type TimelineSelectionState = CanvasSelectionSnapshot;

export const EMPTY_TIMELINE_SELECTION_STATE: TimelineSelectionState =
	EMPTY_CANVAS_SELECTION;

export type TimelineMarqueeRect = {
	readonly left: number;
	readonly top: number;
	readonly right: number;
	readonly bottom: number;
};

export type TimelineMarqueeSelectionKind = 'sequence' | 'keyframes-and-easings';

export type TimelineMarqueeSelectionCandidate = {
	readonly item: TimelineSelection;
	readonly rect: TimelineMarqueeRect;
};

export const getTimelineSelectionAfterInteraction =
	getCanvasSelectionAfterInteraction;

export const getAvailableTimelineSelectionState = ({
	availableKeys,
	availableItemsByKey,
	state,
}: {
	readonly availableKeys: ReadonlySet<string>;
	readonly availableItemsByKey?: ReadonlyMap<string, TimelineSelection>;
	readonly state: TimelineSelectionState;
}): TimelineSelectionState => {
	if (state.selectedItems.length === 0 && state.anchor === null) {
		return state;
	}

	const getCurrentSelectionItem = (item: TimelineSelection) => {
		const key = getTimelineSelectionKey(item);
		if (!availableKeys.has(key)) {
			return null;
		}

		return availableItemsByKey?.get(key) ?? item;
	};

	const selectedItems = state.selectedItems
		.map(getCurrentSelectionItem)
		.filter((item): item is TimelineSelection => item !== null);
	const anchor = state.anchor ? getCurrentSelectionItem(state.anchor) : null;

	if (
		selectedItems.length === state.selectedItems.length &&
		anchor === state.anchor
	) {
		return state;
	}

	return {
		selectedItems,
		anchor,
	};
};

export const getNormalizedTimelineMarqueeRect = ({
	startX,
	startY,
	currentX,
	currentY,
}: {
	readonly startX: number;
	readonly startY: number;
	readonly currentX: number;
	readonly currentY: number;
}): TimelineMarqueeRect => ({
	left: Math.min(startX, currentX),
	top: Math.min(startY, currentY),
	right: Math.max(startX, currentX),
	bottom: Math.max(startY, currentY),
});

export const getClampedTimelineMarqueePoint = ({
	x,
	y,
	bounds,
}: {
	readonly x: number;
	readonly y: number;
	readonly bounds: TimelineMarqueeRect;
}): {
	readonly x: number;
	readonly y: number;
} => ({
	x: Math.min(bounds.right, Math.max(bounds.left, x)),
	y: Math.min(bounds.bottom, Math.max(bounds.top, y)),
});

export const timelineMarqueeRectsIntersect = (
	a: TimelineMarqueeRect,
	b: TimelineMarqueeRect,
) =>
	a.left <= b.right &&
	a.right >= b.left &&
	a.top <= b.bottom &&
	a.bottom >= b.top;

const getTimelineMarqueeSelectionKind = (
	item: TimelineSelection,
): TimelineMarqueeSelectionKind | null => {
	if (item.type === 'sequence') {
		return 'sequence';
	}

	if (item.type === 'keyframe' || item.type === 'easing') {
		return 'keyframes-and-easings';
	}

	return null;
};

const isTimelineSelectionCompatibleWithMarqueeKind = (
	item: TimelineSelection,
	kind: TimelineMarqueeSelectionKind,
) => {
	if (kind === 'sequence') {
		return item.type === 'sequence';
	}

	return item.type === 'keyframe' || item.type === 'easing';
};

export const getTimelineMarqueeSelection = ({
	candidates,
	lockedSelectionKind,
	marqueeRect,
}: {
	readonly candidates: readonly TimelineMarqueeSelectionCandidate[];
	readonly lockedSelectionKind: TimelineMarqueeSelectionKind | null;
	readonly marqueeRect: TimelineMarqueeRect;
}): {
	readonly lockedSelectionKind: TimelineMarqueeSelectionKind | null;
	readonly selectedItems: readonly TimelineSelection[];
} => {
	const geometricallyIntersectingCandidates = candidates.filter((candidate) => {
		return (
			getTimelineMarqueeSelectionKind(candidate.item) !== null &&
			timelineMarqueeRectsIntersect(candidate.rect, marqueeRect)
		);
	});
	const intersectingKeyframes = new Set(
		geometricallyIntersectingCandidates.flatMap((candidate) => {
			if (candidate.item.type !== 'keyframe') {
				return [];
			}

			return [
				`${timelineNodePathInfoToKey(candidate.item.nodePathInfo)}.${candidate.item.frame}`,
			];
		}),
	);
	const intersectingCandidates = geometricallyIntersectingCandidates.filter(
		(candidate) => {
			if (candidate.item.type !== 'easing') {
				return true;
			}

			const fieldKey = timelineNodePathInfoToKey(candidate.item.nodePathInfo);
			return (
				intersectingKeyframes.has(`${fieldKey}.${candidate.item.fromFrame}`) &&
				intersectingKeyframes.has(`${fieldKey}.${candidate.item.toFrame}`)
			);
		},
	);
	const getFirstIntersectingSelectionKind = () =>
		intersectingCandidates.length === 0
			? null
			: getTimelineMarqueeSelectionKind(intersectingCandidates[0].item);
	let nextLockedSelectionKind =
		lockedSelectionKind ?? getFirstIntersectingSelectionKind();
	const getSelectedItemsForKind = (kind: TimelineMarqueeSelectionKind) =>
		intersectingCandidates
			.filter((candidate) =>
				isTimelineSelectionCompatibleWithMarqueeKind(candidate.item, kind),
			)
			.map((candidate) => candidate.item);

	if (nextLockedSelectionKind === null) {
		return {lockedSelectionKind: null, selectedItems: []};
	}

	let selectedItems = getSelectedItemsForKind(nextLockedSelectionKind);
	if (lockedSelectionKind !== null && selectedItems.length === 0) {
		nextLockedSelectionKind = getFirstIntersectingSelectionKind();
		selectedItems =
			nextLockedSelectionKind === null
				? []
				: getSelectedItemsForKind(nextLockedSelectionKind);
	}

	return {
		lockedSelectionKind: nextLockedSelectionKind,
		selectedItems,
	};
};

export const extendTimelineMarqueeSelection = ({
	currentSelection,
	marqueeSelection,
}: {
	readonly currentSelection: readonly TimelineSelection[];
	readonly marqueeSelection: readonly TimelineSelection[];
}): readonly TimelineSelection[] => {
	if (marqueeSelection.length === 0) {
		return currentSelection;
	}

	const marqueeKind = getTimelineMarqueeSelectionKind(marqueeSelection[0]);
	if (marqueeKind === null) {
		return marqueeSelection;
	}

	const selectedKeys = new Set(
		currentSelection.map((item) => getTimelineSelectionKey(item)),
	);
	return [
		...currentSelection.filter((item) =>
			isTimelineSelectionCompatibleWithMarqueeKind(item, marqueeKind),
		),
		...marqueeSelection.filter((item) => {
			const key = getTimelineSelectionKey(item);
			if (selectedKeys.has(key)) {
				return false;
			}

			selectedKeys.add(key);
			return true;
		}),
	];
};

type TimelineSelectionContextValue = {
	readonly canSelect: boolean;
	readonly inspectorRevealRequest: TimelineSelectionRevealRequest | null;
	readonly revealRequest: TimelineSelectionRevealRequest | null;
	readonly consumeRevealRequest: (token: number) => void;
	readonly consumeInspectorRevealRequest: (token: number) => void;
	readonly selectedItems: readonly TimelineSelection[];
	readonly isSelected: (item: TimelineSelection) => boolean;
	readonly selectItem: (
		item: TimelineSelection,
		interaction?: TimelineSelectionInteraction,
		allSelectableItems?: readonly TimelineSelection[],
		options?: TimelineSelectionOptions,
	) => void;
	readonly selectItems: (
		items: readonly TimelineSelection[],
		options?: TimelineSelectionOptions,
	) => void;
	readonly registerMarqueeSelectableItem: (
		item: TimelineSelection,
		getRect: () => DOMRect | null,
	) => () => void;
	readonly getMarqueeSelectionCandidates: () => readonly TimelineMarqueeSelectionCandidate[];
	readonly containsSelection: (nodePathInfo: SequenceNodePathInfo) => boolean;
	readonly remapSelectionNodePaths: (
		mutations: readonly SequenceNodePathMutation[],
	) => void;
	readonly clearSelection: () => void;
};

const defaultTimelineSelectionContextValue: TimelineSelectionContextValue = {
	canSelect: false,
	inspectorRevealRequest: null,
	revealRequest: null,
	consumeRevealRequest: () => undefined,
	consumeInspectorRevealRequest: () => undefined,
	selectedItems: [],
	isSelected: () => false,
	selectItem: () => undefined,
	selectItems: () => undefined,
	registerMarqueeSelectableItem: () => () => undefined,
	getMarqueeSelectionCandidates: () => [],
	containsSelection: () => false,
	remapSelectionNodePaths: () => undefined,
	clearSelection: () => undefined,
};

const TimelineSelectionCanSelectContext = createContext(false);

const TimelineSelectionContext = createContext<TimelineSelectionContextValue>(
	defaultTimelineSelectionContextValue,
);

type TimelineRowSelectionContextValue = Pick<
	TimelineSelectionContextValue,
	| 'canSelect'
	| 'clearSelection'
	| 'registerMarqueeSelectableItem'
	| 'selectItem'
> & {
	readonly getSnapshot: () => TimelineSelectionState;
	readonly subscribe: (listener: () => void) => () => void;
};

const TimelineRowSelectionContext =
	createContext<TimelineRowSelectionContextValue | null>(null);

const EMPTY_SELECTABLE_TIMELINE_ITEMS: readonly TimelineSelection[] = [];

const SelectableTimelineItemsContext = createContext<
	React.RefObject<readonly TimelineSelection[]>
>({current: EMPTY_SELECTABLE_TIMELINE_ITEMS});

export const TimelineSelectionOrderProvider: React.FC<{
	readonly children: React.ReactNode;
	readonly items: readonly TimelineSelection[];
}> = ({children, items}) => {
	const itemsRef = useRef(items);
	itemsRef.current = items;

	return (
		<SelectableTimelineItemsContext.Provider value={itemsRef}>
			{children}
		</SelectableTimelineItemsContext.Provider>
	);
};

const CurrentTimelineSelectionContext =
	createContext<React.RefObject<TimelineSelectionContextValue> | null>(null);

type TimelineSelectionOptions = {
	readonly reveal?: boolean;
	readonly revealInInspector?: boolean;
};

type TimelineSelectionRevealRequest = {
	readonly item: TimelineSelection;
	readonly token: number;
};

const parseEffectIndex = (effectIndex: string): number | null => {
	const parsed = Number(effectIndex);
	if (!Number.isInteger(parsed) || parsed < 0) {
		return null;
	}

	return parsed;
};

export const getTimelineSelectionFromNodePathInfo = (
	nodePathInfo: SequenceNodePathInfo | null,
): TimelineSelection | null => {
	if (nodePathInfo === null) {
		return null;
	}

	const {auxiliaryKeys} = nodePathInfo;
	if (auxiliaryKeys.length === 0) {
		return {type: 'sequence', nodePathInfo};
	}

	if (auxiliaryKeys.length === 2 && auxiliaryKeys[0] === 'controls') {
		return {type: 'sequence-prop', nodePathInfo, key: auxiliaryKeys[1]};
	}

	if (auxiliaryKeys.length === 1 && auxiliaryKeys[0] === 'effects') {
		return {type: 'sequence-all-effects', nodePathInfo};
	}

	if (auxiliaryKeys[0] === 'effects') {
		const effectIndex = parseEffectIndex(auxiliaryKeys[1]);
		if (effectIndex === null) {
			return null;
		}

		if (auxiliaryKeys.length === 2) {
			return {type: 'sequence-effect', nodePathInfo, i: effectIndex};
		}

		if (auxiliaryKeys.length === 3) {
			return {
				type: 'sequence-effect-prop',
				nodePathInfo,
				i: effectIndex,
				key: auxiliaryKeys[2],
			};
		}
	}

	return null;
};

export const getTimelineSelectionKey = getCanvasSelectionItemKey;

const multiSelectedSequenceNodePathInfosByItems = new WeakMap<
	readonly TimelineSelection[],
	readonly SequenceNodePathInfo[] | null
>();

export const getMultiSelectedSequenceNodePathInfos = (
	selectedItems: readonly TimelineSelection[],
): readonly SequenceNodePathInfo[] | null => {
	const cached = multiSelectedSequenceNodePathInfosByItems.get(selectedItems);
	if (cached !== undefined) {
		return cached;
	}

	// All selected clips and labels can share the same action targets.
	const nodePathInfos =
		selectedItems.length < 2 ||
		selectedItems.some((item) => item.type !== 'sequence')
			? null
			: selectedItems.flatMap((item) =>
					item.type === 'sequence' ? [item.nodePathInfo] : [],
				);
	multiSelectedSequenceNodePathInfosByItems.set(selectedItems, nodePathInfos);
	return nodePathInfos;
};

const selectedKeysByItems = new WeakMap<
	readonly TimelineSelection[],
	ReadonlySet<string>
>();

const getTimelineSelectedKeys = (
	selectedItems: readonly TimelineSelection[],
): ReadonlySet<string> => {
	const cached = selectedKeysByItems.get(selectedItems);
	if (cached !== undefined) {
		return cached;
	}

	// Share membership checks across rows without retaining old selections.
	const keys = new Set(selectedItems.map(getTimelineSelectionKey));
	selectedKeysByItems.set(selectedItems, keys);
	return keys;
};

const containedRowsByItems = new WeakMap<
	readonly TimelineSelection[],
	{
		readonly ancestorRows: ReadonlyMap<
			string,
			ReadonlyMap<number, ReadonlySet<string>>
		>;
		readonly animationRows: ReadonlySet<string>;
	}
>();

const timelineRowContainsSelection = (
	nodePathInfo: SequenceNodePathInfo,
	selectedItems: readonly TimelineSelection[],
): boolean => {
	let index = containedRowsByItems.get(selectedItems);
	if (index === undefined) {
		const ancestorRows = new Map<string, Map<number, Set<string>>>();
		const animationRows = new Set<string>();
		for (const item of selectedItems) {
			if (item.type === 'guide') {
				continue;
			}

			if (item.type === 'keyframe' || item.type === 'easing') {
				animationRows.add(timelineNodePathInfoToKey(item.nodePathInfo));
			}

			const {
				auxiliaryKeys,
				sequenceSubscriptionKey,
				index: instanceIndex,
			} = item.nodePathInfo;
			// NaN never matched the strict instance-index comparison.
			if (auxiliaryKeys.length === 0 || Number.isNaN(instanceIndex)) {
				continue;
			}

			const sequenceKey = stringifySequenceExpandedRowKey(
				sequenceSubscriptionKey,
			);
			let instances = ancestorRows.get(sequenceKey);
			if (instances === undefined) {
				instances = new Map();
				ancestorRows.set(sequenceKey, instances);
			}

			let prefixes = instances.get(instanceIndex);
			if (prefixes === undefined) {
				prefixes = new Set();
				instances.set(instanceIndex, prefixes);
			}

			// Only strict ancestors contain a selected row. Preserve auxiliary
			// segments separately: a property name can itself contain a dot.
			for (let length = 0; length < auxiliaryKeys.length; length++) {
				prefixes.add(JSON.stringify(auxiliaryKeys.slice(0, length)));
			}
		}

		index = {ancestorRows, animationRows};
		containedRowsByItems.set(selectedItems, index);
	}

	if (
		index.animationRows.size > 0 &&
		index.animationRows.has(timelineNodePathInfoToKey(nodePathInfo))
	) {
		return true;
	}

	if (index.ancestorRows.size === 0) {
		return false;
	}

	return (
		index.ancestorRows
			.get(
				stringifySequenceExpandedRowKey(nodePathInfo.sequenceSubscriptionKey),
			)
			?.get(nodePathInfo.index)
			?.has(JSON.stringify(nodePathInfo.auxiliaryKeys)) ?? false
	);
};

export const getSelectableTimelineSequenceSelections = (
	tracks: readonly Pick<TimelineTrackData, 'nodePathInfo'>[],
): TimelineSelection[] => {
	return tracks.flatMap((track): TimelineSelection[] => {
		if (
			track.nodePathInfo === null ||
			track.nodePathInfo.auxiliaryKeys.length > 0
		) {
			return [];
		}

		return [{type: 'sequence', nodePathInfo: track.nodePathInfo}];
	});
};

const getSelectableTimelineItemsForTrack = ({
	getDragOverrides,
	getEffectDragOverrides,
	getIsExpanded,
	propStatuses,
	selectedRowKeys,
	track,
	timelinePosition,
}: {
	readonly getDragOverrides: GetDragOverrides;
	readonly getEffectDragOverrides: GetEffectDragOverrides;
	readonly getIsExpanded: GetIsExpanded;
	readonly propStatuses: PropStatuses;
	readonly selectedRowKeys: ReadonlySet<string>;
	readonly track: TimelineTrackData;
	readonly timelinePosition: number;
}): TimelineSelection[] => {
	const {nodePathInfo} = track;
	if (nodePathInfo === null) {
		return [];
	}

	const sequenceSelection = getTimelineSelectionFromNodePathInfo(nodePathInfo);
	if (sequenceSelection === null) {
		return [];
	}

	if (track.sequence.timelineTrack || !getIsExpanded(nodePathInfo)) {
		return [sequenceSelection];
	}

	const tree = buildTimelineTree({
		sequence: track.sequence,
		nodePathInfo,
		getDragOverrides,
		getEffectDragOverrides,
		propStatuses,
		includeTextContent: false,
		includeSourceControls: false,
		runtimeValues: null,
	});
	const filteredTree = filterTimelineExpandedTree({
		nodes: tree,
		shouldShowNode: (node) =>
			isTimelineExpandedNodeSelected({
				nodePathInfo: node.nodePathInfo,
				selectedRowKeys,
			}) ||
			getNodeHasKeyframes({
				node,
				nodePath: nodePathInfo.sequenceSubscriptionKey,
				propStatuses,
				getDragOverrides,
				getEffectDragOverrides,
			}),
	});
	const visibleTreeRows = flattenVisibleTreeNodes({
		nodes: filteredTree,
		getIsExpanded,
	});

	return [
		sequenceSelection,
		...visibleTreeRows.flatMap(({node}): TimelineSelection[] => {
			const rowSelection = getTimelineSelectionFromNodePathInfo(
				node.nodePathInfo,
			);
			if (rowSelection === null) {
				return [];
			}

			const keyframes = getNodeKeyframes({
				node,
				nodePath: nodePathInfo.sequenceSubscriptionKey,
				propStatuses,
				keyframeDisplayOffset: track.keyframeDisplayOffset,
				keyframePlaybackRate: track.keyframePlaybackRate,
				getDragOverrides,
				getEffectDragOverrides,
				timelinePosition,
			});
			const keyframeSelections = keyframes.map(
				(keyframe): TimelineSelection => ({
					type: 'keyframe',
					nodePathInfo: node.nodePathInfo,
					frame: keyframe.frame,
				}),
			);
			const easingSelections = getNodeCanEditEasing({
				node,
				nodePath: nodePathInfo.sequenceSubscriptionKey,
				propStatuses,
			})
				? getKeyframeSegments(keyframes).map(
						(segment): TimelineSelection => ({
							type: 'easing',
							nodePathInfo: node.nodePathInfo,
							fromFrame: segment.fromFrame,
							toFrame: segment.toFrame,
							segmentIndex: segment.segmentIndex,
						}),
					)
				: [];

			return [rowSelection, ...easingSelections, ...keyframeSelections];
		}),
	];
};

export const getSelectableTimelineItems = ({
	getDragOverrides,
	getEffectDragOverrides,
	getIsExpanded,
	propStatuses,
	selectedItems,
	timeline,
	timelinePosition,
}: {
	readonly getDragOverrides: GetDragOverrides;
	readonly getEffectDragOverrides: GetEffectDragOverrides;
	readonly getIsExpanded: GetIsExpanded;
	readonly propStatuses: PropStatuses;
	readonly selectedItems: readonly TimelineSelection[];
	readonly timeline: readonly TimelineTrackData[];
	readonly timelinePosition: number;
}): TimelineSelection[] => {
	const selectedRowKeys = getSelectedTimelineExpandedRowKeys(selectedItems);
	return timeline.flatMap((track) =>
		getSelectableTimelineItemsForTrack({
			getDragOverrides,
			getEffectDragOverrides,
			getIsExpanded,
			propStatuses,
			selectedRowKeys,
			track,
			timelinePosition,
		}),
	);
};

export const getTimelineSequenceSelectionKey = getCanvasSequenceSelectionKey;

export const TimelineSelectAllKeybindings: React.FC<{
	readonly timeline: readonly TimelineTrackData[];
}> = ({timeline}) => {
	const keybindings = useKeybinding();
	const canSelect = useTimelineSelectionCanSelect();
	const currentSelection = useCurrentTimelineSelectionStateAsRef();

	const selectableSequenceSelections = useMemo(
		() => getSelectableTimelineSequenceSelections(timeline),
		[timeline],
	);
	const selectableSequenceSelectionsRef = useRef(selectableSequenceSelections);
	selectableSequenceSelectionsRef.current = selectableSequenceSelections;

	useEffect(() => {
		if (!canSelect) {
			return;
		}

		const selectAll = keybindings.registerKeybinding({
			event: 'keydown',
			action: 'selectAllSequenceRows',
			callback: () => {
				const latestSelectableSequenceSelections =
					selectableSequenceSelectionsRef.current;
				if (latestSelectableSequenceSelections.length === 0) {
					return;
				}

				currentSelection.current.selectItems(
					latestSelectableSequenceSelections,
				);
			},
			preventDefault: true,
			triggerIfInputFieldFocused: false,
			keepRegisteredWhenNotHighestContext: false,
		});

		return () => {
			selectAll.unregister();
		};
	}, [canSelect, currentSelection, keybindings]);

	return null;
};

const fieldsSelectingSequenceOnEscape = new Set([
	'cropLeft',
	'cropRight',
	'cropTop',
	'cropBottom',
	'style.rotate',
]);

export const getTimelineSequenceSelectionForEscape = (
	selectedItems: readonly TimelineSelection[],
): Extract<TimelineSelection, {type: 'sequence'}> | null => {
	if (selectedItems.length !== 1) {
		return null;
	}

	const [selectedItem] = selectedItems;
	let fieldKey: string | null = null;
	if (selectedItem.type === 'sequence-prop') {
		fieldKey = selectedItem.key;
	} else if (
		selectedItem.type === 'keyframe' ||
		selectedItem.type === 'easing'
	) {
		const field = parseKeyframeFieldFromNodePath(
			selectedItem.nodePathInfo.auxiliaryKeys,
		);
		fieldKey = field?.type === 'sequence' ? field.fieldKey : null;
	} else {
		return null;
	}

	if (!fieldsSelectingSequenceOnEscape.has(fieldKey ?? '')) {
		return null;
	}

	return {
		type: 'sequence',
		nodePathInfo: {
			...selectedItem.nodePathInfo,
			auxiliaryKeys: [],
		},
	};
};

const TimelineEscapeKeybindings: React.FC = () => {
	const keybindings = useKeybinding();
	const currentSelection = useCurrentTimelineSelectionStateAsRef();

	useEffect(() => {
		const escape = keybindings.registerKeybinding({
			event: 'keydown',
			key: 'Escape',
			callback: (event) => {
				const {selectedItems, clearSelection, selectItems} =
					currentSelection.current;
				if (selectedItems.length === 0) {
					return;
				}

				const sequenceSelection =
					getTimelineSequenceSelectionForEscape(selectedItems);
				if (sequenceSelection) {
					selectItems([sequenceSelection]);
					event.preventDefault();
					return;
				}

				clearSelection();
				event.preventDefault();
			},
			commandCtrlKey: false,
			preventDefault: false,
			triggerIfInputFieldFocused: false,
			keepRegisteredWhenNotHighestContext: false,
		});

		return () => {
			escape.unregister();
		};
	}, [currentSelection, keybindings]);

	return null;
};

export const TimelineSelectableItemsProvider: React.FC<{
	readonly children: React.ReactNode;
	readonly timeline: readonly TimelineTrackData[];
}> = ({children, timeline}) => {
	const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
	const {propStatuses} = useContext(Internals.VisualModePropStatusesContext);
	const {getDragOverrides, getEffectDragOverrides} = useContext(
		Internals.VisualModeDragOverridesContext,
	);
	const {selectedItems} = useTimelineSelection();
	const previousSelectionRef = useRef<{
		readonly timeline: readonly TimelineTrackData[];
		readonly propStatuses: PropStatuses;
		readonly selectedItems: readonly TimelineSelection[];
		readonly getIsExpanded: GetIsExpanded;
		readonly timelinePosition: number;
		readonly items: TimelineSelection[];
		readonly byId: Map<string, SelectableTrackCache>;
		readonly expandedEntries: readonly SelectableTrackCache[];
	} | null>(null);
	const selection = useMemo(() => {
		const previous = previousSelectionRef.current;
		const timelinePosition = getCurrentFrame();
		if (
			previous !== null &&
			previous.timeline === timeline &&
			previous.propStatuses === propStatuses &&
			previous.selectedItems === selectedItems &&
			previous.getIsExpanded === getIsExpanded &&
			previous.timelinePosition === timelinePosition &&
			previous.expandedEntries.every((entry) => {
				const nodePath = entry.track.nodePathInfo?.sequenceSubscriptionKey;
				if (nodePath === undefined) {
					return false;
				}

				return (
					entry.dragOverride ===
						getDragOverrideSnapshot(getDragOverrides(nodePath)) &&
					entry.effectOverrides.every(
						(value, index) =>
							value ===
							getDragOverrideSnapshot(getEffectDragOverrides(nodePath, index)),
					)
				);
			})
		) {
			return previous;
		}

		const invalidateAll =
			previous === null ||
			previous.propStatuses !== propStatuses ||
			previous.selectedItems !== selectedItems ||
			previous.getIsExpanded !== getIsExpanded ||
			previous.timelinePosition !== timelinePosition;
		const selectedRowKeys = getSelectedTimelineExpandedRowKeys(selectedItems);
		const byId = new Map<string, SelectableTrackCache>();
		const expandedEntries: SelectableTrackCache[] = [];
		let changed = previous === null || previous.timeline !== timeline;
		const items: TimelineSelection[] = [];

		for (const track of timeline) {
			const nodePath = track.nodePathInfo?.sequenceSubscriptionKey ?? null;
			const expanded =
				track.nodePathInfo !== null && getIsExpanded(track.nodePathInfo);
			let dragOverride: object | null = null;
			if (expanded && nodePath !== null) {
				dragOverride = getDragOverrideSnapshot(getDragOverrides(nodePath));
			}

			const effectOverrides =
				expanded && nodePath !== null
					? track.sequence.effects.map((_, index) =>
							getDragOverrideSnapshot(getEffectDragOverrides(nodePath, index)),
						)
					: [];
			const old = previous?.byId.get(track.sequence.id);
			const canReuse =
				!invalidateAll &&
				old?.track === track &&
				old.expanded === expanded &&
				(!expanded ||
					(old.dragOverride === dragOverride &&
						old.effectOverrides.length === effectOverrides.length &&
						old.effectOverrides.every(
							(value, index) => value === effectOverrides[index],
						)));
			const trackItems = canReuse
				? old.items
				: getSelectableTimelineItemsForTrack({
						getDragOverrides,
						getEffectDragOverrides,
						getIsExpanded,
						propStatuses,
						selectedRowKeys,
						track,
						timelinePosition,
					});
			if (!canReuse) {
				changed = true;
			}

			const entry: SelectableTrackCache = {
				track,
				expanded,
				dragOverride,
				effectOverrides,
				items: trackItems,
			};
			byId.set(track.sequence.id, entry);
			if (expanded) {
				expandedEntries.push(entry);
			}

			for (const item of trackItems) {
				items.push(item);
			}
		}

		return {
			timeline,
			propStatuses,
			selectedItems,
			getIsExpanded,
			timelinePosition,
			byId,
			expandedEntries,
			items: changed ? items : previous!.items,
		};
	}, [
		getDragOverrides,
		getEffectDragOverrides,
		getIsExpanded,
		propStatuses,
		selectedItems,
		timeline,
	]);
	useLayoutEffect(() => {
		previousSelectionRef.current = selection;
	}, [selection]);
	const selectableItems = selection.items;

	return (
		<TimelineSelectionOrderProvider items={selectableItems}>
			{children}
		</TimelineSelectionOrderProvider>
	);
};

export const TimelineSelectionProvider: React.FC<{
	readonly children: React.ReactNode;
}> = ({children}) => {
	const {previewServerState} = useContext(StudioServerConnectionCtx);
	const {canvasContent} = useContext(Internals.CompositionManager);
	const timelineSelectionScope =
		canvasContent?.type === 'composition' ? canvasContent.compositionId : null;
	const {expandParentTracks} = useContext(ExpandedTracksSetterContext);
	const {setRightSidebarTemporaryExpansion, sidebarCollapsedStateRight} =
		useContext(SidebarContext);
	const isNarrowLayout = useBreakpoint(SIDEBAR_RESPONSIVE_BREAKPOINTS.right);
	const canSelect =
		isStudioSelectionEnabled() &&
		(previewServerState.type === 'connected' ||
			window.remotion_isReadOnlyStudio);
	const keyframeOperationsAvailable = canUseKeyframeOperations();
	const effectOperationsAvailable = canUseEffectOperations();
	const selectionController = useCanvasSelectionController();
	const selectionState = useCanvasSelection(selectionController);
	const selectionScope = useRef<string | null>(null);
	const marqueeSelectableItems = useRef(
		new Map<
			string,
			{
				readonly getRect: () => DOMRect | null;
				readonly item: TimelineSelection;
				readonly order: number;
			}
		>(),
	);
	const marqueeRegistrationCounter = useRef(0);
	const [revealRequest, setRevealRequest] =
		useState<TimelineSelectionRevealRequest | null>(null);
	const revealTokenCounter = useRef(0);
	const [inspectorRevealRequest, setInspectorRevealRequest] =
		useState<TimelineSelectionRevealRequest | null>(null);
	const inspectorRevealTokenCounter = useRef(0);

	useEffect(() => {
		if (!canSelect) {
			selectionScope.current = null;
			setInspectorRevealRequest(null);
			setRevealRequest(null);
			selectionController.clear();
		}
	}, [canSelect, selectionController]);

	const canSelectItem = useCallback(
		(item: TimelineSelection) =>
			canSelect &&
			(!window.remotion_isReadOnlyStudio ||
				keyframeOperationsAvailable ||
				(effectOperationsAvailable && item.type === 'sequence-effect') ||
				item.type === 'sequence'),
		[canSelect, effectOperationsAvailable, keyframeOperationsAvailable],
	);

	const availableSelectionState =
		selectionScope.current === timelineSelectionScope
			? selectionState
			: EMPTY_TIMELINE_SELECTION_STATE;
	const availableSelectedItems = availableSelectionState.selectedItems;
	const previousSelectedSequenceKeys = useRef<ReadonlySet<string>>(new Set());
	useEffect(() => {
		const selectedSequenceKeys = new Set(
			availableSelectedItems
				.filter((item) => item.type === 'sequence')
				.map(getTimelineSelectionKey),
		);
		const sequenceWasSelected = [...selectedSequenceKeys].some(
			(key) => !previousSelectedSequenceKeys.current.has(key),
		);
		previousSelectedSequenceKeys.current = selectedSequenceKeys;

		if (
			sequenceWasSelected &&
			isNarrowLayout &&
			sidebarCollapsedStateRight === 'responsive'
		) {
			setRightSidebarTemporaryExpansion(true);
		}
	}, [
		availableSelectedItems,
		isNarrowLayout,
		setRightSidebarTemporaryExpansion,
		sidebarCollapsedStateRight,
	]);
	const getAvailableSelectionState = useCallback(
		() =>
			selectionScope.current === timelineSelectionScope
				? selectionController.getSnapshot()
				: EMPTY_TIMELINE_SELECTION_STATE,
		[selectionController, timelineSelectionScope],
	);

	const requestRevealSelectionItem = useCallback((item: TimelineSelection) => {
		revealTokenCounter.current += 1;
		setRevealRequest({
			item,
			token: revealTokenCounter.current,
		});
	}, []);
	const consumeRevealRequest = useCallback((token: number) => {
		setRevealRequest((current) => (current?.token === token ? null : current));
	}, []);
	const consumeInspectorRevealRequest = useCallback((token: number) => {
		setInspectorRevealRequest((current) =>
			current?.token === token ? null : current,
		);
	}, []);
	const requestRevealSelectionItemInInspector = useCallback(
		(item: TimelineSelection) => {
			inspectorRevealTokenCounter.current += 1;
			setInspectorRevealRequest({
				item,
				token: inspectorRevealTokenCounter.current,
			});
		},
		[],
	);

	const expandParentsForSelectionItems = useCallback(
		(items: readonly TimelineSelection[]) => {
			for (const item of items) {
				if (item.type === 'guide') {
					continue;
				}

				expandParentTracks(item.nodePathInfo);
			}
		},
		[expandParentTracks],
	);

	const expandParentsForSelectionItem = useCallback(
		(item: TimelineSelection) => {
			if (item.type === 'guide') {
				return;
			}

			expandParentTracks(item.nodePathInfo);
		},
		[expandParentTracks],
	);

	useEffect(() => {
		if (selectionScope.current === timelineSelectionScope) {
			return;
		}

		selectionScope.current = timelineSelectionScope;
		setRevealRequest(null);
		selectionController.clear();
	}, [selectionController, timelineSelectionScope]);

	const selectedKeys = useMemo(
		() => getTimelineSelectedKeys(availableSelectedItems),
		[availableSelectedItems],
	);

	const isSelected = useCallback(
		(item: TimelineSelection) => {
			return selectedKeys.has(getTimelineSelectionKey(item));
		},
		[selectedKeys],
	);

	const selectItem = useCallback(
		(
			item: TimelineSelection,
			interaction: TimelineSelectionInteraction = {
				shiftKey: false,
				toggleKey: false,
			},
			allSelectableItems: readonly TimelineSelection[] = [],
			options: TimelineSelectionOptions = {},
		) => {
			if (!canSelectItem(item)) {
				return;
			}

			selectOptionsSidebarInspectorPanel();
			expandParentsForSelectionItem(item);
			if (options.reveal) {
				requestRevealSelectionItem(item);
			} else {
				setRevealRequest(null);
			}

			if (options.revealInInspector) {
				requestRevealSelectionItemInInspector(item);
			} else {
				setInspectorRevealRequest(null);
			}

			if (selectionScope.current !== timelineSelectionScope) {
				selectionController.clear();
			}

			selectionScope.current = timelineSelectionScope;
			selectionController.select(item, interaction, allSelectableItems);
		},
		[
			canSelectItem,
			expandParentsForSelectionItem,
			requestRevealSelectionItemInInspector,
			requestRevealSelectionItem,
			selectionController,
			timelineSelectionScope,
		],
	);

	const selectItems = useCallback(
		(
			items: readonly TimelineSelection[],
			options: TimelineSelectionOptions = {},
		) => {
			if (!items.every(canSelectItem)) {
				return;
			}

			if (items.length > 0) {
				selectOptionsSidebarInspectorPanel();
			}

			selectionScope.current = timelineSelectionScope;
			expandParentsForSelectionItems(items);
			if (options.reveal && items.length === 1) {
				requestRevealSelectionItem(items[0]);
			} else {
				setRevealRequest(null);
			}

			if (options.revealInInspector && items.length === 1) {
				requestRevealSelectionItemInInspector(items[0]);
			} else {
				setInspectorRevealRequest(null);
			}

			selectionController.setSelectedItems(items);
		},
		[
			canSelectItem,
			expandParentsForSelectionItems,
			requestRevealSelectionItemInInspector,
			requestRevealSelectionItem,
			selectionController,
			timelineSelectionScope,
		],
	);

	const registerMarqueeSelectableItem = useCallback(
		(item: TimelineSelection, getRect: () => DOMRect | null) => {
			const key = getTimelineSelectionKey(item);
			const registrationOrder = marqueeRegistrationCounter.current;
			marqueeRegistrationCounter.current += 1;
			marqueeSelectableItems.current.set(key, {
				getRect,
				item,
				order: registrationOrder,
			});
			return () => {
				marqueeSelectableItems.current.delete(key);
			};
		},
		[],
	);

	const getMarqueeSelectionCandidates = useCallback(() => {
		return [...marqueeSelectableItems.current.values()]
			.sort((a, b) => a.order - b.order)
			.flatMap((candidate): TimelineMarqueeSelectionCandidate[] => {
				if (!canSelectItem(candidate.item)) {
					return [];
				}

				const rect = candidate.getRect();
				if (rect === null) {
					return [];
				}

				return [{item: candidate.item, rect}];
			});
	}, [canSelectItem]);

	const clearSelection = useCallback(() => {
		selectionScope.current = null;
		setInspectorRevealRequest(null);
		setRevealRequest(null);
		selectionController.clear();
	}, [selectionController]);

	const containsSelection = useCallback(
		(nodePathInfo: SequenceNodePathInfo) => {
			return timelineRowContainsSelection(nodePathInfo, availableSelectedItems);
		},
		[availableSelectedItems],
	);

	const remapSelectionNodePaths = useCallback(
		(mutations: readonly SequenceNodePathMutation[]) => {
			const remappingIndexes = new Map<
				SequenceNodePathMutation['files'][number],
				{
					nextIndex: number;
					byOldPath: Map<string, SequenceNodePathRemapping>;
				}
			>();
			const remapItem = (
				item: TimelineSelection | null,
			): TimelineSelection | null => {
				if (item === null || item.type === 'guide') {
					return item;
				}

				const {sequenceSubscriptionKey} = item.nodePathInfo;
				let {nodePath} = sequenceSubscriptionKey;
				let wasRemapped = false;

				for (const mutation of mutations) {
					const file = mutation.files.find(
						(candidate) =>
							candidate.absolutePath === sequenceSubscriptionKey.absolutePath,
					);
					if (!file) {
						continue;
					}

					const nodePathString = JSON.stringify(nodePath);
					let index = remappingIndexes.get(file);
					if (index === undefined) {
						index = {nextIndex: 0, byOldPath: new Map()};
						remappingIndexes.set(file, index);
					}

					let remapping = index.byOldPath.get(nodePathString);
					// Share each scanned prefix across selected items and the anchor.
					while (
						remapping === undefined &&
						index.nextIndex < file.remappings.length
					) {
						const candidate = file.remappings[index.nextIndex++];
						if (candidate.oldNodePath === null) {
							continue;
						}

						const oldPath = JSON.stringify(candidate.oldNodePath);
						// Keep the original first-match behavior for duplicate paths.
						if (!index.byOldPath.has(oldPath)) {
							index.byOldPath.set(oldPath, candidate);
						}

						if (oldPath === nodePathString) {
							remapping = candidate;
						}
					}

					if (!remapping) {
						continue;
					}

					wasRemapped = true;
					if (remapping.newNodePath === null) {
						return null;
					}

					nodePath = remapping.newNodePath;
				}

				if (!wasRemapped) {
					return item;
				}

				return {
					...item,
					nodePathInfo: {
						...item.nodePathInfo,
						sequenceSubscriptionKey: {
							...sequenceSubscriptionKey,
							nodePath,
						},
					},
				};
			};

			const snapshot = selectionController.getSnapshot();
			const selectedItems = snapshot.selectedItems
				.map(remapItem)
				.filter((item): item is TimelineSelection => item !== null);
			const anchor = remapItem(snapshot.anchor);

			if (
				selectedItems.some(
					(item, index) => item !== snapshot.selectedItems[index],
				) ||
				selectedItems.length !== snapshot.selectedItems.length ||
				anchor !== snapshot.anchor
			) {
				selectionController.setSnapshot({selectedItems, anchor});
			}
		},
		[selectionController],
	);

	const value = useMemo(
		(): TimelineSelectionContextValue => ({
			canSelect,
			inspectorRevealRequest,
			revealRequest,
			consumeRevealRequest,
			consumeInspectorRevealRequest,
			selectedItems: availableSelectedItems,
			isSelected,
			selectItem,
			selectItems,
			registerMarqueeSelectableItem,
			getMarqueeSelectionCandidates,
			containsSelection,
			remapSelectionNodePaths,
			clearSelection,
		}),
		[
			canSelect,
			inspectorRevealRequest,
			revealRequest,
			consumeRevealRequest,
			consumeInspectorRevealRequest,
			availableSelectedItems,
			isSelected,
			selectItem,
			selectItems,
			registerMarqueeSelectableItem,
			getMarqueeSelectionCandidates,
			containsSelection,
			remapSelectionNodePaths,
			clearSelection,
		],
	);
	const currentSelection = useRef(value);
	currentSelection.current = value;
	const rowSelectionContextValue = useMemo(
		(): TimelineRowSelectionContextValue => ({
			canSelect,
			clearSelection,
			getSnapshot: getAvailableSelectionState,
			registerMarqueeSelectableItem,
			selectItem,
			subscribe: selectionController.subscribe,
		}),
		[
			canSelect,
			clearSelection,
			getAvailableSelectionState,
			registerMarqueeSelectableItem,
			selectItem,
			selectionController.subscribe,
		],
	);

	return (
		<TimelineSelectionCanSelectContext.Provider value={canSelect}>
			<CurrentTimelineSelectionContext.Provider value={currentSelection}>
				<TimelineRowSelectionContext.Provider value={rowSelectionContextValue}>
					<TimelineSelectionContext.Provider value={value}>
						{children}
						<TimelineEscapeKeybindings />
						{isStudioInteractivityEnabled() ? (
							<>
								<TimelineClipboardKeybindings />
								<TimelineDeleteKeybindings />
							</>
						) : null}
					</TimelineSelectionContext.Provider>
				</TimelineRowSelectionContext.Provider>
			</CurrentTimelineSelectionContext.Provider>
		</TimelineSelectionCanSelectContext.Provider>
	);
};

export const useTimelineSelectionCanSelect = () => {
	return useContext(TimelineSelectionCanSelectContext);
};

export const useTimelineSelection = () => {
	return useContext(TimelineSelectionContext);
};

export const TIMELINE_MARQUEE_ITEM_ATTR = 'data-timeline-marquee-item';
export const TIMELINE_SCRUBBER_ATTR = 'data-timeline-scrubber';

export const useCurrentTimelineSelectionStateAsRef = () => {
	const currentSelection = useContext(CurrentTimelineSelectionContext);
	if (currentSelection === null) {
		throw new Error(
			'useCurrentTimelineSelectionStateAsRef must be used inside TimelineSelectionProvider',
		);
	}

	return currentSelection;
};

export const useTimelineMarqueeSelection = () => {
	const currentSelection = useCurrentTimelineSelectionStateAsRef();
	const {isHighestContext} = useZIndex();
	const [marqueeRect, setMarqueeRect] = useState<TimelineMarqueeRect | null>(
		null,
	);

	const onPointerDownCapture = useCallback(
		(event: React.PointerEvent<HTMLDivElement>) => {
			const {
				canSelect,
				getMarqueeSelectionCandidates,
				selectedItems,
				selectItems,
			} = currentSelection.current;
			if (!isHighestContext) {
				return;
			}

			if (event.button !== 0 || !canSelect) {
				return;
			}

			if (event.shiftKey) {
				return;
			}

			if (!(event.target instanceof Element)) {
				return;
			}

			if (
				event.target.closest(`[${TIMELINE_MARQUEE_ITEM_ATTR}]`) ||
				event.target.closest(`[${TIMELINE_SCRUBBER_ATTR}]`)
			) {
				return;
			}

			const {currentTarget: target} = event;

			const scrollable = scrollableRef.current;
			const verticalScroll = timelineVerticalScroll.current;
			const initialScrollLeft = scrollable?.scrollLeft ?? 0;
			const initialScrollTop = verticalScroll?.scrollTop ?? 0;

			const initialBounds = target.getBoundingClientRect();
			const start = getClampedTimelineMarqueePoint({
				bounds: {
					bottom: initialBounds.bottom,
					left: initialBounds.left,
					right: initialBounds.right,
					top: initialBounds.top,
				},
				x: event.clientX,
				y: event.clientY,
			});
			const startX = start.x;
			const startY = start.y;
			const previousUserSelect = document.body.style.userSelect;
			const previousWebkitUserSelect = document.body.style.webkitUserSelect;
			document.body.style.userSelect = 'none';
			document.body.style.webkitUserSelect = 'none';

			let hasDragged = false;
			let lockedSelectionKind: TimelineMarqueeSelectionKind | null = null;
			let lastClientX = event.clientX;
			let lastClientY = event.clientY;
			const extendSelection = event.metaKey || event.ctrlKey;
			const selectionBeforeMarquee = selectedItems;
			// Keep content-space bounds for this drag even after virtualization
			// unmounts an item. Refresh mounted items as scrolling reveals them.
			const candidates = new Map<string, TimelineMarqueeSelectionCandidate>();
			const refreshCandidates = () => {
				const scrollLeft = scrollable?.scrollLeft ?? 0;
				const scrollTop = verticalScroll?.scrollTop ?? 0;
				for (const candidate of getMarqueeSelectionCandidates()) {
					candidates.set(getTimelineSelectionKey(candidate.item), {
						item: candidate.item,
						rect: {
							left: candidate.rect.left + scrollLeft,
							right: candidate.rect.right + scrollLeft,
							top: candidate.rect.top + scrollTop,
							bottom: candidate.rect.bottom + scrollTop,
						},
					});
				}
			};

			refreshCandidates();

			const updateSelection = (clientX: number, clientY: number) => {
				lastClientX = clientX;
				lastClientY = clientY;

				// The container moves when the timeline scrolls vertically, so read
				// the bounds live. Restrict them to the visible viewport of the
				// vertical scroller so the fixed-positioned marquee cannot paint
				// outside the visible timeline area.
				const liveBounds = target.getBoundingClientRect();
				const verticalRect = verticalScroll?.getBoundingClientRect() ?? null;
				const bounds: TimelineMarqueeRect = {
					bottom:
						verticalRect === null
							? liveBounds.bottom
							: Math.min(liveBounds.bottom, verticalRect.bottom),
					left: liveBounds.left,
					right: liveBounds.right,
					top:
						verticalRect === null
							? liveBounds.top
							: Math.max(liveBounds.top, verticalRect.top),
				};

				// The anchor is fixed to timeline content, not to the screen:
				// compensate for any scrolling that happened since pointerdown so the
				// marquee keeps covering the originally spanned content while
				// edge auto-scrolling. It is deliberately not clamped to the bounds -
				// it may sit outside the viewport, and the selection should still
				// include everything between it and the pointer.
				const scrollDeltaX = (scrollable?.scrollLeft ?? 0) - initialScrollLeft;
				const scrollDeltaY =
					(verticalScroll?.scrollTop ?? 0) - initialScrollTop;
				const anchorX = startX - scrollDeltaX;
				const anchorY = startY - scrollDeltaY;

				const current = getClampedTimelineMarqueePoint({
					bounds,
					x: clientX,
					y: clientY,
				});
				if (
					!hasDragged &&
					Math.max(
						Math.abs(current.x - anchorX),
						Math.abs(current.y - anchorY),
					) < 3
				) {
					return;
				}

				hasDragged = true;
				const rect = getNormalizedTimelineMarqueeRect({
					currentX: current.x,
					currentY: current.y,
					startX: anchorX,
					startY: anchorY,
				});
				refreshCandidates();
				const scrollLeft = scrollable?.scrollLeft ?? 0;
				const scrollTop = verticalScroll?.scrollTop ?? 0;
				const nextSelection = getTimelineMarqueeSelection({
					candidates: [...candidates.values()],
					lockedSelectionKind,
					marqueeRect: {
						left: rect.left + scrollLeft,
						right: rect.right + scrollLeft,
						top: rect.top + scrollTop,
						bottom: rect.bottom + scrollTop,
					},
				});
				lockedSelectionKind = nextSelection.lockedSelectionKind;
				setMarqueeRect({
					bottom: Math.min(rect.bottom, bounds.bottom),
					left: Math.max(rect.left, bounds.left),
					right: Math.min(rect.right, bounds.right),
					top: Math.max(rect.top, bounds.top),
				});
				selectItems(
					extendSelection
						? extendTimelineMarqueeSelection({
								currentSelection: selectionBeforeMarquee,
								marqueeSelection: nextSelection.selectedItems,
							})
						: nextSelection.selectedItems,
				);
			};

			const autoScroll = startTimelineEdgeAutoScroll({
				includeHorizontal: true,
				includeVertical: true,
				verticalTopOffset: 0,
				onTick: (directions) => {
					if (scrollable && directions.x !== null) {
						scrollable.scrollLeft +=
							directions.x === 'left' ? -SCROLL_INCREMENT : SCROLL_INCREMENT;
					}

					if (verticalScroll && directions.y !== null) {
						verticalScroll.scrollTop +=
							directions.y === 'up'
								? -EDGE_SCROLL_VERTICAL_INCREMENT
								: EDGE_SCROLL_VERTICAL_INCREMENT;
					}

					updateSelection(lastClientX, lastClientY);
				},
			});

			const cleanup = () => {
				autoScroll.stop();
				document.body.style.userSelect = previousUserSelect;
				document.body.style.webkitUserSelect = previousWebkitUserSelect;
				setMarqueeRect(null);
			};

			const onPointerMove = (moveEvent: PointerEvent) => {
				updateSelection(moveEvent.clientX, moveEvent.clientY);
				// Only auto-scroll for an actual marquee drag, not a plain click
				// near an edge
				if (hasDragged) {
					autoScroll.update(moveEvent);
				}
			};

			startCapturedPointerSession({
				event,
				captureTarget: target,
				onMove: onPointerMove,
				onEnd: (reason, endEvent) => {
					if (isPointerSessionRelease(reason, endEvent)) {
						updateSelection(endEvent.clientX, endEvent.clientY);
					}

					cleanup();
				},
			});
		},
		[currentSelection, isHighestContext],
	);

	return {marqueeRect, onPointerDownCapture};
};

export const useTimelineMarqueeSelectableItem = (
	item: TimelineSelection | null,
	ref: React.RefObject<Element | null>,
	horizontalBounds: {readonly cropLeft: number; readonly width: number} | null,
) => {
	const selectionContext = useContext(TimelineRowSelectionContext);
	const sceneRange = useContext(TimelineSceneRangeContext);
	const timelineWidth = useContext(TimelineWidthContext);
	const video = Internals.useUnsafeVideoConfig();
	if (selectionContext === null) {
		throw new Error(
			'useTimelineMarqueeSelectableItem must be used inside TimelineSelectionProvider',
		);
	}

	const {registerMarqueeSelectableItem} = selectionContext;

	useEffect(() => {
		if (item === null) {
			return;
		}

		return registerMarqueeSelectableItem(item, () => {
			const rect = ref.current?.getBoundingClientRect() ?? null;
			if (rect === null) {
				return rect;
			}

			// Sequence bars are cropped to the horizontal render window. Hit-test
			// the full sequence so scrolling does not change its selectable bounds.
			let left = rect.left - (horizontalBounds?.cropLeft ?? 0);
			let right = left + (horizontalBounds?.width ?? rect.width);
			if (sceneRange !== null) {
				const scrollable = scrollableRef.current;
				if (scrollable === null || timelineWidth === null || video === null) {
					return null;
				}

				// CSS scene clipping also hides postmount tails and out-of-scene
				// keyframes. They must not be selectable in the neighboring scene.
				const frameWidth =
					(timelineWidth - TIMELINE_PADDING * 2) / video.durationInFrames;
				const origin =
					scrollable.getBoundingClientRect().left +
					TIMELINE_PADDING -
					scrollable.scrollLeft;
				left = Math.max(
					left,
					origin + Math.max(0, sceneRange.from) * frameWidth,
				);
				right = Math.min(
					right,
					origin +
						Math.min(video.durationInFrames, sceneRange.end) * frameWidth,
				);
				if (right <= left) {
					return null;
				}
			}

			return new DOMRect(left, rect.top, right - left, rect.height);
		});
	}, [
		horizontalBounds,
		item,
		ref,
		registerMarqueeSelectableItem,
		sceneRange,
		timelineWidth,
		video,
	]);
};

export const useTimelineRowSelection = (
	nodePathInfo: SequenceNodePathInfo | null,
	revealInInspector = false,
) => {
	const selectionItem = useMemo(
		(): TimelineSelection | null =>
			getTimelineSelectionFromNodePathInfo(nodePathInfo),
		[nodePathInfo],
	);
	return useTimelineSelectionItem({
		revealInInspector,
		selectionItem,
		useSelectionOrder: true,
	});
};

export const useIsPremountingSelected = (
	nodePathInfo: SequenceNodePathInfo | null,
): boolean => {
	const selectionContext = useContext(TimelineRowSelectionContext);
	if (selectionContext === null) {
		throw new Error(
			'useIsPremountingSelected must be used inside TimelineSelectionProvider',
		);
	}

	const sequenceSelectionKey = useMemo(
		() =>
			nodePathInfo === null
				? null
				: getTimelineSequenceSelectionKey(nodePathInfo),
		[nodePathInfo],
	);
	const getIsPremountingSelected = useCallback(() => {
		if (sequenceSelectionKey === null) {
			return false;
		}

		return selectionContext
			.getSnapshot()
			.selectedItems.some(
				(item) =>
					item.type === 'sequence-prop' &&
					item.key === 'premountFor' &&
					getTimelineSequenceSelectionKey(item.nodePathInfo) ===
						sequenceSelectionKey,
			);
	}, [selectionContext, sequenceSelectionKey]);

	return useSyncExternalStore(
		selectionContext.subscribe,
		getIsPremountingSelected,
		getIsPremountingSelected,
	);
};

const useTimelineSelectionItem = <
	TSelectionItem extends TimelineSelection | null,
>({
	revealInInspector,
	selectionItem,
	useSelectionOrder,
}: {
	readonly revealInInspector: boolean;
	readonly selectionItem: TSelectionItem;
	readonly useSelectionOrder: boolean;
}) => {
	const selectionContext = useContext(TimelineRowSelectionContext);
	if (selectionContext === null) {
		throw new Error(
			'useTimelineSelectionItem must be used inside TimelineSelectionProvider',
		);
	}

	const selectableTimelineItemsRef = useContext(SelectableTimelineItemsContext);
	const selectionItemKey = useMemo(
		() =>
			selectionItem === null ? null : getTimelineSelectionKey(selectionItem),
		[selectionItem],
	);
	const getSelectedState = useCallback((): TimelineSelectionState | null => {
		if (selectionItemKey === null) {
			return null;
		}

		const snapshot = selectionContext.getSnapshot();
		return getTimelineSelectedKeys(snapshot.selectedItems).has(selectionItemKey)
			? snapshot
			: null;
	}, [selectionContext, selectionItemKey]);
	const selectedState = useSyncExternalStore(
		selectionContext.subscribe,
		getSelectedState,
		getSelectedState,
	);

	const onSelect = useCallback(
		(interaction?: TimelineSelectionInteraction) => {
			if (selectionItem === null) {
				return;
			}

			selectionContext.selectItem(
				selectionItem,
				interaction,
				useSelectionOrder ? selectableTimelineItemsRef.current : undefined,
				{revealInInspector},
			);
		},
		[
			revealInInspector,
			selectableTimelineItemsRef,
			selectionContext,
			selectionItem,
			useSelectionOrder,
		],
	);

	return {
		clearSelection: selectionContext.clearSelection,
		onSelect,
		selectable: selectionContext.canSelect && selectionItem !== null,
		selectedItems:
			selectedState?.selectedItems ??
			EMPTY_TIMELINE_SELECTION_STATE.selectedItems,
		selectItem: selectionContext.selectItem,
		selectionItem,
		selected: selectedState !== null,
	};
};

export const useTimelineKeyframeSelection = (
	nodePathInfo: SequenceNodePathInfo,
	frame: number,
) => {
	const selectionItem = useMemo(
		(): TimelineSelection => ({
			type: 'keyframe',
			nodePathInfo,
			frame,
		}),
		[nodePathInfo, frame],
	);
	return useTimelineSelectionItem({
		revealInInspector: false,
		selectionItem,
		useSelectionOrder: true,
	});
};

export const useTimelineEasingSelection = ({
	nodePathInfo,
	fromFrame,
	toFrame,
	segmentIndex,
}: {
	readonly nodePathInfo: SequenceNodePathInfo;
	readonly fromFrame: number;
	readonly toFrame: number;
	readonly segmentIndex: number;
}) => {
	const selectionItem = useMemo(
		(): TimelineEasingSelection => ({
			type: 'easing',
			nodePathInfo,
			fromFrame,
			toFrame,
			segmentIndex,
		}),
		[nodePathInfo, fromFrame, segmentIndex, toFrame],
	);
	return useTimelineSelectionItem({
		revealInInspector: false,
		selectionItem,
		useSelectionOrder: true,
	});
};

export const useTimelineGuideSelection = (guideId: string) => {
	const selectionItem = useMemo(
		(): TimelineSelection => ({
			type: 'guide',
			guideId,
		}),
		[guideId],
	);
	return useTimelineSelectionItem({
		revealInInspector: false,
		selectionItem,
		useSelectionOrder: false,
	});
};

export const useTimelineRowContainsSelection = (
	nodePathInfo: SequenceNodePathInfo | null,
): boolean => {
	const selectionContext = useContext(TimelineRowSelectionContext);
	if (selectionContext === null) {
		throw new Error(
			'useTimelineRowContainsSelection must be used inside TimelineSelectionProvider',
		);
	}

	const getContainsSelection = useCallback(() => {
		if (nodePathInfo === null) {
			return false;
		}

		return timelineRowContainsSelection(
			nodePathInfo,
			selectionContext.getSnapshot().selectedItems,
		);
	}, [nodePathInfo, selectionContext]);

	return useSyncExternalStore(
		selectionContext.subscribe,
		getContainsSelection,
		getContainsSelection,
	);
};

export const useTimelineRowHighlightBackground = (
	nodePathInfo: SequenceNodePathInfo | null,
	{
		hovered,
		selectedBackground,
	}: {
		readonly hovered: boolean;
		readonly selectedBackground: string;
	},
): string | undefined => {
	const {selected} = useTimelineRowSelection(nodePathInfo);
	const containsSelection = useTimelineRowContainsSelection(nodePathInfo);
	return getTimelineRowHighlightBackground({
		showSelectedBackground: true,
		selected,
		containsSelection,
		hovered,
		selectedBackground,
	});
};