@hyperframes/studio 0.9.39 → 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.
Files changed (57) hide show
  1. package/dist/assets/{codec-CY6KnWXa.js → codec-B_mpbc4w.js} +1 -1
  2. package/dist/assets/{hyperframes-player-B9-CuG_h.js → hyperframes-player-Dvi3I_mv.js} +1 -1
  3. package/dist/assets/{index-_pkzoYYF.js → index-B5ExwsOp.js} +146 -146
  4. package/dist/assets/{index-Bjz2UaYv.js → index-BrFm9Y5k.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-Dr7nKUUu.js → pausedVideoDecoder-MHQ57yZ6.js} +1 -1
  6. package/dist/{chunk-NBULUIWH.js → chunk-4QQMX4BX.js} +17 -8
  7. package/dist/{chunk-ATDO3YLX.js → chunk-GVW7AU57.js} +190 -254
  8. package/dist/{chunk-44VRMER6.js → chunk-VHV2ZDKY.js} +17 -97
  9. package/dist/{domEditingLayers-FRVHP6IB.js → domEditingLayers-SA5KLSSS.js} +4 -2
  10. package/dist/index.d.ts +4 -0
  11. package/dist/index.html +1 -1
  12. package/dist/index.js +898 -551
  13. package/dist/player.js +2 -2
  14. package/package.json +8 -8
  15. package/src/components/editor/DomEditOverlay.tsx +3 -15
  16. package/src/components/editor/clickAfterPreventedPress.test.tsx +17 -1
  17. package/src/components/editor/domEditOverlayBasis.ts +7 -2
  18. package/src/components/editor/domEditOverlayCrop.test.ts +1 -1
  19. package/src/components/editor/domEditOverlayCrop.ts +17 -3
  20. package/src/components/editor/domEditOverlayGeometry.ts +64 -139
  21. package/src/components/editor/domEditSelectionRules.ts +138 -0
  22. package/src/components/editor/domEditing.test.ts +6 -28
  23. package/src/components/editor/domEditingDom.preview.test.ts +3 -3
  24. package/src/components/editor/domEditingDom.ts +24 -165
  25. package/src/components/editor/domEditingElement.ts +19 -240
  26. package/src/components/editor/domEditingGroups.ts +0 -31
  27. package/src/components/editor/domEditingLayers.ts +95 -100
  28. package/src/components/editor/domEditingRootLayer.ts +35 -30
  29. package/src/components/editor/domEditingTypes.ts +4 -0
  30. package/src/components/editor/previewGestureStarts.ts +73 -10
  31. package/src/components/editor/previewOverlayRects.test.ts +316 -0
  32. package/src/components/editor/previewOverlayRects.ts +123 -0
  33. package/src/components/editor/previewPointerTargets.test.ts +260 -0
  34. package/src/components/editor/previewSelection.test.ts +481 -0
  35. package/src/components/editor/previewSelection.ts +472 -0
  36. package/src/components/editor/probeSourceElement.ts +19 -1
  37. package/src/components/editor/propertyPanelFill.tsx +5 -1
  38. package/src/components/editor/useDomEditOverlayRects.ts +80 -1
  39. package/src/components/editor/useInlineTextEditing.tsx +1 -6
  40. package/src/hooks/domSelectionTestHarness.ts +1 -1
  41. package/src/hooks/useDomEditSession.test.tsx +23 -1
  42. package/src/hooks/useDomEditSession.ts +2 -1
  43. package/src/hooks/useDomSelection.ts +61 -47
  44. package/src/hooks/useGsapSelectionHandlers.test.tsx +17 -0
  45. package/src/hooks/useGsapSelectionHandlers.ts +2 -1
  46. package/src/hooks/usePreviewInteraction.test.ts +10 -2
  47. package/src/hooks/usePreviewInteraction.ts +5 -4
  48. package/src/hooks/useStudioSelectionPublisher.test.tsx +50 -0
  49. package/src/hooks/useStudioSelectionPublisher.ts +9 -7
  50. package/src/player/lib/previewChannel.ts +26 -10
  51. package/src/player/lib/previewElements.test.ts +167 -0
  52. package/src/player/lib/previewElements.ts +226 -0
  53. package/src/styles/hex-baseline.json +1 -2
  54. package/src/utils/studioHelpers.ts +0 -13
  55. package/src/utils/studioPreviewHelpers.test.ts +8 -8
  56. package/src/utils/studioPreviewHelpers.ts +26 -242
  57. package/src/components/editor/domEditOverlayTransform.ts +0 -126
