diff --git a/packages/core/src/runtime/editingAgent.test.ts b/packages/core/src/runtime/editingAgent.test.ts index 42443b28a9..9561f25554 100644 --- a/packages/core/src/runtime/editingAgent.test.ts +++ b/packages/core/src/runtime/editingAgent.test.ts @@ -150,6 +150,7 @@ describe("installEditingAgent", () => { "geometry", "layers", "draft", + "gesture-start", ], }, }); @@ -352,6 +353,13 @@ describe("installEditingAgent", () => { 150 + index, "invalid-params", ]), + ...[{}, { handles: [] }, { handles: ["1"] }, { handles: Array(257).fill(H) }].map( + (params, index) => [ + { v: 1, id: 170 + index, kind: "gesture-start", params }, + 170 + index, + "invalid-params", + ], + ), ...[["--"], ["--a b"], ["--a;b"], ["--a:b"], ["Color"], ["-x"], ["--" + "a".repeat(255)]].map( (properties, index) => [ { v: 1, id: 60 + index, kind: "computed-styles", params: { selector: "p", properties } }, diff --git a/packages/core/src/runtime/editingAgent.ts b/packages/core/src/runtime/editingAgent.ts index a21e15ae1e..a1d5aaddc2 100644 --- a/packages/core/src/runtime/editingAgent.ts +++ b/packages/core/src/runtime/editingAgent.ts @@ -31,6 +31,7 @@ import { elementGeometry } from "./editingGeometry"; import { pointerTargetsAt } from "./editingTargets"; import { layersOf } from "./editingLayers"; import { drawDrafts } from "./editingDraft"; +import { gestureStart } from "./editingGestureStart"; import { getLogicalOpacity } from "../studioPreviewMark"; type Handlers = { @@ -91,6 +92,7 @@ const HANDLERS: Handlers = { geometry: ({ handles, children }) => elementGeometry(handles, children), layers: ({ group, offset }) => layersOf(group, offset), draft: ({ drafts }) => drawDrafts(drafts), + "gesture-start": ({ handles }) => gestureStart(handles), }; function serve(port: MessagePort): void { diff --git a/packages/core/src/runtime/editingDraft.ts b/packages/core/src/runtime/editingDraft.ts index daf063f892..559aec1448 100644 --- a/packages/core/src/runtime/editingDraft.ts +++ b/packages/core/src/runtime/editingDraft.ts @@ -61,23 +61,25 @@ function percentAsPx(axis: string, side: number): string { ); } +export function computedTranslateAxes(element: Styled): string[] { + const computed = getComputedStyle(element); + const translate = computed.getPropertyValue("translate"); + if (!translate || translate === "none") return []; + const sides = [percentSide(computed, "width"), percentSide(computed, "height")]; + return axes(translate).map((axis, i) => (i < 2 ? percentAsPx(axis, sides[i]!) : axis)); +} + function beforeOf(element: Styled): Before { let saved = before.get(element); if (!saved) { const { style } = element; - const computed = getComputedStyle(element); - const translate = computed.getPropertyValue("translate"); - const sides = [percentSide(computed, "width"), percentSide(computed, "height")]; saved = { inline: DRAWN.map((name) => [ name, style.getPropertyValue(name), style.getPropertyPriority(name), ]), - translate: - translate && translate !== "none" - ? axes(translate).map((axis, i) => (i < 2 ? percentAsPx(axis, sides[i]!) : axis)) - : [], + translate: computedTranslateAxes(element), }; before.set(element, saved); } @@ -92,10 +94,15 @@ function restore(element: Styled, names: readonly string[]): void { } } +export function movedTranslate( + [x = "0px", y = "0px", z]: string[], + offset: { x: number; y: number }, +) { + return `calc(${x} + ${offset.x}px) calc(${y} + ${offset.y}px)${z ? ` ${z}` : ""}`; +} + function drawTranslate(element: Styled, offset: { x: number; y: number }): void { - const [x = "0px", y = "0px", z] = beforeOf(element).translate; - const value = `calc(${x} + ${offset.x}px) calc(${y} + ${offset.y}px)${z ? ` ${z}` : ""}`; - element.style.setProperty("translate", value); + element.style.setProperty("translate", movedTranslate(beforeOf(element).translate, offset)); } function drawSize(element: Styled, size: { width: number; height: number }): void { diff --git a/packages/core/src/runtime/editingGeometry.ts b/packages/core/src/runtime/editingGeometry.ts index 95ef39379c..7536d2a83e 100644 --- a/packages/core/src/runtime/editingGeometry.ts +++ b/packages/core/src/runtime/editingGeometry.ts @@ -206,7 +206,10 @@ const finiteCorners = (corners: EditingCorners | null) => ? corners : null; -function boxOf(element: HTMLElement, memo: Map): EditingElementBox { +export function boxOf( + element: HTMLElement, + memo: Map, +): EditingElementBox { const transform = readElementTransformSnapshot(window, element, memo); const turned = transform ? rotationDegreesFromMatrix(transform.matrix) : 0; const angle = Math.abs(turned) < ROTATION_GATE_EPSILON_DEG ? 0 : turned; @@ -220,6 +223,36 @@ function boxOf(element: HTMLElement, memo: Map): }; } +export const MAX_SNAP_TARGETS = 80; +const NOT_SNAPPED = new Set(["script", "style", "link", "meta", "base", "template", "br", "wbr"]); + +/** + * The elements a moved box can snap to, in document order below `root`: each rendered element + * outside `exclude`, not a nested composition's host, and not in a subtree that is hidden. Once + * `max` are found no further subtree is entered; the children of one being walked are all listed. + */ +export function snapCandidates( + root: Element, + exclude: ReadonlySet, + max = MAX_SNAP_TARGETS, +): HTMLElement[] { + const found: HTMLElement[] = []; + const visible = new Map(); + const visit = (parent: Element) => { + if (found.length >= max) return; + for (const child of Array.from(parent.children)) { + if (NOT_SNAPPED.has(child.tagName.toLowerCase())) continue; + if (child.hasAttribute("data-composition-id") || exclude.has(child)) continue; + const element = child as HTMLElement; + if (!isElementVisibleThroughAncestors(element, visible)) continue; + found.push(element); + visit(element); + } + }; + visit(root); + return found; +} + /** One entry per handle, null as in `describe`, and the boxes of `children`'s descendants. */ export function elementGeometry( handles: readonly EditingElementHandle[], diff --git a/packages/core/src/runtime/editingGestureStart.test.ts b/packages/core/src/runtime/editingGestureStart.test.ts new file mode 100644 index 0000000000..5165515178 --- /dev/null +++ b/packages/core/src/runtime/editingGestureStart.test.ts @@ -0,0 +1,132 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { gestureStart } from "./editingGestureStart"; +import { handleFor } from "./editingHandles"; + +type Tween = { vars: Record; targets: () => unknown[]; duration: () => number }; + +afterEach(() => { + document.body.innerHTML = ""; + Reflect.deleteProperty(window, "gsap"); + Reflect.deleteProperty(window, "__timelines"); +}); + +/** Each translate axis as the sum of its px terms (the test DOM keeps a calc() unsimplified). */ +const translatePx = (element: HTMLElement) => + element.style.translate + .split(/ (?![^(]*\))/) + .map((axis) => + [...axis.matchAll(/-?[\d.]+px/g)].reduce((sum, [px]) => sum + parseFloat(px), 0), + ); + +function boxed(element: Element, [x, y, width, height]: number[]): void { + element.getBoundingClientRect = () => + ({ x, y, width, height, left: x, top: y, right: x + width, bottom: y + height }) as DOMRect; +} + +/** An element under a parent turned a quarter and drawn at half size: translate x goes down, y goes left. */ +function turnedCard(style: string): HTMLElement { + document.body.innerHTML = `
`; + const card = document.getElementById("card")!; + Object.defineProperty(card, "offsetWidth", { value: 200 }); + Object.defineProperty(card, "offsetHeight", { value: 100 }); + card.getBoundingClientRect = () => { + const [tx = 0, ty = 0] = translatePx(card); + const [cx, cy] = [500 - ty / 2, 300 + tx / 2]; + return { + x: cx - 25, + y: cy - 50, + width: 50, + height: 100, + left: cx - 25, + top: cy - 50, + } as DOMRect; + }; + return card; +} + +function gsapWith(tweens: Tween[], position = { x: 200, y: 5 }) { + const getProperty = vi.fn((_: Element, axis: string) => position[axis as "x" | "y"]); + Object.assign(window, { + gsap: { getProperty }, + __timelines: { main: { getChildren: () => tweens } }, + }); + return getProperty; +} + +const tween = (target: unknown, vars: Record, duration = 0): Tween => ({ + vars, + targets: () => [target], + duration: () => duration, +}); + +describe("gesture start", () => { + it("reads a GSAP-held element's x/y, its layout size and how it moves, and puts its translate back", () => { + const card = turnedCard("translate: 10px 20px"); + Object.assign(card, { _gsap: { renderTransform: () => {} } }); + gsapWith([]); + const { elements } = gestureStart([handleFor(card)]); + expect(elements[0]).toMatchObject({ + layout: { width: 200, height: 100 }, + move: { a: 0, b: 0.5, c: -0.5, d: 0 }, + gsap: { x: 200, y: 5, moving: false }, + rect: { x: 465, y: 255, width: 50, height: 100 }, + }); + expect(card.getAttribute("style")).toBe("translate: 10px 20px;"); + }); + + it("leaves GSAP out of an element it does not move, without asking it for x", () => { + const card = turnedCard(""); + const getProperty = gsapWith([tween(document.body, { x: 10 }, 1)]); + const [start] = gestureStart([handleFor(card)]).elements; + expect(start!.gsap).toBeNull(); + expect(getProperty).not.toHaveBeenCalled(); + expect(card.getAttribute("style")).toBe(""); + }); + + it.each<[string, (card: HTMLElement) => Tween, boolean]>([ + ["a hold at its x", (card) => tween(card, { x: 40 }), false], + [ + "a tween along keyframes, named by id", + () => tween({ id: "card" }, { keyframes: [{ y: 1 }] }, 2), + true, + ], + ["a motion path", (card) => tween(card, { motionPath: { path: "M0 0" } }, 2), true], + ])("owns its position through %s", (_name, make, moving) => { + const card = turnedCard(""); + gsapWith([make(card), tween(card, { opacity: 0 }, 3)]); + expect(gestureStart([handleFor(card)]).elements[0]!.gsap).toEqual({ x: 200, y: 5, moving }); + }); + + it("does not own a box that only fades", () => { + const card = turnedCard(""); + gsapWith([tween(card, { opacity: 0, keyframes: { "50%": { scale: 2 } } }, 3)]); + expect(gestureStart([handleFor(card)]).elements[0]!.gsap).toBeNull(); + }); + + it("says it cannot measure a move for an element with no box", () => { + const card = turnedCard(""); + boxed(card, [0, 0, 0, 0]); + expect(gestureStart([handleFor(card)]).elements[0]!.move).toBeNull(); + }); + + it("lists the boxes a move can snap to: rendered, not pressed, not inside a nested composition", () => { + document.body.innerHTML = ` +
+
+
+ +
+ +
+
`; + const ids = ["a", "a1", "pressed", "inside", "under", "sub1", "z"]; + ids.forEach((id, i) => boxed(document.getElementById(id)!, [i * 10, 0, 5, 5])); + const pressed = document.getElementById("pressed")!; + const { snap } = gestureStart([handleFor(pressed), "0123456789abcdef:999"]); + expect(snap.map((box) => box.rect.x)).toEqual([0, 10, 60]); + }); + + it("answers null for an element this load does not have", () => { + expect(gestureStart(["0123456789abcdef:999"]).elements).toEqual([null]); + }); +}); diff --git a/packages/core/src/runtime/editingGestureStart.ts b/packages/core/src/runtime/editingGestureStart.ts new file mode 100644 index 0000000000..b42858dde0 --- /dev/null +++ b/packages/core/src/runtime/editingGestureStart.ts @@ -0,0 +1,142 @@ +/** + * What a drag or resize needs from the frame as it starts, in one read for the editing channel: each + * pressed element's overlay geometry, layout size, how far it moves per px of translate, and GSAP's + * x/y when GSAP owns its position; and the boxes it can snap to. A parent that cannot script the + * frame takes these here rather than off the elements. + */ +import { isStylableElement, type StylableElement } from "./domRealm"; +import type { EditingElementHandle } from "./editingElement"; +import { computedTranslateAxes, movedTranslate } from "./editingDraft"; +import { + boxOf, + elementGeometry, + snapCandidates, + MAX_SNAP_TARGETS, + type EditingElementBox, + type EditingElementGeometry, +} from "./editingGeometry"; +import { elementForHandle } from "./editingHandles"; + +export interface EditingGestureStart extends EditingElementGeometry { + /** The transform-free border box a resize grows from. */ + layout: { width: number; height: number }; + /** Frame px its centre moves per px of translate, columns `a, b` (x) and `c, d` (y); null when it does not move measurably. */ + move: { a: number; b: number; c: number; d: number } | null; + /** Set when GSAP owns its position: its x/y now, and whether a tween with a duration moves it. */ + gsap: { x: number; y: number; moving: boolean } | null; +} + +const PROBE_PX = 100; +const MIN_PROBE_RESPONSE = 0.01; +type Point = { x: number; y: number }; + +/** + * Frame px the element's centre moves per px of translate a draft draws, one column per axis + * (`a, b` for x, `c, d` for y): every transform above it applied. Null when it has no box or does + * not move measurably. Its inline translate is put back as it was. + */ +function measureMoveResponse(element: StylableElement): EditingGestureStart["move"] { + const { style } = element; + const value = style.getPropertyValue("translate"); + const priority = style.getPropertyPriority("translate"); + const base = computedTranslateAxes(element); + const centre = (offset: Point): Point | null => { + style.setProperty("translate", movedTranslate(base, offset)); + const { left, top, width, height } = element.getBoundingClientRect(); + return width > 0 || height > 0 ? { x: left + width / 2, y: top + height / 2 } : null; + }; + try { + const origin = centre({ x: 0, y: 0 }); + const alongX = centre({ x: PROBE_PX, y: 0 }); + const alongY = centre({ x: 0, y: PROBE_PX }); + if (!origin || !alongX || !alongY) return null; + const column = (p: Point) => ({ + x: (p.x - origin.x) / PROBE_PX, + y: (p.y - origin.y) / PROBE_PX, + }); + const [x, y] = [column(alongX), column(alongY)]; + const usable = (p: Point) => + Number.isFinite(p.x) && Number.isFinite(p.y) && Math.hypot(p.x, p.y) >= MIN_PROBE_RESPONSE; + return usable(x) && usable(y) ? { a: x.x, b: x.y, c: y.x, d: y.y } : null; + } finally { + if (value) style.setProperty("translate", value, priority); + else style.removeProperty("translate"); + } +} + +type GsapTween = { + vars?: Record; + targets?: () => unknown[]; + duration?: () => number; +}; +type GsapWindow = { + gsap?: { getProperty?: (element: Element, property: string) => unknown }; + __timelines?: Record GsapTween[] } | undefined>; +}; + +// The channels Studio's move route and drag commit read as GSAP moving an element. +const POSITION = ["x", "y", "xPercent", "yPercent", "left", "top", "translateX", "translateY"]; +const MOVES = [...POSITION, "motionPath"]; + +function carries(vars: Record, channels: string[]): boolean { + if (channels.some((channel) => channel in vars)) return true; + const keyframes = vars.keyframes; + if (!keyframes || typeof keyframes !== "object") return false; + const steps = Array.isArray(keyframes) ? keyframes : [keyframes, ...Object.values(keyframes)]; + return steps.some( + (step) => + !!step && + typeof step === "object" && + channels.some((channel) => Object.prototype.hasOwnProperty.call(step, channel)), + ); +} + +function tweensOn(element: Element): GsapTween[] { + const timelines = Object.values((window as GsapWindow).__timelines ?? {}); + return timelines.flatMap((timeline) => + (timeline?.getChildren?.(true) ?? []).filter((tween) => + (tween.targets?.() ?? []).some( + (target) => + target === element || (!!element.id && (target as Element | null)?.id === element.id), + ), + ), + ); +} + +/** GSAP's x/y when it owns the element's position: it renders its transform, or a tween writes it. */ +function gsapPosition(element: Element): EditingGestureStart["gsap"] { + const tweens = tweensOn(element).filter((tween) => tween.vars && carries(tween.vars, MOVES)); + const renders = !!(element as { _gsap?: { renderTransform?: unknown } })._gsap?.renderTransform; + if (!renders && !tweens.length) return null; + // Read only now: asking GSAP for x makes it fold a CSS translate into its transform. + const read = (axis: string) => + Number((window as GsapWindow).gsap?.getProperty?.(element, axis)) || 0; + const moving = tweens.some((tween) => (tween.duration?.() ?? 0) > 0); + return { x: read("x"), y: read("y"), moving }; +} + +/** Each handle's gesture start, null as in `describe`, and the snap boxes of the page around them. */ +export function gestureStart(handles: readonly EditingElementHandle[]): { + elements: Array; + snap: EditingElementBox[]; +} { + const pressed = handles.map(elementForHandle); + const { elements } = elementGeometry(handles, undefined); + const starts = elements.map((geometry, i) => { + const element = pressed[i]; + if (!geometry || !isStylableElement(element)) return null; + const { offsetWidth = 0, offsetHeight = 0 } = element as Partial; + return { + ...geometry, + layout: { width: offsetWidth, height: offsetHeight }, + move: measureMoveResponse(element), + gsap: gsapPosition(element), + }; + }); + const root = document.querySelector("[data-composition-id]") ?? document.documentElement; + const exclude = new Set(pressed.filter((element): element is Element => !!element)); + const memo = new Map(); + const candidates = snapCandidates(root, exclude).slice(0, MAX_SNAP_TARGETS); + const snap = candidates.map((element) => boxOf(element, memo)); + return { elements: starts, snap }; +} diff --git a/packages/core/src/runtime/editingPort.test.ts b/packages/core/src/runtime/editingPort.test.ts index 69cf7c7b98..9fdcfaf147 100644 --- a/packages/core/src/runtime/editingPort.test.ts +++ b/packages/core/src/runtime/editingPort.test.ts @@ -74,6 +74,7 @@ describe("createEditingPortClient", () => { "geometry", "layers", "draft", + "gesture-start", ], }); } finally { diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts index 03fe5fed37..149b18b47e 100644 --- a/packages/core/src/runtime/editingPort.ts +++ b/packages/core/src/runtime/editingPort.ts @@ -18,6 +18,7 @@ import { type EditingElementMatch, } from "./editingElement"; import type { EditingElementBox, EditingElementGeometry } from "./editingGeometry"; +import type { EditingGestureStart } from "./editingGestureStart"; import type { EditingLayer } from "./editingLayers"; import type { RuntimeTimelineMessage } from "./types"; import { wallClearTimeout, wallSetTimeout } from "./wallClock"; @@ -47,7 +48,9 @@ export { isElementVisibleThroughAncestors, readElementTransformSnapshot, rotationDegreesFromMatrix, + snapCandidates, MAX_GEOMETRY_CHILDREN, + MAX_SNAP_TARGETS, ROTATION_GATE_EPSILON_DEG, type EditingCorner, type EditingCorners, @@ -55,6 +58,7 @@ export { type EditingElementGeometry, type ElementTransformSnapshot, } from "./editingGeometry"; +export type { EditingGestureStart } from "./editingGestureStart"; export { compositionPoint, coversComposition, @@ -101,6 +105,7 @@ export const EDITING_REQUEST_KINDS = [ "geometry", "layers", "draft", + "gesture-start", ] as const; export type EditingRequestKind = (typeof EDITING_REQUEST_KINDS)[number]; @@ -183,6 +188,10 @@ export interface EditingRequests { }; /** Gesture drafts in order, up to MAX_DESCRIBE_HANDLES; `drawn` counts the elements still here. */ draft: { params: { drafts: EditingDraft[] }; result: { drawn: number } }; + "gesture-start": { + params: { handles: EditingElementHandle[] }; + result: { elements: Array; snap: EditingElementBox[] }; + }; } export type EditingParams = EditingRequests[K]["params"]; export type EditingResult = EditingRequests[K]["result"]; @@ -339,6 +348,7 @@ const PARAM_PARSERS: { }, layers: (params) => noParams(params) ?? layerParams(params), draft: draftList, + "gesture-start": handleList, }; /** Validates one request off the wire. Never throws: anything unusable becomes a typed error. */ diff --git a/packages/studio/src/components/editor/domEditOverlayGeometry.ts b/packages/studio/src/components/editor/domEditOverlayGeometry.ts index b58a82f4e0..e75b0b9fda 100644 --- a/packages/studio/src/components/editor/domEditOverlayGeometry.ts +++ b/packages/studio/src/components/editor/domEditOverlayGeometry.ts @@ -15,6 +15,7 @@ import { type ElementTransformSnapshot, } from "@hyperframes/core/runtime/editing-port"; import { type OverlayMeasurePass, readThroughPass } from "./domEditOverlayMeasurePass"; +import type { PreviewGestureStart } from "./previewGestureStartRead"; export const RESIZE_HANDLE_HIT_PX = 16; @@ -40,6 +41,7 @@ export interface GroupOverlayItem { selection: DomEditSelection; element: HTMLElement; rect: OverlayRect; + start?: PreviewGestureStart; } export type ResolvedElementRef = { diff --git a/packages/studio/src/components/editor/domEditOverlayStartGesture.ts b/packages/studio/src/components/editor/domEditOverlayStartGesture.ts index 371057186b..f2e87ddabb 100644 --- a/packages/studio/src/components/editor/domEditOverlayStartGesture.ts +++ b/packages/studio/src/components/editor/domEditOverlayStartGesture.ts @@ -34,7 +34,9 @@ import { type ResizeHandle, type UseDomEditOverlayGesturesOptions, } from "./domEditOverlayGestures"; -import { collectSnapContext, buildExcludeElements } from "./snapTargetCollection"; +import { collectSnapContext, buildExcludeElements, type SnapContext } from "./snapTargetCollection"; +import { MOVING_LAYER_UNREADABLE } from "./plainTranslate"; +import type { PreviewGestureStart } from "./previewGestureStartRead"; import { editsPlainCss } from "../../hooks/gsapRuntimeKeyframes"; import { logResize, resetResizeMoveLog } from "../../utils/resizeDebug"; import { logDrag, readDragPositions, resetDragMoveLog } from "../../utils/dragDebug"; @@ -52,6 +54,7 @@ export function startGroupDrag( e: React.PointerEvent, opts: UseDomEditOverlayGesturesOptions, at?: EditMoment, + snap?: SnapContext, ): boolean { const items = opts.groupOverlayItemsRef.current; if (items.length <= 1) return false; @@ -65,6 +68,13 @@ export function startGroupDrag( notifyBlockedPress(e, opts, blockedSelection); return false; } + const moving = items.find((item) => item.start?.gsap?.moving)?.selection; + if (moving) { + e.preventDefault(); + e.stopPropagation(); + opts.onBlockedMoveRef.current(moving, MOVING_LAYER_UNREADABLE); + return false; + } opts.onManualDragStartRef.current?.(); const dragItems = filterNestedDomEditGroupItems(items); @@ -77,6 +87,7 @@ export function startGroupDrag( rect: item.rect, gesture: "drag", at, + start: item.start, }); if (!result.ok) { restoreManualOffsetDragMembers(members); @@ -108,7 +119,8 @@ export function startGroupDrag( const overlayEl = opts.overlayRef.current; const iframe = opts.iframeRef.current; const snapContext = - overlayEl && iframe + snap ?? + (overlayEl && iframe ? collectSnapContext({ overlayEl, iframe, @@ -117,7 +129,7 @@ export function startGroupDrag( groupSelections: items.map((i) => i.selection), }), }) - : undefined; + : undefined); e.preventDefault(); e.stopPropagation(); @@ -150,9 +162,13 @@ export function startGesture( resizeHandle?: ResizeHandle; resizeCorner?: { x: number; y: number }; at?: EditMoment; + /** Read off a frame Studio cannot script at the press: the element's start and the snap targets. */ + start?: PreviewGestureStart; + snap?: SnapContext; }, ): boolean { const at = options?.at; + const start = options?.start; const sel = options?.selection ?? opts.selectionRef.current; const rect = options?.rect ?? opts.overlayRectRef.current; const box = opts.boxRef.current; @@ -172,8 +188,8 @@ export function startGesture( // layout size. offsetWidth/Height are transform-free; the overlay-rect // fallback (rect / editScale) includes the element's own GSAP scale and // would make a rescaled element's draft grow from the RENDERED size. - const layoutWidth = sel.element.offsetWidth; - const layoutHeight = sel.element.offsetHeight; + const layoutWidth = start?.layout.width ?? sel.element.offsetWidth; + const layoutHeight = start?.layout.height ?? sel.element.offsetHeight; const actualWidth = size.width > 0 ? size.width : layoutWidth > 0 ? layoutWidth : rect.width / rect.editScaleX; const actualHeight = @@ -196,6 +212,11 @@ export function startGesture( let pathOffsetMember: ManualOffsetDragMember | undefined; if (kind === "drag") { + if (start?.gsap?.moving) { + opts.onBlockedMoveRef.current(sel, MOVING_LAYER_UNREADABLE); + e.preventDefault(); + return false; + } opts.onManualDragStartRef.current?.(); const result = createManualOffsetDragMember({ key: selectionCacheKey(sel), @@ -204,6 +225,7 @@ export function startGesture( rect, gesture: "drag", at, + start, }); if (!result.ok) { opts.onBlockedMoveRef.current(result.selection, result.reason); @@ -226,6 +248,7 @@ export function startGesture( rect, gesture: "resize", at, + start, }); if (result.ok) { pathOffsetMember = result.member; @@ -267,11 +290,12 @@ export function startGesture( } const snapContext = (kind === "drag" || kind === "resize") && overlayEl && iframe - ? collectSnapContext({ + ? (options?.snap ?? + collectSnapContext({ overlayEl, iframe, excludeElements: buildExcludeElements({ iframe, selection: sel }), - }) + })) : undefined; e.preventDefault(); e.stopPropagation(); diff --git a/packages/studio/src/components/editor/manualOffsetDrag.ts b/packages/studio/src/components/editor/manualOffsetDrag.ts index 892d8e5c71..cb26b1fb52 100644 --- a/packages/studio/src/components/editor/manualOffsetDrag.ts +++ b/packages/studio/src/components/editor/manualOffsetDrag.ts @@ -18,6 +18,7 @@ import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition"; import { editsPlainCss } from "../../hooks/gsapRuntimeKeyframes"; import { readTranslatePx, UNREADABLE_TRANSLATE, writeTranslatePx } from "./plainTranslate"; import { drawPreviewDraft, endPreviewDraft } from "./previewDrafts"; +import type { PreviewGestureStart } from "./previewGestureStartRead"; interface OffsetDragGsap { set: (el: Element, vars: Record) => void; @@ -320,9 +321,13 @@ export function createManualOffsetDragMember(input: { rect: ManualOffsetDragRect; gesture?: "drag" | "nudge" | "resize"; // resize: the anchor that keeps its centre planted at?: EditMoment; + start?: PreviewGestureStart; }): ManualOffsetDragMemberResult { + const { start, gesture } = input; const plainTranslate = - !!input.gesture && editsPlainCss(input.element, input.gesture === "resize" ? "resize" : "move"); + start && gesture !== "resize" + ? !start.gsap + : !!gesture && editsPlainCss(input.element, gesture === "resize" ? "resize" : "move"); // The APPLIED offset, never a dormant var, so a stale one can't fling the element off-screen. const initialOffset = plainTranslate ? readTranslatePx(input.element) @@ -338,7 +343,7 @@ export function createManualOffsetDragMember(input: { // getProperty makes GSAP bake the CSS translate into its transform, so a plain move never reads it. const gsapX = (!plainTranslate && win?.gsap?.getProperty?.(input.element, "x")) || 0; const gsapY = (!plainTranslate && win?.gsap?.getProperty?.(input.element, "y")) || 0; - const baseGsap = { x: gsapX, y: gsapY }; + const baseGsap = start?.gsap ? { x: start.gsap.x, y: start.gsap.y } : { x: gsapX, y: gsapY }; if (!plainTranslate) stampGestureBase(input.element, initialOffset, baseGsap); if (win?.__timelines) { @@ -371,8 +376,8 @@ export function createManualOffsetDragMember(input: { }); endPreviewDraft(input.element, "keep"); if (!measured.ok) { - // GSAP transforms can defeat the probe: approximate with the preview scale. The commit - // reads GSAP's real position, so only the drag preview is imprecise. + // A stand-in has no box: the frame probed its element at the press. GSAP transforms can defeat + // the probe: approximate with the preview scale; the commit reads GSAP's real position. const scaleX = input.rect.editScaleX || 1; const scaleY = input.rect.editScaleY || 1; const w = readTransformWDivisor(input.element); @@ -387,7 +392,7 @@ export function createManualOffsetDragMember(input: { baseGsap, initialPathOffset, gestureToken, - screenToOffset: { a: w / scaleX, b: 0, c: 0, d: w / scaleY }, + screenToOffset: start?.screenToOffset ?? { a: w / scaleX, b: 0, c: 0, d: w / scaleY }, originRect: input.rect, }, }; diff --git a/packages/studio/src/components/editor/plainTranslate.ts b/packages/studio/src/components/editor/plainTranslate.ts index 640aa97ec9..c953b1399a 100644 --- a/packages/studio/src/components/editor/plainTranslate.ts +++ b/packages/studio/src/components/editor/plainTranslate.ts @@ -128,6 +128,8 @@ export function readTranslatePxLeavingPercent(el: HTMLElement): Point { export const UNREADABLE_TRANSLATE = "Studio can't read this layer's translate. Move it in the Code tab."; +export const MOVING_LAYER_UNREADABLE = + "This preview can't save a move of a layer its animation moves yet. Move it in the Code tab."; /** Plain px only: GSAP's CSSPlugin splits `translate` on spaces and drops a calc(). */ function formatTranslatePx(p: Point): string { diff --git a/packages/studio/src/components/editor/previewGestureStartRead.test.ts b/packages/studio/src/components/editor/previewGestureStartRead.test.ts new file mode 100644 index 0000000000..4a48d0d71d --- /dev/null +++ b/packages/studio/src/components/editor/previewGestureStartRead.test.ts @@ -0,0 +1,279 @@ +// @vitest-environment happy-dom + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +// The frame's real runtime agent, serving the opaque frame from this document. +import { installEditingAgent } from "../../../../core/src/runtime/editingAgent"; +import { makeSelection } from "../../hooks/domSelectionTestHarness"; +import { computeDraggedGsapPosition } from "../../hooks/draggedGsapPosition"; +import { watchPreviewFrame } from "../../player/lib/bridgePlayback"; +import { describePreviewElements, queryPreviewElements } from "../../player/lib/previewElements"; +import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness"; +import type { UseDomEditOverlayGesturesOptions } from "./domEditOverlayGestures"; +import { startGesture, startGroupDrag } from "./domEditOverlayStartGesture"; +import { + applyManualOffsetDragCommit, + createManualOffsetDragMember, + restoreManualOffsetDragMembers, +} from "./manualOffsetDrag"; +import { MOVING_LAYER_UNREADABLE } from "./plainTranslate"; +import { measurePreviewGestureStart, type PreviewGestureStart } from "./previewGestureStartRead"; +import { standInFor } from "./previewSelection"; +import { collectSnapContext } from "./snapTargetCollection"; + +const FIXTURE = ` +
+
+
+

+
+
+
+
+
+
`; +const RECTS: Record = { + host: [200, 100, 960, 540], + inner: [220, 140, 300, 80], + clipped: [900, 700, 400, 300], + gone: [1, 1, 50, 50], + sub: [5, 5, 50, 50], + nested: [6, 6, 10, 10], + last: [1500, 900, 120, 60], +}; + +function sized(element: Element, [x, y, width, height]: number[]) { + element.getBoundingClientRect = () => + ({ x, y, width, height, left: x, top: y, right: x + width, bottom: y + height }) as DOMRect; + Object.defineProperty(element, "offsetWidth", { value: width, configurable: true }); + Object.defineProperty(element, "offsetHeight", { value: height, configurable: true }); + return element; +} + +/** The card sits in a mirrored parent: a px of translate x moves it a px left, y moves it down. */ +function mirroredCard(card: HTMLElement) { + sized(card, [0, 0, 200, 100]); + card.getBoundingClientRect = () => { + const [x = 0, y = 0] = card.style.translate + .split(/ (?![^(]*\))/) + .map((axis) => + [...axis.matchAll(/-?[\d.]+px/g)].reduce((sum, [px]) => sum + Number.parseFloat(px), 0), + ); + const left = 600 - x; + const top = 300 + y; + return { x: left, y: top, width: 200, height: 100, left, top } as DOMRect; + }; +} + +const closers: Array<() => void> = []; +beforeEach(() => { + document.body.innerHTML = FIXTURE; + for (const [id, rect] of Object.entries(RECTS)) sized(document.getElementById(id)!, rect); + mirroredCard(document.getElementById("card")!); +}); +afterEach(() => { + closers.splice(0).forEach((close) => close()); + document.body.innerHTML = ""; + Reflect.deleteProperty(window, "gsap"); + Reflect.deleteProperty(window, "__timelines"); +}); + +/** A frame Studio scripts directly; its document and window are this one's. */ +function liveFrame() { + const iframe = detachedFakeIframe(window as unknown as Record); + Object.defineProperty(iframe, "contentDocument", { value: document }); + return sized(iframe, [100, 50, 960, 540]) as HTMLIFrameElement; +} + +/** A frame Studio cannot read, whose runtime is the real editing agent over this document. */ +function opaqueFrame() { + const frame: Record = {}; + const iframe = sized(detachedFakeIframe(frame), [100, 50, 960, 540]) as HTMLIFrameElement; + Object.defineProperty(iframe, "contentDocument", { value: null }); + Object.defineProperty(frame, "document", { + get: () => { + throw new DOMException("blocked", "SecurityError"); + }, + }); + const parent = { + postMessage: (data: unknown, _origin: string, ports: MessagePort[]) => + window.dispatchEvent( + new MessageEvent("message", { source: frame as unknown as Window, data, ports }), + ), + } as unknown as Window; + const agent = installEditingAgent(parent); + frame.postMessage = (data: unknown) => + window.dispatchEvent(new MessageEvent("message", { source: parent, data })); + closers.push(() => agent.dispose()); + watchPreviewFrame(iframe); + window.dispatchEvent( + new MessageEvent("message", { + source: frame as unknown as Window, + origin: window.location.origin, + data: { source: "hf-preview", type: "stage-size", width: 1920, height: 1080 }, + }), + ); + return iframe; +} + +const overlayEl = () => sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement; +const round = (value: unknown) => + JSON.parse(JSON.stringify(value, (_, v: unknown) => (typeof v === "number" ? +v.toFixed(9) : v))); + +async function opaqueCard() { + const iframe = opaqueFrame(); + const handle = (await queryPreviewElements(iframe, "#card"))!.matches[0]!.handle; + const description = (await describePreviewElements(iframe, [handle]))![0]!; + return { iframe, handle, standIn: standInFor(iframe, description) }; +} + +/** GSAP holds the card at x 200, y 5, as `gsap.set` leaves it. */ +function gsapHoldsCard(tweens: unknown[] = []) { + const card = document.getElementById("card")!; + Object.assign(card, { _gsap: { renderTransform: () => {} } }); + Object.assign(window, { + gsap: { getProperty: (_: Element, axis: string) => (axis === "x" ? 200 : 5) }, + __timelines: { main: { getChildren: () => tweens } }, + }); +} + +describe("a gesture's start on an opaque frame", () => { + it("reads the snap targets a scriptable frame collects, without the pressed element", async () => { + const overlay = overlayEl(); + const card = document.getElementById("card")!; + const live = collectSnapContext({ + overlayEl: overlay, + iframe: liveFrame(), + excludeElements: new Set([card]), + }); + const { iframe, handle } = await opaqueCard(); + const read = await measurePreviewGestureStart(iframe, overlay, [handle]); + expect(round(read!.snap)).toEqual(round(live)); + // host, inner, clipped, last: the hidden span, the nested composition and the card are left out. + expect(live.targets).toHaveLength(4); + expect(live.compositionTarget).not.toBeNull(); + }); + + it("reads GSAP's x/y, the layout size and the frame's move, so a drag saves from where it is", async () => { + gsapHoldsCard(); + const { iframe, handle, standIn } = await opaqueCard(); + const read = await measurePreviewGestureStart(iframe, overlayEl(), [handle]); + const { rect, start } = read!.elements[0]!; + // Mirrored x at a 0.5 page scale: a pointer px left is 2 translate px right. + expect(start).toEqual({ + layout: { width: 200, height: 100 }, + screenToOffset: { a: -2, b: 0, c: 0, d: 2 }, + gsap: { x: 200, y: 5, moving: false }, + }); + const selection = { ...makeSelection("card", standIn), handle }; + const made = createManualOffsetDragMember({ + key: "card", + selection, + element: standIn, + rect, + gesture: "drag", + start, + }); + if (!made.ok) throw new Error(made.reason); + closers.push(() => restoreManualOffsetDragMembers([made.member])); + expect(made.member.plainTranslate).toBe(false); + // The release stamps the gesture's base and offset, as the GSAP commit reads them. + const offset = applyManualOffsetDragCommit(made.member, 10, 0); + expect(offset).toEqual({ x: -20, y: 0 }); + // GSAP's own x plus the drag, not the drag alone. + expect(computeDraggedGsapPosition(standIn, offset, { x: 0, y: 0 }).newX).toBe(180); + }); + + it("moves an element GSAP does not own by its translate, as before", async () => { + const { iframe, handle } = await opaqueCard(); + const read = await measurePreviewGestureStart(iframe, overlayEl(), [handle]); + expect(read!.elements[0]!.start.gsap).toBeNull(); + }); +}); + +function gestureOpts(selection: ReturnType) { + const ref = (current: T) => ({ current }); + return { + selectionRef: ref(selection), + overlayRectRef: ref({ + left: 0, + top: 0, + width: 50, + height: 25, + editScaleX: 0.5, + editScaleY: 0.5, + }), + boxRef: ref(document.createElement("div")), + overlayRef: ref(overlayEl()), + iframeRef: ref(liveFrame()), + gestureRef: ref(null), + groupGestureRef: ref(null), + groupOverlayItemsRef: ref([]), + rafPausedRef: ref(false), + onManualDragStartRef: ref(vi.fn()), + onBlockedMoveRef: ref(vi.fn()), + } as unknown as UseDomEditOverlayGesturesOptions; +} +const press = () => + ({ + clientX: 10, + clientY: 10, + pointerId: 1, + button: 0, + preventDefault() {}, + stopPropagation() {}, + currentTarget: { setPointerCapture() {} }, + }) as unknown as React.PointerEvent; +const START: PreviewGestureStart = { + layout: { width: 200, height: 100 }, + screenToOffset: { a: 2, b: 0, c: 0, d: 2 }, + gsap: null, +}; +const SNAP = { targets: [], compositionTarget: null, gridEdges: null, snapEnabled: true }; + +describe("starting a gesture from what an opaque frame read", () => { + it("grows a resize from the frame's layout size, and snaps with the targets it read", () => { + const element = document.createElement("div"); + const opts = gestureOpts(makeSelection("card", element)); + expect(startGesture("resize", press(), opts, { start: START, snap: SNAP })).toBe(true); + // The element has no box here; its rect over the edit scale would give 100 x 50. + const g = opts.gestureRef.current!; + expect([g.actualWidth, g.actualHeight]).toEqual([200, 100]); + expect(g.snapContext).toBe(SNAP); + restoreManualOffsetDragMembers(g.pathOffsetMember ? [g.pathOffsetMember] : []); + }); + + it("refuses a drag of a layer a tween moves, which this preview cannot save yet", () => { + const element = document.createElement("div"); + const selection = makeSelection("card", element); + const opts = gestureOpts(selection); + const moving = { ...START, gsap: { x: 0, y: 0, moving: true } }; + expect(startGesture("drag", press(), opts, { start: moving })).toBe(false); + expect(opts.onBlockedMoveRef.current).toHaveBeenCalledWith(selection, MOVING_LAYER_UNREADABLE); + expect(opts.gestureRef.current).toBeNull(); + }); + + it("drags a group from each member's GSAP position, snapping with the targets it read", () => { + const items = ["a", "b"].map((key, i) => { + const element = document.createElement("div"); + const gsap = { x: 100 * (i + 1), y: 0, moving: false }; + const rect = { left: 0, top: 0, width: 10, height: 10, editScaleX: 1, editScaleY: 1 }; + return { + key, + selection: makeSelection(key, element), + element, + rect, + start: { ...START, gsap }, + }; + }); + const opts = gestureOpts(items[0]!.selection); + opts.groupOverlayItemsRef.current = items; + expect(startGroupDrag(press(), opts, undefined, SNAP)).toBe(true); + const g = opts.groupGestureRef.current!; + expect(g.snapContext).toBe(SNAP); + expect(g.members.map((member) => [member.plainTranslate, member.baseGsap.x])).toEqual([ + [false, 100], + [false, 200], + ]); + restoreManualOffsetDragMembers(g.members); + }); +}); diff --git a/packages/studio/src/components/editor/previewGestureStartRead.ts b/packages/studio/src/components/editor/previewGestureStartRead.ts new file mode 100644 index 0000000000..194e4c722f --- /dev/null +++ b/packages/studio/src/components/editor/previewGestureStartRead.ts @@ -0,0 +1,67 @@ +/** A gesture on a frame Studio cannot script starts from one `gesture-start` read (core `editingGestureStart.ts`). */ +import { previewGestureStart } from "../../player/lib/previewElements"; +import { computeOverlayRootScale } from "./domEditOverlayBasis"; +import type { OverlayRect } from "./domEditOverlayGeometry"; +import { invertManualOffsetDragMatrix, type ManualOffsetDragMatrix } from "./manualOffsetDrag"; +import { flatVisibleRect, groupAwareRect } from "./previewOverlayRects"; +import { snapContextFromRects, type SnapContext } from "./snapTargetCollection"; + +export interface PreviewGestureStart { + /** The transform-free border box a resize grows from; 0 for an element with none. */ + layout: { width: number; height: number }; + /** Pointer px to translate px, as `measureManualOffsetDragScreenToOffsetMatrix`; null when unmeasured. */ + screenToOffset: ManualOffsetDragMatrix | null; + /** GSAP's x/y when it owns the element's position, and whether a tween with a duration moves it. */ + gsap: { x: number; y: number; moving: boolean } | null; +} + +export interface PreviewGestureStarts { + elements: Array<{ rect: OverlayRect; start: PreviewGestureStart } | null>; + snap: SnapContext; +} + +/** Everything a gesture on `handles` reads from the frame, in one request; null when it cannot answer. */ +export async function measurePreviewGestureStart( + iframe: HTMLIFrameElement, + overlayEl: HTMLDivElement, + handles: string[], +): Promise { + const reply = await previewGestureStart(iframe, handles); + const scale = reply && computeOverlayRootScale(overlayEl, iframe, null); + if (!reply || !scale) return null; + const { iframeRect, overlayRect, rootScaleX: sx, rootScaleY: sy } = scale; + // The frame measures its own px; a frame px is `rootScaleX` by `rootScaleY` pointer px on the page. + const screenToOffset = (move: { a: number; b: number; c: number; d: number } | null) => + move && + invertManualOffsetDragMatrix({ + a: move.a * sx, + b: move.b * sy, + c: move.c * sx, + d: move.d * sy, + }); + const composition: OverlayRect = { + left: iframeRect.left - overlayRect.left, + top: iframeRect.top - overlayRect.top, + width: iframeRect.width, + height: iframeRect.height, + editScaleX: sx, + editScaleY: sy, + }; + return { + elements: reply.elements.map( + (start) => + start && { + rect: groupAwareRect(scale, start, true), + start: { + layout: start.layout, + screenToOffset: screenToOffset(start.move), + gsap: start.gsap, + }, + }, + ), + snap: snapContextFromRects( + composition, + reply.snap.map((box) => flatVisibleRect(scale, box)), + ), + }; +} diff --git a/packages/studio/src/components/editor/previewGestureStarts.ts b/packages/studio/src/components/editor/previewGestureStarts.ts index 411d6a4509..cbf92c71a5 100644 --- a/packages/studio/src/components/editor/previewGestureStarts.ts +++ b/packages/studio/src/components/editor/previewGestureStarts.ts @@ -23,14 +23,16 @@ import { playheadMoment } from "../../hooks/editMoment"; import type { EditMoment } from "./manualEditsTypes"; import { previewFrameOpaque } from "../../player/lib/previewChannel"; import { findPreviewElementForSelection } from "./previewSelection"; -import { measurePreviewRects } from "./previewOverlayRects"; +import { measurePreviewGestureStart, type PreviewGestureStart } from "./previewGestureStartRead"; +import type { SnapContext } from "./snapTargetCollection"; -type Shown = { selection: DomEditSelection; rect: OverlayRect }; +type Shown = { selection: DomEditSelection; rect: OverlayRect; start?: PreviewGestureStart }; +type OverChannel = { shown: Shown[]; snap: SnapContext }; type ShownStart = ( e: React.PointerEvent, at: EditMoment, waited: boolean, - overChannel?: Shown[], + overChannel?: OverChannel, ) => boolean; const MAX_SETTLED_WAIT_FRAMES = 60; @@ -139,11 +141,11 @@ export function createPreviewGestureStarts( }; }; - /** An opaque frame's `selections` on the page it shows now, read over its channel; null when any is gone. */ + /** An opaque frame's `selections` on the page it shows now and what a gesture on them starts from. */ const shownOverChannel = async ( iframe: HTMLIFrameElement, selections: DomEditSelection[], - ): Promise => { + ): Promise => { const overlay = opts.overlayRef.current; const path = opts.activeCompositionPathRef.current; const found = await Promise.all( @@ -151,12 +153,12 @@ export function createPreviewGestureStarts( ); const handles = found.map((element) => element?.handle); if (!overlay || !handles.length || !handles.every(Boolean)) return null; - const rects = await measurePreviewRects(iframe, overlay, handles as string[]); + const read = await measurePreviewGestureStart(iframe, overlay, handles as string[]); const shown = selections.flatMap((selection, i) => { - const rect = rects?.[i]; - return rect ? [{ selection: { ...selection, handle: handles[i] }, rect }] : []; + const element = read?.elements[i]; + return element ? [{ selection: { ...selection, handle: handles[i] }, ...element }] : []; }); - return shown.length === selections.length ? shown : null; + return read && shown.length === selections.length ? { shown, snap: read.snap } : null; }; // A press on the page a reload is replacing would edit it by its old rules: it starts on the new page instead. @@ -168,7 +170,10 @@ export function createPreviewGestureStarts( targets: () => DomEditSelection[] = () => [], ): boolean => { const at = playheadMoment(); - if (!isPreviewChanging() && !opts.waitingPressRef.current) return start(e, at, false); + const iframe = opts.iframeRef.current; + const channel = iframe && previewFrameOpaque(iframe) ? iframe : null; + if (!channel && !isPreviewChanging() && !opts.waitingPressRef.current) + return start(e, at, false); e.preventDefault(); e.stopPropagation(); e.currentTarget.setPointerCapture(e.pointerId); @@ -203,9 +208,7 @@ export function createPreviewGestureStarts( return elements.length; }; // An opaque frame's pressed elements are found over its channel, once the preview settles. - const iframe = opts.iframeRef.current; - const channel = iframe && previewFrameOpaque(iframe) ? iframe : null; - let overChannel: Shown[] | null | undefined; + let overChannel: OverChannel | null | undefined; let asked = false; const channelStep = (frame: HTMLIFrameElement): "wait" | "lost" | "run" => { const overdue = performance.now() - since > PRESS_WAIT_MAX_MS; @@ -275,11 +278,12 @@ export function createPreviewGestureStarts( (pressed, at, waited, overChannel) => { if (!waited) return _startGroupDrag(pressed, opts, at); if (overChannel) { - const measured = items.map((item, i) => ({ ...item, ...overChannel[i]! })); + const measured = items.map((item, i) => ({ ...item, ...overChannel.shown[i]! })); return _startGroupDrag( pressed, { ...opts, groupOverlayItemsRef: { current: measured } }, at, + overChannel.snap, ); } const overlay = opts.overlayRef.current; @@ -335,7 +339,7 @@ export function createPreviewGestureStarts( }, (pressed, at, waited, overChannel) => { if (!waited) return _startGesture(kind, pressed, opts, { ...options, at }); - const shown = overChannel ? overChannel[0] : shownOnPage(target); + const shown = overChannel ? overChannel.shown[0] : shownOnPage(target); return ( !!shown && _startGesture(kind, pressed, opts, { @@ -344,6 +348,8 @@ export function createPreviewGestureStarts( resizeHandle: options?.resizeHandle, resizeCorner: options?.resizeCorner, at, + start: shown.start, + snap: overChannel?.snap, }) ); }, diff --git a/packages/studio/src/components/editor/previewOverlayRects.test.ts b/packages/studio/src/components/editor/previewOverlayRects.test.ts index 99b56d368e..ad8a1bfc8f 100644 --- a/packages/studio/src/components/editor/previewOverlayRects.test.ts +++ b/packages/studio/src/components/editor/previewOverlayRects.test.ts @@ -314,3 +314,65 @@ describe("a press held across a preview change on an opaque frame", () => { expect(press.start).not.toHaveBeenCalled(); }); }); + +describe("a press on a settled preview", () => { + function pressOn(iframe: HTMLIFrameElement, selection: DomEditSelection) { + press.changing = false; + press.start.mockClear(); + const opts = { + waitingPressRef: { current: null }, + boxRef: { current: null }, + rafPausedRef: { current: false }, + iframeRef: { current: iframe }, + overlayRef: { current: sized(document.createElement("div"), [20, 10, 1200, 800]) }, + activeCompositionPathRef: { current: "index.html" }, + selectionRef: { current: selection }, + overlayRectRef: { current: null }, + setOverlayRect: vi.fn(), + } as unknown as UseDomEditOverlayGesturesOptions; + const { startGesture } = createPreviewGestureStarts(opts, vi.fn(), vi.fn()); + const down = { + pointerId: 1, + clientX: 5, + clientY: 6, + currentTarget: { setPointerCapture: vi.fn() }, + preventDefault: vi.fn(), + stopPropagation: vi.fn(), + }; + startGesture("drag", down as unknown as React.PointerEvent); + return opts; + } + + it("on a scriptable frame starts the gesture at once", () => { + pressOn(liveFrame(), makeSelection("turned", document.getElementById("turned")!)); + expect(press.start).toHaveBeenCalledOnce(); + // Synchronously, with nothing read over a channel. + expect(press.start.mock.calls[0]![3]).toEqual({ at: expect.any(Object) }); + }); + + it("on an opaque frame waits for what the gesture starts from, read in one request", async () => { + const { port1 } = new MessageChannel(); + const send = vi.spyOn(Object.getPrototypeOf(port1) as MessagePort, "postMessage"); + const kinds = () => send.mock.calls.map(([message]) => (message as { kind?: string }).kind); + const iframe = opaqueFrame(); + const handle = (await queryPreviewElements(iframe, "#turned"))!.matches[0]!.handle; + const turned = document.getElementById("turned")!; + send.mockClear(); + const opts = pressOn(iframe, { ...makeSelection("turned", turned), handle }); + expect(press.start).not.toHaveBeenCalled(); + await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce()); + const started = press.start.mock.calls[0]![3] as { + rect: OverlayRect; + start: { layout: { width: number } }; + snap: { targets: unknown[] }; + }; + expect(started.start.layout.width).toBe(300); + expect(started.snap.targets.length).toBeGreaterThan(0); + const overlay = opts.overlayRef.current!; + const live = orientedGroupAwareOverlayRect(overlay, liveFrame(), turned); + expect(close(started.rect)).toEqual(close(live)); + expect(kinds().filter((kind) => kind === "gesture-start")).toHaveLength(1); + expect(kinds()).not.toContain("geometry"); + send.mockRestore(); + }); +}); diff --git a/packages/studio/src/components/editor/previewOverlayRects.ts b/packages/studio/src/components/editor/previewOverlayRects.ts index abaa2eccf4..d832394427 100644 --- a/packages/studio/src/components/editor/previewOverlayRects.ts +++ b/packages/studio/src/components/editor/previewOverlayRects.ts @@ -39,7 +39,7 @@ function orientedRect(scale: OverlayRootScale, box: EditingElementBox): OverlayR } /** `groupAwareOverlayRect`, or with `oriented`, `orientedGroupAwareOverlayRect`. */ -function groupAwareRect( +export function groupAwareRect( scale: OverlayRootScale, geometry: EditingElementGeometry, oriented: boolean, @@ -54,6 +54,12 @@ function groupAwareRect( return union ? { ...union, editScaleX: rect.editScaleX, editScaleY: rect.editScaleY } : rect; } +/** `toVisibleOverlayRect`. */ +export function flatVisibleRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect { + const rect = baseRect(scale, box); + return { ...rect, ...hugRectForClipPath(rect, box.clipPath) }; +} + /** `orientedVisibleOverlayRect`. */ function visibleRect(scale: OverlayRootScale, box: EditingElementBox): OverlayRect { const rect = orientedRect(scale, box); @@ -111,18 +117,6 @@ export async function measurePreviewOverlay( }; } -/** `orientedGroupAwareOverlayRect` for each handle; null when the frame cannot answer. */ -export async function measurePreviewRects( - iframe: HTMLIFrameElement, - overlayEl: HTMLDivElement, - handles: string[], -): Promise | null> { - const reply = await previewGeometry(iframe, handles); - const scale = reply && computeOverlayRootScale(overlayEl, iframe, null); - if (!reply || !scale) return null; - return reply.elements.map((geometry) => geometry && groupAwareRect(scale, geometry, true)); -} - /** `toVisibleOverlayRect` for each handle, with its frame rect and whether it renders. */ export async function measurePreviewVisibleRects( iframe: HTMLIFrameElement, @@ -135,14 +129,8 @@ export async function measurePreviewVisibleRects( const scale = reply && computeOverlayRootScale(overlayEl, iframe, null); if (!reply || !scale) return null; for (const box of reply.elements) { - const rect = box && baseRect(scale, box); measured.push( - box && - rect && { - rect: { ...rect, ...hugRectForClipPath(rect, box.clipPath) }, - frame: box.rect, - visible: box.visible, - }, + box && { rect: flatVisibleRect(scale, box), frame: box.rect, visible: box.visible }, ); } } diff --git a/packages/studio/src/components/editor/snapTargetCollection.ts b/packages/studio/src/components/editor/snapTargetCollection.ts index 3988b43cd5..4ce783b40a 100644 --- a/packages/studio/src/components/editor/snapTargetCollection.ts +++ b/packages/studio/src/components/editor/snapTargetCollection.ts @@ -1,10 +1,7 @@ // fallow-ignore-file code-duplication +import { snapCandidates } from "@hyperframes/core/runtime/editing-port"; import type { DomEditSelection } from "./domEditing"; -import { - isElementVisibleForOverlay, - toVisibleOverlayRect, - type OverlayRect, -} from "./domEditOverlayGeometry"; +import { toVisibleOverlayRect, type OverlayRect } from "./domEditOverlayGeometry"; import { extractSnapTargets, buildCompositionSnapTarget, @@ -27,33 +24,23 @@ function readPositiveDimension(value: string | null): number | null { return Number.isFinite(parsed) && parsed > 0 ? parsed : null; } -const IGNORED_TAGS = new Set(["script", "style", "link", "meta", "base", "template", "br", "wbr"]); - -function isHtmlElement(node: Node): node is HTMLElement { - return node.nodeType === 1; -} - -function collectVisibleElements( - root: HTMLElement, - excludeElements: Set, - maxItems: number, -): HTMLElement[] { - const result: HTMLElement[] = []; - // fallow-ignore-next-line complexity - const visit = (el: HTMLElement) => { - if (result.length >= maxItems) return; - for (const child of Array.from(el.children)) { - if (!isHtmlElement(child)) continue; - if (IGNORED_TAGS.has(child.tagName.toLowerCase())) continue; - if (child.hasAttribute("data-composition-id")) continue; - if (excludeElements.has(child)) continue; - if (!isElementVisibleForOverlay(child)) continue; - result.push(child); - visit(child); - } +export function snapContextFromRects( + composition: OverlayRect, + rects: ReadonlyArray, +): SnapContext { + const prefs = readStudioUiPreferences(); + const entries = rects.flatMap((rect, i) => (rect ? [{ rect, id: `snap-target-${i}` }] : [])); + const gridSpacing = prefs.gridSpacing ?? 50; + const snapToGrid = prefs.snapToGrid ?? false; + return { + targets: extractSnapTargets(entries), + compositionTarget: buildCompositionSnapTarget(composition), + gridEdges: + snapToGrid && gridSpacing > 0 + ? buildGridSnapEdges(composition, gridSpacing, composition.editScaleX) + : null, + snapEnabled: prefs.snapEnabled ?? true, }; - visit(root); - return result; } // fallow-ignore-next-line complexity @@ -62,8 +49,7 @@ export function collectSnapContext(input: { iframe: HTMLIFrameElement; excludeElements: Set; }): SnapContext { - const prefs = readStudioUiPreferences(); - const snapEnabled = prefs.snapEnabled ?? true; + const snapEnabled = readStudioUiPreferences().snapEnabled ?? true; const doc = input.iframe.contentDocument; if (!doc) { @@ -84,41 +70,19 @@ export function collectSnapContext(input: { const iframeRect = input.iframe.getBoundingClientRect(); const overlayRect = input.overlayEl.getBoundingClientRect(); - const rootScaleX = iframeRect.width / rootWidth; - const rootScaleY = iframeRect.height / rootHeight; - const compositionOverlayRect: OverlayRect = { left: iframeRect.left - overlayRect.left, top: iframeRect.top - overlayRect.top, width: iframeRect.width, height: iframeRect.height, - editScaleX: rootScaleX, - editScaleY: rootScaleY, + editScaleX: iframeRect.width / rootWidth, + editScaleY: iframeRect.height / rootHeight, }; - const compositionTarget = buildCompositionSnapTarget(compositionOverlayRect); - - const MAX_SNAP_TARGETS = 80; - const elements = collectVisibleElements(root, input.excludeElements, MAX_SNAP_TARGETS); - - const entries: Array<{ - rect: { left: number; top: number; width: number; height: number }; - id: string; - }> = []; - for (let i = 0; i < elements.length; i++) { - const rect = toVisibleOverlayRect(input.overlayEl, input.iframe, elements[i]); - if (rect) entries.push({ rect, id: `snap-target-${i}` }); - } - - const targets = extractSnapTargets(entries); - - let gridEdges: { x: SnapEdge[]; y: SnapEdge[] } | null = null; - const gridSpacing = prefs.gridSpacing ?? 50; - const snapToGrid = prefs.snapToGrid ?? false; - if (snapToGrid && gridSpacing > 0) { - gridEdges = buildGridSnapEdges(compositionOverlayRect, gridSpacing, rootScaleX); - } - - return { targets, compositionTarget, gridEdges, snapEnabled }; + const elements = snapCandidates(root, input.excludeElements); + const rects = elements.map((element) => + toVisibleOverlayRect(input.overlayEl, input.iframe, element), + ); + return snapContextFromRects(compositionOverlayRect, rects); } // fallow-ignore-next-line complexity diff --git a/packages/studio/src/player/lib/previewElements.ts b/packages/studio/src/player/lib/previewElements.ts index 7701c9bbc2..c40ce7b32c 100644 --- a/packages/studio/src/player/lib/previewElements.ts +++ b/packages/studio/src/player/lib/previewElements.ts @@ -8,9 +8,11 @@ import { MAX_ELEMENT_ENTRIES, MAX_ELEMENT_VALUE, MAX_GEOMETRY_CHILDREN, + MAX_SNAP_TARGETS, type EditingElementBox, type EditingElementDescription, type EditingElementGeometry, + type EditingGestureStart, type EditingElementHit, type EditingElementMatch, type EditingElementSource, @@ -226,6 +228,41 @@ export async function previewGeometry( }; } +const isFlag = (value: unknown) => typeof value === "boolean"; +const isFiniteFields = (value: unknown, keys: string[]) => + isFields(value) && keys.every((key) => Number.isFinite(value[key])); + +const readGestureStart = (value: unknown): EditingGestureStart | null => { + if (!readGeometry(value) || !isFields(value)) return null; + const { layout, move, gsap } = value; + const valid = [ + isFiniteFields(layout, ["width", "height"]), + move === null || isFiniteFields(move, ["a", "b", "c", "d"]), + gsap === null || (isFields(gsap) && isFiniteFields(gsap, ["x", "y"]) && isFlag(gsap.moving)), + ]; + return valid.includes(false) ? null : (value as unknown as EditingGestureStart); +}; + +export interface PreviewGestureStartReply { + /** One entry per handle, in order: null for an element the frame no longer has. */ + elements: Array; + snap: EditingElementBox[]; +} + +/** What a gesture on `handles` starts from, in one read; null when the runtime cannot answer. */ +export async function previewGestureStart( + iframe: HTMLIFrameElement, + handles: readonly string[], +): Promise { + if (!handles.length || handles.length > MAX_DESCRIBE_HANDLES) return null; + const reply = await previewRequest(iframe, "gesture-start", { handles: [...handles] }); + const elements: unknown = reply?.elements; + const snap: unknown = reply?.snap; + if (!Array.isArray(elements) || elements.length !== handles.length) return null; + if (!Array.isArray(snap) || snap.length > MAX_SNAP_TARGETS || !snap.every(isBox)) return null; + return { elements: elements.map(readGestureStart), snap }; +} + const LAYER_KINDS = new Set(["image", "video", "audio", "vector", "group", "text", "shape"]); const isLayer = (value: unknown): value is EditingLayer => isFields(value) && diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index 5bc0964512..1523d7e3d6 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -20,7 +20,7 @@ "unpacked": 170124 }, "@hyperframes/inspector": { - "packed": 858046, + "packed": 880000, "unpacked": 3722000 }, "@hyperframes/lint": {