diff --git a/packages/core/src/runtime/drawnBox.ts b/packages/core/src/runtime/drawnBox.ts index 8707c8e5a00..7bdf28c026a 100644 --- a/packages/core/src/runtime/drawnBox.ts +++ b/packages/core/src/runtime/drawnBox.ts @@ -14,6 +14,8 @@ export const SIZE_PROPERTIES = [ "max-height", ] as const; +export const DRAFT_DRAWN = ["translate", "rotate", "transform", ...SIZE_PROPERTIES] as const; + export const PROBE_PX = 100; export const MIN_PROBE_RESPONSE = 0.01; type Point = { x: number; y: number }; diff --git a/packages/core/src/runtime/editingDraft.ts b/packages/core/src/runtime/editingDraft.ts index c298710f183..11c3c3e438e 100644 --- a/packages/core/src/runtime/editingDraft.ts +++ b/packages/core/src/runtime/editingDraft.ts @@ -10,11 +10,15 @@ import type { EditingDraftEnd, EditingDraftReply } from "./editingDraftEnd"; import type { EditingDraft, EditingDraftProps, EditingDraftRotate } from "./editingPort"; import { revealClip } from "./clipReveal"; import { clearDragChrome, drawDragChrome, type EditingDragChrome } from "./editing/dragChrome"; -import { computedTranslateAxes, movedTranslate, SIZE_PROPERTIES, writeDrawnSize } from "./drawnBox"; +import { + computedTranslateAxes, + DRAFT_DRAWN, + movedTranslate, + SIZE_PROPERTIES, + writeDrawnSize, +} from "./drawnBox"; import type { RuntimeTimelineLike } from "./types"; -const DRAWN = ["translate", "rotate", "transform", ...SIZE_PROPERTIES] as const; - interface Before { draft: number; inline: Array<[name: string, value: string, priority: string]>; @@ -55,7 +59,7 @@ function beforeOf(element: Styled): Before { const { style } = element; saved = { draft: ++lastDraft, - inline: DRAWN.map((name) => [ + inline: DRAFT_DRAWN.map((name) => [ name, style.getPropertyValue(name), style.getPropertyPriority(name), @@ -159,7 +163,7 @@ function drawProps(element: Styled, values: EditingDraftProps["values"]): void { } function clearProps(element: Styled, saved: Before): void { - restore(element, DRAWN); + restore(element, DRAFT_DRAWN); saved.putMarkBack?.(); if (saved.propsSet?.length) frameGsap()?.set?.(element, { clearProps: saved.propsSet.join(",") }); const timeline = rootTimeline(); @@ -186,7 +190,7 @@ function end(element: Styled, ending: EditingDraftEnd): boolean { liveDrafts.delete(element); if (ending.end === "clear") clearProps(element, saved); if (ending.end === "restore") { - restore(element, DRAWN); + restore(element, DRAFT_DRAWN); restoreGsapPosition(element); restoreGsapRotation(element); } diff --git a/packages/studio/src/hooks/geometryCommits.sealed.test.ts b/packages/studio/src/hooks/geometryCommits.sealed.test.ts index 32abcd6a725..57d8afb8715 100644 --- a/packages/studio/src/hooks/geometryCommits.sealed.test.ts +++ b/packages/studio/src/hooks/geometryCommits.sealed.test.ts @@ -12,7 +12,11 @@ import { previewDraftsSent, previewStandIn, } from "../components/editor/previewDrafts"; -import { captureStudioBoxSize, restoreStudioBoxSize } from "../components/editor/manualEdits"; +import { + applyStudioBoxSizeDraft, + captureStudioBoxSize, + restoreStudioBoxSize, +} from "../components/editor/manualEdits"; import { STUDIO_ORIGINAL_BOX_HEIGHT_ATTR, STUDIO_ORIGINAL_BOX_WIDTH_ATTR, @@ -251,6 +255,26 @@ it("rescales the crop with a sealed resize, the same as an open element", async ); }); +// fails without the fix: with the resize's draft still live, the commit drew nothing on the frame, so the frame kept +// the old crop on the new box until a reload. +it("draws the rescaled crop on the frame while the resize's draft is still live", async () => { + const { card, selection, commits } = await sealedCard(saved()); + card.style.setProperty("clip-path", "inset(10px 20px 10px 20px)"); + selection.element.style.setProperty("clip-path", "inset(10px 20px 10px 20px)"); + const pressed = { width: 100, height: 50 }; + const next = { width: 200, height: 100 }; + drawPreviewDraft(selection.element, { + size: applyStudioBoxSizeDraft(selection.element, next, pressed), + }); + await settle(); + + await commits.handleDomBoxSizeCommit(selection, next); + await settle(); + + expect(card.style.getPropertyValue("clip-path")).toBe("inset(20px 40px 20px 40px)"); + expect([card.style.width, card.style.height]).toEqual(["200px", "100px"]); +}); + it("rescales the crop with a size typed on an animated element, from the box the frame lays out", async () => { const { card, selection } = await sealedCard(saved()); card.style.setProperty("clip-path", "inset(10px 20px 10px 20px)"); diff --git a/packages/studio/src/hooks/useDomEditPositionPatchCommit.ts b/packages/studio/src/hooks/useDomEditPositionPatchCommit.ts index cd114bb60c9..679e4b4a26a 100644 --- a/packages/studio/src/hooks/useDomEditPositionPatchCommit.ts +++ b/packages/studio/src/hooks/useDomEditPositionPatchCommit.ts @@ -1,4 +1,5 @@ import { useCallback, type RefObject } from "react"; +import { DRAFT_DRAWN } from "@hyperframes/core/drawn-box"; import type { DomEditSelection } from "../components/editor/domEditing"; import { previewDraftLive } from "../components/editor/previewDrafts"; import { @@ -27,14 +28,20 @@ type PositionPatchOptions = { deferRender?: boolean; }; -/** A sealed frame shows the patch's own styles and attributes, unless a gesture's draft already does. */ +/** A sealed frame shows the patch's own styles and attributes, but for those a gesture's live draft draws. */ function drawOnSealedFrame( iframe: HTMLIFrameElement | null, selection: DomEditSelection, patches: PatchOperation[], ) { - if (previewDraftLive(selection.element)) return null; - const own = patches.filter((p) => p.childSelector === undefined && p.childIndex === undefined); + const drafted: readonly string[] = previewDraftLive(selection.element) ? DRAFT_DRAWN : []; + const own = patches.filter( + (p) => + p.childSelector === undefined && + p.childIndex === undefined && + !(p.type === "inline-style" && drafted.includes(p.property)), + ); + if (drafted.length && own.length === 0) return null; const entries = (type: PatchOperation["type"]) => own.filter((p) => p.type === type).map((p): [string, string | null] => [p.property, p.value]); const styles = entries("inline-style"); diff --git a/packages/studio/tests/e2e/edit-accuracy/case.mjs b/packages/studio/tests/e2e/edit-accuracy/case.mjs index 7c378e6ea34..6518fc54473 100644 --- a/packages/studio/tests/e2e/edit-accuracy/case.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/case.mjs @@ -1167,7 +1167,7 @@ export async function inStudio( } } -/** The saved crop's distance, in px, from the authored one scaled with the box: a crop follows a resize. Studio +/** A crop's distance, in px, from the authored one scaled with the box: a crop follows a resize. Studio * shows a selected element uncropped, so the crop before the gesture is the fixture's. */ function cropDrift(authored, pre, now) { const first = parseInset(authored); @@ -1254,7 +1254,10 @@ async function measureCase( spec.gesture, (frames[0] ?? []).map((f) => (spec.crop ? { ...f, clip: undefined } : f)), ), - ...(spec.crop && { crop: cropDrift(spec.crop, pre, reloaded) }), + // The crop shown right after the save too, not only once a reload redraws it. + ...(spec.crop && { + crop: Math.max(cropDrift(spec.crop, pre, committed), cropDrift(spec.crop, pre, reloaded)), + }), drop: quadDistance(drive.lastQuad, committed.visible), // Also against the box the gesture left, so a write the file drops shows here and not only as drop. reload: Math.max(