From ff01d81be1691c8b97803c6d49d0109367928d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 07:17:09 +0000 Subject: [PATCH] fix(studio): a sealed canvas pick selects the timeline clip it belongs to (#8459) * test(studio): a sealed pick of a clip's child selects the clip, as a same-origin one does * fix(studio): a sealed canvas pick selects the timeline clip it belongs to * fix(studio): a frame ancestor with no id is skipped when finding a sealed pick's clip --- .../editor/previewSelection.test.ts | 44 +++++++++++++++++++ .../src/components/editor/previewSelection.ts | 17 +++++-- packages/studio/src/utils/studioHelpers.ts | 17 +++---- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/packages/studio/src/components/editor/previewSelection.test.ts b/packages/studio/src/components/editor/previewSelection.test.ts index 2c3d7b8e73..6f4899a24d 100644 --- a/packages/studio/src/components/editor/previewSelection.test.ts +++ b/packages/studio/src/components/editor/previewSelection.test.ts @@ -29,6 +29,8 @@ import { installReactActEnvironment } from "../../hooks/domSelectionTestHarness" import { useDomSelection } from "../../hooks/useDomSelection"; import type { TimelineElement } from "../../player"; import { sealedPreviewFrame } from "../../player/lib/sealedPreviewFrame.test-helpers"; +import { resolveTimelineIdForSelection } from "../../utils/studioHelpers"; +import { announceTimelineSelection } from "../../hooks/domSelectionTimelineMirror"; installReactActEnvironment(); @@ -234,6 +236,48 @@ describe("opaque frame selections match same-origin ones", () => { }); }); +describe("the timeline clip an opaque frame selection belongs to", () => { + // fails without the fix: a sealed pick's stand-in has no parents, so a clip's child found no clip and copy, cut and + // duplicate acted on the element instead of the clip a same-origin pick selects. + it("is the clip a same-origin pick of the same child belongs to", async () => { + document.body.innerHTML = ` +
+
+
`; + const { live, remote } = await bothPaths(document.querySelector("#shape")!); + const clips = [ + { + id: "zone", + domId: "zone", + key: "index.html#zone", + start: 0, + duration: 6, + track: 0, + tag: "div", + }, + ] as TimelineElement[]; + const announced = (selection: DomEditSelection) => { + const setSelectedTimelineElementId = vi.fn(); + announceTimelineSelection( + { + timelineElements: clips, + activeCompPath: "index.html", + getTimelineSelectionSet: () => new Set(), + setSelectedTimelineElementId, + setTimelineSelectionSet: () => {}, + }, + [selection], + selection, + ); + return setSelectedTimelineElementId.mock.calls.at(-1)?.[0] ?? null; + }; + + expect(announced(live!)).toBe("index.html#zone"); + expect(announced(remote!)).toBe("index.html#zone"); + expect(resolveTimelineIdForSelection(remote!, clips, "index.html")).toBe("index.html#zone"); + }); +}); + describe("opaque frame selection handles", () => { it("gives a selection an inert, tag-only stand-in, the same one for a handle", async () => { document.body.innerHTML = FIXTURE; diff --git a/packages/studio/src/components/editor/previewSelection.ts b/packages/studio/src/components/editor/previewSelection.ts index 7d603b54a6..2a532ee27b 100644 --- a/packages/studio/src/components/editor/previewSelection.ts +++ b/packages/studio/src/components/editor/previewSelection.ts @@ -121,7 +121,7 @@ function takeFrameValuesForSavedStyles(standIn: HTMLElement, frameInline: Record savedOnStandIn.delete(standIn); } const standInHandles = new WeakMap(); -const standInAncestors = new WeakMap(); +const standInAncestors = new WeakMap>(); export function standInFor(iframe: HTMLIFrameElement, element: PreviewElement): HTMLElement { const client = previewEditingClient(iframe); @@ -365,7 +365,7 @@ export async function resolvePreviewSelection( const element = standInFor(iframe, current); standInAncestors.set( element, - chain.slice(index + 1).map((node) => node.handle), + chain.slice(index + 1).map(({ handle, id }) => ({ handle, id })), ); const identity = previewIdentity(current, options.activeCompositionPath); const compositionSrc = compositionSourceOf(current); @@ -486,6 +486,14 @@ export async function findOpaqueTimelineElement( export const standInHandle = (element: Element) => standInHandles.get(element); +export function ancestorIds(element: Element): string[] { + const described = standInAncestors.get(element); + if (described) return described.map((node) => node.id ?? ""); + const ids: string[] = []; + for (let node = element.parentElement; node; node = node.parentElement) ids.push(node.id); + return ids; +} + export function gestureDocumentOf(iframe: HTMLIFrameElement | null): Document | null { if (!iframe) return null; return previewFrameOpaque(iframe) ? inert() : iframe.contentDocument; @@ -494,7 +502,10 @@ export function gestureDocumentOf(iframe: HTMLIFrameElement | null): Document | export function elementWithin(container: HTMLElement, element: HTMLElement): boolean { const handle = standInHandles.get(container); if (handle === undefined) return container.contains(element); - return element === container || (standInAncestors.get(element)?.includes(handle) ?? false); + return ( + element === container || + (standInAncestors.get(element)?.some((node) => node.handle === handle) ?? false) + ); } /** A `data-` attribute of the selection's element; a stand-in's comes from its description. */ diff --git a/packages/studio/src/utils/studioHelpers.ts b/packages/studio/src/utils/studioHelpers.ts index 2893fdd8b5..0afd8c8608 100644 --- a/packages/studio/src/utils/studioHelpers.ts +++ b/packages/studio/src/utils/studioHelpers.ts @@ -2,6 +2,7 @@ import { buildProjectApiPath } from "./projectRouting"; import { isTypingTarget } from "@hyperframes/core/typing-target"; import type { TimelineElement } from "../player/store/playerStore"; import type { DomEditSelection } from "../components/editor/domEditing"; +import { ancestorIds } from "../components/editor/previewSelection"; import type { TimelineAssetKind } from "./timelineAssetDrop"; import { roundToCenti } from "./rounding"; import { studioApiFetch } from "./studioApiFetch"; @@ -263,16 +264,12 @@ export function findTimelineIdByAncestor( elements: TimelineElement[], sourceFile: string, ): string | null { - let ancestor = element?.parentElement ?? null; - while (ancestor) { - const id = ancestor.id; - if (id) { - const match = elements.find( - (el) => el.domId === id && (el.sourceFile ?? "index.html") === sourceFile, - ); - if (match) return match.key ?? match.id; - } - ancestor = ancestor.parentElement; + for (const id of element ? ancestorIds(element) : []) { + if (!id) continue; + const match = elements.find( + (el) => el.domId === id && (el.sourceFile ?? "index.html") === sourceFile, + ); + if (match) return match.key ?? match.id; } return null; }