components
3 years ago
css
3 years ago
block.json
4 years ago
edit.js
3 years ago
editor.scss
3 years ago
index.js
3 years ago
save.js
4 years ago
transforms.js
4 years ago
save.js
82 lines
| 1 | import classnames from 'classnames'; |
| 2 | import Element from '../../components/element'; |
| 3 | import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | import { removeEmpty } from '../query-loop/components/utils'; |
| 6 | import AnchorTag from './components/AnchorTag'; |
| 7 | |
| 8 | export default ( { attributes } ) => { |
| 9 | const { |
| 10 | uniqueId, |
| 11 | mediaUrl, |
| 12 | anchor, |
| 13 | alt, |
| 14 | title, |
| 15 | href, |
| 16 | openInNewWindow, |
| 17 | relNoFollow, |
| 18 | relSponsored, |
| 19 | useDynamicData, |
| 20 | dynamicContentType, |
| 21 | className, |
| 22 | align, |
| 23 | } = attributes; |
| 24 | |
| 25 | if ( useDynamicData && dynamicContentType ) { |
| 26 | return <InnerBlocks.Content />; |
| 27 | } |
| 28 | |
| 29 | if ( ! mediaUrl ) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | const figureAttributes = useBlockProps.save( { |
| 34 | className: classnames( { |
| 35 | 'gb-block-image': true, |
| 36 | [ `gb-block-image-${ uniqueId }` ]: true, |
| 37 | } ), |
| 38 | } ); |
| 39 | |
| 40 | // We don't want our className appearing in the figure. |
| 41 | if ( figureAttributes?.className.includes( className ) ) { |
| 42 | figureAttributes.className = figureAttributes.className.replace( className, '' ).trim(); |
| 43 | } |
| 44 | |
| 45 | const htmlAttributes = applyFilters( |
| 46 | 'generateblocks.frontend.htmlAttributes', |
| 47 | { |
| 48 | className: classnames( { |
| 49 | 'gb-image': true, |
| 50 | [ `gb-image-${ uniqueId }` ]: true, |
| 51 | [ `${ className }` ]: undefined !== className, |
| 52 | [ `align${ align }` ]: '' !== align, |
| 53 | } ), |
| 54 | id: anchor ? anchor : null, |
| 55 | src: mediaUrl, |
| 56 | alt, |
| 57 | title, |
| 58 | }, |
| 59 | 'generateblocks/image', |
| 60 | attributes |
| 61 | ); |
| 62 | |
| 63 | const imageAttributes = removeEmpty( htmlAttributes ); |
| 64 | |
| 65 | const anchorAttributes = { |
| 66 | href, |
| 67 | openInNewWindow, |
| 68 | relNoFollow, |
| 69 | relSponsored, |
| 70 | }; |
| 71 | |
| 72 | return ( |
| 73 | <Element tagName="figure" htmlAttrs={ figureAttributes }> |
| 74 | <AnchorTag { ...anchorAttributes }> |
| 75 | <Element tagName="img" htmlAttrs={ imageAttributes } /> |
| 76 | </AnchorTag> |
| 77 | |
| 78 | <InnerBlocks.Content /> |
| 79 | </Element> |
| 80 | ); |
| 81 | }; |
| 82 |