mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-24 16:06:34 +00:00
fix(studio): stop a group selection from erasing itself on the timeline
Every canvas selection is mirrored onto the timeline, and the timeline syncs back — whatever it holds replaces the canvas selection a moment later. The mirror announced only the primary and anchored it with preserveSet, but preserving a set that does not contain the id empties the set, and an empty set syncs back as "nothing is selected". Adding a second element, or re-resolving a group after moving it, could therefore drop the whole selection rather than keep it. One helper now owns the mirror: publish the members, then anchor. A single selection keeps the previous contract deliberately, so a late async primary still cannot collapse a live group and a fresh click still collapses a stale one. The group re-resolve path also gains the ancestor id fallback the other callers already had — without it a member with no direct timeline row resolved to null and deselected everything. Two tests: a second element joining a selection, and a marquee, both assert the full set reaches the timeline. Both fail against the announce-the-primary-only version.
This commit is contained in:
@@ -145,6 +145,40 @@ describe("useDomSelection marquee", () => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Adding a second element announced only that element, with preserveSet — and
|
||||
* preserving a set that does not contain the id empties it. An empty timeline
|
||||
* selection syncs back as "nothing is selected", so growing a group could wipe
|
||||
* it instead, and so could re-resolving one after a move.
|
||||
*/
|
||||
describe("useDomSelection additive", () => {
|
||||
it("announces both members when a second element joins the selection", () => {
|
||||
const first = document.createElement("div");
|
||||
first.id = "card";
|
||||
const second = document.createElement("div");
|
||||
second.id = "chip";
|
||||
document.body.append(first, second);
|
||||
const harness = renderHarness(
|
||||
{ activeCompPath: "index.html", projectId: "project-1", refreshKey: 0 },
|
||||
{ timelineElements: [timelineElement("card"), timelineElement("chip")] },
|
||||
);
|
||||
|
||||
act(() => harness.current().applyDomSelection(makeSelection("Card", first)));
|
||||
act(() =>
|
||||
harness.current().applyDomSelection(makeSelection("Chip", second), { additive: true }),
|
||||
);
|
||||
|
||||
expect(harness.current().domEditGroupSelections).toHaveLength(2);
|
||||
expect(harness.timeline.setTimelineSelectionSet).toHaveBeenLastCalledWith(
|
||||
new Set(["card", "chip"]),
|
||||
);
|
||||
expect(harness.timeline.setSelectedTimelineElementId).toHaveBeenLastCalledWith("chip", {
|
||||
preserveSet: true,
|
||||
});
|
||||
harness.cleanup();
|
||||
});
|
||||
});
|
||||
|
||||
describe("useDomSelection", () => {
|
||||
it("clears a committed selection when the active composition path changes", () => {
|
||||
const { selection, harness } = setupSelectedHarness();
|
||||
|
||||
@@ -149,6 +149,47 @@ export function useDomSelection({
|
||||
|
||||
// ── Callbacks ──
|
||||
|
||||
/**
|
||||
* Mirror a canvas selection onto the timeline: the whole set first, then the
|
||||
* primary as its anchor.
|
||||
*
|
||||
* The timeline is the source of truth for what is selected and it syncs back —
|
||||
* whatever it holds replaces the canvas selection a moment later. Announcing only
|
||||
* the primary therefore drops every other member. Worse, anchoring with
|
||||
* `preserveSet` on an id the set does not yet contain empties the set outright,
|
||||
* and an empty set syncs back as "nothing is selected" — which is how adding a
|
||||
* second element, or moving a group, could wipe the selection instead of keeping
|
||||
* it. Publishing the members first is what makes the anchor a member, so
|
||||
* preserving the set is meaningful rather than destructive.
|
||||
*/
|
||||
const announceTimelineSelection = useCallback(
|
||||
(group: DomEditSelection[], primary: DomEditSelection | null) => {
|
||||
if (!primary) {
|
||||
setTimelineSelectionSet(new Set());
|
||||
setSelectedTimelineElementId(null);
|
||||
return;
|
||||
}
|
||||
const timelineIdFor = (selection: DomEditSelection) =>
|
||||
findMatchingTimelineElementId(selection, timelineElements) ??
|
||||
findTimelineIdByAncestor(
|
||||
selection.element,
|
||||
timelineElements,
|
||||
selection.sourceFile || "index.html",
|
||||
);
|
||||
// Only a real multi-selection publishes members. A single selection keeps the
|
||||
// older contract on purpose: anchoring with preserveSet holds a live set the
|
||||
// element already belongs to (a late async primary must not collapse a group)
|
||||
// and collapses otherwise, which is what a fresh click means.
|
||||
if (group.length > 1) {
|
||||
setTimelineSelectionSet(
|
||||
new Set(group.map(timelineIdFor).filter((id): id is string => Boolean(id))),
|
||||
);
|
||||
}
|
||||
setSelectedTimelineElementId(timelineIdFor(primary), { preserveSet: true });
|
||||
},
|
||||
[setSelectedTimelineElementId, setTimelineSelectionSet, timelineElements],
|
||||
);
|
||||
|
||||
const applyDomSelection = useCallback(
|
||||
// fallow-ignore-next-line complexity
|
||||
(
|
||||
@@ -160,11 +201,12 @@ export function useDomSelection({
|
||||
},
|
||||
) => {
|
||||
if (!selection) {
|
||||
logSelect("clear", { hadGroup: domEditGroupSelectionsRef.current.length });
|
||||
domEditSelectionRef.current = null;
|
||||
domEditGroupSelectionsRef.current = [];
|
||||
setDomEditSelection(null);
|
||||
setDomEditGroupSelections([]);
|
||||
setSelectedTimelineElementId(null);
|
||||
announceTimelineSelection([], null);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -219,21 +261,13 @@ export function useDomSelection({
|
||||
setRightPanelTab("design");
|
||||
}
|
||||
}
|
||||
const nextSelectedTimelineId =
|
||||
findMatchingTimelineElementId(nextSelection, timelineElements) ??
|
||||
findTimelineIdByAncestor(
|
||||
nextSelection.element,
|
||||
timelineElements,
|
||||
nextSelection.sourceFile || "index.html",
|
||||
);
|
||||
// Late marquee notify: a primary already in the live set must not collapse it.
|
||||
setSelectedTimelineElementId(nextSelectedTimelineId, { preserveSet: true });
|
||||
announceTimelineSelection(nextGroup, nextSelection);
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedTimelineElementId(null);
|
||||
announceTimelineSelection([], null);
|
||||
},
|
||||
[setSelectedTimelineElementId, timelineElements, setRightCollapsed, setRightPanelTab],
|
||||
[announceTimelineSelection, setRightCollapsed, setRightPanelTab],
|
||||
);
|
||||
|
||||
const clearDomSelection = useCallback(() => {
|
||||
@@ -447,21 +481,9 @@ export function useDomSelection({
|
||||
setDomEditSelection(nextSelection);
|
||||
setDomEditGroupSelections(nextGroup);
|
||||
|
||||
if (nextSelection) {
|
||||
setSelectedTimelineElementId(
|
||||
findMatchingTimelineElementId(nextSelection, timelineElements),
|
||||
);
|
||||
} else {
|
||||
setSelectedTimelineElementId(null);
|
||||
}
|
||||
announceTimelineSelection(nextGroup, nextSelection);
|
||||
},
|
||||
[
|
||||
activeCompPath,
|
||||
buildDomSelectionFromTarget,
|
||||
setSelectedTimelineElementId,
|
||||
timelineElements,
|
||||
previewIframeRef,
|
||||
],
|
||||
[activeCompPath, announceTimelineSelection, buildDomSelectionFromTarget, previewIframeRef],
|
||||
);
|
||||
|
||||
// ── Effects ──
|
||||
@@ -538,26 +560,9 @@ export function useDomSelection({
|
||||
domEditGroupSelectionsRef.current = nextGroup;
|
||||
setDomEditSelection(nextSelection);
|
||||
setDomEditGroupSelections(nextGroup);
|
||||
// Publish the WHOLE set, not just the primary. The timeline is the source of
|
||||
// truth for what is selected, and the sync back to the canvas replaces the
|
||||
// canvas selection with whatever the timeline holds. Announcing one member
|
||||
// therefore un-selects every other one the marquee just caught, a moment
|
||||
// after the drop — the group is built correctly and then collapses to one.
|
||||
// The anchor goes second, and preserves the set it is now a member of.
|
||||
const timelineIdFor = (selection: DomEditSelection) =>
|
||||
findMatchingTimelineElementId(selection, timelineElements) ??
|
||||
findTimelineIdByAncestor(
|
||||
selection.element,
|
||||
timelineElements,
|
||||
selection.sourceFile || "index.html",
|
||||
);
|
||||
const nextTimelineId = timelineIdFor(nextSelection);
|
||||
setTimelineSelectionSet(
|
||||
new Set(nextGroup.map(timelineIdFor).filter((id): id is string => Boolean(id))),
|
||||
);
|
||||
setSelectedTimelineElementId(nextTimelineId, { preserveSet: true });
|
||||
announceTimelineSelection(nextGroup, nextSelection);
|
||||
},
|
||||
[applyDomSelection, timelineElements, setSelectedTimelineElementId, setTimelineSelectionSet],
|
||||
[applyDomSelection, announceTimelineSelection],
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -49,6 +49,7 @@ interface HarnessProps {
|
||||
iframe: HTMLIFrameElement | null;
|
||||
timelineElements: TimelineElement[];
|
||||
setSelectedTimelineElementId?: (id: string | null, options?: SelectElementOptions) => void;
|
||||
setTimelineSelectionSet?: (ids: Set<string>) => void;
|
||||
}
|
||||
|
||||
function renderHarness(props: HarnessProps) {
|
||||
@@ -67,6 +68,8 @@ function renderHarness(props: HarnessProps) {
|
||||
previewIframeRef: { current: props.iframe },
|
||||
timelineElements: props.timelineElements,
|
||||
setSelectedTimelineElementId: props.setSelectedTimelineElementId ?? vi.fn(),
|
||||
setTimelineSelectionSet:
|
||||
props.setTimelineSelectionSet ?? usePlayerStore.getState().setSelectedElementIds,
|
||||
setRightCollapsed: vi.fn(),
|
||||
setRightPanelTab: props.setRightPanelTab,
|
||||
previewIframe: props.iframe,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useMemo, useRef } from "react";
|
||||
import type { TimelineElement } from "../player";
|
||||
import type { DomEditSelection } from "../components/editor/domEditing";
|
||||
import { resolveTimelineIdForSelection } from "../utils/studioHelpers";
|
||||
import { logSelect } from "../utils/selectDebug";
|
||||
|
||||
interface UseTimelineSelectionPreviewSyncParams {
|
||||
selectedElementId: string | null;
|
||||
@@ -127,6 +128,11 @@ export function useTimelineSelectionPreviewSync({
|
||||
return;
|
||||
}
|
||||
missingSelectionKeyRef.current = "";
|
||||
logSelect("timeline-sync", {
|
||||
wanted: selectedIds.length,
|
||||
had: currentIds.length,
|
||||
resolved: selections.length,
|
||||
});
|
||||
if (selections.length === 0) {
|
||||
applyDomSelection(null, { revealPanel: false });
|
||||
} else if (selections.length === 1) {
|
||||
|
||||
Reference in New Issue
Block a user