@wordpress/block-editor
Version:
83 lines (77 loc) • 2.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFontSize = void 0;
exports.getFontSizeClass = getFontSizeClass;
exports.getFontSizeObjectByValue = getFontSizeObjectByValue;
var _components = require("@wordpress/components");
var _lockUnlock = require("../../lock-unlock");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const {
kebabCase
} = (0, _lockUnlock.unlock)(_components.privateApis);
/**
* Returns the font size object based on an array of named font sizes and the namedFontSize and customFontSize values.
* If namedFontSize is undefined or not found in fontSizes an object with just the size value based on customFontSize is returned.
*
* @param {Array} fontSizes Array of font size objects containing at least the "name" and "size" values as properties.
* @param {?string} fontSizeAttribute Content of the font size attribute (slug).
* @param {?number} customFontSizeAttribute Contents of the custom font size attribute (value).
*
* @return {?Object} If fontSizeAttribute is set and an equal slug is found in fontSizes it returns the font size object for that slug.
* Otherwise, an object with just the size value based on customFontSize is returned.
*/
const getFontSize = (fontSizes, fontSizeAttribute, customFontSizeAttribute) => {
if (fontSizeAttribute) {
const fontSizeObject = fontSizes?.find(({
slug
}) => slug === fontSizeAttribute);
if (fontSizeObject) {
return fontSizeObject;
}
}
return {
size: customFontSizeAttribute
};
};
/**
* Returns the corresponding font size object for a given value.
*
* @param {Array} fontSizes Array of font size objects.
* @param {number} value Font size value.
*
* @return {Object} Font size object.
*/
exports.getFontSize = getFontSize;
function getFontSizeObjectByValue(fontSizes, value) {
const fontSizeObject = fontSizes?.find(({
size
}) => size === value);
if (fontSizeObject) {
return fontSizeObject;
}
return {
size: value
};
}
/**
* Returns a class based on fontSizeName.
*
* @param {string} fontSizeSlug Slug of the fontSize.
*
* @return {string | undefined} String with the class corresponding to the fontSize passed.
* The class is generated by appending 'has-' followed by fontSizeSlug in kebabCase and ending with '-font-size'.
*/
function getFontSizeClass(fontSizeSlug) {
if (!fontSizeSlug) {
return;
}
return `has-${kebabCase(fontSizeSlug)}-font-size`;
}
//# sourceMappingURL=utils.js.map
;