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.

first working commands from toolbar go through and maintain caret position

Oleg Isonenb594009cdf30
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.tsx
index 3c81ab7c09..d1e4689de9 100644
--- a/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
+++ b/app/designer/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
@@ -3,6 +3,7 @@ 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";
+import { createInstance } from "~/shared/tree-utils";
const getPlacement = ({
toolbarRect,
@@ -37,11 +38,11 @@ const getPlacement = ({
return { top, left, marginBottom, marginTop, transform, visibility };
};
-// @todo dedupe with use-toolbar.tsx
-type Value = "b" | "i" | "a";
+type Value = "Bold" | "Italic" | "Link";
const onClickPreventDefault: MouseEventHandler<HTMLDivElement> = (event) => {
event.preventDefault();
+ event.stopPropagation();
};
type ToolbarProps = {
@@ -65,13 +66,13 @@ const Toolbar = ({ css, onValueChange, rootRef }: ToolbarProps) => {
...css,
}}
>
- <ToggleGroup.Item value="b">
- <FontBoldIcon style={{ pointerEvents: "auto" }} />
+ <ToggleGroup.Item value="Bold">
+ <FontBoldIcon />
</ToggleGroup.Item>
- <ToggleGroup.Item value="i">
+ <ToggleGroup.Item value="Italic">
<FontItalicIcon />
</ToggleGroup.Item>
- <ToggleGroup.Item value="a">
+ <ToggleGroup.Item value="Link">
<Link2Icon />
</ToggleGroup.Item>
</ToggleGroup.Root>
@@ -97,8 +98,9 @@ export const TextToolbar = ({ publish }: TextToolbarProps) => {
<Toolbar
rootRef={setElement}
css={placement}
- onValueChange={(type) => {
- publish({ type: "insertInlineInstance", payload: type });
+ onValueChange={(component) => {
+ const instance = createInstance({ component });
+ publish({ type: "insertInlineInstance", payload: instance });
}}
/>
);
diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsx
index 544b9dae98..19e48c85c7 100644
--- a/app/shared/text-editor/example-text-editor.tsx
+++ b/app/shared/text-editor/example-text-editor.tsx
@@ -25,10 +25,7 @@ const Menu = () => {
const instance = createInstance({ component: "Bold" });
publish({
type: "insertInlineInstance",
- payload: {
- instance,
- props: {},
- },
+ payload: instance,
});
}}
>
@@ -39,10 +36,7 @@ const Menu = () => {
const instance = createInstance({ component: "Italic" });
publish({
type: "insertInlineInstance",
- payload: {
- instance,
- props: {},
- },
+ payload: instance,
});
}}
>
@@ -53,10 +47,7 @@ const Menu = () => {
const instance = createInstance({ component: "Link" });
publish({
type: "insertInlineInstance",
- payload: {
- instance,
- props: { href: "", target: "_blank" },
- },
+ payload: instance,
});
const url = prompt("Enter url");
if (url === null) return;
diff --git a/app/shared/text-editor/plugins/plugin-instance.tsx b/app/shared/text-editor/plugins/plugin-instance.tsx
index af715bfee4..c227ce87d0 100644
--- a/app/shared/text-editor/plugins/plugin-instance.tsx
+++ b/app/shared/text-editor/plugins/plugin-instance.tsx
@@ -51,13 +51,7 @@ const populateRoot = (children: Instance["children"]) => {
root.selectStart();
};
-const INSERT_INSTANCE_COMMAND: LexicalCommand<CreateInstancePayload> =
- createCommand();
-
-type CreateInstancePayload = {
- instance: Instance;
- props: Record<string, unknown>;
-};
+const INSERT_INSTANCE_COMMAND: LexicalCommand<Instance> = createCommand();
type InstancePluginProps = {
children: Instance["children"];
@@ -70,23 +64,29 @@ export const InstancePlugin = ({ children }: InstancePluginProps) => {
throw new Error("InstancePlugin: InstanceNode not registered on editor");
}
- return editor.registerCommand<CreateInstancePayload>(
+ return editor.registerCommand<Instance>(
INSERT_INSTANCE_COMMAND,
- ({ instance, props }) => {
+ (instance) => {
const selection = $getSelection();
const text = selection?.getTextContent();
if ($isRangeSelection(selection) && text) {
const instanceNode = $createInstanceNode({
component: (
- <InlineWrapperComponent {...props} instance={instance}>
+ <InlineWrapperComponent instance={instance}>
{text}
</InlineWrapperComponent>
),
text,
});
selection.insertNodes([instanceNode]);
+ // Dirty hack. When clicking on toolbar outside of the iframe, we are loosing focus.
+ // For some reason we can only refocus after a delay
+ requestAnimationFrame(() => {
+ editor.update(() => {
+ instanceNode.select();
+ });
+ });
}
-
return true;
},
COMMAND_PRIORITY_EDITOR
@@ -99,13 +99,10 @@ export const InstancePlugin = ({ children }: InstancePluginProps) => {
});
}, [editor, children]);
- useSubscribe<"insertInlineInstance", CreateInstancePayload>(
+ useSubscribe<"insertInlineInstance", Instance>(
"insertInlineInstance",
(payload) => {
- editor.dispatchCommand<CreateInstancePayload>(
- INSERT_INSTANCE_COMMAND,
- payload
- );
+ editor.dispatchCommand<Instance>(INSERT_INSTANCE_COMMAND, payload);
}
);