Skip to content

🎥 Make videos programmatically with React

yees

JonnyBurger18d03d7dc60e
diff --git a/packages/studio/src/components/ExpandedTracksProvider.tsx b/packages/studio/src/components/ExpandedTracksProvider.tsx
index d95db42b88..75a862d178 100644
--- a/packages/studio/src/components/ExpandedTracksProvider.tsx
+++ b/packages/studio/src/components/ExpandedTracksProvider.tsx
@@ -1,16 +1,32 @@
import React, {createContext, useCallback, useMemo, useState} from 'react';
+import type {SequenceNodePath} from 'remotion';
-type ExpandedTracksContextValue = {
- readonly expandedTracks: Record<string, boolean>;
- readonly toggleTrack: (id: string) => void;
+const nodePathToKey = (nodePath: SequenceNodePath): string =>
+ JSON.stringify(nodePath);
+
+export type GetIsExpanded = (nodePath: SequenceNodePath) => boolean;
+
+type ExpandedTracksGetterContextValue = {
+ readonly getIsExpanded: GetIsExpanded;
+};
+
+type ExpandedTracksSetterContextValue = {
+ readonly toggleTrack: (nodePath: SequenceNodePath) => void;
};
-export const ExpandedTracksContext = createContext<ExpandedTracksContextValue>({
- expandedTracks: {},
- toggleTrack: () => {
- throw new Error('ExpandedTracksContext not initialized');
- },
-});
+export const ExpandedTracksGetterContext =
+ createContext<ExpandedTracksGetterContextValue>({
+ getIsExpanded: () => {
+ throw new Error('ExpandedTracksGetterContext not initialized');
+ },
+ });
+
+export const ExpandedTracksSetterContext =
+ createContext<ExpandedTracksSetterContextValue>({
+ toggleTrack: () => {
+ throw new Error('ExpandedTracksSetterContext not initialized');
+ },
+ });
export const ExpandedTracksProvider: React.FC<{
readonly children: React.ReactNode;
@@ -19,24 +35,31 @@ export const ExpandedTracksProvider: React.FC<{
{},
);
- const toggleTrack = useCallback((id: string) => {
- setExpandedTracks((prev) => ({
- ...prev,
- [id]: !prev[id],
- }));
+ const toggleTrack = useCallback((nodePath: SequenceNodePath) => {
+ setExpandedTracks((prev) => {
+ const key = nodePathToKey(nodePath);
+ return {...prev, [key]: !prev[key]};
+ });
}, []);
- const value = useMemo(
- (): ExpandedTracksContextValue => ({
- expandedTracks,
- toggleTrack,
+ const getterValue = useMemo(
+ (): ExpandedTracksGetterContextValue => ({
+ getIsExpanded: (nodePath) =>
+ expandedTracks[nodePathToKey(nodePath)] ?? false,
}),
- [expandedTracks, toggleTrack],
+ [expandedTracks],
+ );
+
+ const setterValue = useMemo(
+ (): ExpandedTracksSetterContextValue => ({toggleTrack}),
+ [toggleTrack],
);
return (
- <ExpandedTracksContext.Provider value={value}>
- {children}
- </ExpandedTracksContext.Provider>
+ <ExpandedTracksSetterContext.Provider value={setterValue}>
+ <ExpandedTracksGetterContext.Provider value={getterValue}>
+ {children}
+ </ExpandedTracksGetterContext.Provider>
+ </ExpandedTracksSetterContext.Provider>
);
};
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
index 1b2e35c506..bf394f7fd6 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
@@ -8,6 +8,7 @@ import {
getTreeRowHeight,
TREE_GROUP_ROW_HEIGHT,
} from '../../helpers/timeline-layout';
+import type {GetIsExpanded} from '../ExpandedTracksProvider';
import {Padder} from './Padder';
import {TimelineExpandArrowButton} from './TimelineExpandArrowButton';
import {TimelineFieldRow} from './TimelineFieldRow';
@@ -36,8 +37,8 @@ export const TimelineExpandedRow: React.FC<{
readonly node: TimelineTreeNode;
readonly depth: number;
readonly nestedDepth: number;
- readonly expandedTracks: Record<string, boolean>;
- readonly toggleTrack: (id: string) => void;
+ readonly getIsExpanded: GetIsExpanded;
+ readonly toggleTrack: (nodePath: SequenceNodePath) => void;
readonly validatedLocation: CodePosition | null;
readonly nodePath: SequenceNodePath | null;
readonly schema: SequenceSchema;
@@ -45,7 +46,7 @@ export const TimelineExpandedRow: React.FC<{
node,
depth,
nestedDepth,
- expandedTracks,
+ getIsExpanded,
toggleTrack,
validatedLocation,
nodePath,
@@ -68,13 +69,13 @@ export const TimelineExpandedRow: React.FC<{
);
if (node.kind === 'group') {
- const isExpanded = expandedTracks[node.id] ?? false;
+ const isExpanded = getIsExpanded(node.nodePath);
return (
<div style={groupStyle}>
<Padder depth={nestedDepth + 1} />
<TimelineExpandArrowButton
isExpanded={isExpanded}
- onClick={() => toggleTrack(node.id)}
+ onClick={() => toggleTrack(node.nodePath)}
label={`${node.label} section`}
/>
<span style={rowLabel}>{node.label}</span>
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
index 6fb2123ef3..3823bbbef8 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
@@ -11,7 +11,10 @@ import {
flattenVisibleTreeNodes,
getExpandedTrackHeight,
} from '../../helpers/timeline-layout';
-import {ExpandedTracksContext} from '../ExpandedTracksProvider';
+import {
+ ExpandedTracksGetterContext,
+ ExpandedTracksSetterContext,
+} from '../ExpandedTracksProvider';
import {TimelineExpandedRow} from './TimelineExpandedRow';
const expandedSectionBase: React.CSSProperties = {
@@ -31,16 +34,14 @@ const separator: React.CSSProperties = {
export const TimelineExpandedSection: React.FC<{
readonly sequence: TSequence;
readonly originalLocation: OriginalPosition | null;
- readonly nodePath: SequenceNodePath | null;
+ readonly nodePath: SequenceNodePath;
readonly nestedDepth: number;
}> = ({sequence, originalLocation, nodePath, nestedDepth}) => {
- const {expandedTracks, toggleTrack} = useContext(ExpandedTracksContext);
+ const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
+ const {toggleTrack} = useContext(ExpandedTracksSetterContext);
const {getDragOverrides, getCodeValues} = useContext(
Internals.VisualModeGettersContext,
);
- const {overrideIdToNodePathMappings: subscriptionStates} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
- );
const validatedLocation: CodePosition | null = useMemo(() => {
if (
@@ -62,34 +63,28 @@ export const TimelineExpandedSection: React.FC<{
() =>
buildTimelineTree({
sequence,
+ nodePath,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState: subscriptionStates,
}),
- [sequence, getDragOverrides, getCodeValues, subscriptionStates],
+ [sequence, nodePath, getDragOverrides, getCodeValues],
);
const flat = useMemo(
- () => flattenVisibleTreeNodes({nodes: tree, expandedTracks}),
- [tree, expandedTracks],
+ () => flattenVisibleTreeNodes({nodes: tree, getIsExpanded}),
+ [tree, getIsExpanded],
);
const expandedHeight = useMemo(
() =>
getExpandedTrackHeight({
sequence,
- expandedTracks,
+ nodePath,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState: subscriptionStates,
}),
- [
- sequence,
- expandedTracks,
- getDragOverrides,
- getCodeValues,
- subscriptionStates,
- ],
+ [sequence, nodePath, getIsExpanded, getDragOverrides, getCodeValues],
);
const style = useMemo(() => {
@@ -109,13 +104,13 @@ export const TimelineExpandedSection: React.FC<{
<div style={style}>
{flat.map(({node, depth}, i) => {
return (
- <React.Fragment key={node.id}>
+ <React.Fragment key={JSON.stringify(node.nodePath)}>
{i > 0 ? <div style={separator} /> : null}
<TimelineExpandedRow
node={node}
depth={depth}
nestedDepth={nestedDepth}
- expandedTracks={expandedTracks}
+ getIsExpanded={getIsExpanded}
toggleTrack={toggleTrack}
validatedLocation={validatedLocation}
nodePath={nodePath}
diff --git a/packages/studio/src/components/Timeline/TimelineListItem.tsx b/packages/studio/src/components/Timeline/TimelineListItem.tsx
index c9f44a064e..8271772956 100644
--- a/packages/studio/src/components/Timeline/TimelineListItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineListItem.tsx
@@ -10,7 +10,10 @@ import {
} from '../../helpers/timeline-layout';
import {callApi} from '../call-api';
import {ContextMenu} from '../ContextMenu';
-import {ExpandedTracksContext} from '../ExpandedTracksProvider';
+import {
+ ExpandedTracksGetterContext,
+ ExpandedTracksSetterContext,
+} from '../ExpandedTracksProvider';
import type {ComboboxValue} from '../NewComposition/ComboBox';
import {showNotification} from '../Notifications/NotificationCenter';
import {Padder} from './Padder';
@@ -40,7 +43,8 @@ export const TimelineListItem: React.FC<{
const {hidden, setHidden} = useContext(
Internals.SequenceVisibilityToggleContext,
);
- const {expandedTracks, toggleTrack} = useContext(ExpandedTracksContext);
+ const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
+ const {toggleTrack} = useContext(ExpandedTracksSetterContext);
const {getIsJsxInMapCallback} = useContext(
Internals.VisualModeGettersContext,
);
@@ -181,11 +185,16 @@ export const TimelineListItem: React.FC<{
visualModeEnvEnabled,
]);
- const isExpanded = visualModeActive && (expandedTracks[sequence.id] ?? false);
+ const isExpanded =
+ visualModeActive && nodePath !== null && getIsExpanded(nodePath);
const onToggleExpand = useCallback(() => {
- toggleTrack(sequence.id);
- }, [sequence.id, toggleTrack]);
+ if (nodePath === null) {
+ return;
+ }
+
+ toggleTrack(nodePath);
+ }, [nodePath, toggleTrack]);
const isItemHidden = useMemo(() => {
return hidden[sequence.id] ?? false;
diff --git a/packages/studio/src/components/Timeline/TimelineTracks.tsx b/packages/studio/src/components/Timeline/TimelineTracks.tsx
index 2e147a6b42..981b84cf7c 100644
--- a/packages/studio/src/components/Timeline/TimelineTracks.tsx
+++ b/packages/studio/src/components/Timeline/TimelineTracks.tsx
@@ -1,9 +1,5 @@
import React, {useContext, useMemo} from 'react';
-import type {
- GetCodeValues,
- GetDragOverrides,
- OverrideIdToNodePaths,
-} from 'remotion';
+import type {GetCodeValues, GetDragOverrides, SequenceNodePath} from 'remotion';
import {Internals, type TSequence} from 'remotion';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import type {TrackWithHash} from '../../helpers/get-timeline-sequence-sort-key';
@@ -13,7 +9,10 @@ import {
TIMELINE_ITEM_BORDER_BOTTOM,
TIMELINE_PADDING,
} from '../../helpers/timeline-layout';
-import {ExpandedTracksContext} from '../ExpandedTracksProvider';
+import {
+ ExpandedTracksGetterContext,
+ type GetIsExpanded,
+} from '../ExpandedTracksProvider';
import {isTrackHidden} from './is-collapsed';
import {MaxTimelineTracksReached} from './MaxTimelineTracks';
import {TimelineSequence} from './TimelineSequence';
@@ -31,24 +30,24 @@ const timelineContent: React.CSSProperties = {
const getExpandedPlaceholderStyle = ({
sequence,
- expandedTracks,
+ nodePath,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState,
}: {
sequence: TSequence;
- expandedTracks: Record<string, boolean>;
+ nodePath: SequenceNodePath;
+ getIsExpanded: GetIsExpanded;
getDragOverrides: GetDragOverrides;
getCodeValues: GetCodeValues;
- sequencePropsSubscriptionState: OverrideIdToNodePaths;
}): React.CSSProperties => ({
height:
getExpandedTrackHeight({
sequence,
- expandedTracks,
+ nodePath,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState,
}) + TIMELINE_ITEM_BORDER_BOTTOM,
});
@@ -56,14 +55,11 @@ export const TimelineTracks: React.FC<{
readonly timeline: TrackWithHash[];
readonly hasBeenCut: boolean;
}> = ({timeline, hasBeenCut}) => {
- const {expandedTracks} = useContext(ExpandedTracksContext);
+ const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
const {previewServerState} = useContext(StudioServerConnectionCtx);
const {getDragOverrides, getCodeValues} = useContext(
Internals.VisualModeGettersContext,
);
- const {overrideIdToNodePathMappings: subscriptionStates} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
- );
const visualModeEnabled =
Boolean(process.env.EXPERIMENTAL_VISUAL_MODE_ENABLED) &&
@@ -85,7 +81,8 @@ export const TimelineTracks: React.FC<{
return null;
}
- const isExpanded = expandedTracks[track.sequence.id] ?? false;
+ const isExpanded =
+ track.nodePath !== null && getIsExpanded(track.nodePath);
return (
<div key={track.sequence.id}>
@@ -97,14 +94,14 @@ export const TimelineTracks: React.FC<{
>
<TimelineSequence s={track.sequence} />
</div>
- {visualModeEnabled && isExpanded ? (
+ {visualModeEnabled && isExpanded && track.nodePath ? (
<div
style={getExpandedPlaceholderStyle({
sequence: track.sequence,
- expandedTracks,
+ nodePath: track.nodePath,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState: subscriptionStates,
})}
/>
) : null}
diff --git a/packages/studio/src/components/Timeline/use-timeline-height.ts b/packages/studio/src/components/Timeline/use-timeline-height.ts
index 9020cdbba0..74c431fc65 100644
--- a/packages/studio/src/components/Timeline/use-timeline-height.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-height.ts
@@ -7,7 +7,7 @@ import {
getTimelineLayerHeight,
TIMELINE_ITEM_BORDER_BOTTOM,
} from '../../helpers/timeline-layout';
-import {ExpandedTracksContext} from '../ExpandedTracksProvider';
+import {ExpandedTracksGetterContext} from '../ExpandedTracksProvider';
import {MAX_TIMELINE_TRACKS_NOTICE_HEIGHT} from './MaxTimelineTracks';
import {TIMELINE_TIME_INDICATOR_HEIGHT} from './TimelineTimeIndicators';
@@ -18,14 +18,11 @@ export const useTimelineHeight = ({
shown: TrackWithHash[];
hasBeenCut: boolean;
}): number => {
- const {expandedTracks} = useContext(ExpandedTracksContext);
+ const {getIsExpanded} = useContext(ExpandedTracksGetterContext);
const {previewServerState} = useContext(StudioServerConnectionCtx);
const {getDragOverrides, getCodeValues} = useContext(
Internals.VisualModeGettersContext,
);
- const {overrideIdToNodePathMappings: subscriptionStates} = useContext(
- Internals.OverrideIdsToNodePathsGettersContext,
- );
const visualModeEnabled =
Boolean(process.env.EXPERIMENTAL_VISUAL_MODE_ENABLED) &&
@@ -34,19 +31,22 @@ export const useTimelineHeight = ({
return useMemo(() => {
const tracksHeight = shown.reduce((acc, track) => {
const isExpanded =
- visualModeEnabled && (expandedTracks[track.sequence.id] ?? false);
+ visualModeEnabled &&
+ track.nodePath !== null &&
+ getIsExpanded(track.nodePath);
const layerHeight =
getTimelineLayerHeight(track.sequence.type) +
TIMELINE_ITEM_BORDER_BOTTOM;
- const expandedHeight = isExpanded
- ? getExpandedTrackHeight({
- sequence: track.sequence,
- expandedTracks,
- getDragOverrides,
- getCodeValues,
- sequencePropsSubscriptionState: subscriptionStates,
- }) + TIMELINE_ITEM_BORDER_BOTTOM
- : 0;
+ const expandedHeight =
+ isExpanded && track.nodePath
+ ? getExpandedTrackHeight({
+ sequence: track.sequence,
+ nodePath: track.nodePath,
+ getIsExpanded,
+ getDragOverrides,
+ getCodeValues,
+ }) + TIMELINE_ITEM_BORDER_BOTTOM
+ : 0;
return acc + layerHeight + expandedHeight;
}, 0);
@@ -59,10 +59,9 @@ export const useTimelineHeight = ({
}, [
shown,
hasBeenCut,
- expandedTracks,
visualModeEnabled,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- subscriptionStates,
]);
};
diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.ts
index 68d8da87ba..b137a511c7 100644
--- a/packages/studio/src/helpers/timeline-layout.ts
+++ b/packages/studio/src/helpers/timeline-layout.ts
@@ -9,9 +9,10 @@ import type {
EffectDefinitionAndStack,
GetCodeValues,
GetDragOverrides,
+ SequenceNodePath,
TSequence,
- OverrideIdToNodePaths,
} from 'remotion';
+import type {GetIsExpanded} from '../components/ExpandedTracksProvider';
export type {CodeValues, DragOverrides, SchemaFieldInfo, SequenceControls};
export {
@@ -51,45 +52,45 @@ export const getEffectSchemaLabels = (
export type TimelineTreeNode =
| {
readonly kind: 'group';
- readonly id: string;
+ readonly nodePath: SequenceNodePath;
readonly label: string;
readonly children: TimelineTreeNode[];
}
| {
readonly kind: 'field';
- readonly id: string;
+ readonly nodePath: SequenceNodePath;
readonly label: string;
readonly field: SchemaFieldInfo | null;
};
export const buildTimelineTree = ({
sequence,
+ nodePath,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState,
}: {
sequence: TSequence;
+ nodePath: SequenceNodePath;
getDragOverrides: GetDragOverrides;
getCodeValues: GetCodeValues;
- sequencePropsSubscriptionState: OverrideIdToNodePaths;
}): TimelineTreeNode[] => {
const roots: TimelineTreeNode[] = [];
if (sequence.effects.length > 0) {
roots.push({
kind: 'group',
- id: `${sequence.id}::effects`,
+ nodePath: [...nodePath, 'effects'],
label: 'Effects',
children: sequence.effects.map((effect, i): TimelineTreeNode => {
- const effectId = `${sequence.id}::effects::${i}`;
+ const effectNodePath: SequenceNodePath = [...nodePath, 'effects', i];
return {
kind: 'group',
- id: effectId,
+ nodePath: effectNodePath,
label: effect.definition.label,
children: getEffectSchemaLabels(effect).map(
(label): TimelineTreeNode => ({
kind: 'field',
- id: `${effectId}::${label.key}`,
+ nodePath: [...effectNodePath, label.key],
label: label.description ?? label.key,
field: null,
}),
@@ -99,8 +100,6 @@ export const buildTimelineTree = ({
});
}
- const nodePath =
- sequencePropsSubscriptionState[sequence.controls!.overrideId!] ?? null;
const controlFields = getFieldsToShow({
schema: sequence.controls!.schema,
currentRuntimeValueDotNotation:
@@ -114,7 +113,7 @@ export const buildTimelineTree = ({
for (const f of controlFields) {
roots.push({
kind: 'field',
- id: `${sequence.id}::controls::${f.key}`,
+ nodePath: [...nodePath, 'controls', f.key],
label: f.description ?? f.key,
field: f,
});
@@ -131,21 +130,21 @@ export type FlatTreeRow = {
export const flattenVisibleTreeNodes = ({
nodes,
- expandedTracks,
+ getIsExpanded,
depth = 0,
}: {
nodes: TimelineTreeNode[];
- expandedTracks: Record<string, boolean>;
+ getIsExpanded: GetIsExpanded;
depth?: number;
}): FlatTreeRow[] => {
const out: FlatTreeRow[] = [];
for (const node of nodes) {
out.push({node, depth});
- if (node.kind === 'group' && (expandedTracks[node.id] ?? false)) {
+ if (node.kind === 'group' && getIsExpanded(node.nodePath)) {
out.push(
...flattenVisibleTreeNodes({
nodes: node.children,
- expandedTracks,
+ getIsExpanded,
depth: depth + 1,
}),
);
@@ -165,24 +164,24 @@ export const getTreeRowHeight = (node: TimelineTreeNode): number => {
export const getExpandedTrackHeight = ({
sequence,
- expandedTracks,
+ nodePath,
+ getIsExpanded,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState,
}: {
sequence: TSequence;
- expandedTracks: Record<string, boolean>;
+ nodePath: SequenceNodePath;
+ getIsExpanded: GetIsExpanded;
getDragOverrides: GetDragOverrides;
getCodeValues: GetCodeValues;
- sequencePropsSubscriptionState: OverrideIdToNodePaths;
}): number => {
const tree = buildTimelineTree({
sequence,
+ nodePath,
getDragOverrides,
getCodeValues,
- sequencePropsSubscriptionState,
});
- const flat = flattenVisibleTreeNodes({nodes: tree, expandedTracks});
+ const flat = flattenVisibleTreeNodes({nodes: tree, getIsExpanded});
if (flat.length === 0) {
return TIMELINE_TRACK_EXPANDED_HEIGHT;