@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 12.5 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/navigation-link/shared/controls.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n\tCheckboxControl,\n\tTextControl,\n\tTextareaControl,\n} from '@wordpress/components';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { useToolsPanelDropdownMenuProps } from '../../utils/hooks';\nimport { useHandleLinkChange } from './use-handle-link-change';\nimport { useEntityBinding } from './use-entity-binding';\nimport { getSuggestionsQuery } from '../link-ui';\nimport { useLinkPreview } from './use-link-preview';\nimport { unlock } from '../../lock-unlock';\n\nconst { LinkPicker } = unlock( blockEditorPrivateApis );\n\n/**\n * Get a human-readable entity type name.\n *\n * @param {string} type - The entity type\n * @param {string} kind - The entity kind\n * @return {string} Human-readable entity type name\n */\nfunction getEntityTypeName( type, kind ) {\n\tif ( kind === 'post-type' ) {\n\t\tswitch ( type ) {\n\t\t\tcase 'post':\n\t\t\t\treturn __( 'post' );\n\t\t\tcase 'page':\n\t\t\t\treturn __( 'page' );\n\t\t\tdefault:\n\t\t\t\treturn type || __( 'post' );\n\t\t}\n\t}\n\tif ( kind === 'taxonomy' ) {\n\t\tswitch ( type ) {\n\t\t\tcase 'category':\n\t\t\t\treturn __( 'category' );\n\t\t\tcase 'tag':\n\t\t\t\treturn __( 'tag' );\n\t\t\tdefault:\n\t\t\t\treturn type || __( 'term' );\n\t\t}\n\t}\n\treturn type || __( 'item' );\n}\n\n/**\n * Shared Controls component for Navigation Link and Navigation Submenu blocks.\n *\n * This component provides the inspector controls (ToolsPanel) that are identical\n * between both navigation blocks.\n *\n * @param {Object} props - Component props\n * @param {Object} props.attributes - Block attributes\n * @param {Function} props.setAttributes - Function to update block attributes\n * @param {string} props.clientId - Block client ID\n */\nexport function Controls( { attributes, setAttributes, clientId } ) {\n\tconst { label, url, description, rel, opensInNewTab } = attributes;\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\n\t// Use the entity binding hook for UI state (help text, link preview, etc.)\n\tconst { hasUrlBinding, isBoundEntityAvailable, entityRecord } =\n\t\tuseEntityBinding( {\n\t\t\tclientId,\n\t\t\tattributes,\n\t\t} );\n\n\tconst needsHelpText = hasUrlBinding;\n\tconst helpText = isBoundEntityAvailable\n\t\t? BindingHelpText( {\n\t\t\t\ttype: attributes.type,\n\t\t\t\tkind: attributes.kind,\n\t\t } )\n\t\t: MissingEntityHelpText( {\n\t\t\t\ttype: attributes.type,\n\t\t\t\tkind: attributes.kind,\n\t\t } );\n\n\t// Get the link change handler with built-in binding management\n\tconst handleLinkChange = useHandleLinkChange( {\n\t\tclientId,\n\t\tattributes,\n\t\tsetAttributes,\n\t} );\n\n\tconst linkTitle =\n\t\tentityRecord?.title?.rendered ||\n\t\tentityRecord?.title ||\n\t\tentityRecord?.name;\n\n\tconst linkImage = useSelect(\n\t\t( select ) => {\n\t\t\t// Only fetch for post-type entities with featured media\n\t\t\tif ( ! entityRecord?.featured_media ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst { getEntityRecord } = select( coreStore );\n\n\t\t\t// Get the media entity to fetch the image URL\n\t\t\tconst media = getEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'attachment',\n\t\t\t\tentityRecord.featured_media\n\t\t\t);\n\n\t\t\t// Return the thumbnail or medium size URL, fallback to source_url\n\t\t\treturn (\n\t\t\t\tmedia?.media_details?.sizes?.thumbnail?.source_url ||\n\t\t\t\tmedia?.media_details?.sizes?.medium?.source_url ||\n\t\t\t\tmedia?.source_url ||\n\t\t\t\tnull\n\t\t\t);\n\t\t},\n\t\t[ entityRecord?.featured_media ]\n\t);\n\n\tconst preview = useLinkPreview( {\n\t\turl,\n\t\ttitle: linkTitle,\n\t\timage: linkImage,\n\t\ttype: attributes.type,\n\t\tentityStatus: entityRecord?.status,\n\t\thasBinding: hasUrlBinding,\n\t\tisEntityAvailable: isBoundEntityAvailable,\n\t} );\n\n\treturn (\n\t\t<ToolsPanel\n\t\t\tlabel={ __( 'Settings' ) }\n\t\t\tresetAll={ () => {\n\t\t\t\tsetAttributes( {\n\t\t\t\t\tlabel: '',\n\t\t\t\t\turl: '',\n\t\t\t\t\tdescription: '',\n\t\t\t\t\trel: '',\n\t\t\t\t\topensInNewTab: false,\n\t\t\t\t} );\n\t\t\t} }\n\t\t\tdropdownMenuProps={ dropdownMenuProps }\n\t\t>\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => !! label }\n\t\t\t\tlabel={ __( 'Text' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { label: '' } ) }\n\t\t\t\tisShownByDefault\n\t\t\t>\n\t\t\t\t<TextControl\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\tlabel={ __( 'Text' ) }\n\t\t\t\t\tvalue={ label ? stripHTML( label ) : '' }\n\t\t\t\t\tonChange={ ( labelValue ) => {\n\t\t\t\t\t\tsetAttributes( { label: labelValue } );\n\t\t\t\t\t} }\n\t\t\t\t\tautoComplete=\"off\"\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => !! url }\n\t\t\t\tlabel={ __( 'Link to' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { url: '' } ) }\n\t\t\t\tisShownByDefault\n\t\t\t>\n\t\t\t\t<LinkPicker\n\t\t\t\t\tpreview={ preview }\n\t\t\t\t\tonSelect={ handleLinkChange }\n\t\t\t\t\tsuggestionsQuery={ getSuggestionsQuery(\n\t\t\t\t\t\tattributes.type,\n\t\t\t\t\t\tattributes.kind\n\t\t\t\t\t) }\n\t\t\t\t\tlabel={ __( 'Link to' ) }\n\t\t\t\t\thelp={ needsHelpText ? helpText : undefined }\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => !! opensInNewTab }\n\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { opensInNewTab: false } ) }\n\t\t\t\tisShownByDefault\n\t\t\t>\n\t\t\t\t<CheckboxControl\n\t\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\t\tchecked={ opensInNewTab }\n\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\tsetAttributes( { opensInNewTab: value } )\n\t\t\t\t\t}\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => !! description }\n\t\t\t\tlabel={ __( 'Description' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { description: '' } ) }\n\t\t\t\tisShownByDefault\n\t\t\t>\n\t\t\t\t<TextareaControl\n\t\t\t\t\tlabel={ __( 'Description' ) }\n\t\t\t\t\tvalue={ description || '' }\n\t\t\t\t\tonChange={ ( descriptionValue ) => {\n\t\t\t\t\t\tsetAttributes( { description: descriptionValue } );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={ __(\n\t\t\t\t\t\t'The description will be displayed in the menu if the current theme supports it.'\n\t\t\t\t\t) }\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\n\t\t\t<ToolsPanelItem\n\t\t\t\thasValue={ () => !! rel }\n\t\t\t\tlabel={ __( 'Rel attribute' ) }\n\t\t\t\tonDeselect={ () => setAttributes( { rel: '' } ) }\n\t\t\t\tisShownByDefault\n\t\t\t>\n\t\t\t\t<TextControl\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\tlabel={ __( 'Rel attribute' ) }\n\t\t\t\t\tvalue={ rel || '' }\n\t\t\t\t\tonChange={ ( relValue ) => {\n\t\t\t\t\t\tsetAttributes( { rel: relValue } );\n\t\t\t\t\t} }\n\t\t\t\t\tautoComplete=\"off\"\n\t\t\t\t\thelp={ __(\n\t\t\t\t\t\t'The relationship of the linked URL as space-separated link types.'\n\t\t\t\t\t) }\n\t\t\t\t/>\n\t\t\t</ToolsPanelItem>\n\t\t</ToolsPanel>\n\t);\n}\n\n/**\n * Component to display help text for bound URL attributes.\n *\n * @param {Object} props - Component props\n * @param {string} props.type - The entity type\n * @param {string} props.kind - The entity kind\n * @return {string} Help text for the bound URL\n */\nexport function BindingHelpText( { type, kind } ) {\n\tconst entityType = getEntityTypeName( type, kind );\n\treturn sprintf(\n\t\t/* translators: %s is the entity type (e.g., \"page\", \"post\", \"category\") */\n\t\t__( 'Synced with the selected %s.' ),\n\t\tentityType\n\t);\n}\n\n/**\n * Component to display error help text for missing entity bindings.\n *\n * @param {Object} props - Component props\n * @param {string} props.type - The entity type\n * @param {string} props.kind - The entity kind\n * @return {JSX.Element} Error help text component\n */\nexport function MissingEntityHelpText( { type, kind } ) {\n\tconst entityType = getEntityTypeName( type, kind );\n\treturn sprintf(\n\t\t/* translators: %s is the entity type (e.g., \"page\", \"post\", \"category\") */\n\t\t__( 'Synced %s is missing. Please update or remove this link.' ),\n\t\tentityType\n\t);\n}\n"],
"mappings": ";AAGA;AAAA,EACC,4BAA4B;AAAA,EAC5B,gCAAgC;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,IAAI,eAAe;AAC5B,SAAS,uBAAuB,iBAAiB;AACjD,SAAS,eAAe,8BAA8B;AACtD,SAAS,iBAAiB;AAC1B,SAAS,SAAS,iBAAiB;AAKnC,SAAS,sCAAsC;AAC/C,SAAS,2BAA2B;AACpC,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAAS,cAAc;AAsHrB,SAmBE,KAnBF;AApHF,IAAM,EAAE,WAAW,IAAI,OAAQ,sBAAuB;AAStD,S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