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.

set property from story

Oleg Isonenb9d8e1825ff0
diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsx
index d4c3b381e5..e013491def 100644
--- a/app/shared/text-editor/example-text-editor.tsx
+++ b/app/shared/text-editor/example-text-editor.tsx
@@ -1,4 +1,4 @@
-import { publish } from "@webstudio-is/sdk";
+import { publish, useAllUserProps } from "@webstudio-is/sdk";
import {
type EditorState,
LexicalComposer,
@@ -10,57 +10,77 @@ import {
import { InstancePlugin } from "./plugins/plugin-instance";
import { TreeViewPlugin } from "./plugins/tree-view-plugin";
import { config } from "./config";
+import { createInstance } from "../tree-utils";
type ExampleTextEditorProps = {
onChange: (state: EditorState) => void;
};
-const Menu = () => (
- <div className="editor-menu">
- <button
- onClick={() => {
- publish({
- type: "insertInlineInstance",
- payload: {
- component: "Bold",
- props: {},
- },
- });
- }}
- >
- Bold
- </button>
- <button
- onClick={() => {
- publish({
- type: "insertInlineInstance",
- payload: {
- component: "Italic",
- props: {},
- },
- });
- }}
- >
- Italic
- </button>
- <button
- onClick={() => {
- publish({
- type: "insertInlineInstance",
- payload: {
- component: "Link",
- props: { href: "https://google.com", target: "_blank" },
- },
- });
- const url = prompt("Enter url");
- console.log(url);
- }}
- >
- Link
- </button>
- </div>
-);
-
+const Menu = () => {
+ const [, setAllUserProps] = useAllUserProps();
+ return (
+ <div className="editor-menu">
+ <button
+ onClick={() => {
+ const instance = createInstance({ component: "Bold" });
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ instance,
+ props: {},
+ },
+ });
+ }}
+ >
+ Bold
+ </button>
+ <button
+ onClick={() => {
+ const instance = createInstance({ component: "Italic" });
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ instance,
+ props: {},
+ },
+ });
+ }}
+ >
+ Italic
+ </button>
+ <button
+ onClick={() => {
+ const instance = createInstance({ component: "Link" });
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ instance,
+ props: { href: "", target: "_blank" },
+ },
+ });
+ const url = prompt("Enter url");
+ if (url === null) return;
+ setAllUserProps({
+ [instance.id]: {
+ id: "1",
+ instanceId: instance.id,
+ treeId: "3",
+ props: [
+ {
+ id: "4",
+ prop: "href",
+ value: url,
+ },
+ ],
+ },
+ });
+ }}
+ >
+ Link
+ </button>
+ </div>
+ );
+};
export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {
return (
<>
diff --git a/app/shared/text-editor/nodes/node-instance.tsx b/app/shared/text-editor/nodes/node-instance.tsx
index 1d255e72f6..909ba50216 100644
--- a/app/shared/text-editor/nodes/node-instance.tsx
+++ b/app/shared/text-editor/nodes/node-instance.tsx
@@ -1,9 +1,7 @@
import { type EditorConfig, TextNode } from "../lexical";
import { render } from "react-dom";
-import type { Instance } from "@webstudio-is/sdk";
type Options = {
- id: Instance["id"];
component: JSX.Element;
text: string;
};
@@ -21,7 +19,7 @@ export class InstanceNode extends TextNode {
constructor(options: Options) {
// This makes sure caret is at the end of the selection after inserting this node instance
- super(options.text, options.id);
+ super(options.text);
this.options = options;
}
diff --git a/app/shared/text-editor/plugins/plugin-instance.tsx b/app/shared/text-editor/plugins/plugin-instance.tsx
index cb9f702bb1..b0fd0812fb 100644
--- a/app/shared/text-editor/plugins/plugin-instance.tsx
+++ b/app/shared/text-editor/plugins/plugin-instance.tsx
@@ -1,5 +1,10 @@
-import { useEffect } from "react";
-import { useSubscribe, type Instance } from "@webstudio-is/sdk";
+import { useEffect, useMemo } from "react";
+import {
+ useSubscribe,
+ useUserProps,
+ type Instance,
+ toCss,
+} from "@webstudio-is/sdk";
import { primitives } from "../../canvas-components";
import { $createInstanceNode, InstanceNode } from "../nodes/node-instance";
import {
@@ -10,12 +15,14 @@ import {
useLexicalComposerContext,
type LexicalCommand,
} from "../lexical";
+import { useCss } from "~/canvas/features/wrapper-component/use-css";
+import { useBreakpoints } from "~/shared/nano-states";
-const INSERT_INSTANCE_COMMAND: LexicalCommand<Payload> = createCommand();
+const INSERT_INSTANCE_COMMAND: LexicalCommand<CreateInstancePayload> =
+ createCommand();
-type Payload = {
- id: Instance["id"];
- component: keyof typeof primitives;
+type CreateInstancePayload = {
+ instance: Instance;
props: Record<string, unknown>;
};
@@ -26,17 +33,19 @@ export const InstancePlugin = () => {
throw new Error("InstancePlugin: InstanceNode not registered on editor");
}
- return editor.registerCommand<Payload>(
+ return editor.registerCommand<CreateInstancePayload>(
INSERT_INSTANCE_COMMAND,
- (payload) => {
+ ({ instance, props }) => {
const selection = $getSelection();
const text = selection?.getTextContent();
if ($isRangeSelection(selection) && text) {
- const { Component } = primitives[payload.component];
const instanceNode = $createInstanceNode({
- component: <Component {...payload.props}>{text}</Component>,
+ component: (
+ <InlineWrapperComponent {...props} instance={instance}>
+ {text}
+ </InlineWrapperComponent>
+ ),
text,
- id: payload.id,
});
selection.insertNodes([instanceNode]);
}
@@ -47,11 +56,41 @@ export const InstancePlugin = () => {
);
}, [editor]);
- useSubscribe<"insertInlineInstance", Payload>(
+ useSubscribe<"insertInlineInstance", CreateInstancePayload>(
"insertInlineInstance",
(payload) => {
- editor.dispatchCommand<Payload>(INSERT_INSTANCE_COMMAND, payload);
+ editor.dispatchCommand<CreateInstancePayload>(
+ INSERT_INSTANCE_COMMAND,
+ payload
+ );
}
);
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}
+ />
+ );
+};