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.

fix: Make Content Block template JSX insertable (#6128)

Oleg Isonen523f555cbaec
diff --git a/packages/project-build/package.json b/packages/project-build/package.json
index d072f9baeb..07dff15717 100644
--- a/packages/project-build/package.json
+++ b/packages/project-build/package.json
@@ -89,6 +89,7 @@
"acorn-jsx": "^5.3.2",
"allof-merge": "^0.6.8",
"bcp-47": "^2.1.0",
+ "change-case": "^5.4.4",
"css-tree": "^3.1.0",
"esbuild": "^0.25.3",
"fast-deep-equal": "^3.1.3",
diff --git a/packages/project-build/src/mcp.test.ts b/packages/project-build/src/mcp.test.ts
index 5b9f2f0d42..2a6d0ab340 100644
--- a/packages/project-build/src/mcp.test.ts
+++ b/packages/project-build/src/mcp.test.ts
@@ -5254,6 +5254,10 @@ describe("project session mcp adapter", () => {
name: "components.get",
input: { component: "ws:collection" },
});
+ const blockTemplateDetails = await adapter.callTool({
+ name: "components.get",
+ input: { component: "ws:block-template" },
+ });
const italicDetails = await adapter.callTool({
name: "components.get",
input: { component: "Italic" },
@@ -5738,6 +5742,7 @@ describe("project session mcp adapter", () => {
expect(collectionDetails.structuredContent.data).toEqual(
expect.objectContaining({
component: "ws:collection",
+ jsxElement: "<ws.collection />",
description: expect.stringContaining("array or object"),
collectionUsage: expect.stringMatching(
/insert-collection.*private item\/itemKey parameters.*atomically/
@@ -5750,6 +5755,13 @@ describe("project session mcp adapter", () => {
}),
})
);
+ expect(blockTemplateDetails.structuredContent.data).toEqual(
+ expect.objectContaining({
+ component: "ws:block-template",
+ jsxElement: "<ws.blockTemplate />",
+ standaloneInsertable: false,
+ })
+ );
expect(selectTemplateDetails.structuredContent.data).toEqual(
expect.objectContaining({
found: true,
diff --git a/packages/project-build/src/mcp.ts b/packages/project-build/src/mcp.ts
index df49742509..df58a30138 100644
--- a/packages/project-build/src/mcp.ts
+++ b/packages/project-build/src/mcp.ts
@@ -66,7 +66,12 @@ import {
ListToolsRequestSchema,
ReadResourceRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
-import { componentMetas } from "@webstudio-is/sdk-components-registry/metas";
+import {
+ animationComponentNamespace,
+ componentMetas,
+ radixComponentNamespace,
+} from "@webstudio-is/sdk-components-registry/metas";
+import { camelCase } from "change-case";
import { readProjectBuildDoc } from "./docs";
import type { ComponentTemplateRegistry } from "./runtime/component-template";
import {
@@ -4594,11 +4599,15 @@ const getComponentSummaryEntry = ({
const [parsedNamespace, exportName] = parseComponentName(component);
const namespace = parsedNamespace ?? "global";
const jsxNamespace =
- parsedNamespace === "@webstudio-is/sdk-components-react-radix"
+ parsedNamespace === radixComponentNamespace
? "radix"
- : parsedNamespace === "@webstudio-is/sdk-components-animation"
+ : parsedNamespace === animationComponentNamespace
? "animation"
- : "$";
+ : parsedNamespace === "ws"
+ ? "ws"
+ : "$";
+ const jsxExportName =
+ parsedNamespace === "ws" ? camelCase(exportName) : exportName;
const template = templateMeta?.template;
const hasTemplate = template !== undefined;
const instancesById = new Map(
@@ -4642,7 +4651,7 @@ const getComponentSummaryEntry = ({
component,
exportName,
namespace,
- jsxElement: `<${jsxNamespace}.${exportName} />`,
+ jsxElement: `<${jsxNamespace}.${jsxExportName} />`,
label: meta.label,
category: visibleCategory,
contentCategory: meta.contentModel?.category,
diff --git a/packages/project-build/src/runtime/components.test.ts b/packages/project-build/src/runtime/components.test.ts
index 84dca0a422..a15aeed63f 100644
--- a/packages/project-build/src/runtime/components.test.ts
+++ b/packages/project-build/src/runtime/components.test.ts
@@ -1402,6 +1402,38 @@ test("inserts authored template part structure from webstudio jsx fragments", as
]);
});
+test("inserts authored Content Block template structure from webstudio jsx fragments", async () => {
+ const parent = createParent();
+ const fragment = await parseWebstudioJsxFragment(
+ `<ws.block><ws.blockTemplate><ws.element ws:tag="section"><ws.element ws:tag="h2">Title</ws.element></ws.element></ws.blockTemplate></ws.block>`
+ );
+
+ const mutation = insertFragment(
+ createState(parent),
+ {
+ parentInstanceId: parent.id,
+ fragment,
+ },
+ {
+ createId: createIdFactory(),
+ projectId: "project-id",
+ }
+ );
+
+ const instances = getAddedValues<Instance>(mutation, "instances");
+ expect(instances).toEqual([
+ expect.objectContaining({ component: "ws:block" }),
+ expect.objectContaining({ component: "ws:block-template" }),
+ expect.objectContaining({ component: elementComponent, tag: "section" }),
+ expect.objectContaining({ component: elementComponent, tag: "h2" }),
+ ]);
+ const [block, blockTemplate, section, heading] = instances;
+ expect(block.children).toEqual([{ type: "id", value: blockTemplate.id }]);
+ expect(blockTemplate.children).toEqual([{ type: "id", value: section.id }]);
+ expect(section.children).toEqual([{ type: "id", value: heading.id }]);
+ expect(heading.children).toEqual([{ type: "text", value: "Title" }]);
+});
+
test("supports react style props in webstudio jsx fragments", async () => {
const fragment = await parseWebstudioJsxFragment(
`<$.Box style={{ padding: 24 }}>Hello</$.Box>`
diff --git a/packages/sdk-components-registry/src/metas.ts b/packages/sdk-components-registry/src/metas.ts
index e77d2530a8..6b606be879 100644
--- a/packages/sdk-components-registry/src/metas.ts
+++ b/packages/sdk-components-registry/src/metas.ts
@@ -14,3 +14,5 @@ for (const library of componentMetaLibraries) {
componentMetas.set(getComponentName(library, exportName), meta);
}
}
+
+export { animationComponentNamespace, radixComponentNamespace } from "./shared";
diff --git a/packages/template/package.json b/packages/template/package.json
index 33a3ffeb00..50687f1f93 100644
--- a/packages/template/package.json
+++ b/packages/template/package.json
@@ -20,6 +20,7 @@
"@webstudio-is/css-engine": "workspace:*",
"@webstudio-is/react-sdk": "workspace:*",
"@webstudio-is/sdk": "workspace:*",
+ "change-case": "^5.4.4",
"react": "18.3.0-canary-14898b6a9-20240318"
},
"devDependencies": {
diff --git a/packages/template/src/jsx.test.tsx b/packages/template/src/jsx.test.tsx
index b2b3c2246f..ac734d8e31 100644
--- a/packages/template/src/jsx.test.tsx
+++ b/packages/template/src/jsx.test.tsx
@@ -1144,3 +1144,16 @@ test("render ws:element with ws:tag prop", () => {
]);
expect(props).toEqual([]);
});
+
+test("render camel-cased ws component as canonical kebab-case", () => {
+ const { instances } = renderTemplate(<ws.blockTemplate />);
+
+ expect(instances).toEqual([
+ {
+ type: "instance",
+ id: "0",
+ component: "ws:block-template",
+ children: [],
+ },
+ ]);
+});
diff --git a/packages/template/src/jsx.ts b/packages/template/src/jsx.ts
index 404ded16bc..234b4b7f73 100644
--- a/packages/template/src/jsx.ts
+++ b/packages/template/src/jsx.ts
@@ -1,4 +1,5 @@
import { Fragment, type JSX, type ReactNode } from "react";
+import { kebabCase } from "change-case";
import { hyphenateProperty } from "@webstudio-is/css-engine";
import {
animationAction,
@@ -846,13 +847,16 @@ type Component = { displayName: string } & ((
props: ComponentProps
) => ReactNode);
-export const createProxy = (prefix: string): Record<string, Component> => {
+export const createProxy = (
+ prefix: string,
+ transformName: (name: string) => string = (name) => name
+): Record<string, Component> => {
return new Proxy(
{},
{
get(_target, prop) {
const component: Component = () => undefined;
- component.displayName = `${prefix}${prop as string}`;
+ component.displayName = `${prefix}${transformName(String(prop))}`;
return component;
},
}
@@ -861,4 +865,4 @@ export const createProxy = (prefix: string): Record<string, Component> => {
export const $: Record<string, Component> = createProxy("");
-export const ws: Record<string, Component> = createProxy("ws:");
+export const ws: Record<string, Component> = createProxy("ws:", kebabCase);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index cd4b24d3cb..9865f9cbfb 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2261,6 +2261,9 @@ importers:
bcp-47:
specifier: ^2.1.0
version: 2.1.0
+ change-case:
+ specifier: ^5.4.4
+ version: 5.4.4
css-tree:
specifier: ^3.1.0
version: 3.1.0
@@ -2996,6 +2999,9 @@ importers:
'@webstudio-is/sdk':
specifier: workspace:*
version: link:../sdk
+ change-case:
+ specifier: ^5.4.4
+ version: 5.4.4
react:
specifier: 18.3.0-canary-14898b6a9-20240318
version: 18.3.0-canary-14898b6a9-20240318