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
use Map instead of object
Oleg Isonen02f74bade0a7
diff --git a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsxindex 065f2d700c..258b08522c 100644--- a/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx+++ b/apps/designer/app/designer/shared/fonts-manager/fonts-manager.tsx@@ -20,7 +20,7 @@ import { DotsHorizontalIcon } from "@webstudio-is/icons";const getItems = (assets: Array<Asset | PreviewAsset>): Array<{ label: string }> => {- const system = Object.keys(SYSTEM_FONTS).map((label) => ({ label }));+ const system = Array.from(SYSTEM_FONTS.keys()).map((label) => ({ label }));// We can have 2+ assets with the same family name, so we use a map to dedupe.const uploaded = new Map();for (const asset of assets) {diff --git a/packages/asset-uploader/src/utils/filter-by-type.ts b/packages/asset-uploader/src/utils/filter-by-type.tsindex 39b1900747..e32af2d3fe 100644--- a/packages/asset-uploader/src/utils/filter-by-type.ts+++ b/packages/asset-uploader/src/utils/filter-by-type.ts@@ -1,4 +1,4 @@-import { FONT_FORMATS } from "@webstudio-is/fonts";+import { type FontFormat, FONT_FORMATS } from "@webstudio-is/fonts";import type { AssetType } from "../types";export const filterByType = <PartialAsset extends { format: string }>(@@ -6,7 +6,7 @@ export const filterByType = <PartialAsset extends { format: string }>(type: AssetType) => {return assets.filter(({ format }: PartialAsset) => {- const isFont = format in FONT_FORMATS;+ const isFont = FONT_FORMATS.has(format as FontFormat);if (type === "font") {return isFont;}diff --git a/packages/asset-uploader/src/utils/format-asset.ts b/packages/asset-uploader/src/utils/format-asset.tsindex 52461b2ba8..dd786943b5 100644--- a/packages/asset-uploader/src/utils/format-asset.ts+++ b/packages/asset-uploader/src/utils/format-asset.ts@@ -14,7 +14,7 @@ export type ImageMeta = z.infer<typeof ImageMeta>;export const formatAsset = (asset: DbAsset): Asset => {const base = { ...asset, path: getAssetPath(asset) };- const isFont = asset.format in FONT_FORMATS;+ const isFont = FONT_FORMATS.has(asset.format as FontFormat);if (isFont) {return {diff --git a/packages/fonts/src/constants.ts b/packages/fonts/src/constants.tsindex 42b34cc09a..03df1b85b3 100644--- a/packages/fonts/src/constants.ts+++ b/packages/fonts/src/constants.ts@@ -1,21 +1,21 @@import type { FontFormat } from "./types";-export const SYSTEM_FONTS: Record<string, Array<string>> = {- Arial: ["sans-serif"],- "Times New Roman": ["serif"],- "Courier New": ["monospace"],- "system-ui": [],-};+export const SYSTEM_FONTS = new Map([+ ["Arial", ["sans-serif"]],+ ["Times New Roman", ["sans"]],+ ["Courier New", ["monospace"]],+ ["system-ui", []],+]);export const DEFAULT_FONT_FALLBACK = "sans-serif";-export const FONT_FORMATS: { [key in FontFormat]: string } = {- woff: "woff",- woff2: "woff2",- ttf: "truetype",- otf: "opentype",-} as const;+export const FONT_FORMATS: Map<FontFormat, string> = new Map([+ ["woff", "woff"],+ ["woff2", "woff2"],+ ["ttf", "truetype"],+ ["otf", "opentype"],+]);-export const FONT_MIME_TYPES = Object.keys(FONT_FORMATS)+export const FONT_MIME_TYPES = Array.from(FONT_FORMATS.keys()).map((format) => `.${format}`).join(", ");diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.tsindex d8902f52e2..a946615838 100644--- a/packages/fonts/src/get-font-faces.ts+++ b/packages/fonts/src/get-font-faces.ts@@ -12,7 +12,7 @@ export const getFontFaces = (assets: Array<PartialFontAsset>) => {const faces = new Map();for (const asset of assets) {const face = faces.get(asset.meta.family);- const format = FONT_FORMATS[asset.format as FontFormat];+ const format = FONT_FORMATS.get(asset.format);if (face === undefined) {faces.set(asset.meta.family, {diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsindex 7abfd55129..c362cc2c7c 100644--- a/packages/react-sdk/src/stitches/to-css.ts+++ b/packages/react-sdk/src/stitches/to-css.ts@@ -8,8 +8,9 @@ export const toValue = (value: StyleValue): string => {}if (value.type === "fontFamily") {const family = value.value[0];- if (family in SYSTEM_FONTS) {- return [...value.value, ...SYSTEM_FONTS[family]].join(", ");+ const fallbacks = SYSTEM_FONTS.get(family);+ if (Array.isArray(fallbacks)) {+ return [...value.value, ...fallbacks].join(", ");}return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");}diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.tsindex 8cd2f1dc28..74f82849d2 100644--- a/packages/react-sdk/src/tree/root.ts+++ b/packages/react-sdk/src/tree/root.ts@@ -7,7 +7,11 @@ import {WrapperComponent,type WrapperComponentProps,} from "./wrapper-component";-import { FONT_FORMATS, getFontFaces } from "@webstudio-is/fonts";+import {+ type FontFormat,+ FONT_FORMATS,+ getFontFaces,+} from "@webstudio-is/fonts";import type { Asset, FontAsset } from "@webstudio-is/asset-uploader";import { useRef } from "react";@@ -25,8 +29,8 @@ export const useGlobalStyles = ({ assets }: { assets: Array<Asset> }) => {// so we have to manually ensure calling it only onceif (ref.current === assets) return;- const fontAssets = assets.filter(- (asset) => asset.format in FONT_FORMATS+ const fontAssets = assets.filter((asset) =>+ FONT_FORMATS.has(asset.format as FontFormat)) as Array<FontAsset>;globalCss({