Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Fix slot's getting out of sync between pages (#5818)

Oleg Isonen4beffcd19428
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 2f2788a089..c54cd423fd 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -982,6 +982,46 @@ describe("reparent instance", () => {
);
});
+ test("sort shared slot content through visible slot drop target", () => {
+ const data = renderData(
+ <$.Body ws:id="body">
+ <$.Slot ws:id="slot1">
+ <$.Fragment ws:id="fragment">
+ <$.Box ws:id="box1"></$.Box>
+ <$.Box ws:id="box2"></$.Box>
+ <$.Box ws:id="box3"></$.Box>
+ </$.Fragment>
+ </$.Slot>
+ <$.Slot ws:id="slot2">
+ {/* same ids */}
+ <$.Fragment ws:id="fragment">
+ <$.Box ws:id="box1"></$.Box>
+ <$.Box ws:id="box2"></$.Box>
+ <$.Box ws:id="box3"></$.Box>
+ </$.Fragment>
+ </$.Slot>
+ </$.Body>
+ );
+ $registeredComponentMetas.set(createFakeComponentMetas({}));
+
+ reparentInstanceMutable(data, ["box1", "fragment", "slot1", "body"], {
+ parentSelector: ["slot2", "body"],
+ position: 3,
+ });
+
+ expect(data.instances.get("slot1")?.children).toEqual([
+ { type: "id", value: "fragment" },
+ ]);
+ expect(data.instances.get("slot2")?.children).toEqual([
+ { type: "id", value: "fragment" },
+ ]);
+ expect(data.instances.get("fragment")?.children).toEqual([
+ { type: "id", value: "box2" },
+ { type: "id", value: "box3" },
+ { type: "id", value: "box1" },
+ ]);
+ });
+
test("reparent only selected slot occurrence", () => {
const data = renderData(
<$.Body ws:id="body">
@@ -1259,7 +1299,7 @@ describe("delete instance", () => {
expect(data.instances.get("slotId")?.children.length).toEqual(0);
});
- test("delete only selected slot occurrence", () => {
+ test("delete shared slot content from all slot occurrences", () => {
const data = renderData(
<$.Body ws:id="bodyId">
<$.Slot ws:id="slot1">
@@ -1281,13 +1321,14 @@ describe("delete instance", () => {
getInstancePath(["div", "fragment", "slot1", "bodyId"], data.instances)
);
- expect(data.instances.get("slot1")?.children).toEqual([]);
- expect(data.instances.get("slot2")?.children).toEqual([
+ expect(data.instances.get("slot1")?.children).toEqual([
{ type: "id", value: "fragment" },
]);
- expect(data.instances.get("fragment")?.children).toEqual([
- { type: "id", value: "div" },
+ expect(data.instances.get("slot2")?.children).toEqual([
+ { type: "id", value: "fragment" },
]);
+ expect(data.instances.get("fragment")?.children).toEqual([]);
+ expect(data.instances.has("div")).toEqual(false);
});
});
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 2e658bf894..3fc55b6c77 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -505,18 +505,14 @@ export const reparentInstanceMutable = (
? initialSourceInstancePath[1].instance.id
: undefined;
const targetSlot = data.instances.get(dropTarget.parentSelector[0]);
- if (
+ const isSameSharedSlotFragment =
sourceFragmentId !== undefined &&
targetSlot?.component === "Slot" &&
targetSlot.children[0]?.type === "id" &&
- targetSlot.children[0].value === sourceFragmentId
- ) {
- return sourceInstanceSelector;
- }
- const sourceInstancePath = detachSharedSlotContentMutable(
- data,
- initialSourceInstancePath ?? []
- );
+ targetSlot.children[0].value === sourceFragmentId;
+ const sourceInstancePath = isSameSharedSlotFragment
+ ? (initialSourceInstancePath ?? [])
+ : detachSharedSlotContentMutable(data, initialSourceInstancePath ?? []);
sourceInstanceSelector = sourceInstancePath[0]?.instanceSelector;
if (sourceInstanceSelector === undefined) {
return;
@@ -621,6 +617,21 @@ export const reparentInstance = (
});
};
+const countInstanceChildReferences = (
+ instances: Instances,
+ instanceId: Instance["id"]
+) => {
+ let count = 0;
+ for (const instance of instances.values()) {
+ for (const child of instance.children) {
+ if (child.type === "id" && child.value === instanceId) {
+ count += 1;
+ }
+ }
+ }
+ return count;
+};
+
export const deleteInstanceMutable = (
data: Omit<WebstudioData, "pages">,
instancePath: undefined | InstancePath
@@ -628,7 +639,6 @@ export const deleteInstanceMutable = (
if (instancePath === undefined) {
return false;
}
- instancePath = detachSharedSlotContentMutable(data, instancePath);
const {
instances,
props,
@@ -650,7 +660,8 @@ export const deleteInstanceMutable = (
if (
parentInstance?.component === "Fragment" &&
parentInstance.children.length === 1 &&
- grandparentInstance
+ grandparentInstance &&
+ countInstanceChildReferences(instances, parentInstance.id) < 2
) {
targetInstance = parentInstance;
parentInstance = grandparentInstance;
@@ -707,21 +718,6 @@ export const deleteInstanceMutable = (
return true;
};
-const countInstanceChildReferences = (
- instances: Instances,
- instanceId: Instance["id"]
-) => {
- let count = 0;
- for (const instance of instances.values()) {
- for (const child of instance.children) {
- if (child.type === "id" && child.value === instanceId) {
- count += 1;
- }
- }
- }
- return count;
-};
-
const cloneSharedSlotFragmentMutable = (
data: Omit<WebstudioData, "pages">,
slotId: Instance["id"],
diff --git a/apps/builder/app/shared/page-utils.test.tsx b/apps/builder/app/shared/page-utils.test.tsx
index bca34a5836..452b94c87a 100644
--- a/apps/builder/app/shared/page-utils.test.tsx
+++ b/apps/builder/app/shared/page-utils.test.tsx
@@ -217,6 +217,59 @@ describe("insert page copy", () => {
});
});
+ test("preserves slot content ids when duplicating page", () => {
+ const dataWithoutPages = renderData(
+ <$.Body ws:id="bodyId">
+ <$.Slot ws:id="slotId">
+ <$.Fragment ws:id="fragmentId">
+ <$.Box ws:id="boxId"></$.Box>
+ </$.Fragment>
+ </$.Slot>
+ </$.Body>
+ );
+ const data = getWebstudioDataStub({
+ ...dataWithoutPages,
+ pages: migratePages({
+ meta: {},
+ homePage: {
+ id: "pageId",
+ name: "Name",
+ path: "",
+ title: `"Title"`,
+ meta: {},
+ rootInstanceId: "bodyId",
+ },
+ pages: [],
+ folders: [createRootFolder(["pageId"])],
+ }),
+ });
+
+ insertPageCopyMutable({
+ source: { data, pageId: "pageId" },
+ target: { data, folderId: ROOT_FOLDER_ID },
+ });
+
+ const copiedPage = getCopiedPages(data)[0];
+ expect(copiedPage.rootInstanceId).not.toBe("bodyId");
+ const copiedBody = data.instances.get(copiedPage.rootInstanceId);
+ const copiedSlotId = copiedBody?.children[0]?.value;
+ if (copiedSlotId === undefined) {
+ throw Error("Expected copied slot id");
+ }
+ expect(copiedSlotId).not.toBe("slotId");
+ expect(data.instances.get(copiedSlotId)?.children).toEqual([
+ { type: "id", value: "fragmentId" },
+ ]);
+ expect(
+ Array.from(data.instances.values()).filter(
+ (instance) => instance.id === "fragmentId"
+ )
+ ).toHaveLength(1);
+ expect(data.instances.get("fragmentId")?.children).toEqual([
+ { type: "id", value: "boxId" },
+ ]);
+ });
+
test("deduplicate path for non-home page copy", () => {
const data = getWebstudioDataStub({
instances: toMap<Instance>([