Files
hyperframes/packages/studio/src/utils/keyframeSelection.ts
Carlos Alcaraz GregorandCarlos Alcaraz f1a50e03ea fix(studio): delete only the active element's selected keyframes (#1453)
Pressing Delete with keyframes multi-selected removed keyframes from the
wrong element. selectedKeyframes holds "<elementId>:<percentage>" keys and
can outlive the element it was built on (a clip click, keyframe click, layers
selection, or keyframe context menu changes the active element without clearing
it, and a shift-selection can span elements). deleteSelectedKeyframes parsed
only the percentage from each key and applied it to the active animation,
ignoring which element each selected keyframe belonged to, so a stale selection
deleted keyframes the user never targeted on the active element.

Extract selectedKeyframePercentagesForElement, which keeps only the percentages
whose key matches the active element id, and route the delete through it. The
common case (all selected keyframes on the active element) is unchanged; stale
cross-element keys are skipped instead of mis-applied.

Co-authored-by: Carlos Alcaraz <193642530+calcarazgre646@users.noreply.github.com>
2026-06-14 23:19:28 -04:00

30 lines
1.2 KiB
TypeScript

/**
* Resolves which keyframe percentages a bulk operation should act on.
*
* `selectedKeyframes` holds `"<elementId>:<percentage>"` keys and can contain
* keyframes from more than one element — e.g. a shift-selection made before the
* active element changed (via a keyframe click, a clip click, the layers panel,
* or the keyframe context menu). A bulk delete only targets the active
* element's animation, so keys belonging to other elements must be dropped;
* otherwise their percentages get applied to the active element and remove
* keyframes the user never selected on it.
*
* The element id is everything before the final `:` so element ids that happen
* to contain `:` are handled correctly.
*/
export function selectedKeyframePercentagesForElement(
selectedKeyframes: ReadonlySet<string>,
activeElementId: string | null,
): number[] {
if (!activeElementId) return [];
const percentages: number[] = [];
for (const key of selectedKeyframes) {
const separator = key.lastIndexOf(":");
if (separator < 0) continue;
if (key.slice(0, separator) !== activeElementId) continue;
const percentage = Number(key.slice(separator + 1));
if (Number.isFinite(percentage)) percentages.push(percentage);
}
return percentages;
}