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.

refactor: cancel drag with command (#2509)

Bogdan Chadkin8f47d4a4f2fe
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index cb9df8e19d..7d0db950fd 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -22,7 +22,6 @@ import {
useReadCanvasRect,
Workspace,
} from "./features/workspace";
-import { usePublishShortcuts } from "./shared/shortcuts";
import {
assetsStore,
$authPermit,
@@ -277,7 +276,6 @@ export const Builder = ({
});
const isPreviewMode = useStore($isPreviewMode);
- usePublishShortcuts(publish);
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
// We need to initialize this in both canvas and builder,
// because the events will fire in either one, depending on where the focus is
diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.ts
index 8cb4fdf434..10898cdddd 100644
--- a/apps/builder/app/builder/shared/commands.ts
+++ b/apps/builder/app/builder/shared/commands.ts
@@ -15,6 +15,7 @@ import { onCopy, onPaste } from "~/shared/copy-paste/plugin-instance";
import { deleteInstance } from "~/shared/instance-utils";
import type { InstanceSelector } from "~/shared/tree-utils";
import { serverSyncStore } from "~/shared/sync";
+import { $publisher } from "~/shared/pubsub";
const makeBreakpointCommand = <CommandName extends string>(
name: CommandName,
@@ -80,6 +81,17 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
"formatClear",
],
commands: [
+ // system
+
+ {
+ name: "cancelCurrentDrag",
+ defaultHotkeys: ["escape"],
+ handler: () => {
+ const { publish } = $publisher.get();
+ publish?.({ type: "cancelCurrentDrag" });
+ },
+ },
+
// ui
{
diff --git a/apps/builder/app/builder/shared/shortcuts/index.ts b/apps/builder/app/builder/shared/shortcuts/index.ts
deleted file mode 100644
index d78204a336..0000000000
--- a/apps/builder/app/builder/shared/shortcuts/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-publish-shortcuts";
diff --git a/apps/builder/app/builder/shared/shortcuts/use-publish-shortcuts.ts b/apps/builder/app/builder/shared/shortcuts/use-publish-shortcuts.ts
deleted file mode 100644
index 02d958bbd8..0000000000
--- a/apps/builder/app/builder/shared/shortcuts/use-publish-shortcuts.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { useHotkeys } from "react-hotkeys-hook";
-import { shortcuts, options } from "~/shared/shortcuts";
-import type { Publish } from "~/shared/pubsub";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- shortcut: { name: keyof typeof shortcuts };
- }
-}
-
-const names = Object.keys(shortcuts) as Array<keyof typeof shortcuts>;
-
-/**
- * Forwarding shortcuts to the canvas.
- */
-export const usePublishShortcuts = (publish: Publish) => {
- names.forEach((name) => {
- // as long as the array is static, it's ok
- // eslint-disable-next-line react-hooks/rules-of-hooks
- useHotkeys(
- shortcuts[name],
- (_event) => {
- publish({
- type: "shortcut",
- payload: { name },
- });
- },
- options,
- []
- );
- });
-};
diff --git a/apps/builder/app/canvas/canvas-shortcuts.ts b/apps/builder/app/canvas/canvas-shortcuts.ts
deleted file mode 100644
index 4301c78a38..0000000000
--- a/apps/builder/app/canvas/canvas-shortcuts.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { useHotkeys } from "react-hotkeys-hook";
-import { shortcuts, options } from "~/shared/shortcuts";
-import { publish, useSubscribe } from "~/shared/pubsub";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- cancelCurrentDrag: undefined;
- }
-}
-
-const publishCancelCurrentDrag = () => {
- publish({ type: "cancelCurrentDrag" });
-};
-
-export const useCanvasShortcuts = () => {
- const shortcutHandlerMap = {
- esc: publishCancelCurrentDrag,
- } as const satisfies Record<keyof typeof shortcuts, unknown>;
-
- useHotkeys(
- shortcuts.esc,
- () => {
- shortcutHandlerMap.esc();
- },
- options,
- []
- );
-
- // Shortcuts from the parent window
- useSubscribe("shortcut", ({ name }) => {
- shortcutHandlerMap[name]();
- });
-};
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index d40a681534..376e741549 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -22,7 +22,6 @@ import * as radixComponentPropsMetas from "@webstudio-is/sdk-components-react-ra
import { hooks as radixComponentHooks } from "@webstudio-is/sdk-components-react-radix/hooks";
import { $publisher, publish } from "~/shared/pubsub";
import { registerContainers, useCanvasStore } from "~/shared/sync";
-import { useCanvasShortcuts } from "./canvas-shortcuts";
import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";
import {
WebstudioComponentCanvas,
@@ -200,8 +199,6 @@ export const Canvas = ({
$publisher.set({ publish });
}, []);
- // e.g. toggling preview is still needed in both modes
- useCanvasShortcuts();
const selectedPage = useStore(selectedPageStore);
useEffect(() => {
diff --git a/apps/builder/app/canvas/shared/use-drag-drop.ts b/apps/builder/app/canvas/shared/use-drag-drop.ts
index 6e53b27148..6df910b678 100644
--- a/apps/builder/app/canvas/shared/use-drag-drop.ts
+++ b/apps/builder/app/canvas/shared/use-drag-drop.ts
@@ -39,6 +39,7 @@ declare module "~/shared/pubsub" {
dragMove: DragMovePayload;
dragStart: DragStartPayload;
dropTargetChange: undefined | ItemDropTarget;
+ cancelCurrentDrag: undefined;
}
}
diff --git a/apps/builder/app/shared/shortcuts/index.ts b/apps/builder/app/shared/shortcuts/index.ts
deleted file mode 100644
index 499b146ee7..0000000000
--- a/apps/builder/app/shared/shortcuts/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./shortcuts";
diff --git a/apps/builder/app/shared/shortcuts/shortcuts.ts b/apps/builder/app/shared/shortcuts/shortcuts.ts
deleted file mode 100644
index a217b04e42..0000000000
--- a/apps/builder/app/shared/shortcuts/shortcuts.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import type { Options } from "react-hotkeys-hook";
-
-export const shortcuts = {
- esc: "esc",
-} as const;
-
-export const options: Options = {
- enableOnFormTags: true,
-};