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
28 changes: 23 additions & 5 deletions packages/studio/src/components/editor/domEditOverlayCrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ export function cropRectFromInsets(
// 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 liftListeners = new Set<() => void>();
let liftVersion = 0;
function liftsChanged() {
liftVersion += 1;
for (const listener of liftListeners) listener();
}
function setLift(element: HTMLElement, lift: CropLift | null) {
if (lift) cropLifts.set(element, lift);
else cropLifts.delete(element);
liftsChanged();
}
/** For `useSyncExternalStore`: the overlay redraws its crop hug when a lift starts, lands, is refused or drops. */
export function subscribeCropLifts(listener: () => void): () => void {
liftListeners.add(listener);
return () => liftListeners.delete(listener);
}
export const cropLiftsVersion = () => liftVersion;
const UNLIFT_TRIES = 3;

function liftSelector(element: HTMLElement): string | null {
Expand All @@ -60,7 +77,7 @@ function unliftInFrame(element: HTMLElement, record: CropLift, tries = UNLIFT_TR
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);
if (reply?.lifted === false) setLift(element, null);
else if (tries > 1) unliftInFrame(element, record, tries - 1);
},
);
Expand All @@ -71,11 +88,11 @@ function liftInFrame(element: HTMLElement): boolean {
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);
setLift(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);
if (!reply.lifted) setLift(element, null);
else if (typeof reply.clipPath === "string" && reply.clipPath.length <= MAX_ELEMENT_VALUE)
record.sheetClip = reply.clipPath.trim();
},
Expand All @@ -92,18 +109,19 @@ export function liftElementCrop(element: HTMLElement): void {
const rule = doc.createElement("style");
rule.textContent = `${selector}{clip-path:none!important}`;
(doc.head ?? doc.documentElement).append(rule);
cropLifts.set(element, { rule });
setLift(element, { rule });
}

export function dropElementCropLift(element: HTMLElement): void {
const lift = cropLifts.get(element);
if (!lift || lift.dropped) return;
if (lift.rule) {
lift.rule.remove();
cropLifts.delete(element);
setLift(element, null);
return;
}
lift.dropped = true;
liftsChanged();
unliftInFrame(element, lift);
}

Expand Down
86 changes: 86 additions & 0 deletions packages/studio/src/hooks/useCropOverlay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import {
dropElementCropLift,
liftElementCrop,
rememberFrameClipPath,
} from "../components/editor/domEditOverlayCrop";
import type { DomEditSelection } from "../components/editor/domEditing";
import type { OverlayRect } from "../components/editor/domEditOverlayGeometry";
import { useCropOverlay } from "./useCropOverlay";

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

const sent: Array<(reply: unknown) => void> = [];
let sealed = true;
vi.mock("../player/lib/previewChannel", () => ({
previewRequest: () => new Promise((answer) => sent.push(answer)),
}));
vi.mock("../components/editor/previewDrafts", () => ({
previewStandIn: () => (sealed ? { iframe: {}, handle: "hf-1" } : undefined),
}));

let host: HTMLDivElement;
let root: Root;
beforeEach(() => {
host = document.body.appendChild(document.createElement("div"));
root = createRoot(host);
});
afterEach(() => {
act(() => root.unmount());
host.remove();
sent.length = 0;
});

const rect: OverlayRect = {
left: 0,
top: 0,
width: 400,
height: 200,
editScaleX: 1,
editScaleY: 1,
};

/** The selection box's crop hug as the overlay last drew it. */
function drawn(element: HTMLElement) {
const seen: Array<number | null> = [];
function Probe() {
const { cropOutlineInsetPx } = useCropOverlay({
selection: { element } as DomEditSelection,
overlayRect: rect,
});
seen.push(cropOutlineInsetPx?.right ?? null);
return null;
}
act(() => root.render(createElement(Probe)));
return () => seen.at(-1);
}

// fails without the fix: the lift lives outside React, so the box kept hugging the crop once the handles lifted it.
it("a sealed layer picked with no press drops its crop hug once the crop handles lift it, and gets it back when the frame refuses", async () => {
sealed = true;
const element = document.createElement("div");
rememberFrameClipPath(element, "inset(0px 150px 0px 0px)", "inset(0px 150px 0px 0px)");
const hug = drawn(element);
expect(hug()).toBe(150);
act(() => liftElementCrop(element));
expect(hug(), "the box shows the whole layer while it is lifted").toBe(null);
await act(async () => sent[0]!({ lifted: false, clipPath: null }));
expect(hug(), "a lift the frame refused leaves the crop showing").toBe(150);
});

it("an unsealed layer's crop hug follows its lift and its drop", () => {
sealed = false;
const element = document.body.appendChild(document.createElement("div"));
element.id = "target";
element.style.setProperty("clip-path", "inset(0px 150px 0px 0px)");
const hug = drawn(element);
expect(hug()).toBe(150);
act(() => liftElementCrop(element));
expect(hug()).toBe(null);
act(() => dropElementCropLift(element));
expect(hug()).toBe(150);
element.remove();
});
4 changes: 4 additions & 0 deletions packages/studio/src/hooks/useCropOverlay.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useSyncExternalStore } from "react";
import type { OverlayRect } from "../components/editor/domEditOverlayGeometry";
import type { DomEditSelection } from "../components/editor/domEditing";
import {
cropLiftsVersion,
isElementCropLifted,
readElementCropInsets,
subscribeCropLifts,
} from "../components/editor/domEditOverlayCrop";

/** Selection-box crop hug: the outline that makes the selection box hug the
Expand All @@ -15,6 +18,7 @@ export function useCropOverlay(params: {
overlayRect: OverlayRect | null;
}) {
const { selection, overlayRect } = params;
useSyncExternalStore(subscribeCropLifts, cropLiftsVersion);

const cropInsets =
selection && !isElementCropLifted(selection.element)
Expand Down
Loading