Files
OpenCut/apps/web/src/commands/timeline/element/keyframes/remove-keyframe.ts
T

108 lines
2.7 KiB
TypeScript
Raw Normal View History

import { EditorCore } from "@/core";
import {
hasKeyframesForPath,
removeElementKeyframe,
} from "@/animation";
import { Command, type CommandResult } from "@/commands/base-command";
import { updateElementInSceneTracks } from "@/timeline";
import type { AnimationPath, AnimationValue } from "@/animation/types";
import type { SceneTracks, TimelineElement } from "@/timeline";
2026-04-26 04:04:30 +02:00
import { resolveAnimationTarget } from "@/timeline/animation-targets";
function removeKeyframeAndPersist({
element,
propertyPath,
keyframeId,
2026-04-13 04:40:56 +02:00
valueAtPlayhead,
}: {
element: TimelineElement;
propertyPath: AnimationPath;
keyframeId: string;
2026-04-13 04:40:56 +02:00
valueAtPlayhead: AnimationValue | null;
}): TimelineElement {
const target = resolveAnimationTarget({ element, path: propertyPath });
if (!target) {
return element;
}
const nextAnimations = removeElementKeyframe({
animations: element.animations,
propertyPath,
keyframeId,
});
const isChannelNowEmpty = !hasKeyframesForPath({
animations: nextAnimations,
propertyPath,
});
2026-04-13 04:40:56 +02:00
// When clearing all keyframes, preserve the value at the playhead (what the user was seeing)
const shouldPersistToBase = isChannelNowEmpty && valueAtPlayhead !== null;
const baseElement = shouldPersistToBase
2026-04-13 04:40:56 +02:00
? target.setBaseValue({ value: valueAtPlayhead })
: element;
return { ...baseElement, animations: nextAnimations };
}
export class RemoveKeyframeCommand extends Command {
2026-04-07 04:41:27 +02:00
private savedState: SceneTracks | null = null;
private readonly trackId: string;
private readonly elementId: string;
private readonly propertyPath: AnimationPath;
private readonly keyframeId: string;
2026-04-13 04:40:56 +02:00
private readonly valueAtPlayhead: AnimationValue | null;
constructor({
trackId,
elementId,
propertyPath,
keyframeId,
2026-04-13 04:40:56 +02:00
valueAtPlayhead,
}: {
trackId: string;
elementId: string;
propertyPath: AnimationPath;
keyframeId: string;
2026-04-13 04:40:56 +02:00
valueAtPlayhead: AnimationValue | null;
}) {
super();
this.trackId = trackId;
this.elementId = elementId;
this.propertyPath = propertyPath;
this.keyframeId = keyframeId;
2026-04-13 04:40:56 +02:00
this.valueAtPlayhead = valueAtPlayhead;
}
execute(): CommandResult | undefined {
const editor = EditorCore.getInstance();
2026-04-07 04:41:27 +02:00
this.savedState = editor.scenes.getActiveScene().tracks;
2026-04-07 04:41:27 +02:00
const updatedTracks = updateElementInSceneTracks({
tracks: this.savedState,
trackId: this.trackId,
elementId: this.elementId,
update: (element) =>
removeKeyframeAndPersist({
element,
propertyPath: this.propertyPath,
keyframeId: this.keyframeId,
2026-04-13 04:40:56 +02:00
valueAtPlayhead: this.valueAtPlayhead,
}),
});
editor.timeline.updateTracks(updatedTracks);
return undefined;
}
undo(): void {
if (!this.savedState) {
return;
}
const editor = EditorCore.getInstance();
editor.timeline.updateTracks(this.savedState);
}
}