summaryrefslogtreecommitdiff
path: root/apps/web/src/services/storage/migrations/transformers/v24-to-v25.ts
blob: b3b48f4212624c7f576b7292a923f66c1bf1e5f1 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { MigrationResult, ProjectRecord } from "./types";
import { getProjectId, isRecord } from "./utils";

export function transformProjectV24ToV25({
	project,
}: {
	project: ProjectRecord;
}): MigrationResult<ProjectRecord> {
	if (!getProjectId({ project })) {
		return { project, skipped: true, reason: "no project id" };
	}

	const version = project.version;
	if (typeof version !== "number") {
		return { project, skipped: true, reason: "invalid version" };
	}
	if (version >= 25) {
		return { project, skipped: true, reason: "already v25" };
	}
	if (version !== 24) {
		return { project, skipped: true, reason: "not v24" };
	}

	return {
		project: {
			...migrateProject({ project }),
			version: 25,
		},
		skipped: false,
	};
}

function migrateProject({
	project,
}: {
	project: ProjectRecord;
}): ProjectRecord {
	if (!Array.isArray(project.scenes)) {
		return project;
	}

	return {
		...project,
		scenes: project.scenes.map((scene) => migrateScene({ scene })),
	};
}

function migrateScene({ scene }: { scene: unknown }): unknown {
	if (!isRecord(scene) || !isRecord(scene.tracks)) {
		return scene;
	}

	const tracks = scene.tracks;
	const nextTracks: ProjectRecord = { ...tracks };

	if (isRecord(tracks.main)) {
		nextTracks.main = migrateTrack({ track: tracks.main });
	}

	if (Array.isArray(tracks.overlay)) {
		nextTracks.overlay = tracks.overlay.map((track) =>
			migrateTrack({ track }),
		);
	}

	if (Array.isArray(tracks.audio)) {
		nextTracks.audio = tracks.audio.map((track) => migrateTrack({ track }));
	}

	return { ...scene, tracks: nextTracks };
}

function migrateTrack({ track }: { track: unknown }): unknown {
	if (!isRecord(track) || !Array.isArray(track.elements)) {
		return track;
	}

	return {
		...track,
		elements: track.elements.map((element) => migrateElement({ element })),
	};
}

function migrateElement({ element }: { element: unknown }): unknown {
	if (!isRecord(element) || !isRecord(element.animations)) {
		return element;
	}

	const nextAnimations = migrateAnimations({ animations: element.animations });
	if (nextAnimations === element.animations) {
		return element;
	}

	return { ...element, animations: nextAnimations };
}

function migrateAnimations({
	animations,
}: {
	animations: ProjectRecord;
}): ProjectRecord {
	if (!isRecord(animations.bindings) || !isRecord(animations.channels)) {
		return animations;
	}

	const positionBinding = animations.bindings["transform.position"];
	if (!isRecord(positionBinding) || positionBinding.kind !== "vector2") {
		return animations;
	}

	const xChannel = animations.channels["transform.position:x"];
	const yChannel = animations.channels["transform.position:y"];

	const nextBindings: ProjectRecord = { ...animations.bindings };
	const nextChannels: ProjectRecord = { ...animations.channels };

	delete nextBindings["transform.position"];
	delete nextChannels["transform.position:x"];
	delete nextChannels["transform.position:y"];

	nextBindings["transform.positionX"] = {
		path: "transform.positionX",
		kind: "number",
		components: [{ key: "value", channelId: "transform.positionX:value" }],
	};
	nextBindings["transform.positionY"] = {
		path: "transform.positionY",
		kind: "number",
		components: [{ key: "value", channelId: "transform.positionY:value" }],
	};

	if (isRecord(xChannel)) {
		nextChannels["transform.positionX:value"] = xChannel;
	}
	if (isRecord(yChannel)) {
		nextChannels["transform.positionY:value"] = yChannel;
	}

	return { ...animations, bindings: nextBindings, channels: nextChannels };
}