@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 4.25 kB
Source Map (JSON)
{"version":3,"file":"edit-original-inspector-button.cjs","names":["unlock","blockEditorPrivateApis","useSelect","blockEditorStore","coreStore","Stack","Button","__"],"sources":["../../src/utils/edit-original-inspector-button.tsx"],"sourcesContent":["import { Button } from '@wordpress/components';\nimport { Stack } from '@wordpress/ui';\nimport { __ } from '@wordpress/i18n';\nimport { useSelect } from '@wordpress/data';\nimport {\n\tprivateApis as blockEditorPrivateApis,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { unlock } from '../lock-unlock';\n\nconst { PrivateBlockInspectorEditOriginal } = unlock( blockEditorPrivateApis );\n\ninterface EditOriginalInspectorButtonProps {\n\t/** Client ID of the block that renders the entity. */\n\tclientId: string;\n\t/** The entity's post type, such as `wp_block` or `wp_template_part`. */\n\tpostType: string;\n\t/** The entity's ID. The button is disabled while it's unknown. */\n\tpostId?: number | string;\n}\n\n/**\n * The block inspector's \"Edit original\" button for a block that renders an\n * entity, such as a synced pattern or a template part. It opens the entity\n * for editing, and only shows for users who can edit it, on a block that\n * isn't locked against editing.\n *\n * @param props Component props.\n * @param props.clientId Client ID of the block that renders the entity.\n * @param props.postType The entity's post type.\n * @param props.postId The entity's ID.\n */\nexport default function EditOriginalInspectorButton( {\n\tclientId,\n\tpostType,\n\tpostId,\n}: EditOriginalInspectorButtonProps ) {\n\tconst { canUserEdit, onNavigateToEntityRecord } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { canEditBlock, getSettings } = select( blockEditorStore );\n\t\t\treturn {\n\t\t\t\t// Matches the block toolbar's \"Edit original\" button.\n\t\t\t\tcanUserEdit:\n\t\t\t\t\tcanEditBlock( clientId ) &&\n\t\t\t\t\t( ! postId ||\n\t\t\t\t\t\t!! select( coreStore ).canUser( 'update', {\n\t\t\t\t\t\t\tkind: 'postType',\n\t\t\t\t\t\t\tname: postType,\n\t\t\t\t\t\t\tid: postId,\n\t\t\t\t\t\t} ) ),\n\t\t\t\tonNavigateToEntityRecord:\n\t\t\t\t\tgetSettings().onNavigateToEntityRecord,\n\t\t\t};\n\t\t},\n\t\t[ clientId, postType, postId ]\n\t);\n\n\tif ( ! canUserEdit || ! onNavigateToEntityRecord ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<PrivateBlockInspectorEditOriginal.Fill>\n\t\t\t{ ( { clientId: inspectedClientId }: { clientId: string } ) =>\n\t\t\t\tinspectedClientId === clientId && (\n\t\t\t\t\t<Stack\n\t\t\t\t\t\tdirection=\"column\"\n\t\t\t\t\t\tclassName=\"block-editor-block-inspector-edit-contents\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\tclassName=\"block-editor-block-inspector-edit-contents__button\"\n\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\t\t\tonClick={ () =>\n\t\t\t\t\t\t\t\tonNavigateToEntityRecord( {\n\t\t\t\t\t\t\t\t\t// The button is disabled without an ID.\n\t\t\t\t\t\t\t\t\tpostId: postId as number | string,\n\t\t\t\t\t\t\t\t\tpostType,\n\t\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\t\t\tdisabled={ ! postId }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Edit original' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</Stack>\n\t\t\t\t)\n\t\t\t}\n\t\t</PrivateBlockInspectorEditOriginal.Fill>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;AAWA,MAAM,EAAE,uCAAA,GAAsCA,kBAAAA,OAAAA,CAAQC,wBAAAA,WAAuB;;;;;;;;;;;;AAsB7E,SAAwB,4BAA6B,EACpD,UACA,UACA,UACqC;CACrC,MAAM,EAAE,aAAa,8BAAA,GAA6BC,gBAAAA,UAAAA,EAC/C,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