🎥 Make videos programmatically with React
@remotion/studio: Refactor timeline deletion (#7869)
Jonny Burger47dcc61a8d3b
diff --git a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts b/packages/studio/src/components/Timeline/delete-selected-timeline-item.tsindex 7f5d3d5e18..0d02d50374 100644--- a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts+++ b/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts@@ -136,6 +136,35 @@ const isSequenceRowSelection = (type: 'sequence';} => selection.type === 'sequence';+const isSequenceEffectSelection = (+ selection: TimelineSelection,+): selection is TimelineSelection & {+ type: 'sequence-effect';+} => selection.type === 'sequence-effect';++const isKeyframeSelection = (+ selection: TimelineSelection,+): selection is TimelineSelection & {+ type: 'keyframe';+} => selection.type === 'keyframe';++const assertTimelineSelectionsHaveSameType = (+ selections: readonly TimelineSelection[],+): void => {+ const firstSelection = selections[0];+ if (!firstSelection) {+ return;+ }++ for (const selection of selections) {+ if (selection.type !== firstSelection.type) {+ throw new Error(+ `Assertion failed: Cannot delete timeline selections of different types (${firstSelection.type}, ${selection.type})`,+ );+ }+ }+};+export const deleteSelectedTimelineItems = ({selections,sequences,@@ -149,31 +178,57 @@ export const deleteSelectedTimelineItems = ({setCodeValues: SetCodeValues;clientId: string;}): Promise<void> | null => {- const sequenceSelections = selections.filter(isSequenceRowSelection);- const deletePromises = selections- .filter((selection) => !isSequenceRowSelection(selection))- .map((selection) =>- deleteSelectedTimelineItem({- selection,- sequences,- overrideIdsToNodePaths,- setCodeValues,- clientId,- }),- )- .filter((promise): promise is Promise<void> => promise !== null);-- if (sequenceSelections.length > 0) {- deletePromises.push(- deleteSequences(- sequenceSelections.map((selection) => selection.nodePathInfo),- ),- );- }-- if (deletePromises.length === 0) {+ const firstSelection = selections[0];+ if (!firstSelection) {return null;}- return Promise.all(deletePromises).then(() => undefined);+ assertTimelineSelectionsHaveSameType(selections);++ switch (firstSelection.type) {+ case 'sequence':+ return deleteSequences(+ selections+ .filter(isSequenceRowSelection)+ .map((selection) => selection.nodePathInfo),+ );+ case 'sequence-effect':+ return Promise.all(+ selections+ .filter(isSequenceEffectSelection)+ .map((selection) =>+ deleteEffect(selection.nodePathInfo, selection.i),+ ),+ ).then(() => undefined);+ case 'keyframe': {+ const deletePromises = selections+ .filter(isKeyframeSelection)+ .map((selection) =>+ deleteSelectedKeyframe({+ nodePathInfo: selection.nodePathInfo,+ frame: selection.frame,+ sequences,+ overrideIdsToNodePaths,+ setCodeValues,+ clientId,+ }),+ )+ .filter((promise): promise is Promise<void> => promise !== null);++ if (deletePromises.length === 0) {+ return null;+ }++ return Promise.all(deletePromises).then(() => undefined);+ }++ case 'sequence-prop':+ case 'sequence-all-effects':+ case 'sequence-effect-prop':+ return null;+ default:+ throw new Error(+ `Unexpected timeline selection type: ${firstSelection satisfies never}`,+ );+ }};diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex 7b910eeca0..166cd04e57 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -1,5 +1,6 @@import {expect, test} from 'bun:test';import type {SequenceNodePath, SequencePropsSubscriptionKey} from 'remotion';+import {deleteSelectedTimelineItems} from '../components/Timeline/delete-selected-timeline-item';import {isDuplicatableSequenceRowSelection} from '../components/Timeline/duplicate-selected-timeline-item';import {ENABLE_OUTLINES,@@ -86,6 +87,28 @@ test('Cmd+D only duplicates selected timeline sequence rows', () => {]);});+test('Deleting mixed timeline selection types throws an assertion error', () => {+ const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);+ const effectNodePathInfo = makeNodePathInfo(['body', 1], ['effects', '0']);++ expect(() =>+ deleteSelectedTimelineItems({+ selections: [+ {type: 'sequence', nodePathInfo: sequenceNodePathInfo},+ {+ type: 'sequence-effect',+ nodePathInfo: effectNodePathInfo,+ i: 0,+ },+ ],+ sequences: [],+ overrideIdsToNodePaths: {},+ setCodeValues: () => undefined,+ clientId: 'client',+ }),+ ).toThrow(/Assertion failed/);+});+test('Child timeline selections resolve to the parent sequence selection key', () => {const sequenceNodePathInfo = makeNodePathInfo(['body', 0], []);const effectNodePathInfo = makeNodePathInfo(['body', 0], ['effects', '0']);