summaryrefslogtreecommitdiff
path: root/apps/web/src/commands/timeline/track/update-track-volume.ts
blob: ecd7f6ac433470b5a0effbffaa5c1b0f48283405 (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
import { Command, type CommandResult } from "@/commands/base-command";
import { EditorCore } from "@/core";
import type { SceneTracks } from "@/timeline";
import { canTrackHaveAudio, updateTrackInSceneTracks } from "@/timeline";
import { clampDb } from "@/timeline/audio-state";

export class UpdateTrackVolumeCommand extends Command {
	private savedState: SceneTracks | null = null;

	constructor({ trackId, volume }: { trackId: string; volume: number }) {
		super();
		this.trackId = trackId;
		this.volume = clampDb(volume);
	}

	private trackId: string;
	private volume: number;

	execute(): CommandResult | undefined {
		const editor = EditorCore.getInstance();
		this.savedState = editor.scenes.getActiveScene().tracks;
		const updatedTracks = updateTrackInSceneTracks({
			tracks: this.savedState,
			trackId: this.trackId,
			update: (track) =>
				canTrackHaveAudio(track) ? { ...track, volume: this.volume } : track,
		});
		editor.timeline.updateTracks(updatedTracks);
		return undefined;
	}

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