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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: migrate instances to commands (#2397)
Bogdan Chadkin40deaedd7a3f
diff --git a/apps/builder/app/builder/features/topbar/menu/menu.tsx b/apps/builder/app/builder/features/topbar/menu/menu.tsxindex 1455a81461..03f904c2ad 100644--- a/apps/builder/app/builder/features/topbar/menu/menu.tsx+++ b/apps/builder/app/builder/features/topbar/menu/menu.tsx@@ -29,7 +29,6 @@ import {import { useClientSettings } from "~/builder/shared/client-settings";import { dashboardPath } from "~/shared/router-utils";import { $authPermit } from "~/shared/nano-states";-import { deleteSelectedInstance } from "~/shared/instance-utils";import { emitCommand } from "~/builder/shared/commands";import { MenuButton } from "./menu-button";@@ -159,7 +158,7 @@ export const Menu = () => {</DropdownMenuItem>*/}- <DropdownMenuItem onSelect={deleteSelectedInstance}>+ <DropdownMenuItem onSelect={() => emitCommand("deleteInstance")}>Delete<DropdownMenuItemRightSlot><ShortcutHint value={["backspace"]} />diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.tsindex 9943b9ece5..5f23b326ef 100644--- a/apps/builder/app/builder/shared/commands.ts+++ b/apps/builder/app/builder/shared/commands.ts@@ -1,9 +1,15 @@import { createCommandsEmitter, type Command } from "~/shared/commands-emitter";-import { $isPreviewMode } from "~/shared/nano-states";+import {+ $isPreviewMode,+ editingItemIdStore,+ selectedInstanceSelectorStore,+} from "~/shared/nano-states";import {$breakpointsMenuView,selectBreakpointByOrder,} from "~/shared/breakpoints";+import { onCopy, onPaste } from "~/shared/copy-paste/plugin-instance";+import { deleteSelectedInstance } from "~/shared/instance-utils";const makeBreakpointCommand = <CommandName extends string>(name: CommandName,@@ -20,6 +26,8 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({source: "builder",externalCommands: ["editInstanceText"],commands: [+ // ui+{name: "togglePreview",defaultHotkeys: ["meta+shift+p", "ctrl+shift+p"],@@ -43,5 +51,38 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({makeBreakpointCommand("selectBreakpoint7", 7),makeBreakpointCommand("selectBreakpoint8", 8),makeBreakpointCommand("selectBreakpoint9", 9),++ // instances++ {+ name: "deleteInstance",+ defaultHotkeys: ["backspace", "delete"],+ disableHotkeyOnContentEditable: true,+ // this disables hotkey for inputs on style panel+ // but still work for input on canvas which call event.preventDefault() in keydown handler+ disableHotkeyOnFormTags: true,+ handler: () => {+ deleteSelectedInstance();+ },+ },+ {+ name: "duplicateInstance",+ defaultHotkeys: ["meta+d", "ctrl+d"],+ handler: () => {+ onPaste(onCopy() ?? "");+ },+ },+ {+ name: "editInstanceLabel",+ defaultHotkeys: ["meta+e", "ctrl+e"],+ handler: () => {+ const selectedInstanceSelector = selectedInstanceSelectorStore.get();+ if (selectedInstanceSelector === undefined) {+ return;+ }+ const [targetInstanceId] = selectedInstanceSelector;+ editingItemIdStore.set(targetInstanceId);+ },+ },],});diff --git a/apps/builder/app/shared/commands-emitter.ts b/apps/builder/app/shared/commands-emitter.tsindex b87635fa15..57b799c12b 100644--- a/apps/builder/app/shared/commands-emitter.ts+++ b/apps/builder/app/shared/commands-emitter.ts@@ -6,10 +6,21 @@ import { $publisher, subscribe } from "~/shared/pubsub";type CommandMeta<CommandName extends string> = {// @todo category, descriptionname: CommandName;- // default because hotkeys can be customized from ui+ /** default because hotkeys can be customized from ui */defaultHotkeys?: string[];- // listen hotkeys only locally without sharing with other apps+ /** listen hotkeys only locally without sharing with other apps */disableHotkeyOutsideApp?: boolean;+ /**+ * input, select and textarea will not invoke command when hotkey is hit+ * with the exception when default event behavior is prevented+ **/+ disableHotkeyOnFormTags?: boolean;+ /**+ * element with contenteditable=true will not invoke command+ * when hotkey is hit with the exception when default+ * event behavior is prevented+ **/+ disableHotkeyOnContentEditable?: boolean;};type CommandHandler = () => void;@@ -98,6 +109,30 @@ export const createCommandsEmitter = <CommandName extends string>({) {continue;}+ const element = event.target as HTMLElement;+ const tagName = element.tagName.toLowerCase();+ const isOnFormTags = ["input", "select", "textarea"].includes(tagName);+ const isOnContentEditable = element.isContentEditable;+ const { disableHotkeyOnFormTags, disableHotkeyOnContentEditable } =+ commandMeta;+ // in some cases hotkey override default behavior+ // on form tags and contentEditable+ // though still proceed when default behavior is prevented+ // this hack makes hotkeys work on canvas instances of input etc.+ if (+ isOnFormTags &&+ disableHotkeyOnFormTags &&+ event.defaultPrevented === false+ ) {+ continue;+ }+ if (+ isOnContentEditable &&+ disableHotkeyOnContentEditable &&+ event.defaultPrevented === false+ ) {+ continue;+ }emitted = true;emitCommand(commandMeta.name as CommandName);}diff --git a/apps/builder/app/shared/shortcuts/shortcuts.ts b/apps/builder/app/shared/shortcuts/shortcuts.tsindex 3d84c3dcb4..ae2dba2c0c 100644--- a/apps/builder/app/shared/shortcuts/shortcuts.ts+++ b/apps/builder/app/shared/shortcuts/shortcuts.ts@@ -1,11 +1,5 @@import { type Options, useHotkeys } from "react-hotkeys-hook";import store from "immerhin";-import { deleteSelectedInstance } from "../instance-utils";-import {- editingItemIdStore,- selectedInstanceSelectorStore,-} from "../nano-states";-import { onCopy, onPaste } from "../copy-paste/plugin-instance";export const shortcuts = {esc: "esc",@@ -43,41 +37,4 @@ export const useSharedShortcuts = ({},[]);-- useHotkeys(- "backspace, delete",- deleteSelectedInstance,- {- // When in builder, we don't want to delete instances,- // when user edits text in a control on style panel etc.- // But when a form control on canvas has focus, we want the shortcut to work.- enableOnFormTags: source === "canvas",- enableOnContentEditable: false,- },- []- );-- useHotkeys(- "meta+d, ctrl+d",- (event) => {- event.preventDefault();- onPaste(onCopy() ?? "");- },- {},- []- );-- useHotkeys(- "meta+e, 'ctrl+e",- () => {- const selectedInstanceSelector = selectedInstanceSelectorStore.get();- if (selectedInstanceSelector === undefined) {- return;- }- const targetInstanceId = selectedInstanceSelector[0];- editingItemIdStore.set(targetInstanceId);- },- {},- []- );};