Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,37 @@ async function sealedCard(transform: string, painted: number[]) {
return standInFor(iframe, (await describePreviewElements(iframe, [handle]))![0]!);
}

/** Drags the right crop edge by `dx, dy` screen px and lets go, all before the frame answers. */
async function cropRightOn(standIn: HTMLElement, painted: number[], dx: number, dy: number) {
const onStyleCommit = vi.fn();
const [left, top, width, height] = painted;
type Shown = { left: number; top: number; width: number; height: number; angle?: number };

/** Studio's crop handles on the stand-in, over the oriented box the frame's geometry gives the selection. */
function cropHandles(standIn: HTMLElement, onStyleCommit = vi.fn()) {
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
act(() =>
root.render(
createElement(DomEditCropHandles, {
selection: {
element: standIn,
id: "card",
selector: "#card",
} as unknown as DomEditSelection,
overlayRect: { left, top, width, height, editScaleX: 1, editScaleY: 1 },
onStyleCommit,
}),
),
);
const show = (shown: Shown) =>
act(() =>
root.render(
createElement(DomEditCropHandles, {
selection: {
element: standIn,
id: "card",
selector: "#card",
} as unknown as DomEditSelection,
overlayRect: { ...shown, editScaleX: 1, editScaleY: 1 },
onStyleCommit,
}),
),
);
const turn = () =>
document.querySelector<HTMLElement>("[data-dom-edit-crop-frame]")!.style.transform;
return { root, show, turn };
}

/** Drags the right crop edge by `dx, dy` screen px and lets go, all before the frame answers. */
async function cropRightOn(standIn: HTMLElement, shown: Shown, dx: number, dy: number) {
const onStyleCommit = vi.fn();
const { root, show } = cropHandles(standIn, onStyleCommit);
show(shown);
const edge = document.querySelector<HTMLElement>('[aria-label="Crop right"]')!;
const send = (type: string, x: number, y: number, buttons: number) =>
edge.dispatchEvent(
Expand All @@ -93,7 +104,8 @@ async function cropRightOn(standIn: HTMLElement, painted: number[], dx: number,
it("crops a sealed card GSAP draws at twice its size by its own px, as an unsealed crop does", async () => {
const painted = [0, 0, 400, 200];
const standIn = await sealedCard("matrix(2, 0, 0, 2, 0, 0)", painted);
expect(await cropRightOn(standIn, painted, -20, 0)).toEqual([
const shown = { left: 0, top: 0, width: 400, height: 200 };
expect(await cropRightOn(standIn, shown, -20, 0)).toEqual([
"clip-path",
"inset(0px 10px 0px 0px)",
]);
Expand All @@ -102,8 +114,40 @@ it("crops a sealed card GSAP draws at twice its size by its own px, as an unseal
it("crops a sealed card turned a quarter along its own width, as an unsealed crop does", async () => {
const painted = [0, 0, 100, 200];
const standIn = await sealedCard("matrix(0, 1, -1, 0, 0, 0)", painted);
expect(await cropRightOn(standIn, painted, 0, -20)).toEqual([
const shown = { left: -50, top: 50, width: 200, height: 100, angle: 90 };
expect(await cropRightOn(standIn, shown, 0, -20)).toEqual([
"clip-path",
"inset(0px 20px 0px 0px)",
]);
});

// fails without the fix: before a press, a sealed crop frame was drawn on the stand-in, which has no turn.
it("draws a sealed turned card's crop frame turned before any press", async () => {
const standIn = await sealedCard("rotate(30deg)", [0, 0, 223, 187]);
const { root, show, turn } = cropHandles(standIn);

show({ left: 12, top: 44, width: 200, height: 100, angle: 30 });

expect(turn()).toBe("rotate(30deg)");
act(() => root.unmount());
});

// fails without the fix: the turn measured at a press was held until the selection changed.
it("redraws a sealed crop frame at the turn the card has after a scrub, once a press measured it", async () => {
const standIn = await sealedCard("rotate(30deg)", [0, 0, 223, 187]);
const { root, show, turn } = cropHandles(standIn);
show({ left: 12, top: 44, width: 200, height: 100, angle: 30 });
const edge = document.querySelector<HTMLElement>('[aria-label="Crop right"]')!;
const send = (type: string, buttons: number) =>
edge.dispatchEvent(
new PointerEvent(type, { bubbles: true, pointerId: 7, buttons, clientX: 200, clientY: 100 }),
);
act(() => send("pointerdown", 1));
act(() => send("pointerup", 0));
await act(() => new Promise((resolve) => setTimeout(resolve, 20)));

show({ left: 12, top: 44, width: 200, height: 100, angle: 50 });

expect(turn()).toBe("rotate(50deg)");
act(() => root.unmount());
});
9 changes: 7 additions & 2 deletions packages/studio/src/components/editor/DomEditCropHandles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { isCropBarTarget, useCropPresetBarStore } from "./cropPresetStore";
import { CropPresetBar } from "./CropPresetBar";
import { movesGesture } from "./domEditOverlayGestures";
import { previewStandIn } from "./previewDrafts";
import { readCropFrameStart } from "./previewGestureStartRead";
import { gestureStartLayout, readCropFrameStart } from "./previewGestureStartRead";

interface CropGestureState {
edge: CropEdge | "move";
Expand Down Expand Up @@ -158,7 +158,12 @@ function letGoOf(ref: { current: CropGestureState | null }, gesture: CropGesture
const measuredStartOf = (
state: { element: HTMLElement; start?: CropFrameStart },
element: HTMLElement,
) => (state.element === element ? state.start : undefined);
): CropFrameStart | undefined => {
if (state.element === element && state.start) return state.start;
const handle = previewStandIn(element)?.handle;
const layout = handle ? gestureStartLayout(handle) : null;
return layout ? { layout } : undefined;
};

/** The element's clip-path is the crop; state only holds a crop drag's draft. */
const shownCropInsets = (dragging: boolean, draft: ClipPathInsetSides, element: HTMLElement) =>
Expand Down
31 changes: 24 additions & 7 deletions packages/studio/src/components/editor/domEditOverlayCrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,22 +365,39 @@ function readPlanarTransform(element: HTMLElement): Planar2D | null {
return acc && !isIdentity(acc) ? acc : null;
}

/** What the frame measured of a stand-in's element at the press: a stand-in has no transform or box of its own. */
/** What the frame measured of a stand-in's element at the press: a stand-in has no box of its own. */
export interface CropFrameStart {
planar: Planar2D | null;
layout: { width: number; height: number };
}

const measuredPlanar = ({ planar }: CropFrameStart) =>
planar && !isIdentity(planar) ? planar : null;
/** A stand-in's crop frame: the frame's live oriented box of its element, scaled by the layout a press measured. */
function standInCropFrame(
overlayRect: CropScreenRect & { angle?: number },
layout: { width: number; height: number } | null,
edit: { x: number; y: number },
): CropFrame {
const { left, top, width, height } = overlayRect;
return {
angleDeg: overlayRect.angle ?? 0,
left,
top,
width,
height,
scaleX: layout && layout.width > 0 ? width / layout.width : edit.x,
scaleY: layout && layout.height > 0 ? height / layout.height : edit.y,
};
}

export function readElementCropFrame(
element: HTMLElement,
overlayRect: CropScreenRect & { editScaleX: number; editScaleY: number },
overlayRect: CropScreenRect & { editScaleX: number; editScaleY: number; angle?: number },
start?: CropFrameStart,
): CropFrame {
const editX = overlayRect.editScaleX > 0 ? overlayRect.editScaleX : 1;
const editY = overlayRect.editScaleY > 0 ? overlayRect.editScaleY : 1;
if (previewStandIn(element)) {
return standInCropFrame(overlayRect, start?.layout ?? null, { x: editX, y: editY });
}
const aabb: CropFrame = {
angleDeg: 0,
left: overlayRect.left,
Expand All @@ -390,7 +407,7 @@ export function readElementCropFrame(
scaleX: editX,
scaleY: editY,
};
const planar = start ? measuredPlanar(start) : readPlanarTransform(element);
const planar = readPlanarTransform(element);
if (!planar) return aabb;
const { a, b, c, d } = planar;
const elScaleX = Math.hypot(a, b);
Expand All @@ -401,7 +418,7 @@ export function readElementCropFrame(
const angleDeg = (Math.atan2(b, a) * 180) / Math.PI;
const scaleX = elScaleX * editX;
const scaleY = elScaleY * editY;
const size = start?.layout ?? { width: element.offsetWidth, height: element.offsetHeight };
const size = { width: element.offsetWidth, height: element.offsetHeight };
const width = size.width * scaleX;
const height = size.height * scaleY;
if (!(width > 0) || !(height > 0)) return aabb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,5 @@ export async function readCropFrameStart(
handle: string,
): Promise<CropFrameStart | null> {
const start = (await previewGestureStart(iframe, [handle]))?.elements[0];
return start && start.planar !== undefined
? { planar: start.planar, layout: start.layout }
: null;
return start ? { layout: start.layout } : null;
}
13 changes: 2 additions & 11 deletions packages/studio/src/player/lib/previewElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,34 +256,25 @@ const isLayout = (value: unknown) =>
const isTurnOrNull = (value: unknown) =>
value === null || (Number.isFinite(value) && Math.abs(value as number) <= MAX_DRAFT_DEG);

const isPlanarOrNull = (value: unknown) =>
value === null ||
(isFields(value) &&
["a", "b", "c", "d"].every(
(key) => Number.isFinite(value[key]) && Math.abs(value[key] as number) <= MAX_DRAFT_PX,
));

const kept = <T>(value: T, valid: boolean) => (valid ? value : undefined);

export type ReadGestureStart = Omit<EditingGestureStart, "gsapRotation" | "planar"> & {
gsapRotation?: number | null;
planar?: EditingGestureStart["planar"];
};

const readGestureStart = (value: unknown): ReadGestureStart | null => {
if (!readGeometry(value) || !isFields(value)) return null;
const { layout, move, gsap, gsapRotation, planar } = value;
const { layout, move, gsap, gsapRotation } = value;
const valid = [
isLayout(layout),
move === null || isFiniteFields(move, ["a", "b", "c", "d"]),
gsap === null || (isFields(gsap) && isFiniteFields(gsap, ["x", "y"]) && isFlag(gsap.moving)),
];
if (valid.includes(false)) return null;
const start = value as unknown as ReadGestureStart;
const { planar: _planar, ...start } = value as unknown as EditingGestureStart;
return {
...start,
gsapRotation: kept(start.gsapRotation, isTurnOrNull(gsapRotation)),
planar: kept(start.planar, isPlanarOrNull(planar)),
};
};

Expand Down
Loading