@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 3.3 kB
Source Map (JSON)
{"version":3,"file":"author-control.mjs","names":["coreStore"],"sources":["../../../../src/query/edit/inspector-controls/author-control.jsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { FormTokenField } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { getEntitiesInfo } from '../../utils';\n\nconst AUTHORS_QUERY = {\n\twho: 'authors',\n\tper_page: -1,\n\t_fields: 'id,name',\n\tcontext: 'view',\n};\n\nfunction AuthorControl( { value, onChange } ) {\n\tconst authorsList = useSelect( ( select ) => {\n\t\tconst { getUsers } = select( coreStore );\n\t\treturn getUsers( AUTHORS_QUERY );\n\t}, [] );\n\n\tif ( ! authorsList ) {\n\t\treturn null;\n\t}\n\tconst authorsInfo = getEntitiesInfo( authorsList );\n\t/**\n\t * We need to normalize the value because the block operates on a\n\t * comma(`,`) separated string value and `FormTokenField` needs an\n\t * array.\n\t */\n\tconst normalizedValue = ! value ? [] : value.toString().split( ',' );\n\t// Returns only the existing authors ids. This prevents the component\n\t// from crashing in the editor, when non existing ids are provided.\n\tconst sanitizedValue = normalizedValue.reduce(\n\t\t( accumulator, authorId ) => {\n\t\t\tconst author = authorsInfo.mapById[ authorId ];\n\t\t\tif ( author ) {\n\t\t\t\taccumulator.push( {\n\t\t\t\t\tid: authorId,\n\t\t\t\t\tvalue: author.name,\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t},\n\t\t[]\n\t);\n\n\tconst getIdByValue = ( entitiesMappedByName, authorValue ) => {\n\t\tconst id = authorValue?.id || entitiesMappedByName[ authorValue ]?.id;\n\t\tif ( id ) {\n\t\t\treturn id;\n\t\t}\n\t};\n\tconst onAuthorChange = ( newValue ) => {\n\t\tconst ids = Array.from(\n\t\t\tnewValue.reduce( ( accumulator, author ) => {\n\t\t\t\t// Verify that new values point to existing entities.\n\t\t\t\tconst id = getIdByValue( authorsInfo.mapByName, author );\n\t\t\t\tif ( id ) {\n\t\t\t\t\taccumulator.add( id );\n\t\t\t\t}\n\t\t\t\treturn accumulator;\n\t\t\t}, new Set() )\n\t\t);\n\t\tonChange( { author: ids.join( ',' ) } );\n\t};\n\treturn (\n\t\t<FormTokenField\n\t\t\tlabel={ __( 'Authors' ) }\n\t\t\tvalue={ sanitizedValue }\n\t\t\tsuggestions={ authorsInfo.names }\n\t\t\tonChange={ onAuthorChange }\n\t\t\thelp=\"\"\n\t\t/>\n\t);\n}\n\nexport default AuthorControl;\n"],"mappings":";;;;;;;AAMA,MAAM,gBAAgB;CACrB,KAAK;CACL,UAAU;CACV,SAAS;CACT,SAAS;AACV;AAEA,SAAS,cAAe,EAAE,OAAO,YAAa;CAC7C,MAAM,cAAc,WAAa,WAAY;EAC5C,MAAM,EAAE,aAAa,OAAQA,KAAU;EACvC,OAAO,SAAU,aAAc;CAChC,GAAG,CAAC,CAAE;CAEN,IAAK,CAAE,aACN,OAAO;CAER,MAAM,cAAc,gBAAiB,WAAY;CASjD,MAAM,kBAHkB,CAAE,QAAQ,CAAC,IAAI,MAAM,SAAS,CAAC,CAAC,MAAO,GAAI,EAAA,CAG5B,QACpC,aAAa,aAAc;EAC5B,MAAM,SAAS,YAAY,QAAS;EACpC,IAAK,QACJ,YAAY,KAAM;GACjB,IAAI;GACJ,OAAO,OAAO;EACf,CAAE;EAEH,OAAO;CACR,GACA,CAAC,CACF;CAEA,MAAM,gBAAiB,sBAAsB,gBAAiB;EAC7D,MAAM,KAAK,aAAa,MAAM,qBAAsB,YAAa,EAAE;EACnE,IAAK,IACJ,OAAO;CAET;CACA,MAAM,kBAAmB,aAAc;EAWtC,SAAU,EAAE,QAVA,MAAM,KACjB,SAAS,QAAU,aAAa,WAAY;GAE3C,MAAM,KAAK,aAAc,YAAY,WAAW,MAAO;GACvD,IAAK,IACJ,YAAY,IAAK,EAAG;GAErB,OAAO;EACR,mBAAG,IAAI,IAAI,CAAE,CAEQ,CAAC,CAAC,KAAM,GAAI,EAAE,CAAE;CACvC;CACA,OACC,oBAAC,gBAAD;EACC,OAAQ,GAAI,SAAU;EACtB,OAAQ;EACR,aAAc,YAAY;EAC1B,UAAW;EACX,MAAK;CACL,CAAA;AAEH"}