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
refactor: switch ai to nesting rules (#3605)
Bogdan Chadkin9a39c24502d9
diff --git a/apps/builder/app/builder/features/ai/ai-fetch-result.ts b/apps/builder/app/builder/features/ai/ai-fetch-result.tsindex 20d75c9ddf..0fd72b305b 100644--- a/apps/builder/app/builder/features/ai/ai-fetch-result.ts+++ b/apps/builder/app/builder/features/ai/ai-fetch-result.ts@@ -10,14 +10,12 @@ import {generateJsxElement,generateJsxChildren,getIndexesWithinAncestors,- getStyleRules,idAttribute,componentAttribute,type WsEmbedTemplate,} from "@webstudio-is/react-sdk";import { Instance, createScope, findTreeInstanceIds } from "@webstudio-is/sdk";import { computed } from "nanostores";-import { getMapValuesByKeysSet } from "~/shared/array-utils";import {$dataSources,$instances,@@ -307,23 +305,22 @@ const $jsx = computed(const treeInstanceIds = findTreeInstanceIds(instances, rootInstanceId);- const treeStyleSourceSelections = new Map(- getMapValuesByKeysSet(styleSourceSelections, treeInstanceIds).map(- (styleSourceSelection) => [- styleSourceSelection.instanceId,- styleSourceSelection,- ]- )- );-const sheet = createRegularStyleSheet({ name: "ssr" });-- const styleRules = getStyleRules(styles, treeStyleSourceSelections);- for (const { breakpointId, instanceId, state, style } of styleRules) {- sheet.addStyleRule(- { breakpoint: breakpointId, style },- `[${idAttribute}="${instanceId}"]${state ?? ""}`- );+ for (const styleDecl of styles.values()) {+ sheet.addMediaRule(styleDecl.breakpointId);+ const rule = sheet.addMixinRule(styleDecl.styleSourceId);+ rule.setDeclaration({+ breakpoint: styleDecl.breakpointId,+ selector: styleDecl.state ?? "",+ property: styleDecl.property,+ value: styleDecl.value,+ });+ }+ for (const { instanceId, values } of styleSourceSelections.values()) {+ if (treeInstanceIds.has(instanceId)) {+ const rule = sheet.addNestingRule(`[${idAttribute}="${instanceId}"]`);+ rule.applyMixins(values);+ }}const css = sheet.cssText.replace(/\n/gm, " ");diff --git a/apps/builder/app/shared/array-utils.test.ts b/apps/builder/app/shared/array-utils.test.tsindex b8442a88b4..4610048c93 100644--- a/apps/builder/app/shared/array-utils.test.ts+++ b/apps/builder/app/shared/array-utils.test.ts@@ -1,9 +1,5 @@import { expect, test } from "@jest/globals";-import {- getMapValuesBy,- getMapValuesByKeysSet,- removeByMutable,-} from "./array-utils";+import { removeByMutable } from "./array-utils";test("removeByMutable", () => {const array = [@@ -37,28 +33,3 @@ test("removeByMutable", () => {]`);});--test("getMapValuesByKeysSet", () => {- const map = new Map([- [1, "value1"],- [2, "value2"],- [3, "value3"],- [4, "value4"],- [5, "value5"],- ]);- const keys = new Set([2, 4]);- expect(getMapValuesByKeysSet(map, keys)).toEqual(["value2", "value4"]);-});--test("getMapValuesBy", () => {- const map = new Map([- [1, "value1"],- [2, "value2"],- [3, "value3"],- [4, "value4"],- [5, "value5"],- ]);- expect(- getMapValuesBy(map, (value) => value.includes("3") || value.includes("5"))- ).toEqual(["value3", "value5"]);-});diff --git a/apps/builder/app/shared/array-utils.ts b/apps/builder/app/shared/array-utils.tsindex e1a34f32b7..2ca3ef00f7 100644--- a/apps/builder/app/shared/array-utils.ts+++ b/apps/builder/app/shared/array-utils.ts@@ -11,30 +11,3 @@ export const removeByMutable = <Item>(}}};--export const getMapValuesByKeysSet = <Key, Value>(- map: Map<Key, Value>,- keys: Set<Key>-) => {- const values: NonNullable<Value>[] = [];- for (const key of keys) {- const value = map.get(key);- if (value != null) {- values.push(value);- }- }- return values;-};--export const getMapValuesBy = <Key, Value>(- map: Map<Key, Value>,- predicate: Predicate<Value>-) => {- const values: Value[] = [];- for (const value of map.values()) {- if (predicate(value)) {- values.push(value);- }- }- return values;-};diff --git a/packages/react-sdk/src/css/index.ts b/packages/react-sdk/src/css/index.tsindex 24b800043a..d341d3184b 100644--- a/packages/react-sdk/src/css/index.ts+++ b/packages/react-sdk/src/css/index.ts@@ -1,3 +1,2 @@export * from "./global-rules";-export * from "./style-rules";export * from "./css";diff --git a/packages/react-sdk/src/css/style-rules.test.ts b/packages/react-sdk/src/css/style-rules.test.tsdeleted file mode 100644index dfb9e4d4b1..0000000000--- a/packages/react-sdk/src/css/style-rules.test.ts+++ /dev/null@@ -1,154 +0,0 @@-import { test, expect } from "@jest/globals";-import {- type StyleDecl,- type Styles,- type StyleSourceSelections,- getStyleDeclKey,-} from "@webstudio-is/sdk";-import { getStyleRules } from "./style-rules";--const createStyleDeclPair = (styleDecl: StyleDecl) => {- return [getStyleDeclKey(styleDecl), styleDecl] as const;-};--test("compute styles from different style sources", () => {- const styles: Styles = new Map([- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource1",- property: "width",- value: { type: "unit", value: 10, unit: "px" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource2",- property: "display",- value: { type: "keyword", value: "block" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource4",- property: "color",- value: { type: "keyword", value: "green" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource4",- property: "width",- value: { type: "keyword", value: "min-content" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource3",- property: "color",- value: { type: "keyword", value: "red" },- }),- createStyleDeclPair({- breakpointId: "b",- styleSourceId: "styleSource5",- property: "color",- value: { type: "keyword", value: "orange" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource6",- property: "color",- value: { type: "keyword", value: "blue" },- }),- createStyleDeclPair({- breakpointId: "a",- styleSourceId: "styleSource6",- state: ":hover",- property: "color",- value: { type: "keyword", value: "blue" },- }),- ]);- const styleSourceSelections: StyleSourceSelections = new Map([- [- "instance1",- {- instanceId: "instance1",- values: ["styleSource1"],- },- ],- [- "instance2",- {- instanceId: "instance2",- values: ["styleSource4", "styleSource5", "styleSource3"],- },- ],- [- "instance3",- {- instanceId: "instance3",- values: ["styleSource6"],- },- ],- ]);-- expect(getStyleRules(styles, styleSourceSelections)).toMatchInlineSnapshot(`- [- {- "breakpointId": "a",- "instanceId": "instance1",- "state": undefined,- "style": {- "width": {- "type": "unit",- "unit": "px",- "value": 10,- },- },- },- {- "breakpointId": "a",- "instanceId": "instance2",- "state": undefined,- "style": {- "color": {- "type": "keyword",- "value": "red",- },- "width": {- "type": "keyword",- "value": "min-content",- },- },- },- {- "breakpointId": "b",- "instanceId": "instance2",- "state": undefined,- "style": {- "color": {- "type": "keyword",- "value": "orange",- },- },- },- {- "breakpointId": "a",- "instanceId": "instance3",- "state": undefined,- "style": {- "color": {- "type": "keyword",- "value": "blue",- },- },- },- {- "breakpointId": "a",- "instanceId": "instance3",- "state": ":hover",- "style": {- "color": {- "type": "keyword",- "value": "blue",- },- },- },- ]- `);-});diff --git a/packages/react-sdk/src/css/style-rules.ts b/packages/react-sdk/src/css/style-rules.tsdeleted file mode 100644index a5308b1feb..0000000000--- a/packages/react-sdk/src/css/style-rules.ts+++ /dev/null@@ -1,79 +0,0 @@-import type { Style } from "@webstudio-is/css-engine";-import type {- Breakpoint,- StyleDecl,- Styles,- StyleSource,- StyleSourceSelections,-} from "@webstudio-is/sdk";--type StyleRule = {- instanceId: string;- breakpointId: string;- state: undefined | string;- style: Style;-};--/**- * Merge styles from different style sources- * and group by instance and breakpoint- *- * @deprecated- */-export const getStyleRules = (- styles: Styles,- styleSourceSelections: StyleSourceSelections-) => {- if (styles === undefined || styleSourceSelections === undefined) {- return [];- }- const stylesByStyleSourceId = new Map<StyleSource["id"], StyleDecl[]>();- for (const styleDecl of styles.values()) {- const { styleSourceId } = styleDecl;- let styleSourceStyles = stylesByStyleSourceId.get(styleSourceId);- // instance can be undefined when style is from other tree- if (styleSourceStyles === undefined) {- styleSourceStyles = [];- stylesByStyleSourceId.set(styleSourceId, styleSourceStyles);- }- styleSourceStyles.push(styleDecl);- }-- const styleRules: StyleRule[] = [];- for (const { instanceId, values } of styleSourceSelections.values()) {- const styleRuleByBreakpointId = new Map<- `${Breakpoint["id"]}:${string}`,- StyleRule- >();-- for (const styleSourceId of values) {- const styleSourceStyles = stylesByStyleSourceId.get(styleSourceId);- // instance can be undefined when style is from other tree- if (styleSourceStyles === undefined) {- continue;- }- for (const {- breakpointId,- state,- property,- value,- } of styleSourceStyles) {- const key = `${breakpointId}:${state ?? ""}` as const;- let styleRule = styleRuleByBreakpointId.get(key);- if (styleRule === undefined) {- styleRule = {- instanceId,- breakpointId,- state,- style: {},- };- styleRuleByBreakpointId.set(key, styleRule);- }- styleRule.style[property] = value;- }- }- styleRules.push(...styleRuleByBreakpointId.values());- }-- return styleRules;-};