@hyperframes/studio 0.9.40 → 0.9.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{codec-BPK_2CLL.js → codec-B_mpbc4w.js} +1 -1
- package/dist/assets/{hyperframes-player-D7qTTDIp.js → hyperframes-player-Dvi3I_mv.js} +1 -1
- package/dist/assets/{index-CdA00Ybn.js → index-B5ExwsOp.js} +146 -146
- package/dist/assets/{index-QS-DuxAU.js → index-BrFm9Y5k.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-Byzz98Gb.js → pausedVideoDecoder-MHQ57yZ6.js} +1 -1
- package/dist/{chunk-KF6YVXJP.js → chunk-4QQMX4BX.js} +6 -3
- package/dist/{chunk-VZ5MD32A.js → chunk-GVW7AU57.js} +190 -254
- package/dist/{chunk-AV2PV7NE.js → chunk-VHV2ZDKY.js} +4 -40
- package/dist/{domEditingLayers-4KRREI5C.js → domEditingLayers-SA5KLSSS.js} +4 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.html +1 -1
- package/dist/index.js +898 -551
- package/dist/player.js +2 -2
- package/package.json +8 -8
- package/src/components/editor/DomEditOverlay.tsx +3 -15
- package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
- package/src/components/editor/domEditOverlayBasis.ts +7 -2
- package/src/components/editor/domEditOverlayCrop.test.ts +1 -1
- package/src/components/editor/domEditOverlayCrop.ts +17 -3
- package/src/components/editor/domEditOverlayGeometry.ts +64 -139
- package/src/components/editor/domEditSelectionRules.ts +138 -0
- package/src/components/editor/domEditing.test.ts +6 -28
- package/src/components/editor/domEditingDom.preview.test.ts +3 -3
- package/src/components/editor/domEditingDom.ts +3 -76
- package/src/components/editor/domEditingElement.ts +19 -240
- package/src/components/editor/domEditingGroups.ts +0 -31
- package/src/components/editor/domEditingLayers.ts +95 -100
- package/src/components/editor/domEditingRootLayer.ts +35 -30
- package/src/components/editor/domEditingTypes.ts +4 -0
- package/src/components/editor/previewGestureStarts.ts +73 -10
- package/src/components/editor/previewOverlayRects.test.ts +316 -0
- package/src/components/editor/previewOverlayRects.ts +123 -0
- package/src/components/editor/previewPointerTargets.test.ts +260 -0
- package/src/components/editor/previewSelection.test.ts +481 -0
- package/src/components/editor/previewSelection.ts +472 -0
- package/src/components/editor/probeSourceElement.ts +19 -1
- package/src/components/editor/propertyPanelFill.tsx +5 -1
- package/src/components/editor/useDomEditOverlayRects.ts +80 -1
- package/src/components/editor/useInlineTextEditing.tsx +1 -6
- package/src/hooks/domSelectionTestHarness.ts +1 -1
- package/src/hooks/useDomEditSession.test.tsx +23 -1
- package/src/hooks/useDomEditSession.ts +2 -1
- package/src/hooks/useDomSelection.ts +61 -47
- package/src/hooks/useGsapSelectionHandlers.test.tsx +17 -0
- package/src/hooks/useGsapSelectionHandlers.ts +2 -1
- package/src/hooks/usePreviewInteraction.test.ts +10 -2
- package/src/hooks/usePreviewInteraction.ts +5 -4
- package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
- package/src/hooks/useStudioSelectionPublisher.ts +9 -7
- package/src/player/lib/previewChannel.ts +4 -3
- package/src/player/lib/previewElements.ts +112 -3
- package/src/styles/hex-baseline.json +1 -2
- package/src/utils/studioHelpers.ts +0 -13
- package/src/utils/studioPreviewHelpers.test.ts +8 -8
- package/src/utils/studioPreviewHelpers.ts +26 -242
- package/src/components/editor/domEditOverlayTransform.ts +0 -126
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selections in a preview frame Studio cannot script, built from its runtime's element
|
|
3
|
+
* descriptions. Which element a selection lands on comes from `domEditSelectionRules.ts`, the
|
|
4
|
+
* rules a scriptable frame's selections use, so both kinds of frame select the same element.
|
|
5
|
+
*
|
|
6
|
+
* Such a selection's `element` is a stand-in: an element with only the target's tag, in a
|
|
7
|
+
* document with no window, the same one for a handle for the whole frame load. Nothing in it
|
|
8
|
+
* loads, runs or renders, and nothing from the frame is copied into it. The frame's element is
|
|
9
|
+
* the selection's `handle`.
|
|
10
|
+
*/
|
|
11
|
+
import { resolveEditingAffordances } from "@hyperframes/core/editing";
|
|
12
|
+
import {
|
|
13
|
+
MAX_ELEMENT_ENTRIES,
|
|
14
|
+
MAX_ELEMENT_VALUE,
|
|
15
|
+
type EditingPortClient,
|
|
16
|
+
} from "@hyperframes/core/runtime/editing-port";
|
|
17
|
+
import {
|
|
18
|
+
previewComputedStyles,
|
|
19
|
+
previewEditingClient,
|
|
20
|
+
previewFrameOpaque,
|
|
21
|
+
} from "../../player/lib/previewChannel";
|
|
22
|
+
import {
|
|
23
|
+
describePreviewElements,
|
|
24
|
+
previewAncestors,
|
|
25
|
+
previewElementSelectorIndex,
|
|
26
|
+
previewPointerTargets,
|
|
27
|
+
previewElementSourceFile,
|
|
28
|
+
queryPreviewElements,
|
|
29
|
+
type PreviewElement,
|
|
30
|
+
} from "../../player/lib/previewElements";
|
|
31
|
+
import { coversCompositionRoot } from "./domEditingRootLayer";
|
|
32
|
+
import {
|
|
33
|
+
buildElementLabel,
|
|
34
|
+
getDataAttributes,
|
|
35
|
+
getInlineStyles,
|
|
36
|
+
isHtmlElement,
|
|
37
|
+
resolveElementSourceFile,
|
|
38
|
+
} from "./domEditingDom";
|
|
39
|
+
import { findElementForSelection } from "./domEditingElement";
|
|
40
|
+
import { capabilityFacts, resolveDomEditSelection } from "./domEditingLayers";
|
|
41
|
+
import {
|
|
42
|
+
chainLocked,
|
|
43
|
+
selectionLookups,
|
|
44
|
+
selectionTargetIndex,
|
|
45
|
+
timelineElementLookup,
|
|
46
|
+
type SelectionChainReader,
|
|
47
|
+
type SelectionLookupTarget,
|
|
48
|
+
} from "./domEditSelectionRules";
|
|
49
|
+
import {
|
|
50
|
+
CURATED_STYLE_PROPERTIES,
|
|
51
|
+
type DomEditContextOptions,
|
|
52
|
+
type DomEditSelection,
|
|
53
|
+
type TimelineElementDomTarget,
|
|
54
|
+
type TimelineElementDomTargetOptions,
|
|
55
|
+
} from "./domEditingTypes";
|
|
56
|
+
import { existsInSourceFile } from "./probeSourceElement";
|
|
57
|
+
import {
|
|
58
|
+
framePointFraction,
|
|
59
|
+
getAllPreviewTargetsFromPointer,
|
|
60
|
+
getPreviewTargetFromPointer,
|
|
61
|
+
} from "../../utils/studioPreviewHelpers";
|
|
62
|
+
import { isVideoAudible } from "../../player/lib/timelineElementHelpers";
|
|
63
|
+
|
|
64
|
+
let inertDocument: Document | undefined;
|
|
65
|
+
const inert = () => (inertDocument ??= document.implementation.createHTMLDocument(""));
|
|
66
|
+
|
|
67
|
+
function inertElement(tag: string): HTMLElement {
|
|
68
|
+
try {
|
|
69
|
+
return inert().createElement(tag);
|
|
70
|
+
} catch {
|
|
71
|
+
return inert().createElement("div");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const standIns = new WeakMap<EditingPortClient, Map<string, HTMLElement>>();
|
|
76
|
+
const standInHandles = new WeakMap<Element, string>();
|
|
77
|
+
const standInAncestors = new WeakMap<Element, string[]>();
|
|
78
|
+
|
|
79
|
+
function standInFor(iframe: HTMLIFrameElement, element: PreviewElement): HTMLElement {
|
|
80
|
+
const client = previewEditingClient(iframe);
|
|
81
|
+
const byHandle = standIns.get(client) ?? new Map<string, HTMLElement>();
|
|
82
|
+
standIns.set(client, byHandle);
|
|
83
|
+
let standIn = byHandle.get(element.handle);
|
|
84
|
+
if (!standIn) {
|
|
85
|
+
standIn = inertElement(element.tag);
|
|
86
|
+
byHandle.set(element.handle, standIn);
|
|
87
|
+
standInHandles.set(standIn, element.handle);
|
|
88
|
+
}
|
|
89
|
+
return standIn;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The description as an element of the inert document, discarded after the read, so the label,
|
|
94
|
+
* data-attribute and inline-style helpers read it as they read a live element.
|
|
95
|
+
*/
|
|
96
|
+
function readable(element: PreviewElement): HTMLElement {
|
|
97
|
+
const probe = inertElement(element.tag);
|
|
98
|
+
for (const [name, value] of element.attributes) {
|
|
99
|
+
if (name !== "id" && name !== "class" && !name.startsWith("data-")) continue;
|
|
100
|
+
try {
|
|
101
|
+
probe.setAttribute(name, value);
|
|
102
|
+
} catch {
|
|
103
|
+
// A name the parser took but setAttribute refuses; no helper reads such a name.
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const [name, value] of Object.entries(element.inlineStyles)) {
|
|
107
|
+
probe.style.setProperty(name, value);
|
|
108
|
+
}
|
|
109
|
+
probe.textContent = element.text;
|
|
110
|
+
return probe;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const attr = (element: PreviewElement, name: string) =>
|
|
114
|
+
element.attributes.find(([key]) => key === name)?.[1] ?? null;
|
|
115
|
+
|
|
116
|
+
const mayBeCut = (value: string) =>
|
|
117
|
+
value.length >= MAX_ELEMENT_VALUE || (value.startsWith("data:") && value.endsWith(","));
|
|
118
|
+
|
|
119
|
+
/** A cut attribute or style (a long `data-audio-fx`, say) would be written back cut; text is not read back. */
|
|
120
|
+
function valuesWhole(element: PreviewElement): boolean {
|
|
121
|
+
if (!element.truncated) return true;
|
|
122
|
+
const styles = Object.values(element.inlineStyles);
|
|
123
|
+
if (element.attributes.length >= MAX_ELEMENT_ENTRIES || styles.length >= MAX_ELEMENT_ENTRIES)
|
|
124
|
+
return false;
|
|
125
|
+
return ![...element.attributes.map(([, value]) => value), ...styles].some(mayBeCut);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const hasAudio = (element: PreviewElement) =>
|
|
129
|
+
element.tag === "video" &&
|
|
130
|
+
attr(element, "muted") === null &&
|
|
131
|
+
isVideoAudible({ tag: "video", hasAudioAttr: attr(element, "data-has-audio"), muted: false });
|
|
132
|
+
|
|
133
|
+
/** Undefined when the frame cannot answer; null when nothing matches. */
|
|
134
|
+
type Found = PreviewElement | null | undefined;
|
|
135
|
+
|
|
136
|
+
const validSelector = (selector: string) => {
|
|
137
|
+
try {
|
|
138
|
+
inert().querySelector(selector);
|
|
139
|
+
return true;
|
|
140
|
+
} catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/** `findElementForSelection` over the frame's runtime. */
|
|
146
|
+
export async function findPreviewElementForSelection(
|
|
147
|
+
iframe: HTMLIFrameElement,
|
|
148
|
+
selection: SelectionLookupTarget,
|
|
149
|
+
activeCompositionPath: string | null,
|
|
150
|
+
): Promise<Found> {
|
|
151
|
+
for (const { selector, index } of selectionLookups(selection)) {
|
|
152
|
+
if (!validSelector(selector)) continue;
|
|
153
|
+
const reply = await queryPreviewElements(iframe, selector);
|
|
154
|
+
if (!reply) return undefined;
|
|
155
|
+
const fromFile = reply.matches.filter(
|
|
156
|
+
({ source }) =>
|
|
157
|
+
!selection.sourceFile ||
|
|
158
|
+
resolveElementSourceFile(source, activeCompositionPath) === selection.sourceFile,
|
|
159
|
+
);
|
|
160
|
+
const match = fromFile[index];
|
|
161
|
+
if (!match) {
|
|
162
|
+
if (reply.truncated) return undefined;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const [element] = (await describePreviewElements(iframe, [match.handle])) ?? [];
|
|
166
|
+
return element ?? undefined;
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A clip built without the frame's document has only the runtime's clip id: an id, else a hf-id. */
|
|
172
|
+
async function findByClipId(
|
|
173
|
+
iframe: HTMLIFrameElement,
|
|
174
|
+
element: TimelineElementDomTarget,
|
|
175
|
+
sourceFile: string,
|
|
176
|
+
path: string | null,
|
|
177
|
+
): Promise<Found> {
|
|
178
|
+
if (!element.id || element.domId || element.selector || element.compositionSrc) return null;
|
|
179
|
+
const byId = await findPreviewElementForSelection(iframe, { id: element.id, sourceFile }, path);
|
|
180
|
+
if (byId !== null) return byId;
|
|
181
|
+
return findPreviewElementForSelection(iframe, { hfId: element.id, sourceFile }, path);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** `findElementForTimelineElement` over the frame's runtime. */
|
|
185
|
+
export async function findPreviewElementForTimelineElement(
|
|
186
|
+
iframe: HTMLIFrameElement,
|
|
187
|
+
element: TimelineElementDomTarget,
|
|
188
|
+
options: TimelineElementDomTargetOptions,
|
|
189
|
+
): Promise<Found> {
|
|
190
|
+
const { target, sourceFile, rootFallback } = timelineElementLookup(element, options);
|
|
191
|
+
const path = options.activeCompositionPath;
|
|
192
|
+
const found = target && (await findPreviewElementForSelection(iframe, target, path));
|
|
193
|
+
if (found !== null) return found;
|
|
194
|
+
const byClipId = await findByClipId(iframe, element, sourceFile, path);
|
|
195
|
+
if (byClipId !== null) return byClipId;
|
|
196
|
+
if (!rootFallback) return null;
|
|
197
|
+
const reply = await queryPreviewElements(iframe, "[data-composition-id]");
|
|
198
|
+
if (!reply) return undefined;
|
|
199
|
+
const root = reply.matches[0];
|
|
200
|
+
if (!root || resolveElementSourceFile(root.source, path) !== sourceFile) return null;
|
|
201
|
+
const [described] = (await describePreviewElements(iframe, [root.handle])) ?? [];
|
|
202
|
+
return described ?? undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The target and every ancestor up to the document element, or null when any is missing. */
|
|
206
|
+
async function previewChain(
|
|
207
|
+
iframe: HTMLIFrameElement,
|
|
208
|
+
target: PreviewElement,
|
|
209
|
+
): Promise<PreviewElement[] | null> {
|
|
210
|
+
const [lineage] = (await previewAncestors(iframe, [target.handle])) ?? [];
|
|
211
|
+
if (!lineage) return null;
|
|
212
|
+
const ancestors = lineage.length ? await describePreviewElements(iframe, lineage) : [];
|
|
213
|
+
if (!ancestors || ancestors.some((entry) => entry === null)) return null;
|
|
214
|
+
const chain = [target, ...(ancestors as PreviewElement[])];
|
|
215
|
+
const linked = chain.every((node, i) => node.parent === (chain[i + 1]?.handle ?? null));
|
|
216
|
+
return linked ? chain : null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function chainReader(chain: readonly PreviewElement[]): SelectionChainReader<PreviewElement> {
|
|
220
|
+
const root = chain.at(-1);
|
|
221
|
+
const body = chain.at(-2);
|
|
222
|
+
return {
|
|
223
|
+
attr,
|
|
224
|
+
selector: (node) => node.selector ?? undefined,
|
|
225
|
+
isDocumentRoot: (node) => node === root || (node === body && node.tag === "body"),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function compositionRoot(iframe: HTMLIFrameElement, documentElement: PreviewElement) {
|
|
230
|
+
const reply = await queryPreviewElements(iframe, "[data-composition-id]");
|
|
231
|
+
if (!reply) return null;
|
|
232
|
+
const first = reply.matches[0];
|
|
233
|
+
const [root] = first ? ((await describePreviewElements(iframe, [first.handle])) ?? [null]) : [];
|
|
234
|
+
if (root === null) return null;
|
|
235
|
+
const element = root ?? documentElement;
|
|
236
|
+
const { x, y, width, height } = element.rect;
|
|
237
|
+
return {
|
|
238
|
+
rect: { left: x, top: y, width, height },
|
|
239
|
+
width: attr(element, "data-width"),
|
|
240
|
+
height: attr(element, "data-height"),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type PreviewSelectionOptions = DomEditContextOptions & {
|
|
245
|
+
projectId?: string | null;
|
|
246
|
+
skipSourceProbe?: boolean;
|
|
247
|
+
exactTarget?: boolean;
|
|
248
|
+
previous?: DomEditSelection | null;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
function chainIndex(
|
|
252
|
+
chain: readonly PreviewElement[],
|
|
253
|
+
read: SelectionChainReader<PreviewElement>,
|
|
254
|
+
options: PreviewSelectionOptions,
|
|
255
|
+
): number | null {
|
|
256
|
+
const active = options.activeGroupElement;
|
|
257
|
+
const activeHandle = active ? standInHandles.get(active) : undefined;
|
|
258
|
+
return selectionTargetIndex(chain, read, {
|
|
259
|
+
exactTarget: options.exactTarget,
|
|
260
|
+
preferClipAncestor: options.preferClipAncestor,
|
|
261
|
+
activeGroupIndex: active ? chain.findIndex((node) => node.handle === activeHandle) : null,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function previewIdentity(current: PreviewElement, path: string | null) {
|
|
266
|
+
const selector = current.selector ?? undefined;
|
|
267
|
+
return {
|
|
268
|
+
id: current.id || undefined,
|
|
269
|
+
hfId: attr(current, "data-hf-id")?.trim() || undefined,
|
|
270
|
+
selector,
|
|
271
|
+
selectorIndex: selector ? previewElementSelectorIndex(current, path) : undefined,
|
|
272
|
+
sourceFile: previewElementSourceFile(current, path),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function isPreviewCompositionRoot(
|
|
277
|
+
iframe: HTMLIFrameElement,
|
|
278
|
+
chain: readonly PreviewElement[],
|
|
279
|
+
index: number,
|
|
280
|
+
computedStyles: Record<string, string>,
|
|
281
|
+
): Promise<boolean | null> {
|
|
282
|
+
const current = chain[index]!;
|
|
283
|
+
if (attr(current, "data-composition-id") !== null && !compositionSourceOf(current)) return true;
|
|
284
|
+
const parent = chain[index + 1];
|
|
285
|
+
const underBody = parent?.tag === "body" && chainReader(chain).isDocumentRoot(parent);
|
|
286
|
+
if (!underBody || attr(current, "data-hf-allow-root-edit") !== null) return false;
|
|
287
|
+
const { x, y, width, height } = current.rect;
|
|
288
|
+
const rect = { left: x, top: y, width, height };
|
|
289
|
+
const root = await compositionRoot(iframe, chain.at(-1)!);
|
|
290
|
+
return root && coversCompositionRoot({ rect, computedStyles }, root);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const compositionSourceOf = (element: PreviewElement) =>
|
|
294
|
+
attr(element, "data-composition-src") ?? attr(element, "data-composition-file") ?? undefined;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* `resolveDomEditSelection` for a frame element Studio has a description of. Text fields are left
|
|
298
|
+
* empty: they are read for text edits, which write through the frame's document.
|
|
299
|
+
*/
|
|
300
|
+
export async function resolvePreviewSelection(
|
|
301
|
+
iframe: HTMLIFrameElement,
|
|
302
|
+
target: PreviewElement,
|
|
303
|
+
options: PreviewSelectionOptions,
|
|
304
|
+
): Promise<DomEditSelection | null> {
|
|
305
|
+
const chain = await previewChain(iframe, target);
|
|
306
|
+
const index = chain && chainIndex(chain, chainReader(chain), options);
|
|
307
|
+
if (!chain || index === null) return null;
|
|
308
|
+
const current = chain[index]!;
|
|
309
|
+
if (!valuesWhole(current)) return null;
|
|
310
|
+
const element = standInFor(iframe, current);
|
|
311
|
+
standInAncestors.set(
|
|
312
|
+
element,
|
|
313
|
+
chain.slice(index + 1).map((node) => node.handle),
|
|
314
|
+
);
|
|
315
|
+
const identity = previewIdentity(current, options.activeCompositionPath);
|
|
316
|
+
const compositionSrc = compositionSourceOf(current);
|
|
317
|
+
const probe = readable(current);
|
|
318
|
+
const inlineStyles = getInlineStyles(probe);
|
|
319
|
+
const computedStyles = await previewComputedStyles(iframe, current, [
|
|
320
|
+
...CURATED_STYLE_PROPERTIES,
|
|
321
|
+
]);
|
|
322
|
+
if (!computedStyles) return null;
|
|
323
|
+
const isCompositionRoot = await isPreviewCompositionRoot(iframe, chain, index, computedStyles);
|
|
324
|
+
if (isCompositionRoot === null) return null;
|
|
325
|
+
const isInsideLocked = chainLocked(chain, chainReader(chain), index);
|
|
326
|
+
const { sourceFile } = identity;
|
|
327
|
+
const existsInSource = await existsInSourceFile(
|
|
328
|
+
options.previous,
|
|
329
|
+
element,
|
|
330
|
+
sourceFile,
|
|
331
|
+
identity,
|
|
332
|
+
options,
|
|
333
|
+
);
|
|
334
|
+
const capabilities = resolveEditingAffordances(
|
|
335
|
+
capabilityFacts({
|
|
336
|
+
hasStableTarget: Boolean(identity.selector || identity.hfId),
|
|
337
|
+
tag: current.tag,
|
|
338
|
+
inlineStyles,
|
|
339
|
+
computedStyles,
|
|
340
|
+
isCompositionHost: Boolean(compositionSrc),
|
|
341
|
+
isCompositionRoot,
|
|
342
|
+
isInsideLockedComposition: isInsideLocked,
|
|
343
|
+
isMasterView: options.isMasterView,
|
|
344
|
+
existsInSource: existsInSource ?? true,
|
|
345
|
+
}),
|
|
346
|
+
).capabilities;
|
|
347
|
+
const { x, y, width, height } = current.rect;
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
element,
|
|
351
|
+
handle: current.handle,
|
|
352
|
+
hasAudio: hasAudio(current),
|
|
353
|
+
src: attr(current, "src"),
|
|
354
|
+
...identity,
|
|
355
|
+
compositionPath: sourceFile,
|
|
356
|
+
compositionSrc,
|
|
357
|
+
isCompositionHost: Boolean(compositionSrc),
|
|
358
|
+
isInsideLockedComposition: isInsideLocked,
|
|
359
|
+
label: buildElementLabel(probe),
|
|
360
|
+
tagName: current.tag,
|
|
361
|
+
boundingBox: { x, y, width, height },
|
|
362
|
+
textContent: current.text.trim() || null,
|
|
363
|
+
dataAttributes: getDataAttributes(probe),
|
|
364
|
+
inlineStyles,
|
|
365
|
+
computedStyles,
|
|
366
|
+
textFields: [],
|
|
367
|
+
capabilities,
|
|
368
|
+
existsInSource,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function liveElementForSelection(
|
|
373
|
+
iframe: HTMLIFrameElement | null,
|
|
374
|
+
selection: SelectionLookupTarget,
|
|
375
|
+
activeCompositionPath: string | null,
|
|
376
|
+
): HTMLElement | null | undefined {
|
|
377
|
+
let doc: Document | null = null;
|
|
378
|
+
try {
|
|
379
|
+
doc = iframe?.contentDocument ?? null;
|
|
380
|
+
} catch {
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
return doc ? findElementForSelection(doc, selection, activeCompositionPath) : undefined;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** `selection`'s element in the frame now, or its description; undefined when the frame cannot answer. */
|
|
387
|
+
export async function findSelectionElement(
|
|
388
|
+
iframe: HTMLIFrameElement | null,
|
|
389
|
+
selection: SelectionLookupTarget,
|
|
390
|
+
activeCompositionPath: string | null,
|
|
391
|
+
): Promise<HTMLElement | Found> {
|
|
392
|
+
return iframe && previewFrameOpaque(iframe)
|
|
393
|
+
? findPreviewElementForSelection(iframe, selection, activeCompositionPath)
|
|
394
|
+
: liveElementForSelection(iframe, selection, activeCompositionPath);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** `resolveDomEditSelection` for an element, or for a description from `iframe`. */
|
|
398
|
+
export async function resolveFoundSelection(
|
|
399
|
+
iframe: HTMLIFrameElement | null,
|
|
400
|
+
found: HTMLElement | PreviewElement,
|
|
401
|
+
options: PreviewSelectionOptions,
|
|
402
|
+
): Promise<DomEditSelection | null> {
|
|
403
|
+
if (isHtmlElement(found)) return resolveDomEditSelection(found, options);
|
|
404
|
+
return iframe ? resolvePreviewSelection(iframe, found, options) : null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** A timeline element's description in a frame Studio cannot script; null for any other frame. */
|
|
408
|
+
export async function findOpaqueTimelineElement(
|
|
409
|
+
iframe: HTMLIFrameElement | null,
|
|
410
|
+
element: TimelineElementDomTarget,
|
|
411
|
+
lookup: TimelineElementDomTargetOptions,
|
|
412
|
+
): Promise<PreviewElement | null> {
|
|
413
|
+
if (!iframe || !previewFrameOpaque(iframe)) return null;
|
|
414
|
+
return (await findPreviewElementForTimelineElement(iframe, element, lookup)) ?? null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function elementWithin(container: HTMLElement, element: HTMLElement): boolean {
|
|
418
|
+
const handle = standInHandles.get(container);
|
|
419
|
+
if (handle === undefined) return container.contains(element);
|
|
420
|
+
return element === container || (standInAncestors.get(element)?.includes(handle) ?? false);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** A `data-` attribute of the selection's element; a stand-in's comes from its description. */
|
|
424
|
+
export function selectionDataAttribute(selection: DomEditSelection, name: `data-${string}`) {
|
|
425
|
+
if (!selection.handle) return selection.element.getAttribute(name);
|
|
426
|
+
return selection.dataAttributes[name.slice("data-".length)] ?? null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const pointerAnswers = new WeakMap<HTMLIFrameElement, { x: number; y: number; found: boolean }>();
|
|
430
|
+
|
|
431
|
+
async function previewPointerTarget(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
|
|
432
|
+
const { x, y } = framePointFraction(iframe, clientX, clientY);
|
|
433
|
+
const reply = await previewPointerTargets(iframe, x, y);
|
|
434
|
+
if (reply) pointerAnswers.set(iframe, { x: clientX, y: clientY, found: reply.target !== null });
|
|
435
|
+
return reply;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** An opaque frame answers from its last pointer read at this point, and assumes a hit without one. */
|
|
439
|
+
export function pointerMayHitElement(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
|
|
440
|
+
if (!previewFrameOpaque(iframe))
|
|
441
|
+
return Boolean(getPreviewTargetFromPointer(iframe, clientX, clientY));
|
|
442
|
+
const answer = pointerAnswers.get(iframe);
|
|
443
|
+
return answer?.x === clientX && answer.y === clientY ? answer.found : true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
export async function findPointerTarget(
|
|
447
|
+
iframe: HTMLIFrameElement,
|
|
448
|
+
clientX: number,
|
|
449
|
+
clientY: number,
|
|
450
|
+
): Promise<HTMLElement | Found> {
|
|
451
|
+
if (!previewFrameOpaque(iframe)) return getPreviewTargetFromPointer(iframe, clientX, clientY);
|
|
452
|
+
const reply = await previewPointerTarget(iframe, clientX, clientY);
|
|
453
|
+
if (!reply?.target) return reply ? null : undefined;
|
|
454
|
+
const [described] = (await describePreviewElements(iframe, [reply.target.handle])) ?? [];
|
|
455
|
+
return described ?? undefined;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export async function findPointerTargets(
|
|
459
|
+
iframe: HTMLIFrameElement,
|
|
460
|
+
clientX: number,
|
|
461
|
+
clientY: number,
|
|
462
|
+
): Promise<Array<HTMLElement | PreviewElement>> {
|
|
463
|
+
if (!previewFrameOpaque(iframe)) return getAllPreviewTargetsFromPointer(iframe, clientX, clientY);
|
|
464
|
+
const reply = await previewPointerTarget(iframe, clientX, clientY);
|
|
465
|
+
const handles = reply?.stack.map((hit) => hit.handle) ?? [];
|
|
466
|
+
const described = handles.length ? await describePreviewElements(iframe, handles) : [];
|
|
467
|
+
return (described ?? []).filter((element) => element !== null);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** A stand-in is never connected: an opaque frame's selection stands until it is replaced. */
|
|
471
|
+
export const selectionElementLive = (selection: DomEditSelection) =>
|
|
472
|
+
selection.handle !== undefined || selection.element.isConnected;
|
|
@@ -4,7 +4,7 @@ import type { DomEditSelection } from "./domEditingTypes";
|
|
|
4
4
|
|
|
5
5
|
export type ProbeTarget = { id?: string; hfId?: string; selector?: string; selectorIndex?: number };
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function knownSourceAnswer(
|
|
8
8
|
previous: DomEditSelection | null | undefined,
|
|
9
9
|
element: HTMLElement,
|
|
10
10
|
sourceFile: string,
|
|
@@ -68,3 +68,21 @@ export function probeSourceElement(
|
|
|
68
68
|
probes.push({ target, resolve });
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
|
+
|
|
72
|
+
export async function existsInSourceFile(
|
|
73
|
+
previous: DomEditSelection | null | undefined,
|
|
74
|
+
element: HTMLElement,
|
|
75
|
+
sourceFile: string,
|
|
76
|
+
target: ProbeTarget,
|
|
77
|
+
options: { projectId?: string | null; skipSourceProbe?: boolean },
|
|
78
|
+
): Promise<boolean | undefined> {
|
|
79
|
+
const probeTarget: ProbeTarget = {};
|
|
80
|
+
if (target.id) probeTarget.id = target.id;
|
|
81
|
+
if (target.hfId) probeTarget.hfId = target.hfId;
|
|
82
|
+
if (target.selector) probeTarget.selector = target.selector;
|
|
83
|
+
if (target.selectorIndex != null) probeTarget.selectorIndex = target.selectorIndex;
|
|
84
|
+
const known = knownSourceAnswer(previous, element, sourceFile, probeTarget);
|
|
85
|
+
if (known !== undefined || options.skipSourceProbe || !options.projectId) return known;
|
|
86
|
+
if (!target.id && !target.selector && !target.hfId) return known;
|
|
87
|
+
return probeSourceElement(options.projectId, sourceFile, probeTarget);
|
|
88
|
+
}
|
|
@@ -101,7 +101,7 @@ export function ImageFillField({
|
|
|
101
101
|
const track = useTrackDesignInput();
|
|
102
102
|
const sourceFile = element.sourceFile;
|
|
103
103
|
const isImage = element.tagName === "img";
|
|
104
|
-
const imageUrl = isImage ? (element
|
|
104
|
+
const imageUrl = isImage ? (selectionSrc(element) ?? "") : value;
|
|
105
105
|
const commitImage = (next: string, projectAsset = false) => {
|
|
106
106
|
if (isImage) {
|
|
107
107
|
if (next)
|
|
@@ -510,3 +510,7 @@ export function GradientField({
|
|
|
510
510
|
</div>
|
|
511
511
|
);
|
|
512
512
|
}
|
|
513
|
+
|
|
514
|
+
function selectionSrc(selection: DomEditSelection): string | null {
|
|
515
|
+
return selection.src === undefined ? selection.element.getAttribute("src") : selection.src;
|
|
516
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* RAF-driven hook that tracks overlay, hover, and group rects from the iframe DOM.
|
|
3
3
|
* Runs a requestAnimationFrame loop and writes React state only when rects change.
|
|
4
|
+
* A frame Studio cannot script is measured over its channel instead, one read at a time.
|
|
4
5
|
*/
|
|
5
6
|
import { useRef, useState, type RefObject } from "react";
|
|
6
7
|
import { useMountEffect } from "../../hooks/useMountEffect";
|
|
@@ -21,6 +22,8 @@ import {
|
|
|
21
22
|
} from "./domEditOverlayGeometry";
|
|
22
23
|
import { computeOverlayRootScale } from "./domEditOverlayBasis";
|
|
23
24
|
import { subscribeOverlayFrame } from "./overlayFrameLoop";
|
|
25
|
+
import { previewFrameOpaque } from "../../player/lib/previewChannel";
|
|
26
|
+
import { measurePreviewOverlay } from "./previewOverlayRects";
|
|
24
27
|
|
|
25
28
|
function childRectsEqual(a: OverlayRect[], b: OverlayRect[]): boolean {
|
|
26
29
|
if (a.length !== b.length) return false;
|
|
@@ -74,6 +77,7 @@ export function useDomEditOverlayRects({
|
|
|
74
77
|
const resolvedHoverElementRef = useRef<{ key: string; element: HTMLElement } | null>(null);
|
|
75
78
|
const resolvedGroupElementRef = useRef<Map<string, HTMLElement>>(new Map());
|
|
76
79
|
const childRectsRef = useRef<OverlayRect[]>([]);
|
|
80
|
+
const channelReadRef = useRef(false);
|
|
77
81
|
|
|
78
82
|
const setOverlayRect = (next: OverlayRect | null) => {
|
|
79
83
|
if (rectsEqual(overlayRectRef.current, next)) return;
|
|
@@ -107,6 +111,74 @@ export function useDomEditOverlayRects({
|
|
|
107
111
|
return next;
|
|
108
112
|
};
|
|
109
113
|
|
|
114
|
+
const setChildRects = (next: OverlayRect[]) => {
|
|
115
|
+
if (childRectsEqual(childRectsRef.current, next)) return;
|
|
116
|
+
childRectsRef.current = next;
|
|
117
|
+
setChildRectsState(next);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** Members of the same group collapse to one selection under select-as-unit, so one key each. */
|
|
121
|
+
const distinctGroup = (group: DomEditSelection[]) => {
|
|
122
|
+
const keys = new Set<string>();
|
|
123
|
+
return group.flatMap((selection) => {
|
|
124
|
+
const key = selectionCacheKey(selection);
|
|
125
|
+
if (keys.has(key)) return [];
|
|
126
|
+
keys.add(key);
|
|
127
|
+
return [{ key, selection }];
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const hoverShown = (sel: DomEditSelection | null, group: DomEditSelection[]) => {
|
|
132
|
+
const hoverSel = hoverSelectionRef.current;
|
|
133
|
+
const key = hoverSel && selectionCacheKey(hoverSel);
|
|
134
|
+
const covered =
|
|
135
|
+
(sel && selectionCacheKey(sel) === key) ||
|
|
136
|
+
group.some((entry) => selectionCacheKey(entry) === key);
|
|
137
|
+
return covered ? null : hoverSel;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** The opaque frame's rects, one geometry read in flight at a time; a stale answer is dropped. */
|
|
141
|
+
const updateOverChannel = (iframe: HTMLIFrameElement, overlayEl: HTMLDivElement) => {
|
|
142
|
+
if (channelReadRef.current) return;
|
|
143
|
+
const sel = selectionRef.current;
|
|
144
|
+
const group = groupSelectionsRef.current;
|
|
145
|
+
const members = distinctGroup(group);
|
|
146
|
+
const hover = hoverShown(sel, group);
|
|
147
|
+
const targets = {
|
|
148
|
+
selection: sel?.handle ?? null,
|
|
149
|
+
group: members.map(({ selection }) => selection.handle ?? null),
|
|
150
|
+
hover: hover?.handle ?? null,
|
|
151
|
+
};
|
|
152
|
+
if (!sel && !members.length && !hover) {
|
|
153
|
+
setOverlayRect(null);
|
|
154
|
+
setChildRects([]);
|
|
155
|
+
setGroupOverlayItems([]);
|
|
156
|
+
setHoverRect(null);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
channelReadRef.current = true;
|
|
160
|
+
void measurePreviewOverlay(iframe, overlayEl, targets)
|
|
161
|
+
.then((rects) => {
|
|
162
|
+
const current =
|
|
163
|
+
selectionRef.current === sel &&
|
|
164
|
+
groupSelectionsRef.current === group &&
|
|
165
|
+
hoverShown(sel, group) === hover;
|
|
166
|
+
if (!rects || !current || rafPausedRef.current || iframeRef.current !== iframe) return;
|
|
167
|
+
setOverlayRect(rects.selection);
|
|
168
|
+
setChildRects(rects.children);
|
|
169
|
+
setGroupOverlayItems(
|
|
170
|
+
members.flatMap(({ key, selection }, index) => {
|
|
171
|
+
const rect = rects.group[index];
|
|
172
|
+
return rect ? [{ key, selection, element: selection.element, rect }] : [];
|
|
173
|
+
}),
|
|
174
|
+
);
|
|
175
|
+
setHoverRect(rects.hover);
|
|
176
|
+
})
|
|
177
|
+
.finally(() => {
|
|
178
|
+
channelReadRef.current = false;
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
|
|
110
182
|
useMountEffect(() => {
|
|
111
183
|
const clearAll = () => {
|
|
112
184
|
setOverlayRect(null);
|
|
@@ -261,7 +333,14 @@ export function useDomEditOverlayRects({
|
|
|
261
333
|
setHoverRect(orientedGroupAwareOverlayRect(overlayEl, iframe, hoverEl, scale));
|
|
262
334
|
};
|
|
263
335
|
|
|
264
|
-
|
|
336
|
+
const frame = () => {
|
|
337
|
+
const iframe = iframeRef.current;
|
|
338
|
+
const overlayEl = overlayRef.current;
|
|
339
|
+
const channel = !rafPausedRef.current && iframe && overlayEl && !iframe.contentDocument;
|
|
340
|
+
if (channel && previewFrameOpaque(iframe)) updateOverChannel(iframe, overlayEl);
|
|
341
|
+
else update();
|
|
342
|
+
};
|
|
343
|
+
return subscribeOverlayFrame(frame);
|
|
265
344
|
});
|
|
266
345
|
|
|
267
346
|
return {
|
|
@@ -64,12 +64,7 @@ export function useInlineTextEditing(
|
|
|
64
64
|
if (!iframe) return null;
|
|
65
65
|
// The same hit test the canvas uses to decide what was clicked; a plain
|
|
66
66
|
// elementFromPoint only finds wrappers, since Studio suppresses pointer events.
|
|
67
|
-
return getPreviewTargetFromPointer(
|
|
68
|
-
iframe,
|
|
69
|
-
event.clientX,
|
|
70
|
-
event.clientY,
|
|
71
|
-
selectionRef.current?.compositionPath ?? null,
|
|
72
|
-
);
|
|
67
|
+
return getPreviewTargetFromPointer(iframe, event.clientX, event.clientY);
|
|
73
68
|
};
|
|
74
69
|
|
|
75
70
|
/** A point on Studio's canvas, in the scaled composition's coordinates. */
|
|
@@ -63,7 +63,7 @@ export function makeSelection(label: string, element: HTMLElement): DomEditSelec
|
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
/** Built as
|
|
66
|
+
/** Built as resolveDomLayerIdentity builds it: a bare class for an element with no id or hf-id. */
|
|
67
67
|
export function stableSelectionFor(el: HTMLElement): DomEditSelection {
|
|
68
68
|
const selector = buildStableSelector(el);
|
|
69
69
|
return {
|