summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/track/toggle-track-visibility.ts
blob: 4bfd53c058dffca607897bdc4a83d95af80fd58d (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
import { Command } from "@/lib/commands/base-command";
import type { TimelineTrack } from "@/lib/timeline";
import { EditorCore } from "@/core";
import { canTrackBeHidden } from "@/lib/timeline";

export class ToggleTrackVisibilityCommand extends Command {
	private savedState: TimelineTrack[] | null = null;

	constructor(private trackId: string) {
		super();
	}

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

		const targetTrack = this.savedState.find(
			(track) => track.id === this.trackId,
		);
		if (!targetTrack) {
			return;
		}

		const updatedTracks = this.savedState.map((track) => {
			if (track.id === this.trackId && canTrackBeHidden(track)) {
				return { ...track, hidden: !track.hidden };
			}
			return track;
		});

		editor.timeline.updateTracks(updatedTracks);
	}

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