PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.0
GenerateBlocks v2.2.0
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 / media / edit.js
generateblocks / src / blocks / media Last commit date
components 10 months ago hooks 1 year ago block.json 1 year ago edit.js 6 months ago editor.scss 6 months ago image-utils.js 1 year ago index.js 1 year ago save.js 1 year ago toolbar-appenders.js 1 year ago transforms.js 1 year ago
edit.js
239 lines
1 import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
2 import { useEffect, useState } from '@wordpress/element';
3 import { compose } from '@wordpress/compose';
4 import { isBlobURL, getBlobByURL, revokeBlobURL } from '@wordpress/blob';
5 import { useSelect } from '@wordpress/data';
6 import { applyFilters } from '@wordpress/hooks';
7
8 import { BlockStyles, withUniqueId } from '@edge22/block-styles';
9
10 import { useImageFunctions } from './hooks/useImageFunctions.js';
11 import { Image } from './components/Image.jsx';
12 import { withDynamicTag } from '../../hoc/withDynamicTag.js';
13 import RootElement from '../../components/root-element/index.js';
14 import { BlockSettings } from './components/BlockSettings';
15 import { withStyles } from '@hoc/withStyles';
16 import { BlockStylesBuilder, StylesOnboarder } from '@components/index';
17 import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js';
18 import { getBlockClasses } from '@utils/getBlockClasses.js';
19
20 function EditBlock( props ) {
21 const {
22 attributes,
23 setAttributes,
24 isSelected,
25 name,
26 clientId,
27 onStyleChange,
28 editorHtmlAttributes,
29 htmlAttributes,
30 styles,
31 } = props;
32
33 const {
34 tagName,
35 linkHtmlAttributes = {},
36 } = attributes;
37
38 const [ temporaryURL, setTemporaryURL ] = useState();
39 const { isTemporaryImage, mediaUpload, onUploadError } = useImageFunctions();
40 const classNames = getBlockClasses(
41 'gb-media',
42 {
43 ...attributes,
44 styles,
45 }
46 );
47
48 useEffect( () => {
49 if ( ! tagName ) {
50 setAttributes( { tagName: 'img' } );
51 }
52 }, [ tagName ] );
53
54 const {
55 isBlockMultiSelected,
56 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
57
58 const blockProps = useBlockProps();
59 const shouldWrapBlock = isSelected || isBlockMultiSelected( clientId ) || ( 'img' === tagName && ! temporaryURL && ! htmlAttributes?.src );
60 const elementAttributes = {
61 className: classNames.join( ' ' ).trim(),
62 'data-block': clientId,
63 ...editorHtmlAttributes,
64 };
65 const TagName = tagName || 'img';
66
67 function onSelectImage( image, sizeSlug = null ) {
68 if ( ! image || ! image.url ) {
69 onResetImage();
70 return;
71 }
72
73 if ( isBlobURL( image.url ) ) {
74 setTemporaryURL( image.url );
75 }
76
77 setTemporaryURL();
78
79 if ( !! image ) {
80 const imageUrl = ( image?.sizes && image?.sizes[ sizeSlug ]?.url ) || image?.url;
81
82 const filteredAttributes = applyFilters(
83 'generateblocks.media.imageAttributes',
84 {
85 alt: image.alt,
86 title: image.title,
87 height: image.height,
88 width: image.width,
89 },
90 { image, attributes }
91 );
92
93 const newAttributes = {
94 ...htmlAttributes,
95 ...filteredAttributes,
96 src: imageUrl,
97 };
98
99 setAttributes( {
100 htmlAttributes: newAttributes,
101 mediaId: image.id ?? 0,
102 } );
103 }
104 }
105
106 function onSelectURL( newURL ) {
107 if ( newURL === htmlAttributes.src ) {
108 return;
109 }
110
111 const newAttributes = {
112 ...htmlAttributes,
113 src: newURL,
114 };
115
116 setAttributes( { htmlAttributes: newAttributes } );
117 }
118
119 function onResetImage() {
120 const newAttributes = { ...htmlAttributes };
121 delete newAttributes.src;
122 delete newAttributes.alt;
123 delete newAttributes.title;
124
125 setAttributes( {
126 htmlAttributes: newAttributes,
127 mediaId: 0,
128 } );
129 }
130
131 let isTemp = isTemporaryImage( htmlAttributes?.src );
132
133 useEffect( () => {
134 if ( ! isTemp ) {
135 return;
136 }
137
138 const file = getBlobByURL( htmlAttributes?.src );
139
140 if ( file ) {
141 mediaUpload( {
142 filesList: [ file ],
143 onFileChange: ( [ img ] ) => {
144 onSelectImage( img );
145 },
146 allowedTypes: [ 'image' ],
147 onError: ( message ) => {
148 isTemp = false;
149 onUploadError( message );
150 },
151 } );
152 }
153 }, [] );
154
155 // If an image is temporary, revoke the Blob url when it is uploaded (and is
156 // no longer temporary).
157 useEffect( () => {
158 if ( isTemp ) {
159 setTemporaryURL( htmlAttributes?.src );
160 return;
161 }
162 revokeBlobURL( temporaryURL );
163 }, [ isTemp, htmlAttributes?.src ] );
164
165 function elementRender() {
166 return (
167 <>
168 { 'img' === tagName ? (
169 <Image
170 { ...props }
171 elementAttributes={ elementAttributes }
172 temporaryURL={ temporaryURL }
173 onSelectImage={ onSelectImage }
174 onSelectURL={ onSelectURL }
175 onResetImage={ onResetImage }
176 onUploadError={ onUploadError }
177 linkHtmlAttributes={ linkHtmlAttributes }
178 />
179 ) : (
180 <TagName { ...elementAttributes } />
181 ) }
182 </>
183 );
184 }
185
186 return (
187 <>
188 <InspectorControls>
189 <StylesOnboarder />
190
191 <BlockStyles
192 settingsTab={ (
193 <BlockSettings
194 { ...props }
195 onSelectImage={ onSelectImage }
196 />
197 ) }
198 stylesTab={ (
199 <BlockStylesBuilder
200 attributes={ attributes }
201 setAttributes={ setAttributes }
202 shortcuts={ {} }
203 onStyleChange={ onStyleChange }
204 name={ name }
205 />
206 ) }
207 />
208 </InspectorControls>
209
210 <RootElement
211 name={ name }
212 clientId={ clientId }
213 >
214 <div
215 { ...blockProps }
216 data-block-wrapper
217 style={ {
218 display: ! shouldWrapBlock ? 'none' : undefined,
219 } }
220 >
221 { elementRender() }
222 </div>
223 <div style={ { display: shouldWrapBlock ? 'none' : 'contents' } }>
224 { elementRender() }
225 </div>
226 </RootElement>
227 </>
228 );
229 }
230
231 const Edit = compose(
232 withHtmlAttributes,
233 withStyles,
234 withDynamicTag,
235 withUniqueId
236 )( EditBlock );
237
238 export { Edit };
239