@wordpress/block-editor
Version:
82 lines (81 loc) • 3.23 kB
JavaScript
// packages/block-editor/src/components/global-styles/typography-utils.js
import { getFontStylesAndWeights } from "../../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 } = 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 };
}
export {
findNearestFontStyle,
findNearestFontWeight,
findNearestStyleAndWeight,
getMergedFontFamiliesAndFontFamilyFaces
};
//# sourceMappingURL=typography-utils.js.map