fix(studio): a 3D slider drag previews live in a sealed preview (#8148)

This commit is contained in:
Miguel Ángel
2026-11-18 12:12:09 +00:00
committed by GitHub
parent 1a82f6e9d6
commit 75992d82ad
8 changed files with 145 additions and 2 deletions
@@ -0,0 +1,48 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { handleFor } from "../editingHandles";
import { gsapSet, parseGsapSet } from "./gsapSet";
afterEach(() => {
document.body.innerHTML = "";
Reflect.deleteProperty(window, "gsap");
});
function box() {
document.body.innerHTML = `<div id="box"></div>`;
return document.getElementById("box")!;
}
describe("gsap-set", () => {
it("sets the dragged values on the element in the frame", () => {
const set = vi.fn();
Object.assign(window, { gsap: { set } });
const el = box();
expect(gsapSet({ handle: handleFor(el), props: { rotationY: 30, z: -40 } })).toEqual({
applied: true,
});
expect(set).toHaveBeenCalledExactlyOnceWith(el, { rotationY: 30, z: -40 });
});
it("does nothing when the frame has no GSAP or the element is gone", () => {
const el = box();
const handle = handleFor(el);
expect(gsapSet({ handle, props: { x: 1 } })).toEqual({ applied: false });
Object.assign(window, { gsap: { set: vi.fn() } });
el.remove();
expect(gsapSet({ handle, props: { x: 1 } })).toEqual({ applied: false });
});
it("takes only finite values on the transform channels Studio previews", () => {
const handle = handleFor(box());
expect(parseGsapSet({ handle, props: { x: 4, opacity: 0.5 } })).toEqual({
handle,
props: { x: 4, opacity: 0.5 },
});
for (const props of [{}, { x: Number.NaN }, { x: "4" }, { onComplete: 1 }, [1]])
expect(parseGsapSet({ handle, props })).toBeNull();
expect(parseGsapSet({ handle: "#box", props: { x: 1 } })).toBeNull();
expect(parseGsapSet({ handle, props: { x: 1 }, extra: true })).toBeNull();
});
});
@@ -0,0 +1,48 @@
// Studio's live value preview (a 3D-transform slider mid-drag) in a frame it cannot script.
import { EDITING_ELEMENT_HANDLE, type EditingElementHandle } from "../editingElement";
import { elementForHandle } from "../editingHandles";
const GSAP_SET_CHANNELS = new Set([
"x",
"y",
"z",
"rotation",
"rotationX",
"rotationY",
"rotationZ",
"transformPerspective",
"scale",
"scaleX",
"scaleY",
"opacity",
"width",
"height",
]);
export type GsapSetParams = { handle: EditingElementHandle; props: Record<string, number> };
const isRecord = (value: unknown): value is Record<string, unknown> =>
Object.prototype.toString.call(value) === "[object Object]";
export function parseGsapSet(params: unknown): GsapSetParams | null {
if (!isRecord(params) || Object.keys(params).sort().join() !== "handle,props") return null;
const { handle, props } = params;
if (typeof handle !== "string" || !EDITING_ELEMENT_HANDLE.test(handle) || !isRecord(props))
return null;
const entries = Object.entries(props);
const valid = entries.every(
([channel, value]) =>
GSAP_SET_CHANNELS.has(channel) && typeof value === "number" && Number.isFinite(value),
);
return valid && entries.length > 0 ? { handle, props: props as Record<string, number> } : null;
}
type FrameGsap = { set?: (target: Element, vars: Record<string, number>) => void };
export function gsapSet({ handle, props }: GsapSetParams): { applied: boolean } {
const element = elementForHandle(handle);
const gsap = (window as { gsap?: FrameGsap }).gsap;
if (!element || typeof gsap?.set !== "function") return { applied: false };
gsap.set(element, { ...props });
return { applied: true };
}
@@ -200,6 +200,7 @@ describe("installEditingAgent", () => {
"max-z-index",
"crop-lift",
"edit-element",
"gsap-set",
],
},
});
@@ -39,6 +39,7 @@ import { gestureStart } from "./editingGestureStart";
import { gsapSnapshot } from "./editing/gsapSnapshot";
import { motionPathHome } from "./editing/motionPathHome";
import { cropLift } from "./editing/cropLift";
import { gsapSet } from "./editing/gsapSet";
import {
getLogicalOpacity,
markedForStudio,
@@ -220,6 +221,7 @@ const HANDLERS: Handlers = {
"max-z-index": () => maxZIndex(),
"crop-lift": cropLift,
"edit-element": editElement,
"gsap-set": gsapSet,
};
function serve(port: MessagePort): void {
@@ -103,6 +103,7 @@ describe("createEditingPortClient", () => {
"max-z-index",
"crop-lift",
"edit-element",
"gsap-set",
],
});
} finally {
+4
View File
@@ -28,6 +28,7 @@ import type { EditingElementBox, EditingElementGeometry } from "./editingGeometr
import type { EditingGestureStart } from "./editingGestureStart";
import type { GsapSnapshot } from "./editing/gsapSnapshot";
import type { MotionPathFrameHome } from "./editing/motionPathHome";
import { parseGsapSet, type GsapSetParams } from "./editing/gsapSet";
import type { EditingLayer } from "./editingLayers";
import type { RuntimeTimelineMessage } from "./types";
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
@@ -176,6 +177,7 @@ export const EDITING_REQUEST_KINDS = [
"max-z-index",
"crop-lift",
"edit-element",
"gsap-set",
] as const;
export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number];
@@ -310,6 +312,7 @@ export interface EditingRequests {
result: { lifted: boolean; clipPath: string | null };
};
"edit-element": { params: EditElementParams; result: EditElementResult };
"gsap-set": { params: GsapSetParams; result: { applied: boolean } };
}
export type EditingParams<K extends EditingRequestKind> = EditingRequests[K]["params"];
export type EditingResult<K extends EditingRequestKind> = EditingRequests[K]["result"];
@@ -518,6 +521,7 @@ const PARAM_PARSERS: {
? { handle: params.handle, lift: params.lift }
: null,
"edit-element": parseEditElement,
"gsap-set": parseGsapSet,
};
/** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */
@@ -0,0 +1,35 @@
// @vitest-environment happy-dom
import { expect, it, vi } from "vitest";
const { previewRequest } = vi.hoisted(() => ({
previewRequest: vi.fn(async () => ({ applied: true })),
}));
vi.mock("../../player/lib/previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("../../player/lib/previewChannel")>()),
previewFrameOpaque: () => true,
previewRequest,
}));
import { createGsapLivePreview } from "./gsapLivePreview";
import type { DomEditSelection } from "./domEditingTypes";
it("sends a sealed preview's dragged values to the frame through the editing port", () => {
const iframe = document.createElement("iframe");
const preview = createGsapLivePreview({ current: iframe });
preview({ handle: "0123456789abcdef:7" } as DomEditSelection, { rotationY: 30 });
expect(previewRequest).toHaveBeenCalledExactlyOnceWith(iframe, "gsap-set", {
handle: "0123456789abcdef:7",
props: { rotationY: 30 },
});
});
it("sends nothing for a sealed selection the frame never named", () => {
previewRequest.mockClear();
const preview = createGsapLivePreview({ current: document.createElement("iframe") });
preview({ id: "card" } as DomEditSelection, { x: 10 });
expect(previewRequest).not.toHaveBeenCalled();
});
@@ -1,6 +1,6 @@
import type { DomEditSelection } from "./domEditingTypes";
import { findPreviewNode } from "./domEditingElement";
import { previewFrameOpaque } from "../../player/lib/previewChannel";
import { previewFrameOpaque, previewRequest } from "../../player/lib/previewChannel";
/**
* Build the "live preview" callback the 3D-transform sub-view fires while a
@@ -13,7 +13,11 @@ import { previewFrameOpaque } from "../../player/lib/previewChannel";
export function createGsapLivePreview(iframeRef: { readonly current: HTMLIFrameElement | null }) {
return (el: DomEditSelection, props: Record<string, number>) => {
const iframe = iframeRef.current;
if (!iframe || previewFrameOpaque(iframe)) return;
if (!iframe) return;
if (previewFrameOpaque(iframe)) {
if (el.handle) void previewRequest(iframe, "gsap-set", { handle: el.handle, props });
return;
}
const win = iframe?.contentWindow as
| { gsap?: { set: (t: Element, v: Record<string, number>) => void } }
| null