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.

toolbar plugin to show/hide the toolbar

Oleg Isonen353469ed22a5
diff --git a/app/canvas/features/wrapper-component/editor/editor.tsx b/app/canvas/features/wrapper-component/editor/editor.tsx
index 031ba89eb7..5e387a5497 100644
--- a/app/canvas/features/wrapper-component/editor/editor.tsx
+++ b/app/canvas/features/wrapper-component/editor/editor.tsx
@@ -3,6 +3,7 @@ import {
RichTextPlugin,
HistoryPlugin,
InstancePlugin,
+ ToolbarPlugin,
} from "~/shared/text-editor";
type EditorProps = {
@@ -16,6 +17,7 @@ export const Editor = ({ children, editable }: EditorProps) => {
<RichTextPlugin contentEditable={editable} placeholder="" />
<HistoryPlugin />
<InstancePlugin>{children}</InstancePlugin>
+ <ToolbarPlugin />
</>
);
};
diff --git a/app/canvas/features/wrapper-component/editor/use-is-editing.ts b/app/canvas/features/wrapper-component/editor/use-is-editing.ts
index 615b4ff1e7..7b001e893e 100644
--- a/app/canvas/features/wrapper-component/editor/use-is-editing.ts
+++ b/app/canvas/features/wrapper-component/editor/use-is-editing.ts
@@ -1,9 +1,16 @@
import { type Instance } from "@webstudio-is/sdk";
-import { useEffect, useMemo, useState } from "react";
+import {
+ useEffect,
+ useMemo,
+ useState,
+ type KeyboardEvent,
+ type MouseEvent,
+} from "react";
import { useSelectedInstance } from "~/canvas/shared/nano-states";
type EditableProps = {
- onDoubleClick: () => void;
+ onDoubleClick: (event: MouseEvent) => void;
+ onKeyDown: (event: KeyboardEvent) => void;
};
export const useIsEditing = (instance: Instance): [boolean, EditableProps] => {
@@ -30,11 +37,11 @@ export const useIsEditing = (instance: Instance): [boolean, EditableProps] => {
setIsEditing(false);
}
},
- onDoubleClick() {
+ onDoubleClick(event: MouseEvent) {
// We only want to do this if the component is the one that was clicked
// @todo this logic shouldn't be necessary
if (selectedInstance?.id !== instance.id) return;
-
+ event.preventDefault();
setIsEditing(true);
},
}),
diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsx
index da1a105d6c..544b9dae98 100644
--- a/app/shared/text-editor/example-text-editor.tsx
+++ b/app/shared/text-editor/example-text-editor.tsx
@@ -93,7 +93,7 @@ export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {
/>
<OnChangePlugin onChange={onChange} />
<HistoryPlugin />
- <InstancePlugin children={["Something"]} />
+ <InstancePlugin>{["Something"]}</InstancePlugin>
<TreeViewPlugin />
</div>
</LexicalComposer>
diff --git a/app/shared/text-editor/index.ts b/app/shared/text-editor/index.ts
index 3969201748..f9fa3672ac 100644
--- a/app/shared/text-editor/index.ts
+++ b/app/shared/text-editor/index.ts
@@ -6,4 +6,5 @@ export {
} from "./lexical";
export { config } from "./config";
export { InstancePlugin } from "./plugins/plugin-instance";
+export { ToolbarPlugin } from "./plugins/plugin-toolbar";
export { useContentEditable } from "./content-editable";
diff --git a/app/shared/text-editor/lexical.ts b/app/shared/text-editor/lexical.ts
index b527bf2d75..4529d1e464 100644
--- a/app/shared/text-editor/lexical.ts
+++ b/app/shared/text-editor/lexical.ts
@@ -3,18 +3,22 @@ export type {
EditorState,
LexicalCommand,
LexicalEditor,
+ RangeSelection,
} from "lexical";
export {
+ ElementNode,
TextNode,
RootNode,
createCommand,
$isRangeSelection,
$getSelection,
$getRoot,
- COMMAND_PRIORITY_EDITOR,
$getNodeByKey,
$createTextNode,
$createParagraphNode,
+ COMMAND_PRIORITY_EDITOR,
+ SELECTION_CHANGE_COMMAND,
+ COMMAND_PRIORITY_LOW,
} from "lexical";
export { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
export { LexicalComposer } from "@lexical/react/LexicalComposer";
diff --git a/app/shared/text-editor/plugins/plugin-toolbar.tsx b/app/shared/text-editor/plugins/plugin-toolbar.tsx
new file mode 100644
index 0000000000..ae826f7db9
--- /dev/null
+++ b/app/shared/text-editor/plugins/plugin-toolbar.tsx
@@ -0,0 +1,52 @@
+import { useCallback, useEffect, useRef } from "react";
+import { publish } from "@webstudio-is/sdk";
+
+import {
+ $isRangeSelection,
+ $getSelection,
+ SELECTION_CHANGE_COMMAND,
+ COMMAND_PRIORITY_LOW,
+ useLexicalComposerContext,
+} from "../lexical";
+
+export const ToolbarPlugin = () => {
+ const [editor] = useLexicalComposerContext();
+ const lastSelectionRef = useRef<unknown>();
+
+ const publishSelectionRect = useCallback(() => {
+ const selection = $getSelection();
+ const text = selection?.getTextContent();
+ const nativeSelection = window.getSelection();
+ const isTextSelected =
+ $isRangeSelection(selection) && Boolean(text) && nativeSelection !== null;
+
+ if (isTextSelected) {
+ const domRange = nativeSelection.getRangeAt(0);
+ const rect = domRange.getBoundingClientRect();
+ publish({ type: "selectionRect", payload: rect });
+ lastSelectionRef.current = selection;
+ return;
+ }
+
+ if (lastSelectionRef.current) {
+ // Undefined Rect will hide toolbar
+ publish({ type: "selectionRect", payload: undefined });
+ lastSelectionRef.current = undefined;
+ }
+
+ return true;
+ }, []);
+
+ useEffect(() => {
+ editor.registerCommand(
+ SELECTION_CHANGE_COMMAND,
+ () => {
+ publishSelectionRect();
+ return true;
+ },
+ COMMAND_PRIORITY_LOW
+ );
+ }, [editor, publishSelectionRect]);
+
+ return null;
+};