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
experimental: setup style object model (#3042)
Bogdan Chadkin0fab9af9a54e
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.tsindex 1750f997ab..bb6392216a 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts@@ -65,6 +65,8 @@ type SourceProperties = {* For some cases we are encouraging to use custom defaults, than* `initial` values provided by browsers. This helps in controlling the behaviour* of such properties+ *+ * @todo override in generated properties*/const CUSTOM_DEFAULT_VALUES: Partial<Record<StyleProperty, StyleValue>> = {outlineWidth: { value: 0, type: "unit", unit: "px" },diff --git a/apps/builder/app/shared/style-object-model.test.ts b/apps/builder/app/shared/style-object-model.test.tsnew file mode 100644index 0000000000..4ec669a81a--- /dev/null+++ b/apps/builder/app/shared/style-object-model.test.ts@@ -0,0 +1,213 @@+import { expect, test } from "@jest/globals";+import type { StyleDecl, StyleSource } from "@webstudio-is/sdk";+import {+ type StyleObjectModel,+ type StyleSelector,+ getComputedStyleDecl,+} from "./style-object-model";++const getStyleByStyleSourceId = (styles: StyleDecl[]) => {+ const styleByStyleSourceId = new Map<+ StyleSource["id"],+ Map<StyleDecl["property"], StyleDecl>+ >();+ for (const styleDecl of styles) {+ const styleSourceStyles =+ styleByStyleSourceId.get(styleDecl.styleSourceId) ?? new Map();+ styleByStyleSourceId.set(styleDecl.styleSourceId, styleSourceStyles);+ styleSourceStyles.set(styleDecl.property, styleDecl);+ }+ return styleByStyleSourceId;+};++test("use cascaded style when specified and fallback to initial value", () => {+ const styles: StyleDecl[] = [+ {+ breakpointId: "base",+ styleSourceId: "bodyLocal",+ property: "width",+ value: { type: "unit", value: 10, unit: "px" },+ },+ ];+ const model: StyleObjectModel = {+ styleSourcesByInstanceId: new Map([["body", ["bodyLocal"]]]),+ styleByStyleSourceId: getStyleByStyleSourceId(styles),+ };+ const styleSelector: StyleSelector = {+ instanceSelector: ["body"],+ };+ // cascaded property+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "width" })+ .computedValue+ ).toEqual({ type: "unit", unit: "px", value: 10 });+ // initial for not inherited property+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "height" })+ .computedValue+ ).toEqual({ type: "keyword", value: "auto" });+ // initial for inherited property+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "color" })+ .computedValue+ ).toEqual({ type: "keyword", value: "black" });+});++test("support initial keyword", () => {+ const styles: StyleDecl[] = [+ {+ breakpointId: "base",+ styleSourceId: "bodyLocal",+ property: "width",+ value: { type: "keyword", value: "initial" },+ },+ ];+ const model: StyleObjectModel = {+ styleSourcesByInstanceId: new Map([["body", ["bodyLocal"]]]),+ styleByStyleSourceId: getStyleByStyleSourceId(styles),+ };+ const styleSelector: StyleSelector = {+ instanceSelector: ["body"],+ };+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "width" })+ .computedValue+ ).toEqual({ type: "keyword", value: "auto" });+});++test("support inherit keyword", () => {+ const styles: StyleDecl[] = [+ {+ breakpointId: "base",+ styleSourceId: "level2Local",+ property: "width",+ value: { type: "unit", value: 10, unit: "px" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level3Local",+ property: "width",+ value: { type: "keyword", value: "inherit" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level1Local",+ property: "height",+ value: { type: "unit", value: 20, unit: "px" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level3Local",+ property: "height",+ value: { type: "keyword", value: "inherit" },+ },+ ];+ const model: StyleObjectModel = {+ styleSourcesByInstanceId: new Map([+ ["level3", ["level3Local"]],+ ["level2", ["level2Local"]],+ ["level1", ["level1Local"]],+ ]),+ styleByStyleSourceId: getStyleByStyleSourceId(styles),+ };+ const styleSelector: StyleSelector = {+ instanceSelector: ["level3", "level2", "level1"],+ };+ // should inherit declared value+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "width" })+ .computedValue+ ).toEqual({ type: "unit", unit: "px", value: 10 });+ // should inherit initial value as height is not inherited+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "height" })+ .computedValue+ ).toEqual({ type: "keyword", value: "auto" });+});++test("support unset keyword", () => {+ const styles: StyleDecl[] = [+ {+ breakpointId: "base",+ styleSourceId: "level2Local",+ property: "width",+ value: { type: "unit", value: 10, unit: "px" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level2Local",+ property: "width",+ value: { type: "keyword", value: "unset" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level1Local",+ property: "color",+ value: { type: "keyword", value: "blue" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level2Local",+ property: "color",+ value: { type: "keyword", value: "unset" },+ },+ ];+ const model: StyleObjectModel = {+ styleSourcesByInstanceId: new Map([+ ["level2", ["level2Local"]],+ ["level1", ["level1Local"]],+ ]),+ styleByStyleSourceId: getStyleByStyleSourceId(styles),+ };+ const styleSelector: StyleSelector = {+ instanceSelector: ["level2", "level1"],+ };+ // when property is not inherited use initial value+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "width" })+ .computedValue+ ).toEqual({ type: "keyword", value: "auto" });+ // when property is inherited use inherited value+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "color" })+ .computedValue+ ).toEqual({ type: "keyword", value: "blue" });+});++test("inherit style from ancestors", () => {+ const styles: StyleDecl[] = [+ {+ breakpointId: "base",+ styleSourceId: "level1Local",+ property: "color",+ value: { type: "keyword", value: "blue" },+ },+ {+ breakpointId: "base",+ styleSourceId: "level1Local",+ property: "width",+ value: { type: "unit", value: 10, unit: "px" },+ },+ ];+ const model: StyleObjectModel = {+ styleSourcesByInstanceId: new Map([+ ["level3", ["level3Local"]],+ ["level2", ["level2Local"]],+ ["level1", ["level1Local"]],+ ]),+ styleByStyleSourceId: getStyleByStyleSourceId(styles),+ };+ const styleSelector: StyleSelector = {+ instanceSelector: ["level3", "level2", "level1"],+ };+ // inherited value+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "color" })+ .computedValue+ ).toEqual({ type: "keyword", value: "blue" });+ // not inherited value+ expect(+ getComputedStyleDecl({ model, styleSelector, property: "width" })+ .computedValue+ ).toEqual({ type: "keyword", value: "auto" });+});diff --git a/apps/builder/app/shared/style-object-model.ts b/apps/builder/app/shared/style-object-model.tsnew file mode 100644index 0000000000..8cc4429c71--- /dev/null+++ b/apps/builder/app/shared/style-object-model.ts@@ -0,0 +1,155 @@+import { properties } from "@webstudio-is/css-data";+import { StyleValue } from "@webstudio-is/css-engine";+import type { Instance, StyleDecl, StyleSource } from "@webstudio-is/sdk";++/**+ *+ * use cases where computing is called+ *+ * 1. compiler+ *+ * put the whole tree into the model once+ * lazily compute each point in the tree reusing cached cascaded values+ *+ * 2. builder style panel+ *+ * property is updated within some point in the tree+ * invalidate only changed properties in model+ * lazily recompute changed properties+ *+ * custom property or color is updated within some point in the tree+ * invalidate dependent properties in model+ * lazily recompute changed and dependent properties+ *+ * any selection is changed+ * invalidate model+ * lazily recompute all properties+ *+ * 3. CSS Preview+ *+ * all properties are collected from instance selector+ * property are iterated with own model and computed+ *+ */++type InstanceSelector = string[];++/**+ * style selector is a full address in a tree+ * and style data to extract computed style+ */+export type StyleSelector = {+ instanceSelector: InstanceSelector;+};++type Style = Map<StyleDecl["property"], StyleDecl>;++/**+ * model contains all data and cache of computed styles+ * and manages reactive subscriptions+ */+export type StyleObjectModel = {+ styleSourcesByInstanceId: Map<Instance["id"], StyleSource["id"][]>;+ styleByStyleSourceId: Map<StyleSource["id"], Style>;+};++type Property = keyof typeof properties;++const getCascadedValue = ({+ model,+ instanceId,+ property,+}: {+ model: StyleObjectModel;+ instanceId: Instance["id"];+ property: Property;+}) => {+ // https://drafts.csswg.org/css-cascade-5/#declared+ const declaredValues = [];+ const styleSourceIds = model.styleSourcesByInstanceId.get(instanceId);+ if (styleSourceIds) {+ for (const styleSourceId of styleSourceIds) {+ const style = model.styleByStyleSourceId.get(styleSourceId);+ const styleDecl = style?.get(property);+ if (styleDecl) {+ declaredValues.push(styleDecl.value);+ }+ }+ }++ // https://drafts.csswg.org/css-cascade-5/#cascaded+ const cascadedValue = declaredValues.at(-1);+ return { cascadedValue };+};++const matchKeyword = (styleValue: undefined | StyleValue, keyword: string) =>+ styleValue?.type === "keyword" && styleValue.value === keyword;++/**+ * follow value processing specification+ * https://drafts.csswg.org/css-cascade-5/#value-stages+ *+ * @todo+ * - current color+ * - custom property+ * - html+ * - preset+ * - cascaded+ * - selected style source+ * - selected state+ * - breakpoints+ *+ */+export const getComputedStyleDecl = ({+ model,+ styleSelector,+ property,+}: {+ model: StyleObjectModel;+ styleSelector: StyleSelector;+ property: Property;+}) => {+ const { instanceSelector } = styleSelector;+ const propertyData = properties[property];+ const inherited = propertyData.inherited;+ const initialValue = propertyData.initial;+ let computedValue: StyleValue = initialValue;++ // start computing from the root+ for (const instanceId of Array.from(instanceSelector).reverse()) {+ // https://drafts.csswg.org/css-cascade-5/#inheriting+ const inheritedValue: StyleValue = computedValue;++ // https://drafts.csswg.org/css-cascade-5/#cascaded+ const { cascadedValue } = getCascadedValue({ model, instanceId, property });++ // https://drafts.csswg.org/css-cascade-5/#specified+ let specifiedValue: StyleValue;+ // explicit defaulting+ // https://drafts.csswg.org/css-cascade-5/#defaulting-keywords+ if (matchKeyword(cascadedValue, "initial")) {+ specifiedValue = initialValue;+ } else if (matchKeyword(cascadedValue, "inherit")) {+ specifiedValue = inheritedValue;+ } else if (matchKeyword(cascadedValue, "unset")) {+ if (inherited) {+ specifiedValue = inheritedValue;+ } else {+ specifiedValue = initialValue;+ }+ } else if (cascadedValue) {+ specifiedValue = cascadedValue;+ }+ // defaulting https://drafts.csswg.org/css-cascade-5/#defaulting+ else if (inherited) {+ specifiedValue = inheritedValue;+ } else {+ specifiedValue = initialValue;+ }++ // https://drafts.csswg.org/css-cascade-5/#computed+ computedValue = specifiedValue;+ }++ return { computedValue };+};