@wordpress/block-editor
Version:
8 lines (7 loc) • 3.35 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/block-title/use-block-display-title.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport {\n\t__experimentalGetBlockLabel as getBlockLabel,\n\tstore as blocksStore,\n} from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\n\n/**\n * Returns the block's configured title as a string, or empty if the title\n * cannot be determined.\n *\n * @example\n *\n * ```js\n * useBlockDisplayTitle( { clientId: 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1', maximumLength: 17 } );\n * ```\n *\n * @param {Object} props\n * @param {string} props.clientId Client ID of block.\n * @param {number|undefined} props.maximumLength The maximum length that the block title string may be before truncated.\n * @param {string|undefined} props.context The context to pass to `getBlockLabel`.\n * @return {?string} Block title.\n */\nexport default function useBlockDisplayTitle( {\n\tclientId,\n\tmaximumLength,\n\tcontext,\n} ) {\n\tconst blockTitle = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! clientId ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst { getBlockName, getBlockAttributes } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst { getBlockType, getActiveBlockVariation } =\n\t\t\t\tselect( blocksStore );\n\n\t\t\tconst blockName = getBlockName( clientId );\n\t\t\tconst blockType = getBlockType( blockName );\n\t\t\tif ( ! blockType ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst attributes = getBlockAttributes( clientId );\n\t\t\tconst label = getBlockLabel( blockType, attributes, context );\n\t\t\t// If the label is defined we prioritize it over a possible block variation title match.\n\t\t\tif ( label !== blockType.title ) {\n\t\t\t\treturn label;\n\t\t\t}\n\n\t\t\tconst match = getActiveBlockVariation( blockName, attributes );\n\t\t\t// Label will fallback to the title if no label is defined for the current label context.\n\t\t\treturn match?.title || blockType.title;\n\t\t},\n\t\t[ clientId, context ]\n\t);\n\n\tif ( ! blockTitle ) {\n\t\treturn null;\n\t}\n\n\tif (\n\t\tmaximumLength &&\n\t\tmaximumLength > 0 &&\n\t\tblockTitle.length > maximumLength\n\t) {\n\t\tconst omission = '...';\n\t\treturn (\n\t\t\tblockTitle.slice( 0, maximumLength - omission.length ) + omission\n\t\t);\n\t}\n\n\treturn blockTitle;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA0B;AAC1B,oBAGO;AAKP,mBAA0C;AAkB3B,SAAR,qBAAuC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AACD,GAAI;AACH,QAAM,iBAAa;AAAA,IAClB,CAAE,WAAY;AACb,UAAK,CAAE,UAAW;AACjB,eAAO;AAAA,MACR;AAEA,YAAM,EAAE,cAAc,mBAAmB,IACxC,OAAQ,aAAAA,KAAiB;AAC1B,YAAM,EAAE,cAAc,wBAAwB,IAC7C,OAAQ,cAAAC,KAAY;AAErB,YAAM,YAAY,aAAc,QAAS;AACzC,YAAM,YAAY,aAAc,SAAU;AAC1C,UAAK,CAAE,WAAY;AAClB,eAAO;AAAA,MACR;AAEA,YAAM,aAAa,mBAAoB,QAAS;AAChD,YAAM,YAAQ,cAAAC,6BAAe,WAAW,YAAY,OAAQ;AAE5D,UAAK,UAAU,UAAU,OAAQ;AAChC,eAAO;AAAA,MACR;AAEA,YAAM,QAAQ,wBAAyB,WAAW,UAAW;AAE7D,aAAO,OAAO,SAAS,UAAU;AAAA,IAClC;AAAA,IACA,CAAE,UAAU,OAAQ;AAAA,EACrB;AAEA,MAAK,CAAE,YAAa;AACnB,WAAO;AAAA,EACR;AAEA,MACC,iBACA,gBAAgB,KAChB,WAAW,SAAS,eACnB;AACD,UAAM,WAAW;AACjB,WACC,WAAW,MAAO,GAAG,gBAAgB,SAAS,MAAO,IAAI;AAAA,EAE3D;AAEA,SAAO;AACR;",
"names": ["blockEditorStore", "blocksStore", "getBlockLabel"]
}