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
feat: escape to exit preview mode (#5730)
Shrek9570c4e09143
diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.tsindex e36ac09430..7f9f455a3c 100644--- a/apps/builder/app/builder/shared/commands.ts+++ b/apps/builder/app/builder/shared/commands.ts@@ -8,6 +8,7 @@ import { createCommandsEmitter, type Command } from "~/shared/commands-emitter";import {$editingItemSelector,$isDesignMode,+ $isPreviewMode,toggleBuilderMode,$project,} from "~/shared/nano-states";@@ -75,6 +76,15 @@ const makeBreakpointCommand = <CommandName extends string>(},});+const exitPreviewModeFromNonCanvasSource = (source: string) => {+ if (source === "canvas") {+ return;+ }++ setActiveSidebarPanel("auto");+ toggleBuilderMode("preview");+};+export const { emitCommand, subscribeCommands } = createCommandsEmitter({source: "builder",externalCommands: [@@ -99,7 +109,12 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({defaultHotkeys: ["escape"],// radix check event.defaultPrevented before invoking callbackspreventDefault: false,- handler: () => {+ handler: (source) => {+ if ($isPreviewMode.get()) {+ exitPreviewModeFromNonCanvasSource(source);+ return;+ }+const { publish } = $publisher.get();publish?.({ type: "cancelCurrentDrag" });},diff --git a/apps/builder/app/shared/commands-emitter.test.ts b/apps/builder/app/shared/commands-emitter.test.tsnew file mode 100644index 0000000000..3773ed7be4--- /dev/null+++ b/apps/builder/app/shared/commands-emitter.test.ts@@ -0,0 +1,125 @@+import { afterEach, describe, expect, test, vi } from "vitest";+import { $commandMetas, createCommandsEmitter } from "./commands-emitter";++type CommandPayload = {+ source: string;+ name: string;+};++const pubsubMock = vi.hoisted(() => {+ let publisher: { publish?: (action: unknown) => void } = {};+ const subscribers = new Map<+ string,+ Array<(payload: CommandPayload) => void>+ >();++ return {+ getPublisher: () => publisher,+ setPublisher: (next: { publish?: (action: unknown) => void }) => {+ publisher = next;+ },+ subscribe: (type: string, callback: (payload: CommandPayload) => void) => {+ const callbacks = subscribers.get(type) ?? [];+ callbacks.push(callback);+ subscribers.set(type, callbacks);+ return () => {+ subscribers.set(+ type,+ (subscribers.get(type) ?? []).filter((item) => item !== callback)+ );+ };+ },+ emit: (type: string, payload: CommandPayload) => {+ for (const callback of subscribers.get(type) ?? []) {+ callback(payload);+ }+ },+ reset: () => {+ publisher = {};+ subscribers.clear();+ },+ };+});++vi.mock("~/shared/pubsub", () => ({+ $publisher: {+ get: pubsubMock.getPublisher,+ set: pubsubMock.setPublisher,+ },+ subscribe: pubsubMock.subscribe,+}));++vi.mock("~/shared/sync/sync-stores", () => ({+ clientSyncStore: {+ register: vi.fn(),+ createTransaction: vi.fn(+ (+ stores: Array<{ get: () => unknown }>,+ callback: (value: unknown) => void+ ) => {+ callback(stores[0].get());+ }+ ),+ },+}));++const createTestCommand = (handler: (source: string) => void = vi.fn()) =>+ createCommandsEmitter({+ source: "builder",+ commands: [+ {+ name: "testCommand",+ handler,+ },+ ],+ });++describe("createCommandsEmitter", () => {+ afterEach(() => {+ pubsubMock.reset();+ $commandMetas.set(new Map());+ });++ test("passes own source to handlers when emitting directly", () => {+ const calls: string[] = [];+ const { emitCommand } = createTestCommand((source) => {+ calls.push(source);+ });++ emitCommand("testCommand");++ expect(calls).toEqual(["builder"]);+ });++ test("publishes commands with own source", () => {+ const publish = vi.fn();+ pubsubMock.setPublisher({ publish });+ const { emitCommand } = createTestCommand();++ emitCommand("testCommand");++ expect(publish).toHaveBeenCalledWith({+ type: "command",+ payload: {+ source: "builder",+ name: "testCommand",+ },+ });+ });++ test("passes published source to subscribed command handlers", () => {+ const calls: string[] = [];+ const { subscribeCommands } = createTestCommand((source) => {+ calls.push(source);+ });++ const unsubscribe = subscribeCommands();+ pubsubMock.emit("command", {+ source: "canvas",+ name: "testCommand",+ });+ unsubscribe();++ expect(calls).toEqual(["canvas"]);+ });+});diff --git a/apps/builder/app/shared/commands-emitter.ts b/apps/builder/app/shared/commands-emitter.tsindex c617ce0d26..b7c2905659 100644--- a/apps/builder/app/shared/commands-emitter.ts+++ b/apps/builder/app/shared/commands-emitter.ts@@ -30,7 +30,7 @@ export type CommandMeta<CommandName extends string> = {keepCommandPanelOpen?: boolean;};-type CommandHandler = () => void;+type CommandHandler = (source: string) => void;/*** Command can be registered by builder, canvas or plugin@@ -124,7 +124,7 @@ export const createCommandsEmitter = <CommandName extends string>({},});} else {- commandHandlers.get(name)?.();+ commandHandlers.get(name)?.(source);}};@@ -143,8 +143,8 @@ export const createCommandsEmitter = <CommandName extends string>({});}- const unsubscribePubsub = subscribe("command", ({ name }) => {- commandHandlers.get(name)?.();+ const unsubscribePubsub = subscribe("command", ({ name, source }) => {+ commandHandlers.get(name)?.(source);});const handleKeyDown = (event: KeyboardEvent) => {let emitted = false;diff --git a/apps/builder/docs/test-cases.md b/apps/builder/docs/test-cases.mdindex b47e5f4d8c..15f2d12fa5 100644--- a/apps/builder/docs/test-cases.md+++ b/apps/builder/docs/test-cases.md@@ -193,3 +193,12 @@- The name should change in the pages list as well (with a small delay)- Change the page path- Reload browser tab and open the page settings again and make sure your changes are persisted++1. Preview mode++ - Enter preview mode from the top bar+ - Press `escape` while focus is outside the canvas+ - Check that preview mode exits and the builder UI is shown+ - Enter preview mode again+ - Press `escape` while interacting with the canvas+ - Check that preview mode stays active and the canvas handles the key press