@wordpress/block-editor
Version:
8 lines (7 loc) • 4.92 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/list-view.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { PanelBody } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { store as blocksStore, hasBlockSupport } from '@wordpress/blocks';\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../store';\nimport { PrivateListView } from '../components/list-view';\nimport InspectorControls from '../components/inspector-controls/fill';\nimport { PrivateBlockContext } from '../components/block-list/private-block-context';\n\nexport const LIST_VIEW_SUPPORT_KEY = 'listView';\n\n/**\n * Check if the block has list view support.\n *\n * @param {string|Object} nameOrType Block name or block type object.\n * @return {boolean} Whether the block has list view support.\n */\nexport function hasListViewSupport( nameOrType ) {\n\treturn hasBlockSupport( nameOrType, LIST_VIEW_SUPPORT_KEY );\n}\n\n/**\n * Inspector controls panel for list view.\n *\n * @param {Object} props Component props.\n * @param {string} props.clientId Block client ID.\n * @param {string} props.name Block name.\n * @return {Element|null} List view inspector controls or null.\n */\nexport function ListViewPanel( { clientId, name } ) {\n\tconst { isSelectionWithinCurrentSection } =\n\t\tuseContext( PrivateBlockContext );\n\tconst isEnabled = hasListViewSupport( name );\n\tconst { hasChildren, blockTitle, isNestedListView } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockCount, getBlockParents, getBlockName } =\n\t\t\t\tselect( blockEditorStore );\n\n\t\t\t// When the ListView is shown in a section, avoid showing List Views\n\t\t\t// for both parent and child blocks that have support. In this situation\n\t\t\t// the parent will show the child anyway in its List.\n\t\t\t// Search parents to see if there's one that also has support, and if so\n\t\t\t// skip rendering.\n\t\t\t// This matches closely the logic in the `BlockCard` component.\n\t\t\tlet _isNestedListView = false;\n\t\t\tif ( isSelectionWithinCurrentSection ) {\n\t\t\t\tconst parents = getBlockParents( clientId, true );\n\t\t\t\t_isNestedListView = parents.find( ( parentId ) => {\n\t\t\t\t\tconst parentName = getBlockName( parentId );\n\t\t\t\t\treturn (\n\t\t\t\t\t\tparentName === 'core/navigation' ||\n\t\t\t\t\t\thasBlockSupport( parentName, 'listView' )\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\thasChildren: !! getBlockCount( clientId ),\n\t\t\t\tblockTitle: select( blocksStore ).getBlockType( name )?.title,\n\t\t\t\tisNestedListView: _isNestedListView,\n\t\t\t};\n\t\t},\n\t\t[ clientId, name, isSelectionWithinCurrentSection ]\n\t);\n\n\tif ( ! isEnabled || isNestedListView ) {\n\t\treturn null;\n\t}\n\n\tconst showBlockTitle = isSelectionWithinCurrentSection;\n\n\treturn (\n\t\t<InspectorControls group=\"list\">\n\t\t\t<PanelBody title={ showBlockTitle ? blockTitle : undefined }>\n\t\t\t\t{ ! hasChildren && (\n\t\t\t\t\t<p className=\"block-editor-block-inspector__no-blocks\">\n\t\t\t\t\t\t{ __( 'No items yet.' ) }\n\t\t\t\t\t</p>\n\t\t\t\t) }\n\t\t\t\t<PrivateListView\n\t\t\t\t\trootClientId={ clientId }\n\t\t\t\t\tisExpanded\n\t\t\t\t\tdescription={ blockTitle }\n\t\t\t\t\tshowAppender\n\t\t\t\t/>\n\t\t\t</PanelBody>\n\t\t</InspectorControls>\n\t);\n}\n\n/**\n * Export block support definition.\n */\nexport default {\n\tedit: ListViewPanel,\n\thasSupport: hasListViewSupport,\n\tattributeKeys: [],\n\tsupportsPatternEditing: true,\n};\n"],
"mappings": ";AAGA,SAAS,UAAU;AACnB,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB;AAC1B,SAAS,SAAS,aAAa,uBAAuB;AACtD,SAAS,kBAAkB;AAK3B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,uBAAuB;AAChC,OAAO,uBAAuB;AAC9B,SAAS,2BAA2B;AAkEjC,SAEE,KAFF;AAhEI,IAAM,wBAAwB;AAQ9B,SAAS,m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