PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.3
GenerateBlocks v1.5.3
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / image / components / ImageContentRenderer.js
generateblocks / src / blocks / image / components Last commit date
inspector-controls 4 years ago AlignmentControls.js 4 years ago AnchorTag.js 4 years ago BlockControls.js 4 years ago ComponentCSS.js 4 years ago Image.js 4 years ago ImageContentRenderer.js 4 years ago ImagePlaceholder.js 4 years ago InspectorControls.js 4 years ago
ImageContentRenderer.js
147 lines
1 import ImagePlaceholder from './ImagePlaceholder';
2 import { useBlockProps } from '@wordpress/block-editor';
3 import classnames from 'classnames';
4 import RootElement from '../../../components/root-element';
5 import Element from '../../../components/element';
6 import Image from './Image';
7 import BlockControls from './BlockControls';
8 import { useRef, useState, useMemo } from '@wordpress/element';
9 import { useSelect } from '@wordpress/data';
10 import getDynamicImage from '../../../utils/get-dynamic-image';
11 import getMediaUrl from '../../../utils/get-media-url';
12 import { applyFilters } from '@wordpress/hooks';
13
14 export default function ImageContentRenderer( props ) {
15 const {
16 attributes,
17 setAttributes,
18 name,
19 clientId,
20 deviceType,
21 temporaryURL,
22 } = props;
23
24 const {
25 uniqueId,
26 useDynamicData,
27 dynamicContentType,
28 href,
29 openInNewWindow,
30 relNoFollow,
31 relSponsored,
32 sizeSlug,
33 className,
34 align,
35 } = attributes;
36
37 const imageRef = useRef();
38 const figureRef = useRef();
39 const [
40 { loadedNaturalWidth, loadedNaturalHeight },
41 setLoadedNaturalSize,
42 ] = useState( {} );
43
44 // Get naturalWidth and naturalHeight from image ref, and fall back to loaded natural
45 // width and height. This resolves an issue in Safari where the loaded natural
46 // witdth and height is otherwise lost when switching between alignments.
47 // See: https://github.com/WordPress/gutenberg/pull/37210.
48 const { naturalWidth, naturalHeight } = useMemo( () => {
49 return {
50 naturalWidth:
51 imageRef.current?.naturalWidth ||
52 loadedNaturalWidth ||
53 undefined,
54 naturalHeight:
55 imageRef.current?.naturalHeight ||
56 loadedNaturalHeight ||
57 undefined,
58 };
59 }, [
60 loadedNaturalWidth,
61 loadedNaturalHeight,
62 imageRef.current?.complete,
63 ] );
64
65 const {
66 getBlockRootClientId,
67 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
68
69 const parentBlock = getBlockRootClientId( clientId );
70 const currentImage = getDynamicImage( props );
71 const dynamicImageUrl = getMediaUrl( currentImage, sizeSlug );
72
73 const dynamicImageFallback = applyFilters(
74 'generateblocks.editor.dynamicImageFallback',
75 dynamicImageUrl,
76 props
77 );
78
79 const imageUrl = useDynamicData && dynamicContentType ? dynamicImageFallback : attributes.mediaUrl;
80 const altText = useDynamicData && dynamicContentType ? currentImage?.alt_text : attributes.alt;
81 const titleText = useDynamicData && dynamicContentType ? currentImage?.title?.rendered : attributes.title;
82
83 const figureAttributes = useBlockProps( {
84 className: classnames( {
85 'gb-block-image': true,
86 [ `gb-block-image-${ uniqueId }` ]: true,
87 'is-applying': !! temporaryURL,
88 } ),
89 'data-align': !! parentBlock ? align : null,
90 ref: figureRef,
91 } );
92
93 // We don't want our className appearing in the figure.
94 if ( figureAttributes.className.includes( className ) ) {
95 figureAttributes.className = figureAttributes.className.replace( className, '' ).trim();
96 }
97
98 const canUploadImage =
99 ! useDynamicData ||
100 (
101 useDynamicData &&
102 ! dynamicContentType
103 );
104
105 const anchorAttributes = {
106 href,
107 openInNewWindow,
108 relNoFollow,
109 relSponsored,
110 disabled: true,
111 };
112
113 const imageProps = {
114 src: imageUrl,
115 alt: altText,
116 title: titleText,
117 setAttributes,
118 anchorAttributes,
119 imageRef,
120 setLoadedNaturalSize,
121 naturalWidth,
122 naturalHeight,
123 attributes,
124 temporaryURL,
125 };
126
127 return (
128 <>
129 <BlockControls
130 { ...props }
131 imageUrl={ imageUrl }
132 canUploadImage={ canUploadImage }
133 deviceType={ deviceType }
134 />
135
136 <RootElement name={ name } clientId={ clientId } align={ align }>
137 <Element tagName="figure" htmlAttrs={ figureAttributes }>
138 { ( !! temporaryURL || !! imageUrl )
139 ? <Image { ...imageProps } />
140 : <ImagePlaceholder { ...props } canUploadImage={ canUploadImage } />
141 }
142 </Element>
143 </RootElement>
144 </>
145 );
146 }
147