UNPKG

@wordpress/block-editor

Version:
151 lines (150 loc) 3.99 kB
// packages/block-editor/src/utils/get-font-styles-and-weights.js import { _x, sprintf } from "@wordpress/i18n"; import { formatFontStyle } from "./format-font-style"; import { formatFontWeight } from "./format-font-weight"; var FONT_STYLES = [ { name: _x("Regular", "font style"), value: "normal" }, { name: _x("Italic", "font style"), value: "italic" } ]; var FONT_WEIGHTS = [ { name: _x("Thin", "font weight"), value: "100" }, { name: _x("Extra Light", "font weight"), value: "200" }, { name: _x("Light", "font weight"), value: "300" }, { name: _x("Regular", "font weight"), value: "400" }, { name: _x("Medium", "font weight"), value: "500" }, { name: _x("Semi Bold", "font weight"), value: "600" }, { name: _x("Bold", "font weight"), value: "700" }, { name: _x("Extra Bold", "font weight"), value: "800" }, { name: _x("Black", "font weight"), value: "900" }, { name: _x("Extra Black", "font weight"), value: "1000" } ]; function getFontStylesAndWeights(fontFamilyFaces) { let fontStyles = []; let fontWeights = []; const combinedStyleAndWeightOptions = []; const isSystemFont = !fontFamilyFaces || fontFamilyFaces?.length === 0; let isVariableFont = false; fontFamilyFaces?.forEach((face) => { if ("string" === typeof face.fontWeight && /\s/.test(face.fontWeight.trim())) { isVariableFont = true; let [startValue, endValue] = face.fontWeight.split(" "); startValue = parseInt(startValue.slice(0, 1)); if (endValue === "1000") { endValue = 10; } else { endValue = parseInt(endValue.slice(0, 1)); } for (let i = startValue; i <= endValue; i++) { const fontWeightValue = `${i.toString()}00`; if (!fontWeights.some( (weight) => weight.value === fontWeightValue )) { fontWeights.push(formatFontWeight(fontWeightValue)); } } } const fontWeight = formatFontWeight( "number" === typeof face.fontWeight ? face.fontWeight.toString() : face.fontWeight ); const fontStyle = formatFontStyle(face.fontStyle); if (fontStyle && Object.keys(fontStyle).length) { if (!fontStyles.some( (style) => style.value === fontStyle.value )) { fontStyles.push(fontStyle); } } if (fontWeight && Object.keys(fontWeight).length) { if (!fontWeights.some( (weight) => weight.value === fontWeight.value )) { if (!isVariableFont) { fontWeights.push(fontWeight); } } } }); if (!fontWeights.some((weight) => weight.value >= "600")) { fontWeights.push({ name: _x("Bold", "font weight"), value: "700" }); } if (!fontStyles.some((style) => style.value === "italic")) { fontStyles.push({ name: _x("Italic", "font style"), value: "italic" }); } if (isSystemFont) { fontStyles = FONT_STYLES; fontWeights = FONT_WEIGHTS; } fontStyles = fontStyles.length === 0 ? FONT_STYLES : fontStyles; fontWeights = fontWeights.length === 0 ? FONT_WEIGHTS : fontWeights; fontStyles.forEach(({ name: styleName, value: styleValue }) => { fontWeights.forEach(({ name: weightName, value: weightValue }) => { const optionName = styleValue === "normal" ? weightName : sprintf( /* translators: 1: Font weight name. 2: Font style name. */ _x("%1$s %2$s", "font"), weightName, styleName ); combinedStyleAndWeightOptions.push({ key: `${styleValue}-${weightValue}`, name: optionName, style: { fontStyle: styleValue, fontWeight: weightValue } }); }); }); return { fontStyles, fontWeights, combinedStyleAndWeightOptions, isSystemFont, isVariableFont }; } export { getFontStylesAndWeights }; //# sourceMappingURL=get-font-styles-and-weights.js.map