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
serialize instances
Oleg Isonen74dceedd3fd4
diff --git a/app/shared/text-editor/nodes/node-instance.tsx b/app/shared/text-editor/nodes/node-instance.tsxindex 909ba50216..00c3115f3e 100644--- a/app/shared/text-editor/nodes/node-instance.tsx+++ b/app/shared/text-editor/nodes/node-instance.tsx@@ -1,11 +1,23 @@-import { type EditorConfig, TextNode } from "../lexical";+import { useUserProps, type Instance, toCss } from "@webstudio-is/sdk";import { render } from "react-dom";+import { useMemo } from "react";+import { useCss } from "~/canvas/features/wrapper-component/use-css";+import { useBreakpoints } from "~/shared/nano-states";+import { primitives } from "../../canvas-components";+import {+ type EditorConfig,+ type SerializedTextNode,+ TextNode,+} from "../lexical";type Options = {- component: JSX.Element;+ instance: Instance;text: string;+ isNew: boolean;};+export type SerializedInstanceNode = SerializedTextNode & Options;+export class InstanceNode extends TextNode {options: Options;@@ -23,9 +35,23 @@ export class InstanceNode extends TextNode {this.options = options;}+ exportJSON(): SerializedInstanceNode {+ const json = super.exportJSON();+ return {+ ...json,+ ...this.options,+ type: InstanceNode.getType(),+ };+ }+createDOM(config: EditorConfig) {const container = super.createDOM(config);- render(this.options.component, container);+ const element = (+ <InlineWrapperComponent instance={this.options.instance}>+ {this.options.text}+ </InlineWrapperComponent>+ );+ render(element, container);return container;}@@ -55,3 +81,33 @@ export class InstanceNode extends TextNode {export const $createInstanceNode = (options: Options) => {return new InstanceNode(options);};++// @todo+// - reuse majority of this logic across WrapperComponentDev and WrapperComponent+// - merge className with props+const InlineWrapperComponent = ({+ instance,+ ...rest+}: {+ instance: Instance;+ children: string;+}) => {+ const [breakpoints] = useBreakpoints();+ const css = useMemo(+ () => toCss(instance.cssRules, breakpoints),+ [instance, breakpoints]+ );+ const className = useCss({ instance, css });+ const userProps = useUserProps(instance.id);+ const { Component } = primitives[instance.component];++ return (+ <Component+ {...rest}+ {...userProps}+ key={instance.id}+ id={instance.id}+ className={className}+ />+ );+};diff --git a/app/shared/text-editor/plugins/plugin-instance.tsx b/app/shared/text-editor/plugins/plugin-instance.tsxindex 1b31d7e08d..aa01decb64 100644--- a/app/shared/text-editor/plugins/plugin-instance.tsx+++ b/app/shared/text-editor/plugins/plugin-instance.tsx@@ -1,13 +1,5 @@-import { useEffect, useMemo } from "react";-import {- useSubscribe,- useUserProps,- type Instance,- toCss,-} from "@webstudio-is/sdk";-import { useCss } from "~/canvas/features/wrapper-component/use-css";-import { useBreakpoints } from "~/shared/nano-states";-import { primitives } from "../../canvas-components";+import { useEffect } from "react";+import { useSubscribe, type Instance } from "@webstudio-is/sdk";import { $createInstanceNode, InstanceNode } from "../nodes/node-instance";import {createCommand,@@ -21,7 +13,7 @@ import {import { toLexicalNodes } from "../utils/to-lexical-nodes";const populateRoot = (children: Instance["children"]) => {- const nodes = toLexicalNodes(children, InlineWrapperComponent);+ const nodes = toLexicalNodes(children);const root = $getRoot();root.clear();for (const node of nodes) {@@ -50,12 +42,9 @@ export const InstancePlugin = ({ children }: InstancePluginProps) => {const text = selection?.getTextContent();if ($isRangeSelection(selection) && text) {const instanceNode = $createInstanceNode({- component: (- <InlineWrapperComponent instance={instance}>- {text}- </InlineWrapperComponent>- ),+ instance,text,+ isNew: true,});selection.insertNodes([instanceNode]);// Dirty hack. When clicking on toolbar outside of the iframe, we are loosing focus.@@ -87,30 +76,3 @@ export const InstancePlugin = ({ children }: InstancePluginProps) => {return null;};--const InlineWrapperComponent = ({- instance,- ...rest-}: {- instance: Instance;- children: string;-}) => {- const [breakpoints] = useBreakpoints();- const css = useMemo(- () => toCss(instance.cssRules, breakpoints),- [instance, breakpoints]- );- const className = useCss({ instance, css });- const userProps = useUserProps(instance.id);- const { Component } = primitives[instance.component];-- return (- <Component- {...rest}- {...userProps}- key={instance.id}- id={instance.id}- className={className}- />- );-};diff --git a/app/shared/text-editor/plugins/plugin-on-change.tsx b/app/shared/text-editor/plugins/plugin-on-change.tsxindex cc887b1423..7f3b616928 100644--- a/app/shared/text-editor/plugins/plugin-on-change.tsx+++ b/app/shared/text-editor/plugins/plugin-on-change.tsx@@ -14,8 +14,6 @@ export const OnChangePlugin = ({state.read(() => {const updates = toUpdates(state.toJSON().root);onChange(updates);- console.log(state.toJSON());- console.log(updates);});};diff --git a/app/shared/text-editor/utils/to-lexical-nodes.tsx b/app/shared/text-editor/utils/to-lexical-nodes.tsxindex f42bcefe31..fb9c09930d 100644--- a/app/shared/text-editor/utils/to-lexical-nodes.tsx+++ b/app/shared/text-editor/utils/to-lexical-nodes.tsx@@ -2,13 +2,7 @@ import { type Instance } from "@webstudio-is/sdk";import { $createInstanceNode } from "../nodes/node-instance";import { $createTextNode, $createParagraphNode } from "../lexical";-export const toLexicalNodes = (- children: Instance["children"],- InlineWrapperComponent: (props: {- instance: Instance;- children: string;- }) => JSX.Element-) => {+export const toLexicalNodes = (children: Instance["children"]) => {return children.map((child) => {if (typeof child === "string") {const paragraph = $createParagraphNode();@@ -20,10 +14,9 @@ export const toLexicalNodes = (const text = typeof child.children[0] === "string" ? child.children[0] : "";return $createInstanceNode({- component: (- <InlineWrapperComponent instance={child}>{text}</InlineWrapperComponent>- ),+ instance: child,text,+ isNew: false,});});};diff --git a/app/shared/text-editor/utils/to-updates.test.ts b/app/shared/text-editor/utils/to-updates.test.tsindex f0c3e3b7b0..fe07439ca5 100644--- a/app/shared/text-editor/utils/to-updates.test.ts+++ b/app/shared/text-editor/utils/to-updates.test.ts@@ -52,4 +52,60 @@ describe("toUpdates", () => {const updates = toUpdates(state.root);expect(updates).toStrictEqual(["a\nb"]);});++ test("text and instance", () => {+ const state = {+ root: {+ children: [+ {+ children: [+ {+ detail: 0,+ format: 0,+ mode: "normal",+ style: "",+ text: "a ",+ type: "text",+ version: 1,+ },+ {+ detail: 0,+ format: 0,+ mode: "normal",+ style: "",+ text: "b",+ type: "instance",+ version: 1,+ instance: {+ component: "Bold",+ id: "62bcc02160a439686c7eabde",+ cssRules: [],+ children: [],+ },+ },+ ],+ direction: "ltr",+ format: "",+ indent: 0,+ type: "paragraph",+ version: 1,+ },+ ],+ direction: "ltr",+ format: "",+ indent: 0,+ type: "root",+ version: 1,+ },+ } as const;++ const updates = toUpdates(state.root);+ expect(updates).toStrictEqual([+ "a ",+ {+ id: "62bcc02160a439686c7eabde",+ text: "b",+ },+ ]);+ });});diff --git a/app/shared/text-editor/utils/to-updates.ts b/app/shared/text-editor/utils/to-updates.tsindex e87252c621..53fb2fea65 100644--- a/app/shared/text-editor/utils/to-updates.ts+++ b/app/shared/text-editor/utils/to-updates.ts@@ -1,25 +1,24 @@import { type ChildrenUpdates } from "@webstudio-is/sdk";import {- OnChangePlugin as LexicalOnChangePlugin,- type EditorState,- type SerializedEditorState,type SerializedTextNode,type SerializedElementNode,type SerializedRootNode,type SerializedLexicalNode,} from "../lexical";+import { type SerializedInstanceNode } from "../nodes/node-instance";type Node =| SerializedRootNode| SerializedElementNode| SerializedTextNode- | SerializedLexicalNode;+ | SerializedLexicalNode+ | SerializedInstanceNode;export const toUpdates = (node: Node,updates: ChildrenUpdates = []): ChildrenUpdates => {- if ("text" in node) {+ if (node.type === "text" && "text" in node) {const last = updates[updates.length - 1];if (typeof last === "string") {// Every new Paragraph node is basically just a new line.@@ -29,6 +28,22 @@ export const toUpdates = (}}+ if (node.type === "instance" && "instance" in node) {+ if ("isNew" in node && node.isNew === true) {+ updates.push({+ id: node.instance.id,+ text: node.text,+ component: node.instance.component,+ createInstance: true,+ });+ } else {+ updates.push({+ id: node.instance.id,+ text: node.text,+ });+ }+ }+if ("children" in node) {for (const child of node.children) {toUpdates(child, updates);