summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-04-13 04:40:56 +0200
committerMaze Winther <mazewinther@gmail.com>2026-04-13 05:03:10 +0200
commit2272598d420cf078aaa3928246bfa3a414f6eddb (patch)
treeb52460a2e3576c9e517ef8a9dd86959a68f31b69 /apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts
parent5a2aaf3601b6b050603f51e487a31fb07862a21f (diff)
downloadopencut-2272598d420cf078aaa3928246bfa3a414f6eddb.tar.gz
feat: clipboard with keyframe paste
Made-with: Cursor
Diffstat (limited to 'apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts')
-rw-r--r--apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts135
1 files changed, 135 insertions, 0 deletions
diff --git a/apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts b/apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts
new file mode 100644
index 00000000..210afbd3
--- /dev/null
+++ b/apps/web/src/lib/commands/timeline/clipboard/paste-keyframes.ts
@@ -0,0 +1,135 @@
+import { EditorCore } from "@/core";
+import {
+ getKeyframeAtTime,
+ resolveAnimationTarget,
+ updateScalarKeyframeCurve,
+ upsertPathKeyframe,
+} from "@/lib/animation";
+import { Command, type CommandResult } from "@/lib/commands/base-command";
+import type { KeyframeClipboardItem } from "@/lib/clipboard";
+import type { SceneTracks, TimelineElement } from "@/lib/timeline";
+import { updateElementInSceneTracks } from "@/lib/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);
+ }
+}