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
This commit is contained in:
Miguel Ángel
2026-11-22 07:17:09 +00:00
committed by GitHub
parent 34d5c4678d
commit ff01d81be1
3 changed files with 65 additions and 13 deletions
@@ -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 = `
<div data-composition-id="main" data-width="1920" data-height="1080">
<div class="clip" id="zone" data-start="0" data-duration="6"><div id="shape"></div></div>
</div>`;
const { live, remote } = await bothPaths(document.querySelector<HTMLElement>("#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;
@@ -121,7 +121,7 @@ function takeFrameValuesForSavedStyles(standIn: HTMLElement, frameInline: Record
savedOnStandIn.delete(standIn);
}
const standInHandles = new WeakMap<Element, string>();
const standInAncestors = new WeakMap<Element, string[]>();
const standInAncestors = new WeakMap<Element, Array<{ handle: string; id: string | null }>>();
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. */
+7 -10
View File
@@ -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;
}