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: detect empty tags and components (#5132)

Bogdan Chadkinbd77a252c215
diff --git a/apps/builder/app/builder/features/ai/apply-operations.ts b/apps/builder/app/builder/features/ai/apply-operations.ts
index 0b46a30e85..51f097e0e4 100644
--- a/apps/builder/app/builder/features/ai/apply-operations.ts
+++ b/apps/builder/app/builder/features/ai/apply-operations.ts
@@ -6,6 +6,7 @@ import { serverSyncStore } from "~/shared/sync";
import { isBaseBreakpoint } from "~/shared/breakpoints";
import {
deleteInstanceMutable,
+ findClosestInsertable,
insertWebstudioFragmentAt,
updateWebstudioData,
type Insertable,
@@ -13,6 +14,7 @@ import {
import {
$breakpoints,
$instances,
+ $props,
$registeredComponentMetas,
$selectedInstanceSelector,
$styleSourceSelections,
@@ -22,6 +24,7 @@ import {
import type { InstanceSelector } from "~/shared/tree-utils";
import { $selectedInstance, getInstancePath } from "~/shared/awareness";
import { isInstanceDetachable } from "~/shared/matcher";
+import { isRichTextTree } from "~/shared/content-model";
export const applyOperations = (operations: operations.WsOperations) => {
for (const operation of operations) {
@@ -47,7 +50,7 @@ const insertTemplateByOp = (
operation: operations.generateInsertTemplateWsOperation
) => {
const metas = $registeredComponentMetas.get();
- const templateData = generateDataFromEmbedTemplate(operation.template, metas);
+ const fragment = generateDataFromEmbedTemplate(operation.template, metas);
// @todo Find a way to avoid the workaround below, peharps improving the prompt.
// Occasionally the LLM picks a component name or the entire data-ws-id attribute as the insertion point.
@@ -63,26 +66,18 @@ const insertTemplateByOp = (
}
}
- const rootInstanceIds = templateData.children
+ const rootInstanceIds = fragment.children
.filter((child) => child.type === "id")
.map((child) => child.value);
const instanceSelector = computeSelectorForInstanceId(operation.addTo);
if (instanceSelector) {
- const currentInstance = $instances.get().get(instanceSelector[0]);
- // Only container components are allowed to have child elements.
- if (
- currentInstance &&
- metas.get(currentInstance.component)?.type !== "container"
- ) {
- return;
- }
-
- const dropTarget: Insertable = {
+ let insertable: Insertable = {
parentSelector: instanceSelector,
position: operation.addAtIndex + 1,
};
- insertWebstudioFragmentAt(templateData, dropTarget);
+ insertable = findClosestInsertable(fragment, insertable) ?? insertable;
+ insertWebstudioFragmentAt(fragment, insertable);
return rootInstanceIds;
}
};
@@ -214,39 +209,47 @@ const computeSelectorForInstanceId = (instanceId: Instance["id"]) => {
};
export const patchTextInstance = (textInstance: copywriter.TextInstance) => {
- serverSyncStore.createTransaction([$instances], (instances) => {
- const currentInstance = instances.get(textInstance.instanceId);
-
- if (currentInstance === undefined) {
- return;
- }
-
- const meta = $registeredComponentMetas.get().get(currentInstance.component);
+ serverSyncStore.createTransaction(
+ [$instances, $props],
+ (instances, props) => {
+ const currentInstance = instances.get(textInstance.instanceId);
- // Only container components are allowed to have child elements.
- if (meta?.type !== "container") {
- return;
- }
+ if (currentInstance === undefined) {
+ return;
+ }
- if (currentInstance.children.length === 0) {
- currentInstance.children = [{ type: "text", value: textInstance.text }];
- return;
- }
+ const canBeEdited = isRichTextTree({
+ instanceId: textInstance.instanceId,
+ instances,
+ props,
+ metas: $registeredComponentMetas.get(),
+ });
+ if (!canBeEdited) {
+ return;
+ }
- // Instances can have a number of text child nodes without interleaving components.
- // When this is the case we treat the child nodes as a single text node,
- // otherwise the AI would generate children.length chunks of separate text.
- // We can identify this case of "joint" text instances when the index is -1.
- const replaceAll = textInstance.index === -1;
- if (replaceAll) {
- if (currentInstance.children.every((child) => child.type === "text")) {
+ if (currentInstance.children.length === 0) {
currentInstance.children = [{ type: "text", value: textInstance.text }];
+ return;
}
- return;
- }
- if (currentInstance.children[textInstance.index].type === "text") {
- currentInstance.children[textInstance.index].value = textInstance.text;
+ // Instances can have a number of text child nodes without interleaving components.
+ // When this is the case we treat the child nodes as a single text node,
+ // otherwise the AI would generate children.length chunks of separate text.
+ // We can identify this case of "joint" text instances when the index is -1.
+ const replaceAll = textInstance.index === -1;
+ if (replaceAll) {
+ if (currentInstance.children.every((child) => child.type === "text")) {
+ currentInstance.children = [
+ { type: "text", value: textInstance.text },
+ ];
+ }
+ return;
+ }
+
+ if (currentInstance.children[textInstance.index].type === "text") {
+ currentInstance.children[textInstance.index].value = textInstance.text;
+ }
}
- });
+ );
};
diff --git a/apps/builder/app/builder/features/navigator/navigator-tree.tsx b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
index 75eb0d02b3..062363b041 100644
--- a/apps/builder/app/builder/features/navigator/navigator-tree.tsx
+++ b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
@@ -66,8 +66,9 @@ import {
getInstanceKey,
selectInstance,
} from "~/shared/awareness";
-import { findClosestContainer, isTreeMatching } from "~/shared/matcher";
+import { isTreeMatching } from "~/shared/matcher";
import {
+ findClosestContainer,
isRichTextContent,
isTreeSatisfyingContentModel,
} from "~/shared/content-model";
@@ -550,19 +551,17 @@ const canDrop = (
}
}
// prevent dropping into non-container instances
- const closestContainerIndex = findClosestContainer({
+ const containerSelector = findClosestContainer({
metas,
+ props,
instances,
instanceSelector: dropSelector,
});
- if (closestContainerIndex !== 0) {
+ if (dropSelector.length !== containerSelector.length) {
return false;
}
// make sure dragging tree can be put inside of drop instance
- const containerInstanceSelector = [
- dragSelector[0],
- ...dropSelector.slice(closestContainerIndex),
- ];
+ const containerInstanceSelector = [dragSelector[0], ...dropSelector];
let matches = isTreeMatching({
instances,
metas,
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 14f003eb2d..632568851b 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
@@ -1,16 +1,22 @@
import { nanoid } from "nanoid";
+import { computed } from "nanostores";
import { useStore } from "@nanostores/react";
import type { PropMeta, Instance, Prop } from "@webstudio-is/sdk";
-import { collectionComponent, descendantComponent } from "@webstudio-is/sdk";
+import { descendantComponent } from "@webstudio-is/sdk";
import { showAttribute, textContentAttribute } from "@webstudio-is/react-sdk";
-import { showAttributeMeta, type PropValue } from "../shared";
import {
+ $instances,
$isContentMode,
+ $props,
$registeredComponentMetas,
$registeredComponentPropsMetas,
} from "~/shared/nano-states";
+import { isRichText } from "~/shared/content-model";
+import { $selectedInstancePath } from "~/shared/awareness";
+import { showAttributeMeta, type PropValue } from "../shared";
type PropOrName = { prop?: Prop; propName: string };
+
export type PropAndMeta = {
prop?: Prop;
propName: string;
@@ -132,6 +138,22 @@ const getAndDelete = <Value>(map: Map<string, Value>, key: string) => {
return value;
};
+const $canHaveTextContent = computed(
+ [$instances, $props, $registeredComponentMetas, $selectedInstancePath],
+ (instances, props, metas, instancePath) => {
+ if (instancePath === undefined) {
+ return false;
+ }
+ const [{ instanceSelector }] = instancePath;
+ return isRichText({
+ instances,
+ props,
+ metas,
+ instanceSelector,
+ });
+ }
+);
+
/** usePropsLogic expects that key={instanceId} is used on the ancestor component */
export const usePropsLogic = ({
instance,
@@ -160,9 +182,6 @@ export const usePropsLogic = ({
return propsWhiteList.includes(propName);
};
- const instanceMeta = useStore($registeredComponentMetas).get(
- instance.component
- );
const meta = useStore($registeredComponentPropsMetas).get(
instance.component
) ?? {
@@ -192,9 +211,7 @@ export const usePropsLogic = ({
});
}
- const canHaveTextContent =
- instanceMeta?.type === "container" &&
- instance.component !== collectionComponent;
+ const canHaveTextContent = useStore($canHaveTextContent);
const hasNoChildren = instance.children.length === 0;
const hasOnlyTextChild =
diff --git a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
index 88b373126a..4bf4643b3a 100644
--- a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
+++ b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
@@ -244,8 +244,8 @@ export const CursorPositioningUpDown: StoryFn<typeof TextEditor> = () => {
$registeredComponentMetas.set(
new Map([
- ["Box", { type: "container", icon: "icon" }],
- ["Bold", { type: "container", icon: "icon" }],
+ ["Box", { icon: "icon" }],
+ ["Bold", { icon: "icon" }],
])
);
diff --git a/apps/builder/app/canvas/shared/use-drag-drop.ts b/apps/builder/app/canvas/shared/use-drag-drop.ts
index 97913d0d8d..ffdb6e6fdb 100644
--- a/apps/builder/app/canvas/shared/use-drag-drop.ts
+++ b/apps/builder/app/canvas/shared/use-drag-drop.ts
@@ -30,11 +30,11 @@ import {
areInstanceSelectorsEqual,
} from "~/shared/tree-utils";
import {
- findClosestContainer,
findClosestInstanceMatchingFragment,
isTreeMatching,
} from "~/shared/matcher";
import {
+ findClosestContainer,
findClosestRichText,
isTreeSatisfyingContentModel,
} from "~/shared/content-model";
@@ -74,15 +74,13 @@ const findClosestDroppableInstanceSelector = (
const metas = $registeredComponentMetas.get();
// prevent dropping anything into non containers like image
- let droppableIndex = findClosestContainer({
+ instanceSelector = findClosestContainer({
metas,
+ props,
instances,
instanceSelector,
});
- if (droppableIndex === -1) {
- return;
- }
- instanceSelector = instanceSelector.slice(droppableIndex);
+ let droppableIndex = -1;
if (dragPayload?.type === "insert") {
const fragment = getComponentTemplateData(dragPayload.dragComponent);
if (fragment) {
diff --git a/apps/builder/app/shared/content-model.test.tsx b/apps/builder/app/shared/content-model.test.tsx
index 9283b66fe8..2c985c51d1 100644
--- a/apps/builder/app/shared/content-model.test.tsx
+++ b/apps/builder/app/shared/content-model.test.tsx
@@ -3,6 +3,7 @@ import { coreMetas } from "@webstudio-is/sdk";
import * as baseComponentMetas from "@webstudio-is/sdk-components-react/metas";
import { $, expression, renderData, ws } from "@webstudio-is/template";
import {
+ findClosestContainer,
findClosestNonTextualContainer,
findClosestRichText,
isRichTextTree,
@@ -835,8 +836,68 @@ describe("rich text tree", () => {
});
});
-describe("closest non textual container", () => {
+describe("closest container", () => {
test("skips non-container instances", () => {
+ expect(
+ findClosestContainer({
+ ...renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId">
+ <$.Image ws:id="imageId" />
+ </$.Box>
+ </$.Body>
+ ),
+ metas: defaultMetas,
+ instanceSelector: ["imageId", "boxId", "bodyId"],
+ })
+ ).toEqual(["boxId", "bodyId"]);
+ });
+
+ test("allow containers with text", () => {
+ expect(
+ findClosestContainer({
+ ...renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId">
+ <$.Box ws:id="box-with-text">text</$.Box>
+ </$.Box>
+ </$.Body>
+ ),
+ metas: defaultMetas,
+ instanceSelector: ["box-with-text", "boxId", "bodyId"],
+ })
+ ).toEqual(["box-with-text", "boxId", "bodyId"]);
+ });
+
+ test("allow containers with expression", () => {
+ expect(
+ findClosestContainer({
+ ...renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId">
+ <$.Box ws:id="box-with-expr">{expression`1 + 1`}</$.Box>
+ </$.Box>
+ </$.Body>
+ ),
+ metas: defaultMetas,
+ instanceSelector: ["box-with-expr", "boxId", "bodyId"],
+ })
+ ).toEqual(["box-with-expr", "boxId", "bodyId"]);
+ });
+
+ test("allow root with text", () => {
+ expect(
+ findClosestContainer({
+ ...renderData(<$.Body ws:id="bodyId">text</$.Body>),
+ metas: defaultMetas,
+ instanceSelector: ["bodyId"],
+ })
+ ).toEqual(["bodyId"]);
+ });
+});
+
+describe("closest non textual container", () => {
+ test("skips image tag", () => {
expect(
findClosestNonTextualContainer({
...renderData(
@@ -852,6 +913,22 @@ describe("closest non textual container", () => {
).toEqual(["boxId", "bodyId"]);
});
+ test("skips CodeText component", () => {
+ expect(
+ findClosestNonTextualContainer({
+ ...renderData(
+ <$.Body ws:id="bodyId">
+ <$.Box ws:id="boxId">
+ <$.CodeText ws:id="codeId" />
+ </$.Box>
+ </$.Body>
+ ),
+ metas: defaultMetas,
+ instanceSelector: ["codeId", "boxId", "bodyId"],
+ })
+ ).toEqual(["boxId", "bodyId"]);
+ });
+
test("skips containers with text", () => {
expect(
findClosestNonTextualContainer({
diff --git a/apps/builder/app/shared/content-model.ts b/apps/builder/app/shared/content-model.ts
index 848330b245..815ad42e22 100644
--- a/apps/builder/app/shared/content-model.ts
+++ b/apps/builder/app/shared/content-model.ts
@@ -181,6 +181,9 @@ const defaultComponentContentModel: ContentModel = {
children: ["rich-text", "instance", "transparent"],
};
+const getComponentContentModel = (meta: undefined | WsComponentMeta) =>
+ meta?.contentModel ?? defaultComponentContentModel;
+
const isComponentSatisfyingContentModel = ({
metas,
component,
@@ -190,10 +193,7 @@ const isComponentSatisfyingContentModel = ({
component: string;
allowedCategories: undefined | string[];
}) => {
- const contentModel = {
- ...defaultComponentContentModel,
- ...metas.get(component)?.contentModel,
- };
+ const contentModel = getComponentContentModel(metas.get(component));
return (
// body does not have parent
allowedCategories === undefined ||
@@ -214,8 +214,7 @@ const getComponentChildrenCategories = ({
component: string;
allowedCategories: undefined | string[];
}) => {
- const contentModel =
- metas.get(component)?.contentModel ?? defaultComponentContentModel;
+ const contentModel = getComponentContentModel(metas.get(component));
let childrenCategories = contentModel.children;
// transparent categories makes component inherit constraints from parent
if (childrenCategories.includes("transparent") && allowedCategories) {
@@ -448,8 +447,9 @@ export const isRichTextTree = ({
if (instance === undefined) {
return false;
}
- const componentContentModel =
- metas.get(instance.component)?.contentModel ?? defaultComponentContentModel;
+ const componentContentModel = getComponentContentModel(
+ metas.get(instance.component)
+ );
const isRichText = componentContentModel.children.includes("rich-text");
// only empty instance with rich text content can be edited
if (instance.children.length === 0) {
@@ -520,7 +520,29 @@ export const isRichTextContent = ({
);
};
-export const findClosestNonTextualContainer = ({
+export const isRichText = ({
+ instances,
+ props,
+ metas,
+ instanceSelector,
+}: {
+ instances: Instances;
+ props: Props;
+ metas: Metas;
+ instanceSelector: InstanceSelector;
+}) => {
+ const richTextSelector = findClosestRichText({
+ instanceSelector,
+ instances,
+ props,
+ metas,
+ });
+ return (
+ richTextSelector && richTextSelector.join() === instanceSelector.join()
+ );
+};
+
+export const findClosestContainer = ({
instances,
props,
metas,
@@ -542,11 +564,53 @@ export const findClosestNonTextualContainer = ({
if (instance === undefined) {
continue;
}
+ const tag = getTag({ instance, props, metas });
const meta = metas.get(instance.component);
- if (meta?.type !== "container") {
+ const childrenCategories = tag ? childrenCategoriesByTag[tag] : undefined;
+ const childrenComponentCategories = getComponentContentModel(meta).children;
+ if (
+ childrenComponentCategories.length === 0 ||
+ (childrenCategories && childrenCategories.length === 0)
+ ) {
+ continue;
+ }
+ return instanceSelector.slice(index);
+ }
+ return instanceSelector;
+};
+
+export const findClosestNonTextualContainer = ({
+ instances,
+ props,
+ metas,
+ instanceSelector,
+}: {
+ instances: Instances;
+ props: Props;
+ metas: Metas;
+ instanceSelector: InstanceSelector;
+}) => {
+ // page root with text can be used as container
+ if (instanceSelector.length === 1) {
+ return instanceSelector;
+ }
+ for (let index = 0; index < instanceSelector.length; index += 1) {
+ const instanceId = instanceSelector[index];
+ const instance = instances.get(instanceId);
+ // collection item can be undefined
+ if (instance === undefined) {
continue;
}
const tag = getTag({ instance, props, metas });
+ const meta = metas.get(instance.component);
+ const childrenCategories = tag ? childrenCategoriesByTag[tag] : undefined;
+ const childrenComponentCategories = getComponentContentModel(meta).children;
+ if (
+ childrenComponentCategories.length === 0 ||
+ (childrenCategories && childrenCategories.length === 0)
+ ) {
+ continue;
+ }
if (
instance.children.length === 0 &&
!meta?.placeholder &&
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 71118bdc46..0e9759f41e 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -88,14 +88,14 @@ const createFakeComponentMetas = (
Icon: () => null,
};
const configs = {
- Item: { ...base, type: "container", ...itemMeta },
- AnotherItem: { ...base, type: "container", ...anotherItemMeta },
- Bold: { ...base, type: "container" },
- Text: { ...base, type: "container" },
- Form: { ...base, type: "container" },
- Box: { ...base, type: "container" },
- Div: { ...base, type: "container" },
- Body: { ...base, type: "container" },
+ Item: { ...base, ...itemMeta },
+ AnotherItem: { ...base, ...anotherItemMeta },
+ Bold: base,
+ Text: base,
+ Form: base,
+ Box: base,
+ Div: base,
+ Body: base,
} as const;
return new Map(Object.entries(configs)) as Map<string, WsComponentMeta>;
};
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 594f92b0fc..9054bb4464 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -1218,7 +1218,8 @@ export type Insertable = {
};
export const findClosestInsertable = (
- fragment: WebstudioFragment
+ fragment: WebstudioFragment,
+ from?: Insertable
): undefined | Insertable => {
const selectedPage = $selectedPage.get();
const awareness = $awareness.get();
@@ -1226,9 +1227,8 @@ export const findClosestInsertable = (
return;
}
// paste to the page root if nothing is selected
- const instanceSelector = awareness?.instanceSelector ?? [
- selectedPage.rootInstanceId,
- ];
+ const instanceSelector = from?.parentSelector ??
+ awareness?.instanceSelector ?? [selectedPage.rootInstanceId];
if (instanceSelector[0] === ROOT_INSTANCE_ID) {
toast.error(`Cannot insert into Global Root`);
return;
@@ -1271,10 +1271,7 @@ export const findClosestInsertable = (
insertableIndex = insertableIndex + closestContainerIndex;
const parentSelector = instanceSelector.slice(insertableIndex);
if (insertableIndex === 0) {
- return {
- parentSelector,
- position: "end",
- };
+ return from ?? { parentSelector, position: "end" };
}
const instance = instances.get(instanceSelector[insertableIndex]);
if (instance === undefined) {
diff --git a/apps/builder/app/shared/matcher.test.tsx b/apps/builder/app/shared/matcher.test.tsx
index 79b5070603..718d4657e8 100644
--- a/apps/builder/app/shared/matcher.test.tsx
+++ b/apps/builder/app/shared/matcher.test.tsx
@@ -1,7 +1,6 @@
import { describe, expect, test, vi } from "vitest";
import {
$,
- expression,
renderTemplate,
renderData,
createProxy,
@@ -13,7 +12,6 @@ import type { Matcher, WsComponentMeta } from "@webstudio-is/sdk";
import {
findClosestInstanceMatchingFragment,
isTreeMatching,
- findClosestContainer,
isInstanceDetachable,
__testing__,
} from "./matcher";
@@ -946,63 +944,3 @@ describe("find closest instance matching fragment", () => {
expect(onError).toHaveBeenLastCalledWith("List is missing");
});
});
-
-describe("find closest container", () => {
- test("skips non-container instances", () => {
- expect(
- findClosestContainer({
- ...renderData(
- <$.Body ws:id="body">
- <$.Box ws:id="box">
- <$.Image ws:id="image" />
- </$.Box>
- </$.Body>
- ),
- metas,
- instanceSelector: ["image", "box", "body"],
- })
- ).toEqual(1);
- });
-
- test("allow containers with text", () => {
- expect(
- findClosestContainer({
- ...renderData(
- <$.Body ws:id="body">
- <$.Box ws:id="box">
- <$.Box ws:id="box-with-text">text</$.Box>
- </$.Box>
- </$.Body>
- ),
- metas,
- instanceSelector: ["box-with-text", "box", "body"],
- })
- ).toEqual(0);
- });
-
- test("allow containers with expression", () => {
- expect(
- findClosestContainer({
- ...renderData(
- <$.Body ws:id="body">
- <$.Box ws:id="box">
- <$.Box ws:id="box-with-expr">{expression`1 + 1`}</$.Box>
- </$.Box>
- </$.Body>
- ),
- metas,
- instanceSelector: ["box-with-expr", "box", "body"],
- })
- ).toEqual(0);
- });
-
- test("allow root with text", () => {
- expect(
- findClosestContainer({
- ...renderData(<$.Body ws:id="body">text</$.Body>),
- metas,
- instanceSelector: ["body"],
- })
- ).toEqual(0);
- });
-});
diff --git a/apps/builder/app/shared/matcher.ts b/apps/builder/app/shared/matcher.ts
index c1a01ff5c3..22319d544a 100644
--- a/apps/builder/app/shared/matcher.ts
+++ b/apps/builder/app/shared/matcher.ts
@@ -378,34 +378,6 @@ export const findClosestInstanceMatchingFragment = ({
return -1;
};
-export const findClosestContainer = ({
- metas,
- instances,
- instanceSelector,
-}: {
- metas: Map<string, WsComponentMeta>;
- instances: Instances;
- instanceSelector: InstanceSelector;
-}) => {
- // page root with text can be used as container
- if (instanceSelector.length === 1) {
- return 0;
- }
- for (let index = 0; index < instanceSelector.length; index += 1) {
- const instanceId = instanceSelector[index];
- const instance = instances.get(instanceId);
- // collection item can be undefined
- if (instance === undefined) {
- continue;
- }
- const meta = metas.get(instance.component);
- if (meta?.type === "container") {
- return index;
- }
- }
- return -1;
-};
-
export const __testing__ = {
isInstanceMatching,
};
diff --git a/packages/html-data/bin/html-crawler.ts b/packages/html-data/bin/html-crawler.ts
index 339621b0ce..9071ed56c8 100644
--- a/packages/html-data/bin/html-crawler.ts
+++ b/packages/html-data/bin/html-crawler.ts
@@ -50,7 +50,7 @@ const childrenCategoriesByTag: Record<string, string[]> = {};
const children = parseList(getTextContent(row.childNodes[4]));
for (const tag of elements) {
categoriesByTag[tag] = categories;
- childrenCategoriesByTag[tag] = children;
+ childrenCategoriesByTag[tag] = children.includes("empty") ? [] : children;
}
}
}
diff --git a/packages/html-data/src/__generated__/content-model.ts b/packages/html-data/src/__generated__/content-model.ts
index 43ec62ee5a..8124b376b2 100644
--- a/packages/html-data/src/__generated__/content-model.ts
+++ b/packages/html-data/src/__generated__/content-model.ts
@@ -178,23 +178,23 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
a: ["transparent"],
abbr: ["phrasing"],
address: ["flow"],
- area: ["empty"],
+ area: [],
article: ["flow"],
aside: ["flow"],
audio: ["source", "track", "transparent"],
b: ["phrasing"],
- base: ["empty"],
+ base: [],
bdi: ["phrasing"],
bdo: ["phrasing"],
blockquote: ["flow"],
body: ["flow"],
- br: ["empty"],
+ br: [],
button: ["phrasing"],
canvas: ["transparent"],
caption: ["flow"],
cite: ["phrasing"],
code: ["phrasing"],
- col: ["empty"],
+ col: [],
colgroup: ["col", "template"],
data: ["phrasing"],
datalist: ["phrasing", "option", "script-supporting elements"],
@@ -207,7 +207,7 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
dl: ["dt", "dd", "div", "script-supporting elements"],
dt: ["flow"],
em: ["phrasing"],
- embed: ["empty"],
+ embed: [],
fieldset: ["legend", "flow"],
figcaption: ["flow"],
figure: ["figcaption", "flow"],
@@ -222,23 +222,23 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
head: ["metadata content"],
header: ["flow"],
hgroup: ["h1", "h2", "h3", "h4", "h5", "h6", "script-supporting elements"],
- hr: ["empty"],
+ hr: [],
html: ["head", "body"],
i: ["phrasing"],
- iframe: ["empty"],
- img: ["empty"],
- input: ["empty"],
+ iframe: [],
+ img: [],
+ input: [],
ins: ["transparent"],
kbd: ["phrasing"],
label: ["phrasing"],
legend: ["phrasing", "heading content"],
li: ["flow"],
- link: ["empty"],
+ link: [],
main: ["flow"],
map: ["transparent", "area"],
mark: ["phrasing"],
menu: ["li", "script-supporting elements"],
- meta: ["empty"],
+ meta: [],
meter: ["phrasing"],
nav: ["flow"],
noscript: ["varies"],
@@ -263,7 +263,7 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
select: ["option", "optgroup", "script-supporting elements"],
slot: ["transparent"],
small: ["phrasing"],
- source: ["empty"],
+ source: [],
span: ["phrasing"],
strong: ["phrasing"],
style: ["text"],
@@ -281,7 +281,7 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
],
tbody: ["tr", "script-supporting elements"],
td: ["flow"],
- template: ["empty"],
+ template: [],
textarea: ["text"],
tfoot: ["tr", "script-supporting elements"],
th: ["flow"],
@@ -289,10 +289,10 @@ export const childrenCategoriesByTag: Record<string, string[]> = {
time: ["phrasing"],
title: ["text"],
tr: ["th", "td", "script-supporting elements"],
- track: ["empty"],
+ track: [],
u: ["phrasing"],
ul: ["li", "script-supporting elements"],
var: ["phrasing"],
video: ["source", "track", "transparent"],
- wbr: ["empty"],
+ wbr: [],
};
diff --git a/packages/sdk-components-react/src/checkbox.ws.ts b/packages/sdk-components-react/src/checkbox.ws.ts
index 805574e877..f4503ff28d 100644
--- a/packages/sdk-components-react/src/checkbox.ws.ts
+++ b/packages/sdk-components-react/src/checkbox.ws.ts
@@ -20,7 +20,6 @@ const presetStyle = {
} satisfies PresetStyle<typeof defaultTag>;
export const meta: WsComponentMeta = {
- type: "control",
icon: CheckboxCheckedIcon,
presetStyle,
states: [
diff --git a/packages/sdk-components-react/src/code-text.ws.ts b/packages/sdk-components-react/src/code-text.ws.ts
index 848348d9c1..c1ec4faafe 100644
--- a/packages/sdk-components-react/src/code-text.ws.ts
+++ b/packages/sdk-components-react/src/code-text.ws.ts
@@ -41,10 +41,13 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "general",
- type: "embed",
description:
"Use this component when you want to display code as text on the page.",
icon: BracesIcon,
+ contentModel: {
+ category: "instance",
+ children: [],
+ },
states: defaultStates,
presetStyle,
order: 5,
diff --git a/packages/sdk-components-react/src/head-link.ws.ts b/packages/sdk-components-react/src/head-link.ws.ts
index 9dbcbaa999..e4c893d3c2 100644
--- a/packages/sdk-components-react/src/head-link.ws.ts
+++ b/packages/sdk-components-react/src/head-link.ws.ts
@@ -6,12 +6,10 @@ import {
import { props } from "./__generated__/head-link.props";
export const meta: WsComponentMeta = {
- category: "hidden",
icon: ResourceIcon,
- type: "embed",
contentModel: {
category: "none",
- children: ["empty"],
+ children: [],
},
};
diff --git a/packages/sdk-components-react/src/head-meta.ws.ts b/packages/sdk-components-react/src/head-meta.ws.ts
index 06b6c1e678..b6f4203b9e 100644
--- a/packages/sdk-components-react/src/head-meta.ws.ts
+++ b/packages/sdk-components-react/src/head-meta.ws.ts
@@ -6,12 +6,10 @@ import {
import { props } from "./__generated__/head-meta.props";
export const meta: WsComponentMeta = {
- category: "hidden",
icon: WindowInfoIcon,
- type: "embed",
contentModel: {
category: "none",
- children: ["empty"],
+ children: [],
},
};
diff --git a/packages/sdk-components-react/src/head-title.ws.ts b/packages/sdk-components-react/src/head-title.ws.ts
index 7b2ffb8fb5..55bd6b3cdf 100644
--- a/packages/sdk-components-react/src/head-title.ws.ts
+++ b/packages/sdk-components-react/src/head-title.ws.ts
@@ -6,7 +6,6 @@ import {
import { props } from "./__generated__/head-title.props";
export const meta: WsComponentMeta = {
- category: "hidden",
icon: WindowTitleIcon,
type: "container",
contentModel: {
diff --git a/packages/sdk-components-react/src/image.ws.ts b/packages/sdk-components-react/src/image.ws.ts
index 458ecf965b..570c7d58d3 100644
--- a/packages/sdk-components-react/src/image.ws.ts
+++ b/packages/sdk-components-react/src/image.ws.ts
@@ -36,7 +36,6 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "media",
- type: "embed",
description:
"Add an image asset to the page. Webstudio automatically converts images to WebP or AVIF format and makes them responsive for best performance.",
icon: ImageIcon,
diff --git a/packages/sdk-components-react/src/input.ws.ts b/packages/sdk-components-react/src/input.ws.ts
index cbf24aaaa6..d8c75646db 100644
--- a/packages/sdk-components-react/src/input.ws.ts
+++ b/packages/sdk-components-react/src/input.ws.ts
@@ -21,7 +21,6 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "forms",
- type: "control",
label: "Text Input",
description:
"A single-line text input for collecting string data from your users.",
diff --git a/packages/sdk-components-react/src/option.ws.ts b/packages/sdk-components-react/src/option.ws.ts
index 3f7101c86e..c0c7f70704 100644
--- a/packages/sdk-components-react/src/option.ws.ts
+++ b/packages/sdk-components-react/src/option.ws.ts
@@ -26,7 +26,6 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "hidden",
- type: "control",
description:
"An item within a drop-down menu that users can select as their chosen value.",
icon: ItemIcon,
diff --git a/packages/sdk-components-react/src/radio-button.ws.ts b/packages/sdk-components-react/src/radio-button.ws.ts
index 486e965e00..eb7f645ef1 100644
--- a/packages/sdk-components-react/src/radio-button.ws.ts
+++ b/packages/sdk-components-react/src/radio-button.ws.ts
@@ -20,7 +20,6 @@ const presetStyle = {
} satisfies PresetStyle<typeof defaultTag>;
export const meta: WsComponentMeta = {
- type: "control",
label: "Radio",
icon: RadioCheckedIcon,
presetStyle,
diff --git a/packages/sdk-components-react/src/separator.ws.ts b/packages/sdk-components-react/src/separator.ws.ts
index 72fc3f457e..f0bffdbc4e 100644
--- a/packages/sdk-components-react/src/separator.ws.ts
+++ b/packages/sdk-components-react/src/separator.ws.ts
@@ -42,7 +42,6 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "general",
- type: "embed",
description:
"Used to visually divide sections of content, helping to improve readability and organization within a webpage.",
icon: MinusIcon,
diff --git a/packages/sdk-components-react/src/textarea.ws.ts b/packages/sdk-components-react/src/textarea.ws.ts
index a46cb9bd31..eae2230e6a 100644
--- a/packages/sdk-components-react/src/textarea.ws.ts
+++ b/packages/sdk-components-react/src/textarea.ws.ts
@@ -23,13 +23,16 @@ const presetStyle = {
export const meta: WsComponentMeta = {
category: "forms",
- type: "control",
label: "Text Area",
description:
"A multi-line text input for collecting longer string data from your users.",
icon: FormTextAreaIcon,
presetStyle,
order: 4,
+ contentModel: {
+ category: "instance",
+ children: [],
+ },
states: [
...defaultStates,
{ selector: "::placeholder", label: "Placeholder" },
diff --git a/packages/sdk-components-react/src/video.ws.ts b/packages/sdk-components-react/src/video.ws.ts
index 9ba38d025b..bbad296f7f 100644
--- a/packages/sdk-components-react/src/video.ws.ts
+++ b/packages/sdk-components-react/src/video.ws.ts
@@ -7,8 +7,11 @@ import {
import { props } from "./__generated__/video.props";
export const meta: WsComponentMeta = {
- type: "control",
icon: VideoIcon,
+ contentModel: {
+ category: "instance",
+ children: [],
+ },
presetStyle: {
video: [
{
diff --git a/packages/sdk-components-react/src/vimeo-preview-image.ws.ts b/packages/sdk-components-react/src/vimeo-preview-image.ws.ts
index a40af7c038..4674e356dd 100644
--- a/packages/sdk-components-react/src/vimeo-preview-image.ws.ts
+++ b/packages/sdk-components-react/src/vimeo-preview-image.ws.ts
@@ -8,7 +8,7 @@ export const meta: WsComponentMeta = {
label: "Preview Image",
contentModel: {
category: "none",
- children: ["empty"],
+ children: [],
},
};
diff --git a/packages/sdk/src/core-metas.ts b/packages/sdk/src/core-metas.ts
index 191f5e5d7f..b1921edab5 100644
--- a/packages/sdk/src/core-metas.ts
+++ b/packages/sdk/src/core-metas.ts
@@ -15,7 +15,6 @@ import type {
export const rootComponent = "ws:root";
const rootMeta: WsComponentMeta = {
- type: "container",
label: "Global Root",
icon: SettingsIcon,
presetStyle: {
@@ -30,7 +29,6 @@ const rootPropsMeta: WsComponentPropsMeta = {
export const elementComponent = "ws:element";
const elementMeta: WsComponentMeta = {
- type: "container",
label: "Element",
icon: HtmlElementIcon,
};
@@ -44,9 +42,12 @@ export const portalComponent = "Slot";
export const collectionComponent = "ws:collection";
const collectionMeta: WsComponentMeta = {
- type: "container",
label: "Collection",
icon: ListViewIcon,
+ contentModel: {
+ category: "instance",
+ children: ["instance"],
+ },
};
const collectionPropsMeta: WsComponentPropsMeta = {
@@ -63,12 +64,11 @@ const collectionPropsMeta: WsComponentPropsMeta = {
export const descendantComponent = "ws:descendant";
const descendantMeta: WsComponentMeta = {
- type: "control",
label: "Descendant",
icon: PaintBrushIcon,
contentModel: {
category: "none",
- children: ["empty"],
+ children: [],
},
// @todo infer possible presets
presetStyle: {},
@@ -108,7 +108,6 @@ export const blockComponent = "ws:block";
export const blockTemplateComponent = "ws:block-template";
export const blockTemplateMeta: WsComponentMeta = {
- type: "container",
icon: AddTemplateInstanceIcon,
contentModel: {
category: "none",
@@ -121,7 +120,6 @@ const blockTemplatePropsMeta: WsComponentPropsMeta = {
};
const blockMeta: WsComponentMeta = {
- type: "container",
label: "Content Block",
icon: ContentBlockIcon,
contentModel: {
diff --git a/packages/sdk/src/schema/component-meta.ts b/packages/sdk/src/schema/component-meta.ts
index 74a8506588..c5c7b8dbd6 100644
--- a/packages/sdk/src/schema/component-meta.ts
+++ b/packages/sdk/src/schema/component-meta.ts
@@ -74,24 +74,19 @@ export const ContentModel = z.object({
* transparent - pass through possible children from parent
* rich-text - can be edited as rich text
* instance - other instances accepted
- * empty - no children accepted
* ComponentName - accept specific components with none category
*/
- children: z.array(
- z.string() as z.ZodType<
- "transparent" | "instance" | "rich-text" | "empty" | (string & {})
- >
- ),
+ children: z.array(z.string()) as z.ZodType<
+ Array<"transparent" | "instance" | "rich-text" | (string & {})>
+ >,
});
export type ContentModel = z.infer<typeof ContentModel>;
export const WsComponentMeta = z.object({
category: z.enum(componentCategories).optional(),
- // container - can accept other components with dnd or be edited as text
- // control - usually form controls like inputs, without children
- // embed - images, videos or other embeddable components, without children
- type: z.enum(["container", "control", "embed"]),
+ // container - can accept other components with dnd or be edited as text ..
+ type: z.enum(["container"]).optional(),
/**
* a property used as textual placeholder when no content specified while in builder
* also signals to not insert components inside unless dropped explicitly