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: move click canvas event to commands (#2514)

Bogdan Chadkincac813f2b646
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
index 7560e351e9..539f7e88ac 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-popover.tsx
@@ -21,7 +21,6 @@ import {
PopoverMenuItemContainer,
PopoverMenuItemRightSlot,
} from "@webstudio-is/design-system";
-import { useSubscribe } from "~/shared/pubsub";
import { BreakpointsEditor } from "./breakpoints-editor";
import { BreakpointsPopoverToolbarButton } from "./breakpoints-popover-toolbar-button";
import { WidthInput } from "./width-input";
@@ -52,10 +51,6 @@ export const BreakpointsPopover = () => {
const scale = useStore(scaleStore);
const setInitialCanvasWidth = useSetInitialCanvasWidth();
- useSubscribe("clickCanvas", () => {
- $breakpointsMenuView.set(undefined);
- });
-
if (selectedBreakpoint === undefined) {
return null;
}
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/pages.tsx b/apps/builder/app/builder/features/sidebar-left/panels/pages/pages.tsx
index 7ec95d9234..76892c3fe1 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/pages/pages.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/pages.tsx
@@ -21,7 +21,6 @@ import {
PlusIcon,
} from "@webstudio-is/icons";
import type { Page, Pages } from "@webstudio-is/sdk";
-import type { Publish } from "~/shared/pubsub";
import type { TabName } from "../../types";
import { CloseButton, Header } from "../../header";
import { SettingsPanel } from "./settings-panel";
@@ -31,7 +30,6 @@ import { switchPage } from "~/shared/pages";
type TabContentProps = {
onSetActiveTab: (tabName: TabName) => void;
- publish: Publish;
};
type PagesTreeNode =
diff --git a/apps/builder/app/builder/features/sidebar-left/sidebar-left.tsx b/apps/builder/app/builder/features/sidebar-left/sidebar-left.tsx
index 9b6b5388fd..0b32535414 100644
--- a/apps/builder/app/builder/features/sidebar-left/sidebar-left.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/sidebar-left.tsx
@@ -18,6 +18,7 @@ import { AiIcon, BugIcon, HelpIcon } from "@webstudio-is/icons";
import { HelpPopover } from "./help-popover";
import { useStore } from "@nanostores/react";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
+import { $activeSidebarPanel } from "~/builder/shared/nano-states";
const none = { TabContent: () => null };
@@ -27,16 +28,13 @@ type SidebarLeftProps = {
export const SidebarLeft = ({ publish }: SidebarLeftProps) => {
const dragAndDropState = useStore($dragAndDropState);
- const [activeTab, setActiveTab] = useState<TabName>("none");
+ const activeTab = useStore($activeSidebarPanel);
const { TabContent } = activeTab === "none" ? none : panels[activeTab];
const [helpIsOpen, setHelpIsOpen] = useState(false);
const [clientSettings, setClientSetting] = useClientSettings();
- useSubscribe("clickCanvas", () => {
- setActiveTab("none");
- });
useSubscribe("dragEnd", () => {
- setActiveTab("none");
+ $activeSidebarPanel.set("none");
});
const enabledPanels = (Object.keys(panels) as Array<TabName>).filter(
@@ -59,7 +57,9 @@ export const SidebarLeft = ({ publish }: SidebarLeftProps) => {
key={tabName}
value={tabName}
onClick={() => {
- setActiveTab(activeTab !== tabName ? tabName : "none");
+ $activeSidebarPanel.set(
+ activeTab !== tabName ? tabName : "none"
+ );
}}
>
{tabName === "none" ? null : panels[tabName].icon}
@@ -134,7 +134,10 @@ export const SidebarLeft = ({ publish }: SidebarLeftProps) => {
: "visible",
}}
>
- <TabContent publish={publish} onSetActiveTab={setActiveTab} />
+ <TabContent
+ publish={publish}
+ onSetActiveTab={$activeSidebarPanel.set}
+ />
</SidebarTabsContent>
</SidebarTabs>
</Flex>
diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.ts
index 10898cdddd..916dda4d41 100644
--- a/apps/builder/app/builder/shared/commands.ts
+++ b/apps/builder/app/builder/shared/commands.ts
@@ -16,6 +16,7 @@ import { deleteInstance } from "~/shared/instance-utils";
import type { InstanceSelector } from "~/shared/tree-utils";
import { serverSyncStore } from "~/shared/sync";
import { $publisher } from "~/shared/pubsub";
+import { $activeSidebarPanel } from "./nano-states";
const makeBreakpointCommand = <CommandName extends string>(
name: CommandName,
@@ -91,6 +92,13 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
publish?.({ type: "cancelCurrentDrag" });
},
},
+ {
+ name: "clickCanvas",
+ handler: () => {
+ $breakpointsMenuView.set(undefined);
+ $activeSidebarPanel.set("none");
+ },
+ },
// ui
diff --git a/apps/builder/app/builder/shared/nano-states/index.ts b/apps/builder/app/builder/shared/nano-states/index.ts
index 2a1312c752..46cbdf6b62 100644
--- a/apps/builder/app/builder/shared/nano-states/index.ts
+++ b/apps/builder/app/builder/shared/nano-states/index.ts
@@ -1,5 +1,6 @@
import { atom, computed, type WritableAtom } from "nanostores";
import { useStore } from "@nanostores/react";
+import type { TabName } from "~/builder/features/sidebar-left/types";
const useValue = <T>(atom: WritableAtom<T>) => {
const value = useStore(atom);
@@ -34,3 +35,5 @@ export const scaleStore = computed(
);
}
);
+
+export const $activeSidebarPanel = atom<TabName>("none");
diff --git a/apps/builder/app/canvas/instance-selection.ts b/apps/builder/app/canvas/instance-selection.ts
index 589a6b1a1d..f8eb5a8966 100644
--- a/apps/builder/app/canvas/instance-selection.ts
+++ b/apps/builder/app/canvas/instance-selection.ts
@@ -7,13 +7,7 @@ import {
selectedStyleSourceSelectorStore,
} from "~/shared/nano-states";
import { textEditingInstanceSelectorStore } from "~/shared/nano-states";
-import { publish } from "~/shared/pubsub";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- clickCanvas: undefined;
- }
-}
+import { emitCommand } from "./shared/commands";
export const subscribeInstanceSelection = () => {
let pointerDownElement: undefined | Element = undefined;
@@ -49,7 +43,7 @@ export const subscribeInstanceSelection = () => {
if (clickCount === 1) {
// notify whole app about click on document
// e.g. to hide the side panel
- publish({ type: "clickCanvas" });
+ emitCommand("clickCanvas");
}
// prevent selecting instances inside text editor while editing text
diff --git a/apps/builder/app/canvas/shared/commands.ts b/apps/builder/app/canvas/shared/commands.ts
index f223eba320..604cd19827 100644
--- a/apps/builder/app/canvas/shared/commands.ts
+++ b/apps/builder/app/canvas/shared/commands.ts
@@ -19,6 +19,7 @@ import {
export const { emitCommand, subscribeCommands } = createCommandsEmitter({
source: "canvas",
+ externalCommands: ["clickCanvas"],
commands: [
{
name: "editInstanceText",