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 enter (#1369)

Ivan Starkovc037417cde26
diff --git a/apps/builder/app/canvas/shared/use-shortcuts.ts b/apps/builder/app/canvas/shared/use-shortcuts.ts
index 74c77f9e70..d7c9e1cc7b 100644
--- a/apps/builder/app/canvas/shared/use-shortcuts.ts
+++ b/apps/builder/app/canvas/shared/use-shortcuts.ts
@@ -2,6 +2,7 @@ import { useHotkeys } from "react-hotkeys-hook";
import { shortcuts, options } from "~/shared/shortcuts";
import { publish, useSubscribe } from "~/shared/pubsub";
import { isPreviewModeStore } from "~/shared/nano-states";
+import { enterEditingMode } from "~/shared/nano-states/instances";
declare module "~/shared/pubsub" {
export interface PubsubMap {
@@ -27,7 +28,8 @@ export const useShortcuts = () => {
preview: togglePreviewMode,
breakpointsMenu: publishOpenBreakpointsMenu,
esc: publishCancelCurrentDrag,
- } as const;
+ enter: enterEditingMode,
+ } as const satisfies Record<keyof typeof shortcuts, unknown>;
useHotkeys(shortcuts.preview, shortcutHandlerMap.preview, options, []);
@@ -40,6 +42,8 @@ export const useShortcuts = () => {
useHotkeys(shortcuts.esc, shortcutHandlerMap.esc, options, []);
+ useHotkeys(shortcuts.enter, shortcutHandlerMap.enter, {}, []);
+
// Shortcuts from the parent window
useSubscribe("shortcut", ({ name }) => {
shortcutHandlerMap[name]();
diff --git a/apps/builder/app/shared/dom-utils.ts b/apps/builder/app/shared/dom-utils.ts
index ac7502abbc..84eea5f2e8 100644
--- a/apps/builder/app/shared/dom-utils.ts
+++ b/apps/builder/app/shared/dom-utils.ts
@@ -34,7 +34,7 @@ export const getElementByInstanceSelector = (
.map((id) => `[${idAttribute}="${id}"]`)
.reverse()
.join(" ");
- return document.querySelector(domSelector) ?? undefined;
+ return document.querySelector<HTMLElement>(domSelector) ?? undefined;
};
type Rect = {
diff --git a/apps/builder/app/shared/nano-states/instances.ts b/apps/builder/app/shared/nano-states/instances.ts
index 6a0bf67509..4fb8792d8b 100644
--- a/apps/builder/app/shared/nano-states/instances.ts
+++ b/apps/builder/app/shared/nano-states/instances.ts
@@ -1,16 +1,18 @@
import { atom } from "nanostores";
import { getComponentMeta } from "@webstudio-is/react-sdk";
+
import type { InstanceSelector } from "../tree-utils";
import {
selectedInstanceSelectorStore,
selectedInstanceStore,
} from "./nano-states";
+import { getElementByInstanceSelector } from "../dom-utils";
export const textEditingInstanceSelectorStore = atom<
undefined | InstanceSelector
>();
-export const enterEditingMode = (event: KeyboardEvent) => {
+export const enterEditingMode = (event?: KeyboardEvent) => {
const selectedInstanceSelector = selectedInstanceSelectorStore.get();
const selectedInstance = selectedInstanceStore.get();
if (
@@ -19,12 +21,25 @@ export const enterEditingMode = (event: KeyboardEvent) => {
) {
return;
}
+
const meta = getComponentMeta(selectedInstance.component);
- if (meta?.type === "rich-text") {
- // Prevents inserting a newline when entering text-editing mode
- event.preventDefault();
- textEditingInstanceSelectorStore.set(selectedInstanceSelector);
+ if (meta?.type !== "rich-text") {
+ return;
}
+
+ const element = getElementByInstanceSelector(selectedInstanceSelector);
+
+ if (element === undefined) {
+ return;
+ }
+
+ // When an event is triggered from the Builder,
+ // the canvas element may be unfocused, so it's important to focus the element on the canvas.
+ element.focus();
+
+ // Prevents inserting a newline when entering text-editing mode
+ event?.preventDefault();
+ textEditingInstanceSelectorStore.set(selectedInstanceSelector);
};
export const escapeSelection = () => {
diff --git a/apps/builder/app/shared/shortcuts/shortcuts.ts b/apps/builder/app/shared/shortcuts/shortcuts.ts
index ad12a9a553..9b6a174ec6 100644
--- a/apps/builder/app/shared/shortcuts/shortcuts.ts
+++ b/apps/builder/app/shared/shortcuts/shortcuts.ts
@@ -5,13 +5,14 @@ import {
zoomOut,
selectBreakpointByOrderNumber,
} from "../nano-states/breakpoints";
-import { enterEditingMode, escapeSelection } from "../nano-states/instances";
+import { escapeSelection } from "../nano-states/instances";
import { deleteSelectedInstance } from "../instance-utils";
export const shortcuts = {
esc: "esc",
preview: "meta+shift+p, ctrl+shift+p",
breakpointsMenu: "meta+b, ctrl+b",
+ enter: "enter",
} as const;
export const options: Options = {
@@ -91,7 +92,17 @@ export const useSharedShortcuts = () => {
[]
);
- useHotkeys("enter", enterEditingMode, {}, []);
+ /*
+ useHotkeys(
+ "enter",
+ (event) => {
+ console.log("enter", event);
+ enterEditingMode(event);
+ },
+ {},
+ []
+ );
+ */
useHotkeys("esc", escapeSelection, { enableOnContentEditable: true }, []);
};