Skip to content

Commit c8f848f

Browse files
fix(studio): move and nudge an element without GSAP by its own CSS translate (#4799)
* fix(studio): move and nudge an element without GSAP by its own CSS translate * fix(studio): route a GSAP-free move through the on-element writer and meet the file checks * fix(studio): a drag press on a GSAP page leaves an element GSAP does not position alone * fix(studio): a press reads a percent or calc translate without touching the element * refactor(studio): import POSITION_CHANNELS from its owner instead of a re-export * test(studio): share the drag press helper in the start gesture tests * refactor(studio): split the translate length reader into term and sum * test(studio): cover each translate form the reader resolves or hands to Chrome * test(studio): bank the edit accuracy cases plain-CSS moves now pass baseline.json is the edit-accuracy-gate artifact of the CI run at 2a84c10, copied byte for byte: 60 newly passing cases, none regressed. The commits since change no behaviour (a reader split and tests).
1 parent c777f36 commit c8f848f

17 files changed

Lines changed: 1218 additions & 767 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { memo, useMemo, useRef, useState } from "react";
33
import { Move } from "../../icons/SystemIcons";
44
import { InspectorHeaderActions } from "./InspectorHeaderActions";
55
import { useStudioShellContext } from "../../contexts/StudioContext";
6-
import { readStudioBoxSize, readStudioPathOffset, readStudioRotation } from "./manualEdits";
6+
import { readStudioBoxSize, readStudioRotation } from "./manualEdits";
7+
import { readMoveOffset } from "./plainTranslate";
78
import {
89
buildElementInfoText,
910
EMPTY_STYLES,
@@ -199,7 +200,7 @@ export const PropertyPanel = memo(function PropertyPanel(props: PropertyPanelPro
199200
// selection) so the Timing section shows for pure-GSAP elements with no data-start.
200201
const sections = resolveEditingSections(domEditSelectionToFacts(element, gsapAnimations.length));
201202
const showEditableSections = element.capabilities.canEditStyles && sections.style;
202-
const manualOffset = readStudioPathOffset(element.element);
203+
const manualOffset = readMoveOffset(element.element);
203204
const manualSize = readStudioBoxSize(element.element);
204205
const resolvedWidth =
205206
manualSize.width > 0
@@ -209,7 +210,6 @@ export const PropertyPanel = memo(function PropertyPanel(props: PropertyPanelPro
209210
manualSize.height > 0
210211
? manualSize.height
211212
: (parsePxMetricValue(styles.height ?? "") ?? element.boundingBox.height);
212-
213213
const manualRotation = readStudioRotation(element.element);
214214

215215
const elStart = Number.parseFloat(element?.dataAttributes?.start ?? "0") || 0;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// @vitest-environment happy-dom
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import type { DomEditSelection } from "./domEditing";
4+
import type { GestureState } from "./domEditOverlayGestures";
5+
import { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures";
6+
7+
afterEach(() => {
8+
delete (window as { gsap?: unknown }).gsap;
9+
delete (window as { __timelines?: unknown }).__timelines;
10+
document.body.innerHTML = "";
11+
});
12+
13+
const PRESS = {
14+
clientX: 10,
15+
clientY: 10,
16+
pointerId: 1,
17+
button: 0,
18+
preventDefault() {},
19+
stopPropagation() {},
20+
currentTarget: { setPointerCapture() {} },
21+
};
22+
23+
/** Presses a drag on the element and returns the gesture it started. */
24+
function pressDrag(element: HTMLElement): GestureState | null {
25+
const opts = pressOptions(element);
26+
expect(
27+
createDomEditOverlayGestureHandlers(opts as never).startGesture("drag", PRESS as never),
28+
).toBe(true);
29+
return opts.gestureRef.current;
30+
}
31+
32+
function pressOptions(element: HTMLElement) {
33+
const ref = <T>(current: T) => ({ current });
34+
const selection = { element, capabilities: { canApplyManualOffset: true } };
35+
return {
36+
selectionRef: ref(selection as unknown as DomEditSelection),
37+
overlayRectRef: ref({ left: 0, top: 0, width: 240, height: 160, editScaleX: 1, editScaleY: 1 }),
38+
boxRef: ref(null),
39+
overlayRef: ref(null),
40+
iframeRef: ref(null),
41+
gestureRef: ref<GestureState | null>(null),
42+
rafPausedRef: ref(false),
43+
onManualDragStartRef: ref(vi.fn()),
44+
onBlockedMoveRef: ref(vi.fn()),
45+
};
46+
}
47+
48+
describe("a drag press on a page that loads GSAP", () => {
49+
it.each([
50+
["GSAP animates nothing", false],
51+
["GSAP animates only its parent", true],
52+
])(
53+
"%s: the press never asks GSAP about the element, so its translate stays CSS",
54+
(_, parentTween) => {
55+
const getProperty = vi.fn(() => 0);
56+
const set = vi.fn();
57+
const parent = document.createElement("div");
58+
const element = document.createElement("div");
59+
element.style.setProperty("translate", "40px 30px");
60+
parent.append(element);
61+
document.body.append(parent);
62+
const tween = { targets: () => [parent], vars: { x: 100 }, duration: () => 2 };
63+
const timelines = { main: { getChildren: () => (parentTween ? [tween] : []) } };
64+
Object.assign(window, { gsap: { getProperty, set }, __timelines: timelines });
65+
expect(pressDrag(element)?.pathOffsetMember?.plainTranslate).toBe(true);
66+
expect(getProperty).not.toHaveBeenCalled();
67+
expect(set).not.toHaveBeenCalled();
68+
expect(element.style.getPropertyValue("translate")).toBe("40px 30px");
69+
},
70+
);
71+
});
72+
73+
describe("a drag press on a centred element without GSAP", () => {
74+
it("starts from its -50% translate in px and leaves its style as it was", () => {
75+
const element = document.createElement("div");
76+
element.style.cssText =
77+
"position: absolute; left: 50%; top: 50%; width: 240px; height: 160px; translate: -50% -50%";
78+
document.body.append(element);
79+
const style = element.getAttribute("style");
80+
expect(pressDrag(element)?.pathOffsetMember?.initialOffset).toEqual({ x: -120, y: -80 });
81+
expect(element.getAttribute("style")).toBe(style);
82+
});
83+
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function startGroupDrag(
7070
selection: item.selection,
7171
element: item.element,
7272
rect: item.rect,
73+
gesture: "drag",
7374
});
7475
if (!result.ok) {
7576
restoreManualOffsetDragMembers(members);
@@ -151,10 +152,6 @@ export function startGesture(
151152
return false;
152153

153154
const size = readStudioBoxSize(sel.element);
154-
// Single-source rotation base = the live GSAP transform rotation plus any legacy
155-
// `--hf-studio-rotation` CSS var (old projects), so a rotate gesture starts from the
156-
// element's actual visual angle and commits an absolute angle to the timeline.
157-
const rotation = { angle: readGsapRotation(sel.element) + readStudioRotation(sel.element).angle };
158155
// The draft writes CSS width/height, so the resize base must be the CSS
159156
// layout size. offsetWidth/Height are transform-free; the overlay-rect
160157
// fallback (rect / editScale) includes the element's own GSAP scale and
@@ -198,6 +195,7 @@ export function startGesture(
198195
selection: sel,
199196
element: sel.element,
200197
rect,
198+
gesture: "drag",
201199
});
202200
if (!result.ok) {
203201
opts.onBlockedMoveRef.current(result.selection);
@@ -232,6 +230,10 @@ export function startGesture(
232230
}
233231
}
234232

233+
// Rotation base = GSAP's rotation plus the legacy `--hf-studio-rotation` var. A plain-translate
234+
// move never asks GSAP: reading a property makes it bake the CSS translate into its transform.
235+
const gsapRotation = pathOffsetMember?.plainTranslate ? 0 : readGsapRotation(sel.element);
236+
const rotation = { angle: gsapRotation + readStudioRotation(sel.element).angle };
235237
const overlayBounds = overlayEl?.getBoundingClientRect();
236238
const centerX = (overlayBounds?.left ?? 0) + rect.left + rect.width / 2;
237239
const centerY = (overlayBounds?.top ?? 0) + rect.top + rect.height / 2;

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
resumeGsapTimelines,
66
applyManualOffsetDragDraft,
77
applyManualOffsetDragMatrix,
8+
applyManualOffsetNudgeDraft,
89
createManualOffsetDragMember,
910
endManualOffsetDragMembers,
1011
invertManualOffsetDragMatrix,
@@ -566,3 +567,72 @@ describe("resumeGsapTimelines", () => {
566567
expect(() => resumeGsapTimelines(element)).not.toThrow();
567568
});
568569
});
570+
571+
describe("a move of an element GSAP does not position", () => {
572+
function plainBox(options: { gsapLoaded?: boolean } = {}) {
573+
const window = new Window();
574+
const element = window.document.createElement("div");
575+
window.document.body.append(element);
576+
element.style.setProperty("translate", "40px 30px");
577+
// A rect that never moves: the probe falls back to the 1:1 preview-scale mapping.
578+
element.getBoundingClientRect = () => new window.DOMRect(10, 20, 100, 50);
579+
const gsapCalls: string[] = [];
580+
if (options.gsapLoaded) {
581+
Object.assign(window, {
582+
gsap: { set: () => gsapCalls.push("set"), getProperty: () => gsapCalls.push("read") },
583+
});
584+
}
585+
const member = (gesture: "drag" | "nudge" = "drag") => {
586+
const result = createManualOffsetDragMember({
587+
key: "box",
588+
selection: { element } as never,
589+
element,
590+
rect: { left: 10, top: 20, width: 100, height: 50, editScaleX: 1, editScaleY: 1 },
591+
gesture,
592+
});
593+
if (!result.ok) throw new Error(result.reason);
594+
return result.member;
595+
};
596+
return { window, element, gsapCalls, member };
597+
}
598+
599+
it("drafts and drops the element's own translate in plain px, never touching GSAP", () => {
600+
const { element, gsapCalls, member } = plainBox({ gsapLoaded: true });
601+
const m = member();
602+
expect(m.plainTranslate).toBe(true);
603+
applyManualOffsetDragDraft(m, 90.25, 60);
604+
expect(element.style.getPropertyValue("translate")).toBe("130.25px 90px");
605+
expect(applyManualOffsetDragCommit(m, 90.25, 60)).toEqual({ x: 130.25, y: 90 });
606+
endManualOffsetDragMembers([m]);
607+
expect(element.style.getPropertyValue("translate")).toBe("130.25px 90px");
608+
expect(gsapCalls).toEqual([]);
609+
expect(element.hasAttribute("data-hf-drag-gsap-base-x")).toBe(false);
610+
});
611+
612+
it("starts a second move where the first dropped, while the first save is still in flight", () => {
613+
const { member } = plainBox();
614+
const first = member();
615+
applyManualOffsetDragCommit(first, 90, 60);
616+
const second = member();
617+
expect(second.initialOffset).toEqual({ x: 130, y: 90 });
618+
expect(applyManualOffsetDragCommit(second, 40, 0)).toEqual({ x: 170, y: 90 });
619+
});
620+
621+
it("nudges from the element's translate", () => {
622+
const { member } = plainBox();
623+
expect(applyManualOffsetNudgeDraft(member("nudge"), { x: 5, y: -1 })).toEqual({ x: 45, y: 29 });
624+
});
625+
626+
it("keeps the GSAP writer once GSAP renders the element's transform", () => {
627+
const { element, member } = plainBox({ gsapLoaded: true });
628+
Object.assign(element, { _gsap: { renderTransform: () => {} } });
629+
expect(member().plainTranslate).toBe(false);
630+
});
631+
632+
it("keeps the GSAP writer for an element a timeline hold positions", () => {
633+
const { window, element, member } = plainBox();
634+
const hold = { targets: () => [element], vars: { x: 40 }, duration: () => 0 };
635+
Object.assign(window, { __timelines: { main: { getChildren: () => [hold] } } });
636+
expect(member().plainTranslate).toBe(false);
637+
});
638+
});

0 commit comments

Comments
 (0)