Skip to content

🎥 Make videos programmatically with React

Add Split clip to sequence context menus

Jonny Burger2b39e80d27ed
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mts
index a503312e23..a5f7e279b4 100644
--- a/packages/example/e2e/studio.test.mts
+++ b/packages/example/e2e/studio.test.mts
@@ -2009,6 +2009,101 @@ test.describe('visual mode', () => {
).toBeVisible();
});
+ test('should split the right-clicked clip at the playhead from either timeline surface', async ({
+ page,
+ }) => {
+ const sourceBefore = fs.readFileSync(sequenceShiftFile, 'utf-8');
+ try {
+ fs.writeFileSync(
+ sequenceShiftFile,
+ `import {Sequence} from 'remotion';
+export const SequenceShiftRepro = () => {
+ return (
+ <>
+ <Sequence name="Split first" from={10} durationInFrames={50}>
+ <div />
+ </Sequence>
+ <Sequence name="Split second" from={10} durationInFrames={50}>
+ <div />
+ </Sequence>
+ </>
+ );
+};`,
+ );
+ await page.goto(`${STUDIO_URL}/sequence-shift-repro`);
+ const first = page.getByText('Split first', {exact: true});
+ const second = page.getByText('Split second', {exact: true});
+ await expect(first).toBeVisible({timeout: 30_000});
+ const menu = page.locator('[data-remotion-menu-tree-id]').last();
+ const split = menu.getByRole('button', {name: 'Split clip', exact: true});
+ await page.keyboard.press('g');
+ await page.locator('input:focus').fill('10');
+ await page.locator('input:focus').press('Enter');
+ await expect(async () => {
+ await page.keyboard.press('Escape');
+ await first.click({button: 'right'});
+ await expect(split.locator('span[title]')).toHaveAttribute(
+ 'title',
+ 'Cannot split at the sequence start',
+ {timeout: 500},
+ );
+ }).toPass({timeout: 15_000});
+ await split.click();
+ await expect(split).toBeVisible();
+ await page.keyboard.press('Escape');
+ await page.keyboard.press('g');
+ await page.locator('input:focus').fill('30');
+ await page.locator('input:focus').press('Enter');
+ await first.click();
+ await second.click({modifiers: ['Meta']});
+ await second.click({button: 'right'});
+ await expect(
+ menu.getByRole('button', {name: 'Duplicate selected', exact: true}),
+ ).toBeVisible();
+ await expect(split).toHaveCount(0);
+ await page.keyboard.press('Escape');
+ await first.click();
+ // Right-click an unselected label: split that clip, not the old selection.
+ await second.click({button: 'right'});
+ await expect(split.locator('span[title]')).toHaveAttribute(
+ 'title',
+ 'Split at the playhead',
+ );
+ await split.click();
+ await expect
+ .poll(
+ () =>
+ fs
+ .readFileSync(sequenceShiftFile, 'utf-8')
+ .match(/name="Split second"/g)?.length,
+ )
+ .toBe(2);
+ await expect(second).toHaveCount(2);
+ // The clip bar exposes the same action and timing context.
+ await page
+ .locator('[data-timeline-marquee-item][title="Split first"]')
+ .click({button: 'right'});
+ await expect(split.locator('span[title]')).toHaveAttribute(
+ 'title',
+ 'Split at the playhead',
+ );
+ await split.click();
+ await expect
+ .poll(
+ () =>
+ fs
+ .readFileSync(sequenceShiftFile, 'utf-8')
+ .match(/name="Split first"/g)?.length,
+ )
+ .toBe(2);
+ const source = fs.readFileSync(sequenceShiftFile, 'utf-8');
+ expect(source.match(/durationInFrames=\{20\}/g)).toHaveLength(2);
+ expect(source.match(/trimBefore=\{20\}/g)).toHaveLength(2);
+ } finally {
+ fs.writeFileSync(sequenceShiftFile, sourceBefore);
+ }
+ });
+
test('should apply a timeline context menu action to multiple selected sequences', async ({
page,
}) => {
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index e6ec3eb271..38c06b1e67 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -44,6 +44,7 @@ import {
getMultiSequenceContextMenuItems,
getSequenceContextMenuItems,
} from './get-sequence-context-menu-items';
+import {getSequenceSplitMenuItem} from './get-sequence-split-menu-item';
import {getTimelineSequenceVisibleLayout} from './get-timeline-sequence-visible-layout';
import {getCurrentFrame} from './imperative-state';
import {LoopedTimelineIndicator} from './LoopedTimelineIndicators';
@@ -652,6 +653,15 @@ const TimelineSequenceInner: React.FC<{
});
}
+ const splitMenuItem = getSequenceSplitMenuItem({
+ nodePathInfo,
+ sequence: s,
+ propStatuses: propStatusesForOverride,
+ splitFrame: getCurrentFrame(),
+ canEditSource: previewInteractive && Boolean(validatedLocation?.source),
+ hasMultipleSelection: selected && selectedItems.length > 1,
+ });
+
const freezeFrameMenuItem = getSequenceFreezeFrameMenuItem({
clientId:
previewInteractive && previewServerState.type === 'connected'
@@ -694,10 +704,12 @@ const TimelineSequenceInner: React.FC<{
originalLocation,
selectAsset,
sequence: s,
- sourceActions:
- isStudioInteractivityEnabled() && freezeFrameMenuItem
- ? [freezeFrameMenuItem]
- : [],
+ sourceActions: isStudioInteractivityEnabled()
+ ? [
+ ...(splitMenuItem ? [splitMenuItem] : []),
+ ...(freezeFrameMenuItem ? [freezeFrameMenuItem] : []),
+ ]
+ : [],
});
}, [
canOpenInEditor,
@@ -710,6 +722,7 @@ const TimelineSequenceInner: React.FC<{
isProgrammaticallyDuplicated,
mediaSrc,
nodePath,
+ nodePathInfo,
onSelect,
onDeleteSequenceFromSource,
onDeleteSelectedSequences,
@@ -726,6 +739,7 @@ const TimelineSequenceInner: React.FC<{
selectAsset,
selectable,
selected,
+ selectedItems.length,
selectedSequenceNodePathInfos,
sequenceFrameOffset,
setPropStatuses,
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index b99077a047..4822b45d40 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -67,6 +67,7 @@ import {
getMultiSequenceContextMenuItems,
getSequenceContextMenuItems,
} from './get-sequence-context-menu-items';
+import {getSequenceSplitMenuItem} from './get-sequence-split-menu-item';
import {getCurrentFrame} from './imperative-state';
import {saveSequenceProps} from './save-sequence-prop';
import {getTimelineAssetLinkInfo} from './timeline-asset-link';
@@ -1007,6 +1008,15 @@ const TimelineSequenceItemInner: React.FC<{
});
}
+ const splitMenuItem = getSequenceSplitMenuItem({
+ nodePathInfo,
+ sequence,
+ propStatuses: propStatusesForOverride,
+ splitFrame: getCurrentFrame(),
+ canEditSource: previewInteractive && Boolean(validatedLocation?.source),
+ hasMultipleSelection: selected && selectedItems.length > 1,
+ });
+
const freezeFrameMenuItem = getSequenceFreezeFrameMenuItem({
clientId:
previewInteractive && previewServerState.type === 'connected'
@@ -1123,6 +1133,7 @@ const TimelineSequenceItemInner: React.FC<{
subMenu: null,
value: 'rename-sequence',
},
+ ...(splitMenuItem ? [splitMenuItem] : []),
...(freezeFrameMenuItem ? [freezeFrameMenuItem] : []),
]
: [],
@@ -1142,7 +1153,7 @@ const TimelineSequenceItemInner: React.FC<{
isProgrammaticallyDuplicated,
mediaSrc,
nodePath,
- nodePathInfo?.supportsEffects,
+ nodePathInfo,
onAddEffect,
onCrop,
onRotate,
@@ -1162,6 +1173,7 @@ const TimelineSequenceItemInner: React.FC<{
selectAsset,
selectable,
selected,
+ selectedItems.length,
selectedSequenceNodePathInfos,
sequence,
sequenceFrameOffset,
diff --git a/packages/studio/src/components/Timeline/get-sequence-split-menu-item.tsx b/packages/studio/src/components/Timeline/get-sequence-split-menu-item.tsx
new file mode 100644
index 0000000000..8ed7df1ac1
--- /dev/null
+++ b/packages/studio/src/components/Timeline/get-sequence-split-menu-item.tsx
@@ -0,0 +1,73 @@
+import type {CanUpdateSequencePropStatus, TSequence} from 'remotion';
+import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
+import type {ComboboxValue} from '../NewComposition/ComboBox';
+import {
+ getTimelineSequenceSplitEligibility,
+ splitTimelineSequenceFromSource,
+} from './split-selected-timeline-item';
+
+export const getSequenceSplitMenuItem = ({
+ nodePathInfo,
+ sequence,
+ propStatuses,
+ splitFrame,
+ canEditSource,
+ hasMultipleSelection,
+}: {
+ readonly nodePathInfo: SequenceNodePathInfo | null;
+ readonly sequence: TSequence;
+ readonly propStatuses:
+ | Record<string, CanUpdateSequencePropStatus>
+ | undefined;
+ readonly splitFrame: number;
+ readonly canEditSource: boolean;
+ readonly hasMultipleSelection: boolean;
+}): ComboboxValue | null => {
+ if (hasMultipleSelection) {
+ return null;
+ }
+
+ const eligibility = nodePathInfo
+ ? getTimelineSequenceSplitEligibility({
+ selection: {type: 'sequence', nodePathInfo},
+ sequence,
+ propStatuses,
+ splitFrame,
+ })
+ : {
+ canSplit: false as const,
+ reason: 'Sequence has no editable source node',
+ };
+ const disabledReason = !canEditSource
+ ? 'Sequence source is unavailable'
+ : propStatuses === undefined
+ ? 'Waiting for sequence prop status'
+ : eligibility.canSplit
+ ? null
+ : eligibility.reason;
+
+ return {
+ type: 'item',
+ id: 'split-sequence',
+ label: (
+ <span title={disabledReason ?? 'Split at the playhead'}>Split clip</span>
+ ),
+ value: 'split-sequence',
+ keyHint: null,
+ leftItem: null,
+ quickSwitcherLabel: null,
+ subMenu: null,
+ disabled: disabledReason !== null,
+ onClick: () => {
+ if (disabledReason !== null || !eligibility.canSplit) {
+ return;
+ }
+
+ // Keep the frame used to enable the menu item, even during playback.
+ splitTimelineSequenceFromSource({
+ nodePathInfo: eligibility.nodePathInfo,
+ splitFrame,
+ }).catch(() => undefined);
+ },
+ };
+};