UNPKG

@wordpress/block-library

Version:
1 lines • 4.73 kB
{"version":3,"file":"caption.cjs","names":["__","captionIcon","RichText","useState","useCallback","BlockControls","ToolbarButton","__experimentalGetElementClassName","createBlock","getDefaultBlockName"],"sources":["../../src/utils/caption.jsx"],"sourcesContent":["import clsx from 'clsx';\nimport { useState, useCallback } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tBlockControls,\n\t__experimentalGetElementClassName,\n\tRichText,\n} from '@wordpress/block-editor';\nimport { ToolbarButton } from '@wordpress/components';\nimport { caption as captionIcon } from '@wordpress/icons';\nimport { createBlock, getDefaultBlockName } from '@wordpress/blocks';\n\nexport function Caption( {\n\tattributeKey = 'caption',\n\tattributes,\n\tsetAttributes,\n\tisSelected,\n\tinsertBlocksAfter,\n\tplaceholder = __( 'Add caption' ),\n\tlabel = __( 'Caption text' ),\n\tshowToolbarButton = true,\n\texcludeElementClassName,\n\tclassName,\n\treadOnly,\n\ttagName = 'figcaption',\n\taddLabel = __( 'Add caption' ),\n\tremoveLabel = __( 'Remove caption' ),\n\ticon = captionIcon,\n\t...props\n} ) {\n\tconst caption = attributes[ attributeKey ];\n\tconst isCaptionEmpty = RichText.isEmpty( caption );\n\tconst [ showCaption, setShowCaption ] = useState( ! isCaptionEmpty );\n\n\t// We need to show the caption when changes come from\n\t// history navigation(undo/redo).\n\tif ( ! isCaptionEmpty && ! showCaption ) {\n\t\tsetShowCaption( true );\n\t}\n\n\tif ( ! isSelected && isCaptionEmpty && showCaption ) {\n\t\tsetShowCaption( false );\n\t}\n\n\t// Focus the caption when we click to add one.\n\tconst ref = useCallback(\n\t\t( node ) => {\n\t\t\tif ( node && isCaptionEmpty ) {\n\t\t\t\tnode.focus();\n\t\t\t}\n\t\t},\n\t\t[ isCaptionEmpty ]\n\t);\n\n\treturn (\n\t\t<>\n\t\t\t{ showToolbarButton && (\n\t\t\t\t<BlockControls group=\"block\">\n\t\t\t\t\t<ToolbarButton\n\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\tsetShowCaption( ! showCaption );\n\t\t\t\t\t\t\tif ( showCaption && caption ) {\n\t\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\t\t[ attributeKey ]: undefined,\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} }\n\t\t\t\t\t\ticon={ icon }\n\t\t\t\t\t\tisPressed={ showCaption }\n\t\t\t\t\t\tlabel={ showCaption ? removeLabel : addLabel }\n\t\t\t\t\t/>\n\t\t\t\t</BlockControls>\n\t\t\t) }\n\t\t\t{ showCaption &&\n\t\t\t\t( ! RichText.isEmpty( caption ) || isSelected ) && (\n\t\t\t\t\t<RichText\n\t\t\t\t\t\tidentifier={ attributeKey }\n\t\t\t\t\t\ttagName={ tagName }\n\t\t\t\t\t\tclassName={ clsx(\n\t\t\t\t\t\t\tclassName,\n\t\t\t\t\t\t\texcludeElementClassName\n\t\t\t\t\t\t\t\t? ''\n\t\t\t\t\t\t\t\t: __experimentalGetElementClassName( 'caption' )\n\t\t\t\t\t\t) }\n\t\t\t\t\t\tref={ ref }\n\t\t\t\t\t\taria-label={ label }\n\t\t\t\t\t\tplaceholder={ placeholder }\n\t\t\t\t\t\tvalue={ caption }\n\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\tsetAttributes( { [ attributeKey ]: value } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tinlineToolbar\n\t\t\t\t\t\t__unstableOnSplitAtEnd={\n\t\t\t\t\t\t\tinsertBlocksAfter\n\t\t\t\t\t\t\t\t? () =>\n\t\t\t\t\t\t\t\t\t\tinsertBlocksAfter(\n\t\t\t\t\t\t\t\t\t\t\tcreateBlock( getDefaultBlockName() )\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t\treadOnly={ readOnly }\n\t\t\t\t\t\t{ ...props }\n\t\t\t\t\t/>\n\t\t\t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