-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
348 lines (311 loc) · 9.09 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* External dependencies
*/
import classnames from 'classnames';
import { filter, forEach, map, find, every } from 'lodash';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import {
PanelBody,
RangeControl,
SelectControl,
ToggleControl,
withNotices,
} from '@wordpress/components';
import {
BlockIcon,
MediaPlaceholder,
InspectorControls,
} from '@wordpress/block-editor';
import { Component } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { withSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import GalleryImage from './gallery-image';
import icon from './icon';
import { defaultColumnsNumber, pickRelevantMediaFiles } from './shared';
const MAX_COLUMNS = 8;
const linkOptions = [
{ value: 'attachment', label: __( 'Attachment Page' ) },
{ value: 'media', label: __( 'Media File' ) },
{ value: 'none', label: __( 'None' ) },
];
const ALLOWED_MEDIA_TYPES = [ 'image' ];
class GalleryEdit extends Component {
constructor() {
super( ...arguments );
this.onSelectImage = this.onSelectImage.bind( this );
this.onSelectImages = this.onSelectImages.bind( this );
this.setLinkTo = this.setLinkTo.bind( this );
this.setColumnsNumber = this.setColumnsNumber.bind( this );
this.toggleImageCrop = this.toggleImageCrop.bind( this );
this.onMove = this.onMove.bind( this );
this.onMoveForward = this.onMoveForward.bind( this );
this.onMoveBackward = this.onMoveBackward.bind( this );
this.onRemoveImage = this.onRemoveImage.bind( this );
this.onUploadError = this.onUploadError.bind( this );
this.setImageAttributes = this.setImageAttributes.bind( this );
this.setAttributes = this.setAttributes.bind( this );
this.state = {
selectedImage: null,
attachmentCaptions: null,
};
}
setAttributes( attributes ) {
if ( attributes.ids ) {
throw new Error( 'The "ids" attribute should not be changed directly. It is managed automatically when "images" attribute changes' );
}
if ( attributes.images ) {
attributes = {
...attributes,
ids: map( attributes.images, 'id' ),
};
}
this.props.setAttributes( attributes );
}
onSelectImage( index ) {
return () => {
if ( this.state.selectedImage !== index ) {
this.setState( {
selectedImage: index,
} );
}
};
}
onMove( oldIndex, newIndex ) {
const images = [ ...this.props.attributes.images ];
images.splice( newIndex, 1, this.props.attributes.images[ oldIndex ] );
images.splice( oldIndex, 1, this.props.attributes.images[ newIndex ] );
this.setState( { selectedImage: newIndex } );
this.setAttributes( { images } );
}
onMoveForward( oldIndex ) {
return () => {
if ( oldIndex === this.props.attributes.images.length - 1 ) {
return;
}
this.onMove( oldIndex, oldIndex + 1 );
};
}
onMoveBackward( oldIndex ) {
return () => {
if ( oldIndex === 0 ) {
return;
}
this.onMove( oldIndex, oldIndex - 1 );
};
}
onRemoveImage( index ) {
return () => {
const images = filter( this.props.attributes.images, ( img, i ) => index !== i );
const { columns } = this.props.attributes;
this.setState( { selectedImage: null } );
this.setAttributes( {
images,
columns: columns ? Math.min( images.length, columns ) : columns,
} );
};
}
selectCaption( newImage, images, attachmentCaptions ) {
const currentImage = find(
images, { id: newImage.id }
);
const currentImageCaption = currentImage ? currentImage.caption : newImage.caption;
if ( ! attachmentCaptions ) {
return currentImageCaption;
}
const attachment = find(
attachmentCaptions, { id: newImage.id }
);
// if the attachment caption is updated
if ( attachment && ( attachment.caption !== newImage.caption ) ) {
return newImage.caption;
}
return currentImageCaption;
}
onSelectImages( newImages ) {
const { columns, images } = this.props.attributes;
const { attachmentCaptions } = this.state;
this.setState(
{
attachmentCaptions: newImages.map( ( newImage ) => ( {
id: newImage.id,
caption: newImage.caption,
} ) ),
}
);
this.setAttributes( {
images: newImages.map( ( newImage ) => ( {
...pickRelevantMediaFiles( newImage ),
caption: this.selectCaption( newImage, images, attachmentCaptions ),
} ) ),
columns: columns ? Math.min( newImages.length, columns ) : columns,
} );
}
onUploadError( message ) {
const { noticeOperations } = this.props;
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
}
setLinkTo( value ) {
this.setAttributes( { linkTo: value } );
}
setColumnsNumber( value ) {
this.setAttributes( { columns: value } );
}
toggleImageCrop() {
this.setAttributes( { imageCrop: ! this.props.attributes.imageCrop } );
}
getImageCropHelp( checked ) {
return checked ? __( 'Thumbnails are cropped to align.' ) : __( 'Thumbnails are not cropped.' );
}
setImageAttributes( index, attributes ) {
const { attributes: { images } } = this.props;
const { setAttributes } = this;
if ( ! images[ index ] ) {
return;
}
setAttributes( {
images: [
...images.slice( 0, index ),
{
...images[ index ],
...attributes,
},
...images.slice( index + 1 ),
],
} );
}
componentDidMount() {
const { attributes, mediaUpload } = this.props;
const { images } = attributes;
if ( every( images, ( { url } ) => isBlobURL( url ) ) ) {
const filesList = map( images, ( { url } ) => getBlobByURL( url ) );
forEach( images, ( { url } ) => revokeBlobURL( url ) );
mediaUpload( {
filesList,
onFileChange: this.onSelectImages,
allowedTypes: [ 'image' ],
} );
}
}
componentDidUpdate( prevProps ) {
// Deselect images when deselecting the block
if ( ! this.props.isSelected && prevProps.isSelected ) {
this.setState( {
selectedImage: null,
captionSelected: false,
} );
}
}
render() {
const { attributes, isSelected, className, noticeUI } = this.props;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
const hasImages = !! images.length;
const mediaPlaceholder = (
<MediaPlaceholder
addToGallery={ hasImages }
isAppender={ hasImages }
className={ className }
dropZoneUIOnly={ hasImages && ! isSelected }
icon={ ! hasImages && <BlockIcon icon={ icon } /> }
labels={ {
title: ! hasImages && __( 'Gallery' ),
instructions: ! hasImages && __( 'Drag images, upload new ones or select files from your library.' ),
} }
onSelect={ this.onSelectImages }
accept="image/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
multiple
value={ hasImages ? images : undefined }
onError={ this.onUploadError }
notices={ hasImages ? undefined : noticeUI }
/>
);
if ( ! hasImages ) {
return mediaPlaceholder;
}
return (
<>
<InspectorControls>
<PanelBody title={ __( 'Gallery Settings' ) }>
{ images.length > 1 && <RangeControl
label={ __( 'Columns' ) }
value={ columns }
onChange={ this.setColumnsNumber }
min={ 1 }
max={ Math.min( MAX_COLUMNS, images.length ) }
required
/> }
<ToggleControl
label={ __( 'Crop Images' ) }
checked={ !! imageCrop }
onChange={ this.toggleImageCrop }
help={ this.getImageCropHelp }
/>
<SelectControl
label={ __( 'Link To' ) }
value={ linkTo }
onChange={ this.setLinkTo }
options={ linkOptions }
/>
</PanelBody>
</InspectorControls>
{ noticeUI }
<ul
className={ classnames(
className,
{
[ `align${ align }` ]: align,
[ `columns-${ columns }` ]: columns,
'is-cropped': imageCrop,
}
) }
>
{ images.map( ( img, index ) => {
const imgAlt = ( img.alt && img.alt.length > 0 ) ? `${ img.alt }. ` : '';
/* translators: %1$d is the order number of the image, %2$d is the total number of images. */
const ariaLabel = sprintf( __( '%1$simage %2$d of %3$d in gallery' ), imgAlt, ( index + 1 ), images.length );
return (
<li className="blocks-gallery-item" key={ img.id || img.url }>
<GalleryImage
url={ img.url }
alt={ img.alt }
id={ img.id }
isFirstItem={ index === 0 }
isLastItem={ ( index + 1 ) === images.length }
isSelected={ isSelected && this.state.selectedImage === index }
onMoveBackward={ this.onMoveBackward( index ) }
onMoveForward={ this.onMoveForward( index ) }
onRemove={ this.onRemoveImage( index ) }
onSelect={ this.onSelectImage( index ) }
setAttributes={ ( attrs ) => this.setImageAttributes( index, attrs ) }
caption={ img.caption }
aria-label={ ariaLabel }
/>
</li>
);
} ) }
</ul>
{ mediaPlaceholder }
</>
);
}
}
export default compose( [
withSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
const {
__experimentalMediaUpload,
} = getSettings();
return {
mediaUpload: __experimentalMediaUpload,
};
} ),
withNotices,
] )( GalleryEdit );