Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Improve selected outline controls (#8404)

Jonny Burgerc7b7c9c7ef00
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index e48088b224..d04d85aa8f 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import React, {
forwardRef,
+ useCallback,
useContext,
useEffect,
useMemo,
@@ -133,7 +134,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalPremountDisplay: premountDisplay,
_remotionInternalPostmountDisplay: postmountDisplay,
_remotionInternalIsMedia: isMedia,
- _remotionInternalRefForOutline: refForOutline,
+ _remotionInternalRefForOutline: passedRefForOutline,
...other
},
ref,
@@ -219,6 +220,11 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
Math.min(videoConfig.durationInFrames - from, parentSequenceDuration),
);
const {registerSequence, unregisterSequence} = useContext(SequenceManager);
+ const wrapperRefForOutline = useRef<HTMLDivElement | null>(null);
+ const refForOutline =
+ other.layout === 'none'
+ ? (passedRefForOutline ?? null)
+ : (passedRefForOutline ?? wrapperRefForOutline);
const premounting = useMemo(() => {
// || is intentional, ?? would not trigger on `false`
@@ -435,6 +441,19 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
const styleIfThere = other.layout === 'none' ? undefined : other.style;
+ const sequenceRef = useCallback(
+ (node: HTMLDivElement | null) => {
+ wrapperRefForOutline.current = node;
+
+ if (typeof ref === 'function') {
+ ref(node);
+ } else if (ref) {
+ ref.current = node;
+ }
+ },
+ [ref],
+ );
+
const defaultStyle: React.CSSProperties = useMemo(() => {
return {
flexDirection: undefined,
@@ -460,7 +479,7 @@ const RegularSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
frozenContent
) : (
<AbsoluteFill
- ref={ref}
+ ref={sequenceRef}
style={defaultStyle}
className={other.className}
>
diff --git a/packages/core/src/test/sequence-register-once.test.tsx b/packages/core/src/test/sequence-register-once.test.tsx
index a6655e5e34..24473c04ef 100644
--- a/packages/core/src/test/sequence-register-once.test.tsx
+++ b/packages/core/src/test/sequence-register-once.test.tsx
@@ -140,6 +140,24 @@ test('Sequence registers its documentation link', () => {
);
});
+test('Sequence registers its wrapper element for Studio outlines', () => {
+ const registeredSequences: TSequence[] = [];
+ const ref = React.createRef<HTMLDivElement>();
+
+ render(
+ <SequenceTestWrapper
+ onRegisterSequence={(sequence) => {
+ registeredSequences.push(sequence);
+ }}
+ >
+ <Sequence ref={ref}>hi</Sequence>
+ </SequenceTestWrapper>,
+ );
+
+ expect(registeredSequences[0]?.refForOutline?.current?.tagName).toBe('DIV');
+ expect(registeredSequences[0]?.refForOutline?.current).toBe(ref.current);
+});
+
test('Series.Sequence registers without visual controls', () => {
const registeredSequences: TSequence[] = [];
diff --git a/packages/studio/src/components/selected-outline-drag.ts b/packages/studio/src/components/selected-outline-drag.ts
index 7fb31d1493..bcbb2d0020 100644
--- a/packages/studio/src/components/selected-outline-drag.ts
+++ b/packages/studio/src/components/selected-outline-drag.ts
@@ -260,6 +260,26 @@ type SelectedOutlineScaleEdgeInfo = {
readonly start: OutlinePoint;
};
+const getScaleCursor = (normal: OutlinePoint): string => {
+ const degrees = Math.atan2(normal.y, normal.x) * (180 / Math.PI);
+ const normalizedDegrees = ((degrees % 180) + 180) % 180;
+ const snappedDegrees = Math.round(normalizedDegrees / 45) * 45;
+
+ if (snappedDegrees === 0 || snappedDegrees === 180) {
+ return 'ew-resize';
+ }
+
+ if (snappedDegrees === 45) {
+ return 'nwse-resize';
+ }
+
+ if (snappedDegrees === 90) {
+ return 'ns-resize';
+ }
+
+ return 'nesw-resize';
+};
+
export const getSelectedOutlineScaleEdgeInfo = (
points: SelectedOutline['points'],
edge: SelectedOutlineScaleEdge,
@@ -285,7 +305,7 @@ export const getSelectedOutlineScaleEdgeInfo = (
return {
axis: edge === 'left' || edge === 'right' ? 'x' : 'y',
- cursor: edge === 'left' || edge === 'right' ? 'ew-resize' : 'ns-resize',
+ cursor: getScaleCursor(outward),
end: edgePoints.end,
extent: length,
normal: {x: outward.x / length, y: outward.y / length},
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index d84916fc12..73fb7ed64c 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -2643,13 +2643,43 @@ test('Selected outline scale edges project pointer movement onto the edge normal
const top = getSelectedOutlineScaleEdgeInfo(points, 'top');
expect(right?.axis).toBe('x');
+ expect(right?.cursor).toBe('ew-resize');
expect(right?.extent).toBe(100);
expect(right?.normal).toEqual({x: 1, y: 0});
expect(top?.axis).toBe('y');
+ expect(top?.cursor).toBe('ns-resize');
expect(top?.extent).toBe(50);
expect(top?.normal).toEqual({x: 0, y: -1});
});
+test('Selected outline scale edge cursors follow rotated outlines', () => {
+ const rotated90Degrees = [
+ {x: 0, y: 0},
+ {x: 0, y: 100},
+ {x: -50, y: 100},
+ {x: -50, y: 0},
+ ] as const;
+ const rotated45Degrees = [
+ {x: 0, y: 0},
+ {x: 100, y: 100},
+ {x: 50, y: 150},
+ {x: -50, y: 50},
+ ] as const;
+
+ expect(
+ getSelectedOutlineScaleEdgeInfo(rotated90Degrees, 'right')?.cursor,
+ ).toBe('ns-resize');
+ expect(getSelectedOutlineScaleEdgeInfo(rotated90Degrees, 'top')?.cursor).toBe(
+ 'ew-resize',
+ );
+ expect(
+ getSelectedOutlineScaleEdgeInfo(rotated45Degrees, 'right')?.cursor,
+ ).toBe('nwse-resize');
+ expect(getSelectedOutlineScaleEdgeInfo(rotated45Degrees, 'top')?.cursor).toBe(
+ 'nesw-resize',
+ );
+});
+
test('Backspace reset targets selected effect props', () => {
const schema = {} satisfies SequenceSchema;
const effectSchema = {