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
fix: Reload edited Video Content Block templates (#6512)
Oleg Isonenc7bb11310da6
diff --git a/packages/project-build/src/runtime/mdx-source.test.ts b/packages/project-build/src/runtime/mdx-source.test.tsindex 21973cdd99..d8da4a62a9 100644--- a/packages/project-build/src/runtime/mdx-source.test.ts+++ b/packages/project-build/src/runtime/mdx-source.test.ts@@ -9,6 +9,9 @@ import {type WebstudioData,} from "@webstudio-is/sdk";import { componentMetas } from "@webstudio-is/sdk-components-registry/metas";+import { componentIds } from "@webstudio-is/sdk-components-registry/components";+import { renderTemplate } from "@webstudio-is/template";+import { meta as youtubeTemplate } from "../../../sdk-components-react/src/youtube.template";import {rebaseMdxAuthoredContent,serializeMdxAuthoredContent,@@ -121,6 +124,112 @@ const createAssetSourceData = () => {};describe("materializeMdxSource", () => {+ test("preserves template component descendants when editing a default prop", async () => {+ const sourceData = createSourceData();+ const templateFragment = renderTemplate(+ youtubeTemplate.template,+ undefined,+ [],+ { componentIds }+ );+ const templateRoot = templateFragment.instances.find(+ ({ label }) => label === "YouTube"+ );+ if (templateRoot === undefined) {+ throw new Error("Expected YouTube template root");+ }+ templateRoot.label = "Video";+ for (const instance of templateFragment.instances) {+ sourceData.instances.set(instance.id, instance);+ }+ for (const [id, label] of [+ ["html-embed-template", "HtmlEmbed"],+ ["other-div-template", "Other div template"],+ ]) {+ sourceData.instances.set(id, {+ type: "instance",+ id,+ component: elementComponent,+ tag: "div",+ label,+ children: [],+ });+ sourceData.instances.get("templates")?.children.push({+ type: "id",+ value: id,+ });+ }+ for (const prop of templateFragment.props) {+ sourceData.props.set(prop.id, prop);+ }+ sourceData.instances.get("templates")?.children.push({+ type: "id",+ value: templateRoot.id,+ });++ const result = await materializeMdxSource({+ source: "<Video />\n",+ identity,+ data: sourceData,+ metas: componentMetas,+ projectId: "project",+ });+ const previewImage = result.root.fragment.instances.find(+ ({ component }) => component === "VimeoPreviewImage"+ );+ if (previewImage === undefined) {+ throw new Error("Expected YouTube preview image");+ }+ const edited = structuredClone(result.root.fragment);+ edited.props.push({+ id: "replacement-preview-image-src",+ instanceId: previewImage.id,+ name: "src",+ type: "string",+ value: "/replacement.png",+ });++ const serialized = await serializeMdxAuthoredContent({+ root: result.root,+ fragment: edited,+ });++ expect(serialized).toContain('<img src="/replacement.png" />');++ const reloaded = await materializeMdxSource({+ source: serialized,+ identity,+ data: sourceData,+ metas: componentMetas,+ projectId: "project",+ });+ expect(reloaded.diagnostics).toEqual([]);+ expect(+ reloaded.root.fragment.instances.map(({ component }) => component)+ ).toEqual(+ expect.arrayContaining([+ "VimeoPreviewImage",+ "VimeoSpinner",+ "VimeoPlayButton",+ ])+ );+ expect(reloaded.root.fragment.props).toContainEqual(+ expect.objectContaining({+ instanceId: reloaded.root.fragment.instances.find(+ ({ component }) => component === "VimeoPreviewImage"+ )?.id,+ name: "src",+ value: "/replacement.png",+ })+ );+ await expect(+ serializeMdxAuthoredContent({+ root: reloaded.root,+ fragment: reloaded.root.fragment,+ })+ ).resolves.toBe(serialized);+ });+test.each([["without", 0],["with multiple", 2],diff --git a/packages/project-build/src/runtime/mdx-template-resolution.test.ts b/packages/project-build/src/runtime/mdx-template-resolution.test.tsindex 5b27d64c05..565ac32237 100644--- a/packages/project-build/src/runtime/mdx-template-resolution.test.ts+++ b/packages/project-build/src/runtime/mdx-template-resolution.test.ts@@ -281,6 +281,36 @@ describe("resolveMdxTemplates", () => {});});+ test("keeps raw HTML inside a named template from matching other templates", async () => {+ const instances = createInstances();+ const templates = instances.get("templates");+ if (templates === undefined) {+ throw new Error("Expected Templates container");+ }+ for (const id of ["first-div", "second-div"]) {+ instances.set(id, createInstance(id, elementComponent, { tag: "div" }));+ templates.children.push({ type: "id", value: id });+ }+ const document = await parseMdxDocument({+ source: "<Card><div><Card /></div></Card>\n<div />\n",+ });++ const result = resolveMdxTemplates({+ document,+ identity,+ instances,+ metas,+ });++ expect(result.references.map(({ path }) => path)).toEqual([[0], [0, 0, 0]]);+ expect(result.diagnostics).toEqual([+ expect.objectContaining({+ code: "ambiguous-template",+ semanticKey: "element:div",+ }),+ ]);+ });+test("reads a legacy label alias and resolves it to the stable name", async () => {const instances = createInstances();const card = instances.get("card");diff --git a/packages/project-build/src/runtime/mdx-template-resolution.ts b/packages/project-build/src/runtime/mdx-template-resolution.tsindex 94ace5257c..4e8552f580 100644--- a/packages/project-build/src/runtime/mdx-template-resolution.ts+++ b/packages/project-build/src/runtime/mdx-template-resolution.ts@@ -165,10 +165,21 @@ export const resolveMdxTemplates = ({const visit = (nodes: readonly MdxAuthoredNode[],- parentPath: readonly number[]+ parentPath: readonly number[],+ withinNamedTemplate = false) => {for (const [index, node] of nodes.entries()) {const path = [...parentPath, index];+ // A <div> inside <Video> belongs to Video, not an unrelated div template.+ if (+ withinNamedTemplate &&+ node.type === "element" &&+ node.syntax === "mdx"+ ) {+ visit(node.children, path, true);+ continue;+ }+ let resolvedNamedTemplate = false;if (node.type === "template") {const standard = getMdxStandardTemplateBinding(node);const templateIds =@@ -199,6 +210,7 @@ export const resolveMdxTemplates = ({continue;}if (templateIds?.length === 1) {+ resolvedNamedTemplate = true;const templateInstance = instances.get(templateIds[0]);const componentBinding =templateInstance === undefined@@ -331,7 +343,11 @@ export const resolveMdxTemplates = ({node.type !== "comment" &&node.type !== "opaque") {- visit(node.children, path);+ visit(+ node.children,+ path,+ withinNamedTemplate || resolvedNamedTemplate+ );}}};