@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 12.6 kB
Source Map (JSON)
{"version":3,"file":"controls.mjs","names":["blockEditorPrivateApis","blockEditorStore","ToolsPanel","ToolsPanelItem","WCCheckboxControl","WCTextareaControl"],"sources":["../../../src/navigation-link/shared/controls.jsx"],"sourcesContent":["import {\n\tButton,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n\tCheckboxControl as WCCheckboxControl,\n\tTextControl,\n\tTextareaControl as WCTextareaControl,\n} from '@wordpress/components';\nimport { __, sprintf } from '@wordpress/i18n';\nimport {\n\tprivateApis as blockEditorPrivateApis,\n\tstore as blockEditorStore,\n} from '@wordpress/block-editor';\nimport { useSelect } from '@wordpress/data';\nimport { useToolsPanelDropdownMenuProps } from '../../utils/hooks';\nimport { applyPlainTextEdit, getPlainText } from '../../utils/plain-text-edit';\nimport { useHandleLinkChange } from './use-handle-link-change';\nimport { useEntityBinding } from './use-entity-binding';\nimport { getSuggestionsQuery, useLinkPreview } from '../../utils/link-picker';\nimport { useIsInvalidLink } from './use-is-invalid-link';\nimport ViewLinkButton from '../../utils/view-link-button';\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 * @param {boolean} props.isLinkEditable - Whether link editing should be allowed\n */\nexport function Controls( {\n\tattributes,\n\tsetAttributes,\n\tclientId,\n\tisLinkEditable = true,\n} ) {\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 [ isInvalid, isDraft ] = useIsInvalidLink(\n\t\tattributes.kind,\n\t\tattributes.type,\n\t\tentityRecord?.id,\n\t\thasUrlBinding\n\t);\n\n\tlet helpText = '';\n\n\tif ( isInvalid || ( hasUrlBinding && ! isBoundEntityAvailable ) ) {\n\t\t// Show invalid link help text for:\n\t\t// 1. Invalid post-type links (trashed/deleted posts/pages) - via useIsInvalidLink\n\t\t// 2. Missing bound taxonomy entities (deleted categories/tags) - useIsInvalidLink only checks post-types\n\t\thelpText = getInvalidLinkHelpText();\n\t} else if ( isDraft ) {\n\t\thelpText = getDraftHelpText( {\n\t\t\ttype: attributes.type,\n\t\t\tkind: attributes.kind,\n\t\t} );\n\t}\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 onNavigateToEntityRecord = useSelect(\n\t\t( select ) =>\n\t\t\tselect( blockEditorStore ).getSettings().onNavigateToEntityRecord,\n\t\t[]\n\t);\n\n\tconst blockEditingMode = useSelect(\n\t\t( select ) =>\n\t\t\tselect( blockEditorStore ).getBlockEditingMode( clientId ),\n\t\t[ clientId ]\n\t);\n\n\tconst isContentOnly = blockEditingMode === 'contentOnly';\n\n\tconst preview = useLinkPreview( {\n\t\turl,\n\t\tentityRecord,\n\t\ttype: attributes.type,\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\tlabel={ __( 'Text' ) }\n\t\t\t\t\tvalue={ getPlainText( label ) }\n\t\t\t\t\tonChange={ ( labelValue ) => {\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tlabel: applyPlainTextEdit( label, labelValue ),\n\t\t\t\t\t\t} );\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{ isLinkEditable && (\n\t\t\t\t<>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\thasValue={ () => !! url }\n\t\t\t\t\t\tlabel={ __( 'Link to' ) }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\turl: undefined,\n\t\t\t\t\t\t\t\tid: undefined,\n\t\t\t\t\t\t\t\tkind: undefined,\n\t\t\t\t\t\t\t\ttype: undefined,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t>\n\t\t\t\t\t\t<LinkPicker\n\t\t\t\t\t\t\tpreview={ preview }\n\t\t\t\t\t\t\tonSelect={ handleLinkChange }\n\t\t\t\t\t\t\tsuggestionsQuery={ getSuggestionsQuery(\n\t\t\t\t\t\t\t\tattributes.type,\n\t\t\t\t\t\t\t\tattributes.kind\n\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\tlabel={ __( 'Link to' ) }\n\t\t\t\t\t\t\thelp={ helpText ? helpText : undefined }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\thasValue={ () => !! opensInNewTab }\n\t\t\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( { opensInNewTab: false } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t>\n\t\t\t\t\t\t<WCCheckboxControl\n\t\t\t\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\t\t\t\tchecked={ opensInNewTab }\n\t\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\t\tsetAttributes( { opensInNewTab: value } )\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\n\t\t\t\t\t{ !! url &&\n\t\t\t\t\t\thasUrlBinding &&\n\t\t\t\t\t\tisBoundEntityAvailable &&\n\t\t\t\t\t\tentityRecord?.id &&\n\t\t\t\t\t\tattributes.kind === 'post-type' &&\n\t\t\t\t\t\tonNavigateToEntityRecord && (\n\t\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\tonNavigateToEntityRecord( {\n\t\t\t\t\t\t\t\t\t\tpostId: entityRecord.id,\n\t\t\t\t\t\t\t\t\t\tpostType: attributes.type,\n\t\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\t\tclassName=\"navigation-link-to__action-button\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ __( 'Edit' ) }\n\t\t\t\t\t\t\t</Button>\n\t\t\t\t\t\t) }\n\t\t\t\t\t<ViewLinkButton url={ url } />\n\t\t\t\t</>\n\t\t\t) }\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={ ! isContentOnly }\n\t\t\t>\n\t\t\t\t<WCTextareaControl\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={ ! isContentOnly }\n\t\t\t>\n\t\t\t\t<TextControl\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 * Returns help text for invalid links.\n *\n * @return {string} Error help text string (empty string if valid).\n */\nexport function getInvalidLinkHelpText() {\n\treturn __(\n\t\t'This link is invalid and will not appear on your site. Please update the link.'\n\t);\n}\n\n/**\n * Returns the help text for links to draft entities\n *\n * @param {Object} props - Function props\n * @param {string} props.type - The entity type\n * @param {string} props.kind - The entity kind\n * @return {string} Draft help text\n */\nfunction getDraftHelpText( { type, kind } ) {\n\tconst entityType = getEntityTypeName( type, kind );\n\treturn sprintf(\n\t\t/* translators: %1$s is the entity type (e.g., \"page\", \"post\", \"category\") */\n\t\t__(\n\t\t\t'This link is to a draft %1$s and will not appear on your site until the %1$s is published.'\n\t\t),\n\t\tentityType\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;AAuBA,MAAM,EAAE,eAAe,OAAQA,WAAuB;;;;;;;;AAStD,SAAS,kBAAmB,MAAM,MAAO;CACxC,IAAK,SAAS,aACb,QAAS,MAAT;EACC,KAAK,QACJ,OAAO,GAAI,MAAO;EACnB,KAAK,QACJ,OAAO,GAAI,MAAO;EACnB,SACC,OAAO,QAAQ,GAAI,MAAO;CAC5B;CAED,IAAK,SAAS,YACb,QAAS,MAAT;EACC,KAAK,YACJ,OAAO,GAAI,UAAW;EACvB,KAAK,OACJ,OAAO,GAAI,KAAM;EAClB,SACC,OAAO,QAAQ,GAAI,MAAO;CAC5B;CAED,OAAO,QAAQ,GAAI,MAAO;AAC3B;;;;;;;;;;;;;AAcA,SAAgB,SAAU,EACzB,YACA,eACA,UACA,iBAAiB,QACd;CACH,MAAM,EAAE,OAAO,KAAK,aAAa,KAAK,kBAAkB;CACxD,MAAM,oBAAoB,+BAA+B;CAGzD,MAAM,EAAE,eAAe,wBAAwB,iBAC9C,iBAAkB;EACjB;EACA;CACD,CAAE;CAEH,MAAM,C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