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.

Allow text editor styles nesting (#423)

Bogdan Chadkin15972f892547
diff --git a/apps/designer/app/canvas/features/text-editor/interop.ts b/apps/designer/app/canvas/features/text-editor/interop.ts
index 52a9467cac..773a4ce08a 100644
--- a/apps/designer/app/canvas/features/text-editor/interop.ts
+++ b/apps/designer/app/canvas/features/text-editor/interop.ts
@@ -1,15 +1,14 @@
import {
- type ElementNode,
$getRoot,
$createTextNode,
$createParagraphNode,
TextNode,
+ ElementNode,
LineBreakNode,
ParagraphNode,
} from "lexical";
import { $createLinkNode, LinkNode } from "@lexical/link";
import type { ChildrenUpdates, Instance } from "@webstudio-is/react-sdk";
-import { createInstanceId } from "~/shared/tree-utils";
// Map<nodeKey, Instance>
export type Refs = Map<string, Instance>;
@@ -34,23 +33,38 @@ const $writeUpdates = (
}
}
if (child instanceof LinkNode) {
- // @todo support link children styling
- const text = child.getTextContent();
- const id = refs.get(child.getKey())?.id ?? createInstanceId();
- updates.push({ id, component: "Link", text });
+ const id = refs.get(child.getKey())?.id;
+ const childrenUpdates: ChildrenUpdates = [];
+ $writeUpdates(child, childrenUpdates, refs);
+ updates.push({ id, component: "Link", children: childrenUpdates });
}
if (child instanceof TextNode) {
- // @todo support nesting bold into italic and vice versa
+ // support nesting bold into italic and vice versa
+ // considering lexical represents both as single node
+ // and add ref suffix to distinct styling on one node key
const text = child.getTextContent();
+ let parentUpdates = updates;
if (child.hasFormat("bold")) {
- const id = refs.get(child.getKey())?.id ?? createInstanceId();
- updates.push({ id, component: "Bold", text });
- } else if (child.hasFormat("italic")) {
- const id = refs.get(child.getKey())?.id ?? createInstanceId();
- updates.push({ id, component: "Italic", text });
- } else {
- updates.push(text);
+ const id = refs.get(`${child.getKey()}:bold`)?.id;
+ const update: ChildrenUpdates[number] = {
+ id,
+ component: "Bold",
+ children: [],
+ };
+ parentUpdates.push(update);
+ parentUpdates = update.children;
}
+ if (child.hasFormat("italic")) {
+ const id = refs.get(`${child.getKey()}:italic`)?.id;
+ const update: ChildrenUpdates[number] = {
+ id,
+ component: "Italic",
+ children: [],
+ };
+ parentUpdates.push(update);
+ parentUpdates = update.children;
+ }
+ parentUpdates.push(text);
}
}
};
@@ -62,6 +76,56 @@ export const $convertToUpdates = (refs: Refs) => {
return updates;
};
+type InstanceChild = string | Instance;
+
+const $writeLexical = (
+ parent: ElementNode | TextNode,
+ children: InstanceChild[],
+ refs: Refs
+) => {
+ for (const child of children) {
+ if (typeof child === "string") {
+ if (parent instanceof TextNode) {
+ parent.setTextContent(child);
+ } else {
+ const textNode = $createTextNode(child);
+ parent.append(textNode);
+ }
+ } else {
+ if (parent instanceof ElementNode && child.component === "Link") {
+ const linkNode = $createLinkNode("");
+ refs.set(linkNode.getKey(), child);
+ parent.append(linkNode);
+ $writeLexical(linkNode, child.children, refs);
+ }
+ if (child.component === "Bold") {
+ let textNode;
+ if (parent instanceof TextNode) {
+ textNode = parent;
+ } else {
+ textNode = $createTextNode("");
+ parent.append(textNode);
+ }
+ textNode.toggleFormat("bold");
+ refs.set(`${textNode.getKey()}:bold`, child);
+ $writeLexical(textNode, child.children, refs);
+ }
+ if (child.component === "Italic") {
+ let textNode;
+ if (parent instanceof TextNode) {
+ textNode = parent;
+ } else {
+ textNode = $createTextNode("");
+ parent.append(textNode);
+ }
+ textNode.toggleFormat("italic");
+ refs.set(`${textNode.getKey()}:italic`, child);
+ $writeLexical(textNode, child.children, refs);
+ }
+ }
+ }
+};
+
export const $convertToLexical = (instance: Instance, refs: Refs) => {
const root = $getRoot();
let p = $createParagraphNode();
@@ -70,31 +134,8 @@ export const $convertToLexical = (instance: Instance, refs: Refs) => {
if (child === "\n") {
p = $createParagraphNode();
root.append(p);
- } else if (typeof child === "string") {
- const textNode = $createTextNode(child);
- p.append(textNode);
} else {
- // inline components should always have a single child string
- const text =
- typeof child.children[0] === "string" ? child.children[0] : "";
- if (child.component === "Bold") {
- const textNode = $createTextNode(text);
- textNode.setFormat("bold");
- refs.set(textNode.getKey(), child);
- p.append(textNode);
- }
- if (child.component === "Italic") {
- const textNode = $createTextNode(text);
- textNode.setFormat("italic");
- refs.set(textNode.getKey(), child);
- p.append(textNode);
- }
- if (child.component === "Link") {
- const textNode = $createTextNode(text);
- const linkNode = $createLinkNode(text).append(textNode);
- refs.set(linkNode.getKey(), child);
- p.append(linkNode);
- }
+ $writeLexical(p, [child], refs);
}
}
};
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.tsx
index 28144ab987..3ab024c9c1 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
@@ -66,7 +66,23 @@ export const Basic: ComponentStory<typeof TextEditor> = ({ onChange }) => {
component: "TextBlock",
children: [
"Pragraph you can edit ",
- createInstance({ component: "Bold", children: ["bold"] }),
+ createInstance({
+ component: "Bold",
+ children: ["very bold text "],
+ }),
+ createInstance({
+ component: "Bold",
+ children: [
+ createInstance({
+ component: "Italic",
+ children: ["with small italic"],
+ }),
+ ],
+ }),
+ createInstance({
+ component: "Bold",
+ children: [" subtext"],
+ }),
],
})}
contentEditable={<ContentEditable />}
diff --git a/apps/designer/app/canvas/features/text-editor/text-editor.tsx b/apps/designer/app/canvas/features/text-editor/text-editor.tsx
index 338fa7f5be..bce281c247 100644
--- a/apps/designer/app/canvas/features/text-editor/text-editor.tsx
+++ b/apps/designer/app/canvas/features/text-editor/text-editor.tsx
@@ -25,11 +25,15 @@ const NodeConnector = ({
}) => {
const [breakpoints] = useBreakpoints();
useEffect(() => {
- const element = editor.getElementByKey(nodeKey);
+ const [key, style] = nodeKey.split(":");
+ const element = editor.getElementByKey(key);
if (element) {
const className = css(toCss(instance.cssRules, breakpoints))().toString();
// assume only styles are important while editing text
element.className = className;
+ if (style === "italic") {
+ element.style.fontStyle = "italic";
+ }
}
}, [editor, nodeKey, instance, breakpoints]);
return null;
diff --git a/apps/designer/app/shared/tree-utils/set-instance-children.ts b/apps/designer/app/shared/tree-utils/set-instance-children.ts
index b82e8173e7..da76b5851c 100644
--- a/apps/designer/app/shared/tree-utils/set-instance-children.ts
+++ b/apps/designer/app/shared/tree-utils/set-instance-children.ts
@@ -1,23 +1,12 @@
import { type ChildrenUpdates } from "@webstudio-is/react-sdk";
import { type Instance } from "@webstudio-is/react-sdk";
-import { createInstance } from "./create-instance";
+import { createInstance, createInstanceId } from "./create-instance";
import { findInstanceById } from "./find-instance";
-/**
- * Update children of an instance with new text.
- * When child is an instance - we update it's children with a single text entry.
- */
-export const setInstanceChildrenMutable = (
- id: Instance["id"],
- // Not a consistent format now, maybe better:
- // [{set: 'string'}, {set: instance}, {update: {id,text}}]
- updates: ChildrenUpdates,
- rootInstance: Instance
-) => {
- const instance = findInstanceById(rootInstance, id);
- if (instance === undefined) return false;
- const children = [];
+type InstanceChild = Instance | string;
+const hydrateTree = (parent: Instance, updates: ChildrenUpdates) => {
+ const children: InstanceChild[] = [];
for (const update of updates) {
// Set a string as a child
if (typeof update === "string") {
@@ -25,19 +14,34 @@ export const setInstanceChildrenMutable = (
continue;
}
// create new child or update existing
- let childInstance = findInstanceById(instance, update.id);
- if (childInstance == null) {
- childInstance = createInstance({
- id: update.id,
+ const instanceId = update.id ?? createInstanceId();
+ let child = findInstanceById(parent, instanceId);
+ if (child == null) {
+ child = createInstance({
+ id: instanceId,
component: update.component,
- children: [update.text],
+ children: [],
});
- children.push(childInstance);
- } else {
- children.push({ ...childInstance, children: [update.text] });
}
+ children.push(child);
+ hydrateTree(child, update.children);
}
+ parent.children = children;
+};
- instance.children = children;
+/**
+ * Update children of an instance with new text.
+ * When child is an instance - we update it's children with a single text entry.
+ */
+export const setInstanceChildrenMutable = (
+ id: Instance["id"],
+ // Not a consistent format now, maybe better:
+ // [{set: 'string'}, {set: instance}, {update: {id,text}}]
+ updates: ChildrenUpdates,
+ rootInstance: Instance
+) => {
+ const instance = findInstanceById(rootInstance, id);
+ if (instance === undefined) return false;
+ hydrateTree(instance, updates);
return true;
};
diff --git a/packages/project/src/shared/tree-utils/index.ts b/packages/project/src/shared/tree-utils/index.ts
index 4f4109330c..da30e36aac 100644
--- a/packages/project/src/shared/tree-utils/index.ts
+++ b/packages/project/src/shared/tree-utils/index.ts
@@ -4,7 +4,6 @@ export * from "./insert-instance";
export * from "./populate";
export * from "./find-instance";
export * from "./set-instance-style";
-export * from "./set-instance-children";
export * from "./get-instance-path";
export * from "./find-closest-sibling-instance";
export * from "./find-parent-instance";
diff --git a/packages/project/src/shared/tree-utils/set-instance-children.ts b/packages/project/src/shared/tree-utils/set-instance-children.ts
deleted file mode 100644
index b82e8173e7..0000000000
--- a/packages/project/src/shared/tree-utils/set-instance-children.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import { type ChildrenUpdates } from "@webstudio-is/react-sdk";
-import { type Instance } from "@webstudio-is/react-sdk";
-import { createInstance } from "./create-instance";
-import { findInstanceById } from "./find-instance";
-
-/**
- * Update children of an instance with new text.
- * When child is an instance - we update it's children with a single text entry.
- */
-export const setInstanceChildrenMutable = (
- id: Instance["id"],
- // Not a consistent format now, maybe better:
- // [{set: 'string'}, {set: instance}, {update: {id,text}}]
- updates: ChildrenUpdates,
- rootInstance: Instance
-) => {
- const instance = findInstanceById(rootInstance, id);
- if (instance === undefined) return false;
- const children = [];
-
- for (const update of updates) {
- // Set a string as a child
- if (typeof update === "string") {
- children.push(update);
- continue;
- }
- // create new child or update existing
- let childInstance = findInstanceById(instance, update.id);
- if (childInstance == null) {
- childInstance = createInstance({
- id: update.id,
- component: update.component,
- children: [update.text],
- });
- children.push(childInstance);
- } else {
- children.push({ ...childInstance, children: [update.text] });
- }
- }
-
- instance.children = children;
- return true;
-};
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index 24d021f999..22cfe2b05f 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -8,7 +8,11 @@ import { SessionStoragePolyfill } from "./session-storage-polyfill";
export type ChildrenUpdates = Array<
| string
- | { id: Instance["id"]; component: Instance["component"]; text: string }
+ | {
+ id: undefined | Instance["id"];
+ component: Instance["component"];
+ children: ChildrenUpdates;
+ }
>;
export type OnChangeChildren = (change: {