UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 6.12 kB
{ "version": 3, "sources": ["../../src/quote/edit.js"], "sourcesContent": ["import clsx from 'clsx';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tAlignmentControl,\n\tBlockControls,\n\tuseBlockProps,\n\tuseInnerBlocksProps,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { BlockQuotation } from '@wordpress/components';\nimport { useDispatch, useRegistry, useSelect } from '@wordpress/data';\nimport { useEffect } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\nimport { verse } from '@wordpress/icons';\nimport { migrateToQuoteV2 } from './deprecated';\nimport { Caption } from '../utils/caption';\n\n/**\n * At the moment, deprecations don't handle create blocks from attributes\n * (like when using CPT templates). For this reason, this hook is necessary\n * to avoid breaking templates using the old quote block format.\n *\n * @param {Object} attributes Block attributes.\n * @param {string} clientId Block client ID.\n */\nconst useMigrateOnLoad = ( attributes, clientId ) => {\n\tconst registry = useRegistry();\n\tconst { updateBlockAttributes, replaceInnerBlocks, selectBlock } =\n\t\tuseDispatch( blockEditorStore );\n\tuseEffect( () => {\n\t\t// As soon as the block is loaded, migrate it to the new version.\n\n\t\tif ( ! attributes.value ) {\n\t\t\t// No need to migrate if it doesn't have the value attribute.\n\t\t\treturn;\n\t\t}\n\n\t\tconst [ newAttributes, newInnerBlocks ] =\n\t\t\tmigrateToQuoteV2( attributes );\n\n\t\tdeprecated( 'Value attribute on the quote block', {\n\t\t\tsince: '6.0',\n\t\t\tversion: '6.5',\n\t\t\talternative: 'inner blocks',\n\t\t} );\n\n\t\t// The selection can be inside an inner block created from the block\n\t\t// type template at insertion, which the migration replaces; restore\n\t\t// the selection to the migrated block in that case.\n\t\tconst shouldReselectBlock = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.hasSelectedInnerBlock( clientId, true );\n\n\t\tregistry.batch( () => {\n\t\t\tupdateBlockAttributes( clientId, newAttributes );\n\t\t\treplaceInnerBlocks( clientId, newInnerBlocks );\n\t\t\tif ( shouldReselectBlock ) {\n\t\t\t\tselectBlock( clientId );\n\t\t\t}\n\t\t} );\n\t}, [ attributes.value ] );\n};\n\nexport default function QuoteEdit( {\n\tattributes,\n\tsetAttributes,\n\tinsertBlocksAfter,\n\tclientId,\n\tclassName,\n\tisSelected,\n} ) {\n\tconst { textAlign, allowedBlocks } = attributes;\n\n\tconst { hasInnerBlocks } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockCount } = select( blockEditorStore );\n\t\t\treturn {\n\t\t\t\thasInnerBlocks: getBlockCount( clientId ) > 0,\n\t\t\t};\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tuseMigrateOnLoad( attributes, clientId );\n\n\tconst blockProps = useBlockProps( {\n\t\tclassName: clsx( className, {\n\t\t\t[ `has-text-align-${ textAlign }` ]: textAlign,\n\t\t} ),\n\t} );\n\tconst innerBlocksProps = useInnerBlocksProps( blockProps, {\n\t\t__experimentalCaptureToolbars: true,\n\t\trenderAppender: hasInnerBlocks ? false : undefined,\n\t\tallowedBlocks,\n\t} );\n\n\treturn (\n\t\t<>\n\t\t\t<BlockControls group=\"block\">\n\t\t\t\t<AlignmentControl\n\t\t\t\t\tvalue={ textAlign }\n\t\t\t\t\tonChange={ ( nextAlign ) => {\n\t\t\t\t\t\tsetAttributes( { textAlign: nextAlign } );\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t</BlockControls>\n\t\t\t<BlockQuotation { ...innerBlocksProps }>\n\t\t\t\t{ innerBlocksProps.children }\n\t\t\t\t<Caption\n\t\t\t\t\tattributeKey=\"citation\"\n\t\t\t\t\ttagName=\"cite\"\n\t\t\t\t\tstyle={ { display: 'block' } }\n\t\t\t\t\tisSelected={ isSelected }\n\t\t\t\t\tattributes={ attributes }\n\t\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\t\ticon={ verse }\n\t\t\t\t\tlabel={ __( 'Quote citation' ) }\n\t\t\t\t\tplaceholder={\n\t\t\t\t\t\t// translators: placeholder text used for the\n\t\t\t\t\t\t// citation\n\t\t\t\t\t\t__( 'Add citation' )\n\t\t\t\t\t}\n\t\t\t\t\taddLabel={ __( 'Add citation' ) }\n\t\t\t\t\tremoveLabel={ __( 'Remove citation' ) }\n\t\t\t\t\texcludeElementClassName\n\t\t\t\t\tclassName=\"wp-block-quote__citation\"\n\t\t\t\t\tinsertBlocksAfter={ insertBlocksAfter }\n\t\t\t\t/>\n\t\t\t</BlockQuotation>\n\t\t</>\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiB;AACjB,kBAAmB;AACnB,0BAMO;AACP,wBAA+B;AAC/B,kBAAoD;AACpD,qBAA0B;AAC1B,wBAAuB;AACvB,mBAAsB;AACtB,IAAAA,qBAAiC;AACjC,qBAAwB;AAkFtB;AAxEF,IAAM,mBAAmB,CAAE,YAAY,aAAc;AACpD,QAAM,eAAW,yBAAY;AAC7B,QAAM,EAAE,uBAAuB,oBAAoB,YAAY,QAC9D,yBAAa,oBAAAC,KAAiB;AAC/B,gCAAW,MAAM;AAGhB,QAAK,CAAE,WAAW,OAAQ;AAEzB;AAAA,IACD;AAEA,UAAM,CAAE,eAAe,cAAe,QACrC,qCAAkB,UAAW;AAE9B,0BAAAC,SAAY,sCAAsC;AAAA,MACjD,OAAO;AAAA,MACP,SAAS;AAAA,MACT,aAAa;AAAA,IACd,CAAE;AAKF,UAAM,sBAAsB,SAC1B,OAAQ,oBAAAD,KAAiB,EACzB,sBAAuB,UAAU,IAAK;AAExC,aAAS,MAAO,MAAM;AACrB,4BAAuB,UAAU,aAAc;AAC/C,yBAAoB,UAAU,cAAe;AAC7C,UAAK,qBAAsB;AAC1B,oBAAa,QAAS;AAAA,MACvB;AAAA,IACD,CAAE;AAAA,EACH,GAAG,CAAE,WAAW,KAAM,CAAE;AACzB;AAEe,SAAR,UAA4B;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,E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