@hyperframes/studio 0.9.40 → 0.9.42
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-CKqLGYc6.js} +1 -1
- package/dist/assets/{hyperframes-player-D7qTTDIp.js → hyperframes-player-DoEYqOIa.js} +1 -1
- package/dist/assets/{index-CdA00Ybn.js → index-DLyQ91Ek.js} +166 -166
- package/dist/assets/{index-QS-DuxAU.js → index-s9iqoWnr.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-Byzz98Gb.js → pausedVideoDecoder-B-ReRg6i.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 +12 -0
- package/dist/index.html +1 -1
- package/dist/index.js +1501 -818
- package/dist/player.js +2 -2
- package/package.json +10 -10
- package/src/components/editor/DomEditOverlay.tsx +3 -15
- package/src/components/editor/LayersPanel.opaque.test.ts +176 -0
- package/src/components/editor/LayersPanel.tsx +41 -10
- package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
- package/src/components/editor/domEditInlineText.ts +1 -16
- 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 +10 -0
- package/src/components/editor/gestureUndoRevert.ts +18 -1
- package/src/components/editor/inlineTextStyleRange.fill.test.ts +129 -0
- package/src/components/editor/inlineTextStyleRange.test.ts +0 -82
- package/src/components/editor/layerKind.ts +2 -19
- package/src/components/editor/layersPanelSort.ts +2 -2
- package/src/components/editor/manualEdits.ts +1 -0
- package/src/components/editor/manualEditsSnapshot.ts +10 -0
- package/src/components/editor/manualOffsetDrag.test.ts +1 -1
- package/src/components/editor/manualOffsetDrag.ts +7 -1
- package/src/components/editor/marqueeCommit.ts +47 -4
- package/src/components/editor/plainTranslate.ts +35 -16
- package/src/components/editor/previewDrafts.test.ts +263 -0
- package/src/components/editor/previewDrafts.ts +73 -0
- package/src/components/editor/previewGestureStarts.ts +73 -10
- package/src/components/editor/previewLayers.test.ts +308 -0
- package/src/components/editor/previewLayers.ts +164 -0
- package/src/components/editor/previewOverlayRects.test.ts +316 -0
- package/src/components/editor/previewOverlayRects.ts +150 -0
- package/src/components/editor/previewPointerTargets.test.ts +327 -0
- package/src/components/editor/previewSelection.test.ts +481 -0
- package/src/components/editor/previewSelection.ts +516 -0
- package/src/components/editor/probeSourceElement.ts +19 -1
- package/src/components/editor/propertyPanelFill.tsx +5 -1
- package/src/components/editor/resizeDraft.ts +38 -0
- package/src/components/editor/useDomEditOverlayGestures.ts +2 -2
- package/src/components/editor/useDomEditOverlayRects.ts +80 -1
- package/src/components/editor/useInlineTextEditing.tsx +1 -6
- package/src/components/editor/useLayerDrag.ts +1 -1
- package/src/components/nle/NLEPreview.slowBoot.test.ts +0 -1
- package/src/hooks/domEditDeleteMembers.test.ts +7 -0
- package/src/hooks/domSelectionTestHarness.ts +1 -1
- package/src/hooks/domSelectionTimelineMirror.ts +3 -1
- package/src/hooks/timelineTimingSync.ts +4 -1
- package/src/hooks/useDomEditSession.test.tsx +23 -1
- package/src/hooks/useDomEditSession.ts +2 -1
- package/src/hooks/useDomSelection.ts +63 -47
- package/src/hooks/useEditorSave.test.tsx +22 -1
- package/src/hooks/useEditorSave.ts +3 -1
- 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 +9 -5
- package/src/hooks/useProjectFileWriter.ts +7 -3
- package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
- package/src/hooks/useStudioSelectionPublisher.ts +9 -7
- package/src/hooks/useTimelineSelectionPreviewSync.test.tsx +30 -0
- package/src/hooks/useTimelineSelectionPreviewSync.ts +7 -2
- package/src/player/lib/layerOrdering.ts +2 -11
- package/src/player/lib/previewChannel.ts +4 -3
- package/src/player/lib/previewElements.ts +143 -3
- package/src/styles/hex-baseline.json +1 -2
- package/src/test-setup.ts +2 -0
- package/src/utils/gsapSoftReload.ts +1 -1
- package/src/utils/studioFileHistory.ts +1 -1
- package/src/utils/studioHelpers.ts +27 -17
- package/src/utils/studioPreviewHelpers.test.ts +8 -8
- package/src/utils/studioPreviewHelpers.ts +26 -242
- package/src/utils/studioSaveDiagnostics.test.ts +34 -0
- package/src/utils/studioSaveDiagnostics.ts +35 -6
- package/src/utils/timelineCompositionInsert.ts +3 -1
- package/src/components/editor/domEditOverlayTransform.ts +0 -126
|
@@ -0,0 +1,516 @@
|
|
|
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 the target's tag, in a document
|
|
7
|
+
* with no window, the same one for a handle for the whole frame load. Nothing in it loads, runs or
|
|
8
|
+
* renders; from the frame it takes only the inline translate, box and Studio marks a gesture starts
|
|
9
|
+
* from, and the px the frame renders that translate as. The frame's element is the selection's
|
|
10
|
+
* `handle`.
|
|
11
|
+
*/
|
|
12
|
+
import { resolveEditingAffordances } from "@hyperframes/core/editing";
|
|
13
|
+
import {
|
|
14
|
+
MAX_ELEMENT_ENTRIES,
|
|
15
|
+
MAX_ELEMENT_VALUE,
|
|
16
|
+
type EditingPortClient,
|
|
17
|
+
} from "@hyperframes/core/runtime/editing-port";
|
|
18
|
+
import {
|
|
19
|
+
previewComputedStyles,
|
|
20
|
+
previewEditingClient,
|
|
21
|
+
previewFrameOpaque,
|
|
22
|
+
} from "../../player/lib/previewChannel";
|
|
23
|
+
import {
|
|
24
|
+
describePreviewElements,
|
|
25
|
+
previewAncestors,
|
|
26
|
+
previewElementSelectorIndex,
|
|
27
|
+
previewPointerTargets,
|
|
28
|
+
previewElementSourceFile,
|
|
29
|
+
queryPreviewElements,
|
|
30
|
+
type PreviewElement,
|
|
31
|
+
} from "../../player/lib/previewElements";
|
|
32
|
+
import { coversCompositionRoot } from "./domEditingRootLayer";
|
|
33
|
+
import {
|
|
34
|
+
buildElementLabel,
|
|
35
|
+
getDataAttributes,
|
|
36
|
+
getInlineStyles,
|
|
37
|
+
isHtmlElement,
|
|
38
|
+
resolveElementSourceFile,
|
|
39
|
+
} from "./domEditingDom";
|
|
40
|
+
import { findElementForSelection } from "./domEditingElement";
|
|
41
|
+
import { capabilityFacts, resolveDomEditSelection } from "./domEditingLayers";
|
|
42
|
+
import {
|
|
43
|
+
chainLocked,
|
|
44
|
+
selectionLookups,
|
|
45
|
+
selectionTargetIndex,
|
|
46
|
+
timelineElementLookup,
|
|
47
|
+
type SelectionChainReader,
|
|
48
|
+
type SelectionLookupTarget,
|
|
49
|
+
} from "./domEditSelectionRules";
|
|
50
|
+
import {
|
|
51
|
+
CURATED_STYLE_PROPERTIES,
|
|
52
|
+
type DomEditContextOptions,
|
|
53
|
+
type DomEditSelection,
|
|
54
|
+
type TimelineElementDomTarget,
|
|
55
|
+
type TimelineElementDomTargetOptions,
|
|
56
|
+
} from "./domEditingTypes";
|
|
57
|
+
import { existsInSourceFile } from "./probeSourceElement";
|
|
58
|
+
import {
|
|
59
|
+
framePointFraction,
|
|
60
|
+
getAllPreviewTargetsFromPointer,
|
|
61
|
+
getPreviewTargetFromPointer,
|
|
62
|
+
} from "../../utils/studioPreviewHelpers";
|
|
63
|
+
import { isVideoAudible } from "../../player/lib/timelineElementHelpers";
|
|
64
|
+
import { registerPreviewStandIn } from "./previewDrafts";
|
|
65
|
+
import {
|
|
66
|
+
STUDIO_HEIGHT_PROP,
|
|
67
|
+
STUDIO_OFFSET_X_PROP,
|
|
68
|
+
STUDIO_OFFSET_Y_PROP,
|
|
69
|
+
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
|
|
70
|
+
STUDIO_ORIGINAL_TRANSLATE_ATTR,
|
|
71
|
+
STUDIO_PATH_OFFSET_ATTR,
|
|
72
|
+
STUDIO_WIDTH_PROP,
|
|
73
|
+
} from "./manualEditsTypes";
|
|
74
|
+
import { rememberFrameTranslate, TRANSLATE_PX_PROPERTIES, translatePx } from "./plainTranslate";
|
|
75
|
+
|
|
76
|
+
let inertDocument: Document | undefined;
|
|
77
|
+
const inert = () => (inertDocument ??= document.implementation.createHTMLDocument(""));
|
|
78
|
+
|
|
79
|
+
function inertElement(tag: string): HTMLElement {
|
|
80
|
+
try {
|
|
81
|
+
return inert().createElement(tag);
|
|
82
|
+
} catch {
|
|
83
|
+
return inert().createElement("div");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const GESTURE_START_STYLES = [
|
|
88
|
+
"translate",
|
|
89
|
+
STUDIO_OFFSET_X_PROP,
|
|
90
|
+
STUDIO_OFFSET_Y_PROP,
|
|
91
|
+
STUDIO_WIDTH_PROP,
|
|
92
|
+
STUDIO_HEIGHT_PROP,
|
|
93
|
+
];
|
|
94
|
+
const GESTURE_START_ATTRS = [
|
|
95
|
+
STUDIO_PATH_OFFSET_ATTR,
|
|
96
|
+
STUDIO_ORIGINAL_TRANSLATE_ATTR,
|
|
97
|
+
STUDIO_ORIGINAL_INLINE_TRANSLATE_ATTR,
|
|
98
|
+
];
|
|
99
|
+
const standIns = new WeakMap<EditingPortClient, Map<string, HTMLElement>>();
|
|
100
|
+
const standInHandles = new WeakMap<Element, string>();
|
|
101
|
+
const standInAncestors = new WeakMap<Element, string[]>();
|
|
102
|
+
|
|
103
|
+
export function standInFor(iframe: HTMLIFrameElement, element: PreviewElement): HTMLElement {
|
|
104
|
+
const client = previewEditingClient(iframe);
|
|
105
|
+
const byHandle = standIns.get(client) ?? new Map<string, HTMLElement>();
|
|
106
|
+
standIns.set(client, byHandle);
|
|
107
|
+
let standIn = byHandle.get(element.handle);
|
|
108
|
+
if (!standIn) {
|
|
109
|
+
standIn = inertElement(element.tag);
|
|
110
|
+
for (const name of GESTURE_START_STYLES) {
|
|
111
|
+
const value = element.inlineStyles[name];
|
|
112
|
+
if (value && !mayBeCut(value)) standIn.style.setProperty(name, value);
|
|
113
|
+
}
|
|
114
|
+
for (const name of GESTURE_START_ATTRS) {
|
|
115
|
+
const value = attr(element, name);
|
|
116
|
+
if (value !== null && !mayBeCut(value)) standIn.setAttribute(name, value);
|
|
117
|
+
}
|
|
118
|
+
byHandle.set(element.handle, standIn);
|
|
119
|
+
standInHandles.set(standIn, element.handle);
|
|
120
|
+
registerPreviewStandIn(standIn, iframe, element.handle);
|
|
121
|
+
}
|
|
122
|
+
return standIn;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The description as an element of the inert document, discarded after the read, so the label,
|
|
127
|
+
* data-attribute and inline-style helpers read it as they read a live element.
|
|
128
|
+
*/
|
|
129
|
+
export function readable(element: PreviewElement): HTMLElement {
|
|
130
|
+
const probe = inertElement(element.tag);
|
|
131
|
+
for (const [name, value] of element.attributes) {
|
|
132
|
+
if (name !== "id" && name !== "class" && !name.startsWith("data-")) continue;
|
|
133
|
+
try {
|
|
134
|
+
probe.setAttribute(name, value);
|
|
135
|
+
} catch {
|
|
136
|
+
// A name the parser took but setAttribute refuses; no helper reads such a name.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
for (const [name, value] of Object.entries(element.inlineStyles)) {
|
|
140
|
+
probe.style.setProperty(name, value);
|
|
141
|
+
}
|
|
142
|
+
probe.textContent = element.text;
|
|
143
|
+
return probe;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const attr = (element: PreviewElement, name: string) =>
|
|
147
|
+
element.attributes.find(([key]) => key === name)?.[1] ?? null;
|
|
148
|
+
|
|
149
|
+
const mayBeCut = (value: string) =>
|
|
150
|
+
value.length >= MAX_ELEMENT_VALUE || (value.startsWith("data:") && value.endsWith(","));
|
|
151
|
+
|
|
152
|
+
/** A cut attribute or style (a long `data-audio-fx`, say) would be written back cut; text is not read back. */
|
|
153
|
+
function valuesWhole(element: PreviewElement): boolean {
|
|
154
|
+
if (!element.truncated) return true;
|
|
155
|
+
const styles = Object.values(element.inlineStyles);
|
|
156
|
+
if (element.attributes.length >= MAX_ELEMENT_ENTRIES || styles.length >= MAX_ELEMENT_ENTRIES)
|
|
157
|
+
return false;
|
|
158
|
+
return ![...element.attributes.map(([, value]) => value), ...styles].some(mayBeCut);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const hasAudio = (element: PreviewElement) =>
|
|
162
|
+
element.tag === "video" &&
|
|
163
|
+
attr(element, "muted") === null &&
|
|
164
|
+
isVideoAudible({ tag: "video", hasAudioAttr: attr(element, "data-has-audio"), muted: false });
|
|
165
|
+
|
|
166
|
+
/** Undefined when the frame cannot answer; null when nothing matches. */
|
|
167
|
+
type Found = PreviewElement | null | undefined;
|
|
168
|
+
|
|
169
|
+
const validSelector = (selector: string) => {
|
|
170
|
+
try {
|
|
171
|
+
inert().querySelector(selector);
|
|
172
|
+
return true;
|
|
173
|
+
} catch {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** `findElementForSelection` over the frame's runtime. */
|
|
179
|
+
export async function findPreviewElementForSelection(
|
|
180
|
+
iframe: HTMLIFrameElement,
|
|
181
|
+
selection: SelectionLookupTarget,
|
|
182
|
+
activeCompositionPath: string | null,
|
|
183
|
+
): Promise<Found> {
|
|
184
|
+
for (const { selector, index } of selectionLookups(selection)) {
|
|
185
|
+
if (!validSelector(selector)) continue;
|
|
186
|
+
const reply = await queryPreviewElements(iframe, selector);
|
|
187
|
+
if (!reply) return undefined;
|
|
188
|
+
const fromFile = reply.matches.filter(
|
|
189
|
+
({ source }) =>
|
|
190
|
+
!selection.sourceFile ||
|
|
191
|
+
resolveElementSourceFile(source, activeCompositionPath) === selection.sourceFile,
|
|
192
|
+
);
|
|
193
|
+
const match = fromFile[index];
|
|
194
|
+
if (!match) {
|
|
195
|
+
if (reply.truncated) return undefined;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const [element] = (await describePreviewElements(iframe, [match.handle])) ?? [];
|
|
199
|
+
return element ?? undefined;
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A clip built without the frame's document has only the runtime's clip id: an id, else a hf-id. */
|
|
205
|
+
async function findByClipId(
|
|
206
|
+
iframe: HTMLIFrameElement,
|
|
207
|
+
element: TimelineElementDomTarget,
|
|
208
|
+
sourceFile: string,
|
|
209
|
+
path: string | null,
|
|
210
|
+
): Promise<Found> {
|
|
211
|
+
if (!element.id || element.domId || element.selector || element.compositionSrc) return null;
|
|
212
|
+
const byId = await findPreviewElementForSelection(iframe, { id: element.id, sourceFile }, path);
|
|
213
|
+
if (byId !== null) return byId;
|
|
214
|
+
return findPreviewElementForSelection(iframe, { hfId: element.id, sourceFile }, path);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** `findElementForTimelineElement` over the frame's runtime. */
|
|
218
|
+
export async function findPreviewElementForTimelineElement(
|
|
219
|
+
iframe: HTMLIFrameElement,
|
|
220
|
+
element: TimelineElementDomTarget,
|
|
221
|
+
options: TimelineElementDomTargetOptions,
|
|
222
|
+
): Promise<Found> {
|
|
223
|
+
const { target, sourceFile, rootFallback } = timelineElementLookup(element, options);
|
|
224
|
+
const path = options.activeCompositionPath;
|
|
225
|
+
const found = target && (await findPreviewElementForSelection(iframe, target, path));
|
|
226
|
+
if (found !== null) return found;
|
|
227
|
+
const byClipId = await findByClipId(iframe, element, sourceFile, path);
|
|
228
|
+
if (byClipId !== null) return byClipId;
|
|
229
|
+
if (!rootFallback) return null;
|
|
230
|
+
const reply = await queryPreviewElements(iframe, "[data-composition-id]");
|
|
231
|
+
if (!reply) return undefined;
|
|
232
|
+
const root = reply.matches[0];
|
|
233
|
+
if (!root || resolveElementSourceFile(root.source, path) !== sourceFile) return null;
|
|
234
|
+
const [described] = (await describePreviewElements(iframe, [root.handle])) ?? [];
|
|
235
|
+
return described ?? undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The target and every ancestor up to the document element, or null when any is missing. */
|
|
239
|
+
async function previewChain(
|
|
240
|
+
iframe: HTMLIFrameElement,
|
|
241
|
+
target: PreviewElement,
|
|
242
|
+
): Promise<PreviewElement[] | null> {
|
|
243
|
+
const [lineage] = (await previewAncestors(iframe, [target.handle])) ?? [];
|
|
244
|
+
if (!lineage) return null;
|
|
245
|
+
const ancestors = lineage.length ? await describePreviewElements(iframe, lineage) : [];
|
|
246
|
+
if (!ancestors || ancestors.some((entry) => entry === null)) return null;
|
|
247
|
+
const chain = [target, ...(ancestors as PreviewElement[])];
|
|
248
|
+
const linked = chain.every((node, i) => node.parent === (chain[i + 1]?.handle ?? null));
|
|
249
|
+
return linked ? chain : null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function chainReader(chain: readonly PreviewElement[]): SelectionChainReader<PreviewElement> {
|
|
253
|
+
const root = chain.at(-1);
|
|
254
|
+
const body = chain.at(-2);
|
|
255
|
+
return {
|
|
256
|
+
attr,
|
|
257
|
+
selector: (node) => node.selector ?? undefined,
|
|
258
|
+
isDocumentRoot: (node) => node === root || (node === body && node.tag === "body"),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function compositionRoot(iframe: HTMLIFrameElement, documentElement: PreviewElement) {
|
|
263
|
+
const reply = await queryPreviewElements(iframe, "[data-composition-id]");
|
|
264
|
+
if (!reply) return null;
|
|
265
|
+
const first = reply.matches[0];
|
|
266
|
+
const [root] = first ? ((await describePreviewElements(iframe, [first.handle])) ?? [null]) : [];
|
|
267
|
+
if (root === null) return null;
|
|
268
|
+
const element = root ?? documentElement;
|
|
269
|
+
const { x, y, width, height } = element.rect;
|
|
270
|
+
return {
|
|
271
|
+
rect: { left: x, top: y, width, height },
|
|
272
|
+
width: attr(element, "data-width"),
|
|
273
|
+
height: attr(element, "data-height"),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export type PreviewSelectionOptions = DomEditContextOptions & {
|
|
278
|
+
projectId?: string | null;
|
|
279
|
+
skipSourceProbe?: boolean;
|
|
280
|
+
exactTarget?: boolean;
|
|
281
|
+
previous?: DomEditSelection | null;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
function chainIndex(
|
|
285
|
+
chain: readonly PreviewElement[],
|
|
286
|
+
read: SelectionChainReader<PreviewElement>,
|
|
287
|
+
options: PreviewSelectionOptions,
|
|
288
|
+
): number | null {
|
|
289
|
+
const active = options.activeGroupElement;
|
|
290
|
+
const activeHandle = active ? standInHandles.get(active) : undefined;
|
|
291
|
+
return selectionTargetIndex(chain, read, {
|
|
292
|
+
exactTarget: options.exactTarget,
|
|
293
|
+
preferClipAncestor: options.preferClipAncestor,
|
|
294
|
+
activeGroupIndex: active ? chain.findIndex((node) => node.handle === activeHandle) : null,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function previewIdentity(current: PreviewElement, path: string | null) {
|
|
299
|
+
const selector = current.selector ?? undefined;
|
|
300
|
+
return {
|
|
301
|
+
id: current.id || undefined,
|
|
302
|
+
hfId: attr(current, "data-hf-id")?.trim() || undefined,
|
|
303
|
+
selector,
|
|
304
|
+
selectorIndex: selector ? previewElementSelectorIndex(current, path) : undefined,
|
|
305
|
+
sourceFile: previewElementSourceFile(current, path),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async function isPreviewCompositionRoot(
|
|
310
|
+
iframe: HTMLIFrameElement,
|
|
311
|
+
chain: readonly PreviewElement[],
|
|
312
|
+
index: number,
|
|
313
|
+
computedStyles: Record<string, string>,
|
|
314
|
+
): Promise<boolean | null> {
|
|
315
|
+
const current = chain[index]!;
|
|
316
|
+
if (attr(current, "data-composition-id") !== null && !compositionSourceOf(current)) return true;
|
|
317
|
+
const parent = chain[index + 1];
|
|
318
|
+
const underBody = parent?.tag === "body" && chainReader(chain).isDocumentRoot(parent);
|
|
319
|
+
if (!underBody || attr(current, "data-hf-allow-root-edit") !== null) return false;
|
|
320
|
+
const { x, y, width, height } = current.rect;
|
|
321
|
+
const rect = { left: x, top: y, width, height };
|
|
322
|
+
const root = await compositionRoot(iframe, chain.at(-1)!);
|
|
323
|
+
return root && coversCompositionRoot({ rect, computedStyles }, root);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const compositionSourceOf = (element: PreviewElement) =>
|
|
327
|
+
attr(element, "data-composition-src") ?? attr(element, "data-composition-file") ?? undefined;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* `resolveDomEditSelection` for a frame element Studio has a description of. Text fields are left
|
|
331
|
+
* empty: they are read for text edits, which write through the frame's document.
|
|
332
|
+
*/
|
|
333
|
+
export async function resolvePreviewSelection(
|
|
334
|
+
iframe: HTMLIFrameElement,
|
|
335
|
+
target: PreviewElement,
|
|
336
|
+
options: PreviewSelectionOptions,
|
|
337
|
+
): Promise<DomEditSelection | null> {
|
|
338
|
+
const chain = await previewChain(iframe, target);
|
|
339
|
+
const index = chain && chainIndex(chain, chainReader(chain), options);
|
|
340
|
+
if (!chain || index === null) return null;
|
|
341
|
+
const current = chain[index]!;
|
|
342
|
+
if (!valuesWhole(current)) return null;
|
|
343
|
+
const element = standInFor(iframe, current);
|
|
344
|
+
standInAncestors.set(
|
|
345
|
+
element,
|
|
346
|
+
chain.slice(index + 1).map((node) => node.handle),
|
|
347
|
+
);
|
|
348
|
+
const identity = previewIdentity(current, options.activeCompositionPath);
|
|
349
|
+
const compositionSrc = compositionSourceOf(current);
|
|
350
|
+
const probe = readable(current);
|
|
351
|
+
const inlineStyles = getInlineStyles(probe);
|
|
352
|
+
const styles = await previewComputedStyles(iframe, current, [
|
|
353
|
+
...new Set([...CURATED_STYLE_PROPERTIES, ...TRANSLATE_PX_PROPERTIES]),
|
|
354
|
+
]);
|
|
355
|
+
if (!styles) return null;
|
|
356
|
+
// The stand-in has no box to size a % or em against: a move starts from what the frame renders.
|
|
357
|
+
rememberFrameTranslate(
|
|
358
|
+
element,
|
|
359
|
+
translatePx(styles.translate ?? "none", (p) => styles[p] ?? ""),
|
|
360
|
+
);
|
|
361
|
+
const computedStyles = Object.fromEntries(
|
|
362
|
+
CURATED_STYLE_PROPERTIES.flatMap((p) => (p in styles ? [[p, styles[p]!]] : [])),
|
|
363
|
+
);
|
|
364
|
+
const isCompositionRoot = await isPreviewCompositionRoot(iframe, chain, index, computedStyles);
|
|
365
|
+
if (isCompositionRoot === null) return null;
|
|
366
|
+
const isInsideLocked = chainLocked(chain, chainReader(chain), index);
|
|
367
|
+
const { sourceFile } = identity;
|
|
368
|
+
const existsInSource = await existsInSourceFile(
|
|
369
|
+
options.previous,
|
|
370
|
+
element,
|
|
371
|
+
sourceFile,
|
|
372
|
+
identity,
|
|
373
|
+
options,
|
|
374
|
+
);
|
|
375
|
+
const capabilities = resolveEditingAffordances(
|
|
376
|
+
capabilityFacts({
|
|
377
|
+
hasStableTarget: Boolean(identity.selector || identity.hfId),
|
|
378
|
+
tag: current.tag,
|
|
379
|
+
inlineStyles,
|
|
380
|
+
computedStyles,
|
|
381
|
+
isCompositionHost: Boolean(compositionSrc),
|
|
382
|
+
isCompositionRoot,
|
|
383
|
+
isInsideLockedComposition: isInsideLocked,
|
|
384
|
+
isMasterView: options.isMasterView,
|
|
385
|
+
existsInSource: existsInSource ?? true,
|
|
386
|
+
}),
|
|
387
|
+
).capabilities;
|
|
388
|
+
const { x, y, width, height } = current.rect;
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
element,
|
|
392
|
+
handle: current.handle,
|
|
393
|
+
hasAudio: hasAudio(current),
|
|
394
|
+
src: attr(current, "src"),
|
|
395
|
+
...identity,
|
|
396
|
+
compositionPath: sourceFile,
|
|
397
|
+
compositionSrc,
|
|
398
|
+
isCompositionHost: Boolean(compositionSrc),
|
|
399
|
+
isInsideLockedComposition: isInsideLocked,
|
|
400
|
+
label: buildElementLabel(probe),
|
|
401
|
+
tagName: current.tag,
|
|
402
|
+
boundingBox: { x, y, width, height },
|
|
403
|
+
textContent: current.text.trim() || null,
|
|
404
|
+
dataAttributes: getDataAttributes(probe),
|
|
405
|
+
inlineStyles,
|
|
406
|
+
computedStyles,
|
|
407
|
+
textFields: [],
|
|
408
|
+
capabilities,
|
|
409
|
+
existsInSource,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function liveElementForSelection(
|
|
414
|
+
iframe: HTMLIFrameElement | null,
|
|
415
|
+
selection: SelectionLookupTarget,
|
|
416
|
+
activeCompositionPath: string | null,
|
|
417
|
+
): HTMLElement | null | undefined {
|
|
418
|
+
let doc: Document | null = null;
|
|
419
|
+
try {
|
|
420
|
+
doc = iframe?.contentDocument ?? null;
|
|
421
|
+
} catch {
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
return doc ? findElementForSelection(doc, selection, activeCompositionPath) : undefined;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** `selection`'s element in the frame now, or its description; undefined when the frame cannot answer. */
|
|
428
|
+
export async function findSelectionElement(
|
|
429
|
+
iframe: HTMLIFrameElement | null,
|
|
430
|
+
selection: SelectionLookupTarget,
|
|
431
|
+
activeCompositionPath: string | null,
|
|
432
|
+
): Promise<HTMLElement | Found> {
|
|
433
|
+
return iframe && previewFrameOpaque(iframe)
|
|
434
|
+
? findPreviewElementForSelection(iframe, selection, activeCompositionPath)
|
|
435
|
+
: liveElementForSelection(iframe, selection, activeCompositionPath);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** `resolveDomEditSelection` for an element, or for a description from `iframe`. */
|
|
439
|
+
export async function resolveFoundSelection(
|
|
440
|
+
iframe: HTMLIFrameElement | null,
|
|
441
|
+
found: HTMLElement | PreviewElement,
|
|
442
|
+
options: PreviewSelectionOptions,
|
|
443
|
+
): Promise<DomEditSelection | null> {
|
|
444
|
+
if (isHtmlElement(found)) return resolveDomEditSelection(found, options);
|
|
445
|
+
return iframe ? resolvePreviewSelection(iframe, found, options) : null;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** A timeline element's description in a frame Studio cannot script; null for any other frame. */
|
|
449
|
+
export async function findOpaqueTimelineElement(
|
|
450
|
+
iframe: HTMLIFrameElement | null,
|
|
451
|
+
element: TimelineElementDomTarget,
|
|
452
|
+
lookup: TimelineElementDomTargetOptions,
|
|
453
|
+
): Promise<PreviewElement | null> {
|
|
454
|
+
if (!iframe || !previewFrameOpaque(iframe)) return null;
|
|
455
|
+
return (await findPreviewElementForTimelineElement(iframe, element, lookup)) ?? null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export const standInHandle = (element: Element) => standInHandles.get(element);
|
|
459
|
+
|
|
460
|
+
export function elementWithin(container: HTMLElement, element: HTMLElement): boolean {
|
|
461
|
+
const handle = standInHandles.get(container);
|
|
462
|
+
if (handle === undefined) return container.contains(element);
|
|
463
|
+
return element === container || (standInAncestors.get(element)?.includes(handle) ?? false);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** A `data-` attribute of the selection's element; a stand-in's comes from its description. */
|
|
467
|
+
export function selectionDataAttribute(selection: DomEditSelection, name: `data-${string}`) {
|
|
468
|
+
if (!selection.handle) return selection.element.getAttribute(name);
|
|
469
|
+
return selection.dataAttributes[name.slice("data-".length)] ?? null;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const pointerAnswers = new WeakMap<HTMLIFrameElement, { x: number; y: number; found: boolean }>();
|
|
473
|
+
|
|
474
|
+
async function previewPointerTarget(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
|
|
475
|
+
const { x, y } = framePointFraction(iframe, clientX, clientY);
|
|
476
|
+
const reply = await previewPointerTargets(iframe, x, y);
|
|
477
|
+
if (reply) pointerAnswers.set(iframe, { x: clientX, y: clientY, found: reply.target !== null });
|
|
478
|
+
return reply;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** An opaque frame answers from its last pointer read at this point, and assumes a hit without one. */
|
|
482
|
+
export function pointerMayHitElement(iframe: HTMLIFrameElement, clientX: number, clientY: number) {
|
|
483
|
+
if (!previewFrameOpaque(iframe))
|
|
484
|
+
return Boolean(getPreviewTargetFromPointer(iframe, clientX, clientY));
|
|
485
|
+
const answer = pointerAnswers.get(iframe);
|
|
486
|
+
const near = answer && Math.hypot(answer.x - clientX, answer.y - clientY) <= 2;
|
|
487
|
+
return near ? answer.found : true;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export async function findPointerTarget(
|
|
491
|
+
iframe: HTMLIFrameElement,
|
|
492
|
+
clientX: number,
|
|
493
|
+
clientY: number,
|
|
494
|
+
): Promise<HTMLElement | Found> {
|
|
495
|
+
if (!previewFrameOpaque(iframe)) return getPreviewTargetFromPointer(iframe, clientX, clientY);
|
|
496
|
+
const reply = await previewPointerTarget(iframe, clientX, clientY);
|
|
497
|
+
if (!reply?.target) return reply ? null : undefined;
|
|
498
|
+
const [described] = (await describePreviewElements(iframe, [reply.target.handle])) ?? [];
|
|
499
|
+
return described ?? undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export async function findPointerTargets(
|
|
503
|
+
iframe: HTMLIFrameElement,
|
|
504
|
+
clientX: number,
|
|
505
|
+
clientY: number,
|
|
506
|
+
): Promise<Array<HTMLElement | PreviewElement>> {
|
|
507
|
+
if (!previewFrameOpaque(iframe)) return getAllPreviewTargetsFromPointer(iframe, clientX, clientY);
|
|
508
|
+
const reply = await previewPointerTarget(iframe, clientX, clientY);
|
|
509
|
+
const handles = reply?.stack.map((hit) => hit.handle) ?? [];
|
|
510
|
+
const described = handles.length ? await describePreviewElements(iframe, handles) : [];
|
|
511
|
+
return (described ?? []).filter((element) => element !== null);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** A stand-in is never connected: an opaque frame's selection stands until it is replaced. */
|
|
515
|
+
export const selectionElementLive = (selection: DomEditSelection) =>
|
|
516
|
+
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
|
+
}
|
|
@@ -9,6 +9,9 @@ import {
|
|
|
9
9
|
import { computeNextResizeAnchor, resizeRemainderShift } from "./domEditResizeLocal";
|
|
10
10
|
import type { ResizeDraftSizes } from "./domEditResizeLocal";
|
|
11
11
|
import { applyManualOffsetDragDraft } from "./manualOffsetDrag";
|
|
12
|
+
import { endStudioManualEditGesture, isStudioManualEditGestureCurrent } from "./manualEdits";
|
|
13
|
+
import { drawPreviewDraft, endPreviewDraft } from "./previewDrafts";
|
|
14
|
+
import { previewFrameOpaque } from "../../player/lib/previewChannel";
|
|
12
15
|
|
|
13
16
|
type Corners = ReturnType<typeof elementCornerOverlayPoints>;
|
|
14
17
|
|
|
@@ -80,6 +83,40 @@ function resolveAnchoredResizeDraft(
|
|
|
80
83
|
);
|
|
81
84
|
}
|
|
82
85
|
|
|
86
|
+
/** A frame Studio cannot measure: the box grows about its centre by the size the draft wrote. */
|
|
87
|
+
function resolveUnmeasuredResizeDraft(
|
|
88
|
+
g: GestureState,
|
|
89
|
+
element: HTMLElement,
|
|
90
|
+
{ written }: ResizeDraftSizes,
|
|
91
|
+
): OverlayRect {
|
|
92
|
+
const width = (g.originWidth * written.width) / g.actualWidth;
|
|
93
|
+
const height = (g.originHeight * written.height) / g.actualHeight;
|
|
94
|
+
if (g.pathOffsetMember) {
|
|
95
|
+
const anchor = {
|
|
96
|
+
dx: ((g.actualWidth - written.width) / 2) * g.editScaleX,
|
|
97
|
+
dy: ((g.actualHeight - written.height) / 2) * g.editScaleY,
|
|
98
|
+
};
|
|
99
|
+
g.lastResizeAnchor = anchor;
|
|
100
|
+
applyManualOffsetDragDraft(g.pathOffsetMember, anchor.dx, anchor.dy);
|
|
101
|
+
}
|
|
102
|
+
drawPreviewDraft(element, { size: written });
|
|
103
|
+
return {
|
|
104
|
+
left: g.originLeft + (g.originWidth - width) / 2,
|
|
105
|
+
top: g.originTop + (g.originHeight - height) / 2,
|
|
106
|
+
width,
|
|
107
|
+
height,
|
|
108
|
+
editScaleX: g.editScaleX,
|
|
109
|
+
editScaleY: g.editScaleY,
|
|
110
|
+
angle: g.actualRotation,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Ends a settled resize that has no offset member; a frame it drew on keeps the new size. */
|
|
115
|
+
export function endResizeWithoutMember(element: HTMLElement, token: string | undefined): void {
|
|
116
|
+
if (!token || isStudioManualEditGestureCurrent(element, token)) endPreviewDraft(element, "keep");
|
|
117
|
+
endStudioManualEditGesture(element, token);
|
|
118
|
+
}
|
|
119
|
+
|
|
83
120
|
/** The overlay rect to paint for the current resize pointer-move frame. */
|
|
84
121
|
export function resolveResizeDraftRect(
|
|
85
122
|
g: GestureState,
|
|
@@ -89,6 +126,7 @@ export function resolveResizeDraftRect(
|
|
|
89
126
|
measureOrientedRect: () => OverlayRect | null,
|
|
90
127
|
sizes: ResizeDraftSizes,
|
|
91
128
|
): OverlayRect {
|
|
129
|
+
if (iframe && previewFrameOpaque(iframe)) return resolveUnmeasuredResizeDraft(g, element, sizes);
|
|
92
130
|
if (g.pathOffsetMember) {
|
|
93
131
|
return resolveAnchoredResizeDraft(
|
|
94
132
|
g,
|
|
@@ -45,7 +45,7 @@ import {
|
|
|
45
45
|
resolveDomEditRotationGesture,
|
|
46
46
|
} from "./domEditOverlayGestures";
|
|
47
47
|
import { resolveCenterResizeSize } from "./domEditResizeLocal";
|
|
48
|
-
import { resolveResizeDraftRect } from "./resizeDraft";
|
|
48
|
+
import { endResizeWithoutMember, resolveResizeDraftRect } from "./resizeDraft";
|
|
49
49
|
import { notifyBlockedPress } from "./domEditOverlayStartGesture";
|
|
50
50
|
import { hugRectForElement } from "./domEditOverlayCrop";
|
|
51
51
|
import {
|
|
@@ -530,7 +530,7 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
|
|
|
530
530
|
})
|
|
531
531
|
.finally(() => {
|
|
532
532
|
if (member) endManualOffsetDragMembers([member]);
|
|
533
|
-
else
|
|
533
|
+
else endResizeWithoutMember(sel.element, g.manualEditDragToken);
|
|
534
534
|
});
|
|
535
535
|
edit.settle(saved);
|
|
536
536
|
logResizeSettle(sel.element, "post-release");
|