UNPKG

@wordpress/block-library

Version:
1 lines • 4.25 kB
{"version":3,"file":"caption.mjs","names":["captionIcon","caption"],"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\t) }\n\t\t</>\n\t);\n}\n"],"mappings":";;;;;;;;;AAYA,SAAgB,QAAS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