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.

feat: support editing text content in settings panel (#2754)

Bogdan Chadkin8fa0d7590fa9
diff --git a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
index 7092c9a321..7f301a867d 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
@@ -9,6 +9,7 @@ import { FileControl } from "./file";
import { UrlControl } from "./url";
import type { ControlProps } from "../shared";
import { JsonControl } from "./json";
+import { TextContent } from "./text-content";
export const renderControl = ({
meta,
@@ -17,6 +18,10 @@ export const renderControl = ({
}: ControlProps<string> & { key?: string }) => {
const computed = rest.computedValue;
+ if (meta.control === "textContent") {
+ return <TextContent meta={meta} prop={prop} {...rest} />;
+ }
+
// never render parameter props
if (prop?.type === "parameter") {
return;
diff --git a/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx
new file mode 100644
index 0000000000..5e050b713d
--- /dev/null
+++ b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx
@@ -0,0 +1,110 @@
+import { useStore } from "@nanostores/react";
+import {
+ useId,
+ TextArea,
+ Flex,
+ Label,
+ theme,
+} from "@webstudio-is/design-system";
+import type { Instance } from "@webstudio-is/sdk";
+import {
+ BindingControl,
+ BindingPopover,
+} from "~/builder/shared/binding-popover";
+import {
+ type ControlProps,
+ getLabel,
+ useLocalValue,
+ VerticalLayout,
+ $selectedInstanceScope,
+} from "../shared";
+import { useMemo } from "react";
+import { computed } from "nanostores";
+import { $instances } from "~/shared/nano-states";
+import { serverSyncStore } from "~/shared/sync";
+
+const useInstance = (instanceId: Instance["id"]) => {
+ const $store = useMemo(() => {
+ return computed($instances, (instances) => instances.get(instanceId));
+ }, [instanceId]);
+ return useStore($store);
+};
+
+const updateChildren = (
+ instanceId: Instance["id"],
+ type: "text",
+ value: string
+) => {
+ serverSyncStore.createTransaction([$instances], (instances) => {
+ const instance = instances.get(instanceId);
+ if (instance === undefined) {
+ return;
+ }
+ if (type === "text") {
+ instance.children = [{ type: "text", value }];
+ }
+ });
+};
+
+export const TextContent = ({
+ instanceId,
+ meta,
+ propName,
+ computedValue,
+}: ControlProps<"textContent">) => {
+ const instance = useInstance(instanceId);
+ // text content control is rendered only when empty or single child are present
+ const child = instance?.children?.[0] ?? { type: "text", value: "" };
+ const localValue = useLocalValue(String(computedValue ?? ""), (value) =>
+ updateChildren(instanceId, "text", value)
+ );
+ const id = useId();
+ const label = getLabel(meta, propName);
+
+ const { scope, aliases } = useStore($selectedInstanceScope);
+ let expression: undefined | string;
+ if (child?.type === "text") {
+ expression = JSON.stringify(child.value);
+ }
+
+ return (
+ <VerticalLayout
+ label={
+ <Flex align="center" css={{ gap: theme.spacing[3] }}>
+ <Label truncate>{label}</Label>
+ </Flex>
+ }
+ deletable={false}
+ onDelete={() => {}}
+ >
+ <BindingControl>
+ <TextArea
+ id={id}
+ autoGrow
+ value={localValue.value}
+ rows={1}
+ onChange={localValue.set}
+ onBlur={localValue.save}
+ onSubmit={localValue.save}
+ />
+ {expression !== undefined && (
+ <BindingPopover
+ scope={scope}
+ aliases={aliases}
+ validate={(value) => {
+ if (value !== undefined && typeof value !== "string") {
+ return `${label} expects a string value`;
+ }
+ }}
+ removable={false}
+ value={expression}
+ onChange={(_newExpression) => {}}
+ onRemove={(evaluatedValue) =>
+ updateChildren(instanceId, "text", String(evaluatedValue))
+ }
+ />
+ )}
+ </BindingControl>
+ </VerticalLayout>
+ );
+};
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
index 642eaf64db..e356315cb0 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
@@ -4,11 +4,13 @@ import {
type PropMeta,
showAttribute,
decodeDataSourceVariable,
+ textContentAttribute,
} from "@webstudio-is/react-sdk";
import type { PropValue } from "../shared";
import { useStore } from "@nanostores/react";
import {
$dataSources,
+ $registeredComponentMetas,
$registeredComponentPropsMetas,
} from "~/shared/nano-states";
@@ -132,9 +134,9 @@ const systemPropsMeta: { name: string; meta: PropMeta }[] = [
meta: {
label: "Show",
required: false,
- defaultValue: true,
control: "boolean",
type: "boolean",
+ defaultValue: true,
},
},
];
@@ -146,6 +148,9 @@ export const usePropsLogic = ({
updateProp,
deleteProp,
}: UsePropsLogicInput) => {
+ const instanceMeta = useStore($registeredComponentMetas).get(
+ instance.component
+ );
const meta = useStore($registeredComponentPropsMetas).get(instance.component);
const dataSources = useStore($dataSources);
@@ -191,6 +196,23 @@ export const usePropsLogic = ({
readOnly: isReadOnly(saved),
};
});
+ const canHaveTextContent = instanceMeta?.type === "container";
+ const hasNoChildren = instance.children.length === 0;
+ const hasOnlyTextChildren =
+ instance.children.length === 1 && instance.children[0].type === "text";
+ if (canHaveTextContent && (hasNoChildren || hasOnlyTextChildren)) {
+ systemProps.push({
+ propName: textContentAttribute,
+ meta: {
+ label: "Text Content",
+ required: false,
+ control: "textContent",
+ type: "string",
+ defaultValue: "",
+ },
+ readOnly: false,
+ });
+ }
const initialProps: PropAndMeta[] = [];
for (const name of initialPropsNames) {
diff --git a/apps/builder/app/shared/nano-states/props.test.ts b/apps/builder/app/shared/nano-states/props.test.ts
index 19a1ffa138..28ea13ea3f 100644
--- a/apps/builder/app/shared/nano-states/props.test.ts
+++ b/apps/builder/app/shared/nano-states/props.test.ts
@@ -1,7 +1,10 @@
-import { expect, test } from "@jest/globals";
+import { beforeEach, expect, test } from "@jest/globals";
import { cleanStores } from "nanostores";
import type { Instance, Page } from "@webstudio-is/sdk";
-import { collectionComponent } from "@webstudio-is/react-sdk";
+import {
+ collectionComponent,
+ textContentAttribute,
+} from "@webstudio-is/react-sdk";
import { $instances } from "./instances";
import {
$propValuesByInstanceSelector,
@@ -14,6 +17,7 @@ import {
$dataSources,
$props,
$resourceValues,
+ $resources,
} from "./nano-states";
import { $params } from "~/canvas/stores";
@@ -37,6 +41,13 @@ const selectPageRoot = (rootInstanceId: Instance["id"]) => {
$selectedPageId.set("pageId");
};
+beforeEach(() => {
+ $instances.set(new Map());
+ $props.set(new Map());
+ $resources.set(new Map());
+ $dataSources.set(new Map());
+});
+
test("collect prop values", () => {
setBoxInstance("box");
selectPageRoot("box");
@@ -418,6 +429,60 @@ test("compute props bound to resource variables", () => {
cleanStores($propValuesByInstanceSelector);
});
+test("compute instance text content when plain text", () => {
+ $instances.set(
+ toMap([
+ {
+ id: "body",
+ type: "instance",
+ component: "Body",
+ children: [
+ { type: "id", value: "plainBox" },
+ { type: "id", value: "richBox" },
+ ],
+ },
+ {
+ id: "plainBox",
+ type: "instance",
+ component: "Box",
+ children: [{ type: "text", value: "plain" }],
+ },
+ {
+ id: "richBox",
+ type: "instance",
+ component: "Box",
+ children: [
+ { type: "text", value: "plain" },
+ { type: "id", value: "bold" },
+ ],
+ },
+ {
+ id: "bold",
+ type: "instance",
+ component: "Bold",
+ children: [{ type: "text", value: "bold" }],
+ },
+ ])
+ );
+ selectPageRoot("body");
+ expect($propValuesByInstanceSelector.get()).toEqual(
+ new Map([
+ [JSON.stringify(["body"]), new Map<string, unknown>()],
+ [
+ JSON.stringify(["plainBox", "body"]),
+ new Map<string, unknown>([[textContentAttribute, "plain"]]),
+ ],
+ [JSON.stringify(["richBox", "body"]), new Map<string, unknown>()],
+ [
+ JSON.stringify(["bold", "richBox", "body"]),
+ new Map<string, unknown>([[textContentAttribute, "bold"]]),
+ ],
+ ])
+ );
+
+ cleanStores($propValuesByInstanceSelector);
+});
+
test("compute variable values for root", () => {
$instances.set(
toMap([{ id: "body", type: "instance", component: "Body", children: [] }])
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index aaa22488c8..f5a4ef4038 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -13,6 +13,7 @@ import {
generateDataSources,
normalizeProps,
portalComponent,
+ textContentAttribute,
} from "@webstudio-is/react-sdk";
import { $instances } from "./instances";
import {
@@ -238,6 +239,10 @@ export const $propValuesByInstanceSelector = computed(
return;
}
for (const child of instance.children) {
+ // plain text can be edited from props panel
+ if (child.type === "text" && instance.children.length === 1) {
+ propValues.set(textContentAttribute, child.value);
+ }
if (child.type === "id") {
traverseInstances([child.value, ...instanceSelector]);
}
diff --git a/packages/react-sdk/src/prop-meta.ts b/packages/react-sdk/src/prop-meta.ts
index 72bec1e607..95d2645a3d 100644
--- a/packages/react-sdk/src/prop-meta.ts
+++ b/packages/react-sdk/src/prop-meta.ts
@@ -157,6 +157,13 @@ const Action = z.object({
defaultValue: z.undefined().optional(),
});
+const TextContent = z.object({
+ ...common,
+ control: z.literal("textContent"),
+ type: z.literal("string"),
+ defaultValue: z.string().optional(),
+});
+
export const PropMeta = z.union([
Number,
Range,
@@ -175,6 +182,7 @@ export const PropMeta = z.union([
Json,
Date,
Action,
+ TextContent,
]);
export type PropMeta = z.infer<typeof PropMeta>;
diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.ts
index fb48bfbf23..7730e3a56b 100644
--- a/packages/react-sdk/src/props.ts
+++ b/packages/react-sdk/src/props.ts
@@ -77,6 +77,7 @@ export const componentAttribute = "data-ws-component" as const;
export const showAttribute = "data-ws-show" as const;
export const indexAttribute = "data-ws-index" as const;
export const collapsedAttribute = "data-ws-collapsed" as const;
+export const textContentAttribute = "data-ws-text-content" as const;
export const getInstanceIdFromComponentProps = (
props: Record<string, unknown>