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 text toolbar state (#1314)

Bogdan Chadkin1d6292746231
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
index f159eaefda..d598566983 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
@@ -11,7 +11,7 @@ import {
instancesStore,
} from "~/shared/nano-states";
import { HoveredInstanceOutline, SelectedInstanceOutline } from "./outline";
-import { useSubscribeTextToolbar, TextToolbar } from "./text-toolbar";
+import { TextToolbar } from "./text-toolbar";
import { useSubscribeInstanceRect } from "./hooks/use-subscribe-instance-rect";
import { useSubscribeSwitchPage } from "~/shared/pages";
import { Label } from "./outline/label";
@@ -34,7 +34,6 @@ type CanvasToolsProps = {
export const CanvasTools = ({ publish }: CanvasToolsProps) => {
// @todo try to setup cross-frame atoms to vaoid this
useSubscribeInstanceRect();
- useSubscribeTextToolbar();
useSubscribeScrollState();
useSubscribeDragAndDropState();
useSubscribeSwitchPage();
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
index b888168b3d..95845235df 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
@@ -1,12 +1,7 @@
import { useRef, useEffect } from "react";
import { useStore } from "@nanostores/react";
import { computePosition, flip, offset, shift } from "@floating-ui/dom";
-import type { Publish } from "~/shared/pubsub";
-import {
- type TextToolbarState,
- useTextToolbarState,
-} from "~/builder/shared/nano-states";
-import { Flex, IconButton, Tooltip } from "@webstudio-is/design-system";
+import { theme, Flex, IconButton, Tooltip } from "@webstudio-is/design-system";
import {
FontBoldIcon,
FontItalicIcon,
@@ -16,9 +11,12 @@ import {
BrushIcon,
CrossSmallIcon,
} from "@webstudio-is/icons";
-import { useSubscribe } from "~/shared/pubsub";
-import { theme } from "@webstudio-is/design-system";
import { selectedInstanceSelectorStore } from "~/shared/nano-states";
+import {
+ TextToolbarState,
+ textToolbarStore,
+} from "~/shared/nano-states/canvas";
+import type { Publish } from "~/shared/pubsub";
type Format =
| "bold"
@@ -31,18 +29,10 @@ type Format =
declare module "~/shared/pubsub" {
export interface PubsubMap {
- showTextToolbar: TextToolbarState;
- hideTextToolbar: void;
formatTextToolbar: Format;
}
}
-export const useSubscribeTextToolbar = () => {
- const [, setTextToolbar] = useTextToolbarState();
- useSubscribe("showTextToolbar", setTextToolbar);
- useSubscribe("hideTextToolbar", () => setTextToolbar(undefined));
-};
-
const getRectForRelativeRect = (parent: DOMRect, rel: DOMRect) => {
return {
x: parent.x + rel.x,
@@ -65,6 +55,9 @@ const Toolbar = ({ state, onToggle }: ToolbarProps) => {
const rootRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
+ if (state.selectionRect === undefined) {
+ return;
+ }
if (rootRef.current?.parentElement) {
const floating = rootRef.current;
const parent = rootRef.current.parentElement;
@@ -190,10 +183,13 @@ type TextToolbarProps = {
};
export const TextToolbar = ({ publish }: TextToolbarProps) => {
- const [textToolbar] = useTextToolbarState();
+ const textToolbar = useStore(textToolbarStore);
const selectedInstanceSelector = useStore(selectedInstanceSelectorStore);
- if (textToolbar == null || selectedInstanceSelector === undefined) {
+ if (
+ textToolbar?.selectionRect === undefined ||
+ selectedInstanceSelector === undefined
+ ) {
return null;
}
diff --git a/apps/builder/app/builder/shared/nano-states/index.ts b/apps/builder/app/builder/shared/nano-states/index.ts
index a83da1b79c..c0c1ddded1 100644
--- a/apps/builder/app/builder/shared/nano-states/index.ts
+++ b/apps/builder/app/builder/shared/nano-states/index.ts
@@ -23,16 +23,4 @@ export const useCanvasRect = () => useValue(canvasRectContainer);
export const projectContainer = atom<Project | undefined>();
export const useProject = () => useValue(projectContainer);
-export type TextToolbarState = {
- selectionRect: DOMRect;
- isBold: boolean;
- isItalic: boolean;
- isSuperscript: boolean;
- isSubscript: boolean;
- isLink: boolean;
- isSpan: boolean;
-};
-const textToolbarState = atom<undefined | TextToolbarState>();
-export const useTextToolbarState = () => useValue(textToolbarState);
-
export const isCanvasPointerEventsEnabledStore = atom<boolean>(true);
diff --git a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
index f18e503461..af8153558e 100644
--- a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
+++ b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
@@ -1,17 +1,17 @@
-import { useState } from "react";
+import { useStore } from "@nanostores/react";
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import type { ComponentStory, ComponentMeta } from "@storybook/react";
import { action } from "@storybook/addon-actions";
import { Box } from "@webstudio-is/design-system";
-import { useSubscribe, publish } from "~/shared/pubsub";
-import type { TextToolbarState } from "~/builder/shared/nano-states";
-import { TextEditor } from "./text-editor";
import { theme } from "@webstudio-is/design-system";
import type {
Instance,
Instances,
InstancesItem,
} from "@webstudio-is/project-build";
+import { publish } from "~/shared/pubsub";
+import { textToolbarStore } from "~/shared/nano-states/canvas";
+import { TextEditor } from "./text-editor";
export default {
component: TextEditor,
@@ -59,13 +59,7 @@ const instances: Instances = new Map([
]);
export const Basic: ComponentStory<typeof TextEditor> = ({ onChange }) => {
- const [state, setState] = useState<null | TextToolbarState>(null);
- useSubscribe("showTextToolbar", (event) => {
- setState(event);
- });
- useSubscribe("hideTextToolbar", () => {
- setState(null);
- });
+ const state = useStore(textToolbarStore);
const setFormat = (type: Format) => {
publish({ type: "formatTextToolbar", payload: type });
diff --git a/apps/builder/app/canvas/features/text-editor/toolbar-connector.tsx b/apps/builder/app/canvas/features/text-editor/toolbar-connector.tsx
index 8d25061c03..e99f4dc2ad 100644
--- a/apps/builder/app/canvas/features/text-editor/toolbar-connector.tsx
+++ b/apps/builder/app/canvas/features/text-editor/toolbar-connector.tsx
@@ -13,7 +13,9 @@ import { $getNearestNodeOfType } from "@lexical/utils";
import { $patchStyleText } from "@lexical/selection";
import { LinkNode, TOGGLE_LINK_COMMAND } from "@lexical/link";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
-import { useSubscribe, publish } from "~/shared/pubsub";
+import { useSubscribe } from "~/shared/pubsub";
+import { textToolbarStore } from "~/shared/nano-states/canvas";
+import { subscribeScrollState } from "~/shared/dom-hooks";
const spanTriggerName = "--style-node-trigger";
@@ -80,6 +82,15 @@ const $isSelectedLink = (selection: RangeSelection) => {
return $getNearestNodeOfType(selectedNode, LinkNode) != null;
};
+const getSelectionClienRect = () => {
+ const nativeSelection = window.getSelection();
+ if (nativeSelection === null) {
+ return;
+ }
+ const domRange = nativeSelection.getRangeAt(0);
+ return domRange.getBoundingClientRect();
+};
+
export const ToolbarConnectorPlugin = ({
onSelectNode,
}: {
@@ -91,38 +102,57 @@ export const ToolbarConnectorPlugin = ({
// control toolbar state on data or selection updates
const updateToolbar = useCallback(() => {
const selection = $getSelection();
- const nativeSelection = window.getSelection();
if (
$isRangeSelection(selection) &&
selection.getTextContent().length !== 0 &&
- nativeSelection != null &&
isMouseDownRef.current === false
) {
- const domRange = nativeSelection.getRangeAt(0);
- const selectionRect = domRange.getBoundingClientRect();
+ const selectionRect = getSelectionClienRect();
const isBold = selection.hasFormat("bold");
const isItalic = selection.hasFormat("italic");
const isSuperscript = selection.hasFormat("superscript");
const isSubscript = selection.hasFormat("subscript");
const isLink = $isSelectedLink(selection);
const isSpan = $getSpanNodes(selection).length !== 0;
- publish({
- type: "showTextToolbar",
- payload: {
- selectionRect,
- isBold,
- isItalic,
- isSuperscript,
- isSubscript,
- isLink,
- isSpan,
- },
+ textToolbarStore.set({
+ selectionRect,
+ isBold,
+ isItalic,
+ isSuperscript,
+ isSubscript,
+ isLink,
+ isSpan,
});
} else {
- publish({ type: "hideTextToolbar" });
+ textToolbarStore.set(undefined);
}
}, []);
+ useEffect(() => {
+ return subscribeScrollState({
+ onScrollStart: () => {
+ // hide toolbar on scroll start preserving all data
+ const textToolbar = textToolbarStore.get();
+ if (textToolbar) {
+ textToolbarStore.set({
+ ...textToolbar,
+ selectionRect: undefined,
+ });
+ }
+ },
+ onScrollEnd: () => {
+ // restore toolbar with new position
+ const textToolbar = textToolbarStore.get();
+ if (textToolbar) {
+ textToolbarStore.set({
+ ...textToolbar,
+ selectionRect: getSelectionClienRect(),
+ });
+ }
+ },
+ });
+ }, []);
+
// prevent showing toolbar when select with mouse
useEffect(() => {
const onMouseDown = () => {
@@ -146,7 +176,7 @@ export const ToolbarConnectorPlugin = ({
useEffect(() => {
// hide toolbar when editor is unmounted
return () => {
- publish({ type: "hideTextToolbar" });
+ textToolbarStore.set(undefined);
};
}, []);
diff --git a/apps/builder/app/shared/nano-states/canvas.ts b/apps/builder/app/shared/nano-states/canvas.ts
new file mode 100644
index 0000000000..5eced90969
--- /dev/null
+++ b/apps/builder/app/shared/nano-states/canvas.ts
@@ -0,0 +1,17 @@
+import { atom } from "nanostores";
+
+export type TextToolbarState = {
+ selectionRect: undefined | DOMRect;
+ isBold: boolean;
+ isItalic: boolean;
+ isSuperscript: boolean;
+ isSubscript: boolean;
+ isLink: boolean;
+ isSpan: boolean;
+};
+
+export const textToolbarStore = atom<undefined | TextToolbarState>(undefined);
+
+export const synchronizedCanvasStores = [
+ ["textToolbar", textToolbarStore],
+] as const;
diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.ts
index fbfabf17ae..7c681fcb9c 100644
--- a/apps/builder/app/shared/sync/sync-stores.ts
+++ b/apps/builder/app/shared/sync/sync-stores.ts
@@ -21,6 +21,7 @@ import {
} from "~/shared/nano-states";
import { synchronizedBreakpointsStores } from "~/shared/nano-states/breakpoints";
import { synchronizedInstancesStores } from "~/shared/nano-states/instances";
+import { synchronizedCanvasStores } from "~/shared/nano-states/canvas";
enableMapSet();
@@ -75,6 +76,9 @@ export const registerContainers = () => {
for (const [name, store] of synchronizedInstancesStores) {
clientStores.set(name, store);
}
+ for (const [name, store] of synchronizedCanvasStores) {
+ clientStores.set(name, store);
+ }
};
const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {