UNPKG

@wordpress/block-library

Version:
1 lines • 6.97 kB
{"version":3,"file":"edit.cjs","names":["useSelect","coreStore","usePostTerms","useBlockDisplayInformation","useBlockProps","InspectorControls","TextControl","__","WCSpinner","RichText","decodeEntities","createBlock","getDefaultBlockName"],"sources":["../../src/post-terms/edit.jsx"],"sourcesContent":["import {\n\tInspectorControls,\n\tuseBlockProps,\n\tuseBlockDisplayInformation,\n\tRichText,\n} from '@wordpress/block-editor';\nimport { createBlock, getDefaultBlockName } from '@wordpress/blocks';\nimport { Spinner as WCSpinner, TextControl } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { store as coreStore } from '@wordpress/core-data';\nimport usePostTerms from './use-post-terms';\n\n// Allowed formats for the prefix and suffix fields.\nconst ALLOWED_FORMATS = [\n\t'core/bold',\n\t'core/image',\n\t'core/italic',\n\t'core/link',\n\t'core/strikethrough',\n\t'core/text-color',\n];\n\nexport default function PostTermsEdit( {\n\tattributes,\n\tclientId,\n\tcontext,\n\tisSelected,\n\tsetAttributes,\n\tinsertBlocksAfter,\n} ) {\n\tconst { term, separator, prefix, suffix } = attributes;\n\tconst { postId, postType } = context;\n\n\tconst selectedTerm = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! term ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t\tconst { getTaxonomy } = select( coreStore );\n\t\t\tconst taxonomy = getTaxonomy( term );\n\t\t\treturn taxonomy?.visibility?.publicly_queryable ? taxonomy : {};\n\t\t},\n\t\t[ term ]\n\t);\n\tconst { postTerms, hasPostTerms, isLoading } = usePostTerms( {\n\t\tpostId,\n\t\tterm: selectedTerm,\n\t} );\n\tconst hasPost = postId && postType;\n\tconst blockInformation = useBlockDisplayInformation( clientId );\n\tconst blockProps = useBlockProps( {\n\t\tclassName: term && `taxonomy-${ term }`,\n\t} );\n\n\treturn (\n\t\t<>\n\t\t\t<InspectorControls group=\"advanced\">\n\t\t\t\t<TextControl\n\t\t\t\t\tautoComplete=\"off\"\n\t\t\t\t\tlabel={ __( 'Separator' ) }\n\t\t\t\t\tvalue={ separator || '' }\n\t\t\t\t\tonChange={ ( nextValue ) => {\n\t\t\t\t\t\tsetAttributes( { separator: nextValue } );\n\t\t\t\t\t} }\n\t\t\t\t\thelp={ __( 'Enter character(s) used to separate terms.' ) }\n\t\t\t\t/>\n\t\t\t</InspectorControls>\n\t\t\t<div { ...blockProps }>\n\t\t\t\t{ isLoading && hasPost && <WCSpinner /> }\n\t\t\t\t{ ! isLoading && ( isSelected || prefix ) && (\n\t\t\t\t\t<RichText\n\t\t\t\t\t\tidentifier=\"prefix\"\n\t\t\t\t\t\tallowedFormats={ ALLOWED_FORMATS }\n\t\t\t\t\t\tclassName=\"wp-block-post-terms__prefix\"\n\t\t\t\t\t\taria-label={ __( 'Prefix' ) }\n\t\t\t\t\t\tplaceholder={ __( 'Prefix' ) + ' ' }\n\t\t\t\t\t\tvalue={ prefix }\n\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\tsetAttributes( { prefix: value } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttagName=\"span\"\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t\t{ ( ! hasPost || ! term ) && (\n\t\t\t\t\t<span>{ blockInformation.title }</span>\n\t\t\t\t) }\n\t\t\t\t{ hasPost &&\n\t\t\t\t\t! isLoading &&\n\t\t\t\t\thasPostTerms &&\n\t\t\t\t\tpostTerms\n\t\t\t\t\t\t.map( ( postTerm ) => (\n\t\t\t\t\t\t\t<a\n\t\t\t\t\t\t\t\tkey={ postTerm.id }\n\t\t\t\t\t\t\t\thref={ postTerm.link }\n\t\t\t\t\t\t\t\tonClick={ ( event ) => event.preventDefault() }\n\t\t\t\t\t\t\t\trel=\"tag\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ decodeEntities( postTerm.name ) }\n\t\t\t\t\t\t\t</a>\n\t\t\t\t\t\t) )\n\t\t\t\t\t\t.reduce( ( prev, curr ) => (\n\t\t\t\t\t\t\t<>\n\t\t\t\t\t\t\t\t{ prev }\n\t\t\t\t\t\t\t\t<span className=\"wp-block-post-terms__separator\">\n\t\t\t\t\t\t\t\t\t{ separator || ' ' }\n\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t\t{ curr }\n\t\t\t\t\t\t\t</>\n\t\t\t\t\t\t) ) }\n\t\t\t\t{ hasPost &&\n\t\t\t\t\t! isLoading &&\n\t\t\t\t\t! hasPostTerms &&\n\t\t\t\t\t( selectedTerm?.labels?.no_terms ||\n\t\t\t\t\t\t__( 'Term items not found.' ) ) }\n\t\t\t\t{ ! isLoading && ( isSelected || suffix ) && (\n\t\t\t\t\t<RichText\n\t\t\t\t\t\tidentifier=\"suffix\"\n\t\t\t\t\t\tallowedFormats={ ALLOWED_FORMATS }\n\t\t\t\t\t\tclassName=\"wp-block-post-terms__suffix\"\n\t\t\t\t\t\taria-label={ __( 'Suffix' ) }\n\t\t\t\t\t\tplaceholder={ ' ' + __( 'Suffix' ) }\n\t\t\t\t\t\tvalue={ suffix }\n\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\tsetAttributes( { suffix: value } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttagName=\"span\"\n\t\t\t\t\t\t__unstableOnSplitAtEnd={\n\t\t\t\t\t\t\tinsertBlocksAfter\n\t\t\t\t\t\t\t\t? () =>\n\t\t\t\t\t\t\t\t\t\tinsertBlocksAfter(\n\t\t\t\t\t\t\t\t\t\t\tcreateBlock( getDefaultBlockName() )\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t</div>\n\t\t</>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,MAAM,kBAAkB;CACvB;CACA;CACA;CACA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