index.js
131 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { useContext, useState } from '@wordpress/element'; |
| 6 | import { withSelect } from '@wordpress/data'; |
| 7 | // コアブロックを取得するため |
| 8 | import { registerCoreBlocks } from '@wordpress/block-library'; |
| 9 | registerCoreBlocks(); |
| 10 | |
| 11 | /** |
| 12 | * Internal dependencies |
| 13 | */ |
| 14 | import { AdminContext } from '@vkblocks/admin/index'; |
| 15 | import { AddButton } from '@vkblocks/admin/custom-block-style/add-button'; |
| 16 | import { Item } from './item'; |
| 17 | /*globals vkBlocksObject */ |
| 18 | |
| 19 | function AdminCustomBlockStyle({ |
| 20 | blockTypes, |
| 21 | categories, |
| 22 | hasBlockSupport, |
| 23 | isMatchingSearchTerm, |
| 24 | }) { |
| 25 | const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext); |
| 26 | const [search, setSearch] = useState(''); |
| 27 | const [openNameLists, setOpenNameLists] = useState([]); |
| 28 | |
| 29 | const onChange = (key, value, index) => { |
| 30 | const newItems = vkBlocksOption.custom_block_style_lists; |
| 31 | newItems[index] = { |
| 32 | ...vkBlocksOption.custom_block_style_lists[index], |
| 33 | [key]: value, |
| 34 | }; |
| 35 | setVkBlocksOption({ |
| 36 | ...vkBlocksOption, |
| 37 | custom_block_style_lists: [...newItems], |
| 38 | }); |
| 39 | }; |
| 40 | |
| 41 | // blockJsonのtitleがあったらblockTypesのtitleがあれば |
| 42 | const getBlockTitle = (name, blockTypeTitle) => { |
| 43 | const blockJsonList = vkBlocksObject.blockJsonLists.find( |
| 44 | (item) => item.name === name |
| 45 | ); |
| 46 | return blockJsonList && blockJsonList.title |
| 47 | ? blockJsonList.title |
| 48 | : blockTypeTitle; |
| 49 | }; |
| 50 | |
| 51 | const showBlockTypes = []; |
| 52 | blockTypes.forEach((blockType) => { |
| 53 | if ( |
| 54 | // inserterがtrueのもの |
| 55 | hasBlockSupport(blockType, 'inserter', true) && |
| 56 | // search |
| 57 | (!search || isMatchingSearchTerm(blockType, search)) && |
| 58 | // customClassがtrueのもの |
| 59 | hasBlockSupport(blockType.name, 'customClassName', true) && |
| 60 | // ブロックマネージャーのリストに含まれない |
| 61 | !vkBlocksOption.disable_block_lists.includes(blockType.name) && |
| 62 | // 親ブロックのinserterがtrueの子ブロック |
| 63 | (!blockType.parent || |
| 64 | !vkBlocksOption.disable_block_lists.includes( |
| 65 | blockType.parent[0] |
| 66 | )) |
| 67 | ) { |
| 68 | blockType.title = getBlockTitle(blockType.name, blockType.title); |
| 69 | showBlockTypes.push(blockType); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | return ( |
| 74 | <> |
| 75 | <section> |
| 76 | <h3 id="custom-block-style-setting"> |
| 77 | {__('Custom Block Style Setting', 'vk-blocks')} |
| 78 | </h3> |
| 79 | <p>{__('You can register block styles.', 'vk-blocks')}</p> |
| 80 | {Object.keys(vkBlocksOption.custom_block_style_lists).map( |
| 81 | (key, index, array) => { |
| 82 | const blockStyleListObj = |
| 83 | vkBlocksOption.custom_block_style_lists[key]; |
| 84 | const activeBlockType = blockTypes.find( |
| 85 | (blockType) => |
| 86 | blockType.name === blockStyleListObj.block_name |
| 87 | ); |
| 88 | return ( |
| 89 | <div key={index}> |
| 90 | <Item |
| 91 | activeBlockType={activeBlockType} |
| 92 | index={index} |
| 93 | onChange={onChange} |
| 94 | blockStyleListObj={blockStyleListObj} |
| 95 | openNameLists={openNameLists} |
| 96 | setOpenNameLists={setOpenNameLists} |
| 97 | array={array} |
| 98 | /> |
| 99 | </div> |
| 100 | ); |
| 101 | } |
| 102 | )} |
| 103 | <AddButton |
| 104 | showBlockTypes={showBlockTypes} |
| 105 | categories={categories} |
| 106 | search={search} |
| 107 | setSearch={setSearch} |
| 108 | openNameLists={openNameLists} |
| 109 | setOpenNameLists={setOpenNameLists} |
| 110 | /> |
| 111 | </section> |
| 112 | </> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | export default withSelect((select) => { |
| 117 | const { |
| 118 | getCategories, |
| 119 | getBlockTypes, |
| 120 | hasBlockSupport, |
| 121 | isMatchingSearchTerm, |
| 122 | } = select('core/blocks'); |
| 123 | |
| 124 | return { |
| 125 | blockTypes: getBlockTypes(), |
| 126 | categories: getCategories(), |
| 127 | hasBlockSupport, |
| 128 | isMatchingSearchTerm, |
| 129 | }; |
| 130 | })(AdminCustomBlockStyle); |
| 131 |