style.js
214 lines
| 1 | /** |
| 2 | * columns-style block type |
| 3 | * |
| 4 | */ |
| 5 | import { __ } from '@wordpress/i18n'; |
| 6 | import { addFilter } from '@wordpress/hooks'; |
| 7 | import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components'; |
| 8 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 9 | import { InspectorControls, BlockControls } from '@wordpress/block-editor'; |
| 10 | import classnames from 'classnames'; |
| 11 | import LinkToolbar from '@vkblocks/components/link-toolbar'; |
| 12 | |
| 13 | /** |
| 14 | * Internal dependencies |
| 15 | */ |
| 16 | import { VkPanelIcon } from '@vkblocks/components/vk-icon'; |
| 17 | |
| 18 | const isColumnsBlock = (name) => name === 'core/columns'; |
| 19 | const isColumnBlock = (name) => name === 'core/column'; |
| 20 | |
| 21 | export const enhanceColumnBlock = createHigherOrderComponent((BlockEdit) => { |
| 22 | return (props) => { |
| 23 | const { attributes, setAttributes } = props; |
| 24 | const { |
| 25 | reverse, |
| 26 | className, |
| 27 | linkUrl, |
| 28 | linkTarget, |
| 29 | relAttribute, |
| 30 | linkDescription, |
| 31 | } = attributes; |
| 32 | |
| 33 | if (isColumnsBlock(props.name) && props.isSelected) { |
| 34 | // カラムの方向設定 |
| 35 | return ( |
| 36 | <> |
| 37 | <BlockEdit {...props} /> |
| 38 | <InspectorControls> |
| 39 | <PanelBody |
| 40 | title={__('Column Direction', 'vk-blocks')} |
| 41 | icon={<VkPanelIcon isActive={reverse} />} |
| 42 | initialOpen={false} |
| 43 | > |
| 44 | <ToggleControl |
| 45 | label={__('Reverse', 'vk-blocks')} |
| 46 | checked={reverse} |
| 47 | onChange={(checked) => { |
| 48 | // 既存のクラス名 |
| 49 | const existClass = className |
| 50 | ? className.split(' ') |
| 51 | : []; |
| 52 | let newClassNameArray = []; |
| 53 | if (existClass) { |
| 54 | // いったん削除 |
| 55 | newClassNameArray = existClass.filter( |
| 56 | (item) => { |
| 57 | return !item.match( |
| 58 | /is-vk-row-reverse/ |
| 59 | ); |
| 60 | } |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | // reverse クラスを付与 |
| 65 | const rereverseClass = [ |
| 66 | { |
| 67 | 'is-vk-row-reverse': checked, |
| 68 | }, |
| 69 | ]; |
| 70 | newClassNameArray = classnames( |
| 71 | newClassNameArray, |
| 72 | rereverseClass |
| 73 | ); |
| 74 | |
| 75 | setAttributes({ |
| 76 | className: newClassNameArray, |
| 77 | reverse: checked, |
| 78 | }); |
| 79 | }} |
| 80 | /> |
| 81 | </PanelBody> |
| 82 | </InspectorControls> |
| 83 | </> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | if (isColumnBlock(props.name) && props.isSelected) { |
| 88 | // カラムのリンク設定(URL指定のみ。投稿へのリンクは非対応) |
| 89 | return ( |
| 90 | <> |
| 91 | <BlockEdit {...props} /> |
| 92 | <BlockControls> |
| 93 | <ToolbarGroup> |
| 94 | <LinkToolbar |
| 95 | linkUrl={linkUrl} |
| 96 | setLinkUrl={(url) => |
| 97 | setAttributes({ linkUrl: url }) |
| 98 | } |
| 99 | linkTarget={linkTarget} |
| 100 | setLinkTarget={(target) => |
| 101 | setAttributes({ linkTarget: target }) |
| 102 | } |
| 103 | relAttribute={relAttribute} |
| 104 | setRelAttribute={(rel) => |
| 105 | setAttributes({ relAttribute: rel }) |
| 106 | } |
| 107 | linkDescription={linkDescription} |
| 108 | setLinkDescription={(description) => |
| 109 | setAttributes({ |
| 110 | linkDescription: description, |
| 111 | }) |
| 112 | } |
| 113 | /> |
| 114 | </ToolbarGroup> |
| 115 | </BlockControls> |
| 116 | </> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return <BlockEdit {...props} />; |
| 121 | }; |
| 122 | }, 'addMyCustomBlockControls'); |
| 123 | |
| 124 | const extendColumnBlock = (settings, name) => { |
| 125 | if (!isColumnBlock(name) && !isColumnsBlock(name)) { |
| 126 | return settings; |
| 127 | } |
| 128 | |
| 129 | if (isColumnsBlock(settings.name)) { |
| 130 | settings.attributes = { |
| 131 | ...settings.attributes, |
| 132 | reverse: { |
| 133 | type: 'boolean', |
| 134 | default: false, |
| 135 | }, |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | if (isColumnBlock(settings.name)) { |
| 140 | settings.attributes = { |
| 141 | ...settings.attributes, |
| 142 | linkUrl: { |
| 143 | type: 'string', |
| 144 | default: '', |
| 145 | }, |
| 146 | linkTarget: { |
| 147 | type: 'string', |
| 148 | default: '', |
| 149 | }, |
| 150 | relAttribute: { |
| 151 | type: 'string', |
| 152 | default: '', |
| 153 | }, |
| 154 | linkDescription: { |
| 155 | type: 'string', |
| 156 | default: '', |
| 157 | }, |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | return { |
| 162 | ...settings, |
| 163 | attributes: { |
| 164 | ...settings.attributes, |
| 165 | }, |
| 166 | edit: enhanceColumnBlock(settings.edit), |
| 167 | save: |
| 168 | isColumnBlock(name) && settings.save |
| 169 | ? (props) => { |
| 170 | const { attributes } = props; |
| 171 | const { |
| 172 | linkUrl: colLinkUrl, |
| 173 | linkTarget: colLinkTarget, |
| 174 | relAttribute: colRelAttribute, |
| 175 | linkDescription: colLinkDescription, |
| 176 | } = attributes; |
| 177 | const saveElement = settings.save(props); |
| 178 | |
| 179 | if (!colLinkUrl) { |
| 180 | return saveElement; |
| 181 | } |
| 182 | |
| 183 | return ( |
| 184 | <div {...saveElement.props}> |
| 185 | <a |
| 186 | href={colLinkUrl} |
| 187 | {...(colLinkTarget |
| 188 | ? { target: colLinkTarget } |
| 189 | : {})} |
| 190 | {...(colRelAttribute |
| 191 | ? { rel: colRelAttribute } |
| 192 | : {})} |
| 193 | className="wp-block-column-vk-link" |
| 194 | > |
| 195 | <span className="screen-reader-text"> |
| 196 | {colLinkDescription |
| 197 | ? colLinkDescription |
| 198 | : __('Column link', 'vk-blocks')} |
| 199 | </span> |
| 200 | </a> |
| 201 | {saveElement.props.children} |
| 202 | </div> |
| 203 | ); |
| 204 | } |
| 205 | : settings.save, |
| 206 | }; |
| 207 | }; |
| 208 | |
| 209 | addFilter( |
| 210 | 'blocks.registerBlockType', |
| 211 | 'custom/extend-column-block', |
| 212 | extendColumnBlock |
| 213 | ); |
| 214 |