summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/element/keyframes
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/src/lib/commands/timeline/element/keyframes')
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/index.ts11
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts38
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts85
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts33
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts4
5 files changed, 139 insertions, 32 deletions
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/index.ts b/apps/web/src/lib/commands/timeline/element/keyframes/index.ts
index ea19286c..319a827b 100644
--- a/apps/web/src/lib/commands/timeline/element/keyframes/index.ts
+++ b/apps/web/src/lib/commands/timeline/element/keyframes/index.ts
@@ -1,5 +1,6 @@
-export * from "./remove-effect-param-keyframe";
-export * from "./remove-keyframe";
-export * from "./retime-keyframe";
-export * from "./upsert-effect-param-keyframe";
-export * from "./upsert-keyframe";
+export * from "./remove-effect-param-keyframe";
+export * from "./remove-keyframe";
+export * from "./retime-keyframe";
+export * from "./update-scalar-keyframe-curve";
+export * from "./upsert-effect-param-keyframe";
+export * from "./upsert-keyframe";
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts
index b5c7550d..71b2ac21 100644
--- a/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts
+++ b/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts
@@ -1,8 +1,9 @@
import { EditorCore } from "@/core";
import {
- getChannel,
- getChannelValueAtTime,
+ hasKeyframesForPath,
+ getKeyframeById,
removeElementKeyframe,
+ resolveAnimationPathValueAtTime,
resolveAnimationTarget,
} from "@/lib/animation";
import { Command, type CommandResult } from "@/lib/commands/base-command";
@@ -19,17 +20,6 @@ function sampleValueBeforeRemoval({
propertyPath: AnimationPath;
keyframeId: string;
}): AnimationValue | null {
- const channel = getChannel({
- animations: element.animations,
- propertyPath,
- });
- const keyframe = channel?.keyframes.find(
- (candidate) => candidate.id === keyframeId,
- );
- if (!channel || !keyframe) {
- return null;
- }
-
const target = resolveAnimationTarget({ element, path: propertyPath });
if (!target) {
return null;
@@ -39,9 +29,19 @@ function sampleValueBeforeRemoval({
return null;
}
- return getChannelValueAtTime({
- channel,
- time: keyframe.time,
+ const keyframe = getKeyframeById({
+ animations: element.animations,
+ propertyPath,
+ keyframeId,
+ });
+ if (!keyframe) {
+ return null;
+ }
+
+ return resolveAnimationPathValueAtTime({
+ animations: element.animations,
+ propertyPath,
+ localTime: keyframe.time,
fallbackValue: baseValue,
});
}
@@ -72,8 +72,10 @@ function removeKeyframeAndPersist({
keyframeId,
});
- const isChannelNowEmpty =
- getChannel({ animations: nextAnimations, propertyPath }) === undefined;
+ const isChannelNowEmpty = !hasKeyframesForPath({
+ animations: nextAnimations,
+ propertyPath,
+ });
const shouldPersistToBase = isChannelNowEmpty && valueBefore !== null;
const baseElement = shouldPersistToBase
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts b/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts
new file mode 100644
index 00000000..f1eaab9d
--- /dev/null
+++ b/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts
@@ -0,0 +1,85 @@
+import { EditorCore } from "@/core";
+import {
+ resolveAnimationTarget,
+ updateScalarKeyframeCurve,
+} from "@/lib/animation";
+import { Command, type CommandResult } from "@/lib/commands/base-command";
+import { updateElementInTracks } from "@/lib/timeline";
+import type {
+ AnimationPath,
+ ScalarCurveKeyframePatch,
+} from "@/lib/animation/types";
+import type { TimelineTrack } from "@/lib/timeline";
+
+export class UpdateScalarKeyframeCurveCommand extends Command {
+ private savedState: TimelineTrack[] | null = null;
+ private readonly trackId: string;
+ private readonly elementId: string;
+ private readonly propertyPath: AnimationPath;
+ private readonly componentKey: string;
+ private readonly keyframeId: string;
+ private readonly patch: ScalarCurveKeyframePatch;
+
+ constructor({
+ trackId,
+ elementId,
+ propertyPath,
+ componentKey,
+ keyframeId,
+ patch,
+ }: {
+ trackId: string;
+ elementId: string;
+ propertyPath: AnimationPath;
+ componentKey: string;
+ keyframeId: string;
+ patch: ScalarCurveKeyframePatch;
+ }) {
+ super();
+ this.trackId = trackId;
+ this.elementId = elementId;
+ this.propertyPath = propertyPath;
+ this.componentKey = componentKey;
+ this.keyframeId = keyframeId;
+ this.patch = patch;
+ }
+
+ execute(): CommandResult | undefined {
+ const editor = EditorCore.getInstance();
+ this.savedState = editor.timeline.getTracks();
+
+ const updatedTracks = updateElementInTracks({
+ tracks: this.savedState,
+ trackId: this.trackId,
+ elementId: this.elementId,
+ update: (element) => {
+ if (!resolveAnimationTarget({ element, path: this.propertyPath })) {
+ return element;
+ }
+
+ return {
+ ...element,
+ animations: updateScalarKeyframeCurve({
+ animations: element.animations,
+ propertyPath: this.propertyPath,
+ componentKey: this.componentKey,
+ keyframeId: this.keyframeId,
+ patch: this.patch,
+ }),
+ };
+ },
+ });
+
+ editor.timeline.updateTracks(updatedTracks);
+ return undefined;
+ }
+
+ undo(): void {
+ if (!this.savedState) {
+ return;
+ }
+
+ const editor = EditorCore.getInstance();
+ editor.timeline.updateTracks(this.savedState);
+ }
+}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts
index edbf5198..d0f01104 100644
--- a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts
+++ b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts
@@ -1,8 +1,13 @@
import { EditorCore } from "@/core";
import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { upsertEffectParamKeyframe } from "@/lib/animation/effect-param-channel";
+import {
+ buildEffectParamPath,
+ resolveAnimationTarget,
+ upsertPathKeyframe,
+} from "@/lib/animation";
import { updateElementInTracks } from "@/lib/timeline";
import { isVisualElement } from "@/lib/timeline/element-utils";
+import type { AnimationInterpolation } from "@/lib/animation/types";
import type { TimelineTrack } from "@/lib/timeline";
export class UpsertEffectParamKeyframeCommand extends Command {
@@ -12,8 +17,8 @@ export class UpsertEffectParamKeyframeCommand extends Command {
private readonly effectId: string;
private readonly paramKey: string;
private readonly time: number;
- private readonly value: number;
- private readonly interpolation: "linear" | "hold" | undefined;
+ private readonly value: number | string | boolean;
+ private readonly interpolation: AnimationInterpolation | undefined;
private readonly keyframeId: string | undefined;
constructor({
@@ -31,8 +36,8 @@ export class UpsertEffectParamKeyframeCommand extends Command {
effectId: string;
paramKey: string;
time: number;
- value: number;
- interpolation?: "linear" | "hold";
+ value: number | string | boolean;
+ interpolation?: AnimationInterpolation;
keyframeId?: string;
}) {
super();
@@ -57,14 +62,28 @@ export class UpsertEffectParamKeyframeCommand extends Command {
elementPredicate: isVisualElement,
update: (element) => {
const boundedTime = Math.max(0, Math.min(this.time, element.duration));
- const animations = upsertEffectParamKeyframe({
- animations: element.animations,
+ const propertyPath = buildEffectParamPath({
effectId: this.effectId,
paramKey: this.paramKey,
+ });
+ const target = resolveAnimationTarget({
+ element,
+ path: propertyPath,
+ });
+ if (!target) {
+ return element;
+ }
+
+ const animations = upsertPathKeyframe({
+ animations: element.animations,
+ propertyPath,
time: boundedTime,
value: this.value,
interpolation: this.interpolation,
keyframeId: this.keyframeId,
+ kind: target.kind,
+ defaultInterpolation: target.defaultInterpolation,
+ coerceValue: target.coerceValue,
});
return { ...element, animations };
},
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts
index 98589987..bd56749a 100644
--- a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts
+++ b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts
@@ -73,9 +73,9 @@ export class UpsertKeyframeCommand extends Command {
value: this.value,
interpolation: this.interpolation,
keyframeId: this.keyframeId,
- valueKind: target.valueKind,
+ kind: target.kind,
defaultInterpolation: target.defaultInterpolation,
- numericRange: target.numericRange,
+ coerceValue: target.coerceValue,
}),
};
},