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

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

	constructor({
		sceneId,
		newName,
	}: {
		sceneId: string;
		newName: string;
	}) {
		super();
		this.sceneId = sceneId;
		this.newName = newName;
	}

	private sceneId: string;
	private newName: string;

	execute(): CommandResult | undefined {
		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 });
		}
	}
}