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.

find valid parent when inserting in selected instance

Roman Pominovf616fb596482
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index d7f7195046..b6cfedbf46 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -4,6 +4,7 @@ import {
type InstanceProps,
type Instance,
type Tree,
+ components,
allUserPropsContainer,
getBrowserStyle,
useAllUserProps,
@@ -18,6 +19,7 @@ import {
insertInstanceMutable,
findInstanceById,
reparentInstanceMutable,
+ getInstancePath,
} from "~/shared/tree-utils";
import store from "immerhin";
import {
@@ -48,6 +50,22 @@ export const usePopulateRootInstance = (tree: Tree) => {
}
};
+const getClosestValidParent = (
+ rootInstance: Instance,
+ instanceId: Instance["id"] | undefined
+) => {
+ if (instanceId === undefined) {
+ return rootInstance;
+ }
+
+ const path = getInstancePath(rootInstance, instanceId);
+ path.reverse();
+ return (
+ path.find((instance) => components[instance.component].canAcceptChild()) ??
+ rootInstance
+ );
+};
+
export const useInsertInstance = () => {
const [selectedInstance, setSelectedInstance] = useSelectedInstance();
@@ -69,7 +87,8 @@ export const useInsertInstance = () => {
populatedInstance,
{
parentId:
- dropTarget?.instanceId ?? selectedInstance?.id ?? rootInstance.id,
+ dropTarget?.instanceId ??
+ getClosestValidParent(rootInstance, selectedInstance?.id).id,
position: dropTarget === undefined ? "end" : dropTarget.position,
}
);