summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/element/update-element.ts
blob: 45b2603ffa6cbe0045cb108c51b29885d9bc01a5 (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
import { Command, type CommandResult } from "@/lib/commands/base-command";
import type { TimelineElement, TimelineTrack } from "@/lib/timeline";
import { EditorCore } from "@/core";
import { updateElementInTracks } from "@/lib/timeline";

export class UpdateElementCommand extends Command {
	private savedState: TimelineTrack[] | null = null;
	private readonly trackId: string;
	private readonly elementId: string;
	private readonly updates: Partial<TimelineElement>;

	constructor({
		trackId,
		elementId,
		updates,
	}: {
		trackId: string;
		elementId: string;
		updates: Partial<TimelineElement>;
	}) {
		super();
		this.trackId = trackId;
		this.elementId = elementId;
		this.updates = updates;
	}

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

		const updatedTracks = updateElementInTracks({
			tracks: this.savedState,
			trackId: this.trackId,
			elementId: this.elementId,
			update: (element) => ({ ...element, ...this.updates }) as TimelineElement,
		});

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

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