category.js
180 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 BlockStyleChecklist from './checklist'; |
| 17 | import { AdminContext } from '@vkblocks/admin/index'; |
| 18 | |
| 19 | export default function BlockStyleManagerCategory({ |
| 20 | blockName, |
| 21 | blockTitle, |
| 22 | blockStyleTypes, |
| 23 | }) { |
| 24 | const instanceId = useInstanceId(BlockStyleManagerCategory); |
| 25 | const { vkBlocksOption, setVkBlocksOption } = useContext(AdminContext); |
| 26 | const defaultAllowedBlockStyleTypes = true; |
| 27 | const hiddenBlockTypes = vkBlocksOption.disable_block_style_lists; |
| 28 | const filteredBlockStyleTypes = useMemo(() => { |
| 29 | if (defaultAllowedBlockStyleTypes === true) { |
| 30 | return blockStyleTypes; |
| 31 | } |
| 32 | return blockStyleTypes.filter(({ name }) => { |
| 33 | const hiddenBlockStyleLists = |
| 34 | vkBlocksOption.disable_block_style_lists.find( |
| 35 | (item) => item.block_name === blockName |
| 36 | ); |
| 37 | return !hiddenBlockStyleLists.includes(name); |
| 38 | }); |
| 39 | }, [defaultAllowedBlockStyleTypes, blockStyleTypes]); |
| 40 | |
| 41 | const showBlockStyles = (blockStyles) => { |
| 42 | const targetBlockStyles = vkBlocksOption.disable_block_style_lists.find( |
| 43 | (item) => item.block_name === blockName |
| 44 | ); |
| 45 | const existingBlockStyleNames = targetBlockStyles?.property_name ?? []; |
| 46 | const mergedBlockNames = existingBlockStyleNames.filter( |
| 47 | (type) => |
| 48 | !( |
| 49 | Array.isArray(blockStyles) ? blockStyles : [blockStyles] |
| 50 | ).includes(type) |
| 51 | ); |
| 52 | |
| 53 | // 対象のオプション値がない時 |
| 54 | if ( |
| 55 | existingBlockStyleNames.length === 0 && |
| 56 | targetBlockStyles === undefined |
| 57 | ) { |
| 58 | vkBlocksOption.disable_block_style_lists.push({ |
| 59 | block_name: blockName, |
| 60 | property_name: [...mergedBlockNames], |
| 61 | }); |
| 62 | } |
| 63 | // vkBlocksOptionのproperty_nameを上書きする |
| 64 | vkBlocksOption.disable_block_style_lists.forEach((item) => { |
| 65 | if (item.block_name === blockName) { |
| 66 | item.property_name = [...mergedBlockNames]; |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | setVkBlocksOption({ ...vkBlocksOption }); |
| 71 | }; |
| 72 | |
| 73 | const hideBlockStyles = (blockStyles) => { |
| 74 | const targetBlockStyles = vkBlocksOption.disable_block_style_lists.find( |
| 75 | (item) => item.block_name === blockName |
| 76 | ); |
| 77 | const existingBlockStyleNames = targetBlockStyles?.property_name ?? []; |
| 78 | const mergedBlockNames = new Set([ |
| 79 | ...existingBlockStyleNames, |
| 80 | ...(Array.isArray(blockStyles) ? blockStyles : [blockStyles]), |
| 81 | ]); |
| 82 | |
| 83 | // 対象のオプション値がない時 |
| 84 | if ( |
| 85 | existingBlockStyleNames.length === 0 && |
| 86 | targetBlockStyles === undefined |
| 87 | ) { |
| 88 | vkBlocksOption.disable_block_style_lists.push({ |
| 89 | block_name: blockName, |
| 90 | property_name: [...mergedBlockNames], |
| 91 | }); |
| 92 | } |
| 93 | // vkBlocksOptionのproperty_nameを上書きする |
| 94 | vkBlocksOption.disable_block_style_lists.forEach((item) => { |
| 95 | if (item.block_name === blockName) { |
| 96 | item.property_name = [...mergedBlockNames]; |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | setVkBlocksOption({ ...vkBlocksOption }); |
| 101 | }; |
| 102 | |
| 103 | const toggleVisible = useCallback( |
| 104 | (blockStyleName, nextIsChecked) => { |
| 105 | if (nextIsChecked) { |
| 106 | showBlockStyles(blockStyleName); |
| 107 | } else { |
| 108 | hideBlockStyles(blockStyleName); |
| 109 | } |
| 110 | }, |
| 111 | [showBlockStyles, hideBlockStyles] |
| 112 | ); |
| 113 | const toggleAllVisible = useCallback( |
| 114 | (nextIsChecked) => { |
| 115 | const blockStyles = blockStyleTypes.map( |
| 116 | (blockType) => blockType.name |
| 117 | ); |
| 118 | if (nextIsChecked) { |
| 119 | showBlockStyles(blockStyles); |
| 120 | } else { |
| 121 | hideBlockStyles(blockStyles); |
| 122 | } |
| 123 | }, |
| 124 | [blockStyleTypes, showBlockStyles, hideBlockStyles] |
| 125 | ); |
| 126 | |
| 127 | if (!filteredBlockStyleTypes.length) { |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | // checkするブロック名� |
| 132 | �列を作る |
| 133 | const hiddenBlockStyleLists = hiddenBlockTypes.find( |
| 134 | (item) => item.block_name === blockName |
| 135 | ); |
| 136 | const checkedBlockStyles = filteredBlockStyleTypes |
| 137 | .map((blockType) => blockType.name) |
| 138 | .filter((type) => !hiddenBlockStyleLists?.property_name.includes(type)); |
| 139 | |
| 140 | const titleId = 'block-manager__category-title-' + instanceId; |
| 141 | |
| 142 | const isAllChecked = |
| 143 | checkedBlockStyles.length === filteredBlockStyleTypes.length; |
| 144 | |
| 145 | let ariaChecked; |
| 146 | if (isAllChecked) { |
| 147 | ariaChecked = 'true'; |
| 148 | } else if (checkedBlockStyles.length > 0) { |
| 149 | ariaChecked = 'mixed'; |
| 150 | } else { |
| 151 | ariaChecked = 'false'; |
| 152 | } |
| 153 | |
| 154 | return ( |
| 155 | <div |
| 156 | role="group" |
| 157 | aria-labelledby={titleId} |
| 158 | className={classnames( |
| 159 | 'block-manager__category', |
| 160 | 'blockManagerList' |
| 161 | )} |
| 162 | > |
| 163 | <CheckboxControl |
| 164 | __nextHasNoMarginBottom |
| 165 | checked={isAllChecked} |
| 166 | onChange={toggleAllVisible} |
| 167 | className="block-manager__category-title" |
| 168 | aria-checked={ariaChecked} |
| 169 | label={<span id={titleId}>{blockTitle}</span>} |
| 170 | /> |
| 171 | <BlockStyleChecklist |
| 172 | blockStyleTypes={filteredBlockStyleTypes} |
| 173 | value={checkedBlockStyles} |
| 174 | onItemChange={toggleVisible} |
| 175 | blockName={blockName} |
| 176 | /> |
| 177 | </div> |
| 178 | ); |
| 179 | } |
| 180 |