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
Auto select span after toggling (#622)
Bogdan Chadkinc70d9ff21a64
diff --git a/apps/designer/app/canvas/features/text-editor/interop.ts b/apps/designer/app/canvas/features/text-editor/interop.tsindex 9f12c7daf1..3e54ee2f01 100644--- a/apps/designer/app/canvas/features/text-editor/interop.ts+++ b/apps/designer/app/canvas/features/text-editor/interop.ts@@ -12,6 +12,7 @@ import {} from "lexical";import { $createLinkNode, $isLinkNode } from "@lexical/link";import type { ChildrenUpdates, Instance } from "@webstudio-is/react-sdk";+import { utils } from "@webstudio-is/project";import { $isSpanNode, $setNodeSpan } from "./toolbar-connector";// Map<nodeKey, instanceId>@@ -54,7 +55,12 @@ const $writeUpdates = (const text = child.getTextContent();let parentUpdates = updates;if ($isSpanNode(child)) {- const id = refs.get(`${child.getKey()}:span`);+ // prematurely generate span id to select it right after applying+ const storedId = refs.get(`${child.getKey()}:span`);+ const id = storedId ?? utils.tree.createInstanceId();+ if (storedId == null) {+ refs.set(`${child.getKey()}:span`, id);+ }const update: ChildrenUpdates[number] = {id,component: "Span",diff --git a/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx b/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsxindex e1896ea23d..fa68281f95 100644--- a/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx+++ b/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx@@ -121,6 +121,10 @@ export const Basic: ComponentStory<typeof TextEditor> = ({ onChange }) => {})}contentEditable={<ContentEditable />}onChange={onChange}+ onSelectInstance={(instanceId) =>+ // eslint-disable-next-line no-console+ console.info("select instance", instanceId)+ }/></Box></div>diff --git a/apps/designer/app/canvas/features/text-editor/text-editor.tsx b/apps/designer/app/canvas/features/text-editor/text-editor.tsxindex 4828188d52..ad2e554e68 100644--- a/apps/designer/app/canvas/features/text-editor/text-editor.tsx+++ b/apps/designer/app/canvas/features/text-editor/text-editor.tsx@@ -116,12 +116,14 @@ type TextEditorProps = {instance: Instance;contentEditable: JSX.Element;onChange: (updates: ChildrenUpdates) => void;+ onSelectInstance: (instanceId: string) => void;};export const TextEditor = ({instance,contentEditable,onChange,+ onSelectInstance,}: TextEditorProps) => {const [paragraphClassName] = useState(() => nanoid());const [italicClassName] = useState(() => nanoid());@@ -168,7 +170,14 @@ export const TextEditor = ({<LexicalComposer initialConfig={initialConfig}><AutofocusPlugin /><RemoveParagaphsPlugin />- <ToolbarConnectorPlugin />+ <ToolbarConnectorPlugin+ onSelectNode={(nodeKey) => {+ const instanceId = refs.get(`${nodeKey}:span`);+ if (instanceId !== undefined) {+ onSelectInstance(instanceId);+ }+ }}+ /><BindInstanceToNodePlugin refs={refs} /><RichTextPluginErrorBoundary={LexicalErrorBoundary}diff --git a/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx b/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsxindex fdef3a191b..8d25061c03 100644--- a/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx+++ b/apps/designer/app/canvas/features/text-editor/toolbar-connector.tsx@@ -80,7 +80,11 @@ const $isSelectedLink = (selection: RangeSelection) => {return $getNearestNodeOfType(selectedNode, LinkNode) != null;};-export const ToolbarConnectorPlugin = () => {+export const ToolbarConnectorPlugin = ({+ onSelectNode,+}: {+ onSelectNode: (nodeKey: string) => void;+}) => {const [editor] = useLexicalComposerContext();const isMouseDownRef = useRef(false);@@ -211,9 +215,26 @@ export const ToolbarConnectorPlugin = () => {}}if (type === "span") {- editor.update(() => {- $toggleSpan();- });+ editor.update(+ () => {+ $toggleSpan();+ },+ {+ onUpdate: () => {+ const editorState = editor.getEditorState();+ editorState.read(() => {+ const selection = $getSelection();+ if ($isRangeSelection(selection)) {+ const spans = $getSpanNodes(selection);+ if (spans.length !== 0) {+ const [node] = spans;+ onSelectNode(node.getKey());+ }+ }+ });+ },+ }+ );}if (type === "clear") {editor.update(() => {diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsxindex 088038544a..4d4497f5ea 100644--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx@@ -12,6 +12,7 @@ import {import { useTextEditingInstanceId } from "~/shared/nano-states";import { useSelectedElement } from "~/canvas/shared/nano-states";import { useCssRules } from "~/canvas/shared/styles";+import { publish } from "~/shared/pubsub";const TextEditor = lazy(() => import("../text-editor"));@@ -51,7 +52,8 @@ export const WrapperComponentDev = ({}: WrapperComponentDevProps) => {useCssRules(instance);- const [editingInstanceId] = useTextEditingInstanceId();+ const [editingInstanceId, setTextEditingInstanceId] =+ useTextEditingInstanceId();const [, setSelectedElement] = useSelectedElement();const refCallback = useCallback(@@ -117,6 +119,13 @@ export const WrapperComponentDev = ({onChange={(updates) => {onChangeChildren?.({ instanceId: instance.id, updates });}}+ onSelectInstance={(instanceId) => {+ setTextEditingInstanceId(undefined);+ publish({+ type: "selectInstanceById",+ payload: instanceId,+ });+ }}/></Suspense>);