summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/scene/rename-scene.ts
blob: 554e3940f919315c679c9756b2f4dbb7ac1dccf9 (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
import { Command } from "@/lib/commands/base-command";
import { EditorCore } from "@/core";
import type { TScene } from "@/lib/timeline";
import { updateSceneInArray } from "@/lib/scenes";

export class RenameSceneCommand extends Command {
	private savedScenes: TScene[] | null = null;
	private previousName: string | null = null;

	constructor(
		private sceneId: string,
		private newName: string,
	) {
		super();
	}

	execute(): void {
		const editor = EditorCore.getInstance();
		const scenes = editor.scenes.getScenes();

		this.savedScenes = [...scenes];

		const scene = scenes.find((s) => s.id === this.sceneId);
		if (!scene) {
			console.error("Scene not found:", this.sceneId);
			return;
		}

		this.previousName = scene.name;

		const updatedScenes = updateSceneInArray({
			scenes,
			sceneId: this.sceneId,
			updates: { name: this.newName, updatedAt: new Date() },
		});

		editor.scenes.setScenes({ scenes: updatedScenes });
	}

	undo(): void {
		if (this.savedScenes && this.previousName !== null) {
			const editor = EditorCore.getInstance();
			editor.scenes.setScenes({ scenes: this.savedScenes });
		}
	}
}