deprecated
4 months ago
block.json
4 months ago
component.js
4 months ago
edit.js
4 months ago
icon.svg
4 months ago
index.js
4 months ago
index.php
4 months ago
save.js
4 months ago
style.scss
4 months ago
transforms.js
4 months ago
index.js
202 lines
| 1 | /** |
| 2 | * Button block type |
| 3 | * |
| 4 | */ |
| 5 | import { ReactComponent as Icon } from './icon.svg'; |
| 6 | import { title, iconName, url, iconUser } from '@vkblocks/utils/example-data'; |
| 7 | import edit from './edit'; |
| 8 | import metadata from './block.json'; |
| 9 | import save from './save'; |
| 10 | import { deprecated } from './deprecated/save/'; |
| 11 | import deprecatedHooks from './deprecated/hooks'; |
| 12 | import transforms from './transforms'; |
| 13 | import { addFilter } from '@wordpress/hooks'; |
| 14 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 15 | import { isHexColor } from '@vkblocks/utils/is-hex-color'; |
| 16 | |
| 17 | const { name } = metadata; |
| 18 | |
| 19 | export { metadata, name }; |
| 20 | |
| 21 | export const settings = { |
| 22 | icon: <Icon />, |
| 23 | example: { |
| 24 | attributes: { |
| 25 | content: iconName, |
| 26 | subCaption: title, |
| 27 | buttonUrl: url, |
| 28 | buttonTarget: false, |
| 29 | buttonSize: 'md', |
| 30 | buttonType: '0', |
| 31 | buttonEffect: '', |
| 32 | buttonColor: 'primary', |
| 33 | buttonTextColorCustom: 'undefined', |
| 34 | buttonColorCustom: 'undefined', |
| 35 | buttonAlign: 'left', |
| 36 | buttonWidthMobile: 0, |
| 37 | buttonWidthTablet: 0, |
| 38 | outerGap: null, |
| 39 | buttonWidth: 0, |
| 40 | fontAwesomeIconBefore: iconUser, |
| 41 | fontAwesomeIconAfter: iconUser, |
| 42 | iconSizeBefore: null, |
| 43 | iconSizeAfter: null, |
| 44 | }, |
| 45 | }, |
| 46 | edit, |
| 47 | save, |
| 48 | deprecated, |
| 49 | transforms, |
| 50 | }; |
| 51 | |
| 52 | const generateInlineCss = (attributes) => { |
| 53 | const { buttonTextColorCustom, buttonColorCustom, buttonType, blockId } = |
| 54 | attributes; |
| 55 | let inlineCss = ''; |
| 56 | |
| 57 | // カスタムカラーの場合 |
| 58 | if (buttonColorCustom !== undefined && isHexColor(buttonColorCustom)) { |
| 59 | // 塗り |
| 60 | if (buttonType === '0' || buttonType === null) { |
| 61 | inlineCss += `.vk_button-${blockId} .has-background { |
| 62 | background-color: ${buttonColorCustom}; |
| 63 | border: 1px solid ${buttonColorCustom}; |
| 64 | }`; |
| 65 | } |
| 66 | // アウトライン |
| 67 | if (buttonType === '1') { |
| 68 | inlineCss += `.vk_button-${blockId} .has-text-color.is-style-outline { |
| 69 | background-color: transparent; |
| 70 | border: 1px solid ${buttonColorCustom}; |
| 71 | color: ${buttonColorCustom}; |
| 72 | } |
| 73 | .vk_button-${blockId} .has-text-color.is-style-outline:hover { |
| 74 | background-color: ${buttonColorCustom}; |
| 75 | border: 1px solid ${buttonColorCustom}; |
| 76 | color: #fff; |
| 77 | }`; |
| 78 | } |
| 79 | // テキストのみ |
| 80 | if (buttonType === '2') { |
| 81 | inlineCss = `.vk_button-${blockId} .has-text-color.vk_button_link-type-text { |
| 82 | color: ${buttonColorCustom}; |
| 83 | }`; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // 文字色がカスタムカラーの場合 |
| 88 | if ( |
| 89 | buttonTextColorCustom !== undefined && |
| 90 | isHexColor(buttonTextColorCustom) |
| 91 | ) { |
| 92 | if (buttonType === '0' || buttonType === null) { |
| 93 | inlineCss += ` .vk_button-${blockId} .has-text-color { |
| 94 | color: ${buttonTextColorCustom}; |
| 95 | }`; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return inlineCss; |
| 100 | }; |
| 101 | |
| 102 | const generateInlineGapCss = (attributes, isSave) => { |
| 103 | const { |
| 104 | buttonWidthMobile, |
| 105 | buttonWidthTablet, |
| 106 | buttonWidth, |
| 107 | outerGap, |
| 108 | blockId, |
| 109 | } = attributes; |
| 110 | let inlineCss = ''; |
| 111 | const propaty = isSave |
| 112 | ? '.vk_button' |
| 113 | : '.vk_buttons .vk_buttons_col .block-editor-block-list__layout .vk_button'; |
| 114 | |
| 115 | // 親ブロックのギャップを反映 |
| 116 | if (outerGap) { |
| 117 | if (buttonWidthMobile) { |
| 118 | inlineCss += `@media (max-width: 575.98px) { |
| 119 | ${propaty}.vk_button-${blockId} { |
| 120 | width: calc(${buttonWidthMobile}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidthMobile}))) - 1px); |
| 121 | } |
| 122 | }`; |
| 123 | } |
| 124 | if (buttonWidthTablet) { |
| 125 | inlineCss += `@media(min-width: 576px) and (max-width: 991.98px) { |
| 126 | ${propaty}.vk_button-${blockId} { |
| 127 | width: calc(${buttonWidthTablet}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidthTablet}))) - 1px); |
| 128 | } |
| 129 | }`; |
| 130 | } |
| 131 | if (buttonWidth) { |
| 132 | inlineCss += `@media (min-width: 992px) { |
| 133 | ${propaty}.vk_button-${blockId} { |
| 134 | width: calc(${buttonWidth}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidth}))) - 1px ); |
| 135 | } |
| 136 | }`; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return inlineCss; |
| 141 | }; |
| 142 | |
| 143 | const VKButtonInlineEditorCss = createHigherOrderComponent((BlockEdit) => { |
| 144 | return (props) => { |
| 145 | const { attributes } = props; |
| 146 | |
| 147 | if ('vk-blocks/button' === props.name) { |
| 148 | const cssTag = generateInlineCss(attributes); |
| 149 | const cssEditor = generateInlineGapCss(attributes, false); |
| 150 | if (cssTag !== '' || cssEditor !== '') { |
| 151 | return ( |
| 152 | <> |
| 153 | <BlockEdit {...props} /> |
| 154 | <style type="text/css"> |
| 155 | {cssTag} {cssEditor} |
| 156 | </style> |
| 157 | </> |
| 158 | ); |
| 159 | } |
| 160 | return <BlockEdit {...props} />; |
| 161 | } |
| 162 | return <BlockEdit {...props} />; |
| 163 | }; |
| 164 | }, 'VKButtonInlineEditorCss'); |
| 165 | addFilter('editor.BlockEdit', 'vk-blocks/button', VKButtonInlineEditorCss); |
| 166 | |
| 167 | const VKButtonInlineCss = (el, type, attributes) => { |
| 168 | if ('vk-blocks/button' === type.name) { |
| 169 | //現在実行されている deprecated� |
| 170 | の save関数のindexを取得 |
| 171 | const deprecatedFuncIndex = deprecated.findIndex( |
| 172 | (item) => item.save === type.save |
| 173 | ); |
| 174 | |
| 175 | // 最新版 |
| 176 | if (-1 === deprecatedFuncIndex) { |
| 177 | // NOTE: useBlockProps + style要素を挿� |
| 178 | �する場合、useBlockPropsを使った要素が最初(上)にこないと、 |
| 179 | // カスタムクラスを追加する処理が失敗する[ |
| 180 | const cssTag = generateInlineCss(attributes); |
| 181 | const cssEditor = generateInlineGapCss(attributes, true); |
| 182 | if (cssTag !== '' || cssEditor !== '') { |
| 183 | return ( |
| 184 | <> |
| 185 | {el} |
| 186 | <style type="text/css"> |
| 187 | {cssTag} {cssEditor} |
| 188 | </style> |
| 189 | </> |
| 190 | ); |
| 191 | } |
| 192 | return el; |
| 193 | |
| 194 | //後方互換 |
| 195 | } |
| 196 | const DeprecatedHook = deprecatedHooks[deprecatedFuncIndex]; |
| 197 | return <DeprecatedHook el={el} attributes={attributes} />; |
| 198 | } |
| 199 | return el; |
| 200 | }; |
| 201 | addFilter('blocks.getSaveElement', 'vk-blocks/button', VKButtonInlineCss, 11); |
| 202 |