summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/element/retime/update-element-retime.ts
blob: 474396a188e4bc2add646f81fd41e8f0003da6f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { EditorCore } from "@/core";
import { clampRetimeRate } from "@/lib/retime/rate";
import { clampAnimationsToDuration } from "@/lib/animation";
import { Command, type CommandResult } from "@/lib/commands/base-command";
import { getTimelineDurationForSourceSpan, getSourceSpanAtClipTime } from "@/lib/retime";
import { isRetimableElement, updateElementInTracks } from "@/lib/timeline";
import type { RetimeConfig, TimelineTrack } from "@/lib/timeline";

function getSourceDuration({
	trimStart,
	trimEnd,
	duration,
	sourceDuration,
	retime,
}: {
	trimStart: number;
	trimEnd: number;
	duration: number;
	sourceDuration?: number;
	retime?: RetimeConfig;
}): number {
	if (typeof sourceDuration === "number") {
		return sourceDuration;
	}

	return (
		trimStart +
		getSourceSpanAtClipTime({
			clipTime: duration,
			retime,
		}) +
		trimEnd
	);
}

export class UpdateElementRetimeCommand extends Command {
	private savedState: TimelineTrack[] | null = null;
	private readonly trackId: string;
	private readonly elementId: string;
	private readonly retime: RetimeConfig | undefined;

	constructor({
		trackId,
		elementId,
		retime,
	}: {
		trackId: string;
		elementId: string;
		retime?: RetimeConfig;
	}) {
		super();
		this.trackId = trackId;
		this.elementId = elementId;
		this.retime = retime;
	}

	execute(): CommandResult | undefined {
		const editor = EditorCore.getInstance();
		this.savedState = editor.timeline.getTracks();

		const updatedTracks = updateElementInTracks({
			tracks: this.savedState,
			trackId: this.trackId,
			elementId: this.elementId,
			elementPredicate: isRetimableElement,
			update: (element) => {
				if (!isRetimableElement(element)) {
					return element;
				}

				const nextRetime = this.retime
					? {
							...this.retime,
							rate: clampRetimeRate({ rate: this.retime.rate }),
						}
					: undefined;
				const sourceDuration = getSourceDuration({
					trimStart: element.trimStart,
					trimEnd: element.trimEnd,
					duration: element.duration,
					sourceDuration: element.sourceDuration,
					retime: element.retime,
				});
				const visibleSourceSpan = Math.max(
					0,
					sourceDuration - element.trimStart - element.trimEnd,
				);
				const nextDuration = getTimelineDurationForSourceSpan({
					sourceSpan: visibleSourceSpan,
					retime: nextRetime,
				});

				return {
					...element,
					retime: nextRetime,
					duration: nextDuration,
					animations: clampAnimationsToDuration({
						animations: element.animations,
						duration: nextDuration,
					}),
				};
			},
		});

		editor.timeline.updateTracks(updatedTracks);
		return undefined;
	}

	undo(): void {
		if (this.savedState) {
			const editor = EditorCore.getInstance();
			editor.timeline.updateTracks(this.savedState);
		}
	}
}