fix(studio): crop and scale resize work in a sealed preview (#8005)

* test(studio): sealed preview crop and scale resize read the frame's values

* fix(studio): crop and scale resize work in a sealed preview

* test(studio): a sealed preview's crop lift and resize box survive lost or bad frame replies

* fix(studio): a sealed preview's crop lift and resize box survive lost or bad frame replies

* refactor(studio): the crop's stylesheet clip under a lift is read in its own step

* test(studio): the frame crop-lift helper answers the lift it just sent
This commit is contained in:
Miguel Ángel
2026-11-18 01:44:51 +00:00
committed by GitHub
parent 5f06e1b0a3
commit c569417bb5
14 changed files with 306 additions and 19 deletions
@@ -0,0 +1,31 @@
/**
* Studio's crop lift in a frame it cannot script. While an element's crop is edited, a rule hides
* its clip-path so the cropped-away part shows; the element's own clip-path stays the saved record.
*/
import { elementForHandle } from "../editingHandles";
import type { EditingParams, EditingResult } from "../editingPort";
const lifts = new WeakMap<Element, HTMLStyleElement>();
/** The same selector Studio's own lift writes: only an element with a data-hf-id or an id is lifted. */
function liftSelector(element: Element): string | null {
const quote = (value: string) => `"${value.replace(/["\\]/g, "\\$&")}"`;
const hfId = element.getAttribute("data-hf-id");
if (hfId) return `[data-hf-id=${quote(hfId)}]`;
return element.id ? `[id=${quote(element.id)}]` : null;
}
export function cropLift({ handle, lift }: EditingParams<"crop-lift">): EditingResult<"crop-lift"> {
const element = elementForHandle(handle);
if (!element) return { lifted: false, clipPath: null };
lifts.get(element)?.remove();
lifts.delete(element);
const clipPath = window.getComputedStyle(element).clipPath.trim();
const selector = lift ? liftSelector(element) : null;
if (!selector) return { lifted: false, clipPath };
const rule = document.createElement("style");
rule.textContent = `${selector}{clip-path:none!important}`;
(document.head ?? document.documentElement).append(rule);
lifts.set(element, rule);
return { lifted: true, clipPath };
}
@@ -198,6 +198,7 @@ describe("installEditingAgent", () => {
"gsap-snapshot",
"motion-path-home",
"max-z-index",
"crop-lift",
],
},
});
@@ -38,6 +38,7 @@ import { maxZIndex } from "./editing/maxZIndex";
import { gestureStart } from "./editingGestureStart";
import { gsapSnapshot } from "./editing/gsapSnapshot";
import { motionPathHome } from "./editing/motionPathHome";
import { cropLift } from "./editing/cropLift";
import {
getLogicalOpacity,
markedForStudio,
@@ -216,6 +217,7 @@ const HANDLERS: Handlers = {
"gsap-snapshot": () => gsapSnapshot(),
"motion-path-home": ({ selector }) => motionPathHome(selector),
"max-z-index": () => maxZIndex(),
"crop-lift": cropLift,
};
function serve(port: MessagePort): void {
@@ -91,6 +91,7 @@ describe("createEditingPortClient", () => {
"gsap-snapshot",
"motion-path-home",
"max-z-index",
"crop-lift",
],
});
} finally {
+9
View File
@@ -169,6 +169,7 @@ export const EDITING_REQUEST_KINDS = [
"gsap-snapshot",
"motion-path-home",
"max-z-index",
"crop-lift",
] as const;
export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number];
@@ -298,6 +299,10 @@ export interface EditingRequests {
"gsap-snapshot": { params: NoParams; result: GsapSnapshot };
"motion-path-home": { params: { selector: string }; result: MotionPathFrameHome };
"max-z-index": { params: NoParams; result: { zIndex: number } };
"crop-lift": {
params: { handle: EditingElementHandle; lift: boolean };
result: { lifted: boolean; clipPath: string | null };
};
}
export type EditingParams<K extends EditingRequestKind> = EditingRequests[K]["params"];
export type EditingResult<K extends EditingRequestKind> = EditingRequests[K]["result"];
@@ -501,6 +506,10 @@ const PARAM_PARSERS: {
? { selector: params.selector }
: null,
"max-z-index": noParams,
"crop-lift": (params) =>
hasKeys(params, "handle,lift") && isHandle(params.handle) && typeof params.lift === "boolean"
? { handle: params.handle, lift: params.lift }
: null,
};
/** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */
@@ -7,6 +7,9 @@ import {
type ClipPathInsetSides,
type ParsedInsetClipPathSides,
} from "./clipPathHelpers";
import { MAX_DRAFT_PX, MAX_ELEMENT_VALUE } from "@hyperframes/core/runtime/editing-port";
import { previewRequest } from "../../player/lib/previewChannel";
import { previewStandIn } from "./previewDrafts";
export type CropEdge = "top" | "right" | "bottom" | "left";
@@ -38,7 +41,11 @@ export function cropRectFromInsets(
// A selected croppable element shows uncropped so the crop UI can dim what is cut away. A rule keyed on its
// identity does that, so the element's own clip-path stays the one record undo, redo and panel edits rewrite.
const cropLifts = new WeakMap<HTMLElement, { rule: HTMLStyleElement; sheetClip?: string }>();
// A sealed preview's lift is that rule in the frame (core `editing/cropLift.ts`), and `rule` is null.
// A frame lift is kept until the frame says it is gone, so a lost reply never leaves the crop hidden there.
type CropLift = { rule: HTMLStyleElement | null; sheetClip?: string; dropped?: boolean };
const cropLifts = new WeakMap<HTMLElement, CropLift>();
const UNLIFT_TRIES = 3;
function liftSelector(element: HTMLElement): string | null {
const quote = (value: string) => `"${value.replace(/["\\]/g, "\\$&")}"`;
@@ -47,7 +54,38 @@ function liftSelector(element: HTMLElement): string | null {
return element.id ? `[id=${quote(element.id)}]` : null;
}
function unliftInFrame(element: HTMLElement, record: CropLift, tries = UNLIFT_TRIES): void {
const target = previewStandIn(element);
if (!target) return;
void previewRequest(target.iframe, "crop-lift", { handle: target.handle, lift: false }).then(
(reply) => {
if (cropLifts.get(element) !== record) return;
if (reply?.lifted === false) cropLifts.delete(element);
else if (tries > 1) unliftInFrame(element, record, tries - 1);
},
);
}
function liftInFrame(element: HTMLElement): boolean {
const target = previewStandIn(element);
if (!target) return false;
// Until the frame answers, the clip it last reported is the one under the lift.
const record: CropLift = { rule: null, sheetClip: frameClipPaths.get(element)?.computed ?? "" };
cropLifts.set(element, record);
void previewRequest(target.iframe, "crop-lift", { handle: target.handle, lift: true }).then(
(reply) => {
if (cropLifts.get(element) !== record || !reply) return;
if (!reply.lifted) cropLifts.delete(element);
else if (typeof reply.clipPath === "string" && reply.clipPath.length <= MAX_ELEMENT_VALUE)
record.sheetClip = reply.clipPath.trim();
},
);
return true;
}
export function liftElementCrop(element: HTMLElement): void {
const held = cropLifts.get(element);
if ((held && !held.dropped) || liftInFrame(element)) return;
const selector = liftSelector(element);
if (!selector || cropLifts.has(element)) return;
const doc = element.ownerDocument;
@@ -58,37 +96,62 @@ export function liftElementCrop(element: HTMLElement): void {
}
export function dropElementCropLift(element: HTMLElement): void {
cropLifts.get(element)?.rule.remove();
cropLifts.delete(element);
const lift = cropLifts.get(element);
if (!lift || lift.dropped) return;
if (lift.rule) {
lift.rule.remove();
cropLifts.delete(element);
return;
}
lift.dropped = true;
unliftInFrame(element, lift);
}
export function isElementCropLifted(element: HTMLElement): boolean {
return cropLifts.has(element);
const lift = cropLifts.get(element);
return !!lift && !lift.dropped;
}
const computedClipPath = (element: HTMLElement) =>
element.ownerDocument.defaultView?.getComputedStyle(element).clipPath.trim() || "";
// A sealed preview's stand-in has no styles of its own: its clip-path is the one the frame reported.
const frameClipPaths = new WeakMap<Element, { inline: string; computed: string }>();
/** The element's clip-path, inline first, then the stylesheet's as it is without the lift. */
function readElementClipPath(element: HTMLElement): string {
const inline = element.style.getPropertyValue("clip-path").trim();
const lift = cropLifts.get(element);
if (inline || !lift) return inline || computedClipPath(element);
// Only inline writes happen while selected, so the stylesheet's clip is read once per lift.
if (lift.sheetClip === undefined) {
export function rememberFrameClipPath(element: HTMLElement, inline: string, computed: string) {
frameClipPaths.set(element, { inline: inline.trim(), computed: computed.trim() });
}
const computedClipPath = (element: HTMLElement) => {
const view = element.ownerDocument.defaultView;
if (!view) return frameClipPaths.get(element)?.computed ?? "";
return view.getComputedStyle(element).clipPath.trim();
};
/** The stylesheet's clip under a lift, read once per lift: only inline writes happen while selected. */
function sheetClipUnder(element: HTMLElement, lift: CropLift): string {
if (lift.sheetClip === undefined && lift.rule) {
const { parentNode, nextSibling } = lift.rule;
lift.rule.remove();
lift.sheetClip = computedClipPath(element);
parentNode?.insertBefore(lift.rule, nextSibling);
}
return lift.sheetClip;
return lift.sheetClip ?? "";
}
/** The element's clip-path, inline first, then the stylesheet's as it is without the lift. */
function readElementClipPath(element: HTMLElement): string {
const inline =
element.style.getPropertyValue("clip-path").trim() || frameClipPaths.get(element)?.inline;
if (inline) return inline;
const lift = cropLifts.get(element);
return lift && !lift.dropped ? sheetClipUnder(element, lift) : computedClipPath(element);
}
/** Zeros = no clip yet. `null` = a clip this tool cannot represent (circle/polygon/non-px inset):
* croppers must not lift, edit, or restore it, or deselect silently replaces or destroys it. */
function parseCropClipPath(value: string): ParsedInsetClipPathSides | null {
if (!value || value === "none") return { top: 0, right: 0, bottom: 0, left: 0, radius: 0 };
return parseInsetClipPathSides(value);
const sides = parseInsetClipPathSides(value);
const sane = sides && Object.values(sides).every((side) => Math.abs(side) <= MAX_DRAFT_PX);
return sane ? sides : null;
}
export function readElementCropInsets(element: HTMLElement): ParsedInsetClipPathSides | null {
@@ -0,0 +1,64 @@
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import {
dropElementCropLift,
isElementCropLifted,
liftElementCrop,
readElementCropInsets,
} from "./domEditOverlayCrop";
const sent: Array<{ lift: boolean; answer: (reply: unknown) => void }> = [];
vi.mock("../../player/lib/previewChannel", () => ({
previewRequest: (_iframe: unknown, _kind: string, params: { lift: boolean }) =>
new Promise((answer) => sent.push({ lift: params.lift, answer })),
}));
vi.mock("./previewDrafts", () => ({
previewStandIn: () => ({ iframe: {}, handle: "hf-1" }),
}));
afterEach(() => {
sent.length = 0;
});
const settle = () => new Promise((done) => setTimeout(done, 0));
async function lifted(clipPath = "inset(10px)") {
const el = document.createElement("div");
liftElementCrop(el);
sent.at(-1)!.answer({ lifted: true, clipPath });
await settle();
return el;
}
describe("a crop lift in a frame Studio cannot script", () => {
it("is kept when the lift's reply is lost, so deselecting still asks the frame to drop it", async () => {
const el = document.createElement("div");
liftElementCrop(el);
sent[0]!.answer(null);
await settle();
expect(isElementCropLifted(el)).toBe(true);
dropElementCropLift(el);
expect(sent.map((request) => request.lift)).toEqual([true, false]);
});
it("asks again until the frame says the lift is gone, and only then forgets it", async () => {
const el = await lifted();
dropElementCropLift(el);
sent[1]!.answer(null);
await settle();
expect(sent.map((request) => request.lift)).toEqual([true, false, false]);
sent[2]!.answer({ lifted: false, clipPath: null });
await settle();
expect(sent).toHaveLength(3);
expect(isElementCropLifted(el)).toBe(false);
liftElementCrop(el);
expect(sent.map((request) => request.lift)).toEqual([true, false, false, true]);
});
it("ignores a clip-path past the element value cap, and an inset past the draft limit", async () => {
expect(readElementCropInsets(await lifted(`inset(5px) ${"x".repeat(3000)}`))).toMatchObject({
top: 0,
});
expect(readElementCropInsets(await lifted("inset(200000px 0px 0px 0px)"))).toBeNull();
});
});
@@ -30,6 +30,8 @@ export function registerPreviewStandIn(
standIns.set(standIn, { iframe, handle });
}
export const previewStandIn = (standIn: Element) => standIns.get(standIn);
function queueFor(iframe: HTMLIFrameElement): Queue {
let queue = queues.get(iframe);
if (!queue) {
@@ -1,10 +1,12 @@
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
// The frame's real runtime agent, serving the opaque frame from this document.
import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
import { makeSelection } from "../../hooks/domSelectionTestHarness";
import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition";
import { tryGsapResizeIntercept } from "../../hooks/gsapResizeIntercept";
import { watchPreviewFrame } from "../../player/lib/bridgePlayback";
import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements";
import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
@@ -15,7 +17,11 @@ import {
createManualOffsetDragMember,
restoreManualOffsetDragMembers,
} from "./manualOffsetDrag";
import { measurePreviewGestureStart, type PreviewGestureStart } from "./previewGestureStartRead";
import {
gestureStartLayout,
measurePreviewGestureStart,
type PreviewGestureStart,
} from "./previewGestureStartRead";
import { standInFor } from "./previewSelection";
import { collectSnapContext } from "./snapTargetCollection";
@@ -266,3 +272,52 @@ describe("starting a gesture from what an opaque frame read", () => {
restoreManualOffsetDragMembers(g.members);
});
});
describe("a resize in a preview Studio cannot script", () => {
it.each([-5, 1e-9, 1e9])("keeps no start box for a frame-reported width of %s", async (width) => {
const iframe = opaqueFrame();
Object.defineProperty(document.querySelector("#last"), "offsetWidth", {
value: width,
configurable: true,
});
const handle = (await queryPreviewElements(iframe, "#last"))!.matches[0]!.handle;
await measurePreviewGestureStart(iframe, overlayEl(), [handle]);
expect(gestureStartLayout(handle)).toBeNull();
});
it("scales from the box its gesture started from, not a 200 px fallback", async () => {
const iframe = opaqueFrame();
const handle = (await queryPreviewElements(iframe, "#last"))!.matches[0]!.handle;
const standIn = standInFor(iframe, (await describePreviewElements(iframe, [handle]))![0]!);
await measurePreviewGestureStart(iframe, overlayEl(), [handle]);
const scaleKeys = {
id: "#last-scale",
targetSelector: "#last",
propertyGroup: "scale",
method: "to",
properties: { scale: 1 },
position: 0,
resolvedStart: 0,
duration: 1,
keyframes: {
keyframes: [
{ percentage: 0, properties: { scale: 1 } },
{ percentage: 100, properties: { scale: 1 } },
],
},
} as unknown as GsapAnimation;
const commitMutation = vi.fn();
await tryGsapResizeIntercept(
{ ...makeSelection("last", standIn), handle },
{ width: 240, height: 120 },
[scaleKeys],
iframe,
commitMutation,
);
const scales = JSON.stringify(commitMutation.mock.calls.map((call) => call[1]));
// It measured 120 x 60 at the press, so 240 x 120 is a scale of 2; the 200 px fallback gave 1.2.
expect(scales).toMatch(/"scale":2\b/);
});
});
@@ -20,6 +20,10 @@ export interface PreviewGestureStarts {
snap: SnapContext;
}
const startLayouts = new Map<string, PreviewGestureStart["layout"]>();
export const gestureStartLayout = (handle: string) => startLayouts.get(handle) ?? null;
/** Everything a gesture on `handles` reads from the frame, in one request; null when it cannot answer. */
export async function measurePreviewGestureStart(
iframe: HTMLIFrameElement,
@@ -27,6 +31,11 @@ export async function measurePreviewGestureStart(
handles: string[],
): Promise<PreviewGestureStarts | null> {
const reply = await previewGestureStart(iframe, handles);
startLayouts.clear();
reply?.elements.forEach((start, i) => {
const { width = 0, height = 0 } = start?.layout ?? {};
if (width >= 1 && height >= 1) startLayouts.set(handles[i]!, { width, height });
});
const scale = reply && computeOverlayRootScale(overlayEl, iframe, null);
if (!reply || !scale) return null;
const { iframeRect, overlayRect, rootScaleX: sx, rootScaleY: sy } = scale;
@@ -13,6 +13,7 @@ import {
type DomEditSelection,
} from "./domEditing";
import { setCompositionSourceMap } from "./domEditingDom";
import { dropElementCropLift, liftElementCrop, readElementCropInsets } from "./domEditOverlayCrop";
import { domEditSelectionToFacts } from "./domEditingLayers";
import {
findPreviewElementForSelection,
@@ -479,3 +480,34 @@ describe("useDomSelection on an opaque frame", () => {
expect(hook().domEditGroupSelections.map((s) => s.id)).toEqual(["scene"]);
});
});
describe("an opaque frame selection's crop", () => {
it("reads the clip-path the frame renders, as a scriptable frame's does", async () => {
document.body.innerHTML = FIXTURE;
const plain = document.querySelector<HTMLElement>("#plain")!;
plain.style.clipPath = "inset(10px 20px 30px 40px)";
const { live, remote } = await bothPaths(plain);
expect(readElementCropInsets(remote!.element)).toEqual(readElementCropInsets(live!.element));
expect(readElementCropInsets(remote!.element)).toMatchObject({
top: 10,
right: 20,
bottom: 30,
left: 40,
});
});
it("shows the element uncropped in the frame while selected, and crops it again after", async () => {
document.body.innerHTML = FIXTURE;
const plain = document.querySelector<HTMLElement>("#plain")!;
plain.style.clipPath = "inset(10px 20px 30px 40px)";
const { remote } = await bothPaths(plain);
const shown = () => window.getComputedStyle(plain).clipPath;
liftElementCrop(remote!.element);
await vi.waitFor(() => expect(shown()).toBe("none"));
expect(readElementCropInsets(remote!.element)).toMatchObject({ top: 10, left: 40 });
dropElementCropLift(remote!.element);
await vi.waitFor(() => expect(shown()).toBe("inset(10px 20px 30px 40px)"));
});
});
@@ -72,6 +72,7 @@ import {
STUDIO_WIDTH_PROP,
} from "./manualEditsTypes";
import { rememberFrameTranslate, TRANSLATE_PX_PROPERTIES, translatePx } from "./plainTranslate";
import { rememberFrameClipPath } from "./domEditOverlayCrop";
let inertDocument: Document | undefined;
const inert = () => (inertDocument ??= document.implementation.createHTMLDocument(""));
@@ -354,6 +355,11 @@ export async function resolvePreviewSelection(
]);
if (!styles) return null;
// The stand-in has no box to size a % or em against: a move starts from what the frame renders.
rememberFrameClipPath(
element,
current.inlineStyles["clip-path"] ?? "",
styles["clip-path"] ?? "",
);
rememberFrameTranslate(
element,
translatePx(styles.translate ?? "none", (p) => styles[p] ?? ""),
@@ -19,6 +19,7 @@ import {
} from "../utils/elementGsap";
import { usePlayerStore } from "../player/store/playerStore";
import { readGsapProperty } from "./gsapRuntimeReaders";
import { gestureStartLayout } from "../components/editor/previewGestureStartRead";
import {
commitStaticGsapPosition,
commitStaticGsapSize,
@@ -321,8 +322,12 @@ export async function tryGsapResizeIntercept(
// sent almost every element to the fallback below: a 630px chip scaled by
// 630/200, landing over three times the size it was dropped at, and worse
// on the next drag because the wrong scale then counted as its live one.
const cssW = originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? 200;
const cssH = originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? cssW;
// A sealed preview has no element here: its gesture started from the box the frame measured.
const started = selection.handle ? gestureStartLayout(selection.handle) : null;
const cssW =
originalBoxSize(el, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, "width") ?? (started?.width || 200);
const cssH =
originalBoxSize(el, STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, "height") ?? (started?.height || cssW);
// `size` is the draft's CSS box; on screen it is multiplied by the element's
// LIVE scale (the draft divides the cursor delta by it — see
// resolveDomEditResizeGesture). The committed keyframe REPLACES that live
@@ -5,6 +5,7 @@
import {
EDITING_ELEMENT_HANDLE,
MAX_DESCRIBE_HANDLES,
MAX_DRAFT_PX,
MAX_ELEMENT_ENTRIES,
MAX_ELEMENT_VALUE,
MAX_GEOMETRY_CHILDREN,
@@ -233,11 +234,17 @@ const isFlag = (value: unknown) => typeof value === "boolean";
const isFiniteFields = (value: unknown, keys: string[]) =>
isFields(value) && keys.every((key) => Number.isFinite(value[key]));
const isLayout = (value: unknown) =>
isFields(value) &&
[value.width, value.height].every(
(side) => Number.isFinite(side) && (side as number) >= 0 && (side as number) <= MAX_DRAFT_PX,
);
const readGestureStart = (value: unknown): EditingGestureStart | null => {
if (!readGeometry(value) || !isFields(value)) return null;
const { layout, move, gsap } = value;
const valid = [
isFiniteFields(layout, ["width", "height"]),
isLayout(layout),
move === null || isFiniteFields(move, ["a", "b", "c", "d"]),
gsap === null || (isFields(gsap) && isFiniteFields(gsap, ["x", "y"]) && isFlag(gsap.moving)),
];