mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* fix(studio): hide handles Studio would refuse to commit Studio showed move, resize and rotate handles on animated elements whose motion it cannot write back (a helper or loop, a runtime-computed value, a live tween with no matching source). The drag ran, the commit was refused at release, the element snapped back and a toast explained why. The commit's refusal rules now also run when a selection resolves: - move uses the existing drag preflight, now exported - rotation and resize rules are extracted into preflight functions that the commit paths call first, so both use the same decision code - the session narrows canApplyManualOffset / Size / Rotation (with the refusal's message as reasonIfDisabled) in one place, and the chrome, nudge, group drag and design panel read that unchanged - until an element's first check lands, its manual flags stay closed A press on a box that cannot move now shows the reason at once instead of after 4px of travel. * refactor(studio): read the rotation preflight selector once and derive the scale flag * fix(studio): make the handle check read what the commit reads, and never stall - Match tweens to an element the same way everywhere: the animation fetch used by the handle check and by group moves now also matches through the live element, so an element a class tween animates keeps its handles. - The handle check reads each file's parse once per edit version. A file with no tweens is an answer, not a cold parse to retry, so plain elements get their handles after one read and later selections in that file at once. - A failed read is not remembered: the next selection asks again. - While the check is still running a press stays silent, and only the primary button explains a blocked press. A group press with a blocked member now says why instead of doing nothing. * fix(studio): check handles per project, keep answers on failed reads, refuse shared group tweens - The handle check's parse cache now uses the same project and file key as the parse request, so switching projects never judges one project's file by another's parse. - A failed re-read after a save or undo keeps the last good answer and asks again on the next selection, instead of closing the handles. - A group move stays refused when a member's motion comes from one tween shared with other targets, as before: each member would write into the same tween. The group check and the group commit use the same rule; a single selection still edits the shared tween. - The element-to-tween matcher moves to its own module so the move rule can use it without an import cycle. * docs(studio): the group move rule comment says which tweens it excludes
140 lines
5.8 KiB
TypeScript
140 lines
5.8 KiB
TypeScript
/**
|
|
* Reading a composition file's GSAP tweens into the keyframe cache: fetch and
|
|
* selector -> element id resolution.
|
|
* Split from useGsapTweenCache to keep that file under the 600-line limit.
|
|
*/
|
|
import type { GsapAnimation, GsapKeyframesData, ParsedGsap } from "@hyperframes/core/gsap-parser";
|
|
import { isStudioHoldSet } from "@hyperframes/core/gsap-parser";
|
|
import { isPreviewBooted, usePlayerStore, whenPreviewBooted } from "../player/store/playerStore";
|
|
import { replaceKeyframeCacheForFile } from "./gsapKeyframeCacheHelpers";
|
|
import { resolveClipTimingBasis, resolveSelectorElementIds, toClipKeyframes } from "./gsapShared";
|
|
import {
|
|
deduplicateKeyframes,
|
|
isStaticPositionHold,
|
|
synthesizeFlatTweenKeyframes,
|
|
type MergeableKeyframe,
|
|
} from "./gsapTweenSynth";
|
|
|
|
export { resolveSelectorElementIds };
|
|
|
|
/**
|
|
* The slice of the parse response callers actually read. The endpoint returns
|
|
* the full `ParsedGsap` (preamble/postamble and all), but nothing downstream of
|
|
* this fetch touches the source-text fields, so the guard below only has to
|
|
* vouch for what gets used.
|
|
*/
|
|
type ParsedGsapAnimations = Pick<
|
|
ParsedGsap,
|
|
"animations" | "multipleTimelines" | "unsupportedTimelinePattern"
|
|
>;
|
|
|
|
/**
|
|
* A proxy, an error page, or a stale server can answer 200 with something that
|
|
* has no `animations` array — the case where the old blind cast crashed on
|
|
* `.animations.filter`.
|
|
*/
|
|
function hasAnimations(value: unknown): value is ParsedGsapAnimations {
|
|
return (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
"animations" in value &&
|
|
Array.isArray(value.animations)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Requests for the same file that overlap in time, keyed `projectId|sourceFile`.
|
|
*
|
|
* Every parse re-reads and re-parses the whole composition server-side, and a
|
|
* multi-element action asks for the same file once per element. Sharing the
|
|
* in-flight promise makes that one request. Only OVERLAPPING calls share: the
|
|
* entry is dropped the moment it settles, so a call made after a write still
|
|
* gets a fresh parse.
|
|
*/
|
|
const inFlightParses = new Map<string, Promise<ParsedGsapAnimations | null>>();
|
|
|
|
export const parseCacheKey = (projectId: string, sourceFile: string) =>
|
|
`${projectId}|${sourceFile}`;
|
|
|
|
export function fetchParsedAnimations(
|
|
projectId: string,
|
|
sourceFile: string,
|
|
options: { fresh?: boolean } = {},
|
|
): Promise<ParsedGsapAnimations | null> {
|
|
const key = parseCacheKey(projectId, sourceFile);
|
|
if (options.fresh) inFlightParses.delete(key);
|
|
const inFlight = inFlightParses.get(key);
|
|
if (inFlight) return inFlight;
|
|
const request = requestParsedAnimations(projectId, sourceFile).finally(() => {
|
|
// A superseded pre-write request must not evict the fresh post-write one.
|
|
if (inFlightParses.get(key) === request) inFlightParses.delete(key);
|
|
});
|
|
inFlightParses.set(key, request);
|
|
return request;
|
|
}
|
|
|
|
async function requestParsedAnimations(
|
|
projectId: string,
|
|
sourceFile: string,
|
|
): Promise<ParsedGsapAnimations | null> {
|
|
if (!isPreviewBooted(projectId) && !(await whenPreviewBooted(projectId))) return null;
|
|
try {
|
|
const res = await fetch(
|
|
`/api/projects/${encodeURIComponent(projectId)}/gsap-animations/${encodeURIComponent(sourceFile)}`,
|
|
// Always re-read the freshly-parsed source; no per-call timestamp (which
|
|
// would defeat caching forever and is a deterministic-render no-no).
|
|
{ cache: "no-store" },
|
|
);
|
|
if (!res.ok) return null;
|
|
const parsed: unknown = await res.json();
|
|
if (!hasAnimations(parsed)) return null;
|
|
// Studio-emitted pre-keyframe hold `set`s are an internal runtime detail (they
|
|
// hold an element's first keyframe before its tween). They must not surface as
|
|
// user animations — otherwise they pollute the keyframe cache / timeline diamonds.
|
|
return { ...parsed, animations: parsed.animations.filter((a) => !isStudioHoldSet(a)) };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read one composition file's tweens into the keyframe cache. Split out of the
|
|
* hook so the effect can run it per file without re-nesting the whole body.
|
|
*/
|
|
// fallow-ignore-next-line complexity
|
|
export async function populateKeyframeCacheFromAst(
|
|
projectId: string,
|
|
sf: string,
|
|
doc: Document | null | undefined,
|
|
): Promise<void> {
|
|
const parsed = await fetchParsedAnimations(projectId, sf);
|
|
if (!parsed) return;
|
|
const { elements, domClipChildren } = usePlayerStore.getState();
|
|
const mergedByElement = new Map<string, GsapKeyframesData<MergeableKeyframe>>();
|
|
const sourceByElement = new Map<string, GsapAnimation[]>();
|
|
for (const anim of parsed.animations) {
|
|
if (anim.hasUnresolvedKeyframes) continue;
|
|
if (isStaticPositionHold(anim)) continue;
|
|
const kfData = anim.keyframes ?? synthesizeFlatTweenKeyframes(anim);
|
|
if (!kfData) continue;
|
|
// Attribute the tween to every element it animates (handles class /
|
|
// group / descendant selectors, not just `#id`).
|
|
for (const id of resolveSelectorElementIds(anim.targetSelector, doc)) {
|
|
// kfData is already resolved (real keyframes OR a synthesized flat
|
|
// tween), so a flat tween joins the store like a keyframed one. No
|
|
// property-group filter: this map must cover every tween the cache
|
|
// below records, or expanded lanes have nothing to render.
|
|
sourceByElement.set(id, [...(sourceByElement.get(id) ?? []), anim]);
|
|
const { elStart, elDuration } = resolveClipTimingBasis(id, sf, elements, domClipChildren);
|
|
const clipKeyframes = toClipKeyframes(kfData.keyframes, anim, elStart, elDuration);
|
|
const existing = mergedByElement.get(id);
|
|
if (existing) {
|
|
existing.keyframes = deduplicateKeyframes([...existing.keyframes, ...clipKeyframes]);
|
|
} else {
|
|
mergedByElement.set(id, { ...kfData, keyframes: clipKeyframes });
|
|
}
|
|
}
|
|
}
|
|
replaceKeyframeCacheForFile(sf, mergedByElement, sourceByElement);
|
|
}
|