-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathgsapScriptCommitHelpers.ts
More file actions
128 lines (118 loc) · 3.66 KB
/
Copy pathgsapScriptCommitHelpers.ts
File metadata and controls
128 lines (118 loc) · 3.66 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
import { findUnsafeDomPatchValues } from "@hyperframes/core/studio-api/finite-mutation";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
export const PROPERTY_DEFAULTS: Record<string, number> = {
opacity: 1,
x: 0,
y: 0,
scale: 1,
scaleX: 1,
scaleY: 1,
rotation: 0,
width: 100,
height: 100,
};
export function ensureElementAddressable(selection: DomEditSelection): {
selector: string;
autoId?: string;
} {
if (selection.id) return { selector: `#${selection.id}` };
if (selection.selector) return { selector: selection.selector };
const el = selection.element;
const doc = el.ownerDocument;
const tag = el.tagName.toLowerCase();
let id = tag;
let n = 1;
while (doc.getElementById(id)) {
n += 1;
id = `${tag}-${n}`;
}
el.setAttribute("id", id);
return { selector: `#${id}`, autoId: id };
}
export class GsapMutationHttpError extends Error {
constructor(
readonly statusCode: number,
readonly responseBody: unknown,
) {
super(formatGsapMutationHttpErrorMessage(statusCode, responseBody));
this.name = "GsapMutationHttpError";
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
export async function readJsonResponseBody(res: Response): Promise<unknown> {
const contentType = res.headers.get("content-type") ?? "";
if (!contentType.includes("application/json")) {
return await res.text().catch(() => null);
}
return await res.json().catch(() => null);
}
function formatGsapMutationHttpErrorMessage(statusCode: number, body: unknown): string {
if (isRecord(body) && typeof body.error === "string") {
return body.error;
}
return `GSAP mutation failed with status ${statusCode}`;
}
export function formatGsapMutationRejectionToast(error: GsapMutationHttpError): string {
const body = error.responseBody;
if (isRecord(body)) {
const fields = Array.isArray(body.fields)
? body.fields.filter((field): field is string => typeof field === "string")
: [];
const suffix = fields.length > 0 ? ` (${fields.join(", ")})` : "";
return `Couldn't save animation: ${formatGsapMutationHttpErrorMessage(
error.statusCode,
body,
)}${suffix}`;
}
return `Couldn't save animation: ${error.message}`;
}
interface AssignAutoIdParams {
projectId: string;
targetPath: string;
selection: DomEditSelection;
autoId: string;
showToast?: (message: string, tone?: "error" | "info") => void;
}
export async function assignGsapTargetAutoIdIfNeeded({
projectId,
targetPath,
selection,
autoId,
showToast,
}: AssignAutoIdParams): Promise<boolean> {
const patchBody = {
target: {
id: selection.id,
hfId: selection.hfId,
selector: selection.selector,
selectorIndex: selection.selectorIndex,
},
operations: [{ type: "html-attribute", property: "id", value: autoId }],
};
const unsafePatchFields = findUnsafeDomPatchValues(patchBody);
if (unsafePatchFields.length > 0) {
showToast?.("Couldn't assign element id because the patch contains invalid values", "error");
return false;
}
const res = await fetch(
`/api/projects/${encodeURIComponent(projectId)}/file-mutations/patch-element/${encodeURIComponent(targetPath)}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patchBody),
},
);
if (!res.ok) {
showToast?.(
formatGsapMutationRejectionToast(
new GsapMutationHttpError(res.status, await readJsonResponseBody(res)),
),
"error",
);
return false;
}
const data = (await res.json()) as { changed?: boolean };
return data.changed === true;
}