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

116 lines
3.1 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 "@/lib/timeline";
2026-01-31 00:20:04 +01:00
import { EditorCore } from "@/core";
2026-02-27 16:33:57 +01:00
import { clampAnimationsToDuration } from "@/lib/animation";
import { isRetimableElement, rippleShiftElements } from "@/lib/timeline";
import { enforceMainTrackStart } from "@/lib/timeline/track-utils";
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-03-02 13:26:40 +01:00
private readonly rippleEnabled: boolean;
2026-01-31 00:20:04 +01:00
2026-02-27 16:33:57 +01:00
constructor({
elementId,
trimStart,
trimEnd,
startTime,
duration,
2026-03-02 13:26:40 +01:00
rippleEnabled = false,
2026-02-27 16:33:57 +01:00
}: {
elementId: string;
trimStart: number;
trimEnd: number;
startTime?: number;
duration?: number;
2026-03-02 13:26:40 +01:00
rippleEnabled?: boolean;
2026-02-27 16:33:57 +01:00
}) {
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-03-02 13:26:40 +01:00
this.rippleEnabled = rippleEnabled;
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) => {
2026-03-02 13:26:40 +01:00
const targetElement = track.elements.find(
(element) => element.id === this.elementId,
);
if (!targetElement) return track;
2026-01-31 00:20:04 +01:00
2026-03-02 13:26:40 +01:00
const nextDuration = this.duration ?? targetElement.duration;
const requestedStartTime = this.startTime ?? targetElement.startTime;
const nextStartTime = enforceMainTrackStart({
tracks: this.savedState ?? [],
targetTrackId: track.id,
requestedStartTime,
excludeElementId: this.elementId,
});
2026-03-02 13:26:40 +01:00
const oldEndTime = targetElement.startTime + targetElement.duration;
const newEndTime = nextStartTime + nextDuration;
const shiftAmount = oldEndTime - newEndTime;
const updatedElement = {
...targetElement,
trimStart: this.trimStart,
trimEnd: this.trimEnd,
startTime: nextStartTime,
duration: nextDuration,
...(isRetimableElement(targetElement)
? { retime: targetElement.retime }
: {}),
2026-03-02 13:26:40 +01:00
animations: clampAnimationsToDuration({
animations: targetElement.animations,
2026-02-27 16:33:57 +01:00
duration: nextDuration,
2026-03-02 13:26:40 +01:00
}),
};
if (this.rippleEnabled && Math.abs(shiftAmount) > 0) {
const shiftedOthers = rippleShiftElements({
elements: track.elements.filter(
(element) => element.id !== this.elementId,
),
2026-03-02 13:26:40 +01:00
afterTime: oldEndTime,
shiftAmount,
});
return {
...track,
elements: track.elements.map((element) =>
element.id === this.elementId
? updatedElement
: (shiftedOthers.find((shifted) => shifted.id === element.id) ??
element),
2026-03-02 13:26:40 +01:00
),
} as typeof track;
}
return {
...track,
elements: track.elements.map((element) =>
element.id === this.elementId ? updatedElement : element,
2026-03-02 13:26:40 +01:00
),
} as typeof track;
2026-01-31 00:20:04 +01:00
});
editor.timeline.updateTracks(updatedTracks);
}
undo(): void {
if (this.savedState) {
const editor = EditorCore.getInstance();
editor.timeline.updateTracks(this.savedState);
}
}
}