Skip to content

Commit bc1c77b

Browse files
committed
fix(studio): preserve canvas-only group anchors
1 parent c8e029e commit bc1c77b

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎packages/studio/src/hooks/domSelectionTimelineMirror.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ export function announceTimelineSelection(
4848
);
4949
const members = group.map(timelineIdFor).filter((id): id is string => Boolean(id));
5050
const anchor = timelineIdFor(primary);
51+
const publishedMembers = new Set(members);
52+
if (anchor) publishedMembers.add(anchor);
53+
const timelineAnchor = anchor ?? members[0] ?? null;
5154
// A member with no timeline row of its own resolves to null and is dropped here,
5255
// so a group can announce fewer ids than it has — or none, which reads back as an
5356
// empty selection and takes the canvas selection with it.
5457
logSelect("announce", {
5558
group: group.length,
56-
published: members.length,
59+
published: publishedMembers.size,
5760
anchor,
58-
anchorPublished: anchor != null && members.includes(anchor),
61+
anchorPublished: anchor != null && publishedMembers.has(anchor),
5962
});
63+
// A canvas target can be editable without owning a timeline row. Preserve that
64+
// canvas-only selection when the timeline has nothing truthful to represent.
65+
if (!timelineAnchor) return;
6066
// A late async primary that already belongs to the live set must preserve the
6167
// group. A fresh single click does not belong to it, so publish the singleton
6268
// first; otherwise `preserveSet` clears the set and sync wipes the canvas.
63-
if (group.length > 1 || !anchor || !getTimelineSelectionSet().has(anchor)) {
64-
setTimelineSelectionSet(new Set(members));
69+
if (group.length > 1 || !getTimelineSelectionSet().has(timelineAnchor)) {
70+
setTimelineSelectionSet(publishedMembers);
6571
}
66-
setSelectedTimelineElementId(anchor, { preserveSet: true });
72+
setSelectedTimelineElementId(timelineAnchor, { preserveSet: true });
6773
}

‎packages/studio/src/hooks/useDomSelection.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ describe("useDomSelection marquee", () => {
144144
});
145145
harness.cleanup();
146146
});
147+
148+
it("uses a surviving group member as the timeline anchor when the canvas primary has no row", () => {
149+
const canvasOnly = document.createElement("div");
150+
canvasOnly.id = "canvas-only";
151+
const card = document.createElement("div");
152+
card.id = "card";
153+
document.body.append(canvasOnly, card);
154+
const harness = renderHarness(
155+
{ activeCompPath: "index.html", projectId: "project-1", refreshKey: 0 },
156+
{ timelineElements: [timelineElement("card")] },
157+
);
158+
159+
act(() =>
160+
harness
161+
.current()
162+
.applyMarqueeSelection(
163+
[makeSelection("Canvas only", canvasOnly), makeSelection("Card", card)],
164+
false,
165+
),
166+
);
167+
168+
expect(harness.timeline.setTimelineSelectionSet).toHaveBeenCalledWith(new Set(["card"]));
169+
expect(harness.timeline.setSelectedTimelineElementId).toHaveBeenCalledWith("card", {
170+
preserveSet: true,
171+
});
172+
harness.cleanup();
173+
});
147174
});
148175

149176
/**

‎packages/studio/src/hooks/useDomSelection.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,12 @@ export function useDomSelection({
482482
[activeCompPath, announceTimelineSelection, buildDomSelectionFromTarget, previewIframeRef],
483483
);
484484

485-
refreshDomEditGroupSelectionsFromPreviewRef.current = refreshDomEditGroupSelectionsFromPreview;
486-
487485
// ── Effects ──
488486

487+
useEffect(() => {
488+
refreshDomEditGroupSelectionsFromPreviewRef.current = refreshDomEditGroupSelectionsFromPreview;
489+
}, [refreshDomEditGroupSelectionsFromPreview]);
490+
489491
// Clear hover unconditionally on composition/project/preview change
490492
// eslint-disable-next-line no-restricted-syntax
491493
useEffect(() => {

0 commit comments

Comments
 (0)