style.js
166 lines
| 1 | /** |
| 2 | * cover-style block type |
| 3 | * |
| 4 | * @package |
| 5 | */ |
| 6 | import { addFilter } from '@wordpress/hooks'; |
| 7 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 8 | import { BlockControls } from '@wordpress/block-editor'; |
| 9 | import { ToolbarGroup } from '@wordpress/components'; |
| 10 | import { Fragment } from '@wordpress/element'; |
| 11 | import LinkToolbar from '@vkblocks/components/link-toolbar'; |
| 12 | import { __ } from '@wordpress/i18n'; |
| 13 | |
| 14 | // コアの Cover ブロックかどうかを判定 |
| 15 | const isCoverBlock = (name) => name === 'core/cover'; |
| 16 | |
| 17 | // Cover ブロックを拡張する HOC |
| 18 | const enhanceCoverBlock = createHigherOrderComponent((BlockEdit) => { |
| 19 | return (props) => { |
| 20 | if (!isCoverBlock(props.name)) { |
| 21 | return <BlockEdit {...props} />; |
| 22 | } |
| 23 | |
| 24 | const { |
| 25 | attributes: { |
| 26 | linkUrl, |
| 27 | linkTarget, |
| 28 | relAttribute, |
| 29 | linkDescription, |
| 30 | linkToPost, |
| 31 | }, |
| 32 | setAttributes, |
| 33 | context, |
| 34 | } = props; |
| 35 | |
| 36 | const isDescendentOfQueryLoop = |
| 37 | typeof context?.queryId === 'number' && |
| 38 | Number.isFinite(context?.queryId); |
| 39 | |
| 40 | return ( |
| 41 | <Fragment> |
| 42 | <BlockEdit {...props} /> |
| 43 | <BlockControls> |
| 44 | <ToolbarGroup> |
| 45 | <LinkToolbar |
| 46 | linkUrl={linkUrl} |
| 47 | setLinkUrl={(url) => |
| 48 | setAttributes({ linkUrl: url }) |
| 49 | } |
| 50 | linkTarget={linkTarget} |
| 51 | setLinkTarget={(target) => |
| 52 | setAttributes({ linkTarget: target }) |
| 53 | } |
| 54 | relAttribute={relAttribute} |
| 55 | setRelAttribute={(rel) => |
| 56 | setAttributes({ relAttribute: rel }) |
| 57 | } |
| 58 | linkDescription={linkDescription} |
| 59 | setLinkDescription={(description) => |
| 60 | setAttributes({ linkDescription: description }) |
| 61 | } |
| 62 | isDescendentOfQueryLoop={isDescendentOfQueryLoop} |
| 63 | linkToPost={linkToPost} |
| 64 | setLinkToPost={(checked) => |
| 65 | setAttributes({ linkToPost: !!checked }) |
| 66 | } |
| 67 | /> |
| 68 | </ToolbarGroup> |
| 69 | </BlockControls> |
| 70 | </Fragment> |
| 71 | ); |
| 72 | }; |
| 73 | }, 'enhanceCoverBlock'); |
| 74 | |
| 75 | const extendCoverBlock = (settings, name) => { |
| 76 | if (!isCoverBlock(name)) { |
| 77 | return settings; |
| 78 | } |
| 79 | |
| 80 | const save = (props) => { |
| 81 | const { attributes } = props; |
| 82 | const { |
| 83 | linkUrl, |
| 84 | linkTarget, |
| 85 | relAttribute, |
| 86 | linkDescription, |
| 87 | linkToPost, |
| 88 | } = attributes; |
| 89 | |
| 90 | // � |
| 91 | �のブロックの `save` を取得 |
| 92 | const saveElement = settings.save(props); |
| 93 | |
| 94 | const hasLink = linkUrl || linkToPost; |
| 95 | const effectiveUrl = linkToPost ? '' : linkUrl; |
| 96 | |
| 97 | if (!saveElement || !hasLink) { |
| 98 | return saveElement; |
| 99 | } |
| 100 | |
| 101 | const existingClassName = saveElement.props.className || ''; |
| 102 | const classNameWithLink = `${existingClassName} has-link`.trim(); |
| 103 | const existingStyle = saveElement.props.style || {}; |
| 104 | |
| 105 | return ( |
| 106 | <div className={classNameWithLink} style={existingStyle}> |
| 107 | {saveElement.props.children} |
| 108 | <a |
| 109 | href={effectiveUrl} |
| 110 | {...(linkToPost ? { 'data-vk-link-to-post': '1' } : {})} |
| 111 | {...(linkTarget ? { target: linkTarget } : {})} |
| 112 | {...(relAttribute ? { rel: relAttribute } : {})} |
| 113 | className="wp-block-cover-vk-link" |
| 114 | > |
| 115 | <span className="screen-reader-text"> |
| 116 | {linkDescription |
| 117 | ? linkDescription |
| 118 | : __('Cover link', 'vk-blocks')} |
| 119 | </span> |
| 120 | </a> |
| 121 | </div> |
| 122 | ); |
| 123 | }; |
| 124 | |
| 125 | return { |
| 126 | ...settings, |
| 127 | attributes: { |
| 128 | ...settings.attributes, |
| 129 | linkUrl: { |
| 130 | type: 'string', |
| 131 | default: '', |
| 132 | }, |
| 133 | linkTarget: { |
| 134 | type: 'string', |
| 135 | default: '', |
| 136 | }, |
| 137 | relAttribute: { |
| 138 | type: 'string', |
| 139 | default: '', |
| 140 | }, |
| 141 | linkDescription: { |
| 142 | type: 'string', |
| 143 | default: '', |
| 144 | }, |
| 145 | linkToPost: { |
| 146 | type: 'boolean', |
| 147 | default: false, |
| 148 | }, |
| 149 | }, |
| 150 | usesContext: [ |
| 151 | ...(settings.usesContext || []), |
| 152 | 'postId', |
| 153 | 'postType', |
| 154 | 'queryId', |
| 155 | ], |
| 156 | edit: enhanceCoverBlock(settings.edit), |
| 157 | save, // 修正した `save` を適用 |
| 158 | }; |
| 159 | }; |
| 160 | |
| 161 | addFilter( |
| 162 | 'blocks.registerBlockType', |
| 163 | 'custom/extend-cover-block', |
| 164 | extendCoverBlock |
| 165 | ); |
| 166 |