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: prevent deleting Body (#2467)

Bogdan Chadkin392b6b100509
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.ts
index bd15b3b15e..4bc024006e 100644
--- a/apps/builder/app/shared/instance-utils.test.ts
+++ b/apps/builder/app/shared/instance-utils.test.ts
@@ -10,9 +10,11 @@ import {
findClosestEditableInstanceSelector,
insertTemplateData,
type InsertConstraints,
+ deleteInstance,
} from "./instance-utils";
import {
instancesStore,
+ registeredComponentMetasStore,
styleSourceSelectionsStore,
styleSourcesStore,
stylesStore,
@@ -417,3 +419,48 @@ test("insert template data with only new style sources", () => {
])
);
});
+
+test("delete instance with its children", () => {
+ // body
+ // box1
+ // box11
+ // box2
+ instancesStore.set(
+ new Map([
+ createInstancePair("body", "Body", [
+ { type: "id", value: "box1" },
+ { type: "id", value: "box2" },
+ ]),
+ createInstancePair("box1", "Box", [{ type: "id", value: "box11" }]),
+ createInstancePair("box11", "Box", []),
+ createInstancePair("box2", "Box", []),
+ ])
+ );
+ registeredComponentMetasStore.set(createFakeComponentMetas({}));
+ deleteInstance(["box1", "body"]);
+ expect(instancesStore.get()).toEqual(
+ new Map([
+ createInstancePair("body", "Body", [{ type: "id", value: "box2" }]),
+ createInstancePair("box2", "Box", []),
+ ])
+ );
+});
+
+test("prevent deleting root instance", () => {
+ // body
+ // box1
+ instancesStore.set(
+ new Map([
+ createInstancePair("body", "Body", [{ type: "id", value: "box1" }]),
+ createInstancePair("box1", "Box", []),
+ ])
+ );
+ registeredComponentMetasStore.set(createFakeComponentMetas({}));
+ deleteInstance(["body"]);
+ expect(instancesStore.get()).toEqual(
+ new Map([
+ createInstancePair("body", "Body", [{ type: "id", value: "box1" }]),
+ createInstancePair("box1", "Box", []),
+ ])
+ );
+});
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 2a14ff40f8..763fbd7a3f 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -391,6 +391,10 @@ export const reparentInstance = (
};
export const deleteInstance = (instanceSelector: InstanceSelector) => {
+ // @todo tell user they can't delete root
+ if (instanceSelector.length === 1) {
+ return;
+ }
if (isInstanceDetachable(instanceSelector) === false) {
toast.error(
"This instance can not be moved outside of its parent component."
@@ -498,11 +502,7 @@ export const deleteSelectedInstance = () => {
if (textEditingInstanceSelector) {
return;
}
- // @todo tell user they can't delete root
- if (
- selectedInstanceSelector === undefined ||
- selectedInstanceSelector.length === 1
- ) {
+ if (selectedInstanceSelector === undefined) {
return;
}
deleteInstance(selectedInstanceSelector);