@wordpress/block-editor
Version:
176 lines (174 loc) • 5.47 kB
JavaScript
;
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/utils/get-font-styles-and-weights.js
var get_font_styles_and_weights_exports = {};
__export(get_font_styles_and_weights_exports, {
getFontStylesAndWeights: () => getFontStylesAndWeights
});
module.exports = __toCommonJS(get_font_styles_and_weights_exports);
var import_i18n = require("@wordpress/i18n");
var import_format_font_style = require("./format-font-style");
var import_format_font_weight = require("./format-font-weight");
var FONT_STYLES = [
{
name: (0, import_i18n._x)("Regular", "font style"),
value: "normal"
},
{
name: (0, import_i18n._x)("Italic", "font style"),
value: "italic"
}
];
var FONT_WEIGHTS = [
{
name: (0, import_i18n._x)("Thin", "font weight"),
value: "100"
},
{
name: (0, import_i18n._x)("Extra Light", "font weight"),
value: "200"
},
{
name: (0, import_i18n._x)("Light", "font weight"),
value: "300"
},
{
name: (0, import_i18n._x)("Regular", "font weight"),
value: "400"
},
{
name: (0, import_i18n._x)("Medium", "font weight"),
value: "500"
},
{
name: (0, import_i18n._x)("Semi Bold", "font weight"),
value: "600"
},
{
name: (0, import_i18n._x)("Bold", "font weight"),
value: "700"
},
{
name: (0, import_i18n._x)("Extra Bold", "font weight"),
value: "800"
},
{
name: (0, import_i18n._x)("Black", "font weight"),
value: "900"
},
{
name: (0, import_i18n._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((0, import_format_font_weight.formatFontWeight)(fontWeightValue));
}
}
}
const fontWeight = (0, import_format_font_weight.formatFontWeight)(
"number" === typeof face.fontWeight ? face.fontWeight.toString() : face.fontWeight
);
const fontStyle = (0, import_format_font_style.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: (0, import_i18n._x)("Bold", "font weight"),
value: "700"
});
}
if (!fontStyles.some((style) => style.value === "italic")) {
fontStyles.push({
name: (0, import_i18n._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 : (0, import_i18n.sprintf)(
/* translators: 1: Font weight name. 2: Font style name. */
(0, import_i18n._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
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getFontStylesAndWeights
});
//# sourceMappingURL=get-font-styles-and-weights.js.map