UNPKG

@wordpress/blocks

Version:
291 lines (240 loc) 9.55 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); Object.defineProperty(exports, "__esModule", { value: true }); exports.isUnmodifiedDefaultBlock = isUnmodifiedDefaultBlock; exports.isValidIcon = isValidIcon; exports.normalizeIconObject = normalizeIconObject; exports.normalizeBlockType = normalizeBlockType; exports.getBlockLabel = getBlockLabel; exports.getAccessibleBlockLabel = getAccessibleBlockLabel; exports.__experimentalSanitizeBlockAttributes = __experimentalSanitizeBlockAttributes; exports.__experimentalGetBlockAttributesNamesByRole = __experimentalGetBlockAttributesNamesByRole; var _lodash = require("lodash"); var _tinycolor = _interopRequireWildcard(require("tinycolor2")); var _element = require("@wordpress/element"); var _i18n = require("@wordpress/i18n"); var _dom = require("@wordpress/dom"); var _registration = require("./registration"); var _factory = require("./factory"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ /** * Array of icon colors containing a color to be used if the icon color * was not explicitly set but the icon background color was. * * @type {Object} */ const ICON_COLORS = ['#191e23', '#f8f9f9']; /** * Determines whether the block is a default block * and its attributes are equal to the default attributes * which means the block is unmodified. * * @param {WPBlock} block Block Object * * @return {boolean} Whether the block is an unmodified default block */ function isUnmodifiedDefaultBlock(block) { const defaultBlockName = (0, _registration.getDefaultBlockName)(); if (block.name !== defaultBlockName) { return false; } // Cache a created default block if no cache exists or the default block // name changed. if (!isUnmodifiedDefaultBlock.block || isUnmodifiedDefaultBlock.block.name !== defaultBlockName) { isUnmodifiedDefaultBlock.block = (0, _factory.createBlock)(defaultBlockName); } const newDefaultBlock = isUnmodifiedDefaultBlock.block; const blockType = (0, _registration.getBlockType)(defaultBlockName); return (0, _lodash.every)(blockType.attributes, (value, key) => newDefaultBlock.attributes[key] === block.attributes[key]); } /** * Function that checks if the parameter is a valid icon. * * @param {*} icon Parameter to be checked. * * @return {boolean} True if the parameter is a valid icon and false otherwise. */ function isValidIcon(icon) { return !!icon && ((0, _lodash.isString)(icon) || (0, _element.isValidElement)(icon) || (0, _lodash.isFunction)(icon) || icon instanceof _element.Component); } /** * Function that receives an icon as set by the blocks during the registration * and returns a new icon object that is normalized so we can rely on just on possible icon structure * in the codebase. * * @param {WPBlockTypeIconRender} icon Render behavior of a block type icon; * one of a Dashicon slug, an element, or a * component. * * @return {WPBlockTypeIconDescriptor} Object describing the icon. */ function normalizeIconObject(icon) { if (isValidIcon(icon)) { return { src: icon }; } if ((0, _lodash.has)(icon, ['background'])) { const tinyBgColor = (0, _tinycolor.default)(icon.background); return { ...icon, foreground: icon.foreground ? icon.foreground : (0, _tinycolor.mostReadable)(tinyBgColor, ICON_COLORS, { includeFallbackColors: true, level: 'AA', size: 'large' }).toHexString(), shadowColor: tinyBgColor.setAlpha(0.3).toRgbString() }; } return icon; } /** * Normalizes block type passed as param. When string is passed then * it converts it to the matching block type object. * It passes the original object otherwise. * * @param {string|Object} blockTypeOrName Block type or name. * * @return {?Object} Block type. */ function normalizeBlockType(blockTypeOrName) { if ((0, _lodash.isString)(blockTypeOrName)) { return (0, _registration.getBlockType)(blockTypeOrName); } return blockTypeOrName; } /** * Get the label for the block, usually this is either the block title, * or the value of the block's `label` function when that's specified. * * @param {Object} blockType The block type. * @param {Object} attributes The values of the block's attributes. * @param {Object} context The intended use for the label. * * @return {string} The block label. */ function getBlockLabel(blockType, attributes, context = 'visual') { const { __experimentalLabel: getLabel, title } = blockType; const label = getLabel && getLabel(attributes, { context }); if (!label) { return title; } // Strip any HTML (i.e. RichText formatting) before returning. return (0, _dom.__unstableStripHTML)(label); } /** * Get a label for the block for use by screenreaders, this is more descriptive * than the visual label and includes the block title and the value of the * `getLabel` function if it's specified. * * @param {Object} blockType The block type. * @param {Object} attributes The values of the block's attributes. * @param {?number} position The position of the block in the block list. * @param {string} [direction='vertical'] The direction of the block layout. * * @return {string} The block label. */ function getAccessibleBlockLabel(blockType, attributes, position, direction = 'vertical') { // `title` is already localized, `label` is a user-supplied value. const { title } = blockType; const label = getBlockLabel(blockType, attributes, 'accessibility'); const hasPosition = position !== undefined; // getBlockLabel returns the block title as a fallback when there's no label, // if it did return the title, this function needs to avoid adding the // title twice within the accessible label. Use this `hasLabel` boolean to // handle that. const hasLabel = label && label !== title; if (hasPosition && direction === 'vertical') { if (hasLabel) { return (0, _i18n.sprintf)( /* translators: accessibility text. 1: The block title. 2: The block row number. 3: The block label.. */ (0, _i18n.__)('%1$s Block. Row %2$d. %3$s'), title, position, label); } return (0, _i18n.sprintf)( /* translators: accessibility text. 1: The block title. 2: The block row number. */ (0, _i18n.__)('%1$s Block. Row %2$d'), title, position); } else if (hasPosition && direction === 'horizontal') { if (hasLabel) { return (0, _i18n.sprintf)( /* translators: accessibility text. 1: The block title. 2: The block column number. 3: The block label.. */ (0, _i18n.__)('%1$s Block. Column %2$d. %3$s'), title, position, label); } return (0, _i18n.sprintf)( /* translators: accessibility text. 1: The block title. 2: The block column number. */ (0, _i18n.__)('%1$s Block. Column %2$d'), title, position); } if (hasLabel) { return (0, _i18n.sprintf)( /* translators: accessibility text. %1: The block title. %2: The block label. */ (0, _i18n.__)('%1$s Block. %2$s'), title, label); } return (0, _i18n.sprintf)( /* translators: accessibility text. %s: The block title. */ (0, _i18n.__)('%s Block'), title); } /** * Ensure attributes contains only values defined by block type, and merge * default values for missing attributes. * * @param {string} name The block's name. * @param {Object} attributes The block's attributes. * @return {Object} The sanitized attributes. */ function __experimentalSanitizeBlockAttributes(name, attributes) { // Get the type definition associated with a registered block. const blockType = (0, _registration.getBlockType)(name); if (undefined === blockType) { throw new Error(`Block type '${name}' is not registered.`); } return (0, _lodash.reduce)(blockType.attributes, (accumulator, schema, key) => { const value = attributes[key]; if (undefined !== value) { accumulator[key] = value; } else if (schema.hasOwnProperty('default')) { accumulator[key] = schema.default; } if (['node', 'children'].indexOf(schema.source) !== -1) { // Ensure value passed is always an array, which we're expecting in // the RichText component to handle the deprecated value. if (typeof accumulator[key] === 'string') { accumulator[key] = [accumulator[key]]; } else if (!Array.isArray(accumulator[key])) { accumulator[key] = []; } } return accumulator; }, {}); } /** * Filter block attributes by `role` and return their names. * * @param {string} name Block attribute's name. * @param {string} role The role of a block attribute. * * @return {string[]} The attribute names that have the provided role. */ function __experimentalGetBlockAttributesNamesByRole(name, role) { var _getBlockType; const attributes = (_getBlockType = (0, _registration.getBlockType)(name)) === null || _getBlockType === void 0 ? void 0 : _getBlockType.attributes; if (!attributes) return []; const attributesNames = Object.keys(attributes); if (!role) return attributesNames; return attributesNames.filter(attributeName => { var _attributes$attribute; return ((_attributes$attribute = attributes[attributeName]) === null || _attributes$attribute === void 0 ? void 0 : _attributes$attribute.__experimentalRole) === role; }); } //# sourceMappingURL=utils.js.map