index.js
170 lines
| 1 | /** |
| 2 | * Content width half extension |
| 3 | */ |
| 4 | import { useEffect } from 'react'; |
| 5 | import { __ } from '@wordpress/i18n'; |
| 6 | import { addFilter } from '@wordpress/hooks'; |
| 7 | import { PanelBody, ToggleControl } from '@wordpress/components'; |
| 8 | import { InspectorControls } from '@wordpress/block-editor'; |
| 9 | import { useSelect } from '@wordpress/data'; |
| 10 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 11 | import classnames from 'classnames'; |
| 12 | |
| 13 | /** |
| 14 | * Internal dependencies |
| 15 | */ |
| 16 | import { VkPanelIcon } from '@vkblocks/components/vk-icon'; |
| 17 | |
| 18 | const TARGET_BLOCKS = ['core/column']; |
| 19 | const HALF_CLASS = 'is-vk-content-width-half'; |
| 20 | |
| 21 | const addAttribute = (settings) => { |
| 22 | if (!TARGET_BLOCKS.includes(settings.name)) { |
| 23 | return settings; |
| 24 | } |
| 25 | |
| 26 | return { |
| 27 | ...settings, |
| 28 | attributes: { |
| 29 | ...settings.attributes, |
| 30 | contentWidthHalf: { |
| 31 | type: 'boolean', |
| 32 | default: false, |
| 33 | }, |
| 34 | }, |
| 35 | }; |
| 36 | }; |
| 37 | |
| 38 | addFilter( |
| 39 | 'blocks.registerBlockType', |
| 40 | 'vk-blocks/content-width-half/add-attribute', |
| 41 | addAttribute |
| 42 | ); |
| 43 | |
| 44 | const withContentWidthHalfControls = createHigherOrderComponent((BlockEdit) => { |
| 45 | return (props) => { |
| 46 | const { name, attributes, setAttributes, isSelected, clientId } = props; |
| 47 | |
| 48 | if (!TARGET_BLOCKS.includes(name)) { |
| 49 | return <BlockEdit {...props} />; |
| 50 | } |
| 51 | |
| 52 | const { className, contentWidthHalf } = attributes; |
| 53 | // PHP から渡された vk_blocks_params.has_theme_json(テーマに theme.json があるかどうか)を、安� |
| 54 | �に参� |
| 55 | �したうえで、結果を� |
| 56 | ず真偽値として変数 hasThemeJson に� |
| 57 | �れる |
| 58 | const hasThemeJson = !!window?.vk_blocks_params?.has_theme_json; |
| 59 | const isTwoColumnLayout = useSelect( |
| 60 | (select) => { |
| 61 | const { getBlockRootClientId, getBlock } = |
| 62 | select('core/block-editor'); |
| 63 | const parentClientId = getBlockRootClientId(clientId); |
| 64 | |
| 65 | if (!parentClientId) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | const parentBlock = getBlock(parentClientId); |
| 70 | |
| 71 | if (!parentBlock || parentBlock.name !== 'core/columns') { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | const columnBlocks = |
| 76 | parentBlock.innerBlocks?.filter( |
| 77 | (block) => block.name === 'core/column' |
| 78 | ) ?? []; |
| 79 | |
| 80 | return columnBlocks.length === 2; |
| 81 | }, |
| 82 | [clientId] |
| 83 | ); |
| 84 | |
| 85 | useEffect(() => { |
| 86 | const classList = |
| 87 | typeof className === 'string' |
| 88 | ? className.split(' ').filter((item) => item) |
| 89 | : []; |
| 90 | const hasHalfClass = classList.includes(HALF_CLASS); |
| 91 | |
| 92 | if (hasHalfClass !== !!contentWidthHalf) { |
| 93 | setAttributes({ contentWidthHalf: hasHalfClass }); |
| 94 | } |
| 95 | }, [className, contentWidthHalf, setAttributes]); |
| 96 | |
| 97 | useEffect(() => { |
| 98 | const classList = |
| 99 | typeof className === 'string' |
| 100 | ? className.split(' ').filter((item) => item) |
| 101 | : []; |
| 102 | const hasHalfClass = classList.includes(HALF_CLASS); |
| 103 | |
| 104 | if (!isTwoColumnLayout && (contentWidthHalf || hasHalfClass)) { |
| 105 | const updatedClassName = |
| 106 | classnames( |
| 107 | classList.filter((item) => item !== HALF_CLASS) |
| 108 | ) || undefined; |
| 109 | |
| 110 | setAttributes({ |
| 111 | className: updatedClassName, |
| 112 | contentWidthHalf: false, |
| 113 | }); |
| 114 | } |
| 115 | }, [className, contentWidthHalf, isTwoColumnLayout, setAttributes]); |
| 116 | |
| 117 | const toggleContentWidthHalf = (checked) => { |
| 118 | const baseClassList = |
| 119 | typeof className === 'string' |
| 120 | ? className.split(' ').filter((item) => item) |
| 121 | : []; |
| 122 | const trimmedClassList = baseClassList.filter( |
| 123 | (item) => item !== HALF_CLASS |
| 124 | ); |
| 125 | const updatedClassName = |
| 126 | classnames(trimmedClassList, { |
| 127 | [HALF_CLASS]: checked, |
| 128 | }) || undefined; |
| 129 | |
| 130 | setAttributes({ |
| 131 | className: updatedClassName, |
| 132 | contentWidthHalf: checked, |
| 133 | }); |
| 134 | }; |
| 135 | |
| 136 | const shouldShowPanel = isSelected && hasThemeJson && isTwoColumnLayout; |
| 137 | |
| 138 | return ( |
| 139 | <> |
| 140 | <BlockEdit {...props} /> |
| 141 | {shouldShowPanel && ( |
| 142 | <InspectorControls> |
| 143 | <PanelBody |
| 144 | title={__('Layout Extension', 'vk-blocks')} |
| 145 | icon={<VkPanelIcon isActive={!!contentWidthHalf} />} |
| 146 | initialOpen={false} |
| 147 | > |
| 148 | <ToggleControl |
| 149 | label={__('Half Content Width', 'vk-blocks')} |
| 150 | help={__( |
| 151 | 'When the screen size is 782px or more, the maximum width of the inner block is half of the content width. Below 782px, the horizontal padding is removed.', |
| 152 | 'vk-blocks' |
| 153 | )} |
| 154 | checked={!!contentWidthHalf} |
| 155 | onChange={toggleContentWidthHalf} |
| 156 | /> |
| 157 | </PanelBody> |
| 158 | </InspectorControls> |
| 159 | )} |
| 160 | </> |
| 161 | ); |
| 162 | }; |
| 163 | }, 'withContentWidthHalfControls'); |
| 164 | |
| 165 | addFilter( |
| 166 | 'editor.BlockEdit', |
| 167 | 'vk-blocks/content-width-half/with-controls', |
| 168 | withContentWidthHalfControls |
| 169 | ); |
| 170 |