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: replace keywords with change-case (#5011)
Bogdan Chadkine3eaf5b4b1ae
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsxindex d505fb12d8..8c98b4d8d2 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx@@ -1,3 +1,4 @@+import { useState } from "react";import {Box,Flex,@@ -8,7 +9,6 @@ import {toast,} from "@webstudio-is/design-system";import { keywordValues } from "@webstudio-is/css-data";-import { toPascalCase } from "~/builder/features/style-panel/shared/keyword-utils";import { useIds } from "~/shared/form-utils";import type {@@ -31,9 +31,8 @@ import {toValue,type StyleValue,} from "@webstudio-is/css-engine";-import { useState } from "react";import { Keyframes } from "./animation-keyframes";-import { titleCase } from "title-case";+import { humanizeString } from "~/shared/string-utils";type Props = {type: "scroll" | "view";@@ -274,7 +273,7 @@ export const AnimationPanelContent = ({ onChange, value, type }: Props) => {<Selectid={fieldIds.fill}options={fillModeNames}- getLabel={(fillModeName: string) => titleCase(fillModeName)}+ getLabel={humanizeString}value={value.timing.fill ?? fillModeNames[0]}onItemHighlight={(fillModeName) => {if (fillModeName === undefined) {@@ -356,9 +355,7 @@ export const AnimationPanelContent = ({ onChange, value, type }: Props) => {<Selectid={fieldIds.rangeStartName}options={timelineRangeNames}- getLabel={(timelineRangeName: string) =>- toPascalCase(timelineRangeName)- }+ getLabel={humanizeString}value={value.timing.rangeStart?.[0] ?? timelineRangeNames[0]!}getDescription={(timelineRangeName: string) => (<Box@@ -455,9 +452,7 @@ export const AnimationPanelContent = ({ onChange, value, type }: Props) => {<Selectid={fieldIds.rangeEndName}options={timelineRangeNames}- getLabel={(timelineRangeName: string) =>- toPascalCase(timelineRangeName)- }+ getLabel={humanizeString}value={value.timing.rangeEnd?.[0] ?? timelineRangeNames[0]!}getDescription={(timelineRangeName: string) => (<Boxdiff --git a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-section.tsxindex 71b33c0ab9..046ed3594c 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-section.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-section.tsx@@ -1,3 +1,4 @@+import { useState } from "react";import {Grid,theme,@@ -20,7 +21,6 @@ import type {AnimationActionScroll,InsetUnitValue,} from "@webstudio-is/sdk";-import { toPascalCase } from "~/builder/features/style-panel/shared/keyword-utils";import {animationActionSchema,insetUnitValueSchema,@@ -34,7 +34,7 @@ import {CssValueInput,type IntermediateStyleValue,} from "~/builder/features/style-panel/shared/css-value-input";-import { useState } from "react";+import { humanizeString } from "~/shared/string-utils";const animationTypeDescription: Record<AnimationAction["type"], string> = {scroll:@@ -292,9 +292,7 @@ export const AnimateSection = ({<Selectid={fieldIds.type}options={animationTypes}- getLabel={(animationType: AnimationAction["type"]) =>- toPascalCase(animationType)- }+ getLabel={humanizeString}value={value.type}getDescription={(animationType: AnimationAction["type"]) => (<Box@@ -352,9 +350,7 @@ export const AnimateSection = ({<Selectid={fieldIds.source}options={animationSources}- getLabel={(- animationSource: NonNullable<AnimationActionScroll["source"]>- ) => toPascalCase(animationSource)}+ getLabel={humanizeString}value={value.source ?? "nearest"}getDescription={(animationSource: NonNullable<AnimationActionScroll["source"]>diff --git a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsxindex 53b2a62bc7..a6200e27e0 100644--- a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx@@ -1,4 +1,3 @@-import { noCase } from "change-case";import {camelCaseProperty,declarationDescriptions,@@ -11,7 +10,6 @@ import {type CssProperty,} from "@webstudio-is/css-engine";import { Box, Select, theme } from "@webstudio-is/design-system";-import { toKebabCase } from "../../shared/keyword-utils";import { useComputedStyleDecl } from "../../shared/model";import {resetEphemeralStyles,@@ -70,7 +68,6 @@ export const SelectControl = ({// Show empty field instead of radix placeholder like css value input does.placeholder=""options={options}- getLabel={toKebabCase}value={valueString}onChange={(name) => setValue({ type: "keyword", value: name })}onItemHighlight={(name) => {@@ -100,7 +97,7 @@ export const SelectControl = ({declarationDescriptions[`${camelCaseProperty(property)}:${option}`];return (<Box css={{ width: theme.spacing[26] }}>- {description ?? `The ${noCase(property)} is ${option}`}+ {description ?? `The ${property} is ${option}`}</Box>);}}diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsxindex f62a75ead2..2d09ba76d0 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx@@ -9,7 +9,7 @@ import {} from "@webstudio-is/css-engine";import { $assets } from "~/shared/nano-states";import brokenImage from "~/shared/images/broken-image-placeholder.svg";-import { toPascalCase } from "../../shared/keyword-utils";+import { humanizeString } from "~/shared/string-utils";import { useComputedStyles } from "../../shared/model";import { getComputedRepeatedItem } from "../../shared/repeated-style";@@ -105,7 +105,7 @@ export const getBackgroundLabel = (backgroundImageStyle.value.includes(name));- return gradientName ? toPascalCase(gradientName) : "Gradient";+ return gradientName ? humanizeString(gradientName) : "Gradient";}return "None";diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsxindex 7962944c33..bacc3bf149 100644--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx@@ -78,7 +78,7 @@ export const Section = () => {onAdd={() => {addRepeatedStyleItem([styleDecl],- parseCssFragment(initialBoxShadow, ["boxShadow"])+ parseCssFragment(initialBoxShadow, ["box-shadow"]));}}>diff --git a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsxindex 01c2e71418..6f3d09c099 100644--- a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx@@ -65,7 +65,7 @@ export const Section = () => {onAdd={() => {addRepeatedStyleItem([styleDecl],- parseCssFragment(initialTextShadow, ["textShadow"])+ parseCssFragment(initialTextShadow, ["text-shadow"]));}}>diff --git a/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx b/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsxindex ecf04cf9d6..b5b937efd2 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-fragment.tsx@@ -7,7 +7,7 @@ import {completionKeymap,type CompletionSource,} from "@codemirror/autocomplete";-import { parseCss } from "@webstudio-is/css-data";+import { parseCss, shorthandProperties } from "@webstudio-is/css-data";import { css as style } from "@webstudio-is/design-system";import type { CssProperty, StyleValue } from "@webstudio-is/css-engine";import {@@ -19,9 +19,11 @@ import {} from "~/builder/shared/code-editor-base";import { $availableVariables } from "./model";+type ShorthandProperty = (typeof shorthandProperties)[number];+export const parseCssFragment = (css: string,- fallbacks: string[]+ fallbacks: (CssProperty | ShorthandProperty)[]): Map<CssProperty, StyleValue> => {let parsed = parseCss(`.styles{${css}}`);if (parsed.length === 0) {diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/keyword-utils.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/keyword-utils.test.tsdeleted file mode 100644index c229e74773..0000000000--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/keyword-utils.test.ts+++ /dev/null@@ -1,34 +0,0 @@-import { describe, test, expect } from "vitest";-import { toKebabCase } from "../keyword-utils";--describe("toKebabCase", () => {- test("PascalCase", () => {- expect(toKebabCase("PascalCase")).toEqual("pascal-case");- });-- test("Pascal Case", () => {- expect(toKebabCase("Pascal Case")).toEqual("pascal-case");- expect(toKebabCase("Pascal Case")).toEqual("pascal-case");- });-- test("Pascal Case", () => {- expect(toKebabCase("Pascal Case")).toEqual("pascal-case");- expect(toKebabCase("Pascal Case")).toEqual("pascal-case");- });-- test("camelCase", () => {- expect(toKebabCase("camelCase")).toEqual("camel-case");- });-- test("camel Case", () => {- expect(toKebabCase("camel Case")).toEqual("camel-case");- });-- test("kebab-case", () => {- expect(toKebabCase("kebab-case")).toEqual("kebab-case");- });-- test("not touches numerics", () => {- expect(toKebabCase("02020px")).toEqual("02020px");- });-});diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.tsindex 2130977825..0bd3924ea1 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts@@ -1,3 +1,4 @@+import { kebabCase } from "change-case";import type {StyleValue,InvalidValue,@@ -12,7 +13,6 @@ import {} from "@webstudio-is/css-data";import type { IntermediateStyleValue } from "./css-value-input";import { evaluateMath } from "./evaluate-math";-import { toKebabCase } from "../keyword-utils";const unitsList = Object.values(units).flat();@@ -110,7 +110,7 @@ export const parseIntermediateOrInvalidValue = (}// Probably in kebab-case value will be valid- styleInput = parseCssValue(property, toKebabCase(value));+ styleInput = parseCssValue(property, kebabCase(value));if (styleInput.type !== "invalid") {return styleInput;diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.tsindex 6b53a98d58..9ba4da5220 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts@@ -1,14 +1,8 @@import { describe, test, expect } from "vitest";import { parseIntermediateOrInvalidValue } from "./parse-intermediate-or-invalid-value";-import { toKebabCase, toPascalCase } from "../keyword-utils";const properties = ["width", "line-height"] as const;-const propertiesAndKeywords = [- ["width", "auto" as string],- ["line-height", "normal" as string],-] as const;-test("forgive trailing semicolon", () => {expect(parseIntermediateOrInvalidValue("width", {@@ -99,37 +93,37 @@ describe("Parse intermediate or invalid value without math evaluation", () => {});test("accept keywords", () => {- for (const [propery, keyword] of propertiesAndKeywords) {- const result = parseIntermediateOrInvalidValue(propery, {+ expect(+ parseIntermediateOrInvalidValue("width", {type: "intermediate",- value: keyword,+ value: "auto",unit: "em",- });-- expect(result).toEqual({- type: "keyword",- value: keyword,- });- }+ })+ ).toEqual({ type: "keyword", value: "auto" });+ expect(+ parseIntermediateOrInvalidValue("line-height", {+ type: "intermediate",+ value: "normal",+ unit: "em",+ })+ ).toEqual({ type: "keyword", value: "normal" });});test("accept keywords written as pascal case", () => {- const pascalCaseKeywords = propertiesAndKeywords.map(- ([property, keyword]) => [property, toPascalCase(keyword)] as const- );-- for (const [propery, keyword] of pascalCaseKeywords) {- const result = parseIntermediateOrInvalidValue(propery, {+ expect(+ parseIntermediateOrInvalidValue("width", {type: "intermediate",- value: keyword,+ value: "Auto",unit: "em",- });-- expect(result).toEqual({- type: "keyword",- value: toKebabCase(keyword),- });- }+ })+ ).toEqual({ type: "keyword", value: "auto" });+ expect(+ parseIntermediateOrInvalidValue("line-height", {+ type: "intermediate",+ value: "Normal",+ unit: "em",+ })+ ).toEqual({ type: "keyword", value: "normal" });});test("keyword with pascal case name", () => {diff --git a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsxindex 2191d0d9a1..dc9736edf5 100644--- a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx@@ -1,4 +1,3 @@-import { camelCase } from "change-case";import {type InvalidValue,type TupleValue,@@ -150,7 +149,7 @@ export const FilterSectionContent = ({value: string,options: StyleUpdateOptions = { isEphemeral: false }) => {- const parsed = parseCssFragment(value, [camelCase(property)]);+ const parsed = parseCssFragment(value, [property]);const parsedValue = parsed.get(property);const invalid = parsedValue === undefined || parsedValue.type === "invalid";setIntermediateValue({diff --git a/apps/builder/app/builder/features/style-panel/shared/keyword-utils.test.ts b/apps/builder/app/builder/features/style-panel/shared/keyword-utils.test.tsdeleted file mode 100644index 46e8fccc22..0000000000--- a/apps/builder/app/builder/features/style-panel/shared/keyword-utils.test.ts+++ /dev/null@@ -1,10 +0,0 @@-import { describe, expect, test } from "vitest";-import { toPascalCase } from "./keyword-utils";--describe("keyword-utils", () => {- test("toPascalCase", () => {- expect(toPascalCase("box")).toBe("Box");- expect(toPascalCase("border-box")).toBe("Border Box");- expect(toPascalCase("-border-box")).toBe("Border Box");- });-});diff --git a/apps/builder/app/builder/features/style-panel/shared/keyword-utils.ts b/apps/builder/app/builder/features/style-panel/shared/keyword-utils.tsdeleted file mode 100644index 40e30058ef..0000000000--- a/apps/builder/app/builder/features/style-panel/shared/keyword-utils.ts+++ /dev/null@@ -1,29 +0,0 @@-/**- * Beautify a CSS keyword by capitalizing the first letter of each word,- * and replacing dashes with spaces.- */-export const toPascalCase = (keyword: string) => {- const label = keyword- .replace(/-/g, " ")- .split(" ")- .filter(Boolean)- .map((word) => {- return word[0].toUpperCase() + word.slice(1);- })- .join(" ");-- return label;-};--/**- * Try to transform something from Pascal Case / Pascal Case / camelCase / camel Case to kebab-case- */-export const toKebabCase = (keyword: string) => {- const label = keyword- .trim()- .replace(/([a-z])([A-Z])/g, "$1-$2")- .replace(/\s+/g, "-")- .toLowerCase();-- return label;-};diff --git a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsxindex 87a9b77a56..35b3cd70f5 100644--- a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx@@ -33,7 +33,6 @@ import {} from "@webstudio-is/icons";import type { IntermediateStyleValue } from "../shared/css-value-input";import { CssValueInputContainer } from "../shared/css-value-input";-import { toPascalCase } from "../shared/keyword-utils";import type { StyleUpdateOptions } from "../shared/use-style-data";import {CssFragmentEditor,@@ -93,11 +92,6 @@ const convertValuesToTupple = (};};-const boxShadowInsetValues = [- { value: "normal", Icon: ShadowNormalIcon },- { value: "inset", Icon: ShadowInsetIcon },-] as const;-const shadowPropertySyntaxes = {"box-shadow": {x: propertySyntaxes.boxShadowOffsetX,@@ -217,7 +211,6 @@ export const ShadowContent = ({description={shadowPropertySyntaxes[property].x}/><CssValueInputContainer- key="boxShadowOffsetX"// outline-offset is a fake property for validating box-shadow's offsetX.property="outline-offset"styleSource="local"@@ -241,7 +234,6 @@ export const ShadowContent = ({description={shadowPropertySyntaxes[property].y}/><CssValueInputContainer- key="boxShadowOffsetY"// outline-offset is a fake property for validating box-shadow's offsetY.property="outline-offset"styleSource="local"@@ -265,7 +257,6 @@ export const ShadowContent = ({description={shadowPropertySyntaxes[property].blur}/><CssValueInputContainer- key="boxShadowBlur"// border-top-width is a fake property for validating box-shadow's blur.property="border-top-width"styleSource="local"@@ -290,7 +281,6 @@ export const ShadowContent = ({description={shadowPropertySyntaxes["box-shadow"].spread}/><CssValueInputContainer- key="boxShadowSpread"// outline-offset is a fake property for validating box-shadow's spread.property="outline-offset"styleSource="local"@@ -354,7 +344,7 @@ export const ShadowContent = ({<ToggleGrouptype="single"disabled={layer.type === "var"}- value={inset?.value ?? "normal"}+ value={inset?.value ?? "outset"}defaultValue="inset"onValueChange={(value) => {if (value === "inset") {@@ -366,13 +356,16 @@ export const ShadowContent = ({}}}>- {boxShadowInsetValues.map(({ value, Icon }) => (- <Tooltip key={value} content={toPascalCase(value)}>- <ToggleGroupButton value={value}>- <Icon />- </ToggleGroupButton>- </Tooltip>- ))}+ <Tooltip content="Outset">+ <ToggleGroupButton value="outset">+ <ShadowNormalIcon />+ </ToggleGroupButton>+ </Tooltip>+ <Tooltip content="Inset">+ <ToggleGroupButton value="inset">+ <ShadowInsetIcon />+ </ToggleGroupButton>+ </Tooltip></ToggleGroup></Flex>) : null}