Files
hyperframes/packages/studio/src/hooks/gsapKeyframeCommit.ts
T
Miguel Angel Simon Sierra 1f3fd2800c fix(studio): give the motion path and the fallbacks a one-element target
The narrowing this branch adds missed the motion-path overlay, and every
caller that could not narrow fell back to the exact bare class the narrowing
exists to replace.

- motionPathSelection.selectorFor now goes through writeTargetSelector. It
  feeds both the geometry read and the "set destination" write, so a class
  sibling measured its home off the FIRST sibling and then authored
  add-motion-path onto all of them. The toolbar toggle hides when no
  one-element form exists rather than arming a press that is dropped.
- The five new-tween writers that fell back to the selection's own selector
  now drop the commit instead. A gesture that does not persist reverts on the
  next reload; a tween silently aimed at five elements does not.
- tweenTargetsElement only follows the DOM to a target that matches exactly
  one element. A target the element merely shares with its siblings is a group
  tween, and these callers mutate what they find, so an individual nudge was
  rewriting the group's own tween and moving all five.
2026-07-28 21:21:37 +02:00

92 lines
2.9 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";
type CommitFn = (
selection: DomEditSelection,
mutation: Record<string, unknown>,
options: {
label: string;
coalesceKey?: string;
softReload?: boolean;
skipReload?: boolean;
},
) => Promise<void>;
export async function commitKeyframeAtTimeImpl(
selection: DomEditSelection,
absoluteTime: number,
animations: GsapAnimation[],
properties: Record<string, number | string>,
commitMutation: CommitFn,
): 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", 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,
},
);
}
}