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
wip moving text toolbar to the main document
Oleg Isonenf396aaac585f
diff --git a/app/canvas/features/wrapper-component/editable/index.ts b/app/canvas/features/wrapper-component/editable/index.tsindex c40a3842d6..7847a93944 100644--- a/app/canvas/features/wrapper-component/editable/index.ts+++ b/app/canvas/features/wrapper-component/editable/index.ts@@ -1,2 +1 @@export * from "./use-content-editable";-export * from "./use-toolbar";diff --git a/app/canvas/features/wrapper-component/editable/use-toolbar.tsx b/app/canvas/features/wrapper-component/editable/use-toolbar.tsxindex 12eeb05a76..ac9c3858b4 100644--- a/app/canvas/features/wrapper-component/editable/use-toolbar.tsx+++ b/app/canvas/features/wrapper-component/editable/use-toolbar.tsx@@ -1,87 +1,11 @@-import { useState, useEffect, type MouseEventHandler } from "react";+import { useState, useEffect } from "react";import debounce from "lodash.debounce";-import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";-import {- Portal,- ToggleGroup,- darkTheme,- type CSS,-} from "~/shared/design-system";-import { FontBoldIcon, FontItalicIcon, Link2Icon } from "~/shared/icons";import { type Edit } from "./use-editable";+import { publish, useSubscribe } from "@webstudio-is/sdk";+import { useSelectedElement } from "~/canvas/shared/nano-states";-export type Value = "b" | "i" | "a";--type ToolbarProps = {- css?: CSS;- onValueChange: (value: Value) => void;- rootRef: React.Ref<HTMLDivElement>;-};--const onClickPreventDefault: MouseEventHandler<HTMLDivElement> = (event) => {- event.preventDefault();-};--const Toolbar = ({ css, onValueChange, rootRef }: ToolbarProps) => {- return (- <Portal.Root className={darkTheme}>- <ToggleGroup.Root- ref={rootRef}- type="single"- onValueChange={onValueChange}- onClick={onClickPreventDefault}- css={{- position: "absolute",- borderRadius: "$1",- padding: "$1 $2",- ...css,- }}- >- <ToggleGroup.Item value="b">- <FontBoldIcon />- </ToggleGroup.Item>- <ToggleGroup.Item value="i">- <FontItalicIcon />- </ToggleGroup.Item>- <ToggleGroup.Item value="a">- <Link2Icon />- </ToggleGroup.Item>- </ToggleGroup.Root>- </Portal.Root>- );-};--const getPlacement = ({- rootRect,- rangeRect,-}: {- rootRect?: DOMRect;- rangeRect: DOMRect;-}) => {- let align = "top";- let left = rangeRect.x + rangeRect.width / 2;- // We measure the size in a hidden state after we render the menu,- // then show it- let visibility = "hidden";- if (rootRect !== undefined) {- visibility = "visible";- // Prevent going further than left 0- left = Math.max(left, rootRect.width / 2);- // Prevent going further than window width- left = Math.min(left, window.innerWidth - rootRect.width / 2);- align = rangeRect.y > rootRect.height ? "top" : "bottom";- }-- const marginBottom = align === "bottom" ? "-$5" : 0;- const marginTop = align === "bottom" ? 0 : "-$5";- const transform = "translateX(-50%)";- let top =- align === "top"- ? Math.max(rangeRect.y - rangeRect.height, 0)- : Math.max(rangeRect.y + rangeRect.height);- top += window.pageYOffset;- return { top, left, marginBottom, marginTop, transform, visibility };-};+// @todo dedupe with text-toolbar.tsx+type Value = "b" | "i" | "a";type UseToolbarProps<Item> = {editable?: Edit<Item>;@@ -92,15 +16,13 @@ export const useToolbar = <Item extends Record<string, unknown>>({editable,onInsert,}: UseToolbarProps<Item>) => {- const [rangeRect, setRangeRect] = useState<DOMRect | undefined>();+ const [selectionRect, setRangeRect] = useState<DOMRect | undefined>();const [selectedText, setSelectedText] = useState<string | undefined>();- const [placement, setPlacement] = useState<CSS | undefined>();- const [element, setElement] = useState<HTMLElement | null>(null);+ const [selectedElement] = useSelectedElement();const cleanup = () => {setSelectedText(undefined);setRangeRect(undefined);- setPlacement(undefined);};useEffect(() => {@@ -128,25 +50,16 @@ export const useToolbar = <Item extends Record<string, unknown>>({};}, [editable]);- useLayoutEffect(() => {- if (element === null || rangeRect === undefined) {- return;- }- const rootRect = element.getBoundingClientRect();- const placement = getPlacement({ rootRect, rangeRect });- setPlacement(placement);- }, [rangeRect, element]);-- if (rangeRect === undefined) return null;+ useEffect(() => {+ publish({ type: "selectionRect", payload: selectionRect });+ }, [selectionRect]);- return (- <Toolbar- rootRef={setElement}- css={placement}- onValueChange={(type) => {- if (selectedText === undefined) return;- onInsert({ type, text: selectedText });- }}- />+ useSubscribe<"insertInlineInstance", Value>(+ "insertInlineInstance",+ (type) => {+ if (selectedText === undefined) return;+ console.log("selected text", selectedText);+ onInsert({ type, text: selectedText });+ });};diff --git a/app/designer/features/workspace/canvas-tools/canvas-tools.tsx b/app/designer/features/workspace/canvas-tools/canvas-tools.tsxindex 70f1d3c4cf..831cc4ef93 100644--- a/app/designer/features/workspace/canvas-tools/canvas-tools.tsx+++ b/app/designer/features/workspace/canvas-tools/canvas-tools.tsx@@ -1,7 +1,10 @@+import { type Publish } from "@webstudio-is/sdk";import { Box } from "~/shared/design-system";import { useIsPreviewMode } from "~/shared/nano-states";import { HoveredInstanceOutline, SelectedInstanceOutline } from "./outline";-import { useSubscribeInstanceRect } from "./use-subscribe-instance-rect";+import { TextToolbar } from "./text-toolbar";+import { useSubscribeInstanceRect } from "./hooks/use-subscribe-instance-rect";+import { useSubscribeSelectionRect } from "./hooks/use-subscribe-selection-rect";const toolsStyle = {position: "absolute",@@ -13,14 +16,20 @@ const toolsStyle = {overflow: "hidden",};-export const CanvasTools = () => {+type CanvasToolsProps = {+ publish: Publish;+};++export const CanvasTools = ({ publish }: CanvasToolsProps) => {useSubscribeInstanceRect();+ useSubscribeSelectionRect();const [isPreviewMode] = useIsPreviewMode();if (isPreviewMode) return null;return (<Box css={toolsStyle}><SelectedInstanceOutline /><HoveredInstanceOutline />+ <TextToolbar publish={publish} /></Box>);};diff --git a/app/designer/features/workspace/canvas-tools/use-subscribe-instance-rect.ts b/app/designer/features/workspace/canvas-tools/hooks/use-subscribe-instance-rect.tssimilarity index 100%rename from app/designer/features/workspace/canvas-tools/use-subscribe-instance-rect.tsrename to app/designer/features/workspace/canvas-tools/hooks/use-subscribe-instance-rect.tsdiff --git a/app/designer/features/workspace/canvas-tools/hooks/use-subscribe-selection-rect.ts b/app/designer/features/workspace/canvas-tools/hooks/use-subscribe-selection-rect.tsnew file mode 100644index 0000000000..533b4229fd--- /dev/null+++ b/app/designer/features/workspace/canvas-tools/hooks/use-subscribe-selection-rect.ts@@ -0,0 +1,7 @@+import { useSubscribe } from "@webstudio-is/sdk";+import { useSelectionRect } from "~/designer/shared/nano-states";++export const useSubscribeSelectionRect = () => {+ const [, setSelectionRect] = useSelectionRect();+ useSubscribe<"selectionRect", DOMRect>("selectionRect", setSelectionRect);+};diff --git a/app/designer/features/workspace/canvas-tools/outline/label.tsx b/app/designer/features/workspace/canvas-tools/outline/label.tsxindex bf6add852b..77ce312846 100644--- a/app/designer/features/workspace/canvas-tools/outline/label.tsx+++ b/app/designer/features/workspace/canvas-tools/outline/label.tsx@@ -50,6 +50,7 @@ const LabelContainer = styled(minWidth: "$6",whiteSpace: "nowrap",backgroundColor: "$blue9",+ pointerEvents: "auto",},{variants: {diff --git a/app/designer/features/workspace/canvas-tools/text-toolbar/index.ts b/app/designer/features/workspace/canvas-tools/text-toolbar/index.tsnew file mode 100644index 0000000000..3b4edfd2cd--- /dev/null+++ b/app/designer/features/workspace/canvas-tools/text-toolbar/index.ts@@ -0,0 +1 @@+export * from "./text-toolbar";diff --git a/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx b/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsxnew file mode 100644index 0000000000..1f5d07b37d--- /dev/null+++ b/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx@@ -0,0 +1,107 @@+import { type Publish } from "@webstudio-is/sdk";+import { useMemo, useState, type MouseEventHandler } from "react";+import { useSelectionRect } from "~/designer/shared/nano-states";+import { ToggleGroup, type CSS } from "~/shared/design-system";+import { FontBoldIcon, FontItalicIcon, Link2Icon } from "~/shared/icons";++const getPlacement = ({+ toolbarRect,+ selectionRect,+}: {+ toolbarRect?: DOMRect;+ selectionRect: DOMRect;+}) => {+ let align = "top";+ let left = selectionRect.x + selectionRect.width / 2;+ // We measure the size in a hidden state after we render the menu,+ // then show it+ let visibility = "hidden";+ if (toolbarRect !== undefined) {+ visibility = "visible";+ // Prevent going further than left 0+ left = Math.max(left, toolbarRect.width / 2);+ // Prevent going further than window width+ left = Math.min(left, window.innerWidth - toolbarRect.width / 2);+ align = selectionRect.y > toolbarRect.height ? "top" : "bottom";+ }++ const marginBottom = align === "bottom" ? "-$5" : 0;+ const marginTop = align === "bottom" ? 0 : "-$5";+ const transform = "translateX(-50%)";+ let top =+ align === "top"+ ? Math.max(selectionRect.y - selectionRect.height, 0)+ : Math.max(selectionRect.y + selectionRect.height);+ // @todo this has to come from canvas+ top += window.pageYOffset;+ return { top, left, marginBottom, marginTop, transform, visibility };+};++// @todo dedupe with use-toolbar.tsx+type Value = "b" | "i" | "a";++const onClickPreventDefault: MouseEventHandler<HTMLDivElement> = (event) => {+ console.log(event);+ event.preventDefault();+};++type ToolbarProps = {+ css?: CSS;+ onValueChange: (value: Value) => void;+ rootRef: React.Ref<HTMLDivElement>;+};++const Toolbar = ({ css, onValueChange, rootRef }: ToolbarProps) => {+ return (+ <ToggleGroup.Root+ ref={rootRef}+ type="single"+ onValueChange={onValueChange}+ onClick={onClickPreventDefault}+ css={{+ position: "absolute",+ borderRadius: "$1",+ padding: "$1 $2",+ pointerEvents: "auto",+ ...css,+ }}+ >+ <ToggleGroup.Item value="b">+ <FontBoldIcon style={{ pointerEvents: "auto" }} />+ </ToggleGroup.Item>+ <ToggleGroup.Item value="i">+ <FontItalicIcon />+ </ToggleGroup.Item>+ <ToggleGroup.Item value="a">+ <Link2Icon />+ </ToggleGroup.Item>+ </ToggleGroup.Root>+ );+};++type TextToolbarProps = {+ publish: Publish;+};++export const TextToolbar = ({ publish }: TextToolbarProps) => {+ const [selectionRect] = useSelectionRect();+ const [element, setElement] = useState<HTMLElement | null>(null);+ console.log({ selectionRect });+ const placement = useMemo(() => {+ if (selectionRect === undefined || element === null) return;+ const toolbarRect = element.getBoundingClientRect();+ return getPlacement({ toolbarRect, selectionRect });+ }, [selectionRect, element]);++ if (selectionRect === undefined) return null;++ return (+ <Toolbar+ rootRef={setElement}+ css={placement}+ onValueChange={(type) => {+ publish({ type: "insertInlineInstance", payload: type });+ }}+ />+ );+};diff --git a/app/designer/features/workspace/workspace.tsx b/app/designer/features/workspace/workspace.tsxindex 5966133edc..fdfa7f3663 100644--- a/app/designer/features/workspace/workspace.tsx+++ b/app/designer/features/workspace/workspace.tsx@@ -51,7 +51,7 @@ export const Workspace = ({><Box css={canvasContainerStyle} style={{ width: canvasWidth }}>{children}- <CanvasTools />+ <CanvasTools publish={publish} /></Box></Flex></Box>diff --git a/app/designer/shared/nano-states/index.ts b/app/designer/shared/nano-states/index.tsindex a5bde7cb82..96635904c9 100644--- a/app/designer/shared/nano-states/index.ts+++ b/app/designer/shared/nano-states/index.ts@@ -42,3 +42,6 @@ export const useCanvasRect = () => useValue(canvasRectContainer);const syncStatusContainer = createValueContainer<SyncStatus>("idle");export const useSyncStatus = () => useValue(syncStatusContainer);++const selectionRectContainer = createValueContainer<DOMRect | undefined>();+export const useSelectionRect = () => useValue(selectionRectContainer);