diff --git a/packages/core/src/runtime/editingAgent.test.ts b/packages/core/src/runtime/editingAgent.test.ts
index f9ea3b044b..d423d4a1cf 100644
--- a/packages/core/src/runtime/editingAgent.test.ts
+++ b/packages/core/src/runtime/editingAgent.test.ts
@@ -142,6 +142,7 @@ describe("installEditingAgent", () => {
"set-loop-start",
"computed-styles",
"elements-at-point",
+ "pointer-targets",
"describe",
"ancestors",
"query",
@@ -238,11 +239,13 @@ describe("installEditingAgent", () => {
{ x: "1", y: 1 },
{ x: 1, y: Infinity },
{ x: 1, y: 2, z: 3 },
- ].map((params, index) => [
- { v: 1, id: 30 + index, kind: "elements-at-point", params },
- 30 + index,
- "invalid-params",
- ]),
+ ].flatMap((params, index) =>
+ (["elements-at-point", "pointer-targets"] as const).map((kind, k) => [
+ { v: 1, id: 30 + index * 2 + k, kind, params },
+ 30 + index * 2 + k,
+ "invalid-params",
+ ]),
+ ),
...[
{},
{ handles: [] },
diff --git a/packages/core/src/runtime/editingAgent.ts b/packages/core/src/runtime/editingAgent.ts
index 9e23dce67a..ce38af181a 100644
--- a/packages/core/src/runtime/editingAgent.ts
+++ b/packages/core/src/runtime/editingAgent.ts
@@ -28,6 +28,7 @@ import {
queryElements,
} from "./editingHandles";
import { elementGeometry } from "./editingGeometry";
+import { pointerTargetsAt } from "./editingTargets";
import { getLogicalOpacity } from "../studioPreviewMark";
type Handlers = {
@@ -81,6 +82,7 @@ const HANDLERS: Handlers = {
return { styles };
},
"elements-at-point": ({ x, y }) => ({ elements: elementsAtPoint(x, y) }),
+ "pointer-targets": ({ x, y }) => pointerTargetsAt(x, y),
describe: ({ handles }) => ({ elements: describeHandles(handles) }),
ancestors: ({ handles }) => ({ ancestors: ancestorHandles(handles) }),
query: ({ selector }) => queryElements(selector),
diff --git a/packages/core/src/runtime/editingElement.ts b/packages/core/src/runtime/editingElement.ts
index 3fffbf8094..d04257cff3 100644
--- a/packages/core/src/runtime/editingElement.ts
+++ b/packages/core/src/runtime/editingElement.ts
@@ -28,7 +28,7 @@ export interface EditingElementHit {
}
/**
- * An element as plain data, cut at the MAX_ELEMENT_* limits; a `data:` value keeps only its header.
+ * An element as plain data, cut at the MAX_ELEMENT_* limits; a `data:` value over its limit keeps only its header.
* `truncated` says something was cut, so the description is not safe to write back to source.
*/
export interface EditingElementDescription extends EditingElementHit {
diff --git a/packages/core/src/runtime/editingHandles.test.ts b/packages/core/src/runtime/editingHandles.test.ts
index 467a07f1e8..905b53c6f0 100644
--- a/packages/core/src/runtime/editingHandles.test.ts
+++ b/packages/core/src/runtime/editingHandles.test.ts
@@ -157,7 +157,7 @@ describe("element handles over the editing port", () => {
}
});
- it("cuts long values, keeps only a data: header, and leaves script and style text out", async () => {
+ it("cuts long values, keeps only a long data: value's header, and leaves script and style text out", async () => {
const long = "x".repeat(MAX_ELEMENT_VALUE + 50);
document.body.innerHTML = `

@@ -176,6 +176,16 @@ describe("element handles over the editing port", () => {
const style = await describeOne(card.querySelector("style")!);
expect(style).toMatchObject({ text: "", children: [], truncated: false });
+ const icon = `data:image/svg+xml,
`;
+ document.body.innerHTML = `
![]()
`;
+ const iconImg = document.querySelector("img")!;
+ iconImg.setAttribute("src", icon);
+ iconImg.style.setProperty("background", `url('${icon}')`);
+ const small = await describeOne(iconImg);
+ expect(small.attributes).toEqual([["src", icon]]);
+ expect(small.inlineStyles.background).toContain("data:image/svg+xml,");
+ expect(small.truncated).toBe(false);
+
document.body.innerHTML = `
data: 42 users, up 3%
`;
const dataText = await describeOne(document.querySelector("p")!);
expect(dataText).toMatchObject({ text: "data: 42 users, up 3%", truncated: false });
diff --git a/packages/core/src/runtime/editingHandles.ts b/packages/core/src/runtime/editingHandles.ts
index ff0295655b..d0213e5559 100644
--- a/packages/core/src/runtime/editingHandles.ts
+++ b/packages/core/src/runtime/editingHandles.ts
@@ -87,12 +87,12 @@ const NO_TEXT = new Set(["script", "style"]);
type Cut = { truncated: boolean };
-/** A `dataUrl` value starting `data:` keeps only its header; text is cut by length alone. */
+/** A value over `limit` is cut: a `dataUrl` one starting `data:` to its header, any other by length. */
function cut(value: string, limit: number, state: Cut, dataUrl = false): string {
+ if (value.length <= limit) return value;
+ state.truncated = true;
const header = dataUrl && value.startsWith("data:") ? value.slice(0, value.indexOf(",") + 1) : "";
- const kept = header || value.slice(0, limit);
- if (kept.length < value.length) state.truncated = true;
- return kept.slice(0, limit);
+ return (header || value).slice(0, limit);
}
function renderedText(element: Element, state: Cut): string {
@@ -151,7 +151,7 @@ function children(element: Element, state: Cut): EditingElementDescription["chil
return out;
}
-function hit(element: Element): EditingElementHit {
+export function elementHit(element: Element): EditingElementHit {
const rect = element.getBoundingClientRect();
return {
handle: handleFor(element),
@@ -164,7 +164,7 @@ function describeElement(element: Element): EditingElementDescription {
const selector = buildStableSelector(element);
const state: Cut = { truncated: false };
return {
- ...hit(element),
+ ...elementHit(element),
parent: element.parentElement ? handleFor(element.parentElement) : null,
id: element.id || null,
selector: selector ?? null,
@@ -180,7 +180,7 @@ function describeElement(element: Element): EditingElementDescription {
}
export function elementsAtPoint(x: number, y: number): EditingElementHit[] {
- return document.elementsFromPoint(x, y).slice(0, MAX_POINT_STACK).map(hit);
+ return document.elementsFromPoint(x, y).slice(0, MAX_POINT_STACK).map(elementHit);
}
export function describeHandles(
diff --git a/packages/core/src/runtime/editingPort.test.ts b/packages/core/src/runtime/editingPort.test.ts
index 1885ff778a..d43a203d34 100644
--- a/packages/core/src/runtime/editingPort.test.ts
+++ b/packages/core/src/runtime/editingPort.test.ts
@@ -67,6 +67,7 @@ describe("createEditingPortClient", () => {
"set-loop-start",
"computed-styles",
"elements-at-point",
+ "pointer-targets",
"describe",
"ancestors",
"query",
diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts
index 826cf8a8b9..04e44ca279 100644
--- a/packages/core/src/runtime/editingPort.ts
+++ b/packages/core/src/runtime/editingPort.ts
@@ -54,6 +54,18 @@ export {
type EditingElementGeometry,
type ElementTransformSnapshot,
} from "./editingGeometry";
+export {
+ compositionPoint,
+ coversComposition,
+ isDomLayerElement,
+ isInspectableLayerElement,
+ pointerTarget,
+ pointerTargets,
+ resolveAllVisualDomEditTargets,
+ resolveLayerSelector,
+ type EditingLocalPointer,
+ type EditingViewport,
+} from "./editingTargets";
export {
composeElementTransform,
individualRotateDegrees,
@@ -73,6 +85,7 @@ export const EDITING_REQUEST_KINDS = [
"set-loop-start",
"computed-styles",
"elements-at-point",
+ "pointer-targets",
"describe",
"ancestors",
"query",
@@ -112,6 +125,11 @@ export interface EditingRequests {
params: { x: number; y: number };
result: { elements: EditingElementHit[] };
};
+ /** The pick at fractions `x`, `y` of the root's box, and the stack a press cycles, MAX_POINT_STACK. */
+ "pointer-targets": {
+ params: { x: number; y: number };
+ result: { target: EditingElementHit | null; stack: EditingElementHit[] };
+ };
/** One entry per handle, in order; null for an unknown or removed element. */
describe: {
params: { handles: EditingElementHandle[] };
@@ -206,6 +224,12 @@ function handleList(params: unknown): { handles: EditingElementHandle[] } | null
return handles.every(isHandle) ? { handles } : null;
}
+function point(params: unknown): { x: number; y: number } | null {
+ if (!hasKeys(params, "x,y")) return null;
+ const { x, y } = params;
+ return Number.isFinite(x) && Number.isFinite(y) ? { x: x as number, y: y as number } : null;
+}
+
const PARAM_PARSERS: {
[K in EditingRequestKind]: (params: unknown) => EditingParams
| null;
} = {
@@ -226,11 +250,8 @@ const PARAM_PARSERS: {
const properties = styleProperties(params.properties);
return target && properties ? { ...target, properties } : null;
},
- "elements-at-point": (params) => {
- if (!hasKeys(params, "x,y")) return null;
- const { x, y } = params;
- return Number.isFinite(x) && Number.isFinite(y) ? { x: x as number, y: y as number } : null;
- },
+ "elements-at-point": point,
+ "pointer-targets": point,
describe: handleList,
ancestors: handleList,
query: (params) =>
diff --git a/packages/core/src/runtime/editingTargets.test.ts b/packages/core/src/runtime/editingTargets.test.ts
new file mode 100644
index 0000000000..89d28d84b7
--- /dev/null
+++ b/packages/core/src/runtime/editingTargets.test.ts
@@ -0,0 +1,57 @@
+import { afterEach, describe, expect, it } from "vitest";
+import { MAX_POINT_STACK } from "./editingElement";
+import { pointerTargetsAt } from "./editingTargets";
+
+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;
+ return element;
+}
+
+const asked: Array<[number, number]> = [];
+function stackAt(elements: Element[]) {
+ Object.assign(document, {
+ elementsFromPoint: (x: number, y: number) => {
+ asked.push([x, y]);
+ return elements;
+ },
+ elementFromPoint: () => elements[0] ?? null,
+ });
+}
+
+afterEach(() => {
+ asked.length = 0;
+ document.body.innerHTML = "";
+ delete (document as { elementsFromPoint?: unknown }).elementsFromPoint;
+ delete (document as { elementFromPoint?: unknown }).elementFromPoint;
+});
+
+describe("pointer-targets", () => {
+ it("reads a fraction of the composition root's box, and answers the pick and its stack", () => {
+ document.body.innerHTML = `A
`;
+ sized(document.querySelector("main")!, [0, 0, 1920, 1080]);
+ const a = sized(document.getElementById("a")!, [10, 20, 300, 40]);
+ stackAt([a]);
+ const answer = pointerTargetsAt(0.25, 0.5);
+ expect(asked[0]).toEqual([480, 540]);
+ expect(answer.target).toMatchObject({ tag: "p", rect: { x: 10, y: 20, width: 300 } });
+ expect(answer.stack.map((hit) => hit.handle)).toEqual([answer.target!.handle]);
+ });
+
+ it("caps the stack, and answers nothing when the composition has no size", () => {
+ const tiles = Array.from({ length: MAX_POINT_STACK + 5 }, (_, i) => `t
`);
+ document.body.innerHTML = `${tiles.join("")}`;
+ sized(document.querySelector("main")!, [0, 0, 1920, 1080]);
+ const elements = [...document.querySelectorAll("p")].map((p) => sized(p, [0, 0, 50, 50]));
+ stackAt(elements);
+ expect(pointerTargetsAt(0.1, 0.1).stack).toHaveLength(MAX_POINT_STACK);
+
+ sized(document.querySelector("main")!, [0, 0, 0, 0]);
+ Object.defineProperty(window, "innerWidth", { value: 0, configurable: true });
+ try {
+ expect(pointerTargetsAt(0.1, 0.1)).toEqual({ target: null, stack: [] });
+ } finally {
+ Object.defineProperty(window, "innerWidth", { value: 1024, configurable: true });
+ }
+ });
+});
diff --git a/packages/core/src/runtime/editingTargets.ts b/packages/core/src/runtime/editingTargets.ts
new file mode 100644
index 0000000000..0b03606349
--- /dev/null
+++ b/packages/core/src/runtime/editingTargets.ts
@@ -0,0 +1,322 @@
+/** The canvas hit-test `pointer-targets` answers; Studio runs it on frames it scripts too. */
+import { MAX_POINT_STACK, buildStableSelector, type EditingElementHit } from "./editingElement";
+import { isElementVisibleThroughAncestors } from "./editingGeometry";
+import { elementHit } from "./editingHandles";
+import { isElementNode } from "./domRealm";
+
+export interface EditingViewport {
+ width: number;
+ height: number;
+}
+
+/** A point in the frame's CSS pixels, with the composition box it is measured against. */
+export interface EditingLocalPointer {
+ x: number;
+ y: number;
+ viewport: EditingViewport;
+}
+
+// A box this close to the composition on both axes is a backdrop: a click passes it to the content.
+const FULL_BLEED_RATIO = 0.95;
+
+// A full-bleed video or image is itself what the user clicks, so media stays pickable.
+const FULL_BLEED_SELECTABLE_MEDIA_TAGS = new Set(["video", "img", "canvas", "svg"]);
+
+export function coversComposition(
+ elRect: { width: number; height: number },
+ viewport: EditingViewport,
+): boolean {
+ if (viewport.width <= 1 || viewport.height <= 1) return false;
+ return (
+ elRect.width / viewport.width >= FULL_BLEED_RATIO &&
+ elRect.height / viewport.height >= FULL_BLEED_RATIO
+ );
+}
+
+function isFullBleedTarget(el: HTMLElement, viewport: EditingViewport): boolean {
+ if (FULL_BLEED_SELECTABLE_MEDIA_TAGS.has(el.tagName.toLowerCase())) return false;
+ return coversComposition(el.getBoundingClientRect(), viewport);
+}
+
+const VISUAL_LEAF_TAGS = new Set(["img", "video", "canvas", "svg", "audio"]);
+
+// fallow-ignore-next-line complexity
+function hasVisualPresence(el: HTMLElement): boolean {
+ const win = el.ownerDocument.defaultView;
+ if (!win) return false;
+ const cs = win.getComputedStyle(el);
+ if (cs.backgroundImage !== "none") return true;
+ if (
+ cs.backgroundColor &&
+ cs.backgroundColor !== "transparent" &&
+ cs.backgroundColor !== "rgba(0, 0, 0, 0)"
+ )
+ return true;
+ if (cs.borderWidth && parseFloat(cs.borderWidth) > 0 && cs.borderStyle !== "none") return true;
+ if (cs.boxShadow && cs.boxShadow !== "none") return true;
+ return false;
+}
+
+function isEmptyVisualContainer(el: HTMLElement): boolean {
+ const tag = el.tagName.toLowerCase();
+ if (VISUAL_LEAF_TAGS.has(tag)) return false;
+ if (hasVisualPresence(el)) return false;
+
+ const { children } = el;
+ if (children.length === 0) {
+ return (el.textContent ?? "").trim().length === 0;
+ }
+
+ for (let i = 0; i < children.length; i += 1) {
+ const child = children[i];
+ if (!isElementNode(child)) continue;
+ if (VISUAL_LEAF_TAGS.has(child.tagName.toLowerCase())) return false;
+ if (isElementVisibleThroughAncestors(child as HTMLElement)) return false;
+ }
+
+ return true;
+}
+
+function hasRenderedBox(el: HTMLElement): boolean {
+ const rect = el.getBoundingClientRect();
+ if (rect.width <= 1 || rect.height <= 1) return false;
+ if (!isElementVisibleThroughAncestors(el)) return false;
+ if (isEmptyVisualContainer(el)) return false;
+ return true;
+}
+
+const DOM_LAYER_IGNORED_TAGS = new Set([
+ "base",
+ "br",
+ "canvas",
+ "link",
+ "meta",
+ "script",
+ "source",
+ "style",
+ "template",
+ "track",
+ "wbr",
+]);
+
+/** Is it rendered at all: the half of being a layer that a style write can flip. */
+export function isInspectableLayerElement(el: HTMLElement): boolean {
+ const tagName = el.tagName.toLowerCase();
+ if (DOM_LAYER_IGNORED_TAGS.has(tagName)) return false;
+
+ const computed = el.ownerDocument.defaultView?.getComputedStyle(el);
+ if (computed?.display === "none" || computed?.visibility === "hidden") return false;
+
+ return true;
+}
+
+/** The selector a layer is addressed by; undefined for an element that cannot be one. */
+export function resolveLayerSelector(el: HTMLElement): string | undefined {
+ if (el.hasAttribute("data-composition-id")) return undefined;
+ return buildStableSelector(el);
+}
+
+/** Is `el` a layer, without resolving its selector's occurrence index (a whole-document query). */
+export function isDomLayerElement(el: HTMLElement): boolean {
+ return isInspectableLayerElement(el) && resolveLayerSelector(el) !== undefined;
+}
+
+/** The selectable layers in a hit stack, topmost first: one per run of nested hits, its deepest. */
+export function resolveAllVisualDomEditTargets(
+ elementsFromPoint: Iterable,
+): HTMLElement[] {
+ const raw: HTMLElement[] = [];
+
+ for (const entry of elementsFromPoint) {
+ if (!isElementNode(entry)) continue;
+ const el = entry as HTMLElement;
+ if (hasRenderedBox(el) && isDomLayerElement(el)) {
+ raw.push(el);
+ }
+ }
+
+ if (raw.length === 0) return [];
+
+ const layers: HTMLElement[] = [];
+ let best = raw[0]!;
+ for (let i = 1; i < raw.length; i++) {
+ const el = raw[i]!;
+ if (best.contains(el)) {
+ best = el; // go deeper in this subtree
+ } else {
+ layers.push(best);
+ best = el;
+ }
+ }
+ layers.push(best);
+
+ return layers;
+}
+
+const pointerEventsInheritanceFallbackByDocument = new WeakMap();
+
+function needsPointerEventsInheritanceFallback(doc: Document, win: Window): boolean {
+ const cached = pointerEventsInheritanceFallbackByDocument.get(doc);
+ if (cached !== undefined) return cached;
+
+ const parent = doc.createElement("div");
+ const child = doc.createElement("div");
+ parent.style.pointerEvents = "none";
+ parent.appendChild(child);
+ const host = doc.body ?? doc.documentElement;
+ if (!host) return false;
+
+ host.appendChild(parent);
+ const needsFallback = win.getComputedStyle(child).pointerEvents !== "none";
+ parent.remove();
+ pointerEventsInheritanceFallbackByDocument.set(doc, needsFallback);
+ return needsFallback;
+}
+
+// Computed, not inline, so a class's pointer-events opt-in under a none ancestor counts.
+function hasOwnPointerEventsOverride(el: HTMLElement, win: Window): boolean {
+ const value = win.getComputedStyle(el).pointerEvents;
+ return value !== "" && value !== "inherit" && value !== "unset";
+}
+
+function inheritsPointerEventsNoneFromAncestor(el: HTMLElement, win: Window): boolean {
+ let current = el.parentElement;
+ while (current) {
+ if (win.getComputedStyle(current).pointerEvents === "none") {
+ let descendant: HTMLElement | null = el;
+ while (descendant && descendant !== current) {
+ if (hasOwnPointerEventsOverride(descendant, win)) {
+ return win.getComputedStyle(descendant).pointerEvents === "none";
+ }
+ descendant = descendant.parentElement;
+ }
+ return true;
+ }
+ current = current.parentElement;
+ }
+ return false;
+}
+
+function hasAuthorPointerEventsNone(el: HTMLElement): boolean {
+ const win = el.ownerDocument.defaultView;
+ if (!win) return false;
+ if (win.getComputedStyle(el).pointerEvents === "none") return true;
+ if (!needsPointerEventsInheritanceFallback(el.ownerDocument, win)) return false;
+ return inheritsPointerEventsNoneFromAncestor(el, win);
+}
+
+function collectPointerEventsNoneTargets(
+ elements: Iterable,
+): WeakSet {
+ const disabled = new WeakSet();
+ for (const entry of elements) {
+ if (isElementNode(entry) && hasAuthorPointerEventsNone(entry as HTMLElement)) {
+ disabled.add(entry as HTMLElement);
+ }
+ }
+ return disabled;
+}
+
+function filterAuthorInteractiveTargets(elements: Element[]): HTMLElement[] {
+ const pointerEventsNoneTargets = collectPointerEventsNoneTargets(elements);
+ return resolveAllVisualDomEditTargets(elements).filter((el) => !pointerEventsNoneTargets.has(el));
+}
+
+function eventTargetElement(target: EventTarget | null): HTMLElement | null {
+ if (!target || typeof target !== "object") return null;
+ const maybeNode = target as { nodeType?: number; parentElement?: Element | null };
+ if (maybeNode.nodeType === 1) return target as HTMLElement;
+ if (maybeNode.nodeType === 3 && maybeNode.parentElement) {
+ return maybeNode.parentElement as HTMLElement;
+ }
+ return null;
+}
+
+// The smallest group whose members' union holds the point: its gaps hit-test to nothing.
+// fallow-ignore-next-line complexity
+function findGroupAtPoint(doc: Document, x: number, y: number): HTMLElement | null {
+ let best: HTMLElement | null = null;
+ let bestArea = Infinity;
+ for (const group of Array.from(doc.querySelectorAll("[data-hf-group]"))) {
+ let left = Infinity;
+ let top = Infinity;
+ let right = -Infinity;
+ let bottom = -Infinity;
+ for (const member of Array.from(group.children)) {
+ const r = member.getBoundingClientRect();
+ if (r.width === 0 && r.height === 0) continue;
+ left = Math.min(left, r.left);
+ top = Math.min(top, r.top);
+ right = Math.max(right, r.right);
+ bottom = Math.max(bottom, r.bottom);
+ }
+ if (right < left || x < left || x > right || y < top || y > bottom) continue;
+ const area = (right - left) * (bottom - top);
+ if (area < bestArea) {
+ bestArea = area;
+ best = group;
+ }
+ }
+ return best;
+}
+
+/** The element a press at `point` picks, or null for empty canvas. */
+export function pointerTarget(doc: Document, point: EditingLocalPointer): HTMLElement | null {
+ // Reads only: a style added here, even briefly, restyles the whole film on each hover.
+ if (typeof doc.elementsFromPoint === "function") {
+ const elements = doc.elementsFromPoint(point.x, point.y);
+ const candidates = filterAuthorInteractiveTargets(elements);
+ const visualTarget = candidates.find((el) => !isFullBleedTarget(el, point.viewport)) ?? null;
+ if (visualTarget) return visualTarget;
+ }
+
+ const groupHit = findGroupAtPoint(doc, point.x, point.y);
+ if (groupHit && !hasAuthorPointerEventsNone(groupHit) && isDomLayerElement(groupHit))
+ return groupHit;
+
+ const fallback = eventTargetElement(doc.elementFromPoint(point.x, point.y));
+ if (!fallback || !isDomLayerElement(fallback)) return null;
+ if (hasAuthorPointerEventsNone(fallback)) return null;
+ if (!isElementVisibleThroughAncestors(fallback)) return null;
+ if (isFullBleedTarget(fallback, point.viewport)) return null;
+ return fallback;
+}
+
+/** Every independently-selectable element at `point`, topmost first. */
+export function pointerTargets(doc: Document, point: EditingLocalPointer): HTMLElement[] {
+ if (typeof doc.elementsFromPoint === "function") {
+ const elements = doc.elementsFromPoint(point.x, point.y);
+ return filterAuthorInteractiveTargets(elements).filter(
+ (el) => !isFullBleedTarget(el, point.viewport),
+ );
+ }
+ const fallback = eventTargetElement(doc.elementFromPoint(point.x, point.y));
+ if (!fallback || !isDomLayerElement(fallback)) return [];
+ if (hasAuthorPointerEventsNone(fallback)) return [];
+ if (!isElementVisibleThroughAncestors(fallback)) return [];
+ if (isFullBleedTarget(fallback, point.viewport)) return [];
+ return [fallback];
+}
+
+/** The frame point at fractions `x` and `y` of the composition root's box, else the viewport's. */
+export function compositionPoint(doc: Document, x: number, y: number): EditingLocalPointer | null {
+ const root =
+ doc.querySelector("[data-composition-id]") ?? doc.documentElement ?? null;
+ const rootRect = root?.getBoundingClientRect();
+ const width = rootRect?.width || doc.defaultView?.innerWidth || 0;
+ const height = rootRect?.height || doc.defaultView?.innerHeight || 0;
+ if (!width || !height) return null;
+ return { x: x * width, y: y * height, viewport: { width, height } };
+}
+
+/** `pointer-targets`: the pick at a composition point, and the stack a repeated press cycles. */
+export function pointerTargetsAt(
+ x: number,
+ y: number,
+): { target: EditingElementHit | null; stack: EditingElementHit[] } {
+ const point = compositionPoint(document, x, y);
+ if (!point) return { target: null, stack: [] };
+ const target = pointerTarget(document, point);
+ const stack = pointerTargets(document, point).slice(0, MAX_POINT_STACK);
+ return { target: target && elementHit(target), stack: stack.map(elementHit) };
+}
diff --git a/packages/studio/src/components/editor/DomEditOverlay.tsx b/packages/studio/src/components/editor/DomEditOverlay.tsx
index 271ab76623..2117896121 100644
--- a/packages/studio/src/components/editor/DomEditOverlay.tsx
+++ b/packages/studio/src/components/editor/DomEditOverlay.tsx
@@ -38,6 +38,7 @@ import { useMountEffect } from "../../hooks/useMountEffect";
import { countStudioManualEditSave as counted } from "./manualEditsDom";
import type { ZOrderAction, ZOrderPatch } from "./canvasContextMenuZOrder";
import { getPreviewTargetFromPointer } from "../../utils/studioPreviewHelpers";
+import { pointerMayHitElement } from "./previewSelection";
import { logSelect } from "../../utils/selectDebug";
import { useOffCanvasIndicators } from "./useOffCanvasIndicators";
import { DomEditGestureReadout } from "./DomEditGestureReadout";
@@ -382,12 +383,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({
const candidate = resolveShiftClickCandidate({
cached: hoverSelectionRef.current,
elementAtPoint: shiftIframe
- ? getPreviewTargetFromPointer(
- shiftIframe,
- event.clientX,
- event.clientY,
- activeCompositionPathRef.current,
- )
+ ? getPreviewTargetFromPointer(shiftIframe, event.clientX, event.clientY)
: null,
});
// Not confident: fall through untouched — no preventDefault, no suppression —
@@ -421,15 +417,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({
// Confirm emptiness with a fresh SYNCHRONOUS hit-test before committing.
if (!hoverSelectionRef.current && onMarqueeSelectRef.current && compRect.width > 0) {
const iframe = iframeRef.current;
- const freshTarget = iframe
- ? getPreviewTargetFromPointer(
- iframe,
- event.clientX,
- event.clientY,
- activeCompositionPathRef.current,
- )
- : null;
- if (freshTarget) return;
+ if (iframe && pointerMayHitElement(iframe, event.clientX, event.clientY)) return;
if (overlayRef.current) {
// Anywhere empty on the overlay starts one, not just inside the frame.
// An element dragged past the edge sits OUT there in the grey, and a
diff --git a/packages/studio/src/components/editor/clickAfterPreventedPress.test.tsx b/packages/studio/src/components/editor/clickAfterPreventedPress.test.tsx
index aec0f1bd32..3bcfaae670 100644
--- a/packages/studio/src/components/editor/clickAfterPreventedPress.test.tsx
+++ b/packages/studio/src/components/editor/clickAfterPreventedPress.test.tsx
@@ -47,11 +47,12 @@ function mount() {
function render(
hoverSelection: DomEditSelection | null,
selection: DomEditSelection | null = null,
+ iframe = document.createElement("iframe"),
) {
act(() =>
root.render(
{
expect(onSelectionChange).toHaveBeenCalledTimes(1);
expect(onCanvasMouseDown).not.toHaveBeenCalled();
});
+
+it("on a frame Studio cannot script, a press with no pointer answer yet selects, not starts a band", () => {
+ const { host } = mount();
+ const frame = {};
+ Object.defineProperty(frame, "document", {
+ get: () => {
+ throw new DOMException("blocked", "SecurityError");
+ },
+ });
+ const opaque = document.createElement("iframe");
+ Object.defineProperty(opaque, "contentWindow", { value: frame });
+ render(null, null, opaque);
+ press(host.firstElementChild!);
+ expect(onCanvasMouseDown).toHaveBeenCalledTimes(1);
+});
diff --git a/packages/studio/src/components/editor/domEditing.test.ts b/packages/studio/src/components/editor/domEditing.test.ts
index 7b3889a575..fefa774d11 100644
--- a/packages/studio/src/components/editor/domEditing.test.ts
+++ b/packages/studio/src/components/editor/domEditing.test.ts
@@ -240,11 +240,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(container, { width: 900, height: 520 });
setElementRect(headline, { left: 240, top: 160, width: 180, height: 36 });
- expect(
- resolveVisualDomEditSelectionTarget([container, headline], {
- activeCompositionPath: "index.html",
- }),
- ).toBe(headline);
+ expect(resolveVisualDomEditSelectionTarget([container, headline])).toBe(headline);
});
it("skips hidden and zero-size elements before picking a rendered candidate", () => {
@@ -260,11 +256,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(empty, { width: 0, height: 0 });
setElementRect(visible, { width: 120, height: 32 });
- expect(
- resolveVisualDomEditSelectionTarget([hidden, empty, visible], {
- activeCompositionPath: "index.html",
- }),
- ).toBe(visible);
+ expect(resolveVisualDomEditSelectionTarget([hidden, empty, visible])).toBe(visible);
});
it("skips transparent elements that still report a box", () => {
@@ -277,11 +269,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(transparent, { width: 120, height: 32 });
setElementRect(visible, { width: 120, height: 32 });
- expect(
- resolveVisualDomEditSelectionTarget([transparent, visible], {
- activeCompositionPath: "index.html",
- }),
- ).toBe(visible);
+ expect(resolveVisualDomEditSelectionTarget([transparent, visible])).toBe(visible);
});
it("falls back to the nearest stable editable ancestor when a visual child has no target", () => {
@@ -295,11 +283,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(card, { width: 400, height: 200 });
setElementRect(span, { left: 40, top: 40, width: 140, height: 28 });
- expect(
- resolveVisualDomEditSelectionTarget([span, card], {
- activeCompositionPath: "index.html",
- }),
- ).toBe(card);
+ expect(resolveVisualDomEditSelectionTarget([span, card])).toBe(card);
});
it("keeps explicit layer selection able to target containers", async () => {
@@ -313,9 +297,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(container, { width: 900, height: 520 });
setElementRect(headline, { left: 240, top: 160, width: 180, height: 36 });
- const visualTarget = resolveVisualDomEditSelectionTarget([container, headline], {
- activeCompositionPath: "index.html",
- });
+ const visualTarget = resolveVisualDomEditSelectionTarget([container, headline]);
const explicitSelection = await resolveDomEditSelection(container, {
activeCompositionPath: "index.html",
isMasterView: false,
@@ -341,11 +323,7 @@ describe("resolveVisualDomEditSelectionTarget", () => {
setElementRect(sfChrome, { left: 0, top: 0, width: 1920, height: 1080 });
setElementRect(subComp, { left: 0, top: 0, width: 1920, height: 1080 });
- expect(
- resolveVisualDomEditSelectionTarget([pipStudio, subComp, sfChrome], {
- activeCompositionPath: "index.html",
- }),
- ).toBe(pipStudio);
+ expect(resolveVisualDomEditSelectionTarget([pipStudio, subComp, sfChrome])).toBe(pipStudio);
});
});
diff --git a/packages/studio/src/components/editor/domEditingDom.preview.test.ts b/packages/studio/src/components/editor/domEditingDom.preview.test.ts
index edf1ee2409..9b7ab979c7 100644
--- a/packages/studio/src/components/editor/domEditingDom.preview.test.ts
+++ b/packages/studio/src/components/editor/domEditingDom.preview.test.ts
@@ -42,7 +42,7 @@ it("keeps a provider-backed video selectable and listed with its logical opacity
const { video, canvas, root } = preview("0.65");
expect(getComputedStyle(video).opacity).toBe("0");
expect(isElementVisibleThroughAncestors(video)).toBe(true);
- expect(resolveVisualDomEditSelectionTarget([canvas, video], options)).toBe(video);
+ expect(resolveVisualDomEditSelectionTarget([canvas, video])).toBe(video);
expect(collectDomEditLayerItems(root, options).map((item) => item.element)).toEqual([video]);
const selected = await resolveDomEditSelection(video, options);
expect(selected?.computedStyles.opacity).toBe("0.65");
@@ -53,7 +53,7 @@ it("keeps a provider-backed video selectable and listed with its logical opacity
it("keeps authored zero opacity invisible and reports zero to the inspector", async () => {
const { video, canvas } = preview("0");
expect(isElementVisibleThroughAncestors(video)).toBe(false);
- expect(resolveVisualDomEditSelectionTarget([canvas, video], options)).toBeNull();
+ expect(resolveVisualDomEditSelectionTarget([canvas, video])).toBeNull();
expect(getCuratedComputedStyles(video).opacity).toBe("0");
const selected = await resolveDomEditSelection(video, options);
expect(selected?.computedStyles.opacity).toBe("0");
@@ -64,7 +64,7 @@ it("still respects a hidden ancestor when the preview frame itself is visible",
const { video, root } = preview("0.65");
root.style.opacity = "0";
expect(isElementVisibleThroughAncestors(video)).toBe(false);
- expect(resolveVisualDomEditSelectionTarget([video], options)).toBeNull();
+ expect(resolveVisualDomEditSelectionTarget([video])).toBeNull();
});
it("does not treat an unrelated preceding canvas as the video's visible frame", () => {
diff --git a/packages/studio/src/components/editor/domEditingElement.ts b/packages/studio/src/components/editor/domEditingElement.ts
index 004610c723..b545a353d6 100644
--- a/packages/studio/src/components/editor/domEditingElement.ts
+++ b/packages/studio/src/components/editor/domEditingElement.ts
@@ -3,14 +3,12 @@
* and the `findElementForSelection` / `findElementForTimelineElement` lookups.
*/
import type {
- DomEditContextOptions,
DomEditSelection,
DomEditViewport,
TimelineElementDomTarget,
TimelineElementDomTargetOptions,
} from "./domEditingTypes";
import {
- buildStableSelector,
getSelectorIndex,
getSourceFileForElement,
isHtmlElement,
@@ -18,6 +16,15 @@ import {
querySelectorAllSafely,
} from "./domEditingDom";
import { selectionLookups, timelineElementLookup } from "./domEditSelectionRules";
+import {
+ isDomLayerElement,
+ isInspectableLayerElement,
+ resolveAllVisualDomEditTargets,
+ resolveLayerSelector,
+} from "@hyperframes/core/runtime/editing-port";
+
+// One hit-test rule for Studio and the runtime that answers it for a frame Studio cannot script.
+export { isDomLayerElement, isInspectableLayerElement };
// ─── Visibility ──────────────────────────────────────────────────────────────
@@ -28,87 +35,10 @@ export function isElementComputedVisible(
return isElementVisibleThroughAncestors(el, memo);
}
-const VISUAL_LEAF_TAGS = new Set(["img", "video", "canvas", "svg", "audio"]);
-
-// fallow-ignore-next-line complexity
-function hasVisualPresence(el: HTMLElement): boolean {
- const win = el.ownerDocument.defaultView;
- if (!win) return false;
- const cs = win.getComputedStyle(el);
- if (cs.backgroundImage !== "none") return true;
- if (
- cs.backgroundColor &&
- cs.backgroundColor !== "transparent" &&
- cs.backgroundColor !== "rgba(0, 0, 0, 0)"
- )
- return true;
- if (cs.borderWidth && parseFloat(cs.borderWidth) > 0 && cs.borderStyle !== "none") return true;
- if (cs.boxShadow && cs.boxShadow !== "none") return true;
- return false;
-}
-
-function isEmptyVisualContainer(el: HTMLElement): boolean {
- const tag = el.tagName.toLowerCase();
- if (VISUAL_LEAF_TAGS.has(tag)) return false;
- if (hasVisualPresence(el)) return false;
-
- const { children } = el;
- if (children.length === 0) {
- return (el.textContent ?? "").trim().length === 0;
- }
-
- for (let i = 0; i < children.length; i += 1) {
- const child = children[i];
- if (!isHtmlElement(child)) continue;
- if (VISUAL_LEAF_TAGS.has(child.tagName.toLowerCase())) return false;
- if (isElementComputedVisible(child)) return false;
- }
-
- return true;
-}
-
-function hasRenderedBox(el: HTMLElement): boolean {
- const rect = el.getBoundingClientRect();
- if (rect.width <= 1 || rect.height <= 1) return false;
- if (!isElementComputedVisible(el)) return false;
- if (isEmptyVisualContainer(el)) return false;
- return true;
-}
-
// ─── Visual scoring ──────────────────────────────────────────────────────────
// ─── Layer patch target ──────────────────────────────────────────────────────
-const DOM_LAYER_IGNORED_TAGS = new Set([
- "base",
- "br",
- "canvas",
- "link",
- "meta",
- "script",
- "source",
- "style",
- "template",
- "track",
- "wbr",
-]);
-
-/**
- * Is the element rendered at all? The half of `getDomLayerPatchTarget` that a
- * plain style write can flip, and the only half that reads computed style.
- * Exported so a caller memoizing a layer walk can give it its own (short)
- * cache lifetime — see `domEditLayerWalkCache`.
- */
-export function isInspectableLayerElement(el: HTMLElement): boolean {
- const tagName = el.tagName.toLowerCase();
- if (DOM_LAYER_IGNORED_TAGS.has(tagName)) return false;
-
- const computed = el.ownerDocument.defaultView?.getComputedStyle(el);
- if (computed?.display === "none" || computed?.visibility === "hidden") return false;
-
- return true;
-}
-
/**
* How the element is ADDRESSED — its selector, that selector's occurrence index
* within the source file, and the file itself. Independent of whether the
@@ -116,17 +46,6 @@ export function isInspectableLayerElement(el: HTMLElement): boolean {
* when an identity attribute changes or the document's population of elements
* sharing the selector does.
*/
-/**
- * The selector an element is addressed by, or undefined when it cannot be a
- * layer at all. Sole owner of that rule: `isDomLayerElement` answers the same
- * question without paying for the occurrence index, and a second copy of the
- * conditions would go stale the first time one of them changed.
- */
-function resolveLayerSelector(el: HTMLElement): string | undefined {
- if (el.hasAttribute("data-composition-id")) return undefined;
- return buildStableSelector(el);
-}
-
export function resolveDomLayerIdentity(
el: HTMLElement,
activeCompositionPath: string | null,
@@ -150,75 +69,12 @@ export function resolveDomLayerIdentity(
};
}
-export function getDomLayerPatchTarget(
- el: HTMLElement,
- activeCompositionPath: string | null,
-): Pick | null {
- return isInspectableLayerElement(el) ? resolveDomLayerIdentity(el, activeCompositionPath) : null;
-}
-
-/**
- * Is `el` a layer at all? Exactly the condition under which
- * `getDomLayerPatchTarget` returns non-null, for callers that want the yes/no
- * and throw the target away.
- *
- * Worth its own function because the target carries the selector's OCCURRENCE
- * INDEX, and resolving that is a whole-document query — which the yes/no does
- * not depend on. Counting an element's layer children asked for one per child.
- */
-export function isDomLayerElement(el: HTMLElement): boolean {
- return isInspectableLayerElement(el) && resolveLayerSelector(el) !== undefined;
-}
-
// ─── Visual target resolution ─────────────────────────────────────────────────
export function resolveVisualDomEditSelectionTarget(
elementsFromPoint: Iterable,
- options: Pick,
): HTMLElement | null {
- const candidates = resolveAllVisualDomEditTargets(elementsFromPoint, options);
- return candidates[0] ?? null;
-}
-
-/**
- * Returns all independently-selectable elements at the given point, in paint
- * order (topmost first). Used for click-cycling through stacked layers.
- *
- * Each entry in the returned array is an independent "layer" — an element
- * that is not an ancestor of an earlier entry. This gives one result per
- * z-stacked element rather than one per DOM node.
- */
-export function resolveAllVisualDomEditTargets(
- elementsFromPoint: Iterable,
- options: Pick,
-): HTMLElement[] {
- const raw: HTMLElement[] = [];
-
- for (const entry of elementsFromPoint) {
- if (!isHtmlElement(entry)) continue;
- if (hasRenderedBox(entry) && getDomLayerPatchTarget(entry, options.activeCompositionPath)) {
- raw.push(entry);
- }
- }
-
- if (raw.length === 0) return [];
-
- // First pass: for each contiguous ancestor-descendant run, keep only the
- // deepest (most specific) element, matching the original single-pick logic.
- const layers: HTMLElement[] = [];
- let best = raw[0];
- for (let i = 1; i < raw.length; i++) {
- const el = raw[i];
- if (best.contains(el)) {
- best = el; // go deeper in this subtree
- } else {
- layers.push(best);
- best = el;
- }
- }
- layers.push(best);
-
- return layers;
+ return resolveAllVisualDomEditTargets(elementsFromPoint)[0] ?? null;
}
// ─── Raster detection ────────────────────────────────────────────────────────
diff --git a/packages/studio/src/components/editor/previewPointerTargets.test.ts b/packages/studio/src/components/editor/previewPointerTargets.test.ts
new file mode 100644
index 0000000000..8bc721fad4
--- /dev/null
+++ b/packages/studio/src/components/editor/previewPointerTargets.test.ts
@@ -0,0 +1,260 @@
+// @vitest-environment happy-dom
+
+import React, { act } from "react";
+import { createRoot } from "react-dom/client";
+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 {
+ getAllPreviewTargetsFromPointer,
+ getPreviewTargetFromPointer,
+} from "../../utils/studioPreviewHelpers";
+import {
+ elementWithin,
+ findPointerTarget,
+ findPointerTargets,
+ pointerMayHitElement,
+ selectionDataAttribute,
+} from "./previewSelection";
+import type { PreviewElement } from "../../player/lib/previewElements";
+import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
+import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness";
+import { useDomSelection } from "../../hooks/useDomSelection";
+import type { TimelineElement } from "../../player";
+
+installReactActEnvironment();
+
+const FIXTURE = `
+
+
+
+
+ Note
+
+ `;
+const RECTS: Record = {
+ title: [44, 44, 220, 48],
+ mask: [40, 40, 260, 64],
+ block: [30, 30, 300, 100],
+ "under-ghost": [500, 40, 200, 60],
+ ghost: [500, 40, 200, 60],
+ backdrop: [0, 0, 1920, 1080],
+ pair: [0, 0, 1, 1],
+ a: [1000, 600, 100, 100],
+ "b-hf": [1300, 800, 100, 100],
+};
+/** Frame points, in the composition's pixels, and what lies under each, topmost first. */
+const STACKS: Array<[x: number, y: number, ids: string[]]> = [
+ [60, 60, ["title", "mask", "block", "backdrop"]],
+ [560, 60, ["under-ghost", "ghost", "backdrop"]],
+ [900, 500, ["backdrop"]],
+ [1200, 700, ["backdrop"]],
+ [1050, 650, ["a", "pair", "backdrop"]],
+];
+const FRAME = [100, 50, 960, 540];
+
+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;
+ return element;
+}
+
+const at = (x: number, y: number) => STACKS.find(([px, py]) => px === x && py === y)?.[2] ?? [];
+const byId = (id: string) =>
+ document.getElementById(id) ?? document.querySelector(`[data-hf-id="${id}"]`);
+
+const closers: Array<() => void> = [];
+beforeEach(() => {
+ document.body.innerHTML = FIXTURE;
+ sized(document.querySelector("main")!, [0, 0, 1920, 1080]);
+ for (const [id, rect] of Object.entries(RECTS)) sized(byId(id)!, rect);
+ const stack = (x: number, y: number) => at(Math.round(x), Math.round(y)).map((id) => byId(id)!);
+ Object.assign(document, {
+ elementsFromPoint: stack,
+ elementFromPoint: (x: number, y: number) => stack(x, y)[0] ?? null,
+ });
+});
+afterEach(() => {
+ closers.splice(0).forEach((close) => close());
+ document.body.innerHTML = "";
+ delete (document as { elementsFromPoint?: unknown }).elementsFromPoint;
+ delete (document as { elementFromPoint?: unknown }).elementFromPoint;
+});
+
+/** A frame Studio scripts directly; its document is this one. */
+function liveFrame(): HTMLIFrameElement {
+ const iframe = detachedFakeIframe({});
+ Object.defineProperty(iframe, "contentDocument", { value: document });
+ return sized(iframe, FRAME) as HTMLIFrameElement;
+}
+
+/** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
+function opaqueFrame(): HTMLIFrameElement {
+ const frame: Record = {};
+ const iframe = sized(detachedFakeIframe(frame), FRAME) 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());
+ return iframe;
+}
+
+/** The client point over frame point (x, y): the frame shows the composition at half size. */
+const client = (x: number, y: number) => [FRAME[0]! + x / 2, FRAME[1]! + y / 2] as const;
+const nameOf = (element: HTMLElement | PreviewElement | null | undefined) => {
+ if (!element) return null;
+ if ("handle" in element)
+ return element.id ?? element.attributes.find(([k]) => k === "data-hf-id")?.[1];
+ return element.id || element.getAttribute("data-hf-id");
+};
+
+describe("canvas hit-tests on an opaque frame match same-origin ones", () => {
+ it.each(STACKS.map(([x, y]) => [x, y]))("at (%d, %d)", async (x, y) => {
+ const [cx, cy] = client(x, y);
+ const live = liveFrame();
+ const opaque = opaqueFrame();
+ const pick = nameOf(getPreviewTargetFromPointer(live, cx, cy));
+ expect(nameOf(await findPointerTarget(opaque, cx, cy))).toBe(pick);
+ const stack = getAllPreviewTargetsFromPointer(live, cx, cy).map(nameOf);
+ expect((await findPointerTargets(opaque, cx, cy)).map(nameOf)).toEqual(stack);
+ });
+
+ it("picks what each rule decides: the leaf, not a pointer-events:none layer, not a backdrop, a group's gap", async () => {
+ const opaque = opaqueFrame();
+ const picks = [];
+ for (const [x, y] of STACKS)
+ picks.push(nameOf(await findPointerTarget(opaque, ...client(x, y))));
+ expect(picks).toEqual(["title", null, null, "pair", "a"]);
+ });
+
+ it("knows whether a press lands on an element only where the frame last answered", async () => {
+ const opaque = opaqueFrame();
+ const title = client(60, 60);
+ const empty = client(900, 500);
+ expect(pointerMayHitElement(opaque, ...empty)).toBe(true);
+ await findPointerTarget(opaque, ...empty);
+ expect(pointerMayHitElement(opaque, ...empty)).toBe(false);
+ expect(pointerMayHitElement(opaque, ...title)).toBe(true);
+ await findPointerTarget(opaque, ...title);
+ expect(pointerMayHitElement(opaque, ...title)).toBe(true);
+ expect(pointerMayHitElement(liveFrame(), ...empty)).toBe(false);
+ });
+
+ it("says it cannot tell when the frame has no editing agent", async () => {
+ vi.useFakeTimers();
+ try {
+ const iframe = sized(detachedFakeIframe({}), FRAME) as HTMLIFrameElement;
+ Object.defineProperty(iframe, "contentDocument", { value: null });
+ Object.defineProperty(iframe.contentWindow!, "document", {
+ get: () => {
+ throw new DOMException("blocked", "SecurityError");
+ },
+ });
+ const found = findPointerTarget(iframe, ...client(60, 60));
+ await vi.advanceTimersByTimeAsync(2_000);
+ expect(await found).toBeUndefined();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});
+
+describe("useDomSelection over an opaque frame's canvas", () => {
+ function renderSelection(iframe: HTMLIFrameElement) {
+ let hook: ReturnType | null = null;
+ function Harness() {
+ hook = useDomSelection({
+ projectId: null,
+ activeCompPath: "index.html",
+ isMasterView: true,
+ compIdToSrc: new Map(),
+ captionEditMode: false,
+ previewIframeRef: { current: iframe },
+ timelineElements: [],
+ getTimelineSelectionSet: () => new Set(),
+ setSelectedTimelineElementId: vi.fn(),
+ setTimelineSelectionSet: vi.fn(),
+ setRightCollapsed: vi.fn(),
+ setRightPanelTab: vi.fn(),
+ previewIframe: iframe,
+ refreshKey: 0,
+ rightPanelTab: "design",
+ });
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ act(() => root.render(React.createElement(Harness)));
+ closers.push(() => act(() => root.unmount()));
+ return () => hook!;
+ }
+
+ it("selects the clicked element and keeps a hover on it", async () => {
+ const hook = renderSelection(opaqueFrame());
+ const point = client(60, 60);
+ const picked = await act(() => hook().resolveDomSelectionFromPreviewPoint(...point));
+ expect(picked).toMatchObject({ id: "title", selector: "#title" });
+ act(() => hook().updateDomEditHoverSelection(picked));
+ expect(hook().domEditHoverSelection).toBe(picked);
+ const cycle = await act(() => hook().resolveAllDomSelectionsFromPreviewPoint(...point));
+ expect(cycle.map((selection) => selection.id)).toEqual(["title", "mask", "block"]);
+ });
+
+ it("takes a hover of the same element from a reloaded frame, whose handle is new", async () => {
+ const hook = renderSelection(opaqueFrame());
+ const picked = (await act(() =>
+ hook().resolveDomSelectionFromPreviewPoint(...client(60, 60)),
+ ))!;
+ act(() => hook().updateDomEditHoverSelection(picked));
+ const reloaded = { ...picked, handle: "0123456789abcdef:1" };
+ act(() => hook().updateDomEditHoverSelection(reloaded));
+ expect(hook().domEditHoverSelection).toBe(reloaded);
+ act(() => hook().updateDomEditHoverSelection({ ...reloaded }));
+ expect(hook().domEditHoverSelection).toBe(reloaded);
+ });
+
+ it("drills into a group and stays in it while selecting its members", async () => {
+ const hook = renderSelection(opaqueFrame());
+ const group = (await act(() =>
+ hook().resolveDomSelectionFromPreviewPoint(...client(1200, 700)),
+ ))!;
+ expect(selectionDataAttribute(group, "data-hf-group")).toBe("pair");
+ expect(group.element.hasAttribute("data-hf-group")).toBe(false);
+ act(() => hook().setActiveGroupElement(group.element));
+ const member = (await act(() =>
+ hook().resolveDomSelectionFromPreviewPoint(...client(1050, 650), {
+ activeGroupElement: group.element,
+ }),
+ ))!;
+ expect(member.id).toBe("a");
+ expect(elementWithin(group.element, member.element)).toBe(true);
+ expect(elementWithin(member.element, group.element)).toBe(false);
+ act(() => hook().applyDomSelection(member));
+ expect(hook().activeGroupElement).toBe(group.element);
+ });
+
+ it("selects a timeline clip the frame knows only by its clip id", async () => {
+ const hook = renderSelection(opaqueFrame());
+ const clip = (id: string) => ({ id, key: `index.html:${id}:0` }) as unknown as TimelineElement;
+ await act(() => hook().handleTimelineElementSelect(clip("title")));
+ expect(hook().domEditSelection).toMatchObject({ id: "title" });
+ await act(() => hook().handleTimelineElementSelect(clip("note")));
+ expect(hook().domEditSelection).toMatchObject({ hfId: "note" });
+ await act(() => hook().handleTimelineElementSelect(clip("nowhere")));
+ expect(hook().domEditSelection).toBeNull();
+ const moved = { ...clip("title"), selector: "#renamed" } as TimelineElement;
+ await act(() => hook().handleTimelineElementSelect(moved));
+ expect(hook().domEditSelection).toBeNull();
+ });
+});
diff --git a/packages/studio/src/components/editor/previewSelection.test.ts b/packages/studio/src/components/editor/previewSelection.test.ts
index 881113a109..83426ea3b2 100644
--- a/packages/studio/src/components/editor/previewSelection.test.ts
+++ b/packages/studio/src/components/editor/previewSelection.test.ts
@@ -342,6 +342,19 @@ describe("opaque frame selections read only what arrived whole", () => {
expect(essay.remote).toMatchObject({ id: "essay", selector: "#essay" });
});
+ it("selects an image with a short data: src, and not one whose data: src was cut", async () => {
+ const icon = `data:image/svg+xml,`;
+ document.body.innerHTML = `
+
![]()

`;
+ document.getElementById("icon")!.setAttribute("src", icon);
+ const short = await bothPaths(document.getElementById("icon")!);
+ expect(shared(short.remote)).toEqual(shared(short.live));
+ expect(short.remote!.src).toBe(icon);
+ const long = await bothPaths(document.getElementById("photo")!);
+ expect(long.live).not.toBeNull();
+ expect(long.remote).toBeNull();
+ });
+
it("builds nothing when the frame cannot read computed styles or the composition root", async () => {
document.body.innerHTML = FIXTURE;
const opaque = opaqueFrame();
diff --git a/packages/studio/src/components/editor/previewSelection.ts b/packages/studio/src/components/editor/previewSelection.ts
index 8b7be274ed..eaf7b632c0 100644
--- a/packages/studio/src/components/editor/previewSelection.ts
+++ b/packages/studio/src/components/editor/previewSelection.ts
@@ -23,6 +23,7 @@ import {
describePreviewElements,
previewAncestors,
previewElementSelectorIndex,
+ previewPointerTargets,
previewElementSourceFile,
queryPreviewElements,
type PreviewElement,
@@ -53,6 +54,11 @@ import {
type TimelineElementDomTargetOptions,
} from "./domEditingTypes";
import { existsInSourceFile } from "./probeSourceElement";
+import {
+ framePointFraction,
+ getAllPreviewTargetsFromPointer,
+ getPreviewTargetFromPointer,
+} from "../../utils/studioPreviewHelpers";
import { isVideoAudible } from "../../player/lib/timelineElementHelpers";
let inertDocument: Document | undefined;
@@ -68,6 +74,7 @@ function inertElement(tag: string): HTMLElement {
const standIns = new WeakMap>();
const standInHandles = new WeakMap();
+const standInAncestors = new WeakMap();
function standInFor(iframe: HTMLIFrameElement, element: PreviewElement): HTMLElement {
const client = previewEditingClient(iframe);
@@ -161,6 +168,19 @@ export async function findPreviewElementForSelection(
return null;
}
+/** A clip built without the frame's document has only the runtime's clip id: an id, else a hf-id. */
+async function findByClipId(
+ iframe: HTMLIFrameElement,
+ element: TimelineElementDomTarget,
+ sourceFile: string,
+ path: string | null,
+): Promise {
+ if (!element.id || element.domId || element.selector || element.compositionSrc) return null;
+ const byId = await findPreviewElementForSelection(iframe, { id: element.id, sourceFile }, path);
+ if (byId !== null) return byId;
+ return findPreviewElementForSelection(iframe, { hfId: element.id, sourceFile }, path);
+}
+
/** `findElementForTimelineElement` over the frame's runtime. */
export async function findPreviewElementForTimelineElement(
iframe: HTMLIFrameElement,
@@ -171,6 +191,8 @@ export async function findPreviewElementForTimelineElement(
const path = options.activeCompositionPath;
const found = target && (await findPreviewElementForSelection(iframe, target, path));
if (found !== null) return found;
+ const byClipId = await findByClipId(iframe, element, sourceFile, path);
+ if (byClipId !== null) return byClipId;
if (!rootFallback) return null;
const reply = await queryPreviewElements(iframe, "[data-composition-id]");
if (!reply) return undefined;
@@ -286,6 +308,10 @@ export async function resolvePreviewSelection(
const current = chain[index]!;
if (!valuesWhole(current)) return null;
const element = standInFor(iframe, current);
+ standInAncestors.set(
+ element,
+ chain.slice(index + 1).map((node) => node.handle),
+ );
const identity = previewIdentity(current, options.activeCompositionPath);
const compositionSrc = compositionSourceOf(current);
const probe = readable(current);
@@ -387,3 +413,60 @@ export async function findOpaqueTimelineElement(
if (!iframe || !previewFrameOpaque(iframe)) return null;
return (await findPreviewElementForTimelineElement(iframe, element, lookup)) ?? null;
}
+
+export function elementWithin(container: HTMLElement, element: HTMLElement): boolean {
+ const handle = standInHandles.get(container);
+ if (handle === undefined) return container.contains(element);
+ return element === container || (standInAncestors.get(element)?.includes(handle) ?? false);
+}
+
+/** A `data-` attribute of the selection's element; a stand-in's comes from its description. */
+export function selectionDataAttribute(selection: DomEditSelection, name: `data-${string}`) {
+ if (!selection.handle) return selection.element.getAttribute(name);
+ return selection.dataAttributes[name.slice("data-".length)] ?? null;
+}
+
+const pointerAnswers = new WeakMap();
+
+async function previewPointerTarget(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
+ const { x, y } = framePointFraction(iframe, clientX, clientY);
+ const reply = await previewPointerTargets(iframe, x, y);
+ if (reply) pointerAnswers.set(iframe, { x: clientX, y: clientY, found: reply.target !== null });
+ return reply;
+}
+
+/** An opaque frame answers from its last pointer read at this point, and assumes a hit without one. */
+export function pointerMayHitElement(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
+ if (!previewFrameOpaque(iframe))
+ return Boolean(getPreviewTargetFromPointer(iframe, clientX, clientY));
+ const answer = pointerAnswers.get(iframe);
+ return answer?.x === clientX && answer.y === clientY ? answer.found : true;
+}
+
+export async function findPointerTarget(
+ iframe: HTMLIFrameElement,
+ clientX: number,
+ clientY: number,
+): Promise {
+ if (!previewFrameOpaque(iframe)) return getPreviewTargetFromPointer(iframe, clientX, clientY);
+ const reply = await previewPointerTarget(iframe, clientX, clientY);
+ if (!reply?.target) return reply ? null : undefined;
+ const [described] = (await describePreviewElements(iframe, [reply.target.handle])) ?? [];
+ return described ?? undefined;
+}
+
+export async function findPointerTargets(
+ iframe: HTMLIFrameElement,
+ clientX: number,
+ clientY: number,
+): Promise> {
+ if (!previewFrameOpaque(iframe)) return getAllPreviewTargetsFromPointer(iframe, clientX, clientY);
+ const reply = await previewPointerTarget(iframe, clientX, clientY);
+ const handles = reply?.stack.map((hit) => hit.handle) ?? [];
+ const described = handles.length ? await describePreviewElements(iframe, handles) : [];
+ return (described ?? []).filter((element) => element !== null);
+}
+
+/** A stand-in is never connected: an opaque frame's selection stands until it is replaced. */
+export const selectionElementLive = (selection: DomEditSelection) =>
+ selection.handle !== undefined || selection.element.isConnected;
diff --git a/packages/studio/src/components/editor/useInlineTextEditing.tsx b/packages/studio/src/components/editor/useInlineTextEditing.tsx
index 08cdf00728..d991e78626 100644
--- a/packages/studio/src/components/editor/useInlineTextEditing.tsx
+++ b/packages/studio/src/components/editor/useInlineTextEditing.tsx
@@ -64,12 +64,7 @@ export function useInlineTextEditing(
if (!iframe) return null;
// The same hit test the canvas uses to decide what was clicked; a plain
// elementFromPoint only finds wrappers, since Studio suppresses pointer events.
- return getPreviewTargetFromPointer(
- iframe,
- event.clientX,
- event.clientY,
- selectionRef.current?.compositionPath ?? null,
- );
+ return getPreviewTargetFromPointer(iframe, event.clientX, event.clientY);
};
/** A point on Studio's canvas, in the scaled composition's coordinates. */
diff --git a/packages/studio/src/hooks/domSelectionTestHarness.ts b/packages/studio/src/hooks/domSelectionTestHarness.ts
index 1ce80f0f76..cb65bcab87 100644
--- a/packages/studio/src/hooks/domSelectionTestHarness.ts
+++ b/packages/studio/src/hooks/domSelectionTestHarness.ts
@@ -63,7 +63,7 @@ export function makeSelection(label: string, element: HTMLElement): DomEditSelec
};
}
-/** Built as getDomLayerPatchTarget builds it: a bare class for an element with no id or hf-id. */
+/** Built as resolveDomLayerIdentity builds it: a bare class for an element with no id or hf-id. */
export function stableSelectionFor(el: HTMLElement): DomEditSelection {
const selector = buildStableSelector(el);
return {
diff --git a/packages/studio/src/hooks/useDomEditSession.test.tsx b/packages/studio/src/hooks/useDomEditSession.test.tsx
index 1a0f23c5dd..a3168be4b5 100644
--- a/packages/studio/src/hooks/useDomEditSession.test.tsx
+++ b/packages/studio/src/hooks/useDomEditSession.test.tsx
@@ -64,6 +64,7 @@ const capturedOnReorderShadow: { fn: ((targets: string[]) => void) | undefined }
const domEditSelectionRef: { current: DomEditSelection | null } = { current: null };
const domEditGroupSelectionsRef: { current: DomEditSelection[] } = { current: [] };
const groupSelectionSpy = vi.fn();
+const ungroupSelectionSpy = vi.fn();
const gsapCommitMutation = Object.assign(vi.fn(), { batch: vi.fn() });
const neverParsed = new Promise<{ animations: GsapAnimation[] }>(() => undefined);
const parsedFile = { current: neverParsed };
@@ -211,7 +212,7 @@ vi.mock("./useGsapScriptCommits", () => ({
vi.mock("./useGroupCommits", () => ({
useGroupCommits: () => ({
groupSelection: (...args: unknown[]) => groupSelectionSpy(...args),
- ungroupSelection: vi.fn(),
+ ungroupSelection: (...args: unknown[]) => ungroupSelectionSpy(...args),
}),
}));
vi.mock("./useDomEditWiring", () => ({
@@ -501,6 +502,27 @@ describe("handleGroupSelection with audio in the selection", () => {
await group([sel("div"), sel("span")]);
expect(groupSelectionSpy).toHaveBeenCalledTimes(1);
});
+
+ it("ungroups an opaque frame's group, read off its description, not the tag-only stand-in", async () => {
+ const { useDomEditSession } = await import("./useDomEditSession");
+ const wrapper = {
+ ...sel("div"),
+ handle: "0123456789abcdef:1",
+ dataAttributes: { "hf-group": "g" },
+ };
+ domEditSelectionRef.current = wrapper as DomEditSelection;
+ const captured: { fn?: () => void } = {};
+ function Probe() {
+ captured.fn = useDomEditSession(createSessionParams()).handleUngroupSelection;
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ act(() => root.render());
+ act(() => captured.fn?.());
+ act(() => root.unmount());
+ domEditSelectionRef.current = null;
+ expect(ungroupSelectionSpy).toHaveBeenCalledWith(wrapper);
+ });
});
// ── Delete routing: a timeline row goes through the timeline's own delete op ──
diff --git a/packages/studio/src/hooks/useDomEditSession.ts b/packages/studio/src/hooks/useDomEditSession.ts
index 97c246cb88..46ffe88ff9 100644
--- a/packages/studio/src/hooks/useDomEditSession.ts
+++ b/packages/studio/src/hooks/useDomEditSession.ts
@@ -21,6 +21,7 @@ import { useStudioSelectionPublisher } from "./useStudioSelectionPublisher";
import { useKeyframeEaseCommits } from "./useKeyframeEaseCommits";
import { useCommitPreflightCapabilities } from "./useCommitPreflightCapabilities";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import { selectionDataAttribute } from "../components/editor/previewSelection";
import { membersForDelete, timelineElementsForDelete } from "./domEditDeleteMembers";
import type { RecordEditInput } from "./domEditDeleteMembers";
import type { DomEditTimelineParams } from "./useDomSelectionTypes";
@@ -351,7 +352,7 @@ export function useDomEditSession({
const handleUngroupSelection = useCallback(() => {
const sel = domEditSelectionRef.current;
- if (!sel?.element.hasAttribute("data-hf-group")) {
+ if (!sel || selectionDataAttribute(sel, "data-hf-group") === null) {
showToast("Select a group to ungroup", "info");
return;
}
diff --git a/packages/studio/src/hooks/useDomSelection.ts b/packages/studio/src/hooks/useDomSelection.ts
index 383c5ed5d8..8e38909332 100644
--- a/packages/studio/src/hooks/useDomSelection.ts
+++ b/packages/studio/src/hooks/useDomSelection.ts
@@ -1,9 +1,5 @@
import { useState, useCallback, useRef, useEffect } from "react";
import type { TimelineElement } from "../player";
-import {
- getAllPreviewTargetsFromPointer,
- getPreviewTargetFromPointer,
-} from "../utils/studioPreviewHelpers";
import {
domEditSelectionsEqual,
domEditSelectionsTargetSame,
@@ -17,9 +13,13 @@ import {
type DomEditSelection,
} from "../components/editor/domEditing";
import {
+ elementWithin,
findOpaqueTimelineElement,
+ findPointerTarget,
+ findPointerTargets,
findSelectionElement,
resolveFoundSelection,
+ selectionElementLive,
} from "../components/editor/previewSelection";
import type { PreviewElement } from "../player/lib/previewElements";
import { reapplyPositionEditsAfterSeek } from "../components/editor/manualEdits";
@@ -189,7 +189,7 @@ export function useDomSelection({
// Selecting something outside the drilled-into group exits the drill-in, so
// a later click on the group selects it as a unit again (non-sticky drill-in).
const activeGroup = activeGroupElementRef.current;
- if (activeGroup && nextSelection && !activeGroup.contains(nextSelection.element)) {
+ if (activeGroup && nextSelection && !elementWithin(activeGroup, nextSelection.element)) {
activeGroupElementRef.current = null;
setActiveGroupElementState(null);
}
@@ -269,7 +269,7 @@ export function useDomSelection({
} catch {
/* cross-origin guard */
}
- const target = getPreviewTargetFromPointer(iframe, clientX, clientY, activeCompPath);
+ const target = await findPointerTarget(iframe, clientX, clientY);
if (!target) return null;
return buildDomSelectionFromTarget(
target,
@@ -285,7 +285,7 @@ export function useDomSelection({
},
);
},
- [activeCompPath, buildDomSelectionFromTarget, captionEditMode, previewIframeRef],
+ [buildDomSelectionFromTarget, captionEditMode, previewIframeRef],
);
const resolveAllDomSelectionsFromPreviewPoint = useCallback(
@@ -298,7 +298,7 @@ export function useDomSelection({
} catch {
/* cross-origin guard */
}
- const targets = getAllPreviewTargetsFromPointer(iframe, clientX, clientY, activeCompPath);
+ const targets = await findPointerTargets(iframe, clientX, clientY);
const results: DomEditSelection[] = [];
for (const target of targets) {
const sel = await buildDomSelectionFromTarget(target, { skipSourceProbe: true });
@@ -306,11 +306,13 @@ export function useDomSelection({
}
return results;
},
- [activeCompPath, buildDomSelectionFromTarget, captionEditMode, previewIframeRef],
+ [buildDomSelectionFromTarget, captionEditMode, previewIframeRef],
);
const updateDomEditHoverSelection = useCallback((selection: DomEditSelection | null) => {
- if (domEditSelectionsTargetSame(domEditHoverSelectionRef.current, selection)) return;
+ const current = domEditHoverSelectionRef.current;
+ const sameHandle = current?.handle === selection?.handle;
+ if (domEditSelectionsTargetSame(current, selection) && sameHandle) return;
domEditHoverSelectionRef.current = selection;
setDomEditHoverSelection(selection);
}, []);
@@ -481,7 +483,7 @@ export function useDomSelection({
captionEditMode ||
domEditSelectionsTargetSame(domEditHoverSelection, domEditSelection) ||
domEditSelectionInGroup(domEditGroupSelections, domEditHoverSelection) ||
- !domEditHoverSelection.element.isConnected;
+ !selectionElementLive(domEditHoverSelection);
if (shouldClear) updateDomEditHoverSelection(null);
}, [
captionEditMode,
diff --git a/packages/studio/src/hooks/useGsapSelectionHandlers.test.tsx b/packages/studio/src/hooks/useGsapSelectionHandlers.test.tsx
index 47b00f805f..1a42bfacd3 100644
--- a/packages/studio/src/hooks/useGsapSelectionHandlers.test.tsx
+++ b/packages/studio/src/hooks/useGsapSelectionHandlers.test.tsx
@@ -182,3 +182,20 @@ describe("useGsapSelectionHandlers retime settlement", () => {
withSelection.unmount();
});
});
+
+describe("useGsapSelectionHandlers on an opaque frame's selection", () => {
+ it("reads a moved element's offset mark off the description, not the tag-only stand-in", async () => {
+ const selection = {
+ ...makeSelection(),
+ handle: "0123456789abcdef:1",
+ dataAttributes: { "hf-studio-path-offset": "" },
+ } as DomEditSelection;
+ const handleDomManualEditsReset = vi.fn().mockResolvedValue(undefined);
+ const rendered = renderHandlers(
+ makeParams({ domEditSelection: selection, handleDomManualEditsReset }),
+ );
+ await act(() => rendered.handlers().handleGsapAddAnimation("to"));
+ expect(handleDomManualEditsReset).toHaveBeenCalledWith(selection);
+ rendered.unmount();
+ });
+});
diff --git a/packages/studio/src/hooks/useGsapSelectionHandlers.ts b/packages/studio/src/hooks/useGsapSelectionHandlers.ts
index e7101ffca5..4910b15d73 100644
--- a/packages/studio/src/hooks/useGsapSelectionHandlers.ts
+++ b/packages/studio/src/hooks/useGsapSelectionHandlers.ts
@@ -9,6 +9,7 @@ import {
trackStudioSaveFailure,
} from "../utils/studioSaveDiagnostics";
import { trackStudioEvent } from "../utils/studioTelemetry";
+import { selectionDataAttribute } from "../components/editor/previewSelection";
import type { CommitMutationOptions } from "./gsapScriptCommitTypes";
/**
@@ -243,7 +244,7 @@ export function useGsapSelectionHandlers({
"add",
`Add GSAP ${method} animation`,
);
- if (selection.element.hasAttribute("data-hf-studio-path-offset")) {
+ if (selectionDataAttribute(selection, "data-hf-studio-path-offset") !== null) {
// The reset owns rollback and the position commit already owns user and
// telemetry reporting. This is only the fire-and-forget UI boundary.
void landed.then((didLand) => {
diff --git a/packages/studio/src/hooks/usePreviewInteraction.test.ts b/packages/studio/src/hooks/usePreviewInteraction.test.ts
index 6bc79817fa..c76a38b670 100644
--- a/packages/studio/src/hooks/usePreviewInteraction.test.ts
+++ b/packages/studio/src/hooks/usePreviewInteraction.test.ts
@@ -165,13 +165,21 @@ describe("usePreviewInteraction", () => {
cleanup();
});
- it("preserves group drill-in on double-click", async () => {
+ it.each([
+ ["a wrapper Studio can script", false],
+ ["an opaque frame's tag-only stand-in", true],
+ ])("preserves group drill-in on double-click on %s", async (_, opaque) => {
const group = document.createElement("div");
- group.setAttribute("data-hf-group", "hero");
+ if (!opaque) group.setAttribute("data-hf-group", "hero");
const child = document.createElement("span");
child.id = "headline";
group.append(child);
const groupSelection = makeSelection("Hero Group", group);
+ if (opaque)
+ Object.assign(groupSelection, {
+ handle: "0123456789abcdef:1",
+ dataAttributes: { "hf-group": "hero" },
+ });
const childSelection = makeSelection("Headline", child);
const applyDomSelection = vi.fn();
const setActiveGroupElement = vi.fn();
diff --git a/packages/studio/src/hooks/usePreviewInteraction.ts b/packages/studio/src/hooks/usePreviewInteraction.ts
index 7cfb3bba04..c20146d129 100644
--- a/packages/studio/src/hooks/usePreviewInteraction.ts
+++ b/packages/studio/src/hooks/usePreviewInteraction.ts
@@ -3,6 +3,7 @@ import { useCallback, useRef } from "react";
import { liveTime, usePlayerStore } from "../player";
import { pauseStudioPreviewPlayback } from "../utils/studioPreviewHelpers";
import { type DomEditSelection } from "../components/editor/domEditing";
+import { selectionDataAttribute } from "../components/editor/previewSelection";
import type { ApplyDomSelectionOptions, ResolveDomSelectionOptions } from "./useDomSelection";
import { trackStudioEvent } from "../utils/studioTelemetry";
@@ -119,7 +120,7 @@ export function usePreviewInteraction({
cycle.candidates.length > 1 &&
Math.hypot(e.clientX - cycle.x, e.clientY - cycle.y) < CYCLE_RADIUS_PX &&
downTs - cycle.at < CYCLE_WINDOW_MS;
- if (hit?.element.hasAttribute("data-hf-group")) {
+ if (hit && selectionDataAttribute(hit, "data-hf-group") !== null) {
e.preventDefault();
e.stopPropagation();
cycleRef.current = null;
@@ -134,8 +135,8 @@ export function usePreviewInteraction({
if (
hit &&
!hasStackCycleAtSpot &&
- !hit.element.hasAttribute("data-composition-src") &&
- !hit.element.hasAttribute("data-composition-file")
+ selectionDataAttribute(hit, "data-composition-src") === null &&
+ selectionDataAttribute(hit, "data-composition-file") === null
) {
e.preventDefault();
e.stopPropagation();
diff --git a/packages/studio/src/hooks/useStudioSelectionPublisher.test.tsx b/packages/studio/src/hooks/useStudioSelectionPublisher.test.tsx
new file mode 100644
index 0000000000..1b30ec33c9
--- /dev/null
+++ b/packages/studio/src/hooks/useStudioSelectionPublisher.test.tsx
@@ -0,0 +1,50 @@
+// @vitest-environment happy-dom
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import { afterEach, expect, it, vi } from "vitest";
+import type { DomEditSelection } from "../components/editor/domEditingTypes";
+import { makeSelection } from "./domSelectionTestHarness";
+import { useStudioSelectionPublisher } from "./useStudioSelectionPublisher";
+
+(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+const fetches = vi.hoisted(() => ({ bodies: [] as unknown[] }));
+vi.mock("../utils/studioApiFetch", () => ({
+ studioApiFetch: (_url: string, init: { body: string }) => {
+ fetches.bodies.push(JSON.parse(init.body).selection);
+ return Promise.resolve(new Response(null));
+ },
+}));
+
+afterEach(() => {
+ fetches.bodies.length = 0;
+});
+
+function publish(selection: DomEditSelection) {
+ function Probe() {
+ useStudioSelectionPublisher({
+ projectId: "p",
+ domEditSelection: selection,
+ domEditSelectionRef: { current: selection },
+ refreshKey: 0,
+ previewDocumentVersion: 0,
+ refreshDomEditSelectionFromPreview: () => Promise.resolve(),
+ });
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ act(() => root.render());
+ act(() => root.unmount());
+ return fetches.bodies[0];
+}
+
+it("tells agents about an opaque frame's selection, whose stand-in is never connected", () => {
+ const selection = makeSelection("Title", document.createElement("h1"));
+ expect(publish({ ...selection, handle: "0123456789abcdef:1" })).toMatchObject({
+ schemaVersion: 1,
+ });
+});
+
+it("still withholds a scriptable frame's selection once its element is gone", () => {
+ expect(publish(makeSelection("Title", document.createElement("h1")))).toBeNull();
+});
diff --git a/packages/studio/src/hooks/useStudioSelectionPublisher.ts b/packages/studio/src/hooks/useStudioSelectionPublisher.ts
index b5c4deae09..334658a0fc 100644
--- a/packages/studio/src/hooks/useStudioSelectionPublisher.ts
+++ b/packages/studio/src/hooks/useStudioSelectionPublisher.ts
@@ -2,6 +2,7 @@ import { useEffect, useRef, type MutableRefObject } from "react";
import type { DomEditSelection } from "../components/editor/domEditing";
import { usePlayerStore } from "../player";
import { buildStudioSelectionSnapshot } from "../utils/studioSelectionSnapshot";
+import { selectionElementLive } from "../components/editor/previewSelection";
import { trackStudioEvent } from "../utils/studioTelemetry";
import { studioApiFetch } from "../utils/studioApiFetch";
@@ -49,13 +50,14 @@ export function useStudioSelectionPublisher({
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
if (!projectId) return;
- const selection = domEditSelection?.element.isConnected
- ? buildStudioSelectionSnapshot({
- projectId,
- selection: domEditSelection,
- currentTime: usePlayerStore.getState().currentTime,
- })
- : null;
+ const selection =
+ domEditSelection && selectionElementLive(domEditSelection)
+ ? buildStudioSelectionSnapshot({
+ projectId,
+ selection: domEditSelection,
+ currentTime: usePlayerStore.getState().currentTime,
+ })
+ : null;
const controller = new AbortController();
void putSelection(projectId, selection, controller.signal).catch(reportSelectionPublishError);
return () => controller.abort();
diff --git a/packages/studio/src/player/lib/previewElements.ts b/packages/studio/src/player/lib/previewElements.ts
index be18cbd43a..184c8c71e7 100644
--- a/packages/studio/src/player/lib/previewElements.ts
+++ b/packages/studio/src/player/lib/previewElements.ts
@@ -85,6 +85,18 @@ export async function previewElementsAtPoint(
return elements.map(readHit).filter((element) => element !== null);
}
+export async function previewPointerTargets(
+ iframe: HTMLIFrameElement,
+ rootFractionX: number,
+ rootFractionY: number,
+): Promise<{ target: PreviewElementHit | null; stack: PreviewElementHit[] } | null> {
+ const params = { x: rootFractionX, y: rootFractionY };
+ const reply = await previewRequest(iframe, "pointer-targets", params);
+ const stack: unknown = reply?.stack;
+ if (!reply || !Array.isArray(stack)) return null;
+ return { target: readHit(reply.target), stack: stack.map(readHit).filter((hit) => hit !== null) };
+}
+
/**
* One entry per handle, in order: null for an element the frame no longer has. The whole answer is
* null when the runtime cannot answer, or when there are no handles or more than the runtime takes.
diff --git a/packages/studio/src/styles/hex-baseline.json b/packages/studio/src/styles/hex-baseline.json
index 5e0c513e0d..e1e9f64f8c 100644
--- a/packages/studio/src/styles/hex-baseline.json
+++ b/packages/studio/src/styles/hex-baseline.json
@@ -1,5 +1,5 @@
{
- "total": 226,
+ "total": 223,
"files": {
"packages/studio/src/captions/components/CaptionOverlay.tsx": 5,
"packages/studio/src/captions/components/CaptionTimeline.tsx": 11,
@@ -18,7 +18,6 @@
"packages/studio/src/components/editor/SnapGuideOverlay.tsx": 3,
"packages/studio/src/components/editor/Transform3DCube.tsx": 5,
"packages/studio/src/components/editor/colorValue.ts": 3,
- "packages/studio/src/components/editor/domEditingElement.ts": 1,
"packages/studio/src/components/editor/gradientValue.ts": 6,
"packages/studio/src/components/editor/panelTokens.ts": 4,
"packages/studio/src/components/editor/propertyPanelColor.tsx": 7,
diff --git a/packages/studio/src/utils/studioHelpers.ts b/packages/studio/src/utils/studioHelpers.ts
index 20a77cc8df..b38782ae08 100644
--- a/packages/studio/src/utils/studioHelpers.ts
+++ b/packages/studio/src/utils/studioHelpers.ts
@@ -103,19 +103,6 @@ export function isManualGeometryStyleProperty(property: string): boolean {
return property === "left" || property === "top" || property === "width" || property === "height";
}
-export function getEventTargetElement(target: EventTarget | null): HTMLElement | null {
- if (!target || typeof target !== "object") return null;
- const maybeNode = target as {
- nodeType?: number;
- parentElement?: Element | null;
- };
- if (maybeNode.nodeType === 1) return target as HTMLElement;
- if (maybeNode.nodeType === 3 && maybeNode.parentElement) {
- return maybeNode.parentElement as HTMLElement;
- }
- return null;
-}
-
export function shouldIgnoreHistoryShortcut(target: EventTarget | null): boolean {
return isTypingTarget(target);
}
diff --git a/packages/studio/src/utils/studioPreviewHelpers.test.ts b/packages/studio/src/utils/studioPreviewHelpers.test.ts
index d121a83324..450e6da973 100644
--- a/packages/studio/src/utils/studioPreviewHelpers.test.ts
+++ b/packages/studio/src/utils/studioPreviewHelpers.test.ts
@@ -138,7 +138,7 @@ describe("getPreviewTargetFromPointer", () => {
stubRect(headline, domRect(44, 44, 220, 48));
doc.elementsFromPoint = () => [headline, mask, block, scene];
- expect(getPreviewTargetFromPointer(iframe, 80, 64, "index.html")).toBe(headline);
+ expect(getPreviewTargetFromPointer(iframe, 80, 64)).toBe(headline);
iframe.remove();
});
@@ -156,12 +156,12 @@ describe("getPreviewTargetFromPointer", () => {
return [headline, scene];
};
// First call runs the once-per-document inheritance probe, a body mutation.
- expect(getPreviewTargetFromPointer(iframe, 80, 64, "index.html")).toBe(headline);
+ expect(getPreviewTargetFromPointer(iframe, 80, 64)).toBe(headline);
const observer = new MutationObserver(() => {});
observer.observe(doc, { subtree: true, childList: true, attributes: true });
- expect(getPreviewTargetFromPointer(iframe, 80, 64, "index.html")).toBe(headline);
- expect(getAllPreviewTargetsFromPointer(iframe, 80, 64, "index.html")).toEqual([headline]);
+ expect(getPreviewTargetFromPointer(iframe, 80, 64)).toBe(headline);
+ expect(getAllPreviewTargetsFromPointer(iframe, 80, 64)).toEqual([headline]);
const mutations = observer.takeRecords();
observer.disconnect();
@@ -197,7 +197,7 @@ describe("getPreviewTargetFromPointer", () => {
stubRect(overlay, domRect(0, 0, 360, 260));
doc.elementsFromPoint = () => [overlay, overlayParent, headline, scene];
- expect(getPreviewTargetFromPointer(iframe, 80, 64, "index.html")).toBe(headline);
+ expect(getPreviewTargetFromPointer(iframe, 80, 64)).toBe(headline);
iframe.remove();
});
@@ -227,7 +227,7 @@ describe("getPreviewTargetFromPointer", () => {
stubRect(clickableChild, domRect(40, 40, 80, 24));
doc.elementsFromPoint = () => [clickableChild, overlayParent, scene];
- expect(getPreviewTargetFromPointer(iframe, 60, 50, "index.html")).toBe(clickableChild);
+ expect(getPreviewTargetFromPointer(iframe, 60, 50)).toBe(clickableChild);
iframe.remove();
});
@@ -256,7 +256,7 @@ describe("getPreviewTargetFromPointer", () => {
// Before the fix the video was full-bleed-excluded and the picker fell through
// to the backdrop (or null). It must now return the video itself.
- expect(getPreviewTargetFromPointer(iframe, 200, 150, "index.html")).toBe(hero);
+ expect(getPreviewTargetFromPointer(iframe, 200, 150)).toBe(hero);
iframe.remove();
});
@@ -281,7 +281,7 @@ describe("getPreviewTargetFromPointer", () => {
stubRect(headline, domRect(40, 40, 160, 48));
doc.elementsFromPoint = () => [headline, wrapper, scene];
- expect(getPreviewTargetFromPointer(iframe, 80, 64, "index.html")).toBe(headline);
+ expect(getPreviewTargetFromPointer(iframe, 80, 64)).toBe(headline);
iframe.remove();
});
diff --git a/packages/studio/src/utils/studioPreviewHelpers.ts b/packages/studio/src/utils/studioPreviewHelpers.ts
index 84fd645176..839c7c3810 100644
--- a/packages/studio/src/utils/studioPreviewHelpers.ts
+++ b/packages/studio/src/utils/studioPreviewHelpers.ts
@@ -1,232 +1,21 @@
-import type { DomEditViewport } from "../components/editor/domEditing";
import {
- getDomLayerPatchTarget,
- isElementComputedVisible,
- resolveAllVisualDomEditTargets,
-} from "../components/editor/domEditingElement";
-import { isHtmlElement } from "../components/editor/domEditingDom";
-import { getEventTargetElement } from "./studioHelpers";
+ compositionPoint,
+ coversComposition,
+ pointerTarget,
+ pointerTargets,
+} from "@hyperframes/core/runtime/editing-port";
-interface PreviewLocalPointer {
- x: number;
- y: number;
- viewport: DomEditViewport;
-}
+// One canvas hit-test for Studio and the runtime that answers it for a frame Studio cannot script.
+export { coversComposition };
-// An element is "full-bleed" when its box spans nearly the whole composition on
-// BOTH axes. Such elements (scene wrappers, backdrops) are excluded from canvas
-// click-picking so a click lands on inner content — or deselects on empty area —
-// instead of grabbing the giant container. The Layers panel still selects them.
-// ponytail: pure size heuristic; tighten the ratio if decorative full-bleed art
-// should remain canvas-selectable.
-const FULL_BLEED_RATIO = 0.95;
-
-// Media leaves (a hero/background video, a full-bleed image, an