Skip to content

🎥 Make videos programmatically with React

nice sliders

Jonny Burger5348d5188bca
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index 4bbefc43c7..dd84e58afc 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -85,8 +85,12 @@ export const Editor: React.FC = () => {
},
[]
);
- const [inFrame, setInFrame] = useState<number | null>(null);
- const [outFrame, setOutFrame] = useState<number | null>(null);
+ const [inAndOutFrames, setInAndOutFrames] = useState<
+ TimelineInOutContextValue
+ >({
+ inFrame: null,
+ outFrame: null,
+ });
const [mediaMuted, setMediaMuted] = useState<boolean>(false);
const [mediaVolume, setMediaVolume] = useState<number>(1);
const [modalContextType, setModalContextType] = useState<ModalState | null>(
@@ -113,18 +117,14 @@ export const Editor: React.FC = () => {
}, [richTimeline, setRichTimeline]);
const timelineInOutContextValue = useMemo((): TimelineInOutContextValue => {
- return {
- inFrame,
- outFrame,
- };
- }, [inFrame, outFrame]);
+ return inAndOutFrames;
+ }, [inAndOutFrames]);
const setTimelineInOutContextValue = useMemo((): SetTimelineInOutContextValue => {
return {
- setInFrame,
- setOutFrame,
+ setInAndOutFrames,
};
- }, [setInFrame, setOutFrame]);
+ }, []);
const mediaVolumeContextValue = useMemo((): MediaVolumeContextValue => {
return {
diff --git a/packages/cli/src/editor/components/Timeline/Timeline.tsx b/packages/cli/src/editor/components/Timeline/Timeline.tsx
index cd54c995af..f47ff8e56a 100644
--- a/packages/cli/src/editor/components/Timeline/Timeline.tsx
+++ b/packages/cli/src/editor/components/Timeline/Timeline.tsx
@@ -113,9 +113,9 @@ export const Timeline: React.FC = () => {
fps={videoConfig.fps}
hasBeenCut={withoutHidden.length > shown.length}
/>
- <TimelineSlider />
<TimelineInOutPointer />
<TimelineDragHandler />
+ <TimelineSlider />
</SplitterElement>
</SplitterContainer>
</div>
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
index d8b5de304f..ed1c098e75 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
@@ -70,8 +70,7 @@ export const TimelineDragHandler: React.FC = () => {
} = Internals.Timeline.useTimelineInOutFramePosition();
const {
- setInFrame,
- setOutFrame,
+ setInAndOutFrames,
} = Internals.Timeline.useTimelineSetInOutFramePosition();
const {get} = useGetXPositionOfItemInTimeline();
@@ -277,18 +276,30 @@ export const TimelineDragHandler: React.FC = () => {
);
if (inOutDragging.dragging === 'in') {
if (frame < 1) {
- return setInFrame(null);
+ return setInAndOutFrames((prev) => ({
+ ...prev,
+ inFrame: null,
+ }));
}
const maxFrame = outFrame === null ? Infinity : outFrame - 1;
- setInFrame(Math.min(maxFrame, frame));
+ setInAndOutFrames((prev) => ({
+ ...prev,
+ inFrame: Math.min(maxFrame, frame),
+ }));
} else {
if (frame > videoConfig.durationInFrames - 2) {
- return setOutFrame(null);
+ return setInAndOutFrames((prev) => ({
+ ...prev,
+ outFrame: null,
+ }));
}
const minFrame = inFrame === null ? -Infinity : inFrame + 1;
- setOutFrame(Math.max(minFrame, frame));
+ setInAndOutFrames((prev) => ({
+ ...prev,
+ outFrame: Math.max(minFrame, frame),
+ }));
}
},
[
@@ -296,8 +307,7 @@ export const TimelineDragHandler: React.FC = () => {
inOutDragging.dragging,
left,
outFrame,
- setInFrame,
- setOutFrame,
+ setInAndOutFrames,
videoConfig,
width,
]
diff --git a/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx
index fac648ae0a..b158ab9ddf 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx
@@ -5,7 +5,7 @@ import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timelin
const areaHighlight: React.CSSProperties = {
position: 'absolute',
- backgroundColor: 'rgba(0, 0, 0, 0.3)',
+ backgroundColor: 'rgba(0, 0, 0, 0.5)',
height: '100%',
bottom: 0,
top: 0,
@@ -21,8 +21,7 @@ export const TimelineInOutPointer: React.FC = () => {
outFrame,
} = Internals.Timeline.useTimelineInOutFramePosition();
const {
- setInFrame,
- setOutFrame,
+ setInAndOutFrames,
} = Internals.Timeline.useTimelineSetInOutFramePosition();
const videoConfig = Internals.useUnsafeVideoConfig();
const {playing} = PlayerInternals.usePlayer();
@@ -31,12 +30,18 @@ export const TimelineInOutPointer: React.FC = () => {
useEffect(() => {
if (playing) {
if (inFrame && timelinePosition < inFrame) {
- setInFrame(inFrame);
+ setInAndOutFrames((prev) => ({
+ ...prev,
+ inFrame,
+ }));
} else if (outFrame && timelinePosition > outFrame) {
- setOutFrame(outFrame);
+ setInAndOutFrames((prev) => ({
+ ...prev,
+ outFrame,
+ }));
}
}
- }, [timelinePosition, playing, inFrame, outFrame, setInFrame, setOutFrame]);
+ }, [timelinePosition, playing, inFrame, outFrame, setInAndOutFrames]);
if (!videoConfig) {
return null;
diff --git a/packages/cli/src/editor/components/TimelineInOutToggle.tsx b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
index 85dda405e2..63cc1e17f9 100644
--- a/packages/cli/src/editor/components/TimelineInOutToggle.tsx
+++ b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
@@ -22,77 +22,62 @@ export const TimelineInOutPointToggle: React.FC = () => {
outFrame,
} = Internals.Timeline.useTimelineInOutFramePosition();
const {
- setInFrame,
- setOutFrame,
+ setInAndOutFrames,
} = Internals.Timeline.useTimelineSetInOutFramePosition();
const videoConfig = Internals.useUnsafeVideoConfig();
const keybindings = useKeybinding();
- const getValidFrame = useCallback(
- (type: 'inFrame' | 'outFrame') => {
- let validInFrame = null;
- let validOutFrame = null;
-
- if (!videoConfig) {
- return {validInFrame, validOutFrame};
- }
-
- if (type === 'inFrame') {
- validInFrame = timelinePosition;
- if (outFrame !== null && timelinePosition <= outFrame) {
- validOutFrame =
- timelinePosition === outFrame
- ? timelinePosition + MIN_FRAMES_BETWEEN_POINTS
- : outFrame;
- if (validOutFrame > videoConfig.durationInFrames - 1) {
- validInFrame = timelinePosition - MIN_FRAMES_BETWEEN_POINTS;
- validOutFrame = videoConfig.durationInFrames - 1;
- }
- }
-
- return {validInFrame, validOutFrame};
- }
-
- validOutFrame = timelinePosition;
- if (inFrame !== null && timelinePosition >= inFrame) {
- validInFrame =
- timelinePosition === inFrame
- ? timelinePosition - MIN_FRAMES_BETWEEN_POINTS
- : inFrame;
- if (validInFrame < 0) {
- validInFrame = 0;
- validOutFrame = timelinePosition + MIN_FRAMES_BETWEEN_POINTS;
- }
- }
-
- return {validInFrame, validOutFrame};
- },
- [inFrame, outFrame, timelinePosition, videoConfig]
- );
-
const onInFrameBtnClick = useCallback(() => {
if (!videoConfig) {
return null;
}
- const {validInFrame, validOutFrame} = getValidFrame('inFrame');
+ setInAndOutFrames((prev) => {
+ const biggestPossible =
+ prev.outFrame === null ? Infinity : prev.outFrame - 1;
+ const selected = Math.min(timelinePosition, biggestPossible);
+ if (prev.inFrame !== null) {
+ // Disable if already at this position
+ if (prev.inFrame === selected) {
+ return {
+ ...prev,
+ inFrame: null,
+ };
+ }
+ }
- setInFrame((prev) => {
- return prev ? null : validInFrame;
+ return {
+ ...prev,
+ inFrame: selected,
+ };
});
- if (inFrame) {
- setOutFrame(validOutFrame);
- }
- }, [getValidFrame, inFrame, setInFrame, setOutFrame, videoConfig]);
+ }, [setInAndOutFrames, timelinePosition, videoConfig]);
const onOutFrameBtnClick = useCallback(() => {
if (!videoConfig) {
return null;
}
- const {validOutFrame} = getValidFrame('outFrame');
- setOutFrame((prev) => (prev ? null : validOutFrame));
- }, [getValidFrame, setOutFrame, videoConfig]);
+ setInAndOutFrames((prev) => {
+ const smallestPossible =
+ prev.inFrame === null ? -Infinity : prev.inFrame + 1;
+ const selected = Math.max(timelinePosition, smallestPossible);
+
+ if (prev.outFrame !== null) {
+ if (prev.outFrame === selected) {
+ return {
+ ...prev,
+ outFrame: null,
+ };
+ }
+ }
+
+ return {
+ ...prev,
+ outFrame: selected,
+ };
+ });
+ }, [setInAndOutFrames, timelinePosition, videoConfig]);
useEffect(() => {
const iKey = keybindings.registerKeybinding('keypress', 'i', () => {
diff --git a/packages/core/src/timeline-inout-position-state.ts b/packages/core/src/timeline-inout-position-state.ts
index 6229a0739e..7b3ea4d871 100644
--- a/packages/core/src/timeline-inout-position-state.ts
+++ b/packages/core/src/timeline-inout-position-state.ts
@@ -1,4 +1,4 @@
-import {createContext, useContext} from 'react';
+import React, {createContext, useContext} from 'react';
export type TimelineInOutContextValue = {
inFrame: number | null;
@@ -6,8 +6,9 @@ export type TimelineInOutContextValue = {
};
export type SetTimelineInOutContextValue = {
- setInFrame: (u: React.SetStateAction<number | null>) => void;
- setOutFrame: (u: React.SetStateAction<number | null>) => void;
+ setInAndOutFrames: (
+ u: React.SetStateAction<TimelineInOutContextValue>
+ ) => void;
};
export const TimelineInOutContext = createContext<TimelineInOutContextValue>({
@@ -18,10 +19,7 @@ export const TimelineInOutContext = createContext<TimelineInOutContextValue>({
export const SetTimelineInOutContext = createContext<
SetTimelineInOutContextValue
>({
- setInFrame: () => {
- throw new Error('default');
- },
- setOutFrame: () => {
+ setInAndOutFrames: () => {
throw new Error('default');
},
});
@@ -32,6 +30,6 @@ export const useTimelineInOutFramePosition = (): TimelineInOutContextValue => {
};
export const useTimelineSetInOutFramePosition = (): SetTimelineInOutContextValue => {
- const {setInFrame, setOutFrame} = useContext(SetTimelineInOutContext);
- return {setInFrame, setOutFrame};
+ const {setInAndOutFrames} = useContext(SetTimelineInOutContext);
+ return {setInAndOutFrames};
};