Files
hyperframes/packages/core/src/runtime/editingAgent.ts
T
James RussoandClaude Opus 5.5 faff4e16a8 feat(studio): read overlay geometry from an opaque preview over its channel (#7628)
* feat(studio): read overlay geometry from an opaque preview over its channel

The runtime's editing channel gains a `geometry` request: per element
handle its client rect, the corners it paints at when rotated, whether it
renders, its clip-path, its source boundary's size, a group wrapper's
member rects, and optionally one element's descendant boxes. On a preview
frame Studio cannot script, the selection, child, group and hover boxes,
the root scale (from the mirrored stage size) and a press held across a
preview change read that geometry, with one read in flight at a time.
Same-origin frames keep reading the DOM directly; the measuring code they
use now lives in core so both paths share one rule.

Also from the review of #7613: opaque selections carry `hasAudio` and
`src` read off the description, a description with a cut attribute or
inline style builds no selection, a failed computed-styles or root read
builds none either, and the group refresh threads its frame and leaves a
group the user changed meanwhile. `findClosestByAttribute`, left without
a caller by #7613, is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(producer): raise the packed size budget for the runtime's geometry read

The producer ships the preview runtime, which now carries the editing
channel's `geometry` request and the measuring code it shares with Studio.
The packed tarball measured 888,810 bytes in CI against an 880,000 budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 00:26:30 +00:00

166 lines
5.5 KiB
TypeScript

/**
* The runtime's end of the editing channel (wire types in `editingPort.ts`).
*
* Each connect from `window.parent` gets a fresh port, so only the parent holds a channel and a
* remounted client is served too; a connect from anything else is ignored. Earlier ports stay
* open: a client may take any one of them, and a disposed client closes its own.
* Requests on the port are validated; a bad one gets a typed error reply, never a throw.
*/
import { swallow } from "./diagnostics";
import {
EDITING_CONNECT,
EDITING_PORT,
EDITING_PROTOCOL_VERSION,
EDITING_REQUEST_KINDS,
isPlainRecord,
parseEditingRequest,
type EditingParams,
type EditingReply,
type EditingRequestKind,
type EditingResult,
} from "./editingPort";
import type { ClipNode } from "./clipTree";
import {
ancestorHandles,
describeHandles,
elementForHandle,
elementsAtPoint,
queryElements,
} from "./editingHandles";
import { elementGeometry } from "./editingGeometry";
import { getLogicalOpacity } from "../studioPreviewMark";
type Handlers = {
[K in EditingRequestKind]: (
params: EditingParams<K>,
) => EditingResult<K> | Promise<EditingResult<K>>;
};
function clipParents(nodes: readonly ClipNode[], out: Array<[string, string]> = []) {
for (const node of nodes) {
if (node.id && node.parentId) out.push([node.id, node.parentId]);
clipParents(node.children, out);
}
return out;
}
const HANDLERS: Handlers = {
hello: () => ({ version: EDITING_PROTOCOL_VERSION, kinds: [...EDITING_REQUEST_KINDS] }),
"runtime-snapshot": () => ({
playerReady: Boolean(window.__player),
startupError: typeof window.__hfStartupError === "string" ? window.__hfStartupError : null,
clipManifest: window.__clipManifest ?? null,
clipParents: clipParents(window.__clipTree?.roots ?? []),
}),
"wait-for-seek": async () => {
const wait = window.__hfWaitForSeekCompletion;
if (typeof wait !== "function") return { waited: false };
await wait();
return { waited: true };
},
"set-loop-start": ({ seconds }) => {
const setLoopStart = window.__hf?.setLoopStart;
if (typeof setLoopStart !== "function") return { applied: false };
setLoopStart(seconds);
return { applied: true };
},
"computed-styles": (params) => {
const { properties } = params;
const element =
"handle" in params
? elementForHandle(params.handle)
: document.querySelector(params.selector);
if (!element) return { styles: null };
const computed = window.getComputedStyle(element);
const styles: Record<string, string> = {};
for (const name of properties) {
const value =
name === "opacity" ? getLogicalOpacity(element, computed) : computed.getPropertyValue(name);
if (value) styles[name] = value;
}
return { styles };
},
"elements-at-point": ({ x, y }) => ({ elements: elementsAtPoint(x, y) }),
describe: ({ handles }) => ({ elements: describeHandles(handles) }),
ancestors: ({ handles }) => ({ ancestors: ancestorHandles(handles) }),
query: ({ selector }) => queryElements(selector),
geometry: ({ handles, children }) => elementGeometry(handles, children),
};
function serve(port: MessagePort): void {
const reply = (message: EditingReply) => {
try {
port.postMessage(message);
} catch (err) {
// A result that cannot be cloned still owes the parent an answer.
if (message.ok) reply(failure(message.id, err));
else swallow("editingAgent.reply", err);
}
};
port.onmessage = (event: MessageEvent<unknown>) => {
const request = parseEditingRequest(event.data);
if (!request.ok) {
reply({
v: EDITING_PROTOCOL_VERSION,
id: request.id,
ok: false,
error: { code: request.code, message: request.message },
});
return;
}
const handler = HANDLERS[request.kind] as (params: unknown) => unknown;
Promise.resolve()
.then(() => handler(request.params))
.then(
(result) => reply({ v: EDITING_PROTOCOL_VERSION, id: request.id, ok: true, result }),
(err: unknown) => reply(failure(request.id, err)),
);
};
}
function failure(id: number, err: unknown): EditingReply {
const message = err instanceof Error ? err.message : String(err);
return {
v: EDITING_PROTOCOL_VERSION,
id,
ok: false,
error: { code: "failed", message: message.slice(0, 500) },
};
}
export function installEditingAgent(parent: Window = window.parent): { dispose: () => void } {
const ports = new Set<MessagePort>();
const onMessage = (event: MessageEvent<unknown>) => {
// A top-level page is its own parent, so a self-post would pass the source check.
if (parent === window || event.source !== parent) return;
const data = event.data;
if (!isPlainRecord(data) || data.source !== "hf-parent" || data.type !== EDITING_CONNECT)
return;
if (data.v !== EDITING_PROTOCOL_VERSION) return;
const channel = new MessageChannel();
const port = channel.port1;
ports.add(port);
serve(port);
try {
// "*" as every runtime post does (bridge.ts): the frame is not told its parent's origin.
parent.postMessage(
{ source: "hf-preview", type: EDITING_PORT, v: EDITING_PROTOCOL_VERSION },
"*",
[channel.port2],
);
} catch (err) {
swallow("editingAgent.handoff", err);
port.close();
ports.delete(port);
}
};
window.addEventListener("message", onMessage);
return {
dispose() {
window.removeEventListener("message", onMessage);
for (const port of ports) port.close();
ports.clear();
},
};
}