🎥 Make videos programmatically with React
Add wrapper timing regression coverage
JonnyBurger4b7b0f1c13cd
diff --git a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsxindex adc0206d96..dd2d8c5db4 100644--- a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx+++ b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx@@ -13,7 +13,6 @@ import type {TimelineTrackData,} from '../../helpers/get-timeline-sequence-sort-key';import {installRequiredPackages} from '../../helpers/install-required-package';-import {timelineSequenceNodePathToKey} from '../../helpers/timeline-node-path-key';import {HtmlInCanvasIcon} from '../../icons/html-in-canvas';import {MotionBlurIcon} from '../../icons/motion-blur';import {SetSelectedModalContext} from '../../state/modals';@@ -24,6 +23,7 @@ import {largeInspectorActionIconContainerStyle,largeInspectorActionIconStyle,} from './common';+import {getHtmlInCanvasWrapperTiming} from './get-html-in-canvas-wrapper-timing';type HtmlInCanvasWrapper = Extract<NodeWrapper,@@ -56,80 +56,22 @@ export const SequenceWrapAction: React.FC<{const eligibilityKey = JSON.stringify([nodePathKey, sequenceStack]);const [busy, setBusy] = useState(false);const [eligibleNodeKey, setEligibleNodeKey] = useState<string | null>(null);- const timing = useMemo(() => {- const selectedNodePathKey = timelineSequenceNodePathToKey(+ const timing = useMemo(+ () =>+ getHtmlInCanvasWrapperTiming({+ tracks: calculateTimeline({+ sequences,+ overrideIdsToNodePaths: overrideIdToNodePathMappings,+ }),+ sequenceSubscriptionKey: nodePathInfo.sequenceSubscriptionKey,+ }),+ [nodePathInfo.sequenceSubscriptionKey,- );- const matchingTracks = calculateTimeline({+ overrideIdToNodePathMappings,sequences,- overrideIdsToNodePaths: overrideIdToNodePathMappings,- }).filter(- (candidate) =>- candidate.nodePathInfo !== null &&- timelineSequenceNodePathToKey(- candidate.nodePathInfo.sequenceSubscriptionKey,- ) === selectedNodePathKey,- );- const timings = matchingTracks.map((candidate) => {- const from = Math.max(- 0,- candidate.localStart +- (candidate.sequence.from - candidate.cascadedStart) *- candidate.keyframePlaybackRate,- );- return {- from,- durationInFrames:- candidate.sequence.duration * candidate.keyframePlaybackRate,- trimBefore: from,- };- });- const firstTiming = timings[0];- if (- !firstTiming ||- !Number.isFinite(firstTiming.from) ||- !Number.isFinite(firstTiming.durationInFrames) ||- firstTiming.durationInFrames <= 0- ) {- return null;- }-- for (const candidate of timings.slice(1)) {- if (- !Number.isFinite(candidate.from) ||- !Number.isFinite(candidate.durationInFrames) ||- candidate.durationInFrames <= 0- ) {- return null;- }-- const fromTolerance =- Number.EPSILON *- Math.max(1, Math.abs(firstTiming.from), Math.abs(candidate.from)) *- 16;- const durationTolerance =- Number.EPSILON *- Math.max(- 1,- Math.abs(firstTiming.durationInFrames),- Math.abs(candidate.durationInFrames),- ) *- 16;- if (- Math.abs(firstTiming.from - candidate.from) > fromTolerance ||- Math.abs(firstTiming.durationInFrames - candidate.durationInFrames) >- durationTolerance- ) {- return null;- }- }-- return firstTiming;- }, [- nodePathInfo.sequenceSubscriptionKey,- overrideIdToNodePathMappings,- sequences,- ]);+ ],+ );+ const timingIsNull = timing === null;const wouldNestAtRuntime = useMemo(() => {const sequencesById = new Map(sequences.map((registeredSequence) => [@@ -179,7 +121,7 @@ export const SequenceWrapAction: React.FC<{}, [sequence.id, sequences]);useEffect(() => {- if (sourceActionsDisabled || wouldNestAtRuntime || timing === null) {+ if (sourceActionsDisabled || wouldNestAtRuntime || timingIsNull) {setEligibleNodeKey(null);return;}@@ -219,7 +161,7 @@ export const SequenceWrapAction: React.FC<{eligibilityKey,nodePathKey,sourceActionsDisabled,- timing,+ timingIsNull,wouldNestAtRuntime,]);diff --git a/packages/studio/src/components/InspectorPanel/get-html-in-canvas-wrapper-timing.ts b/packages/studio/src/components/InspectorPanel/get-html-in-canvas-wrapper-timing.tsnew file mode 100644index 0000000000..d6357f61ba--- /dev/null+++ b/packages/studio/src/components/InspectorPanel/get-html-in-canvas-wrapper-timing.ts@@ -0,0 +1,84 @@+import type {SequencePropsSubscriptionKey} from 'remotion';+import type {TimelineTrackData} from '../../helpers/get-timeline-sequence-sort-key';+import {timelineSequenceNodePathToKey} from '../../helpers/timeline-node-path-key';++export type HtmlInCanvasWrapperTiming = {+ readonly from: number;+ readonly durationInFrames: number;+ readonly trimBefore: number;+};++export const getHtmlInCanvasWrapperTiming = ({+ tracks,+ sequenceSubscriptionKey,+}: {+ readonly tracks: readonly TimelineTrackData[];+ readonly sequenceSubscriptionKey: SequencePropsSubscriptionKey;+}): HtmlInCanvasWrapperTiming | null => {+ const selectedNodePathKey = timelineSequenceNodePathToKey(+ sequenceSubscriptionKey,+ );+ const timings = tracks+ .filter(+ (candidate) =>+ candidate.nodePathInfo !== null &&+ timelineSequenceNodePathToKey(+ candidate.nodePathInfo.sequenceSubscriptionKey,+ ) === selectedNodePathKey,+ )+ .map((candidate) => {+ const from = Math.max(+ 0,+ candidate.localStart ++ (candidate.sequence.from - candidate.cascadedStart) *+ candidate.keyframePlaybackRate,+ );+ return {+ from,+ durationInFrames:+ candidate.sequence.duration * candidate.keyframePlaybackRate,+ trimBefore: from,+ };+ });+ const firstTiming = timings[0];+ if (+ !firstTiming ||+ !Number.isFinite(firstTiming.from) ||+ !Number.isFinite(firstTiming.durationInFrames) ||+ firstTiming.durationInFrames <= 0+ ) {+ return null;+ }++ for (const candidate of timings.slice(1)) {+ if (+ !Number.isFinite(candidate.from) ||+ !Number.isFinite(candidate.durationInFrames) ||+ candidate.durationInFrames <= 0+ ) {+ return null;+ }++ const fromTolerance =+ Number.EPSILON *+ Math.max(1, Math.abs(firstTiming.from), Math.abs(candidate.from)) *+ 16;+ const durationTolerance =+ Number.EPSILON *+ Math.max(+ 1,+ Math.abs(firstTiming.durationInFrames),+ Math.abs(candidate.durationInFrames),+ ) *+ 16;+ if (+ Math.abs(firstTiming.from - candidate.from) > fromTolerance ||+ Math.abs(firstTiming.durationInFrames - candidate.durationInFrames) >+ durationTolerance+ ) {+ return null;+ }+ }++ return firstTiming;+};diff --git a/packages/studio/src/test/html-in-canvas-wrapper-timing.test.ts b/packages/studio/src/test/html-in-canvas-wrapper-timing.test.tsnew file mode 100644index 0000000000..b5d253c207--- /dev/null+++ b/packages/studio/src/test/html-in-canvas-wrapper-timing.test.ts@@ -0,0 +1,121 @@+import {expect, test} from 'bun:test';+import type {SequencePropsSubscriptionKey, TSequence} from 'remotion';+import {getHtmlInCanvasWrapperTiming} from '../components/InspectorPanel/get-html-in-canvas-wrapper-timing';+import {calculateTimeline} from '../helpers/calculate-timeline';+import {makeRuntimeValueStore} from './make-runtime-value-store';++const nodePath: SequencePropsSubscriptionKey = {+ absolutePath: '/src/Composition.tsx',+ nodePath: ['selected'],+ sequenceKeys: [],+ effectKeys: [],+ videoConfigValues: null,+};++const makeSequence = ({+ id,+ parent,+ from,+ duration = 100,+ trimBefore = null,+ sequencePlaybackRate = 1,+ selected = false,+}: {+ readonly id: string;+ readonly parent: string | null;+ readonly from: number;+ readonly duration?: number;+ readonly trimBefore?: number | null;+ readonly sequencePlaybackRate?: number;+ readonly selected?: boolean;+}): TSequence => ({+ controls: selected+ ? {+ schema: {},+ runtimeValues: makeRuntimeValueStore({}),+ videoConfigValues: null,+ overrideId: 'selected',+ supportsEffects: false,+ componentIdentity: null,+ componentName: '<Sequence>',+ }+ : null,+ displayName: id,+ documentationLink: null,+ duration,+ effects: [],+ effectRuntimeValues: null,+ from,+ sequencePlaybackRate,+ frozenFrame: null,+ getStack: () => null,+ id,+ isInsideSeries: false,+ loopDisplay: undefined,+ parent,+ postmountDisplay: null,+ premountDisplay: null,+ refForOutline: null,+ showInTimeline: true,+ timelineOrder: null,+ trimBefore,+ type: 'sequence',+});++test('converts nested playback and trim into the parent clock', () => {+ const nestedTracks = calculateTimeline({+ sequences: [+ makeSequence({+ id: 'outer',+ parent: null,+ from: 10,+ trimBefore: 6,+ sequencePlaybackRate: 1.5,+ }),+ makeSequence({+ id: 'selected',+ parent: 'outer',+ from: 30,+ duration: 60,+ trimBefore: 8,+ sequencePlaybackRate: 2,+ selected: true,+ }),+ ],+ overrideIdsToNodePaths: {selected: nodePath},+ });++ expect(+ getHtmlInCanvasWrapperTiming({+ tracks: nestedTracks,+ sequenceSubscriptionKey: nodePath,+ }),+ ).toEqual({from: 30, durationInFrames: 60, trimBefore: 30});+});++test('clips wrapper timing to the parent visible range', () => {+ const parentClippedTracks = calculateTimeline({+ sequences: [+ makeSequence({+ id: 'parent',+ parent: null,+ from: 20,+ duration: 40,+ }),+ makeSequence({+ id: 'selected',+ parent: 'parent',+ from: -10,+ selected: true,+ }),+ ],+ overrideIdsToNodePaths: {selected: nodePath},+ });++ expect(+ getHtmlInCanvasWrapperTiming({+ tracks: parentClippedTracks,+ sequenceSubscriptionKey: nodePath,+ }),+ ).toEqual({from: 0, durationInFrames: 40, trimBefore: 0});+});