index.js
92 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { withSelect } from '@wordpress/data'; |
| 5 | import { __ } from '@wordpress/i18n'; |
| 6 | // 登録されたブロックを取得するため |
| 7 | import '@wordpress/block-library'; |
| 8 | |
| 9 | /** |
| 10 | * Internal dependencies |
| 11 | */ |
| 12 | import BlockManagerCategory from './category'; |
| 13 | /*globals vkBlocksObject */ |
| 14 | |
| 15 | function BlockManager({ blockTypes, categories, hasBlockSupport }) { |
| 16 | const showCategories = categories.filter((category) => { |
| 17 | return category.slug.match(/vk-blocks/); |
| 18 | }); |
| 19 | |
| 20 | const showBlockTypes = []; |
| 21 | blockTypes.forEach((blockType) => { |
| 22 | if ( |
| 23 | // showCategoriesにcategoryが含まれる |
| 24 | showCategories.find((showCategory) => { |
| 25 | return showCategory.slug === blockType.category; |
| 26 | }) !== undefined && |
| 27 | // vk-blocksが含まれる |
| 28 | blockType.name.match(/vk-blocks/) && |
| 29 | // inserterがtrueのもの |
| 30 | hasBlockSupport(blockType, 'inserter', true) && |
| 31 | // 子ブロックではない |
| 32 | !blockType.parent && |
| 33 | // 非推奨ブロックに含まれない |
| 34 | !vkBlocksObject.deprecatedLists.includes(blockType.name) |
| 35 | ) { |
| 36 | showBlockTypes.push(blockType); |
| 37 | } else if (vkBlocksObject.deprecatedLists.includes(blockType.name)) { |
| 38 | // 非推奨ブロックの時はカテゴリー名を上書きする |
| 39 | const pushBlockType = { |
| 40 | ...blockType, |
| 41 | category: 'vk-blocks-cat-deprecated', |
| 42 | }; |
| 43 | showBlockTypes.push(pushBlockType); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | return ( |
| 48 | <> |
| 49 | <section> |
| 50 | <h3 id="block-manager-setting"> |
| 51 | {__('Block Manager Setting', 'vk-blocks')} |
| 52 | </h3> |
| 53 | <div |
| 54 | tabIndex="0" |
| 55 | role="region" |
| 56 | className="block-manager__results" |
| 57 | > |
| 58 | {showCategories.map((category) => { |
| 59 | const propsBlockTypes = |
| 60 | showBlockTypes && |
| 61 | showBlockTypes.filter((blockType) => { |
| 62 | return ( |
| 63 | blockType && |
| 64 | blockType.category && |
| 65 | blockType.category === category.slug |
| 66 | ); |
| 67 | }); |
| 68 | return ( |
| 69 | <BlockManagerCategory |
| 70 | key={category.slug} |
| 71 | title={category.title} |
| 72 | blockTypes={propsBlockTypes} |
| 73 | /> |
| 74 | ); |
| 75 | })} |
| 76 | </div> |
| 77 | </section> |
| 78 | </> |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | export default withSelect((select) => { |
| 83 | const { getCategories, getBlockTypes, hasBlockSupport } = |
| 84 | select('core/blocks'); |
| 85 | |
| 86 | return { |
| 87 | blockTypes: getBlockTypes(), |
| 88 | categories: getCategories(), |
| 89 | hasBlockSupport, |
| 90 | }; |
| 91 | })(BlockManager); |
| 92 |