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.

experimental: Add banner to Animate components (#5041)

Ivan Starkov9397f7af0c98
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 32852a385e..691dd31d59 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -179,7 +179,7 @@ export const getInstanceLabel = (
);
};
-const isTextEditingInstance = (
+const isTextEditableInstance = (
instance: Instance,
instances: Instances,
metas: Map<string, WsComponentMeta>
@@ -200,6 +200,19 @@ const isTextEditingInstance = (
if (meta.type !== "container") {
return false;
}
+
+ if (meta.constraints !== undefined) {
+ const hasTextContraint = (
+ Array.isArray(meta.constraints) ? meta.constraints : [meta.constraints]
+ ).some(
+ (constraint) =>
+ constraint.relation === "child" && constraint.text === false
+ );
+ if (hasTextContraint) {
+ return false;
+ }
+ }
+
// only container with rich-text-child children and text can be edited
for (const child of instance.children) {
if (child.type === "id") {
@@ -235,7 +248,7 @@ export const findAllEditableInstanceSelector = (
}
// Check if current instance is text editing instance
- if (isTextEditingInstance(instance, instances, metas)) {
+ if (isTextEditableInstance(instance, instances, metas)) {
results.push(currentPath);
return;
}
@@ -266,7 +279,7 @@ export const findClosestEditableInstanceSelector = (
return;
}
- if (isTextEditingInstance(instance, instances, metas)) {
+ if (isTextEditableInstance(instance, instances, metas)) {
return getAncestorInstanceSelector(instanceSelector, instanceId);
}
}
@@ -1284,7 +1297,12 @@ export const findClosestInsertable = (
instances,
instanceSelector: instanceSelector.slice(closestContainerIndex),
fragment,
- onError: (message) => toast.error(message),
+ onError: (message) =>
+ toast.error(
+ message === ""
+ ? "findClosestInstanceMatchingFragment encountered an unknown error"
+ : message
+ ),
});
if (insertableIndex === -1) {
return;
diff --git a/apps/builder/app/shared/matcher.test.tsx b/apps/builder/app/shared/matcher.test.tsx
index 544c928d48..33c6f22417 100644
--- a/apps/builder/app/shared/matcher.test.tsx
+++ b/apps/builder/app/shared/matcher.test.tsx
@@ -12,12 +12,14 @@ import type { Matcher, WsComponentMeta } from "@webstudio-is/sdk";
import {
findClosestNonTextualContainer,
findClosestInstanceMatchingFragment,
- isInstanceMatching,
isTreeMatching,
findClosestContainer,
isInstanceDetachable,
+ __testing__,
} from "./matcher";
+const { isInstanceMatching } = __testing__;
+
const metas = new Map(Object.entries({ ...coreMetas, ...baseMetas }));
metas.set("ListItem", {
...baseMetas.ListItem,
@@ -27,6 +29,33 @@ metas.set("ListItem", {
},
});
+metas.set("NoTextItem", {
+ ...baseMetas.Box,
+ constraints: {
+ relation: "child",
+ text: false,
+ },
+});
+
+// Solutions added as a quick workaround
+describe("Fast workaround tests", () => {
+ // For simplicity, we don’t check the relation=child and text=false constraints during tree validation.
+ // These are only used to prevent text editing of container components.
+ test("Child text=false constraints does not affect matching", () => {
+ expect(
+ isTreeMatching({
+ ...renderData(
+ <$.Body ws:id="body">
+ <$.NoTextItem ws:id="notextitem">AnyText</$.NoTextItem>
+ </$.Body>
+ ),
+ metas,
+ instanceSelector: ["notextitem", "body"],
+ })
+ ).toBeTruthy();
+ });
+});
+
describe("is instance matching", () => {
test("matches self with self matcher", () => {
expect(
diff --git a/apps/builder/app/shared/matcher.ts b/apps/builder/app/shared/matcher.ts
index 40a1baa179..aab271ae78 100644
--- a/apps/builder/app/shared/matcher.ts
+++ b/apps/builder/app/shared/matcher.ts
@@ -78,7 +78,7 @@ const formatList = (operation: MatcherOperation) => {
* @todo following features are missing
* - tag matcher
*/
-export const isInstanceMatching = ({
+const isInstanceMatching = ({
instances,
instanceSelector,
query,
@@ -89,10 +89,13 @@ export const isInstanceMatching = ({
query: undefined | Matcher | Matcher[];
onError?: (message: string) => void;
}): boolean => {
+ query = filterConstraints(query);
+
// fast path to the lack of constraints
if (query === undefined) {
return true;
}
+
const queries = Array.isArray(query) ? query : [query];
const matchesByMatcher = new Map<Matcher, boolean>();
let aborted = false;
@@ -167,6 +170,30 @@ export const isInstanceMatching = ({
return true;
};
+/**
+ * Added as a quick workaround!!!
+ *
+ * Some constraints don’t make much sense to check in this module, like relation=child and text=false.
+ * They’re only used to prevent text editing of container instances.
+ */
+const filterConstraints = (
+ constraints: Matcher | Matcher[] | undefined
+): undefined | Matcher[] => {
+ if (constraints === undefined) {
+ return undefined;
+ }
+
+ const result = (
+ Array.isArray(constraints) ? constraints : [constraints]
+ ).filter(
+ (constraint) => constraint.relation !== "child" || constraint.text !== false
+ );
+ if (result.length === 0) {
+ return undefined;
+ }
+ return result;
+};
+
export const isTreeMatching = ({
instances,
metas,
@@ -191,7 +218,7 @@ export const isTreeMatching = ({
let matches = isInstanceMatching({
instances,
instanceSelector,
- query: meta?.constraints,
+ query: filterConstraints(meta?.constraints),
onError,
});
// check ancestors only on the first run
@@ -206,7 +233,7 @@ export const isTreeMatching = ({
const matches = isInstanceMatching({
instances,
instanceSelector: instanceSelector.slice(index),
- query: meta?.constraints,
+ query: filterConstraints(meta?.constraints),
onError,
});
if (matches === false) {
@@ -266,7 +293,7 @@ export const isInstanceDetachable = ({
const matches = isInstanceMatching({
instances: newInstances,
instanceSelector: instanceSelector.slice(index),
- query: meta?.constraints,
+ query: filterConstraints(meta?.constraints),
});
if (matches === false) {
return false;
@@ -401,3 +428,7 @@ export const findClosestNonTextualContainer = ({
}
return -1;
};
+
+export const __testing__ = {
+ isInstanceMatching,
+};
diff --git a/packages/sdk-components-animation/private-src b/packages/sdk-components-animation/private-src
index bc17045f23..685b0a413e 160000
--- a/packages/sdk-components-animation/private-src
+++ b/packages/sdk-components-animation/private-src
@@ -1 +1 @@
-Subproject commit bc17045f232d95176d6317d655e06abb92ed6356
+Subproject commit 685b0a413e1af70828f6a57d8ba1ba4dd5b0c763
diff --git a/packages/sdk-components-animation/src/animate-children.ws.ts b/packages/sdk-components-animation/src/animate-children.ws.ts
index ce46b4356b..e5dba50d36 100644
--- a/packages/sdk-components-animation/src/animate-children.ws.ts
+++ b/packages/sdk-components-animation/src/animate-children.ws.ts
@@ -8,6 +8,10 @@ export const meta: WsComponentMeta = {
icon: SlotComponentIcon,
order: 5,
label: "Animate Children",
+ constraints: {
+ relation: "child",
+ text: false,
+ },
};
export const propsMeta: WsComponentPropsMeta = {
diff --git a/packages/sdk-components-animation/src/animate-text.ws.ts b/packages/sdk-components-animation/src/animate-text.ws.ts
index 833eb573bd..b5bd1366ea 100644
--- a/packages/sdk-components-animation/src/animate-text.ws.ts
+++ b/packages/sdk-components-animation/src/animate-text.ws.ts
@@ -9,6 +9,13 @@ export const meta: WsComponentMeta = {
icon: TextIcon,
order: 6,
label: "Animate Text",
+ constraints: [
+ { relation: "parent", component: { $eq: "AnimateChildren" } },
+ {
+ relation: "child",
+ text: false,
+ },
+ ],
};
export const propsMeta: WsComponentPropsMeta = {
diff --git a/packages/sdk/src/schema/instances.ts b/packages/sdk/src/schema/instances.ts
index 3f6f437835..80be4ea6fb 100644
--- a/packages/sdk/src/schema/instances.ts
+++ b/packages/sdk/src/schema/instances.ts
@@ -61,6 +61,15 @@ export const Matcher = z.object({
relation: MatcherRelation,
component: MatcherOperation.optional(),
tag: MatcherOperation.optional(),
+ text: z
+ .literal(false)
+ .describe(
+ `
+ To disallow text editing on a container instance,
+ use: { relation: 'child', text: false }
+ `
+ )
+ .optional(),
});
export type Matcher = z.infer<typeof Matcher>;