Files
OpenCut/apps/web/src/lib/commands/timeline/element/update-element-trim.ts
T

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-01-31 00:20:04 +01:00
import { Command } from "@/lib/commands/base-command";
import type { TimelineTrack } from "@/types/timeline";
import { EditorCore } from "@/core";
2026-02-27 16:33:57 +01:00
import { clampAnimationsToDuration } from "@/lib/animation";
2026-01-31 00:20:04 +01:00
export class UpdateElementTrimCommand extends Command {
private savedState: TimelineTrack[] | null = null;
2026-02-27 16:33:57 +01:00
private readonly elementId: string;
private readonly trimStart: number;
private readonly trimEnd: number;
private readonly startTime: number | undefined;
private readonly duration: number | undefined;
2026-01-31 00:20:04 +01:00
2026-02-27 16:33:57 +01:00
constructor({
elementId,
trimStart,
trimEnd,
startTime,
duration,
}: {
elementId: string;
trimStart: number;
trimEnd: number;
startTime?: number;
duration?: number;
}) {
2026-01-31 00:20:04 +01:00
super();
2026-02-27 16:33:57 +01:00
this.elementId = elementId;
this.trimStart = trimStart;
this.trimEnd = trimEnd;
this.startTime = startTime;
this.duration = duration;
2026-01-31 00:20:04 +01:00
}
execute(): void {
const editor = EditorCore.getInstance();
this.savedState = editor.timeline.getTracks();
const updatedTracks = this.savedState.map((track) => {
const newElements = track.elements.map((element) => {
if (element.id !== this.elementId) {
return element;
}
2026-02-27 16:33:57 +01:00
const nextDuration = this.duration ?? element.duration;
2026-01-31 00:20:04 +01:00
return {
...element,
trimStart: this.trimStart,
trimEnd: this.trimEnd,
startTime: this.startTime ?? element.startTime,
2026-02-27 16:33:57 +01:00
duration: nextDuration,
animations: clampAnimationsToDuration({
animations: element.animations,
duration: nextDuration,
}),
2026-01-31 00:20:04 +01:00
};
});
return { ...track, elements: newElements } as typeof track;
});
editor.timeline.updateTracks(updatedTracks);
}
undo(): void {
if (this.savedState) {
const editor = EditorCore.getInstance();
editor.timeline.updateTracks(this.savedState);
}
}
}