@@ -42,7 +42,7 @@ it("keeps a provider-backed video selectable and listed with its logical opacity
42
42
  const { video, canvas, root } = preview("0.65");
43
43
  expect(getComputedStyle(video).opacity).toBe("0");
44
44
  expect(isElementVisibleThroughAncestors(video)).toBe(true);
45
- expect(resolveVisualDomEditSelectionTarget([canvas, video], options)).toBe(video);
45
+ expect(resolveVisualDomEditSelectionTarget([canvas, video])).toBe(video);
46
46
  expect(collectDomEditLayerItems(root, options).map((item) => item.element)).toEqual([video]);
47
47
  const selected = await resolveDomEditSelection(video, options);
48
48
  expect(selected?.computedStyles.opacity).toBe("0.65");
@@ -53,7 +53,7 @@ it("keeps a provider-backed video selectable and listed with its logical opacity
53
53
  it("keeps authored zero opacity invisible and reports zero to the inspector", async () => {
54
54
  const { video, canvas } = preview("0");
55
55
  expect(isElementVisibleThroughAncestors(video)).toBe(false);
56
- expect(resolveVisualDomEditSelectionTarget([canvas, video], options)).toBeNull();
56
+ expect(resolveVisualDomEditSelectionTarget([canvas, video])).toBeNull();
57
57
  expect(getCuratedComputedStyles(video).opacity).toBe("0");
58
58
  const selected = await resolveDomEditSelection(video, options);
59
59
  expect(selected?.computedStyles.opacity).toBe("0");
@@ -64,7 +64,7 @@ it("still respects a hidden ancestor when the preview frame itself is visible",
64
64
  const { video, root } = preview("0.65");
65
65
  root.style.opacity = "0";
66
66
  expect(isElementVisibleThroughAncestors(video)).toBe(false);
67
- expect(resolveVisualDomEditSelectionTarget([video], options)).toBeNull();
67
+ expect(resolveVisualDomEditSelectionTarget([video])).toBeNull();
68
68
  });
69
69
 
70
70
  it("does not treat an unrelated preceding canvas as the video's visible frame", () => {
@@ -4,7 +4,14 @@
4
4
  * No imports from other domEditing* modules — safe to import from anywhere.
5
5
  */
6
6
  import { getLogicalOpacity } from "@hyperframes/core/studio-preview-mark";
7
- import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading";
7
+ import {
8
+ buildStableSelector,
9
+ escapeCssString,
10
+ getPreferredClassSelector,
11
+ isElementVisibleThroughAncestors,
12
+ readElementSource,
13
+ type EditingElementSource,
14
+ } from "@hyperframes/core/runtime/editing-port";
8
15
  import { getSourceScopedSelectorIndex } from "../../utils/sourceScopedSelectorIndex";
9
16
  import { CURATED_STYLE_PROPERTIES } from "./domEditingTypes";
10
17
 
@@ -36,65 +43,10 @@ export function isEditableTextLeaf(el: HTMLElement): boolean {
36
43
  }
37
44
 
38
45
  export { getLogicalOpacity };
39
-
40
- /** Does this node render AT ALL, ignoring what it inherits? Sole owner of the
41
- * rule; the walk below only decides which nodes to ask it about. */
42
- function elementRendersItself(win: Window, el: HTMLElement): boolean {
43
- const computed = win.getComputedStyle(el);
44
- if (computed.display === "none" || computed.visibility === "hidden") return false;
45
- const opacity = Number.parseFloat(getLogicalOpacity(el, computed));
46
- return !(
47
- Number.isFinite(opacity) &&
48
- opacity <= 0.01 &&
49
- !el.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)
50
- );
51
- }
52
-
53
- /**
54
- * Does `el` render, given everything above it?
55
- *
56
- * `memo` is for a caller asking this about MANY elements in one synchronous
57
- * pass. Answers are a function of the node and its ancestors, and siblings
58
- * share almost all of their chain, so memoizing per node turns a walk per
59
- * element into one style read per node in the tree. It is only ever valid for
60
- * the length of one pass — the DOM cannot change under a pass, and every read
61
- * here is a read — so the caller creates it and drops it, and nothing survives
62
- * to be invalidated. Omitted, every call walks the chain itself.
63
- *
64
- * The ANSWER is what it always was. Which nodes get a style read is not: this
65
- * resolves top-down and stops at the first node that is out, where the previous
66
- * version resolved bottom-up and stopped at the first one that is out going the
67
- * other way. Same boolean for every input, a different (and, for a subtree
68
- * hidden near the root, smaller) set of reads.
69
- */
70
- export function isElementVisibleThroughAncestors(
71
- el: HTMLElement,
72
- memo?: Map<HTMLElement, boolean>,
73
- ): boolean {
74
- const win = el.ownerDocument.defaultView;
75
- if (!win) return true;
76
- // Up to the first node already answered for, then back down: a node's answer
77
- // needs its ancestors' first, and the topmost unanswered node is where the
78
- // chain of unknowns starts.
79
- const pending: HTMLElement[] = [];
80
- let inherited = true;
81
- for (let node: HTMLElement | null = el; node; node = node.parentElement) {
82
- const answered = memo?.get(node);
83
- if (answered !== undefined) {
84
- inherited = answered;
85
- break;
86
- }
87
- pending.push(node);
88
- }
89
- for (let i = pending.length - 1; i >= 0; i -= 1) {
90
- const node = pending[i]!;
91
- // Once an ancestor is out, its descendants are out with it, and asking the
92
- // platform about them would be a style read for an answer already known.
93
- inherited = inherited && elementRendersItself(win, node);
94
- memo?.set(node, inherited);
95
- }
96
- return inherited;
97
- }
46
+ // One addressing rule for Studio and the runtime that describes elements to it.
47
+ export { buildStableSelector, escapeCssString };
48
+ // One visibility rule for Studio and the runtime that measures overlay geometry for it.
49
+ export { isElementVisibleThroughAncestors };
98
50
 
99
51
  // ─── Style accessors ──────────────────────────────────────────────────────────
100
52
 
@@ -131,22 +83,6 @@ export function getDataAttributes(el: HTMLElement): Record<string, string> {
131
83
  return attrs;
132
84
  }
133
85
 
134
- // ─── DOM traversal ────────────────────────────────────────────────────────────
135
-
136
- export function findClosestByAttribute(
137
- el: HTMLElement,
138
- attributeNames: string[],
139
- ): HTMLElement | null {
140
- let current: HTMLElement | null = el;
141
- while (current) {
142
- const candidate = current;
143
- if (attributeNames.some((attribute) => candidate.hasAttribute(attribute))) {
144
- return candidate;
145
- }
146
- current = current.parentElement;
147
- }
148
- return null;
149
- }
150
86
  // ─── Composition source resolution ───────────────────────────────────────────
151
87
 
152
88
  // The runtime INLINES subcompositions and strips the source-file linkage from the
@@ -171,33 +107,24 @@ export function getCompositionSourceMapRevision(): number {
171
107
  return compositionSourceMapRevision;
172
108
  }
173
109
 
174
- function sourceFromCompositionId(ownerRoot: HTMLElement | null): string | undefined {
175
- if (!ownerRoot || compositionSourceMap.size === 0) return undefined;
176
- // The runtime may rename the mounted id to a runtime-unique one, preserving the
177
- // authored id on `data-hf-original-composition-id` — prefer that, then the current id.
178
- const authored = ownerRoot.getAttribute("data-hf-original-composition-id");
179
- const current = ownerRoot.getAttribute("data-composition-id");
180
- return (
181
- (authored ? compositionSourceMap.get(authored) : undefined) ??
182
- (current ? compositionSourceMap.get(current) : undefined)
183
- );
110
+ /** The source file `source` names (see `EditingElementSource`), with this project's composition map. */
111
+ export function resolveElementSourceFile(
112
+ source: EditingElementSource,
113
+ activeCompositionPath: string | null,
114
+ ): string {
115
+ const { file, compositionId, originalCompositionId } = source;
116
+ const mapped = compositionSourceMap.size
117
+ ? ((originalCompositionId ? compositionSourceMap.get(originalCompositionId) : undefined) ??
118
+ (compositionId ? compositionSourceMap.get(compositionId) : undefined))
119
+ : undefined;
120
+ return file ?? mapped ?? activeCompositionPath ?? "index.html";
184
121
  }
185
122
 
186
123
  export function getSourceFileForElement(
187
124
  el: HTMLElement,
188
125
  activeCompositionPath: string | null,
189
126
  ): { sourceFile: string; compositionPath: string } {
190
- const sourceHost = findClosestByAttribute(el, ["data-composition-file", "data-composition-src"]);
191
- const ownerRoot = findClosestByAttribute(el, ["data-composition-id"]);
192
- const sourceFile =
193
- sourceHost?.getAttribute("data-composition-file") ??
194
- sourceHost?.getAttribute("data-composition-src") ??
195
- ownerRoot?.getAttribute("data-composition-file") ??
196
- ownerRoot?.getAttribute("data-composition-src") ??
197
- sourceFromCompositionId(ownerRoot) ??
198
- activeCompositionPath ??
199
- "index.html";
200
-
127
+ const sourceFile = resolveElementSourceFile(readElementSource(el), activeCompositionPath);
201
128
  return {
202
129
  sourceFile,
203
130
  compositionPath: sourceFile,
@@ -228,49 +155,6 @@ export function normalizeTimelineCompositionSource(value: string | undefined): s
228
155
 
229
156
  // ─── CSS escaping ─────────────────────────────────────────────────────────────
230
157
 
231
- function escapeCssIdentifier(value: string): string {
232
- const css = globalThis.CSS as { escape?: (input: string) => string } | undefined;
233
- if (typeof css?.escape === "function") return css.escape(value);
234
-
235
- if (value === "-") return "\\-";
236
-
237
- let escaped = "";
238
- for (let index = 0; index < value.length; index += 1) {
239
- const char = value[index] ?? "";
240
- const code = char.charCodeAt(0);
241
- if (code === 0) {
242
- escaped += "�";
243
- continue;
244
- }
245
-
246
- const isDigit = code >= 48 && code <= 57;
247
- const isUpperAlpha = code >= 65 && code <= 90;
248
- const isLowerAlpha = code >= 97 && code <= 122;
249
- const isControl = (code >= 1 && code <= 31) || code === 127;
250
- const isLeadingDigit = index === 0 && isDigit;
251
- const isSecondDigitAfterDash = index === 1 && value.startsWith("-") && isDigit;
252
- if (isControl || isLeadingDigit || isSecondDigitAfterDash) {
253
- escaped += `\\${code.toString(16)} `;
254
- continue;
255
- }
256
- if (isUpperAlpha || isLowerAlpha || isDigit || char === "-" || char === "_" || code >= 128) {
257
- escaped += char;
258
- continue;
259
- }
260
- escaped += `\\${char}`;
261
- }
262
- return escaped;
263
- }
264
-
265
- export function escapeCssString(value: string): string {
266
- return value
267
- .replace(/\\/g, "\\\\")
268
- .replace(/"/g, '\\"')
269
- .replace(/\n/g, "\\a ")
270
- .replace(/\r/g, "\\d ")
271
- .replace(/\f/g, "\\c ");
272
- }
273
-
274
158
  export function querySelectorAllSafely(doc: Document, selector: string): Element[] {
275
159
  try {
276
160
  return Array.from(doc.querySelectorAll(selector));
@@ -293,31 +177,6 @@ function humanizeIdentifier(value: string): string {
293
177
 
294
178
  // ─── CSS selector building ────────────────────────────────────────────────────
295
179
 
296
- export function buildStableSelector(el: HTMLElement): string | undefined {
297
- if (el.id) return `#${escapeCssIdentifier(el.id)}`;
298
-
299
- const compositionId = el.getAttribute("data-composition-id");
300
- if (compositionId) return `[data-composition-id="${escapeCssString(compositionId)}"]`;
301
-
302
- // Group wrappers carry no id/class; their data-hf-group value is the unique,
303
- // stable handle the source mutations write — use it so the wrapper is
304
- // selectable, patchable (move/scale), and addressable for ungroup.
305
- const group = el.getAttribute("data-hf-group");
306
- if (group) return `[data-hf-group="${escapeCssString(group)}"]`;
307
-
308
- return getPreferredClassSelector(el);
309
- }
310
-
311
- function getPreferredClassSelector(el: HTMLElement): string | undefined {
312
- const classes = Array.from(el.classList)
313
- .map((value) => value.trim())
314
- .filter(Boolean);
315
- if (classes.length === 0) return undefined;
316
- const preferred =
317
- classes.find((value) => value !== "clip" && !value.startsWith("__hf-")) ?? classes[0];
318
- return preferred ? `.${escapeCssIdentifier(preferred)}` : undefined;
319
- }
320
-
321
180
  // fallow-ignore-next-line complexity
322
181
  export function buildElementLabel(el: HTMLElement): string {
323
182
  const compositionId =
@@ -3,22 +3,28 @@
3
3
  * and the `findElementForSelection` / `findElementForTimelineElement` lookups.
4
4
  */
5
5
  import type {
6
- DomEditContextOptions,
7
6
  DomEditSelection,
8
7
  DomEditViewport,
9
8
  TimelineElementDomTarget,
10
9
  TimelineElementDomTargetOptions,
11
10
  } from "./domEditingTypes";
12
11
  import {
13
- buildStableSelector,
14
- escapeCssString,
15
12
  getSelectorIndex,
16
13
  getSourceFileForElement,
17
14
  isHtmlElement,
18
15
  isElementVisibleThroughAncestors,
19
- normalizeTimelineCompositionSource,
20
16
  querySelectorAllSafely,
21
17
  } from "./domEditingDom";
18
+ import { selectionLookups, timelineElementLookup } from "./domEditSelectionRules";
19
+ import {
20
+ isDomLayerElement,
21
+ isInspectableLayerElement,
22
+ resolveAllVisualDomEditTargets,
23
+ resolveLayerSelector,
24
+ } from "@hyperframes/core/runtime/editing-port";
25
+
26
+ // One hit-test rule for Studio and the runtime that answers it for a frame Studio cannot script.
27
+ export { isDomLayerElement, isInspectableLayerElement };
22
28
 
23
29
  // ─── Visibility ──────────────────────────────────────────────────────────────
24
30
 
@@ -29,87 +35,10 @@ export function isElementComputedVisible(
29
35
  return isElementVisibleThroughAncestors(el, memo);
30
36
  }
31
37
 
32
- const VISUAL_LEAF_TAGS = new Set(["img", "video", "canvas", "svg", "audio"]);
33
-
34
- // fallow-ignore-next-line complexity
35
- function hasVisualPresence(el: HTMLElement): boolean {
36
- const win = el.ownerDocument.defaultView;
37
- if (!win) return false;
38
- const cs = win.getComputedStyle(el);
39
- if (cs.backgroundImage !== "none") return true;
40
- if (
41
- cs.backgroundColor &&
42
- cs.backgroundColor !== "transparent" &&
43
- cs.backgroundColor !== "rgba(0, 0, 0, 0)"
44
- )
45
- return true;
46
- if (cs.borderWidth && parseFloat(cs.borderWidth) > 0 && cs.borderStyle !== "none") return true;
47
- if (cs.boxShadow && cs.boxShadow !== "none") return true;
48
- return false;
49
- }
50
-
51
- function isEmptyVisualContainer(el: HTMLElement): boolean {
52
- const tag = el.tagName.toLowerCase();
53
- if (VISUAL_LEAF_TAGS.has(tag)) return false;
54
- if (hasVisualPresence(el)) return false;
55
-
56
- const { children } = el;
57
- if (children.length === 0) {
58
- return (el.textContent ?? "").trim().length === 0;
59
- }
60
-
61
- for (let i = 0; i < children.length; i += 1) {
62
- const child = children[i];
63
- if (!isHtmlElement(child)) continue;
64
- if (VISUAL_LEAF_TAGS.has(child.tagName.toLowerCase())) return false;
65
- if (isElementComputedVisible(child)) return false;
66
- }
67
-
68
- return true;
69
- }
70
-
71
- function hasRenderedBox(el: HTMLElement): boolean {
72
- const rect = el.getBoundingClientRect();
73
- if (rect.width <= 1 || rect.height <= 1) return false;
74
- if (!isElementComputedVisible(el)) return false;
75
- if (isEmptyVisualContainer(el)) return false;
76
- return true;
77
- }
78
-
79
38
  // ─── Visual scoring ──────────────────────────────────────────────────────────
80
39
 
81
40
  // ─── Layer patch target ──────────────────────────────────────────────────────
82
41
 
83
- const DOM_LAYER_IGNORED_TAGS = new Set([
84
- "base",
85
- "br",
86
- "canvas",
87
- "link",
88
- "meta",
89
- "script",
90
- "source",
91
- "style",
92
- "template",
93
- "track",
94
- "wbr",
95
- ]);
96
-
97
- /**
98
- * Is the element rendered at all? The half of `getDomLayerPatchTarget` that a
99
- * plain style write can flip, and the only half that reads computed style.
100
- * Exported so a caller memoizing a layer walk can give it its own (short)
101
- * cache lifetime — see `domEditLayerWalkCache`.
102
- */
103
- export function isInspectableLayerElement(el: HTMLElement): boolean {
104
- const tagName = el.tagName.toLowerCase();
105
- if (DOM_LAYER_IGNORED_TAGS.has(tagName)) return false;
106
-
107
- const computed = el.ownerDocument.defaultView?.getComputedStyle(el);
108
- if (computed?.display === "none" || computed?.visibility === "hidden") return false;
109
-
110
- return true;
111
- }
112
-
113
42
  /**
114
43
  * How the element is ADDRESSED — its selector, that selector's occurrence index
115
44
  * within the source file, and the file itself. Independent of whether the
@@ -117,17 +46,6 @@ export function isInspectableLayerElement(el: HTMLElement): boolean {
117
46
  * when an identity attribute changes or the document's population of elements
118
47
  * sharing the selector does.
119
48
  */
120
- /**
121
- * The selector an element is addressed by, or undefined when it cannot be a
122
- * layer at all. Sole owner of that rule: `isDomLayerElement` answers the same
123
- * question without paying for the occurrence index, and a second copy of the
124
- * conditions would go stale the first time one of them changed.
125
- */
126
- function resolveLayerSelector(el: HTMLElement): string | undefined {
127
- if (el.hasAttribute("data-composition-id")) return undefined;
128
- return buildStableSelector(el);
129
- }
130
-
131
49
  export function resolveDomLayerIdentity(
132
50
  el: HTMLElement,
133
51
  activeCompositionPath: string | null,
@@ -151,105 +69,12 @@ export function resolveDomLayerIdentity(
151
69
  };
152
70
  }
153
71
 
154
- export function getDomLayerPatchTarget(
155
- el: HTMLElement,
156
- activeCompositionPath: string | null,
157
- ): Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex" | "sourceFile"> | null {
158
- return isInspectableLayerElement(el) ? resolveDomLayerIdentity(el, activeCompositionPath) : null;
159
- }
160
-
161
- /**
162
- * Is `el` a layer at all? Exactly the condition under which
163
- * `getDomLayerPatchTarget` returns non-null, for callers that want the yes/no
164
- * and throw the target away.
165
- *
166
- * Worth its own function because the target carries the selector's OCCURRENCE
167
- * INDEX, and resolving that is a whole-document query — which the yes/no does
168
- * not depend on. Counting an element's layer children asked for one per child.
169
- */
170
- export function isDomLayerElement(el: HTMLElement): boolean {
171
- return isInspectableLayerElement(el) && resolveLayerSelector(el) !== undefined;
172
- }
173
-
174
- // ─── Clip ancestor / selection candidate ─────────────────────────────────────
175
-
176
- function getPreferredClipAncestor(startEl: HTMLElement): HTMLElement | null {
177
- let current: HTMLElement | null = startEl;
178
- while (current) {
179
- if (current.classList.contains("clip")) {
180
- const isCompositionHost =
181
- current.hasAttribute("data-composition-src") ||
182
- current.hasAttribute("data-composition-file");
183
- if (!isCompositionHost || current === startEl) return current;
184
- }
185
- current = current.parentElement;
186
- }
187
- return null;
188
- }
189
-
190
- export function getSelectionCandidate(
191
- startEl: HTMLElement,
192
- options: DomEditContextOptions,
193
- ): HTMLElement {
194
- if (options.preferClipAncestor) {
195
- const clipAncestor = getPreferredClipAncestor(startEl);
196
- if (clipAncestor) {
197
- return clipAncestor;
198
- }
199
- }
200
-
201
- return startEl;
202
- }
203
-
204
72
  // ─── Visual target resolution ─────────────────────────────────────────────────
205
73
 
206
74
  export function resolveVisualDomEditSelectionTarget(
207
75
  elementsFromPoint: Iterable<Element | null | undefined>,
208
- options: Pick<DomEditContextOptions, "activeCompositionPath">,
209
76
  ): HTMLElement | null {
210
- const candidates = resolveAllVisualDomEditTargets(elementsFromPoint, options);
211
- return candidates[0] ?? null;
212
- }
213
-
214
- /**
215
- * Returns all independently-selectable elements at the given point, in paint
216
- * order (topmost first). Used for click-cycling through stacked layers.
217
- *
218
- * Each entry in the returned array is an independent "layer" — an element
219
- * that is not an ancestor of an earlier entry. This gives one result per
220
- * z-stacked element rather than one per DOM node.
221
- */
222
- export function resolveAllVisualDomEditTargets(
223
- elementsFromPoint: Iterable<Element | null | undefined>,
224
- options: Pick<DomEditContextOptions, "activeCompositionPath">,
225
- ): HTMLElement[] {
226
- const raw: HTMLElement[] = [];
227
-
228
- for (const entry of elementsFromPoint) {
229
- if (!isHtmlElement(entry)) continue;
230
- if (hasRenderedBox(entry) && getDomLayerPatchTarget(entry, options.activeCompositionPath)) {
231
- raw.push(entry);
232
- }
233
- }
234
-
235
- if (raw.length === 0) return [];
236
-
237
- // First pass: for each contiguous ancestor-descendant run, keep only the
238
- // deepest (most specific) element, matching the original single-pick logic.
239
- const layers: HTMLElement[] = [];
240
- let best = raw[0];
241
- for (let i = 1; i < raw.length; i++) {
242
- const el = raw[i];
243
- if (best.contains(el)) {
244
- best = el; // go deeper in this subtree
245
- } else {
246
- layers.push(best);
247
- best = el;
248
- }
249
- }
250
- layers.push(best);
251
-
252
- return layers;
77
+ return resolveAllVisualDomEditTargets(elementsFromPoint)[0] ?? null;
253
78
  }
254
79
 
255
80
  // ─── Raster detection ────────────────────────────────────────────────────────
@@ -295,23 +120,11 @@ export function findElementForSelection(
295
120
  (!selection.sourceFile ||
296
121
  getSourceFileForElement(candidate, activeCompositionPath).sourceFile ===
297
122
  selection.sourceFile);
298
- const findAll = (selector: string): HTMLElement[] =>
299
- querySelectorAllSafely(doc, selector).filter(sourceMatches);
300
-
301
- if (selection.hfId) {
302
- const byHfId = findAll(`[data-hf-id="${escapeCssString(selection.hfId)}"]`)[0];
303
- if (byHfId) return byHfId;
123
+ for (const { selector, index } of selectionLookups(selection)) {
124
+ const found = querySelectorAllSafely(doc, selector).filter(sourceMatches)[index];
125
+ if (found) return found;
304
126
  }
305
-
306
- if (selection.id) {
307
- // Flattened sub-compositions can repeat authored ids. getElementById returns
308
- // only the first document match, so filter every id match by source first.
309
- const byId = findAll(`[id="${escapeCssString(selection.id)}"]`)[0];
310
- if (byId) return byId;
311
- }
312
-
313
- if (!selection.selector) return null;
314
- return findAll(selection.selector)[selection.selectorIndex ?? 0] ?? null;
127
+ return null;
315
128
  }
316
129
 
317
130
  // The selected node while mounted, else its copy in the selection's own file (ids repeat across files), else any copy.
@@ -327,49 +140,15 @@ export function findPreviewNode(
327
140
  );
328
141
  }
329
142
 
330
- // fallow-ignore-next-line complexity
331
143
  export function findElementForTimelineElement(
332
144
  doc: Document,
333
145
  element: TimelineElementDomTarget,
334
146
  options: TimelineElementDomTargetOptions,
335
147
  ): HTMLElement | null {
336
- const elementId = typeof element.id === "string" ? element.id : "";
337
- const compositionSource =
338
- normalizeTimelineCompositionSource(element.compositionSrc) ??
339
- options.compIdToSrc?.get(elementId);
340
- const sourceFile =
341
- compositionSource ??
342
- normalizeTimelineCompositionSource(element.sourceFile) ??
343
- options.activeCompositionPath ??
344
- "index.html";
345
- const escapedElementId = escapeCssString(elementId);
346
- const escapedCompositionSource = compositionSource ? escapeCssString(compositionSource) : null;
347
- const selector =
348
- element.selector ??
349
- (compositionSource
350
- ? `[data-composition-src="${escapedCompositionSource}"],[data-composition-file="${escapedCompositionSource}"],[data-composition-id="${escapedElementId}"]`
351
- : escapedElementId
352
- ? `[data-composition-id="${escapedElementId}"]`
353
- : undefined);
354
-
355
- if (selector || element.domId) {
356
- const targetElement = findElementForSelection(
357
- doc,
358
- {
359
- id: element.domId ?? undefined,
360
- selector,
361
- selectorIndex: element.selectorIndex,
362
- sourceFile,
363
- },
364
- options.activeCompositionPath,
365
- );
366
- if (targetElement) return targetElement;
367
- }
368
-
369
- const hasExplicitDomTarget = Boolean(element.domId || element.selector || compositionSource);
370
- if (options.isMasterView || hasExplicitDomTarget || !options.activeCompositionPath) {
371
- return null;
372
- }
148
+ const { target, sourceFile, rootFallback } = timelineElementLookup(element, options);
149
+ const found = target && findElementForSelection(doc, target, options.activeCompositionPath);
150
+ if (found) return found;
151
+ if (!rootFallback) return null;
373
152
 
374
153
  const root = doc.querySelector("[data-composition-id]");
375
154
  if (!isHtmlElement(root)) return null;
@@ -1,15 +1,5 @@
1
1
  import { isHtmlElement } from "./domEditingDom";
2
2
 
3
- // `data-hf-group` selection semantics: a group wrapper is selected as one unit
4
- // until the user drills into it; once drilled in, clicks resolve to its children
5
- // (or to the next nested group inside it). One level of drill-in at a time keeps
6
- // nested groups navigable.
7
-
8
- export type GroupCapture =
9
- | { kind: "unit"; element: HTMLElement } // select this group wrapper as one unit
10
- | { kind: "child" } // resolve the clicked element normally
11
- | { kind: "out-of-scope" }; // clicked outside the drilled-into group → select nothing
12
-
13
3
  // Layer-tree roots: the drilled-into group's element children, else the doc root.
14
4
  export function groupScopedLayerRoots(
15
5
  root: HTMLElement,
@@ -18,24 +8,3 @@ export function groupScopedLayerRoots(
18
8
  const els = activeGroupElement?.isConnected ? Array.from(activeGroupElement.children) : [root];
19
9
  return els.filter(isHtmlElement);
20
10
  }
21
-
22
- export function resolveGroupCapture(
23
- startEl: HTMLElement,
24
- activeGroupElement: HTMLElement | null,
25
- ): GroupCapture {
26
- const groups: HTMLElement[] = [];
27
- for (let n: HTMLElement | null = startEl; n; n = n.parentElement) {
28
- if (n.hasAttribute("data-hf-group")) groups.push(n);
29
- }
30
- const result = ((): GroupCapture => {
31
- if (!activeGroupElement) {
32
- const outermost = groups[groups.length - 1];
33
- return outermost ? { kind: "unit", element: outermost } : { kind: "child" };
34
- }
35
- const idx = groups.indexOf(activeGroupElement);
36
- if (idx === -1) return { kind: "out-of-scope" };
37
- const nestedInside = groups[idx - 1];
38
- return nestedInside ? { kind: "unit", element: nestedInside } : { kind: "child" };
39
- })();
40
- return result;
41
- }