UNPKG

@gechiui/block-editor

Version:
94 lines (78 loc) 2.33 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = BlockTitle; var _lodash = require("lodash"); var _data = require("@gechiui/data"); var _blocks = require("@gechiui/blocks"); var _useBlockDisplayInformation = _interopRequireDefault(require("../use-block-display-information")); var _store = require("../../store"); /** * External dependencies */ /** * GeChiUI dependencies */ /** * Internal dependencies */ /** * Renders the block's configured title as a string, or empty if the title * cannot be determined. * * @example * * ```jsx * <BlockTitle clientId="afd1cb17-2c08-4e7a-91be-007ba7ddc3a1" /> * ``` * * @param {Object} props * @param {string} props.clientId Client ID of block. * * @return {?string} Block title. */ function BlockTitle(_ref) { let { clientId } = _ref; 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) : 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 possible // possible block variation title match. if (label && label !== blockType.title) { return (0, _lodash.truncate)(label, { length: 35 }); } return blockInformation.title; } //# sourceMappingURL=index.js.map