mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(studio): a sealed scale resize no longer shows its old size for a frame after the drop (#8616)
* fix(studio): a sealed scale resize no longer shows its old size for a frame after the drop * fix(core): gsap-set draws its drafts even when the set itself can't apply
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// Studio's live value preview (a 3D-transform slider mid-drag) in a frame it cannot script.
|
||||
import { drawDrafts, rebaseDrawnPosition } from "../editingDraft";
|
||||
import { EDITING_ELEMENT_HANDLE, type EditingElementHandle } from "../editingElement";
|
||||
import { elementForHandle } from "../editingHandles";
|
||||
import { rebaseDrawnPosition } from "../editingDraft";
|
||||
import type { EditingDraft } from "../editingPort";
|
||||
|
||||
const GSAP_SET_CHANNELS = new Set([
|
||||
"x",
|
||||
@@ -20,7 +21,11 @@ const GSAP_SET_CHANNELS = new Set([
|
||||
"height",
|
||||
]);
|
||||
|
||||
export type GsapSetParams = { handle: EditingElementHandle; props: Record<string, number> };
|
||||
export type GsapSetParams = {
|
||||
handle: EditingElementHandle;
|
||||
props: Record<string, number>;
|
||||
drafts?: EditingDraft[];
|
||||
};
|
||||
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
||||
Object.prototype.toString.call(value) === "[object Object]";
|
||||
@@ -40,7 +45,8 @@ export function parseGsapSet(params: unknown): GsapSetParams | null {
|
||||
|
||||
type FrameGsap = { set?: (target: Element, vars: Record<string, number>) => void };
|
||||
|
||||
export function gsapSet({ handle, props }: GsapSetParams): { applied: boolean } {
|
||||
export function gsapSet({ handle, props, drafts }: GsapSetParams): { applied: boolean } {
|
||||
if (drafts) drawDrafts(drafts);
|
||||
const element = elementForHandle(handle);
|
||||
const gsap = (window as { gsap?: FrameGsap }).gsap;
|
||||
if (!element || typeof gsap?.set !== "function") return { applied: false };
|
||||
|
||||
@@ -698,6 +698,45 @@ describe("installEditingAgent", () => {
|
||||
expect(spun.style.transform).toBe(value);
|
||||
});
|
||||
|
||||
it("ends a drawn size and sets GSAP values in one request, the size ended first", async () => {
|
||||
document.body.innerHTML = `<div id="box"></div>`;
|
||||
const box = document.getElementById("box")!;
|
||||
const handle = handleFor(box);
|
||||
const widthAtSet: string[] = [];
|
||||
Object.assign(window, { gsap: { set: () => widthAtSet.push(box.style.width) } });
|
||||
const port = connectedPort();
|
||||
const size = { width: 307, height: 204 };
|
||||
const drawn = await ask(port, {
|
||||
v: 1,
|
||||
id: 1,
|
||||
kind: "draft",
|
||||
params: { drafts: [{ handle, translate: null, size }] },
|
||||
});
|
||||
expect(drawn).toMatchObject({ ok: true, result: { gsapSetTakesDrafts: true } });
|
||||
expect(box.style.width).toBe("307px");
|
||||
|
||||
const ending = [{ handle, translate: null, size: null }];
|
||||
const props = { scaleX: 1.9, scaleY: 1.9 };
|
||||
const set = await ask(port, {
|
||||
v: 1,
|
||||
id: 2,
|
||||
kind: "gsap-set",
|
||||
params: { handle, props, drafts: ending },
|
||||
});
|
||||
const malformed = [{ handle, translate: null, size: "none" }];
|
||||
const refused = await ask(port, {
|
||||
v: 1,
|
||||
id: 3,
|
||||
kind: "gsap-set",
|
||||
params: { handle, props, drafts: malformed },
|
||||
});
|
||||
|
||||
expect(set).toMatchObject({ id: 2, ok: true, result: { applied: true } });
|
||||
expect(widthAtSet).toEqual([""]);
|
||||
expect(refused).toMatchObject({ id: 3, ok: false, error: { code: "invalid-params" } });
|
||||
Reflect.deleteProperty(window, "gsap");
|
||||
});
|
||||
|
||||
// fails without the fix: the frame refused a move routed through its GSAP, so a sealed drag drew a CSS translate.
|
||||
it("moves a GSAP-held element's x/y through the port", async () => {
|
||||
const port = connectedPort();
|
||||
|
||||
@@ -204,7 +204,7 @@ const HANDLERS: Handlers = {
|
||||
query: ({ selector }) => queryElements(selector),
|
||||
geometry: ({ handles, children }) => elementGeometry(handles, children),
|
||||
layers: ({ group, offset }) => layersOf(group, offset),
|
||||
draft: ({ drafts }) => drawDrafts(drafts),
|
||||
draft: ({ drafts }) => ({ ...drawDrafts(drafts), gsapSetTakesDrafts: true as const }),
|
||||
"gesture-start": ({ handles }) => gestureStart(handles),
|
||||
"gesture-base": ({ handles }) => ({ elements: handles.map(gestureBase) }),
|
||||
"console-errors": ({ from }) => {
|
||||
|
||||
@@ -339,7 +339,10 @@ export interface EditingRequests {
|
||||
result: { layers: EditingLayer[]; truncated: boolean; missingGroup: boolean };
|
||||
};
|
||||
/** Gesture drafts in order, up to MAX_DESCRIBE_HANDLES; `drawn` counts the elements still here. */
|
||||
draft: { params: { drafts: EditingDraft[] }; result: EditingDraftReply };
|
||||
draft: {
|
||||
params: { drafts: EditingDraft[] };
|
||||
result: EditingDraftReply & { gsapSetTakesDrafts?: true };
|
||||
};
|
||||
"gesture-start": {
|
||||
params: { handles: EditingElementHandle[] };
|
||||
result: { elements: Array<EditingGestureStart | null>; snap: EditingElementBox[] };
|
||||
@@ -573,6 +576,14 @@ function draftPropsWithOneXYWriter(value: unknown, translate: unknown): boolean
|
||||
return entries.every(isDraftValue);
|
||||
}
|
||||
|
||||
function gsapSetWithDrafts(params: unknown): GsapSetParams | null {
|
||||
if (!isPlainRecord(params) || !("drafts" in params)) return parseGsapSet(params);
|
||||
const { drafts, ...set } = params;
|
||||
const parsed = parseGsapSet(set);
|
||||
const drawn = draftList({ drafts });
|
||||
return parsed && drawn ? { ...parsed, drafts: drawn.drafts } : null;
|
||||
}
|
||||
|
||||
function draftList(params: unknown): EditingParams<"draft"> | null {
|
||||
if (!hasKeys(params, "drafts") || !Array.isArray(params.drafts)) return null;
|
||||
const { drafts } = params;
|
||||
@@ -673,7 +684,7 @@ const PARAM_PARSERS: {
|
||||
"edit-element": parseEditElement,
|
||||
"stacking-family": (params) =>
|
||||
hasKeys(params, "handle") && isHandle(params.handle) ? { handle: params.handle } : null,
|
||||
"gsap-set": parseGsapSet,
|
||||
"gsap-set": gsapSetWithDrafts,
|
||||
"patch-tweens": parsePatchTweensRequest,
|
||||
"soft-reload": parseSoftReload,
|
||||
"hold-scenes": parseHoldScenes,
|
||||
|
||||
@@ -30,6 +30,7 @@ interface Queue {
|
||||
sending: boolean;
|
||||
sent: Array<() => void>;
|
||||
sendQueued: boolean;
|
||||
gsapSetTakesDrafts?: boolean;
|
||||
}
|
||||
|
||||
const standIns = new WeakMap<Element, { iframe: HTMLIFrameElement; handle: string }>();
|
||||
@@ -70,6 +71,7 @@ function send(iframe: HTMLIFrameElement, queue: Queue): void {
|
||||
queue.sending = true;
|
||||
void previewRequest(iframe, "draft", { drafts })
|
||||
.then((reply) => {
|
||||
if (reply?.gsapSetTakesDrafts) queue.gsapSetTakesDrafts = true;
|
||||
for (const op of drafts) {
|
||||
const entry = queue.drawn.get(op.handle);
|
||||
const draft = reply?.drafts?.[op.handle];
|
||||
@@ -109,10 +111,9 @@ export function frameDraftTranslate(member: DraftedMember, offset: Point) {
|
||||
return { x: newX, y: newY, route: "gsap" as const };
|
||||
}
|
||||
|
||||
/** Draws `part` of the stand-in's draft on its frame element; any other element is left alone. */
|
||||
export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
|
||||
function nextDraw(element: Element, part: Partial<Drawn>) {
|
||||
const target = standIns.get(element);
|
||||
if (!target) return;
|
||||
if (!target) return null;
|
||||
const queue = queueFor(target.iframe);
|
||||
const prior = queue.drawn.get(target.handle);
|
||||
const entry: Entry = { translate: null, size: null, gesture: {}, ...prior, ...part };
|
||||
@@ -127,6 +128,26 @@ export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
|
||||
...(entry.props && { props: entry.props }),
|
||||
};
|
||||
opGesture.set(op, entry.gesture);
|
||||
return { target, queue, op };
|
||||
}
|
||||
|
||||
export function previewDraftRidingGsapSet(
|
||||
element: Element,
|
||||
part: Partial<Drawn>,
|
||||
): EditingDraft | null {
|
||||
const target = standIns.get(element);
|
||||
if (!target || !queues.get(target.iframe)?.gsapSetTakesDrafts) return null;
|
||||
const next = nextDraw(element, part);
|
||||
if (!next) return null;
|
||||
next.queue.ops = next.queue.ops.filter((op) => op.handle !== next.op.handle || "end" in op);
|
||||
return next.op;
|
||||
}
|
||||
|
||||
/** Draws `part` of the stand-in's draft on its frame element; any other element is left alone. */
|
||||
export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
|
||||
const next = nextDraw(element, part);
|
||||
if (!next) return;
|
||||
const { target, queue, op } = next;
|
||||
const last = queue.ops.map((queued) => queued.handle).lastIndexOf(target.handle);
|
||||
const waiting = queue.ops[last];
|
||||
const replaceable = (queued: EditingDraft) => !("end" in queued) && !queued.props;
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { EditingDraft } from "@hyperframes/core/runtime/editing-port";
|
||||
import { drawPreviewDraft, registerPreviewStandIn } from "../components/editor/previewDrafts";
|
||||
import { previewRequest } from "../player/lib/previewChannel";
|
||||
import { frameCentreScaledAt } from "./scaleDropOnFrame";
|
||||
|
||||
vi.mock("../player/lib/previewChannel", () => ({ previewRequest: vi.fn() }));
|
||||
vi.mock("../player/lib/previewElements", () => ({
|
||||
previewGeometry: async () => ({ elements: [{ rect: { x: 0, y: 0, width: 10, height: 10 } }] }),
|
||||
}));
|
||||
|
||||
afterEach(() => vi.mocked(previewRequest).mockReset());
|
||||
|
||||
type Shown = { sizeDrawn: boolean; scale: number };
|
||||
type Sent = { kind: string; drafts?: EditingDraft[] };
|
||||
|
||||
type Asked = { drafts?: EditingDraft[]; props?: { scaleX?: number } };
|
||||
|
||||
/** A sealed frame that may paint after any message: what each message left on screen, and what was sent. */
|
||||
function frameShowing(scale: number, { takesDraftsWithSet = true } = {}) {
|
||||
const painted: Shown[] = [];
|
||||
const sent: Sent[] = [];
|
||||
let sizeDrawn = true;
|
||||
const apply = (kind: string, { drafts = [], props }: Asked) => {
|
||||
for (const draft of drafts) if ("size" in draft) sizeDrawn = draft.size !== null;
|
||||
if (kind === "gsap-set") scale = props?.scaleX ?? scale;
|
||||
};
|
||||
const draftReply = {
|
||||
drawn: 1,
|
||||
drafts: {},
|
||||
...(takesDraftsWithSet && { gsapSetTakesDrafts: true }),
|
||||
};
|
||||
vi.mocked(previewRequest).mockImplementation(async (_iframe, kind, params) => {
|
||||
const asked = params as Asked;
|
||||
sent.push({ kind, ...(asked.drafts && { drafts: asked.drafts }) });
|
||||
apply(kind, asked);
|
||||
painted.push({ sizeDrawn, scale });
|
||||
return kind === "draft" ? draftReply : { applied: true };
|
||||
});
|
||||
return { painted, sent };
|
||||
}
|
||||
|
||||
async function dropScaledResize() {
|
||||
const standIn = document.createElement("div");
|
||||
const iframe = document.createElement("iframe");
|
||||
registerPreviewStandIn(standIn, iframe, "a:1");
|
||||
drawPreviewDraft(standIn, { size: { width: 307, height: 204 }, translate: { x: -33, y: -21 } });
|
||||
await frameCentreScaledAt(
|
||||
{ standIn, iframe, handle: "a:1" },
|
||||
{ x: 1.9, y: 1.9 },
|
||||
{ x: 40, y: 30 },
|
||||
);
|
||||
}
|
||||
|
||||
describe("frameCentreScaledAt", () => {
|
||||
// fails without the fix: the size ended in its own message, so the frame could paint the old scale with no size.
|
||||
it("never leaves the frame showing the old scale with the drawn size ended", async () => {
|
||||
const { painted } = frameShowing(1.5);
|
||||
await dropScaledResize();
|
||||
expect(painted).not.toContainEqual({ sizeDrawn: false, scale: 1.5 });
|
||||
expect(painted.at(-1)).toEqual({ sizeDrawn: false, scale: 1.9 });
|
||||
});
|
||||
|
||||
it("sends an older runtime today's two messages and never a draft riding the set", async () => {
|
||||
const { painted, sent } = frameShowing(1.5, { takesDraftsWithSet: false });
|
||||
await dropScaledResize();
|
||||
const ending = { handle: "a:1", translate: null, size: null };
|
||||
expect(sent.slice(-2)).toEqual([{ kind: "draft", drafts: [ending] }, { kind: "gsap-set" }]);
|
||||
expect(sent.filter(({ kind, drafts }) => kind === "gsap-set" && drafts)).toEqual([]);
|
||||
expect(painted.at(-1)).toEqual({ sizeDrawn: false, scale: 1.9 });
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
/** The scale route's drop correction on a sealed frame: the frame draws and measures, over its port. */
|
||||
import type { EditingDraft } from "@hyperframes/core/runtime/editing-port";
|
||||
import {
|
||||
drawPreviewDraft,
|
||||
previewDraftRidingGsapSet,
|
||||
previewDraftsSent,
|
||||
previewStandIn,
|
||||
} from "../components/editor/previewDrafts";
|
||||
@@ -22,8 +24,20 @@ export async function frameBoxCentre(frame: SealedScaleFrame): Promise<Point | n
|
||||
return rect ? { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 } : null;
|
||||
}
|
||||
|
||||
const gsapSet = (frame: SealedScaleFrame, props: Record<string, number>) =>
|
||||
previewRequest(frame.iframe, "gsap-set", { handle: frame.handle, props });
|
||||
const gsapSet = (frame: SealedScaleFrame, props: Record<string, number>, drafts?: EditingDraft[]) =>
|
||||
previewRequest(frame.iframe, "gsap-set", {
|
||||
handle: frame.handle,
|
||||
props,
|
||||
...(drafts && { drafts }),
|
||||
});
|
||||
|
||||
async function endSizeAndSetInOneFrameTask(frame: SealedScaleFrame, props: Record<string, number>) {
|
||||
await previewDraftsSent(frame.iframe);
|
||||
const ending = previewDraftRidingGsapSet(frame.standIn, { size: null, translate: null });
|
||||
if (!ending) return false;
|
||||
await gsapSet(frame, props, [ending]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The box with the committed scale at `at` and no draft, as the save will render it; its centre. */
|
||||
export async function frameCentreScaledAt(
|
||||
@@ -31,9 +45,12 @@ export async function frameCentreScaledAt(
|
||||
scale: Point,
|
||||
at: Point,
|
||||
): Promise<Point | null> {
|
||||
drawPreviewDraft(frame.standIn, { size: null, translate: null });
|
||||
await previewDraftsSent(frame.iframe);
|
||||
await gsapSet(frame, { scaleX: scale.x, scaleY: scale.y, x: at.x, y: at.y });
|
||||
const props = { scaleX: scale.x, scaleY: scale.y, x: at.x, y: at.y };
|
||||
if (!(await endSizeAndSetInOneFrameTask(frame, props))) {
|
||||
drawPreviewDraft(frame.standIn, { size: null, translate: null });
|
||||
await previewDraftsSent(frame.iframe);
|
||||
await gsapSet(frame, props);
|
||||
}
|
||||
return frameBoxCentre(frame);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user