UNPKG

@wordpress/block-editor

Version:
95 lines (78 loc) 2.75 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useBlockDisplayTitle; var _data = require("@wordpress/data"); var _blocks = require("@wordpress/blocks"); var _useBlockDisplayInformation = _interopRequireDefault(require("../use-block-display-information")); var _store = require("../../store"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Returns the block's configured title as a string, or empty if the title * cannot be determined. * * @example * * ```js * useBlockDisplayTitle( { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1', maximumLength: 17 } ); * ``` * * @param {Object} props * @param {string} props.clientId Client ID of block. * @param {number|undefined} props.maximumLength The maximum length that the block title string may be before truncated. * @param {string|undefined} props.context The context to pass to `getBlockLabel`. * @return {?string} Block title. */ function useBlockDisplayTitle({ clientId, maximumLength, context }) { const { attributes, name, reusableBlockTitle } = (0, _data.useSelect)(select => { if (!clientId) { return {}; } const { getBlockName, getBlockAttributes, __experimentalGetReusableBlockTitle } = select(_store.store); const blockName = getBlockName(clientId); if (!blockName) { return {}; } const isReusable = (0, _blocks.isReusableBlock)((0, _blocks.getBlockType)(blockName)); return { attributes: getBlockAttributes(clientId), name: blockName, reusableBlockTitle: isReusable && __experimentalGetReusableBlockTitle(getBlockAttributes(clientId).ref) }; }, [clientId]); const blockInformation = (0, _useBlockDisplayInformation.default)(clientId); if (!name || !blockInformation) { return null; } const blockType = (0, _blocks.getBlockType)(name); const blockLabel = blockType ? (0, _blocks.__experimentalGetBlockLabel)(blockType, attributes, context) : null; const label = reusableBlockTitle || blockLabel; // Label will fallback to the title if no label is defined for the current // label context. If the label is defined we prioritize it over a // possible block variation title match. const blockTitle = label && label !== blockType.title ? label : blockInformation.title; if (maximumLength && maximumLength > 0 && blockTitle.length > maximumLength) { const omission = '...'; return blockTitle.slice(0, maximumLength - omission.length) + omission; } return blockTitle; } //# sourceMappingURL=use-block-display-title.js.map