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.

Delete page data on client (#1171)

Bogdan Chadkind49ce1e85299
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/settings.tsx b/apps/builder/app/builder/features/sidebar-left/panels/pages/settings.tsx
index e0face8a38..7afd903562 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/pages/settings.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/settings.tsx
@@ -37,6 +37,7 @@ import type {
} from "~/shared/pages";
import { restPagesPath } from "~/shared/router-utils";
import { Header, HeaderSuffixSpacer } from "../../header";
+import { deleteInstance } from "~/shared/instance-utils";
const Group = styled(Flex, {
marginBottom: theme.spacing[9],
@@ -45,7 +46,7 @@ const Group = styled(Flex, {
});
const fieldNames = ["name", "path", "title", "description"] as const;
-type FieldName = typeof fieldNames[number];
+type FieldName = (typeof fieldNames)[number];
type FormPage = Pick<Page, "name" | "path" | "title"> & {
description: string;
};
@@ -364,6 +365,10 @@ export const PageSettings = ({
if (pages === undefined) {
return;
}
+ const page = pages.pages.find((item) => item.id === pageId);
+ if (page) {
+ deleteInstance(page.rootInstanceId);
+ }
setPages({
homePage: pages.homePage,
pages: pages.pages.filter((item) => item.id !== pageId),
diff --git a/apps/builder/app/builder/features/topbar/menu/menu.tsx b/apps/builder/app/builder/features/topbar/menu/menu.tsx
index f99c8fc6c3..ffcfd845d1 100644
--- a/apps/builder/app/builder/features/topbar/menu/menu.tsx
+++ b/apps/builder/app/builder/features/topbar/menu/menu.tsx
@@ -28,11 +28,8 @@ import {
} from "~/shared/theme";
import { useClientSettings } from "~/builder/shared/client-settings";
import { dashboardPath } from "~/shared/router-utils";
-import {
- selectedInstanceIdStore,
- useIsPreviewMode,
-} from "~/shared/nano-states";
-import { deleteInstance } from "~/shared/instance-utils";
+import { useIsPreviewMode } from "~/shared/nano-states";
+import { deleteSelectedInstance } from "~/shared/instance-utils";
import { MenuButton } from "./menu-button";
import { useAuthPermit } from "~/shared/nano-states";
import { zoomIn, zoomOut } from "~/shared/nano-states/breakpoints";
@@ -167,15 +164,7 @@ export const Menu = ({ publish }: MenuProps) => {
</DropdownMenuItem>
*/}
- <DropdownMenuItem
- onSelect={() => {
- const selectedInstanceId = selectedInstanceIdStore.get();
- if (selectedInstanceId === undefined) {
- return;
- }
- deleteInstance(selectedInstanceId);
- }}
- >
+ <DropdownMenuItem onSelect={deleteSelectedInstance}>
Delete
<DropdownMenuItemRightSlot>
<ShortcutHint value={["backspace"]} />
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index c4570ee4a0..f735963604 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -181,7 +181,12 @@ export const onCopy = () => {
export const onCut = () => {
const selectedInstanceId = selectedInstanceIdStore.get();
- if (selectedInstanceId === undefined) {
+ const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
+ // @todo tell user they can't delete root
+ if (
+ selectedInstanceId === undefined ||
+ selectedInstanceId === rootInstanceId
+ ) {
return;
}
const data = getTreeData(selectedInstanceId);
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 75dfcb20cc..1cbbba7980 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -49,12 +49,6 @@ export const reparentInstance = (
};
export const deleteInstance = (targetInstanceId: Instance["id"]) => {
- const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
- // @todo tell user they can't delete root
- if (targetInstanceId === rootInstanceId) {
- return;
- }
-
store.createTransaction(
[
instancesStore,
@@ -71,14 +65,15 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {
styleSources,
styleSourceSelections
);
- if (parentInstance === undefined) {
- return;
+
+ // may not exist when delete root
+ if (parentInstance) {
+ removeByMutable(
+ parentInstance.children,
+ (child) => child.type === "id" && child.value === targetInstanceId
+ );
}
- removeByMutable(
- parentInstance.children,
- (child) => child.type === "id" && child.value === targetInstanceId
- );
for (const instanceId of subtreeIds) {
instances.delete(instanceId);
}
@@ -100,7 +95,22 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {
}
}
- selectedInstanceIdStore.set(parentInstance.id);
+ if (parentInstance) {
+ selectedInstanceIdStore.set(parentInstance.id);
+ }
}
);
};
+
+export const deleteSelectedInstance = () => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
+ // @todo tell user they can't delete root
+ if (
+ selectedInstanceId === undefined ||
+ selectedInstanceId === rootInstanceId
+ ) {
+ return;
+ }
+ deleteInstance(selectedInstanceId);
+};
diff --git a/apps/builder/app/shared/shortcuts/shortcuts.ts b/apps/builder/app/shared/shortcuts/shortcuts.ts
index e9015ac48a..2bed2938d8 100644
--- a/apps/builder/app/shared/shortcuts/shortcuts.ts
+++ b/apps/builder/app/shared/shortcuts/shortcuts.ts
@@ -1,12 +1,11 @@
import { Options, useHotkeys } from "react-hotkeys-hook";
import store from "immerhin";
-import { selectedInstanceIdStore } from "../nano-states";
import {
zoomIn,
zoomOut,
selectBreakpointByOrderNumber,
} from "../nano-states/breakpoints";
-import { deleteInstance } from "../instance-utils";
+import { deleteSelectedInstance } from "../instance-utils";
export const shortcuts = {
esc: "esc",
@@ -37,13 +36,7 @@ export const useSharedShortcuts = () => {
useHotkeys(
"backspace, delete",
- () => {
- const selectedInstanceId = selectedInstanceIdStore.get();
- if (selectedInstanceId === undefined) {
- return;
- }
- deleteInstance(selectedInstanceId);
- },
+ deleteSelectedInstance,
// prevent instance deletion while deleting text
{ enableOnFormTags: false, enableOnContentEditable: false },
[]
diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.ts
index 494dcfda30..dcb0358d25 100644
--- a/packages/project-build/src/db/build.ts
+++ b/packages/project-build/src/db/build.ts
@@ -234,10 +234,6 @@ export const deletePage = async ({
throw new Error(`Page with id "${pageId}" not found`);
}
- // @todo cleanup style source selections of deleted pages
- // @todo cleanup props of deleted pages
- // @todo cleanup instances of deleted pages
-
return {
homePage: currentPages.homePage,
pages: currentPages.pages.filter((page) => page.id !== pageId),