category.js
126 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import classnames from 'classnames'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress dependencies |
| 8 | */ |
| 9 | import { CheckboxControl } from '@wordpress/components'; |
| 10 | import { useMemo, useContext, useCallback } from '@wordpress/element'; |
| 11 | import { useInstanceId } from '@wordpress/compose'; |
| 12 | |
| 13 | /** |
| 14 | * Internal dependencies |
| 15 | */ |
| 16 | import BlockTypesChecklist from './checklist'; |
| 17 | import { AdminContext } from '@vkblocks/admin/index'; |
| 18 | |
| 19 | function BlockManagerCategory({ title, blockTypes }) { |
| 20 | const instanceId = useInstanceId(BlockManagerCategory); |
| 21 | const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext); |
| 22 | const defaultAllowedBlockTypes = true; |
| 23 | const hiddenBlockTypes = vkBlocksOption.disable_block_lists; |
| 24 | const filteredBlockTypes = useMemo(() => { |
| 25 | if (defaultAllowedBlockTypes === true) { |
| 26 | return blockTypes; |
| 27 | } |
| 28 | return blockTypes.filter(({ name }) => { |
| 29 | return !vkBlocksOption.disable_block_lists.includes(name); |
| 30 | }); |
| 31 | }, [defaultAllowedBlockTypes, blockTypes]); |
| 32 | |
| 33 | const showBlockTypes = (blockNames) => { |
| 34 | const existingBlockNames = vkBlocksOption.disable_block_lists ?? []; |
| 35 | const newBlockNames = existingBlockNames.filter( |
| 36 | (type) => |
| 37 | !( |
| 38 | Array.isArray(blockNames) ? blockNames : [blockNames] |
| 39 | ).includes(type) |
| 40 | ); |
| 41 | vkBlocksOption.disable_block_lists = newBlockNames; |
| 42 | setVkBlocksOption({ ...vkBlocksOption }); |
| 43 | }; |
| 44 | |
| 45 | const hideBlockTypes = (blockNames) => { |
| 46 | const existingBlockNames = vkBlocksOption.disable_block_lists ?? []; |
| 47 | const mergedBlockNames = new Set([ |
| 48 | ...existingBlockNames, |
| 49 | ...(Array.isArray(blockNames) ? blockNames : [blockNames]), |
| 50 | ]); |
| 51 | vkBlocksOption.disable_block_lists = [...mergedBlockNames]; |
| 52 | setVkBlocksOption({ ...vkBlocksOption }); |
| 53 | }; |
| 54 | |
| 55 | const toggleVisible = useCallback( |
| 56 | (blockName, nextIsChecked) => { |
| 57 | if (nextIsChecked) { |
| 58 | showBlockTypes(blockName); |
| 59 | } else { |
| 60 | hideBlockTypes(blockName); |
| 61 | } |
| 62 | }, |
| 63 | [showBlockTypes, hideBlockTypes] |
| 64 | ); |
| 65 | const toggleAllVisible = useCallback( |
| 66 | (nextIsChecked) => { |
| 67 | const blockNames = blockTypes.map((blockType) => blockType.name); |
| 68 | if (nextIsChecked) { |
| 69 | showBlockTypes(blockNames); |
| 70 | } else { |
| 71 | hideBlockTypes(blockNames); |
| 72 | } |
| 73 | }, |
| 74 | [blockTypes, showBlockTypes, hideBlockTypes] |
| 75 | ); |
| 76 | |
| 77 | if (!filteredBlockTypes.length) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | // checkするブロック名� |
| 82 | �列を作る |
| 83 | const checkedBlockNames = filteredBlockTypes |
| 84 | .map((blockType) => blockType.name) |
| 85 | .filter((type) => !hiddenBlockTypes.includes(type)); |
| 86 | |
| 87 | const titleId = 'block-manager__category-title-' + instanceId; |
| 88 | |
| 89 | const isAllChecked = checkedBlockNames.length === filteredBlockTypes.length; |
| 90 | |
| 91 | let ariaChecked; |
| 92 | if (isAllChecked) { |
| 93 | ariaChecked = 'true'; |
| 94 | } else if (checkedBlockNames.length > 0) { |
| 95 | ariaChecked = 'mixed'; |
| 96 | } else { |
| 97 | ariaChecked = 'false'; |
| 98 | } |
| 99 | |
| 100 | return ( |
| 101 | <div |
| 102 | role="group" |
| 103 | aria-labelledby={titleId} |
| 104 | className={classnames( |
| 105 | 'block-manager__category', |
| 106 | 'blockManagerList' |
| 107 | )} |
| 108 | > |
| 109 | <CheckboxControl |
| 110 | __nextHasNoMarginBottom |
| 111 | checked={isAllChecked} |
| 112 | onChange={toggleAllVisible} |
| 113 | className="block-manager__category-title" |
| 114 | aria-checked={ariaChecked} |
| 115 | label={<span id={titleId}>{title}</span>} |
| 116 | /> |
| 117 | <BlockTypesChecklist |
| 118 | blockTypes={filteredBlockTypes} |
| 119 | value={checkedBlockNames} |
| 120 | onItemChange={toggleVisible} |
| 121 | /> |
| 122 | </div> |
| 123 | ); |
| 124 | } |
| 125 | export default BlockManagerCategory; |
| 126 |