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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Fix insertion into legacy slots (#5820)
Oleg Isonen7ded63065c77
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsxindex c54cd423fd..9a0fb8907a 100644--- a/apps/builder/app/shared/instance-utils.test.tsx+++ b/apps/builder/app/shared/instance-utils.test.tsx@@ -428,6 +428,39 @@ describe("insert instance children", () => {);});+ test("insert instance children into legacy slot with direct children", () => {+ const data = renderData(+ <$.Body ws:id="bodyId">+ <$.Slot ws:id="slotId">+ <$.Box ws:id="boxId"></$.Box>+ </$.Slot>+ </$.Body>+ );+ const [div] = renderTemplate(+ <ws.element ws:tag="div" ws:id="divId"></ws.element>+ ).instances;+ data.instances.set(div.id, div);++ insertInstanceChildrenMutable(data, [{ type: "id", value: "divId" }], {+ parentSelector: ["slotId", "bodyId"],+ position: "end",+ });++ const fragmentId = data.instances.get("slotId")?.children[0]?.value;+ expect(data).toEqual(+ renderData(+ <$.Body ws:id="bodyId">+ <$.Slot ws:id="slotId">+ <$.Fragment ws:id={fragmentId}>+ <$.Box ws:id="boxId"></$.Box>+ <ws.element ws:tag="div" ws:id="divId"></ws.element>+ </$.Fragment>+ </$.Slot>+ </$.Body>+ )+ );+ });+test("insert instance children into the start of target", () => {const data = renderData(<ws.element ws:tag="body" ws:id="bodyId">@@ -646,6 +679,47 @@ describe("insert webstudio element at", () => {);});+ test("insert element into selected legacy slot with direct children", () => {+ $pages.set(+ createDefaultPages({ homePageId: "homePageId", rootInstanceId: "bodyId" })+ );+ $instances.set(+ renderData(+ <$.Body ws:id="bodyId">+ <$.Slot ws:id="slotId">+ <$.Box ws:id="boxId"></$.Box>+ </$.Slot>+ </$.Body>+ ).instances+ );+ selectPage("homePageId");+ selectInstance(["slotId", "bodyId"]);++ insertWebstudioElementAt();++ const instances = $instances.get();+ const fragmentId = instances.get("slotId")?.children[0]?.value;+ const newInstanceId = Array.from(instances.keys()).find(+ (id) =>+ id !== "bodyId" &&+ id !== "slotId" &&+ id !== "boxId" &&+ id !== fragmentId+ );+ expect(instances).toEqual(+ renderData(+ <$.Body ws:id="bodyId">+ <$.Slot ws:id="slotId">+ <$.Fragment ws:id={fragmentId}>+ <$.Box ws:id="boxId"></$.Box>+ <ws.element ws:id={newInstanceId} ws:tag="div" />+ </$.Fragment>+ </$.Slot>+ </$.Body>+ ).instances+ );+ });+test("insert element into closest non-textual container", () => {$pages.set(createDefaultPages({ homePageId: "homePageId", rootInstanceId: "bodyId" })diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.tsindex 0a578532c1..cad091cd3a 100644--- a/apps/builder/app/shared/tree-utils.ts+++ b/apps/builder/app/shared/tree-utils.ts@@ -79,6 +79,21 @@ export const getInstanceOrCreateFragmentIfNecessary = (// otherwise multiple slots will have to maintain own children// here all slot children are wrapped with fragment instanceif (instance.component === "Slot") {+ const wrapSlotChildrenWithFragment = () => {+ const id = nanoid();+ instances.set(id, {+ type: "instance",+ id,+ component: "Fragment",+ children: instance.children,+ });+ instance.children = [{ type: "id", value: id }];+ return {+ parentSelector: [id, ...dropTarget.parentSelector],+ position: dropTarget.position,+ };+ };+if (instance.children.length === 0) {const id = nanoid();const fragment: Instance = {@@ -94,14 +109,20 @@ export const getInstanceOrCreateFragmentIfNecessary = (position: dropTarget.position,};}- // first slot child is always fragmentif (instance.children[0].type === "id") {const fragmentId = instance.children[0].value;+ const fragment = instances.get(fragmentId);+ if (fragment?.component !== "Fragment") {+ // Legacy slots stored content directly under Slot. Normalize before+ // inserting so the first content child is not mistaken for Fragment.+ return wrapSlotChildrenWithFragment();+ }return {parentSelector: [fragmentId, ...dropTarget.parentSelector],position: dropTarget.position,};}+ return wrapSlotChildrenWithFragment();}return;};