summaryrefslogtreecommitdiff
path: root/apps/web/src/commands/timeline/clipboard/paste-keyframes.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
committerMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
commit3e89d2998588f8e35c524e523ba2ee853f0f6475 (patch)
tree57221f96c1259b3b07e3ddf8c1adefc502b9d8eb /apps/web/src/commands/timeline/clipboard/paste-keyframes.ts
parent729d10592f4e802f91c555c309ff3ba4b119f843 (diff)
downloadopencut-3e89d2998588f8e35c524e523ba2ee853f0f6475.tar.gz
refactor: restructure files to their domains, new preview overlay system, and dep graph
Diffstat (limited to 'apps/web/src/commands/timeline/clipboard/paste-keyframes.ts')
-rw-r--r--apps/web/src/commands/timeline/clipboard/paste-keyframes.ts135
1 files changed, 135 insertions, 0 deletions
diff --git a/apps/web/src/commands/timeline/clipboard/paste-keyframes.ts b/apps/web/src/commands/timeline/clipboard/paste-keyframes.ts
new file mode 100644
index 00000000..e05b49fa
--- /dev/null
+++ b/apps/web/src/commands/timeline/clipboard/paste-keyframes.ts
@@ -0,0 +1,135 @@
+import { EditorCore } from "@/core";
+import {
+ getKeyframeAtTime,
+ resolveAnimationTarget,
+ updateScalarKeyframeCurve,
+ upsertPathKeyframe,
+} from "@/animation";
+import { Command, type CommandResult } from "@/commands/base-command";
+import type { KeyframeClipboardItem } from "@/clipboard";
+import type { SceneTracks, TimelineElement } from "@/timeline";
+import { updateElementInSceneTracks } from "@/timeline";
+import { generateUUID } from "@/utils/id";
+
+function pasteKeyframesIntoElement({
+ element,
+ time,
+ clipboardItems,
+}: {
+ element: TimelineElement;
+ time: number;
+ clipboardItems: KeyframeClipboardItem[];
+}): TimelineElement {
+ let nextElement = element;
+
+ for (const item of clipboardItems) {
+ const target = resolveAnimationTarget({
+ element: nextElement,
+ path: item.propertyPath,
+ });
+ if (!target) {
+ continue;
+ }
+
+ const keyframeTime = Math.max(
+ 0,
+ Math.min(time + item.timeOffset, nextElement.duration),
+ );
+ const nextAnimations = upsertPathKeyframe({
+ animations: nextElement.animations,
+ propertyPath: item.propertyPath,
+ time: keyframeTime,
+ value: item.value,
+ interpolation: item.interpolation,
+ keyframeId: generateUUID(),
+ kind: target.kind,
+ defaultInterpolation: target.defaultInterpolation,
+ coerceValue: target.coerceValue,
+ });
+ const pastedKeyframe = getKeyframeAtTime({
+ animations: nextAnimations,
+ propertyPath: item.propertyPath,
+ time: keyframeTime,
+ });
+
+ let patchedAnimations = nextAnimations;
+ if (pastedKeyframe) {
+ for (const curvePatch of item.curvePatches) {
+ const nextPatchedAnimations = updateScalarKeyframeCurve({
+ animations: patchedAnimations,
+ propertyPath: item.propertyPath,
+ componentKey: curvePatch.componentKey,
+ keyframeId: pastedKeyframe.id,
+ patch: curvePatch.patch,
+ });
+ patchedAnimations = nextPatchedAnimations ?? patchedAnimations;
+ }
+ }
+
+ nextElement = {
+ ...nextElement,
+ animations: patchedAnimations,
+ };
+ }
+
+ return nextElement;
+}
+
+export class PasteKeyframesCommand extends Command {
+ private savedState: SceneTracks | null = null;
+ private readonly trackId: string;
+ private readonly elementId: string;
+ private readonly time: number;
+ private readonly clipboardItems: KeyframeClipboardItem[];
+
+ constructor({
+ trackId,
+ elementId,
+ time,
+ clipboardItems,
+ }: {
+ trackId: string;
+ elementId: string;
+ time: number;
+ clipboardItems: KeyframeClipboardItem[];
+ }) {
+ super();
+ this.trackId = trackId;
+ this.elementId = elementId;
+ this.time = time;
+ this.clipboardItems = clipboardItems;
+ }
+
+ execute(): CommandResult | undefined {
+ if (this.clipboardItems.length === 0) {
+ return undefined;
+ }
+
+ const editor = EditorCore.getInstance();
+ this.savedState = editor.scenes.getActiveScene().tracks;
+
+ const updatedTracks = updateElementInSceneTracks({
+ tracks: this.savedState,
+ trackId: this.trackId,
+ elementId: this.elementId,
+ update: (element) =>
+ pasteKeyframesIntoElement({
+ element,
+ time: this.time,
+ clipboardItems: this.clipboardItems,
+ }),
+ });
+
+ editor.timeline.updateTracks(updatedTracks);
+ return undefined;
+ }
+
+ undo(): void {
+ if (!this.savedState) {
+ return;
+ }
+
+ const editor = EditorCore.getInstance();
+ editor.timeline.updateTracks(this.savedState);
+ }
+}