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: migrate image control to style object model (#4102)
Bogdan Chadkin534f4b2c56c7
diff --git a/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx b/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsxindex 89e6329bb1..3c3052bc84 100644--- a/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx@@ -3,9 +3,13 @@ import { Button, InputField, Flex } from "@webstudio-is/design-system";import { $assets } from "~/shared/nano-states";import { FloatingPanel } from "~/builder/shared/floating-panel";import { ImageManager } from "~/builder/shared/image-manager";-import type { ControlProps } from "../types";import { useEffect, useState } from "react";-import type { InvalidValue } from "@webstudio-is/css-engine";+import type { InvalidValue, StyleProperty } from "@webstudio-is/css-engine";+import { useComputedStyleDecl } from "../../shared/model";+import {+ getRepeatedStyleItem,+ setRepeatedStyleItem,+} from "../../shared/repeated-style";const isValidURL = (value: string) => {try {@@ -22,12 +26,14 @@ type IntermediateValue = {export const ImageControl = ({property,- currentStyle,- setProperty,-}: ControlProps) => {+ index,+}: {+ property: StyleProperty;+ index: number;+}) => {const assets = useStore($assets);- const setValue = setProperty(property);- const styleValue = currentStyle[property]?.value;+ const styleDecl = useComputedStyleDecl(property);+ const styleValue = getRepeatedStyleItem(styleDecl.cascadedValue, index);const [remoteImageURL, setRemoteImageURL] = useState<IntermediateValue | InvalidValue | undefined>(undefined);@@ -65,7 +71,7 @@ export const ImageControl = ({remoteImageURL?.type === "intermediate" &&isValidURL(remoteImageURL.value) === true) {- setValue({+ setRepeatedStyleItem(styleDecl, index, {type: "image",value: { type: "url", url: remoteImageURL.value },});@@ -93,7 +99,7 @@ export const ImageControl = ({content={<ImageManageronChange={(assetId) => {- setValue({+ setRepeatedStyleItem(styleDecl, index, {type: "image",value: { type: "asset", value: assetId },});diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.stories.tsxindex 4189105dae..ba32e56c6f 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.stories.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.stories.tsx@@ -6,13 +6,38 @@ import {} from "~/builder/shared/floating-panel";import { useRef, useState } from "react";import type { SetProperty } from "../../shared/use-style-data";+import {+ $breakpoints,+ $selectedBreakpointId,+ $selectedInstanceSelector,+ $styles,+ $styleSourceSelections,+} from "~/shared/nano-states";+import { registerContainers } from "~/shared/sync";+import { getStyleDeclKey, type StyleDecl } from "@webstudio-is/sdk";++const backgroundImage: StyleDecl = {+ breakpointId: "base",+ styleSourceId: "local",+ property: "backgroundImage",+ value: {+ type: "layers",+ value: [{ type: "keyword", value: "none" }],+ },+};++registerContainers();+$breakpoints.set(new Map([["base", { id: "base", label: "" }]]));+$selectedBreakpointId.set("base");+$styles.set(new Map([[getStyleDeclKey(backgroundImage), backgroundImage]]));+$styleSourceSelections.set(+ new Map([["box", { instanceId: "box", values: ["local"] }]])+);+$selectedInstanceSelector.set(["box"]);const defaultCurrentStyle = getLayerBackgroundStyleInfo(0, {backgroundImage: {- value: {- type: "layers",- value: [{ type: "keyword", value: "none" }],- },+ value: backgroundImage.value,},});const deleteProperty = () => () => {@@ -49,6 +74,7 @@ export const BackgroundContentStory = () => {title="Background"content={<BackgroundContent+ index={0}currentStyle={currentStyle}deleteProperty={deleteProperty}setProperty={setProperty}diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsxindex a1f19b6562..43f55a65e5 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx@@ -44,6 +44,7 @@ import { PropertyInlineLabel } from "../../property-label";import { ToggleGroupTooltip } from "../../controls/toggle-group/toggle-group-control";type BackgroundContentProps = {+ index: number;currentStyle: StyleInfo;setProperty: SetBackgroundProperty;deleteProperty: DeleteBackgroundProperty;@@ -198,7 +199,7 @@ const BackgroundRepeat = (props: BackgroundContentProps) => {export const BackgroundContent = (props: BackgroundContentProps) => {const setProperty = safeSetProperty(props.setProperty);const deleteProperty = safeDeleteProperty(props.deleteProperty);- const { currentStyle } = props;+ const { currentStyle, index } = props;const elementRef = useRef<HTMLDivElement>(null);const [imageGradientToggle, setImageGradientToggle] = useState<@@ -252,12 +253,7 @@ export const BackgroundContent = (props: BackgroundContentProps) => {</Flex><FloatingPanelProvider container={elementRef}>- <ImageControl- setProperty={setProperty}- deleteProperty={deleteProperty}- currentStyle={currentStyle}- property="backgroundImage"- />+ <ImageControl property="backgroundImage" index={index} /></FloatingPanelProvider></>)}diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsxindex 1e5a60af76..d6ae5e8ca6 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx@@ -86,6 +86,7 @@ const Layer = (props: {collisionPadding={{ bottom: 200, top: 200 }}content={<BackgroundContent+ index={props.index}currentStyle={props.layerStyle}setProperty={props.setProperty}deleteProperty={props.deleteProperty}diff --git a/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts b/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.tsindex 1f5ea58aee..09cb3d603d 100644--- a/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts@@ -1,8 +1,11 @@import { beforeEach, expect, test } from "@jest/globals";+import type { StyleValue } from "@webstudio-is/css-engine";import {addRepeatedStyleItem,deleteRepeatedStyleItem,editRepeatedStyleItem,+ getRepeatedStyleItem,+ setRepeatedStyleItem,swapRepeatedStyleItems,toggleRepeatedStyleItem,} from "./repeated-style";@@ -25,6 +28,42 @@ beforeEach(() => {$styles.set(new Map());});+test("get repeated style item by index", () => {+ const styleValue: StyleValue = {+ type: "layers",+ value: [+ { type: "keyword", value: "red" },+ { type: "keyword", value: "green" },+ { type: "keyword", value: "blue" },+ ],+ };+ expect(getRepeatedStyleItem(styleValue, 0)).toEqual({+ type: "keyword",+ value: "red",+ });+ expect(getRepeatedStyleItem(styleValue, 1)).toEqual({+ type: "keyword",+ value: "green",+ });+ expect(getRepeatedStyleItem(styleValue, 2)).toEqual({+ type: "keyword",+ value: "blue",+ });+ // repeat values+ expect(getRepeatedStyleItem(styleValue, 3)).toEqual({+ type: "keyword",+ value: "red",+ });+ expect(getRepeatedStyleItem(styleValue, 4)).toEqual({+ type: "keyword",+ value: "green",+ });+ expect(getRepeatedStyleItem(styleValue, 5)).toEqual({+ type: "keyword",+ value: "blue",+ });+});+test("add layer to repeated style", () => {const $transitionProperty =createComputedStyleDeclStore("transitionProperty");@@ -141,6 +180,44 @@ test("edit tuple in repeated style", () => {});});+test("set layers item into repeated style", () => {+ const $transitionProperty =+ createComputedStyleDeclStore("transitionProperty");+ addRepeatedStyleItem(+ [$transitionProperty.get()],+ parseCssFragment("opacity", "transitionProperty")+ );+ addRepeatedStyleItem(+ [$transitionProperty.get()],+ parseCssFragment("transform", "transitionProperty")+ );+ setRepeatedStyleItem($transitionProperty.get(), 0, {+ type: "unparsed",+ value: "width",+ });+ expect($transitionProperty.get().cascadedValue).toEqual({+ type: "layers",+ value: [+ { type: "unparsed", value: "width" },+ { type: "unparsed", value: "transform" },+ ],+ });+ // out of bounds will repeat existing values+ setRepeatedStyleItem($transitionProperty.get(), 3, {+ type: "unparsed",+ value: "left",+ });+ expect($transitionProperty.get().cascadedValue).toEqual({+ type: "layers",+ value: [+ { type: "unparsed", value: "width" },+ { type: "unparsed", value: "transform" },+ { type: "unparsed", value: "width" },+ { type: "unparsed", value: "left" },+ ],+ });+});+test("delete layer from repeated style", () => {const $transitionProperty =createComputedStyleDeclStore("transitionProperty");diff --git a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsxindex 67e5b5fd4e..606f7442d2 100644--- a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx@@ -27,6 +27,12 @@ import type { ComputedStyleDecl } from "~/shared/style-object-model";import { createBatchUpdate, type StyleUpdateOptions } from "./use-style-data";import { ColorThumb } from "./color-thumb";+export const getRepeatedStyleItem = (styleValue: StyleValue, index: number) => {+ if (styleValue.type === "layers" || styleValue.type === "tuple") {+ return styleValue.value[index % styleValue.value.length];+ }+};+type ItemType = "layers" | "tuple";const normalizeStyleValue = (@@ -59,9 +65,13 @@ export const addRepeatedStyleItem = (for (const [property, value] of newItems) {const oldValue = currentStyles.get(property);const newValue = normalizeStyleValue(oldValue, primaryValue, itemType);- const newItems =- value.type === newValue.type ? (value.value as UnparsedValue[]) : [];- newValue.value.push(...newItems);+ if (value.type !== itemType) {+ console.error(+ `Unexpected item type "${value.type}" for ${property} repeated value`+ );+ continue;+ }+ newValue.value.push(...(value.value as UnparsedValue[]));batch.setProperty(property)(newValue);}batch.publish();@@ -81,13 +91,36 @@ export const editRepeatedStyleItem = (for (const [property, value] of newItems) {const oldValue = currentStyles.get(property);const newValue = normalizeStyleValue(oldValue, primaryValue);- const newItems = value.type === newValue.type ? value.value : [];- newValue.value.splice(index, 1, ...newItems);+ if (value.type !== newValue.type) {+ console.error(+ `Unexpected item type "${value.type}" for ${property} repeated value`+ );+ continue;+ }+ newValue.value.splice(index, 1, ...value.value);batch.setProperty(property)(newValue);}batch.publish(options);};+export const setRepeatedStyleItem = (+ styleDecl: ComputedStyleDecl,+ index: number,+ newItem: StyleValue+) => {+ const batch = createBatchUpdate();+ const value = styleDecl.cascadedValue;+ const valueType: ItemType = value.type === "tuple" ? "tuple" : "layers";+ const oldItems = value.type === valueType ? value.value : [];+ const newItems: StyleValue[] = repeatUntil(oldItems, index);+ newItems[index] = newItem;+ batch.setProperty(styleDecl.property as StyleProperty)({+ type: valueType,+ value: newItems as UnparsedValue[],+ });+ batch.publish();+};+export const deleteRepeatedStyleItem = (styles: ComputedStyleDecl[],index: numberdiff --git a/apps/builder/app/shared/array-utils.test.ts b/apps/builder/app/shared/array-utils.test.tsindex 63862a4578..631e1d2423 100644--- a/apps/builder/app/shared/array-utils.test.ts+++ b/apps/builder/app/shared/array-utils.test.ts@@ -36,4 +36,5 @@ test("removeByMutable", () => {test("repeatUntil", () => {expect(repeatUntil([1, 2, 3], 5)).toEqual([1, 2, 3, 1, 2]);+ expect(repeatUntil([1, 2, 3], 1)).toEqual([1, 2, 3]);});diff --git a/apps/builder/app/shared/array-utils.ts b/apps/builder/app/shared/array-utils.tsindex 303d19d0ee..5b0dae0a4d 100644--- a/apps/builder/app/shared/array-utils.ts+++ b/apps/builder/app/shared/array-utils.ts@@ -14,7 +14,7 @@ export const removeByMutable = <Item>(export const repeatUntil = <Item>(array: Item[], count: number) => {const repeatedArray: Item[] = [];- for (let index = 0; index < count; index += 1) {+ for (let index = 0; index < Math.max(count, array.length); index += 1) {repeatedArray.push(array[index % array.length]);}return repeatedArray;