UNPKG

@wordpress/block-editor

Version:
110 lines (108 loc) 4.54 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/global-styles/typography-utils.js var typography_utils_exports = {}; __export(typography_utils_exports, { findNearestFontStyle: () => findNearestFontStyle, findNearestFontWeight: () => findNearestFontWeight, findNearestStyleAndWeight: () => findNearestStyleAndWeight, getMergedFontFamiliesAndFontFamilyFaces: () => getMergedFontFamiliesAndFontFamilyFaces }); module.exports = __toCommonJS(typography_utils_exports); var import_get_font_styles_and_weights = require("../../utils/get-font-styles-and-weights"); function getMergedFontFamiliesAndFontFamilyFaces(settings, selectedFontFamily) { const fontFamiliesFromSettings = settings?.typography?.fontFamilies; const fontFamilies = ["default", "theme", "custom"].flatMap( (key) => fontFamiliesFromSettings?.[key] ?? [] ); const fontFamilyFaces = fontFamilies.find( (family) => family.fontFamily === selectedFontFamily )?.fontFace ?? []; return { fontFamilies, fontFamilyFaces }; } function findNearestFontWeight(availableFontWeights, newFontWeightValue) { newFontWeightValue = "number" === typeof newFontWeightValue ? newFontWeightValue.toString() : newFontWeightValue; if (!newFontWeightValue || typeof newFontWeightValue !== "string") { return ""; } if (!availableFontWeights || availableFontWeights.length === 0) { return newFontWeightValue; } const nearestFontWeight = availableFontWeights?.reduce( (nearest, { value: fw }) => { const currentDiff = Math.abs( parseInt(fw) - parseInt(newFontWeightValue) ); const nearestDiff = Math.abs( parseInt(nearest) - parseInt(newFontWeightValue) ); return currentDiff < nearestDiff ? fw : nearest; }, availableFontWeights[0]?.value ); return nearestFontWeight; } function findNearestFontStyle(availableFontStyles, newFontStyleValue) { if (typeof newFontStyleValue !== "string" || !newFontStyleValue) { return ""; } const validStyles = ["normal", "italic", "oblique"]; if (!validStyles.includes(newFontStyleValue)) { return ""; } if (!availableFontStyles || availableFontStyles.length === 0 || availableFontStyles.find( (style) => style.value === newFontStyleValue )) { return newFontStyleValue; } if (newFontStyleValue === "oblique" && !availableFontStyles.find((style) => style.value === "oblique")) { return "italic"; } return ""; } function findNearestStyleAndWeight(fontFamilyFaces, fontStyle, fontWeight) { let nearestFontStyle = fontStyle; let nearestFontWeight = fontWeight; const { fontStyles, fontWeights, combinedStyleAndWeightOptions } = (0, import_get_font_styles_and_weights.getFontStylesAndWeights)(fontFamilyFaces); const hasFontStyle = fontStyles?.some( ({ value: fs }) => fs === fontStyle ); const hasFontWeight = fontWeights?.some( ({ value: fw }) => fw?.toString() === fontWeight?.toString() ); if (!hasFontStyle) { nearestFontStyle = fontStyle ? findNearestFontStyle(fontStyles, fontStyle) : combinedStyleAndWeightOptions?.find( (option) => option.style.fontWeight === findNearestFontWeight(fontWeights, fontWeight) )?.style?.fontStyle; } if (!hasFontWeight) { nearestFontWeight = fontWeight ? findNearestFontWeight(fontWeights, fontWeight) : combinedStyleAndWeightOptions?.find( (option) => option.style.fontStyle === (nearestFontStyle || fontStyle) )?.style?.fontWeight; } return { nearestFontStyle, nearestFontWeight }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { findNearestFontStyle, findNearestFontWeight, findNearestStyleAndWeight, getMergedFontFamiliesAndFontFamilyFaces }); //# sourceMappingURL=typography-utils.js.map