deprecated
2 weeks ago
_swiper._scss
2 months ago
block.json
2 weeks ago
edit-multiItem.js
2 weeks ago
edit-slider.js
2 weeks ago
edit.js
1 day ago
icon.svg
2 months ago
index.js
2 months ago
index.php
2 months ago
loop-min-slides.js
2 months ago
pause-button.js
1 day ago
save.js
1 day ago
style.scss
2 months ago
view.js
1 day ago
index.js
183 lines
| 1 | /** |
| 2 | * Slider block type |
| 3 | * |
| 4 | */ |
| 5 | import { ReactComponent as Icon } from './icon.svg'; |
| 6 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 7 | import { addFilter } from '@wordpress/hooks'; |
| 8 | |
| 9 | import deprecatedHooks from './deprecated/hooks'; |
| 10 | import deprecated from './deprecated/save'; |
| 11 | import edit from './edit'; |
| 12 | import metadata from './block.json'; |
| 13 | import save from './save'; |
| 14 | |
| 15 | const { name } = metadata; |
| 16 | |
| 17 | export { metadata, name }; |
| 18 | |
| 19 | export const settings = { |
| 20 | icon: <Icon />, |
| 21 | edit, |
| 22 | save, |
| 23 | deprecated, |
| 24 | }; |
| 25 | |
| 26 | const generateHeightCss = (attributes, cssSelector = '') => { |
| 27 | const { blockId, mobile, tablet, pc, unit } = attributes; |
| 28 | let css = ''; |
| 29 | if (unit !== undefined && unit !== null) { |
| 30 | if (mobile !== undefined && mobile !== null && !isNaN(mobile)) { |
| 31 | css += `@media (max-width: 575.98px) { |
| 32 | ${cssSelector} |
| 33 | .vk_slider_${blockId} .vk_slider_item{ |
| 34 | height:${mobile}${unit}!important; |
| 35 | } |
| 36 | }`; |
| 37 | } |
| 38 | if (tablet !== undefined && tablet !== null && !isNaN(tablet)) { |
| 39 | css += `@media (min-width: 576px) and (max-width: 991.98px) { |
| 40 | ${cssSelector} |
| 41 | .vk_slider_${blockId} .vk_slider_item{ |
| 42 | height:${tablet}${unit}!important; |
| 43 | } |
| 44 | }`; |
| 45 | } |
| 46 | if (pc !== undefined && pc !== null && !isNaN(pc)) { |
| 47 | css += `@media (min-width: 992px) { |
| 48 | ${cssSelector} |
| 49 | .vk_slider_${blockId} .vk_slider_item{ |
| 50 | height:${pc}${unit}!important; |
| 51 | } |
| 52 | }`; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return css; |
| 57 | }; |
| 58 | |
| 59 | const generateZoomAnimationCss = (attributes = '') => { |
| 60 | const { |
| 61 | blockId, |
| 62 | zoomAnimation, |
| 63 | zoomInitialScale, |
| 64 | zoomFinalScale, |
| 65 | autoPlayDelay, |
| 66 | speed, |
| 67 | } = attributes; |
| 68 | |
| 69 | let css = ''; |
| 70 | |
| 71 | if (zoomAnimation) { |
| 72 | // ズーム用のセレクターは ::before 専用にして、親要素への副作用を防ぐ |
| 73 | const zoomSelector = `.vk_slider_${blockId}`; |
| 74 | |
| 75 | css += ` |
| 76 | ${zoomSelector} .vk_slider_item { |
| 77 | position: relative; |
| 78 | overflow: hidden; |
| 79 | } |
| 80 | |
| 81 | ${zoomSelector} .vk_slider_item::before { |
| 82 | content: ""; |
| 83 | position: absolute; |
| 84 | top: 0; |
| 85 | left: 0; |
| 86 | width: 100%; |
| 87 | height: 100%; |
| 88 | background-size: cover; |
| 89 | background-position: center; |
| 90 | background-image: var(--vk-slider-item-bg-image, inherit); |
| 91 | will-change: transform; |
| 92 | transform: scale(${zoomInitialScale !== undefined ? zoomInitialScale : 1}); |
| 93 | transition: transform ${(autoPlayDelay + speed + autoPlayDelay * 0.5) / 1000 || 6}s linear; |
| 94 | } |
| 95 | |
| 96 | ${zoomSelector} .vk_slider_item.swiper-slide-active::before, |
| 97 | ${zoomSelector} .vk_slider_item.swiper-slide-duplicate-active::before { |
| 98 | transform: scale(${zoomFinalScale !== undefined ? zoomFinalScale : 1.25}); |
| 99 | } |
| 100 | |
| 101 | ${zoomSelector} .vk_slider_item.swiper-slide-prev::before, |
| 102 | ${zoomSelector} .vk_slider_item.swiper-slide-next::before { |
| 103 | transform: scale(${zoomInitialScale !== undefined ? zoomInitialScale : 1}); |
| 104 | } |
| 105 | `; |
| 106 | } |
| 107 | |
| 108 | return css; |
| 109 | }; |
| 110 | |
| 111 | // Add column css for editor. |
| 112 | const vkbwithClientIdClassName = createHigherOrderComponent( |
| 113 | (BlockListBlock) => { |
| 114 | return (props) => { |
| 115 | if ('vk-blocks/slider' === props.name) { |
| 116 | const heightCss = generateHeightCss(props.attributes, ''); |
| 117 | const zoomCss = generateZoomAnimationCss(props.attributes, ''); |
| 118 | const cssTag = heightCss + zoomCss; |
| 119 | return ( |
| 120 | <> |
| 121 | <BlockListBlock {...props} /> |
| 122 | {(() => { |
| 123 | if (cssTag) { |
| 124 | return <style type="text/css">{cssTag}</style>; |
| 125 | } |
| 126 | })()} |
| 127 | </> |
| 128 | ); |
| 129 | } |
| 130 | return <BlockListBlock {...props} />; |
| 131 | }; |
| 132 | }, |
| 133 | 'vkbwithClientIdClassName' |
| 134 | ); |
| 135 | addFilter( |
| 136 | 'editor.BlockListBlock', |
| 137 | 'vk-blocks/slider', |
| 138 | vkbwithClientIdClassName |
| 139 | ); |
| 140 | |
| 141 | /** |
| 142 | * Swiperの設定をフロント側に出力するフィルター。 |
| 143 | * 0.49.8で、jSをfooterに出力するよう構造変更。CSSは継続して出力。 |
| 144 | * |
| 145 | * @param {*} el |
| 146 | * @param {*} type |
| 147 | * @param {*} attributes |
| 148 | */ |
| 149 | const addSwiperConfig = (el, type, attributes) => { |
| 150 | if ('vk-blocks/slider' === type.name) { |
| 151 | //現在実行されている deprecated� |
| 152 | の save関数のindexを取得 |
| 153 | const deprecatedFuncIndex = deprecated.findIndex( |
| 154 | (item) => item.save === type.save |
| 155 | ); |
| 156 | |
| 157 | // 最新版 |
| 158 | if (-1 === deprecatedFuncIndex) { |
| 159 | const cssSelector = `.vk_slider_${attributes.blockId},`; |
| 160 | const heightCss = generateHeightCss(attributes, cssSelector); |
| 161 | const zoomCss = generateZoomAnimationCss(attributes, cssSelector); |
| 162 | const cssTag = heightCss + zoomCss; |
| 163 | return ( |
| 164 | <> |
| 165 | {el} |
| 166 | {(() => { |
| 167 | if (cssTag) { |
| 168 | return <style type="text/css">{cssTag}</style>; |
| 169 | } |
| 170 | })()} |
| 171 | </> |
| 172 | ); |
| 173 | |
| 174 | //後方互換 |
| 175 | } |
| 176 | const DeprecatedHook = deprecatedHooks[deprecatedFuncIndex]; |
| 177 | return <DeprecatedHook el={el} attributes={attributes} />; |
| 178 | } |
| 179 | // Slider以外のブロック |
| 180 | return el; |
| 181 | }; |
| 182 | addFilter('blocks.getSaveElement', 'vk-blocks/slider', addSwiperConfig, 11); |
| 183 |