{"version":3,"file":"patterns-manage-button.mjs","names":["blockEditorStore","coreStore","patternsStore","ConfirmDialog"],"sources":["../../src/components/patterns-manage-button.tsx"],"sourcesContent":["import {\n\tMenuItem,\n\t__experimentalConfirmDialog as ConfirmDialog,\n} from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { isReusableBlock } from '@wordpress/blocks';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useState } from '@wordpress/element';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { addQueryArgs } from '@wordpress/url';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as patternsStore } from '../store';\nimport { unlock } from '../lock-unlock';\nimport type { PatternsManageButtonProps } from './types';\n\nfunction PatternsManageButton( {\n\tclientId,\n\tonClose,\n}: PatternsManageButtonProps ) {\n\tconst [ showConfirmDialog, setShowConfirmDialog ] = useState( false );\n\n\tconst {\n\t\tattributes,\n\t\tcanDetach,\n\t\tisVisible,\n\t\tmanagePatternsUrl,\n\t\tisSyncedPattern,\n\t\tisUnsyncedPattern,\n\t\tcanEdit,\n\t} = useSelect(\n\t\t( select ) => {\n\t\t\tconst { canRemoveBlock, getBlock, canEditBlock } =\n\t\t\t\tselect( blockEditorStore );\n\t\t\tconst { canUser } = select( coreStore );\n\t\t\tconst block = getBlock( clientId );\n\n\t\t\tconst _isUnsyncedPattern =\n\t\t\t\t!! block?.attributes?.metadata?.patternName;\n\n\t\t\t// Detaching only copies the pattern's blocks into the post, so\n\t\t\t// reading the pattern is enough. Editing it isn't required.\n\t\t\tconst _isSyncedPattern =\n\t\t\t\t!! block &&\n\t\t\t\tisReusableBlock( block ) &&\n\t\t\t\t!! canUser( 'read', {\n\t\t\t\t\tkind: 'postType',\n\t\t\t\t\tname: 'wp_block',\n\t\t\t\t\t// A pattern block's `ref` is its `wp_block` post ID.\n\t\t\t\t\tid: block.attributes.ref as number,\n\t\t\t\t} );\n\n\t\t\treturn {\n\t\t\t\t// The menu only renders for a block that is in the editor.\n\t\t\t\tattributes: block!.attributes,\n\t\t\t\tcanEdit: canEditBlock( clientId ),\n\t\t\t\t// For unsynced patterns, detaching is simply removing the `patternName` attribute.\n\t\t\t\t// For synced patterns, the `core:block` block is replaced with its inner blocks,\n\t\t\t\t// so checking whether `canRemoveBlock` is possible is required.\n\t\t\t\tcanDetach:\n\t\t\t\t\t_isUnsyncedPattern ||\n\t\t\t\t\t( _isSyncedPattern && canRemoveBlock( clientId ) ),\n\t\t\t\tisUnsyncedPattern: _isUnsyncedPattern,\n\t\t\t\tisSyncedPattern: _isSyncedPattern,\n\t\t\t\tisVisible: _isUnsyncedPattern || _isSyncedPattern,\n\t\t\t\t// The site editor and templates both check whether the user\n\t\t\t\t// has edit_theme_options capabilities. We can leverage that here\n\t\t\t\t// and omit the manage patterns link if the user can't access it.\n\t\t\t\tmanagePatternsUrl: canUser( 'create', {\n\t\t\t\t\tkind: 'postType',\n\t\t\t\t\tname: 'wp_template',\n\t\t\t\t} )\n\t\t\t\t\t? addQueryArgs( 'site-editor.php', {\n\t\t\t\t\t\t\tp: '/pattern',\n\t\t\t\t\t\t} )\n\t\t\t\t\t: addQueryArgs( 'edit.php', {\n\t\t\t\t\t\t\tpost_type: 'wp_block',\n\t\t\t\t\t\t} ),\n\t\t\t};\n\t\t},\n\t\t[ clientId ]\n\t);\n\n\tconst { updateBlockAttributes } = useDispatch( blockEditorStore );\n\n\t// Ignore reason: false positive of the lint rule.\n\t// eslint-disable-next-line @wordpress/no-unused-vars-before-return\n\tconst { convertSyncedPatternToStatic } = unlock(\n\t\tuseDispatch( patternsStore )\n\t);\n\n\tif ( ! isVisible || ! canEdit ) {\n\t\treturn null;\n\t}\n\n\tconst handleDetach = () => {\n\t\tif ( isSyncedPattern ) {\n\t\t\tconvertSyncedPatternToStatic( clientId );\n\t\t}\n\n\t\tif ( isUnsyncedPattern ) {\n\t\t\tconst { patternName, ...attributesWithoutPatternName } =\n\t\t\t\tattributes?.metadata ?? {};\n\t\t\tupdateBlockAttributes( clientId, {\n\t\t\t\tmetadata: attributesWithoutPatternName,\n\t\t\t} );\n\t\t}\n\t\tonClose?.();\n\t\tsetShowConfirmDialog( false );\n\t};\n\n\treturn (\n\t\t<>\n\t\t\t{ canDetach && (\n\t\t\t\t<>\n\t\t\t\t\t<MenuItem onClick={ () => setShowConfirmDialog( true ) }>\n\t\t\t\t\t\t{ __( 'Detach' ) }\n\t\t\t\t\t</MenuItem>\n\t\t\t\t\t<ConfirmDialog\n\t\t\t\t\t\tisOpen={ showConfirmDialog }\n\t\t\t\t\t\tonConfirm={ handleDetach }\n\t\t\t\t\t\tonCancel={ () => setShowConfirmDialog( false ) }\n\t\t\t\t\t\tconfirmButtonText={ __( 'Detach' ) }\n\t\t\t\t\t\tsize=\"medium\"\n\t\t\t\t\t\ttitle={ __( 'Detach pattern?' ) }\n\t\t\t\t\t\t__experimentalHideHeader={ false }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ isSyncedPattern\n\t\t\t\t\t\t\t? __(\n\t\t\t\t\t\t\t\t\t'The blocks will be separated from the original pattern and will be fully editable. Future changes to the pattern will not apply here.'\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: __(\n\t\t\t\t\t\t\t\t\t'Blocks will no longer be associated with this pattern and will be fully editable.'\n\t\t\t\t\t\t\t\t) }\n\t\t\t\t\t</ConfirmDialog>\n\t\t\t\t</>\n\t\t\t) }\n\t\t\t<MenuItem href={ managePatternsUrl }>\n\t\t\t\t{ __( 'Manage patterns' ) }\n\t\t\t</MenuItem>\n\t\t</>\n\t);\n}\n\nexport default PatternsManageButton;\n"],"mappings":";;;;;;;;;;;;AAeA,SAAS,qBAAsB,EAC9B,UACA,WAC8B;CAC9B,MAAM,CAAE,mBAAmB,wBAAyB,SAAU,KAAM;CAEpE,MAAM,EACL,YACA,WACA,WACA,mBACA,iBACA,mBACA,YACG,WACD,WAAY;EACb,MAAM,EAAE,gBAAgB,UAAU,iBACjC,OAAQA,KAAiB;EAC1B,MAAM,EAAE,YAA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