Files
hyperframes/packages/studio/src/hooks/gsapKeyframeCommit.ts
T
Miguel Ángel 852782bbb0 feat(studio): track completed preview editing gestures (#4916)
* feat(studio): track completed preview editing gestures

* fix(studio): preserve gesture receipts and suppress duplicate usage

* fix(studio): count semantic keyframe writes once per gesture

* fix(studio): isolate recording state during pending saves

* fix(studio): count marquee selection changes from owner receipts

* fix(studio): retain accurate empty marquee receipts

* refactor(studio): simplify recording transitions and selection tests

* refactor(studio): share recording timing and marquee fixtures

* fix(studio): preserve recording targets and batch attribution

* fix(studio): retain per-member telemetry suppression

* style(studio): format batch receipt attribution

* test(studio): match writer receipts and animation metadata

* docs(studio): clarify paused position save rejection

* fix(studio): replace telemetry route names with route identity

* style(studio): format route identity regression

* Use random tab-local route identities in Studio telemetry

* Preserve route identities inherited through session storage

* Separate route storage validation from token creation
2026-10-03 00:13:18 +00:00

83 lines
2.8 KiB
TypeScript

import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { absoluteToPercentageForAnimation, findTweenAtTime } from "../utils/globalTimeCompiler";
import { PROPERTY_DEFAULTS, selectorFromSelection, writeTargetSelector } from "./gsapShared";
import { roundToCenti } from "../utils/rounding";
import type { CommitMutation } from "./gsapScriptCommitTypes";
export async function commitKeyframeAtTimeImpl(
selection: DomEditSelection,
absoluteTime: number,
animations: GsapAnimation[],
properties: Record<string, number | string>,
commitMutation: CommitMutation,
): Promise<void> {
// Matching an authored tween is a string compare against what the author
// wrote, so it keeps using the selection's own selector; the NEW tween below
// is authored with the one-element form instead.
const selector = selectorFromSelection(selection);
if (!selector) return;
const tween = findTweenAtTime(absoluteTime, animations, selector);
if (tween) {
const pct = absoluteToPercentageForAnimation(absoluteTime, tween);
if (pct === null) return;
const hasExplicitKeyframes = !!tween.keyframes && tween.keyframes.keyframes.length > 0;
if (!hasExplicitKeyframes) {
await commitMutation(
selection,
{ type: "convert-to-keyframes", animationId: tween.id },
{ label: "Convert to keyframes", keyframeTelemetry: false, skipReload: true },
);
}
const backfillDefaults: Record<string, number | string> = {};
for (const key of Object.keys(properties)) {
backfillDefaults[key] = PROPERTY_DEFAULTS[key] ?? 0;
}
await commitMutation(
selection,
{
type: "add-keyframe",
animationId: tween.id,
percentage: pct,
properties,
backfillDefaults,
},
{
label: `Add keyframe at ${roundToCenti(absoluteTime)}s`,
coalesceKey: `keyframe:${tween.id}:${pct}`,
softReload: true,
},
);
} else {
// Null means the live DOM could not prove any one-element form. Falling
// back to the author's own selector would write the group-collapsing
// target this narrowing exists to prevent, so the keyframe is dropped
// instead (see writeTargetSelector).
const target = writeTargetSelector(selection);
if (!target) return;
const defaultDuration = 0.5;
await commitMutation(
selection,
{
type: "add-with-keyframes" as const,
targetSelector: target,
position: absoluteTime,
duration: defaultDuration,
keyframes: [
{ percentage: 0, properties },
{ percentage: 100, properties },
],
},
{
label: `New animation at ${roundToCenti(absoluteTime)}s`,
softReload: true,
},
);
}
}