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.

refactor: drop embed template usages (#5040)

Bogdan Chadkin5bfd37f4139b
diff --git a/apps/builder/app/shared/copy-paste/init-copy-paste.ts b/apps/builder/app/shared/copy-paste/init-copy-paste.ts
index 07988e48e0..8a76bec724 100644
--- a/apps/builder/app/shared/copy-paste/init-copy-paste.ts
+++ b/apps/builder/app/shared/copy-paste/init-copy-paste.ts
@@ -4,7 +4,6 @@ import {
$textEditingInstanceSelector,
} from "../nano-states";
import * as instance from "./plugin-instance";
-import * as embedTemplate from "./plugin-embed-template";
import * as markdown from "./plugin-markdown";
import * as webflow from "./plugin-webflow/plugin-webflow";
import { builderApi } from "../builder-api";
@@ -135,7 +134,7 @@ const initPlugins = ({
export const initCopyPaste = ({ signal }: { signal: AbortSignal }) => {
initPlugins({
- plugins: [instance, embedTemplate, markdown, webflow],
+ plugins: [instance, markdown, webflow],
signal,
});
};
diff --git a/apps/builder/app/shared/copy-paste/plugin-embed-template.ts b/apps/builder/app/shared/copy-paste/plugin-embed-template.ts
deleted file mode 100644
index a86a23ba11..0000000000
--- a/apps/builder/app/shared/copy-paste/plugin-embed-template.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { z } from "zod";
-import { WsEmbedTemplate } from "@webstudio-is/sdk";
-import { generateDataFromEmbedTemplate } from "@webstudio-is/react-sdk";
-import { $registeredComponentMetas } from "../nano-states";
-import {
- findClosestInsertable,
- insertWebstudioFragmentAt,
-} from "../instance-utils";
-
-const version = "@webstudio/template";
-
-const ClipboardData = z.object({ [version]: WsEmbedTemplate });
-
-const parse = (clipboardData: string): WsEmbedTemplate | undefined => {
- try {
- const data = ClipboardData.parse(JSON.parse(clipboardData));
- return data[version];
- } catch {
- return;
- }
-};
-
-export const mimeType = "text/plain";
-
-export const onPaste = (clipboardData: string) => {
- const template = parse(clipboardData);
- if (template === undefined) {
- return false;
- }
- const metas = $registeredComponentMetas.get();
- const fragment = generateDataFromEmbedTemplate(template, metas);
- const insertable = findClosestInsertable(fragment);
- if (insertable === undefined) {
- return false;
- }
- insertWebstudioFragmentAt(fragment, insertable);
- return true;
-};
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index f0609c257e..32852a385e 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -1,3 +1,4 @@
+import { current, isDraft } from "immer";
import { nanoid } from "nanoid";
import { toast } from "@webstudio-is/design-system";
import { equalMedia, type StyleValue } from "@webstudio-is/css-engine";
@@ -28,7 +29,6 @@ import {
collectionComponent,
Prop,
} from "@webstudio-is/sdk";
-import { generateDataFromEmbedTemplate } from "@webstudio-is/react-sdk";
import {
$props,
$styles,
@@ -74,7 +74,6 @@ import {
restoreExpressionVariables,
unsetExpressionVariables,
} from "./data-variables";
-import { current, isDraft } from "immer";
/**
* structuredClone can be invoked on draft and throw error
@@ -372,17 +371,24 @@ export const getComponentTemplateData = (
if (templateMeta) {
return templateMeta.template;
}
- const metas = $registeredComponentMetas.get();
- const componentMeta = metas.get(componentOrTemplate);
- // when template not specified fallback to template with the component
- const template = componentMeta?.template ?? [
- {
- type: "instance",
- component: componentOrTemplate,
- children: [],
- },
- ];
- return generateDataFromEmbedTemplate(template, metas);
+ const newInstance: Instance = {
+ id: nanoid(),
+ type: "instance",
+ component: componentOrTemplate,
+ children: [],
+ };
+ return {
+ children: [{ type: "id", value: newInstance.id }],
+ instances: [newInstance],
+ props: [],
+ dataSources: [],
+ styleSourceSelections: [],
+ styleSources: [],
+ styles: [],
+ breakpoints: [],
+ assets: [],
+ resources: [],
+ };
};
export const reparentInstanceMutable = (
diff --git a/packages/react-sdk/src/embed-template.test.tsx b/packages/react-sdk/src/embed-template.test.tsx
index e9c5749683..8a594f2203 100644
--- a/packages/react-sdk/src/embed-template.test.tsx
+++ b/packages/react-sdk/src/embed-template.test.tsx
@@ -665,7 +665,7 @@ test("generate data for embedding from instance child bound to variables", () =>
});
});
-test("add namespace to selected components in embed template", () => {
+test("add namespace to constants and indexWithinAncestor", () => {
expect(
namespaceMeta(
{
@@ -677,26 +677,6 @@ test("add namespace to selected components in embed template", () => {
component: { $nin: ["Button", "Box"] },
},
indexWithinAncestor: "Tooltip",
- template: [
- {
- type: "instance",
- component: "Tooltip",
- children: [
- { type: "text", value: "Some text" },
- {
- type: "instance",
- component: "Box",
- children: [
- {
- type: "instance",
- component: "Button",
- children: [],
- },
- ],
- },
- ],
- },
- ],
},
"my-namespace",
new Set(["Tooltip", "Button"])
@@ -710,25 +690,5 @@ test("add namespace to selected components in embed template", () => {
component: { $nin: ["my-namespace:Button", "my-namespace:Box"] },
},
indexWithinAncestor: "my-namespace:Tooltip",
- template: [
- {
- type: "instance",
- component: "my-namespace:Tooltip",
- children: [
- { type: "text", value: "Some text" },
- {
- type: "instance",
- component: "Box",
- children: [
- {
- type: "instance",
- component: "my-namespace:Button",
- children: [],
- },
- ],
- },
- ],
- },
- ],
});
});
diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.ts
index 6d51982f81..970cca2959 100644
--- a/packages/react-sdk/src/embed-template.ts
+++ b/packages/react-sdk/src/embed-template.ts
@@ -276,35 +276,6 @@ export const generateDataFromEmbedTemplate = (
};
};
-const namespaceEmbedTemplateComponents = (
- template: WsEmbedTemplate,
- namespace: string,
- components: Set<EmbedTemplateInstance["component"]>
-): WsEmbedTemplate => {
- return template.map((item) => {
- if (item.type === "text") {
- return item;
- }
- if (item.type === "expression") {
- return item;
- }
- if (item.type === "instance") {
- const prefix = components.has(item.component) ? `${namespace}:` : "";
- return {
- ...item,
- component: `${prefix}${item.component}`,
- children: namespaceEmbedTemplateComponents(
- item.children,
- namespace,
- components
- ),
- };
- }
- item satisfies never;
- throw Error("Impossible case");
- });
-};
-
const namespaceMatcher = (namespace: string, matcher: Matcher) => {
const newMatcher = structuredClone(matcher);
if (newMatcher.component?.$eq) {
@@ -346,12 +317,5 @@ export const namespaceMeta = (
? `${namespace}:${newMeta.indexWithinAncestor}`
: newMeta.indexWithinAncestor;
}
- if (newMeta.template) {
- newMeta.template = namespaceEmbedTemplateComponents(
- newMeta.template,
- namespace,
- components
- );
- }
return newMeta;
};
diff --git a/packages/sdk/src/schema/component-meta.ts b/packages/sdk/src/schema/component-meta.ts
index 5afee52d0e..1a2f31d26a 100644
--- a/packages/sdk/src/schema/component-meta.ts
+++ b/packages/sdk/src/schema/component-meta.ts
@@ -2,7 +2,6 @@ import { z } from "zod";
import type { HtmlTags } from "html-tags";
import { PropMeta } from "./prop-meta";
import { Matchers } from "./instances";
-import { WsEmbedTemplate } from "./embed-template";
import { StyleValue, type CssProperty } from "@webstudio-is/css-engine";
import type { Simplify } from "type-fest";
@@ -87,7 +86,6 @@ export const WsComponentMeta = z.object({
icon: z.string(),
presetStyle: z.optional(z.record(z.string(), z.array(PresetStyleDecl))),
states: z.optional(z.array(ComponentState)),
- template: z.optional(WsEmbedTemplate),
order: z.number().optional(),
});