Skip to content

🎥 Make videos programmatically with React

persist in and out

Jonny Burgerc1546a623327
diff --git a/packages/cli/src/editor/components/InitialCompositionLoader.tsx b/packages/cli/src/editor/components/InitialCompositionLoader.tsx
index 03c817492c..29c488804a 100644
--- a/packages/cli/src/editor/components/InitialCompositionLoader.tsx
+++ b/packages/cli/src/editor/components/InitialCompositionLoader.tsx
@@ -4,17 +4,14 @@ import type {AnyComposition} from 'remotion';
import {Internals} from 'remotion';
import type {ExpandedFoldersState} from '../helpers/persist-open-folders';
import {FolderContext} from '../state/folders';
-import {loadMarks} from '../state/marks';
import {getKeysToExpand} from './CompositionSelector';
import {getCurrentCompositionFromUrl} from './FramePersistor';
-import {inOutHandles} from './TimelineInOutToggle';
export const useSelectComposition = () => {
const {setFoldersExpanded} = useContext(FolderContext);
const {setCurrentComposition} = useContext(Internals.CompositionManager);
return (c: AnyComposition, push: boolean) => {
- inOutHandles.current?.setMarks(loadMarks(c.id, c.durationInFrames));
if (push) {
window.history.pushState({}, 'Studio', `/${c.id}`);
}
diff --git a/packages/cli/src/editor/components/SetTimelineInOutProvider.tsx b/packages/cli/src/editor/components/SetTimelineInOutProvider.tsx
index 02f6085082..292e15e87b 100644
--- a/packages/cli/src/editor/components/SetTimelineInOutProvider.tsx
+++ b/packages/cli/src/editor/components/SetTimelineInOutProvider.tsx
@@ -1,18 +1,16 @@
-import React, {useMemo, useState} from 'react';
+import React, {useEffect, useMemo, useState} from 'react';
import type {
SetTimelineInOutContextValue,
TimelineInOutContextValue,
} from '../state/in-out';
import {SetTimelineInOutContext, TimelineInOutContext} from '../state/in-out';
+import {loadMarks, persistMarks} from '../state/marks';
export const SetTimelineInOutProvider: React.FC<{
children: React.ReactNode;
}> = ({children}) => {
const [inAndOutFrames, setInAndOutFrames] =
- useState<TimelineInOutContextValue>({
- inFrame: null,
- outFrame: null,
- });
+ useState<TimelineInOutContextValue>(() => loadMarks());
const setTimelineInOutContextValue =
useMemo((): SetTimelineInOutContextValue => {
@@ -21,6 +19,10 @@ export const SetTimelineInOutProvider: React.FC<{
};
}, []);
+ useEffect(() => {
+ persistMarks(inAndOutFrames);
+ }, [inAndOutFrames]);
+
return (
<TimelineInOutContext.Provider value={inAndOutFrames}>
<SetTimelineInOutContext.Provider value={setTimelineInOutContextValue}>
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
index b2d57ecda6..f21e11d30f 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
@@ -407,27 +407,39 @@ const Inner: React.FC = () => {
if (frame < 1) {
return setInAndOutFrames((prev) => ({
...prev,
- inFrame: null,
+ [videoConfig.id]: {
+ ...prev[videoConfig.id],
+ inFrame: null,
+ },
}));
}
const maxFrame = outFrame === null ? Infinity : outFrame - 1;
setInAndOutFrames((prev) => ({
...prev,
- inFrame: Math.min(maxFrame, frame),
+ [videoConfig.id]: {
+ ...prev[videoConfig.id],
+ inFrame: Math.min(maxFrame, frame),
+ },
}));
} else {
if (frame > videoConfig.durationInFrames - 2) {
return setInAndOutFrames((prev) => ({
...prev,
- outFrame: null,
+ [videoConfig.id]: {
+ ...prev[videoConfig.id],
+ outFrame: null,
+ },
}));
}
const minFrame = inFrame === null ? -Infinity : inFrame + 1;
setInAndOutFrames((prev) => ({
...prev,
- outFrame: Math.max(minFrame, frame),
+ [videoConfig.id]: {
+ ...prev[videoConfig.id],
+ outFrame: Math.max(minFrame, frame),
+ },
}));
}
},
diff --git a/packages/cli/src/editor/components/TimelineInOutToggle.tsx b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
index d02719f049..24bcaf671c 100644
--- a/packages/cli/src/editor/components/TimelineInOutToggle.tsx
+++ b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
@@ -1,7 +1,6 @@
import React, {
createRef,
useCallback,
- useContext,
useEffect,
useImperativeHandle,
} from 'react';
@@ -17,11 +16,11 @@ import {
TimelineInPointer,
TimelineOutPointer,
} from '../icons/timelineInOutPointer';
+import type {InOutValue, TimelineInOutContextValue} from '../state/in-out';
import {
useTimelineInOutFramePosition,
useTimelineSetInOutFramePosition,
} from '../state/in-out';
-import {persistMarks} from '../state/marks';
import {ControlButton} from './ControlButton';
const getTooltipText = (pointType: string, key: string) =>
@@ -42,26 +41,35 @@ export const inOutHandles = createRef<{
inMarkClick: (e: KeyboardEvent | null) => void;
outMarkClick: (e: KeyboardEvent | null) => void;
clearMarks: () => void;
- setMarks: (marks: [number | null, number | null]) => void;
}>();
+const defaultInOutValue: InOutValue = {inFrame: null, outFrame: null};
+
export const TimelineInOutPointToggle: React.FC = () => {
const timelinePosition = Internals.Timeline.useTimelinePosition();
const {inFrame, outFrame} = useTimelineInOutFramePosition();
+
+ console.log({inFrame, outFrame});
+
const {setInAndOutFrames} = useTimelineSetInOutFramePosition();
- const {currentComposition} = useContext(Internals.CompositionManager);
const isStill = useIsStill();
const videoConfig = Internals.useUnsafeVideoConfig();
const keybindings = useKeybinding();
- const onInOutClear = useCallback(() => {
- setInAndOutFrames(() => {
- return {
- inFrame: null,
- outFrame: null,
- };
- });
- }, [setInAndOutFrames]);
+ const onInOutClear = useCallback(
+ (composition: string) => {
+ setInAndOutFrames((prev) => {
+ return {
+ ...prev,
+ [composition]: {
+ inFrame: null,
+ outFrame: null,
+ },
+ };
+ });
+ },
+ [setInAndOutFrames]
+ );
const onInMark = useCallback(
(e: KeyboardEvent | React.MouseEvent | null) => {
@@ -70,40 +78,54 @@ export const TimelineInOutPointToggle: React.FC = () => {
}
if (e?.shiftKey) {
- setInAndOutFrames((f) => {
+ setInAndOutFrames((prev) => {
return {
- ...f,
- inFrame: null,
+ ...prev,
+ [videoConfig.id]: {
+ ...prev[videoConfig.id],
+ inFrame: null,
+ },
};
});
return null;
}
- setInAndOutFrames((prev) => {
+ setInAndOutFrames((prev): TimelineInOutContextValue => {
+ const prevOut = prev[videoConfig.id]?.outFrame;
const biggestPossible =
- prev.outFrame === null ? Infinity : prev.outFrame - 1;
+ prevOut === undefined || prevOut === null ? Infinity : prevOut - 1;
const selected = Math.min(timelinePosition, biggestPossible);
if (selected === 0) {
return {
...prev,
- inFrame: null,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ inFrame: null,
+ },
};
}
- if (prev.inFrame !== null) {
+ const prevIn = prev[videoConfig.id]?.inFrame;
+ if (prevIn !== null && prevIn !== undefined) {
// Disable if already at this position
- if (prev.inFrame === selected) {
+ if (prevIn === selected) {
return {
...prev,
- inFrame: null,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ inFrame: null,
+ },
};
}
}
return {
...prev,
- inFrame: selected,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ inFrame: selected,
+ },
};
});
},
@@ -121,7 +143,10 @@ export const TimelineInOutPointToggle: React.FC = () => {
setInAndOutFrames((f) => {
return {
...f,
- inFrame: null,
+ [videoConfig.id]: {
+ ...(f[videoConfig.id] ?? defaultInOutValue),
+ inFrame: null,
+ },
};
});
},
@@ -139,7 +164,10 @@ export const TimelineInOutPointToggle: React.FC = () => {
setInAndOutFrames((f) => {
return {
...f,
- outFrame: null,
+ [videoConfig.id]: {
+ ...(f[videoConfig.id] ?? defaultInOutValue),
+ outFrame: null,
+ },
};
});
},
@@ -156,43 +184,66 @@ export const TimelineInOutPointToggle: React.FC = () => {
setInAndOutFrames((f) => {
return {
...f,
- outFrame: null,
+ [videoConfig.id]: {
+ ...(f[videoConfig.id] ?? defaultInOutValue),
+ outFrame: null,
+ },
};
});
return;
}
setInAndOutFrames((prev) => {
+ const prevInFrame = prev[videoConfig.id]?.inFrame;
const smallestPossible =
- prev.inFrame === null ? -Infinity : prev.inFrame + 1;
+ prevInFrame === null || prevInFrame === undefined
+ ? -Infinity
+ : prevInFrame + 1;
const selected = Math.max(timelinePosition, smallestPossible);
if (selected === videoConfig.durationInFrames - 1) {
return {
...prev,
- outFrame: null,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ outFrame: null,
+ },
};
}
- if (prev.outFrame !== null) {
- if (prev.outFrame === selected) {
+ const prevOut = prev[videoConfig.id]?.outFrame;
+
+ if (prevOut !== null && prevOut !== undefined) {
+ if (prevOut === selected) {
return {
...prev,
- outFrame: null,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ outFrame: null,
+ },
};
}
}
return {
...prev,
- outFrame: selected,
+ [videoConfig.id]: {
+ ...(prev[videoConfig.id] ?? defaultInOutValue),
+ outFrame: selected,
+ },
};
});
},
[setInAndOutFrames, timelinePosition, videoConfig]
);
+ const confId = videoConfig?.id;
+
useEffect(() => {
+ if (!confId) {
+ return;
+ }
+
const iKey = keybindings.registerKeybinding({
event: 'keypress',
key: 'i',
@@ -217,7 +268,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
event: 'keypress',
key: 'x',
callback: () => {
- onInOutClear();
+ onInOutClear(confId);
},
commandCtrlKey: false,
preventDefault: true,
@@ -228,18 +279,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
iKey.unregister();
xKey.unregister();
};
- }, [keybindings, onInMark, onInOutClear, onOutMark]);
-
- useEffect(() => {
- if (!currentComposition || !videoConfig) {
- return;
- }
-
- persistMarks(currentComposition, videoConfig.durationInFrames, [
- inFrame,
- outFrame,
- ]);
- }, [currentComposition, inFrame, outFrame, videoConfig]);
+ }, [confId, keybindings, onInMark, onInOutClear, onOutMark]);
// If duration changes and it goes out of range, we reset
useEffect(() => {
@@ -252,7 +292,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
}
if (outFrame >= videoConfig.durationInFrames - 1) {
- onInOutClear();
+ onInOutClear(videoConfig.id);
}
}, [onInOutClear, outFrame, videoConfig]);
@@ -266,7 +306,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
}
if (inFrame >= videoConfig.durationInFrames - 1) {
- onInOutClear();
+ onInOutClear(videoConfig.id);
}
}, [onInOutClear, inFrame, videoConfig]);
@@ -274,18 +314,18 @@ export const TimelineInOutPointToggle: React.FC = () => {
inOutHandles,
() => {
return {
- clearMarks: onInOutClear,
+ clearMarks: () => {
+ if (!confId) {
+ return;
+ }
+
+ onInOutClear(confId);
+ },
inMarkClick: onInMark,
outMarkClick: onOutMark,
- setMarks: ([newInFrame, newOutFrame]) => {
- setInAndOutFrames({
- inFrame: newInFrame,
- outFrame: newOutFrame,
- });
- },
};
},
- [onInMark, onInOutClear, onOutMark, setInAndOutFrames]
+ [confId, onInMark, onInOutClear, onOutMark]
);
if (isStill) {
diff --git a/packages/cli/src/editor/state/in-out.ts b/packages/cli/src/editor/state/in-out.ts
index 47c30532f6..6d606de1bc 100644
--- a/packages/cli/src/editor/state/in-out.ts
+++ b/packages/cli/src/editor/state/in-out.ts
@@ -1,21 +1,23 @@
import type React from 'react';
import {createContext, useContext} from 'react';
+import {Internals} from 'remotion';
-export type TimelineInOutContextValue = {
+export type InOutValue = {
inFrame: number | null;
outFrame: number | null;
};
+export type TimelineInOutContextValue = Record<string, InOutValue>;
+
export type SetTimelineInOutContextValue = {
setInAndOutFrames: (
u: React.SetStateAction<TimelineInOutContextValue>
) => void;
};
-export const TimelineInOutContext = createContext<TimelineInOutContextValue>({
- inFrame: null,
- outFrame: null,
-});
+export const TimelineInOutContext = createContext<TimelineInOutContextValue>(
+ {}
+);
export const SetTimelineInOutContext =
createContext<SetTimelineInOutContextValue>({
@@ -24,9 +26,14 @@ export const SetTimelineInOutContext =
},
});
-export const useTimelineInOutFramePosition = (): TimelineInOutContextValue => {
+export const useTimelineInOutFramePosition = (): InOutValue => {
+ const videoConfig = Internals.useUnsafeVideoConfig();
const state = useContext(TimelineInOutContext);
- return state;
+ if (!videoConfig) {
+ return {inFrame: null, outFrame: null};
+ }
+
+ return state[videoConfig.id] ?? {inFrame: null, outFrame: null};
};
export const useTimelineSetInOutFramePosition =
diff --git a/packages/cli/src/editor/state/marks.ts b/packages/cli/src/editor/state/marks.ts
index eeacdce3d7..2de694dc60 100644
--- a/packages/cli/src/editor/state/marks.ts
+++ b/packages/cli/src/editor/state/marks.ts
@@ -1,29 +1,16 @@
-const localStorageKey = (comp: string, durationInFrames: number) =>
- `remotion.editor.marks.${comp}.${durationInFrames}`;
+import type {TimelineInOutContextValue} from './in-out';
-export const persistMarks = (
- comp: string,
- durationInFrames: number,
- marks: [number | null, number | null]
-) => {
- localStorage.setItem(
- localStorageKey(comp, durationInFrames),
- JSON.stringify(marks)
- );
-};
+const localStorageKey = () => `remotion.editor.marksv2`;
-export const loadMarks = (
- comp: string,
- durationInFrames: number | undefined
-): [number | null, number | null] => {
- if (durationInFrames === undefined) {
- return [null, null];
- }
+export const persistMarks = (marks: TimelineInOutContextValue) => {
+ localStorage.setItem(localStorageKey(), JSON.stringify(marks));
+};
- const item = localStorage.getItem(localStorageKey(comp, durationInFrames));
+export const loadMarks = (): TimelineInOutContextValue => {
+ const item = localStorage.getItem(localStorageKey());
if (item === null) {
- return [null, null];
+ return {};
}
return JSON.parse(item);