Skip to content

🎥 Make videos programmatically with React

Fix Studio selection flashes when deleting keyframes

Jonny Burger17f4815efc95
diff --git a/packages/example/e2e/effect-keyframes.test.mts b/packages/example/e2e/effect-keyframes.test.mts
index cc9f6cc157..7d310c261c 100644
--- a/packages/example/e2e/effect-keyframes.test.mts
+++ b/packages/example/e2e/effect-keyframes.test.mts
@@ -44,6 +44,129 @@ test.describe('effect keyframes', () => {
await stopStudio();
});
+ test('deletes marquee-selected keyframes without flashing and keeps their property selected', async ({
+ page,
+ }) => {
+ test.setTimeout(120_000);
+ const originalContent = fs.readFileSync(effectKeyframeE2eFile, 'utf-8');
+ fs.writeFileSync(
+ effectKeyframeE2eFile,
+ originalContent.replace(
+ 'opacity: interpolate(frame, [0, 30], [0, 1])',
+ 'opacity: interpolate(frame, [0, 10, 20, 30, 40, 60, 89], [0, 1, 0, 1, 0, 1, 0])',
+ ),
+ );
+ await page.goto(`${STUDIO_URL}/effect-keyframe-e2e`);
+ const opacity = page.getByText('Opacity', {exact: true});
+ await expect(async () => {
+ await page
+ .getByTitle('Timeline expansion', {exact: true})
+ .first()
+ .click();
+ await expect(opacity).toHaveCount(1, {timeout: 1_000});
+ }).toPass({timeout: 30_000});
+ await opacity.click();
+
+ const keyframes = page.getByRole('button', {
+ name: /^Select keyframe at frame /,
+ });
+ await expect(keyframes).toHaveCount(7);
+ await page
+ .getByRole('button', {name: 'Select keyframe at frame 10', exact: true})
+ .scrollIntoViewIfNeeded();
+ const propertyLabel = opacity.last().locator('..');
+ await page
+ .getByRole('button', {name: 'Select keyframe at frame 10', exact: true})
+ .click();
+ await expect(propertyLabel).toHaveCSS(
+ 'background-color',
+ 'rgba(255, 255, 255, 0.1)',
+ );
+ await page.keyboard.press('Escape');
+ await expect(propertyLabel).toHaveCSS(
+ 'background-color',
+ 'rgba(0, 0, 0, 0)',
+ );
+ await page
+ .getByRole('button', {
+ name: 'Select easing from frame 10 to 20',
+ exact: true,
+ })
+ .click();
+ await expect(propertyLabel).toHaveCSS(
+ 'background-color',
+ 'rgba(255, 255, 255, 0.1)',
+ );
+ const first = await page
+ .getByRole('button', {name: 'Select keyframe at frame 10', exact: true})
+ .last()
+ .boundingBox();
+ const second = await page
+ .getByRole('button', {name: 'Select keyframe at frame 20', exact: true})
+ .last()
+ .boundingBox();
+ assert(first && second);
+ await page.mouse.move(first.x - 2, first.y - 2);
+ await page.mouse.down();
+ await page.mouse.move(
+ second.x + second.width + 2,
+ second.y + second.height + 2,
+ {
+ steps: 5,
+ },
+ );
+ await page.mouse.up();
+ await expect(propertyLabel).toHaveCSS(
+ 'background-color',
+ 'rgba(255, 255, 255, 0.1)',
+ );
+
+ // Hold the real save request so the optimistic timeline update is visible.
+ let resumeDelete: () => void = () => undefined;
+ const deleteGate = new Promise<void>((resolve) => {
+ resumeDelete = resolve;
+ });
+ await page.route('**/api/delete-keyframes', async (route) => {
+ await deleteGate;
+ await route.continue();
+ });
+ const deleteRequest = page.waitForRequest('**/api/delete-keyframes', {
+ timeout: 10_000,
+ });
+ try {
+ await page.keyboard.press('Backspace');
+ await deleteRequest;
+ await expect(keyframes).toHaveCount(5);
+ const easings = page.getByRole('button', {
+ name: /^Select easing from frame /,
+ });
+ await expect(easings).toHaveCount(4);
+ for (const easing of await easings.all()) {
+ await expect(easing.locator('div')).toHaveCSS('outline-style', 'none');
+ }
+
+ await expect(opacity).toHaveCount(2);
+ } finally {
+ resumeDelete();
+ await page.unrouteAll({behavior: 'wait'});
+ }
+
+ await expect
+ .poll(() => fs.readFileSync(effectKeyframeE2eFile, 'utf-8'))
+ .toMatch(/interpolate\(\s*frame,\s*\[0,\s*30,\s*40,\s*60,\s*89\]/);
+
+ // The property remains selected, so another Backspace resets its value.
+ await page.keyboard.press('Backspace');
+ await expect
+ .poll(() => fs.readFileSync(effectKeyframeE2eFile, 'utf-8'))
+ .not.toContain('opacity:');
+ await page.reload();
+ await expect(
+ page.getByTitle('Timeline expansion', {exact: true}).first(),
+ ).toBeVisible();
+ await expect(keyframes).toHaveCount(0);
+ });
+
test('edits precise inspector values and adds effect keyframes', async ({
page,
}) => {
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index c8bc3ebba6..dc15a49ec3 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -323,6 +323,7 @@ import {
InteractiveSvgElements,
} from './VisualModeTests/InteractiveComponents';
import {Issue9170} from './VisualModeTests/Issue9170';
+import {KeyframeDeleteFlash} from './VisualModeTests/KeyframeDeleteFlash';
import {OutlineSelectionCases} from './VisualModeTests/OutlineSelectionCases';
import {SequenceDurationInterpolation} from './VisualModeTests/SequenceDurationInterpolation';
import {SequenceShiftRepro} from './VisualModeTests/SequenceShiftRepro';
@@ -3037,6 +3038,14 @@ export const Index: React.FC = () => {
durationInFrames={180}
/>
<Folder name="VisualModeTests">
+ <Composition
+ id="keyframe-delete-flash"
+ component={KeyframeDeleteFlash}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={420}
+ />
<Composition
id="constant-multiplication"
component={ConstantMultiplication}
diff --git a/packages/example/src/VisualModeTests/KeyframeDeleteFlash.tsx b/packages/example/src/VisualModeTests/KeyframeDeleteFlash.tsx
new file mode 100644
index 0000000000..ddf520822a
--- /dev/null
+++ b/packages/example/src/VisualModeTests/KeyframeDeleteFlash.tsx
@@ -0,0 +1,52 @@
+import {Easing, interpolate, Solid, useCurrentFrame} from 'remotion';
+
+export const KeyframeDeleteFlash = () => {
+ const frame = useCurrentFrame();
+
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ backgroundColor: '#111827',
+ color: 'white',
+ fontFamily: 'sans-serif',
+ }}
+ >
+ <div style={{position: 'absolute', top: 40, left: 48}}>
+ <div style={{fontSize: 32, fontWeight: 600}}>
+ Keyframe deletion test
+ </div>
+ <div style={{fontSize: 20, marginTop: 12, color: '#cbd5e1'}}>
+ Open Scale, marquee-select two keyframes, then press Backspace.
+ </div>
+ </div>
+ <Solid
+ name="Scale keyframes"
+ width={220}
+ height={220}
+ color="#3b82f6"
+ style={{
+ borderRadius: 24,
+ scale: interpolate(
+ frame,
+ [0, 108, 394, 399],
+ [
+ 1.5316030229881714, 1.5316030229881714, 1.3784427206893544,
+ 1.5316030229881714,
+ ],
+ {
+ easing: [Easing.step1, Easing.linear, Easing.step1],
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ outputType: 'scale',
+ },
+ ),
+ }}
+ />
+ </div>
+ );
+};
diff --git a/packages/studio/src/components/Timeline/TimelineEffectPropItem.tsx b/packages/studio/src/components/Timeline/TimelineEffectPropItem.tsx
index 48606046e7..a3ca657e18 100644
--- a/packages/studio/src/components/Timeline/TimelineEffectPropItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineEffectPropItem.tsx
@@ -44,7 +44,10 @@ import {
TimelineNonEditableStatus,
UnsupportedStatus,
} from './TimelineSchemaField';
-import {useTimelineRowSelection} from './TimelineSelection';
+import {
+ useTimelineRowContainsSelection,
+ useTimelineRowSelection,
+} from './TimelineSelection';
const fieldRowBase: React.CSSProperties = {};
@@ -441,6 +444,7 @@ export const TimelineEffectPropItem: React.FC<{
Internals.VisualModeDragOverridesContext,
);
const selection = useTimelineRowSelection(nodePathInfo, revealInInspector);
+ const containsSelection = useTimelineRowContainsSelection(nodePathInfo);
const style = useMemo((): React.CSSProperties => {
return field.typeName === 'text-content'
? fieldRowBase
@@ -618,13 +622,13 @@ export const TimelineEffectPropItem: React.FC<{
onSelect={selection.onSelect}
onDoubleClick={onPropertyDoubleClick}
showSelectedBackground
- containsSelection={false}
+ containsSelection={containsSelection}
outerHeight={null}
>
<TimelineFieldRowContent
field={field}
rowDepth={rowDepth}
- selected={selection.selected}
+ selected={selection.selected || containsSelection}
>
<TimelineEffectPropValueAtCurrentFrame
field={field}
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index 3aa228f44e..07d742d6dc 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -613,6 +613,16 @@ const nodePathDescendsFrom = (
);
};
+const timelineRowContainsSelection = (
+ nodePathInfo: SequenceNodePathInfo,
+ selection: TimelineSelection,
+): boolean =>
+ selection.type !== 'guide' &&
+ (nodePathDescendsFrom(selection.nodePathInfo, nodePathInfo) ||
+ ((selection.type === 'keyframe' || selection.type === 'easing') &&
+ timelineNodePathInfoToKey(selection.nodePathInfo) ===
+ timelineNodePathInfoToKey(nodePathInfo)));
+
export const getSelectableTimelineSequenceSelections = (
tracks: readonly Pick<TimelineTrackData, 'nodePathInfo'>[],
): TimelineSelection[] => {
@@ -1224,10 +1234,8 @@ export const TimelineSelectionProvider: React.FC<{
const containsSelection = useCallback(
(nodePathInfo: SequenceNodePathInfo) => {
- return availableSelectedItems.some(
- (selected) =>
- selected.type !== 'guide' &&
- nodePathDescendsFrom(selected.nodePathInfo, nodePathInfo),
+ return availableSelectedItems.some((selected) =>
+ timelineRowContainsSelection(nodePathInfo, selected),
);
},
[availableSelectedItems],
@@ -1794,10 +1802,8 @@ export const useTimelineRowContainsSelection = (
return selectionContext
.getSnapshot()
- .selectedItems.some(
- (selected) =>
- selected.type !== 'guide' &&
- nodePathDescendsFrom(selected.nodePathInfo, nodePathInfo),
+ .selectedItems.some((selected) =>
+ timelineRowContainsSelection(nodePathInfo, selected),
);
}, [nodePathInfo, selectionContext]);
diff --git a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
index 367d324b4d..cc063d70d0 100644
--- a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx
@@ -45,7 +45,10 @@ import {
TimelineFieldValue,
TimelineNonEditableStatus,
} from './TimelineSchemaField';
-import {useTimelineRowSelection} from './TimelineSelection';
+import {
+ useTimelineRowContainsSelection,
+ useTimelineRowSelection,
+} from './TimelineSelection';
import {Transform3DModeContext} from './Transform3DModeContext';
const fieldRowBase: React.CSSProperties = {};
@@ -433,6 +436,7 @@ export const TimelineSequencePropItem: React.FC<{
previewServerState.type === 'connected',
);
const selection = useTimelineRowSelection(nodePathInfo);
+ const containsSelection = useTimelineRowContainsSelection(nodePathInfo);
const transform3DMode = useContext(Transform3DModeContext);
const propStatusesForOverride = Internals.getPropStatusesCtx(
visualModePropStatuses,
@@ -648,13 +652,13 @@ export const TimelineSequencePropItem: React.FC<{
onSelect={selection.onSelect}
onDoubleClick={onPropertyDoubleClick}
showSelectedBackground
- containsSelection={false}
+ containsSelection={containsSelection}
outerHeight={null}
>
<TimelineFieldRowContent
field={field}
rowDepth={rowDepth}
- selected={selection.selected}
+ selected={selection.selected || containsSelection}
>
{fieldValue}
</TimelineFieldRowContent>
diff --git a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts b/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
index 951442063d..0e4a795a12 100644
--- a/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
+++ b/packages/studio/src/components/Timeline/delete-selected-timeline-item.ts
@@ -13,6 +13,7 @@ import {getKeyframeDisplayOffset} from './get-timeline-keyframes';
import {parseKeyframeFieldFromNodePath} from './parse-keyframe-field-from-node-path';
import type {SetPropStatuses} from './save-sequence-prop';
import {
+ getTimelineSelectionFromNodePathInfo,
getTimelineSelectionKey,
type TimelineSelection,
} from './TimelineSelection';
@@ -314,7 +315,14 @@ export const getTimelineSelectionAfterDeletingItems = ({
const nextSelections = new Map<string, TimelineSelection>();
for (const selection of selections) {
- const nextSelection = getSequenceSelectionAfterDeletingEffect(selection);
+ if (selection.type === 'easing') {
+ continue;
+ }
+
+ const nextSelection =
+ selection.type === 'keyframe'
+ ? getTimelineSelectionFromNodePathInfo(selection.nodePathInfo)
+ : getSequenceSelectionAfterDeletingEffect(selection);
if (!nextSelection) {
return [];
}
diff --git a/packages/studio/src/components/Timeline/use-delete-timeline-items.ts b/packages/studio/src/components/Timeline/use-delete-timeline-items.ts
index 043f3f1132..b1af477f06 100644
--- a/packages/studio/src/components/Timeline/use-delete-timeline-items.ts
+++ b/packages/studio/src/components/Timeline/use-delete-timeline-items.ts
@@ -81,26 +81,36 @@ export const useDeleteTimelineItems = () => {
});
if (deletePromise !== null) {
- deletePromise
- .then((deleted) => {
- if (!deleted) {
- return;
- }
+ const updateSelectionAfterDeletion = () => {
+ const nextSelection = getTimelineSelectionAfterDeletingItems({
+ selections: selectedItems,
+ sequences,
+ overrideIdsToNodePaths: overrideIdToNodePathMappings,
+ propStatuses,
+ timelinePosition,
+ });
+ if (nextSelection.length === 0) {
+ clearSelection();
+ } else {
+ selectItems(nextSelection);
+ }
+ };
+
+ if (selectedItems.some((item) => item.type === 'keyframe')) {
+ // Keyframes are removed optimistically. Update selection in the same
+ // event so easing segments cannot inherit the deleted segments' indices.
+ updateSelectionAfterDeletion();
+ deletePromise.catch(() => undefined);
+ } else {
+ deletePromise
+ .then((deleted) => {
+ if (deleted) {
+ updateSelectionAfterDeletion();
+ }
+ })
+ .catch(() => undefined);
+ }
- const nextSelection = getTimelineSelectionAfterDeletingItems({
- selections: selectedItems,
- sequences,
- overrideIdsToNodePaths: overrideIdToNodePathMappings,
- propStatuses,
- timelinePosition,
- });
- if (nextSelection.length === 0) {
- clearSelection();
- } else {
- selectItems(nextSelection);
- }
- })
- .catch(() => undefined);
return;
}
diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.ts
index bea4e27b6c..7e91c08982 100644
--- a/packages/studio/src/test/timeline-selection.test.ts
+++ b/packages/studio/src/test/timeline-selection.test.ts
@@ -128,6 +128,7 @@ import {
TIMELINE_SELECTED_BACKGROUND,
TIMELINE_TICKS_BACKGROUND,
timelineMarqueeRectsIntersect,
+ type TimelineSelection,
} from '../components/Timeline/TimelineSelection';
import {
getTimelineSequenceDurationDragChanges,
@@ -8454,7 +8455,7 @@ test('Deleting selected keyframe selects remaining easing under playhead', () =>
]);
});
-test('Deleting selected keyframe clears selection when playhead is not between remaining keyframes', () => {
+test('Deleting selected keyframe selects its property when playhead is not between remaining keyframes', () => {
const schema = {
opacity: {type: 'number', default: 1, hiddenFromList: false},
} satisfies InteractivitySchema;
@@ -8500,7 +8501,54 @@ test('Deleting selected keyframe clears selection when playhead is not between r
propStatuses,
timelinePosition: 0,
}),
- ).toEqual([]);
+ ).toEqual([
+ {
+ type: 'sequence-prop',
+ nodePathInfo: opacityNodePathInfo,
+ key: 'opacity',
+ },
+ ]);
+});
+
+test('Deleting keyframes across properties selects each affected property once', () => {
+ const opacityNodePathInfo = makeNodePathInfo(
+ ['body', 0],
+ ['controls', 'style.opacity'],
+ );
+ const effectNodePathInfo = makeNodePathInfo(
+ ['body', 1],
+ ['effects', '1', 'radius'],
+ );
+
+ expect(
+ getTimelineSelectionAfterDeletingItems({
+ selections: [opacityNodePathInfo, effectNodePathInfo].flatMap(
+ (nodePathInfo): TimelineSelection[] => [
+ {type: 'keyframe', nodePathInfo, frame: 10},
+ {type: 'keyframe', nodePathInfo, frame: 20},
+ {
+ type: 'easing',
+ nodePathInfo,
+ fromFrame: 10,
+ toFrame: 20,
+ segmentIndex: 1,
+ },
+ ],
+ ),
+ }),
+ ).toEqual([
+ {
+ type: 'sequence-prop',
+ nodePathInfo: opacityNodePathInfo,
+ key: 'style.opacity',
+ },
+ {
+ type: 'sequence-effect-prop',
+ nodePathInfo: effectNodePathInfo,
+ i: 1,
+ key: 'radius',
+ },
+ ]);
});
test('Deleting selected keyframes ignores selected easings', async () => {