mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
Closes #6306 ## Outcome Content Block templates are now the public component model for MDX authoring. - Standard Markdown resolves through matching Content Block templates. - PascalCase JSX resolves to a named template first, then to a registered component. - Adding or changing a template immediately affects matching MDX, including content authored before that template existed. - Every supported standard MDX element is present in new Content Blocks by default. - Template `name` is a stable PascalCase JSX identifier. `label` remains optional display text only. - Lowercase JSX represents native HTML and SVG without exposing `ws.element` in authored syntax. - Unknown, ambiguous, and unsafe JSX stays in source, produces a source-ranged Builder diagnostic, and is omitted from published output. - Frontmatter images preserve exact `$ref` values and use the Asset description for `alt` text. - Keyboard and mouse insertion from the slash menu remove the trigger slash and preserve the selected template. ## Public MDX contract - Markdown is canonical for standard prose. - Lowercase JSX creates native elements: `<section><p>Text</p></section>`. - PascalCase JSX uses templates and registered components: `<PromotionCard />`, `<Heading tag="h1">Title</Heading>`, and `<YouTube src="https://youtu.be/KI5JHpzBK1s" />`. - Component-name collisions use stable qualified identifiers such as `Checkbox` and `RadixCheckbox`. - A self-closing template keeps its designed default subtree. An explicitly paired empty template clears it. - Static quoted values and bare booleans are supported. - Imports, exports, expressions, spreads, functions, executable JavaScript, unsafe attributes, `<$.*>`, and authored `<ws.element>` are rejected. - `ws` remains only for compiler primitives with no React component. ## Implementation - [x] Add stable, unique template names and keep them separate from labels. - [x] Parse and validate Markdown and JSX through their ASTs with precise source ranges. - [x] Share component identity between template JSX, Content MDX, Builder materialization, reload, copy, and publication. - [x] Resolve templates live after add, edit, rename, reorder, removal, undo/redo, collaboration updates, and reload. - [x] Preserve authored props, children, Asset references, template defaults, and the exact slash-menu template choice. - [x] Make one typed MDX semantic inventory drive default templates and slash-menu eligibility. - [x] Move component-dependent template assembly into the component registry. - [x] Replace production `$`, `animation`, and `radix` JSX namespaces with direct component imports. - [x] Keep only explicit compiler primitives in `ws` template JSX. - [x] Migrate repository templates, fixtures, stories, examples, MCP guidance, CLI guidance, and generated outputs. - [x] Add Builder unit and Playwright coverage for authoring, diagnostics, insertion, synchronization, reload, and publication. - [x] Review and update the authoritative Content Block and generated MCP documentation. ## Compatibility - Persisted project data continues to use namespaced component IDs and `ws:element` for generic HTML elements. No project-data migration is required. - Legacy `<ws.element ws:tag="…">`, `<ws.element ws:name="…">`, `<$.*>`, `<animation.*>`, and `<radix.*>` input remains readable during the compatibility window. - Current serializers and repository-owned sources emit only canonical Markdown, lowercase intrinsic JSX, or PascalCase identifiers. - Untouched user MDX is not rewritten automatically. Legacy syntax is canonicalized when its owning source is edited. - Deferred follow-up: remove legacy namespace readers and proxy support only after the canonical writer is deployed and existing projects are backfilled. This is intentionally outside this PR. ## Verification - [x] CI package tests, typechecks, lint, package boundaries, generated assets, migrations, and size checks pass. - [x] All six Builder E2E shards pass. - [x] Fixture checks pass on Ubuntu and Windows. - [x] Targeted local Content Engine, Template, SDK, registry, Project Build, migration, Builder, component, CLI, and Playwright tests pass. - [x] Runtime contracts, CLI docs, and MCP docs were regenerated from their owning sources. - [x] Local fixture generation completed without an unexpected fixture diff. - [x] Documentation impact reviewed and included in this PR. - [x] Review and approve the intentional List visual snapshot change. - [x] Run the complete agent evaluation suite after explicit approval. - [x] Run the network fixture link/sync pipeline against the matching PR deployment after explicit permission. ## Known visual change The generated List story now targets its actual default `ul` element instead of `ol`, so the default list styling appears in the visual snapshot. The visual change was reviewed and the approved visual check passes.
640 lines
16 KiB
TypeScript
640 lines
16 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
import type { BuilderPatchChange } from "@webstudio-is/project-build/contracts";
|
|
import {
|
|
blockComponent,
|
|
blockTemplateComponent,
|
|
elementComponent,
|
|
type Instance,
|
|
type Prop,
|
|
} from "@webstudio-is/sdk";
|
|
import {
|
|
$externalContentRoots,
|
|
getAffectedExternalContentTemplateRootKeys,
|
|
isExternalContentInstance,
|
|
isExternalContentMutation,
|
|
publishExternalContentTemplateMutation,
|
|
registerExternalContentRoot,
|
|
subscribeExternalContentTemplateMutations,
|
|
} from "./external-content-mutations";
|
|
|
|
afterEach(() => {
|
|
$externalContentRoots.set(new Map());
|
|
});
|
|
|
|
const instance = (
|
|
id: string,
|
|
children: Instance["children"] = []
|
|
): Instance => ({
|
|
type: "instance",
|
|
id,
|
|
component: elementComponent,
|
|
tag: "div",
|
|
children,
|
|
});
|
|
|
|
const state = {
|
|
instances: new Map([
|
|
["block", instance("block", [{ type: "id", value: "external" }])],
|
|
["external", instance("external")],
|
|
["ordinary", instance("ordinary")],
|
|
]),
|
|
props: new Map<string, Prop>([
|
|
[
|
|
"external-prop",
|
|
{
|
|
id: "external-prop",
|
|
instanceId: "external",
|
|
name: "title",
|
|
type: "string",
|
|
value: "Before",
|
|
},
|
|
],
|
|
]),
|
|
};
|
|
|
|
const change = (
|
|
namespace: BuilderPatchChange["namespace"],
|
|
patches: BuilderPatchChange["patches"],
|
|
revisePatches?: BuilderPatchChange["patches"]
|
|
): BuilderPatchChange & {
|
|
revisePatches?: BuilderPatchChange["patches"];
|
|
} => ({ namespace, patches, revisePatches });
|
|
|
|
describe("external content mutation detection", () => {
|
|
const roots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set(["external"]),
|
|
ownership: {
|
|
instances: new Set(["external"]),
|
|
props: new Set(["external-prop"]),
|
|
styles: new Set(["external:base:color"]),
|
|
},
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
|
|
test("detects edits to existing external instances and props", () => {
|
|
expect(
|
|
isExternalContentMutation({
|
|
state,
|
|
roots,
|
|
payload: [
|
|
change("instances", [
|
|
{
|
|
op: "replace",
|
|
path: ["external", "children"],
|
|
value: [{ type: "text", value: "After" }],
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
isExternalContentMutation({
|
|
state,
|
|
roots,
|
|
payload: [
|
|
change("props", [
|
|
{
|
|
op: "replace",
|
|
path: ["external-prop", "value"],
|
|
value: "After",
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toBe(true);
|
|
});
|
|
|
|
test("detects insertion into an empty connected block", () => {
|
|
expect(
|
|
isExternalContentMutation({
|
|
state,
|
|
roots,
|
|
payload: [
|
|
change("instances", [
|
|
{
|
|
op: "replace",
|
|
path: ["block", "children"],
|
|
value: [{ type: "id", value: "inserted" }],
|
|
},
|
|
{
|
|
op: "add",
|
|
path: ["inserted"],
|
|
value: instance("inserted"),
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toBe(true);
|
|
});
|
|
|
|
test("detects edits to every registered external namespace", () => {
|
|
expect(
|
|
isExternalContentMutation({
|
|
state,
|
|
roots,
|
|
payload: [
|
|
change("styles", [
|
|
{
|
|
op: "replace",
|
|
path: ["external:base:color", "value"],
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toBe(true);
|
|
});
|
|
|
|
test("does not reroute unrelated project mutations", () => {
|
|
expect(
|
|
isExternalContentMutation({
|
|
state,
|
|
roots,
|
|
payload: [
|
|
change("instances", [
|
|
{
|
|
op: "replace",
|
|
path: ["ordinary", "children"],
|
|
value: [{ type: "text", value: "After" }],
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
test("identifies template changes separately from authored content", () => {
|
|
const templateState = {
|
|
...state,
|
|
instances: new Map([
|
|
...state.instances,
|
|
[
|
|
"templates",
|
|
{
|
|
type: "instance" as const,
|
|
id: "templates",
|
|
component: blockTemplateComponent,
|
|
children: [],
|
|
},
|
|
] as const,
|
|
["heading-template", instance("heading-template")] as const,
|
|
]),
|
|
};
|
|
const templateRoots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
...roots.get("scope")!,
|
|
templateOwnership: {
|
|
instances: new Set(["templates", "heading-template"]),
|
|
styleSources: new Set(["heading-template-style"]),
|
|
styles: new Set(["heading-template:base:color"]),
|
|
},
|
|
},
|
|
],
|
|
]);
|
|
const payload = [
|
|
change("instances", [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["templates", "children"],
|
|
value: [{ type: "id", value: "heading-template" }],
|
|
},
|
|
]),
|
|
];
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload,
|
|
})
|
|
).toEqual(["scope"]);
|
|
expect(
|
|
isExternalContentMutation({ state, roots: templateRoots, payload })
|
|
).toBe(false);
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload: [
|
|
change("instances", [
|
|
{
|
|
op: "replace",
|
|
path: ["block", "children"],
|
|
value: [{ type: "id", value: "inserted" }],
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toEqual([]);
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload: [
|
|
change("styleSourceSelections", [
|
|
{
|
|
op: "add",
|
|
path: ["heading-template"],
|
|
value: {
|
|
instanceId: "heading-template",
|
|
values: ["heading-template-style"],
|
|
},
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload: [
|
|
change("styles", [
|
|
{
|
|
op: "add",
|
|
path: ["heading-template-style:base:background-color"],
|
|
value: {
|
|
breakpointId: "base",
|
|
styleSourceId: "heading-template-style",
|
|
property: "background-color",
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
});
|
|
|
|
test("detects removal of template records added before ownership refresh", () => {
|
|
const templateState = {
|
|
instances: new Map([
|
|
[
|
|
"block",
|
|
{
|
|
type: "instance" as const,
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [{ type: "id" as const, value: "templates" }],
|
|
},
|
|
],
|
|
[
|
|
"templates",
|
|
{
|
|
type: "instance" as const,
|
|
id: "templates",
|
|
component: blockTemplateComponent,
|
|
children: [{ type: "id" as const, value: "heading-template" }],
|
|
},
|
|
],
|
|
["heading-template", instance("heading-template")],
|
|
]),
|
|
props: new Map<string, Prop>(),
|
|
};
|
|
const templateRoots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
templateOwnership: {
|
|
instances: new Set(["templates", "heading-template"]),
|
|
props: new Set<string>(),
|
|
styleSources: new Set(["heading-template-style"]),
|
|
styles: new Set<string>(),
|
|
},
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
const removedProp: Prop = {
|
|
id: "new-template-prop",
|
|
instanceId: "heading-template",
|
|
name: "title",
|
|
type: "string",
|
|
value: "Temporary",
|
|
};
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload: [
|
|
change(
|
|
"props",
|
|
[{ op: "remove", path: [removedProp.id] }],
|
|
[{ op: "add", path: [removedProp.id], value: removedProp }]
|
|
),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: templateState,
|
|
roots: templateRoots,
|
|
payload: [
|
|
change(
|
|
"styles",
|
|
[{ op: "remove", path: ["new-template-style"] }],
|
|
[
|
|
{
|
|
op: "add",
|
|
path: ["new-template-style"],
|
|
value: {
|
|
breakpointId: "base",
|
|
styleSourceId: "heading-template-style",
|
|
property: "color",
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
},
|
|
]
|
|
),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
});
|
|
|
|
test("detects adding and removing the Templates container", () => {
|
|
const block = {
|
|
type: "instance" as const,
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [{ type: "id" as const, value: "templates" }],
|
|
};
|
|
const templates = {
|
|
type: "instance" as const,
|
|
id: "templates",
|
|
component: blockTemplateComponent,
|
|
children: [],
|
|
};
|
|
const roots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
sourceBlockInstanceId: "block",
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
templateOwnership: { instances: new Set(["templates"]) },
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: {
|
|
instances: new Map([
|
|
[block.id, block],
|
|
[templates.id, templates],
|
|
]),
|
|
},
|
|
roots,
|
|
payload: [
|
|
change("instances", [
|
|
{ op: "replace", path: ["block", "children"], value: [] },
|
|
]),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
|
|
const rootsWithoutTemplates = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
...roots.get("scope")!,
|
|
templateOwnership: { instances: new Set<string>() },
|
|
},
|
|
],
|
|
]);
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: { instances: new Map([[block.id, { ...block, children: [] }]]) },
|
|
roots: rootsWithoutTemplates,
|
|
payload: [
|
|
change("instances", [
|
|
{ op: "add", path: ["templates"], value: templates },
|
|
{
|
|
op: "replace",
|
|
path: ["block", "children"],
|
|
value: [{ type: "id", value: "templates" }],
|
|
},
|
|
]),
|
|
],
|
|
})
|
|
).toEqual(["scope"]);
|
|
});
|
|
|
|
test("detects remotely applied Templates container changes", () => {
|
|
const template = {
|
|
type: "instance" as const,
|
|
id: "templates",
|
|
component: blockTemplateComponent,
|
|
children: [],
|
|
};
|
|
const removalRoots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
sourceBlockInstanceId: "block",
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
templateContainerIds: [template.id],
|
|
templateOwnership: { instances: new Set([template.id]) },
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
const removalPayload = [
|
|
change("instances", [
|
|
{ op: "replace" as const, path: ["block", "children"], value: [] },
|
|
{ op: "remove" as const, path: [template.id] },
|
|
]),
|
|
];
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: {
|
|
instances: new Map([
|
|
[
|
|
"block",
|
|
{
|
|
type: "instance",
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [],
|
|
},
|
|
],
|
|
]),
|
|
},
|
|
roots: removalRoots,
|
|
payload: removalPayload,
|
|
})
|
|
).toEqual(["scope"]);
|
|
|
|
const additionRoots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
sourceBlockInstanceId: "block",
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
templateContainerIds: [],
|
|
templateOwnership: { instances: new Set<string>() },
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
const additionPayload = [
|
|
change("instances", [
|
|
{ op: "add" as const, path: [template.id], value: template },
|
|
{
|
|
op: "replace" as const,
|
|
path: ["block", "children"],
|
|
value: [{ type: "id", value: template.id }],
|
|
},
|
|
]),
|
|
];
|
|
|
|
expect(
|
|
getAffectedExternalContentTemplateRootKeys({
|
|
state: {
|
|
instances: new Map([
|
|
[
|
|
"block",
|
|
{
|
|
type: "instance",
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [{ type: "id", value: template.id }],
|
|
},
|
|
],
|
|
[template.id, template],
|
|
]),
|
|
},
|
|
roots: additionRoots,
|
|
payload: additionPayload,
|
|
})
|
|
).toEqual(["scope"]);
|
|
});
|
|
});
|
|
|
|
test("identifies only instances authored by external content", () => {
|
|
const roots = new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set(["external"]),
|
|
mutationRevision: 0,
|
|
},
|
|
],
|
|
]);
|
|
|
|
expect(isExternalContentInstance(roots, "external")).toBe(true);
|
|
expect(isExternalContentInstance(roots, "block")).toBe(false);
|
|
expect(isExternalContentInstance(roots, "ordinary")).toBe(false);
|
|
});
|
|
|
|
test("routes template invalidation through the synchronized root revision", () => {
|
|
const root = {
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
mutationRevision: 0,
|
|
templateMutationRevision: 0,
|
|
};
|
|
$externalContentRoots.set(new Map([["scope", root]]));
|
|
const listener = vi.fn();
|
|
const unsubscribe = subscribeExternalContentTemplateMutations(listener);
|
|
|
|
try {
|
|
$externalContentRoots.set(
|
|
new Map([
|
|
[
|
|
"scope",
|
|
{
|
|
...root,
|
|
templateMutationRevision: 1,
|
|
},
|
|
],
|
|
])
|
|
);
|
|
} finally {
|
|
unsubscribe();
|
|
}
|
|
|
|
expect(listener).toHaveBeenCalledExactlyOnceWith(["scope"]);
|
|
});
|
|
|
|
test("publishing a template mutation advances only matching roots", () => {
|
|
$externalContentRoots.set(
|
|
new Map([
|
|
[
|
|
"current",
|
|
{
|
|
blockInstanceId: "current-block",
|
|
instanceIds: new Set<string>(),
|
|
mutationRevision: 0,
|
|
templateMutationRevision: 2,
|
|
},
|
|
],
|
|
[
|
|
"other",
|
|
{
|
|
blockInstanceId: "other-block",
|
|
instanceIds: new Set<string>(),
|
|
mutationRevision: 0,
|
|
templateMutationRevision: 4,
|
|
},
|
|
],
|
|
])
|
|
);
|
|
|
|
publishExternalContentTemplateMutation(["current", "missing"]);
|
|
|
|
expect(
|
|
$externalContentRoots.get().get("current")?.templateMutationRevision
|
|
).toBe(3);
|
|
expect(
|
|
$externalContentRoots.get().get("other")?.templateMutationRevision
|
|
).toBe(4);
|
|
});
|
|
|
|
test("unregisters the current root after synchronized revisions replace it", () => {
|
|
const unregister = registerExternalContentRoot("scope", {
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
mutationRevision: 0,
|
|
});
|
|
|
|
publishExternalContentTemplateMutation(["scope"]);
|
|
unregister();
|
|
|
|
expect($externalContentRoots.get().has("scope")).toBe(false);
|
|
});
|
|
|
|
test("does not let an older disposer unregister a replacement root", () => {
|
|
const root = {
|
|
blockInstanceId: "block",
|
|
instanceIds: new Set<string>(),
|
|
mutationRevision: 0,
|
|
};
|
|
const unregisterPrevious = registerExternalContentRoot("scope", root);
|
|
const unregisterCurrent = registerExternalContentRoot("scope", root);
|
|
|
|
unregisterPrevious();
|
|
expect($externalContentRoots.get().has("scope")).toBe(true);
|
|
|
|
unregisterCurrent();
|
|
expect($externalContentRoots.get().has("scope")).toBe(false);
|
|
});
|