-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathuseGsapAnimationOps.ts
More file actions
286 lines (272 loc) · 8.6 KB
/
Copy pathuseGsapAnimationOps.ts
File metadata and controls
286 lines (272 loc) · 8.6 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { useCallback } from "react";
import type { Composition } from "@hyperframes/sdk";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { roundTo3 } from "../utils/rounding";
import {
sdkGsapTweenPersist,
sdkGsapDeleteAllForSelectorPersist,
sdkAddWithKeyframesPersist,
sdkReplaceWithKeyframesPersist,
type CutoverDeps,
} from "../utils/sdkCutover";
import {
assignGsapTargetAutoIdIfNeeded,
ensureElementAddressable,
} from "./gsapScriptCommitHelpers";
import type { CommitMutation, SafeGsapCommitMutation } from "./gsapScriptCommitTypes";
interface SdkAnimationDeps {
sdkSession?: Composition | null;
sdkDeps?: CutoverDeps | null;
}
interface GsapAnimationOpsParams extends SdkAnimationDeps {
projectIdRef: React.MutableRefObject<string | null>;
activeCompPath: string | null;
commitMutation: CommitMutation;
commitMutationSafely: SafeGsapCommitMutation;
showToast: (message: string, tone?: "error" | "info") => void;
}
export function useGsapAnimationOps({
projectIdRef,
activeCompPath,
commitMutation,
commitMutationSafely,
showToast,
sdkSession,
sdkDeps,
}: GsapAnimationOpsParams) {
const updateGsapMeta = useCallback(
async (
selection: DomEditSelection,
animationId: string,
updates: { duration?: number; ease?: string; easeEach?: string; position?: number },
) => {
if (sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const handled = await sdkGsapTweenPersist(
targetPath,
{ kind: "set", animationId, properties: updates },
sdkSession,
sdkDeps,
{ label: "Edit GSAP animation", coalesceKey: `gsap:${animationId}:meta` },
);
if (handled) return;
}
commitMutationSafely(
selection,
{ type: "update-meta", animationId, updates },
{ label: "Edit GSAP animation", coalesceKey: `gsap:${animationId}:meta`, softReload: true },
);
},
[commitMutationSafely, activeCompPath, sdkSession, sdkDeps],
);
const deleteGsapAnimation = useCallback(
async (selection: DomEditSelection, animationId: string) => {
if (sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const handled = await sdkGsapTweenPersist(
targetPath,
{ kind: "remove", animationId },
sdkSession,
sdkDeps,
{ label: "Delete GSAP animation" },
);
if (handled) return;
}
commitMutationSafely(
selection,
{ type: "delete", animationId, stripStudioEdits: true },
{ label: "Delete GSAP animation" },
);
},
[commitMutationSafely, activeCompPath, sdkSession, sdkDeps],
);
const deleteAllForSelector = useCallback(
async (selection: DomEditSelection, targetSelector: string) => {
if (sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const handled = await sdkGsapDeleteAllForSelectorPersist(
targetPath,
targetSelector,
sdkSession,
sdkDeps,
{ label: "Delete all animations for element" },
);
if (handled) return;
}
void commitMutation(
selection,
{ type: "delete-all-for-selector", targetSelector },
{ label: "Delete all animations for element", softReload: true },
);
},
[commitMutation, activeCompPath, sdkSession, sdkDeps],
);
// fallow-ignore-next-line complexity
const addGsapAnimation = useCallback(
// fallow-ignore-next-line complexity
async (
selection: DomEditSelection,
method: "to" | "from" | "set" | "fromTo",
_currentTime?: number,
) => {
const { selector, autoId } = ensureElementAddressable(selection);
if (autoId) {
const pid = projectIdRef.current;
const targetPath = selection.sourceFile || activeCompPath || "index.html";
if (!pid) return;
const assigned = await assignGsapTargetAutoIdIfNeeded({
projectId: pid,
targetPath,
selection,
autoId,
showToast,
});
if (!assigned) return;
}
const elStart = Number.parseFloat(selection.dataAttributes?.start ?? "0") || 0;
const elDuration = Number.parseFloat(selection.dataAttributes?.duration ?? "1") || 1;
const position = roundTo3(elStart);
const duration = roundTo3(elDuration);
const toDefaults: Record<string, Record<string, number>> = {
from: { opacity: 0 },
to: { x: 0, y: 0, opacity: 1 },
set: { opacity: 1 },
fromTo: { x: 0, y: 0, opacity: 1 },
};
// Skip SDK path when an id was just assigned server-side (autoId): the
// SDK session hasn't reloaded that write yet, so persisting its
// serialization would clobber the new id — let the server add the tween
// atomically with the id it wrote.
if (!autoId && selection.hfId && sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const spec = {
method,
position,
...(method !== "set" ? { duration, ease: "power2.out" as const } : {}),
properties: toDefaults[method] ?? { opacity: 1 },
...(method === "fromTo" ? { fromProperties: { opacity: 0 } } : {}),
};
const handled = await sdkGsapTweenPersist(
targetPath,
{ kind: "add", target: selection.hfId, spec },
sdkSession,
sdkDeps,
{ label: `Add GSAP ${method} animation` },
);
if (handled) return;
}
await commitMutation(
selection,
{
type: "add",
targetSelector: selector,
method,
position,
duration: method === "set" ? undefined : duration,
ease: method === "set" ? undefined : "power2.out",
properties: toDefaults[method] ?? { opacity: 1 },
fromProperties: method === "fromTo" ? { opacity: 0 } : undefined,
},
{ label: `Add GSAP ${method} animation` },
);
},
[activeCompPath, commitMutation, projectIdRef, showToast, sdkSession, sdkDeps],
);
type KeyframeEntry = {
percentage: number;
properties: Record<string, number | string>;
ease?: string;
auto?: boolean;
};
const addWithKeyframes = useCallback(
async (
selection: DomEditSelection,
targetSelector: string,
position: number,
duration: number,
keyframes: KeyframeEntry[],
ease?: string,
label = "Add animation with keyframes",
) => {
if (sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const handled = await sdkAddWithKeyframesPersist(
targetPath,
targetSelector,
position,
duration,
keyframes,
ease,
sdkSession,
sdkDeps,
{ label },
);
if (handled) return;
}
void commitMutation(
selection,
{
type: "add-with-keyframes",
targetSelector,
position,
duration,
keyframes,
...(ease ? { ease } : {}),
},
{ label, softReload: true },
);
},
[commitMutation, activeCompPath, sdkSession, sdkDeps],
);
const replaceWithKeyframes = useCallback(
async (
selection: DomEditSelection,
animationId: string,
targetSelector: string,
position: number,
duration: number,
keyframes: KeyframeEntry[],
ease?: string,
label = "Replace animation with keyframes",
) => {
if (sdkSession && sdkDeps) {
const targetPath = selection.sourceFile || activeCompPath || "index.html";
const handled = await sdkReplaceWithKeyframesPersist(
targetPath,
animationId,
targetSelector,
position,
duration,
keyframes,
ease,
sdkSession,
sdkDeps,
{ label },
);
if (handled) return;
}
void commitMutation(
selection,
{
type: "replace-with-keyframes",
animationId,
targetSelector,
position,
duration,
keyframes,
...(ease ? { ease } : {}),
},
{ label, softReload: true },
);
},
[commitMutation, activeCompPath, sdkSession, sdkDeps],
);
return {
updateGsapMeta,
deleteGsapAnimation,
deleteAllForSelector,
addGsapAnimation,
addWithKeyframes,
replaceWithKeyframes,
};
}