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: insert marketplace sections after selected instance (#4918)

Bogdan Chadkin4e6a448d96b6
diff --git a/apps/builder/app/builder/features/marketplace/templates.tsx b/apps/builder/app/builder/features/marketplace/templates.tsx
index 64a4c7ff72..099cfdb5ee 100644
--- a/apps/builder/app/builder/features/marketplace/templates.tsx
+++ b/apps/builder/app/builder/features/marketplace/templates.tsx
@@ -57,6 +57,11 @@ const insertSection = ({
}
const insertable = findClosestInsertable(fragment);
if (insertable) {
+ // numeric position means the instance already
+ // insertd after or even into ancestor
+ if (insertable.position === "end") {
+ insertable.position = "after";
+ }
insertWebstudioFragmentAt(fragment, insertable);
}
};
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 2fecef9404..049809226f 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -13,7 +13,7 @@ import {
$instances,
$registeredComponentMetas,
} from "../nano-states";
-import type { InstanceSelector, DroppableTarget } from "../tree-utils";
+import type { InstanceSelector } from "../tree-utils";
import {
deleteInstanceMutable,
extractWebstudioFragment,
@@ -22,6 +22,7 @@ import {
getWebstudioData,
insertInstanceChildrenMutable,
findClosestInsertable,
+ type Insertable,
} from "../instance-utils";
import { isInstanceDetachable } from "../matcher";
import { $selectedInstancePath } from "../awareness";
@@ -91,7 +92,7 @@ export const getPortalFragmentSelector = (
return [instance.children[0].value, ...instanceSelector];
};
-const findPasteTarget = (data: InstanceData): undefined | DroppableTarget => {
+const findPasteTarget = (data: InstanceData): undefined | Insertable => {
const instances = $instances.get();
const instanceSelector = $selectedInstanceSelector.get();
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 5fbaf0b528..8313d804c9 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -420,8 +420,7 @@ describe("insert instance children", () => {
});
describe("insert webstudio fragment at", () => {
- test("insert multiple instances", () => {
- $instances.set(renderData(<$.Body ws:id="bodyId"></$.Body>).instances);
+ beforeEach(() => {
$styleSourceSelections.set(new Map());
$styleSources.set(new Map());
$breakpoints.set(new Map());
@@ -430,6 +429,10 @@ describe("insert webstudio fragment at", () => {
$resources.set(new Map());
$props.set(new Map());
$assets.set(new Map());
+ });
+
+ test("insert multiple instances", () => {
+ $instances.set(renderData(<$.Body ws:id="bodyId"></$.Body>).instances);
insertWebstudioFragmentAt(
renderTemplate(
<>
@@ -451,6 +454,49 @@ describe("insert webstudio fragment at", () => {
).instances
);
});
+
+ test("insert fragment after insertable", () => {
+ $instances.set(
+ renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId"></$.Box>
+ </$.Body>
+ ).instances
+ );
+ insertWebstudioFragmentAt(
+ renderTemplate(<$.Heading ws:id="headingId"></$.Heading>),
+ {
+ parentSelector: ["boxId", "bodyId"],
+ position: "after",
+ }
+ );
+ expect($instances.get()).toEqual(
+ renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId"></$.Box>
+ <$.Heading ws:id={expect.any(String)}></$.Heading>
+ </$.Body>
+ ).instances
+ );
+ });
+
+ test("insert fragment inside of body when configured to place after insertable", () => {
+ $instances.set(renderData(<$.Body ws:id="bodyId"></$.Body>).instances);
+ insertWebstudioFragmentAt(
+ renderTemplate(<$.Heading ws:id="headingId"></$.Heading>),
+ {
+ parentSelector: ["bodyId"],
+ position: "after",
+ }
+ );
+ expect($instances.get()).toEqual(
+ renderData(
+ <$.Body ws:id="bodyId">
+ <$.Heading ws:id={expect.any(String)}></$.Heading>
+ </$.Body>
+ ).instances
+ );
+ });
});
describe("reparent instance", () => {
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 600f3e9f71..37a5404e2e 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -276,29 +276,32 @@ export const findClosestEditableInstanceSelector = (
export const insertInstanceChildrenMutable = (
data: WebstudioData,
children: Instance["children"],
- insertTarget: DroppableTarget
+ insertTarget: Insertable
) => {
+ const dropTarget: DroppableTarget = {
+ parentSelector: insertTarget.parentSelector,
+ position: insertTarget.position === "after" ? "end" : insertTarget.position,
+ };
const metas = $registeredComponentMetas.get();
insertTarget =
- getInstanceOrCreateFragmentIfNecessary(data.instances, insertTarget) ??
+ getInstanceOrCreateFragmentIfNecessary(data.instances, dropTarget) ??
insertTarget;
insertTarget =
wrapEditableChildrenAroundDropTargetMutable(
data.instances,
data.props,
metas,
- insertTarget
+ dropTarget
) ?? insertTarget;
const [parentInstanceId] = insertTarget.parentSelector;
const parentInstance = data.instances.get(parentInstanceId);
if (parentInstance === undefined) {
return;
}
- const { position } = insertTarget;
- if (position === "end") {
+ if (dropTarget.position === "end") {
parentInstance.children.push(...children);
} else {
- parentInstance.children.splice(position, 0, ...children);
+ parentInstance.children.splice(dropTarget.position, 0, ...children);
}
};
@@ -306,17 +309,21 @@ export const insertWebstudioFragmentAt = (
fragment: WebstudioFragment,
insertable: Insertable
) => {
- let children: undefined | Instance["children"];
+ let newInstanceSelector: undefined | InstanceSelector;
updateWebstudioData((data) => {
+ const instancePath = getInstancePath(
+ insertable.parentSelector,
+ data.instances
+ );
const { newInstanceIds } = insertWebstudioFragmentCopy({
data,
fragment,
availableVariables: findAvailableVariables({
...data,
- startingInstanceId: insertable.parentSelector[0],
+ startingInstanceId: instancePath[0].instance.id,
}),
});
- children = fragment.children.map((child) => {
+ const children: Instance["children"] = fragment.children.map((child) => {
if (child.type === "id") {
return {
type: "id",
@@ -325,10 +332,32 @@ export const insertWebstudioFragmentAt = (
}
return child;
});
- insertInstanceChildrenMutable(data, children, insertable);
+ let parentSelector;
+ let position: number | "end";
+ if (insertable.position === "after") {
+ if (instancePath.length === 1) {
+ parentSelector = insertable.parentSelector;
+ position = "end";
+ } else {
+ parentSelector = instancePath[1].instanceSelector;
+ const [{ instance }, { instance: parentInstance }] = instancePath;
+ const index = parentInstance.children.findIndex(
+ (child) => child.type === "id" && child.value === instance.id
+ );
+ position = 1 + index;
+ }
+ } else {
+ parentSelector = insertable.parentSelector;
+ position = insertable.position;
+ }
+ insertInstanceChildrenMutable(data, children, {
+ parentSelector,
+ position,
+ });
+ newInstanceSelector = [children[0].value, ...parentSelector];
});
- if (children?.[0].type === "id") {
- selectInstance([children[0].value, ...insertable.parentSelector]);
+ if (newInstanceSelector) {
+ selectInstance(newInstanceSelector);
}
};
@@ -1153,7 +1182,7 @@ export const findClosestSlot = (
export type Insertable = {
parentSelector: InstanceSelector;
- position: number | "end";
+ position: number | "end" | "after";
};
export const findClosestInsertable = (