Skip to content

Commit ada9c08

Browse files
authored
fix: detach slot content before unwrap (#5790)
1 parent 8bbb649 commit ada9c08

2 files changed

Lines changed: 61 additions & 15 deletions

File tree

apps/builder/app/shared/instance-utils.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,6 +3351,47 @@ describe("unwrap instance", () => {
33513351
expect($instances.get().has("slot")).toBe(false);
33523352
expect($instances.get().has("fragment")).toBe(false);
33533353
});
3354+
3355+
test("unwrap command detaches selected duplicated slot occurrence", () => {
3356+
const { instances, props } = renderData(
3357+
<$.Body ws:id="body">
3358+
<$.Slot ws:id="slot1">
3359+
<$.Fragment ws:id="fragment">
3360+
<ws.element ws:tag="div" ws:id="div"></ws.element>
3361+
</$.Fragment>
3362+
</$.Slot>
3363+
<$.Slot ws:id="slot2">
3364+
{/* same ids */}
3365+
<$.Fragment ws:id="fragment">
3366+
<ws.element ws:tag="div" ws:id="div"></ws.element>
3367+
</$.Fragment>
3368+
</$.Slot>
3369+
</$.Body>
3370+
);
3371+
$instances.set(instances);
3372+
$props.set(props);
3373+
const pages = createDefaultPages({ rootInstanceId: "body" });
3374+
$pages.set(pages);
3375+
$selectedPageId.set(pages.homePageId);
3376+
selectInstance(["div", "fragment", "slot1", "body"]);
3377+
3378+
unwrapInstance();
3379+
3380+
const bodyChildren = $instances.get().get("body")?.children;
3381+
const unwrappedDivId =
3382+
bodyChildren?.[0]?.type === "id" ? bodyChildren[0].value : undefined;
3383+
3384+
expect(unwrappedDivId).toBeDefined();
3385+
expect(unwrappedDivId).not.toBe("div");
3386+
expect($selectedInstanceSelector.get()).toEqual([unwrappedDivId, "body"]);
3387+
expect($instances.get().get("slot1")).toBeUndefined();
3388+
expect($instances.get().get("slot2")?.children).toEqual([
3389+
{ type: "id", value: "fragment" },
3390+
]);
3391+
expect($instances.get().get("fragment")?.children).toEqual([
3392+
{ type: "id", value: "div" },
3393+
]);
3394+
});
33543395
});
33553396

33563397
describe("canUnwrapInstance", () => {

apps/builder/app/shared/instance-utils.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,18 +1194,22 @@ export const unwrapInstance = () => {
11941194
return;
11951195
}
11961196

1197-
const [selectedItem, defaultParentItem] = instancePath;
1198-
const parentItem =
1199-
defaultParentItem?.instance.component === "Fragment" &&
1200-
instancePath[2]?.instance.component === "Slot"
1201-
? instancePath[2]
1202-
: defaultParentItem;
1203-
if (parentItem === undefined) {
1204-
return;
1205-
}
1206-
12071197
try {
12081198
updateWebstudioData((data) => {
1199+
const detachedInstancePath = detachSharedSlotContentMutable(
1200+
data,
1201+
instancePath
1202+
);
1203+
const [selectedItem, defaultParentItem] = detachedInstancePath;
1204+
const parentItem =
1205+
defaultParentItem?.instance.component === "Fragment" &&
1206+
detachedInstancePath[2]?.instance.component === "Slot"
1207+
? detachedInstancePath[2]
1208+
: defaultParentItem;
1209+
if (parentItem === undefined) {
1210+
return;
1211+
}
1212+
12091213
const result = unwrapInstanceMutable({
12101214
instances: data.instances,
12111215
props: data.props,
@@ -1218,12 +1222,13 @@ export const unwrapInstance = () => {
12181222
toast.error(result.error ?? "Cannot unwrap instance");
12191223
throw Error("Abort transaction");
12201224
}
1225+
1226+
// After unwrap, select the child that replaced the parent.
1227+
selectInstance([
1228+
selectedItem.instance.id,
1229+
...parentItem.instanceSelector.slice(1),
1230+
]);
12211231
});
1222-
// After unwrap, select the child that replaced the parent
1223-
selectInstance([
1224-
selectedItem.instance.id,
1225-
...parentItem.instanceSelector.slice(1),
1226-
]);
12271232
} catch {
12281233
// do nothing
12291234
}

0 commit comments

Comments
 (0)