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.

make delete work

Roman Pominov543982db779f
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index eb73ce930d..d3f6d40608 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -148,6 +148,11 @@ export const useDeleteInstance = () => {
"deleteInstance",
({ id }) => {
if (rootInstance !== undefined && selectedInstance !== undefined) {
+ // @todo tell user they can't delete root
+ if (id === rootInstance.id) {
+ return;
+ }
+
const parentInstance = findParentInstance(rootInstance, id);
if (parentInstance !== undefined) {
const siblingInstance = findClosestSiblingInstance(
diff --git a/apps/designer/app/canvas/shared/use-shortcuts.ts b/apps/designer/app/canvas/shared/use-shortcuts.ts
index 21e47c0955..67310ea495 100644
--- a/apps/designer/app/canvas/shared/use-shortcuts.ts
+++ b/apps/designer/app/canvas/shared/use-shortcuts.ts
@@ -9,10 +9,7 @@ import {
import { shortcuts, options } from "~/shared/shortcuts";
import { useSelectedInstance } from "./nano-states";
import { copy, paste } from "./copy-paste";
-import {
- useRootInstance,
- useTextEditingInstanceId,
-} from "~/shared/nano-states";
+import { useTextEditingInstanceId } from "~/shared/nano-states";
const inputTags = ["INPUT", "SELECT", "TEXTAREA"] as const;
@@ -49,23 +46,16 @@ const publishCancelCurrentDrag = () => {
};
export const useShortcuts = () => {
- const [rootInstance] = useRootInstance();
const [selectedInstance, setSelectedInstance] = useSelectedInstance();
const [editingInstanceId, setEditingInstanceId] = useTextEditingInstanceId();
const publishDeleteInstance = () => {
- // @todo tell user they can't delete root
- if (
- selectedInstance === undefined ||
- selectedInstance.id === rootInstance?.id
- ) {
+ if (selectedInstance === undefined) {
return;
}
publish<"deleteInstance", { id: Instance["id"] }>({
type: "deleteInstance",
- payload: {
- id: selectedInstance.id,
- },
+ payload: { id: selectedInstance.id },
});
};
@@ -82,6 +72,7 @@ export const useShortcuts = () => {
esc: publishCancelCurrentDrag,
} as const;
+ // @todo: doesnt allow to delete when navigator is in focus
useHotkeys(
"backspace, delete",
shortcutHandlerMap.delete,
diff --git a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
index 3fa7af1dfc..4bcb55e9b3 100644
--- a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
@@ -1,5 +1,6 @@
import { Flex } from "@webstudio-is/design-system";
import type { Instance, Publish } from "@webstudio-is/react-sdk";
+import { useCallback } from "react";
import { useSelectedInstanceData } from "~/designer/shared/nano-states";
import { Tree } from "~/designer/shared/tree";
import { useRootInstance } from "~/shared/nano-states";
@@ -15,6 +16,40 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {
const [selectedInstanceData] = useSelectedInstanceData();
const [rootInstance] = useRootInstance();
+ const handleSelect = useCallback(
+ (instanceId: Instance["id"]) => {
+ publish<"selectInstanceById", Instance["id"]>({
+ type: "selectInstanceById",
+ payload: instanceId,
+ });
+ },
+ [publish]
+ );
+
+ type ReparentInstancePayload = {
+ instanceId: Instance["id"];
+ dropTarget: { instanceId: Instance["id"]; position: number | "end" };
+ };
+ const handleDragEnd = useCallback(
+ (payload: ReparentInstancePayload) => {
+ publish<"reparentInstance", ReparentInstancePayload>({
+ type: "reparentInstance",
+ payload,
+ });
+ },
+ [publish]
+ );
+
+ const handleDelete = useCallback(
+ (instanceId: Instance["id"]) => {
+ publish<"deleteInstance", { id: Instance["id"] }>({
+ type: "deleteInstance",
+ payload: { id: instanceId },
+ });
+ },
+ [publish]
+ );
+
if (rootInstance === undefined) return null;
return (
<Flex css={{ height: "100%", flexDirection: "column" }}>
@@ -23,24 +58,9 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {
<Tree
root={rootInstance}
selectedInstanceId={selectedInstanceData?.id}
- onSelect={(instanceId) => {
- publish<"selectInstanceById", Instance["id"]>({
- type: "selectInstanceById",
- payload: instanceId,
- });
- }}
- onDragEnd={(payload) => {
- publish<
- "reparentInstance",
- {
- instanceId: Instance["id"];
- dropTarget: {
- instanceId: Instance["id"];
- position: number | "end";
- };
- }
- >({ type: "reparentInstance", payload });
- }}
+ onSelect={handleSelect}
+ onDragEnd={handleDragEnd}
+ onDelete={handleDelete}
/>
</Flex>
</Flex>
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index f4af5c65a3..505fa3638b 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -42,6 +42,7 @@ type TreeProps = {
instanceId: Instance["id"];
dropTarget: { instanceId: Instance["id"]; position: number | "end" };
}) => void;
+ onDelete: (instanceId: Instance["id"]) => void;
};
export const Tree = ({
@@ -50,6 +51,7 @@ export const Tree = ({
onSelect,
animate,
onDragEnd,
+ onDelete,
}: TreeProps) => {
const { getIsExpanded, setIsExpanded } = useExpandState({
root,
@@ -367,6 +369,7 @@ export const Tree = ({
selectedInstanceId,
getIsExpanded,
setIsExpanded,
+ onDelete,
});
return (
@@ -424,11 +427,13 @@ const useKeyboardNavigation = ({
selectedInstanceId,
getIsExpanded,
setIsExpanded,
+ onDelete,
}: {
root: Instance;
selectedInstanceId: Instance["id"] | undefined;
getIsExpanded: (instance: Instance) => boolean;
setIsExpanded: (instance: Instance, isExpanded: boolean) => void;
+ onDelete: (instanceId: Instance["id"]) => void;
}) => {
const selectedInstance = useMemo(() => {
if (selectedInstanceId === undefined) {
@@ -453,7 +458,7 @@ const useKeyboardNavigation = ({
}, [root, getIsExpanded]);
const rootRef = useHotkeys(
- "up,down,right,left,space",
+ "up,down,right,left,space,backspace,delete",
(event, { shortcut }) => {
if (selectedInstance === undefined) {
return;
@@ -481,8 +486,17 @@ const useKeyboardNavigation = ({
event.preventDefault(); // prevent scrolling
}
}
+ if (shortcut === "backspace" || shortcut === "delete") {
+ onDelete(selectedInstance.id);
+ }
},
- [selectedInstance, flatCurrentlyExpandedTree, getIsExpanded, setIsExpanded]
+ [
+ selectedInstance,
+ flatCurrentlyExpandedTree,
+ getIsExpanded,
+ setIsExpanded,
+ onDelete,
+ ]
);
const setFocus = useCallback(