Skip to content

Commit 5606b31

Browse files
fix(studio): moving one word moves only that word, not every word sharing its animation (#4705)
* fix(studio): move only the dragged word, not every word sharing its tween A drag on one element whose position tween also targets its siblings (a stagger on a shared class) now persists that element's own offset instead of rewriting the shared tween. The motion-path overlay leaves such a tween read-only, and every write of one drag records as one undo step. * fix(studio): keep the comment share flat in the word-move fix * fix(studio): save a shared-tween word's move as left/top on that word A CSS translate offset on an element a GSAP tween still drives gets folded into GSAP's transform, so render, preview and a second drag disagreed. left/top is never parsed by GSAP, adds to any offset the word already has, and is refused when it would not shift the box exactly. The resize anchor move takes the same channel, and tweens that miss the element or are shared with siblings no longer steer a drag that has its own tween. * refactor(studio): drop the untested own-tween selector override in the drag * fix(studio): keep a Layers-picked word's move and make a resize one undo step A word picked in the Layers panel is lifted to position:relative for display only; the move now reads the authored position, saves position:relative, and survives deselect. An anchored resize saves its move after the size, under the same undo key, and drops it when the size save fails. A percent left or top gets its own refusal message. * fix(studio): give the host canvas hook the per-word left/top move The host geometry hook now stages a shared-tween word's move with the same left/top offset as the editor, saved through the host's patch path, so a Desktop drag of one word moves only that word. * fix(studio): keep host word moves saving after one failed save The host canvas hook reset its save queue per save, as the host style hook does, so one conflict no longer pauses every later word move. A save error the patch path already showed is no longer covered by a generic failure toast.
1 parent c6b5edc commit 5606b31

28 files changed

Lines changed: 862 additions & 75 deletions

‎packages/studio/src/components/editor/MotionPathOverlay.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ export const MotionPathOverlay = memo(function MotionPathOverlay({
127127
// just in render, after the early returns) so the park-timer cleanup can key on
128128
// it: a pending park seek belongs to the OLD animation, so firing it after the
129129
// active animation changed would jump the playhead onto a stale keyframe.
130-
const animId = editableAnimationId(selectedGsapAnimations ?? [], geometry?.kind ?? "linear");
130+
const animId = editableAnimationId(
131+
selectedGsapAnimations ?? [],
132+
geometry?.kind ?? "linear",
133+
selection,
134+
);
131135
// Clear the debounced park timer on unmount AND whenever the active animation id
132136
// changes — not unmount-only, or a queued seek from the previous selection still
133137
// fires against the new one.

‎packages/studio/src/components/editor/commitPreflightHandles.test.tsx‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -377,45 +377,32 @@ describe("handles follow what Studio would commit", () => {
377377
expect(flags(seen.selection)[0]).toBe(true);
378378
});
379379

380-
it("refuses a group move carried by one shared tween, while a single member stays movable", async () => {
380+
// The per-element left/top channel supersedes the old refusal: each member saves its own move.
381+
it("lets a group move carried by one shared tween through as each member's own offset", async () => {
381382
const dots = [element("dot-1", "dot"), element("dot-2", "dot")];
382383
const preview = livePreview(dots, { y: 40 });
383384
const stagger = tween({ y: 40 }, { targetSelector: ".dot", method: "from" } as never);
384385
const [a, b] = dots.map(resolved);
385-
const { seen, overlay, spies, render } = await select([stagger], {
386+
const { seen, overlay, spies } = await select([stagger], {
386387
selection: a!,
387388
groups: [a!, b!],
388389
preview,
389390
});
390391

391-
expect(seen.groups.map((s) => flags(s)[0])).toEqual([false, false]);
392-
expect(seen.groups[0]?.capabilities.reasonIfDisabled).toBe(
393-
GSAP_EDIT_BLOCK_COPY["source-uneditable"],
394-
);
392+
expect(seen.groups.map((s) => flags(s)[0])).toEqual([true, true]);
395393
fire(overlay().querySelector(BOX)!, "pointerdown", { clientX: 150, clientY: 150 });
396-
expect(spies.onBlockedMove).toHaveBeenCalledTimes(1);
397-
const commitList = getAnimationsForElement(
398-
[stagger],
399-
{ id: "dot-1", selector: "#dot-1" },
400-
dots[0],
401-
);
394+
expect(spies.onBlockedMove).not.toHaveBeenCalled();
402395
const groupPreflight = { preflightOnly: true, group: true };
403396
const commit = tryGsapDragIntercept(
404397
a!,
405398
{ x: 0, y: 0 },
406-
commitList,
399+
[stagger],
407400
preview,
408401
vi.fn(),
409402
undefined,
410403
groupPreflight,
411404
);
412-
expect(await commit).toMatchObject({
413-
status: "blocked",
414-
detail: "live-position-no-source-tween",
415-
});
416-
417-
render({ selection: a!, preview });
418-
expect(flags(seen.selection)[0]).toBe(true);
405+
expect(await commit).toEqual({ status: "element-offset" });
419406
});
420407

421408
it("toasts once on the primary press of a blocked element, before any travel", async () => {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// @vitest-environment jsdom
2+
import { describe, expect, it } from "vitest";
3+
import { applyElementPositionOffset } from "./elementPositionOffset";
4+
5+
// jsdom has no layout: offsets follow left/top unless the box is anchored elsewhere.
6+
function word(style: string, anchoredRight = false): HTMLElement {
7+
const el = document.createElement("span");
8+
el.setAttribute("style", style);
9+
const px = (v: string) => Number.parseFloat(v) || 0;
10+
Object.defineProperties(el, {
11+
offsetLeft: { get: () => (anchoredRight ? 500 : 100 + px(el.style.left)) },
12+
offsetTop: { get: () => 200 + px(el.style.top) },
13+
offsetWidth: { get: () => 80 },
14+
offsetHeight: { get: () => 40 },
15+
});
16+
el.setAttribute("data-hf-drag-initial-offset-x", "0");
17+
el.setAttribute("data-hf-drag-initial-offset-y", "0");
18+
document.body.append(el);
19+
return el;
20+
}
21+
22+
describe("applyElementPositionOffset", () => {
23+
it("persists position for a static word the Layers panel lifted to relative", () => {
24+
const el = word("position: relative");
25+
el.setAttribute("data-hf-reveal-prior-pos", "static");
26+
expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([
27+
{ type: "inline-style", property: "position", value: "relative" },
28+
{ type: "inline-style", property: "left", value: "40px" },
29+
{ type: "inline-style", property: "top", value: "20px" },
30+
]);
31+
expect(el.hasAttribute("data-hf-reveal-prior-pos")).toBe(false);
32+
});
33+
34+
it("names percent positioning as the reason it refuses", () => {
35+
const el = word("position: relative; left: 10%; top: 0px");
36+
expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toBe("percent");
37+
expect(el.style.left).toBe("10%");
38+
});
39+
40+
it("moves a static element by relative left/top", () => {
41+
const el = word("");
42+
expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([
43+
{ type: "inline-style", property: "position", value: "relative" },
44+
{ type: "inline-style", property: "left", value: "40px" },
45+
{ type: "inline-style", property: "top", value: "20px" },
46+
]);
47+
});
48+
49+
it("adds to left/top the element already has, so a second drag accumulates", () => {
50+
const el = word("position: relative; left: 40px; top: 20px");
51+
expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([
52+
{ type: "inline-style", property: "left", value: "80px" },
53+
{ type: "inline-style", property: "top", value: "40px" },
54+
]);
55+
});
56+
57+
it("refuses, leaving the element untouched, when left does not move the box", () => {
58+
const el = word("position: absolute; right: 30px", true);
59+
expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toBe("anchored");
60+
expect([el.style.left, el.style.top, el.style.right]).toEqual(["", "", "30px"]);
61+
});
62+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { PatchOperation } from "../../utils/sourcePatcher";
2+
import { LAYER_REVEAL_PRIOR_POSITION_ATTR } from "../../player/lib/timelineElementHelpers";
3+
4+
interface LayoutBox {
5+
left: number;
6+
top: number;
7+
width: number;
8+
height: number;
9+
}
10+
11+
const layoutBox = (el: HTMLElement): LayoutBox => ({
12+
left: el.offsetLeft,
13+
top: el.offsetTop,
14+
width: el.offsetWidth,
15+
height: el.offsetHeight,
16+
});
17+
18+
function readDragStart(el: HTMLElement, axis: "x" | "y"): number {
19+
const value = Number.parseFloat(el.getAttribute(`data-hf-drag-initial-offset-${axis}`) ?? "");
20+
return Number.isFinite(value) ? value : 0;
21+
}
22+
23+
export type ElementOffsetRefusal = "percent" | "anchored";
24+
25+
/** Moves one element by adding to its own `left`/`top` (GSAP never parses them), or
26+
* names why not, leaving the element untouched. */
27+
export function applyElementPositionOffset(
28+
el: HTMLElement,
29+
gestureOffset: { x: number; y: number },
30+
): PatchOperation[] | ElementOffsetRefusal {
31+
const cs = el.ownerDocument.defaultView?.getComputedStyle(el);
32+
if (!cs) return "anchored";
33+
const dx = Math.round(gestureOffset.x - readDragStart(el, "x"));
34+
const dy = Math.round(gestureOffset.y - readDragStart(el, "y"));
35+
// A Layers-panel pick lifts a static element to relative for display only.
36+
const lifted = el.getAttribute(LAYER_REVEAL_PRIOR_POSITION_ATTR) === "static";
37+
const isStatic = lifted || !/^(relative|absolute|fixed|sticky)$/.test(cs.position);
38+
if (!isStatic && (cs.left.endsWith("%") || cs.top.endsWith("%"))) return "percent";
39+
const baseLeft = isStatic ? 0 : Number.parseFloat(cs.left) || 0;
40+
const baseTop = isStatic ? 0 : Number.parseFloat(cs.top) || 0;
41+
const previous = { position: el.style.position, left: el.style.left, top: el.style.top };
42+
const before = layoutBox(el);
43+
if (isStatic) el.style.position = "relative";
44+
el.style.left = `${Math.round(baseLeft) + dx}px`;
45+
el.style.top = `${Math.round(baseTop) + dy}px`;
46+
const after = layoutBox(el);
47+
const shiftedExactly =
48+
Math.abs(after.left - before.left - dx) <= 1 &&
49+
Math.abs(after.top - before.top - dy) <= 1 &&
50+
Math.abs(after.width - before.width) <= 1 &&
51+
Math.abs(after.height - before.height) <= 1;
52+
if (!shiftedExactly) {
53+
Object.assign(el.style, previous);
54+
return "anchored";
55+
}
56+
if (lifted) el.removeAttribute(LAYER_REVEAL_PRIOR_POSITION_ATTR);
57+
return [
58+
...(isStatic
59+
? [{ type: "inline-style", property: "position", value: "relative" } as const]
60+
: []),
61+
{ type: "inline-style", property: "left", value: el.style.left },
62+
{ type: "inline-style", property: "top", value: el.style.top },
63+
];
64+
}

‎packages/studio/src/components/editor/motionPathCommit.test.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @vitest-environment jsdom
12
import { describe, it, expect, vi } from "vitest";
23
import type { GsapAnimation } from "@hyperframes/parsers/gsap-parser";
34
import { editableAnimationId } from "./motionPathSelection";
@@ -19,10 +20,13 @@ const anim = (over: Partial<GsapAnimation>): GsapAnimation =>
1920
...over,
2021
}) as GsapAnimation;
2122

23+
// The element the default `#el` tweens target.
24+
const elSel = { id: "el" } as never;
25+
2226
describe("editableAnimationId", () => {
2327
it("picks the arc animation for an arc path", () => {
2428
const arc = anim({ id: "arc1", arcPath: { enabled: true, autoRotate: false, segments: [] } });
25-
expect(editableAnimationId([anim({ id: "other" }), arc], "arc")).toBe("arc1");
29+
expect(editableAnimationId([anim({ id: "other" }), arc], "arc", elSel)).toBe("arc1");
2630
});
2731

2832
it("picks a position-keyframe animation for a linear path", () => {
@@ -34,7 +38,7 @@ describe("editableAnimationId", () => {
3438
keyframes: [{ percentage: 0, properties: { x: 0, y: 0 } }],
3539
} as never,
3640
});
37-
expect(editableAnimationId([kf], "linear")).toBe("kf1");
41+
expect(editableAnimationId([kf], "linear", elSel)).toBe("kf1");
3842
});
3943

4044
it("returns null for dynamic (unresolved) tweens — read-only", () => {
@@ -43,7 +47,7 @@ describe("editableAnimationId", () => {
4347
arcPath: { enabled: true, autoRotate: false, segments: [] },
4448
hasUnresolvedKeyframes: true,
4549
});
46-
expect(editableAnimationId([dyn], "arc")).toBeNull();
50+
expect(editableAnimationId([dyn], "arc", elSel)).toBeNull();
4751
});
4852

4953
it("returns null for non-literal (helper) provenance — read-only", () => {
@@ -52,11 +56,27 @@ describe("editableAnimationId", () => {
5256
arcPath: { enabled: true, autoRotate: false, segments: [] },
5357
provenance: { kind: "helper" } as never,
5458
});
55-
expect(editableAnimationId([helper], "arc")).toBeNull();
59+
expect(editableAnimationId([helper], "arc", elSel)).toBeNull();
60+
});
61+
62+
it("leaves a tween that also moves the element's siblings read-only", () => {
63+
document.body.innerHTML = `<span class="w" data-hf-id="w0">How</span><span class="w" data-hf-id="w1">we</span>`;
64+
const word = document.querySelector<HTMLElement>('[data-hf-id="w0"]')!;
65+
const stagger = anim({
66+
id: "stagger",
67+
targetSelector: ".w",
68+
propertyGroup: "position",
69+
keyframes: {
70+
format: "percentage",
71+
keyframes: [{ percentage: 0, properties: { x: 0, y: 0 } }],
72+
} as never,
73+
});
74+
const wordSel = { selector: ".w", selectorIndex: 0, hfId: "w0", element: word } as never;
75+
expect(editableAnimationId([stagger], "linear", wordSel)).toBeNull();
5676
});
5777

5878
it("returns null when nothing matches", () => {
59-
expect(editableAnimationId([anim({ id: "x" })], "linear")).toBeNull();
79+
expect(editableAnimationId([anim({ id: "x" })], "linear", elSel)).toBeNull();
6080
});
6181
});
6282

‎packages/studio/src/components/editor/motionPathSelection.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import type { GsapAnimation } from "@hyperframes/parsers/gsap-parser";
77
import type { DomEditSelection } from "./domEditing";
8-
import { writeTargetSelector } from "../../hooks/gsapShared";
8+
import { tweenTargetsElement, writeTargetSelector } from "../../hooks/gsapShared";
99

1010
/**
1111
* The selector the overlay both MEASURES the element by and authors a new
@@ -24,13 +24,20 @@ export function selectorFor(sel: DomEditSelection | null): string | null {
2424

2525
/** The animation whose path is editable on-canvas: literal, statically resolved,
2626
* and matching the rendered geometry kind. Returns null when the path can only
27-
* be displayed (dynamic/helper tweens) — those nodes stay read-only. */
27+
* be displayed (dynamic/helper tweens, or a tween that also moves other
28+
* elements) — those nodes stay read-only and a press drags the element itself. */
2829
export function editableAnimationId(
2930
animations: GsapAnimation[],
3031
kind: "linear" | "arc",
32+
sel: DomEditSelection | null,
3133
): string | null {
34+
const own = selectorFor(sel);
3235
const ok = (a: GsapAnimation) =>
33-
!a.hasUnresolvedKeyframes && !a.hasUnresolvedSelector && !a.provenance;
36+
!a.hasUnresolvedKeyframes &&
37+
!a.hasUnresolvedSelector &&
38+
!a.provenance &&
39+
!!own &&
40+
tweenTargetsElement(a.targetSelector, own, sel?.element);
3441
if (kind === "arc") return animations.find((a) => a.arcPath?.enabled && ok(a))?.id ?? null;
3542
const a = animations.find(
3643
(anim) =>

‎packages/studio/src/components/editor/useLayerRevealOverride.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ describe("liftElementToTop / restoreLiftedElement", () => {
5656
expect(el.style.zIndex).toBe("");
5757
});
5858

59+
it("keeps a position a move authored while the element was lifted", () => {
60+
const el = makeEl();
61+
const lift = liftElementToTop(el)!;
62+
el.removeAttribute("data-hf-reveal-prior-pos");
63+
restoreLiftedElement(el, lift);
64+
expect(el.style.position).toBe("relative");
65+
});
66+
5967
it("a z-reorder commit consumes the lift: restore becomes a no-op", () => {
6068
const el = makeEl("3", "absolute");
6169
const lift = liftElementToTop(el)!;

‎packages/studio/src/components/editor/useLayerRevealOverride.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,13 @@ export function restoreLiftedElement(element: HTMLElement, lift: RevealLift): vo
271271
}
272272
return;
273273
}
274+
const positionStillLifted = element.hasAttribute(LAYER_REVEAL_PRIOR_POSITION_ATTR);
274275
element.removeAttribute(LAYER_REVEAL_PRIOR_Z_ATTR);
275276
element.removeAttribute(LAYER_REVEAL_PRIOR_POSITION_ATTR);
276277
if (element.style.zIndex === LAYER_REVEAL_LIFT_Z) {
277278
restoreInline(element, "z-index", lift.priors.zIndex);
278279
}
279-
if (lift.positionLifted && element.style.position === "relative") {
280+
if (lift.positionLifted && positionStillLifted && element.style.position === "relative") {
280281
restoreInline(element, "position", lift.priors.position);
281282
}
282283
}

‎packages/studio/src/hooks/domEditPersistFailure.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ function getSelectionLabel(selection: DomEditPersistFailureSelection): string {
4646
return selection.label || selection.selector || selection.id || "this element";
4747
}
4848

49+
export function wasAlreadyToasted(error: unknown): boolean {
50+
return (
51+
(error instanceof DomEditPersistUnsafeValueError || error instanceof StudioSaveHttpError) &&
52+
error.alreadyToasted
53+
);
54+
}
55+
4956
export function reportDomEditPersistFailure(
5057
selection: DomEditPersistFailureSelection,
5158
operations: PatchOperation[],
@@ -59,12 +66,7 @@ export function reportDomEditPersistFailure(
5966
error: detail,
6067
});
6168

62-
const wasAlreadyToasted =
63-
(error instanceof DomEditPersistUnsafeValueError || error instanceof StudioSaveHttpError) &&
64-
error.alreadyToasted;
65-
if (wasAlreadyToasted) {
66-
return;
67-
}
69+
if (wasAlreadyToasted(error)) return;
6870

6971
showToast(`Couldn't save "${getSelectionLabel(selection)}": ${detail}`, "error");
7072
}

0 commit comments

Comments
 (0)