Skip to content

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.

detect font family

Oleg Isonen2fdb44b4b041
diff --git a/apps/designer/@types/detect-font.d.ts b/apps/designer/@types/detect-font.d.ts
new file mode 100644
index 0000000000..5dc8d92953
--- /dev/null
+++ b/apps/designer/@types/detect-font.d.ts
@@ -0,0 +1,8 @@
+declare module "detect-font" {
+ type Options = {
+ text?: string;
+ fontSize?: number;
+ baseFont?: string;
+ };
+ export function detectFont(element: Element, options?: Options): string;
+}
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 9ad3d8c290..66ed7d9efe 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -34,6 +34,7 @@
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"debug": "^4.3.4",
+ "detect-font": "^0.1.5",
"downshift": "^6.1.7",
"hyphenate-style-name": "^1.0.4",
"immer": "^9.0.12",
diff --git a/packages/react-sdk/src/css/get-browser-style.ts b/packages/react-sdk/src/css/get-browser-style.ts
index 9f7d3f8e58..fabb46ef6b 100644
--- a/packages/react-sdk/src/css/get-browser-style.ts
+++ b/packages/react-sdk/src/css/get-browser-style.ts
@@ -1,3 +1,4 @@
+import { detectFont } from "detect-font";
import type { StyleProperty } from "./types";
import type { Style, StyleValue, Unit } from "./schema";
import { properties } from "./properties";
@@ -39,5 +40,10 @@ export const getBrowserStyle = (element?: Element): Style => {
const computedValue = computedStyle[knownProperty as unknown as number];
browserStyle[knownProperty] = parseValue(knownProperty, computedValue);
}
+ // We need a single font-family that is actually rendered. Computed style will return a list of potential fonts.
+ browserStyle.fontFamily = {
+ type: "fontFamily",
+ value: detectFont(element),
+ };
return browserStyle;
};
diff --git a/yarn.lock b/yarn.lock
index e3e83c0e52..efb3153ddc 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -8471,6 +8471,13 @@ [email protected]:
dependencies:
repeat-string "^1.5.4"
+detect-font@^0.1.5:
+ version "0.1.5"
+ resolved "https://registry.yarnpkg.com/detect-font/-/detect-font-0.1.5.tgz#e9612bc2e9f87431b891847c1a5459d6b9a82eff"
+ integrity sha512-Ir0jJlsJ7hkpbqzpr8TB5grZTbhZ2Lk90Z0Yt1LpUd1xTZEl/bLhKypDyLc7jEeuuI8ZA0kmDwxARB4n0/jPTA==
+ dependencies:
+ is-element "^0.1.0"
+
detect-indent@^6.0.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.1.0.tgz#592485ebbbf6b3b1ab2be175c8393d04ca0d57e6"
@@ -11577,6 +11584,11 @@ is-dom@^1.0.0:
is-object "^1.0.1"
is-window "^1.0.2"
+is-element@^0.1.0:
+ version "0.1.0"
+ resolved "https://registry.yarnpkg.com/is-element/-/is-element-0.1.0.tgz#c43478df14ab02b7c66d7498fb5456600bfef1fb"
+ integrity sha512-qtlzvwm4bPrI6hBRHBhLWgp/fUeDaeCBRcT8eHhI7yP1C2g45AilloSAmbPgeAMm3InTgMy6z3ZImq9byMlYXg==
+
is-extendable@^0.1.0, is-extendable@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"