mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* refactor(studio): the auto-id write posts through the one patch-element client * test(studio): pin the auto-id write's saves, refusals and unsafe targets * test(studio): pin the auto-id write's plain refusal and missing changed flag * test(studio): the auto-id write needs a toast handler and sends the write token * refactor(studio): one request for every GSAP mutation write * fix(studio): a refused id write no longer leaves the id on the previewed element * style(studio): drop the two comments the names already carry * fix(studio): keep minting the id on the element so overlapping adds never share one * fix(studio): an id the file already holds counts as saved, and no project leaves no id * fix(studio): the server picks an added effect's element id, so ids never repeat The browser minted the id from the live preview and rolled it back when the write failed. A reply lost after the id was saved left the file holding an id the preview had dropped, so the next add on a sibling wrote the same id again; and a refused add could strip the id an overlapping saved add had just given the same element. A new patch-element op, ensure-id, keeps the element's id or makes the proposed one unique in the file, and every patch-element reply names the element's id. Studio sets the id on the preview only from that reply, so there is nothing to roll back. nextUniqueId moves to sourceMutation as the one owner (split and duplicate already used it). * test(studio): pin the gsap write token and the refused-write toast * refactor(studio): one unique-id helper on the server, and shorter id comments * style(studio): keep the id helper doc under the line limit and the test-utils header true * fix(studio): an added effect's id stays unique across every composition file * style(studio): keep the files route's comment share at the base * fix(studio): count an id written right after a quote or slash as taken * fix(studio): a quote-ended string no longer hides another file's id
78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import type { DomEditSelection } from "../components/editor/domEditingTypes";
|
|
|
|
export { PROPERTY_DEFAULTS } from "./gsapShared";
|
|
import { idSelector, matchesExactlyOne } from "./gsapShared";
|
|
|
|
/**
|
|
* The selector for a NEW tween; a shared-class selector would hit every sibling, so an
|
|
* element without a unique one gets an `autoId` proposal the server makes unique (`ensure-id`).
|
|
*/
|
|
export function ensureElementAddressable(selection: DomEditSelection): {
|
|
selector: string;
|
|
autoId?: string;
|
|
} {
|
|
if (selection.id) return { selector: idSelector(selection.id) };
|
|
|
|
const el = selection.element;
|
|
const doc = el.ownerDocument;
|
|
if (selection.selector && matchesExactlyOne(doc, selection.selector, el)) {
|
|
return { selector: selection.selector };
|
|
}
|
|
|
|
const tag = el.tagName.toLowerCase();
|
|
let id = tag;
|
|
let n = 1;
|
|
while (doc.getElementById(id)) {
|
|
n += 1;
|
|
id = `${tag}-${n}`;
|
|
}
|
|
return { selector: idSelector(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 function formatFieldsSuffix(rawFields: unknown): string {
|
|
const fields = Array.isArray(rawFields)
|
|
? rawFields.filter((f): f is string => typeof f === "string")
|
|
: [];
|
|
return fields.length > 0 ? ` (${fields.join(", ")})` : "";
|
|
}
|
|
|
|
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)) {
|
|
return `Couldn't save animation: ${formatGsapMutationHttpErrorMessage(
|
|
error.statusCode,
|
|
body,
|
|
)}${formatFieldsSuffix(body.fields)}`;
|
|
}
|
|
return `Couldn't save animation: ${error.message}`;
|
|
}
|