UNPKG

@wordpress/block-library

Version:
1 lines • 4.61 kB
{"version":3,"file":"format-controls.cjs","names":["__","useSelect","coreStore","FormTokenField"],"sources":["../../../../src/query/edit/inspector-controls/format-controls.jsx"],"sourcesContent":["import { FormTokenField } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __ } from '@wordpress/i18n';\n\n// All WP post formats, sorted alphabetically by translated name.\n// Value is the post format slug. Label is the name.\nconst POST_FORMATS = [\n\t{ value: 'aside', label: __( 'Aside' ) },\n\t{ value: 'audio', label: __( 'Audio' ) },\n\t{ value: 'chat', label: __( 'Chat' ) },\n\t{ value: 'gallery', label: __( 'Gallery' ) },\n\t{ value: 'image', label: __( 'Image' ) },\n\t{ value: 'link', label: __( 'Link' ) },\n\t{ value: 'quote', label: __( 'Quote' ) },\n\t{ value: 'standard', label: __( 'Standard' ) },\n\t{ value: 'status', label: __( 'Status' ) },\n\t{ value: 'video', label: __( 'Video' ) },\n].sort( ( a, b ) => {\n\tconst normalizedA = a.label.toUpperCase();\n\tconst normalizedB = b.label.toUpperCase();\n\n\tif ( normalizedA < normalizedB ) {\n\t\treturn -1;\n\t}\n\tif ( normalizedA > normalizedB ) {\n\t\treturn 1;\n\t}\n\treturn 0;\n} );\n\n// A helper function to convert translatable post format names into their static values.\nfunction formatNamesToValues( names, formats ) {\n\treturn names\n\t\t.map( ( name ) => {\n\t\t\treturn formats.find(\n\t\t\t\t( item ) =>\n\t\t\t\t\titem.label.toLocaleLowerCase() === name.toLocaleLowerCase()\n\t\t\t)?.value;\n\t\t} )\n\t\t.filter( Boolean );\n}\n\nexport default function FormatControls( { onChange, query: { format } } ) {\n\t// 'format' is expected to be an array. If it is not an array, for example\n\t// if a user has manually entered an invalid value in the block markup,\n\t// convert it to an array to prevent JavaScript errors.\n\tconst normalizedFormats = Array.isArray( format ) ? format : [ format ];\n\n\tconst { supportedFormats } = useSelect( ( select ) => {\n\t\tconst themeSupports = select( coreStore ).getThemeSupports();\n\t\treturn {\n\t\t\tsupportedFormats: themeSupports.formats,\n\t\t};\n\t}, [] );\n\n\tconst formats = POST_FORMATS.filter( ( item ) =>\n\t\tsupportedFormats.includes( item.value )\n\t);\n\n\tconst values = normalizedFormats\n\t\t.map(\n\t\t\t( name ) => formats.find( ( item ) => item.value === name )?.label\n\t\t)\n\t\t.filter( Boolean );\n\n\tconst suggestions = formats\n\t\t.filter( ( item ) => ! normalizedFormats.includes( item.value ) )\n\t\t.map( ( item ) => item.label );\n\n\treturn (\n\t\t<FormTokenField\n\t\t\tlabel={ __( 'Formats' ) }\n\t\t\tvalue={ values }\n\t\t\tsuggestions={ suggestions }\n\t\t\tonChange={ ( newValues ) => {\n\t\t\t\tonChange( {\n\t\t\t\t\tformat: formatNamesToValues( newValues, formats ),\n\t\t\t\t} );\n\t\t\t} }\n\t\t\thelp=\"\"\n\t\t\t__experimentalExpandOnFocus\n\t\t/>\n\t);\n}\n"],"mappings":";;;;;;;;;;;AAOA,MAAM,eAAe;CACpB;EAAE,OAAO;EAAS,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,OAAQ;CAAE;CACvC;EAAE,OAAO;EAAS,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,OAAQ;CAAE;CACvC;EAAE,OAAO;EAAQ,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,MAAO;CAAE;CACrC;EAAE,OAAO;EAAW,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,SAAU;CAAE;CAC3C;EAAE,OAAO;EAAS,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,OAAQ;CAAE;CACvC;EAAE,OAAO;EAAQ,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,MAAO;CAAE;CACrC;EAAE,OAAO;EAAS,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,OAAQ;CAAE;CACvC;EAAE,OAAO;EAAY,QAAA,GAAOA,gBAAAA,GAAAA,CAAI,UAAW;CAAE;CAC7C;EAAE,OAAO;EAAU,Q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