-
Notifications
You must be signed in to change notification settings - Fork 381
/
custom-video-block-edit.js
425 lines (389 loc) · 11.8 KB
/
custom-video-block-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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { getBlobByURL, isBlobURL } from '@wordpress/blob';
import { useRef, useState, useEffect } from '@wordpress/element';
import {
BaseControl,
Button,
IconButton,
Notice,
PanelBody,
Path,
ResponsiveWrapper,
SVG,
TextControl,
ToggleControl,
Toolbar,
withNotices,
} from '@wordpress/components';
import {
BlockControls,
BlockIcon,
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
RichText,
} from '@wordpress/block-editor';
import { compose, withInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { uploadVideoFrame, getPosterImageFromFileObj } from '../helpers';
import { getContentLengthFromUrl, isVideoSizeExcessive } from '../../common/helpers';
import { MEGABYTE_IN_BYTES, VIDEO_ALLOWED_MEGABYTES_PER_SECOND } from '../../common/constants';
import { POSTER_ALLOWED_MEDIA_TYPES } from '../constants';
const icon = (
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M4 6.47L5.76 10H20v8H4V6.47M22 4h-4l2 4h-3l-2-4h-2l2 4h-3l-2-4H8l2 4H7L5 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V4z" />
</SVG>
);
/**
* Mainly forked from the Core Video block edit component, but allows the <video> to play instead of being disabled.
*
* There are very few changes from the Core Video block's component.
* The main change is that in render(), the <video> is not wrapped in <Disabled>, so it can play.
*
* Also removes video settings that are not applicable / allowed in an AMP Stories context.
*/
const CustomVideoBlockEdit = ( { instanceId, isSelected, className, attributes, setAttributes, noticeUI, noticeOperations } ) => {
const {
caption,
loop,
id,
poster,
src,
width,
height,
ampAriaLabel,
} = attributes;
const [ isEditing, setIsEditing ] = useState( ! src );
const [ videoSize, setVideoSize ] = useState( null );
const [ duration, setDuration ] = useState( null );
const [ isExtractingPoster, setExtractingPoster ] = useState( false );
const switchToEditing = () => setIsEditing( true );
const videoPlayer = useRef( null );
const {
media,
videoFeaturedImage,
allowedVideoMimeTypes,
} = useSelect( ( select ) => {
const { getMedia } = select( 'core' );
const { getSettings } = select( 'amp/story' );
let featuredImage;
const mediaObj = id ? getMedia( id ) : undefined;
if ( mediaObj && mediaObj.featured_media && ! poster ) {
featuredImage = getMedia( mediaObj.featured_media );
}
return {
media: mediaObj,
videoFeaturedImage: featuredImage,
allowedVideoMimeTypes: getSettings().allowedVideoMimeTypes,
};
}, [ id, poster ] );
const { mediaUpload } = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
const { __experimentalMediaUpload } = getSettings();
return {
mediaUpload: __experimentalMediaUpload,
};
}, [] );
useEffect( () => {
if ( ! id && isBlobURL( src ) ) {
const file = getBlobByURL( src );
if ( file ) {
mediaUpload( {
filesList: [ file ],
onFileChange: ( [ { id: mediaId, url } ] ) => {
setDuration( null );
setVideoSize( null );
setAttributes( { id: mediaId, src: url } );
},
onError: ( message ) => {
setIsEditing( true );
noticeOperations.createErrorNotice( message );
},
allowedTypes: allowedVideoMimeTypes,
} );
}
}
if ( src && ! isBlobURL( src ) ) {
getContentLengthFromUrl( src ).then( setVideoSize );
}
// Disable reason: src is constantly changing, but we want this hook to fire only once (like componentDidMount).
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );
useEffect( () => {
if ( videoPlayer.current ) {
videoPlayer.current.load();
}
}, [ poster ] );
useEffect( () => {
if ( src && ! isBlobURL( src ) ) {
getContentLengthFromUrl( src ).then( setVideoSize );
}
}, [ src ] );
useEffect( () => {
if ( ! ampAriaLabel && media ) {
/*
* New video set from media library and we don't have an aria label already,
* use alt text or title from media object.
*/
const newAriaLabel = media.alt_text || ( media.title && media.title.raw ) || '';
setAttributes( { ampAriaLabel: newAriaLabel } );
}
if ( videoFeaturedImage ) {
setAttributes( { poster: videoFeaturedImage.source_url } );
} else if ( media && ! media.featured_media && ! isExtractingPoster ) {
/*
* The video has changed, and its media object has been loaded already.
*
* Since it's clear that the video does not have a featured (poster) image,
* one can be generated now.
*/
setExtractingPoster( true );
uploadVideoFrame( { id, src } )
.then( ( fileObj ) => {
setAttributes( { poster: getPosterImageFromFileObj( fileObj ) } );
setExtractingPoster( false );
} )
.catch( () => setExtractingPoster( false ) );
}
}, [ media, ampAriaLabel, id, isExtractingPoster, setAttributes, src, videoFeaturedImage ] );
/**
* Callback to toggle an attribute's value.
*
* @param {string} attribute Attribute name.
* @return {Function} Function that updates the block's attributes.
*/
const toggleAttribute = ( attribute ) => ( newValue ) => setAttributes( { [ attribute ]: newValue } );
/**
* URL selection callback.
*
* @param {string} newSrc New src value.
*/
const onSelectURL = ( newSrc ) => {
// Set the block's src from the edit component's state, and switch off
// the editing UI.
if ( newSrc !== src ) {
setAttributes( { src: newSrc, id: undefined, poster: undefined } );
setExtractingPoster( true );
/*
* Since the video has been added via URL, there's no attachment object
* a poster image could be retrieved from.
*/
uploadVideoFrame( { src: newSrc } )
.then( ( fileObj ) => {
setAttributes( { poster: getPosterImageFromFileObj( fileObj ) } );
setExtractingPoster( false );
} )
.catch( () => setExtractingPoster( false ) );
}
setIsEditing( false );
setDuration( null );
setVideoSize( null );
};
/**
* Upload error callback.
*
* @param {string} message Error message.
*/
const onUploadError = ( message ) => {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
};
/**
* Metadata loaded callback.
*
* @param {Event} event Event object.
*/
const onLoadedMetadata = ( event ) => {
setDuration( Math.round( event.currentTarget.duration ) );
};
const onSelectVideo = ( mediaObj ) => {
if ( ! mediaObj || ! mediaObj.url ) {
// in this case there was an error and we should continue in the editing state
// previous attributes should be removed because they may be temporary blob urls
setAttributes( { src: undefined, id: undefined, poster: undefined } );
switchToEditing();
return;
}
// sets the block's attribute and updates the edit component from the
// selected media, then switches off the editing UI
setAttributes( { src: mediaObj.url, id: mediaObj.id, poster: undefined } );
setIsEditing( false );
setDuration( null );
setVideoSize( null );
};
if ( isEditing ) {
return (
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
className={ className }
onSelect={ onSelectVideo }
onSelectURL={ onSelectURL }
accept={ allowedVideoMimeTypes.join( ',' ) }
allowedTypes={ allowedVideoMimeTypes }
value={ attributes }
notices={ noticeUI }
onError={ onUploadError }
/>
);
}
const videoBytesPerSecond = duration && videoSize ? videoSize / duration : 0;
const videoPosterId = `video-block__poster-image-${ instanceId }`;
const isExcessiveVideoSize = videoBytesPerSecond ? isVideoSizeExcessive( videoBytesPerSecond ) : null;
return (
<>
<BlockControls>
<Toolbar>
<IconButton
className="components-icon-button components-toolbar__control"
label={ __( 'Edit video', 'amp' ) }
onClick={ switchToEditing }
icon="edit"
/>
</Toolbar>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Video Settings', 'amp' ) }>
<ToggleControl
label={ __( 'Loop', 'amp' ) }
onChange={ toggleAttribute( 'loop' ) }
checked={ loop }
/>
<TextControl
label={ __( 'Assistive Text', 'amp' ) }
help={ __( 'Used to inform visually impaired users about the video content.', 'amp' ) }
value={ ampAriaLabel }
onChange={ ( label ) => setAttributes( { ampAriaLabel: label } ) }
/>
{ ( ! isExtractingPoster || poster ) && (
<MediaUploadCheck>
<BaseControl
id={ videoPosterId }
label={ __( 'Poster Image', 'amp' ) }
className="editor-video-poster-control"
>
{
! poster &&
<Notice status="error" isDismissible={ false } >
{ __( 'A poster image must be set.', 'amp' ) }
</Notice>
}
<MediaUpload
title={ __( 'Select Poster Image', 'amp' ) }
onSelect={ ( image ) => {
setAttributes( { poster: image.url } );
} }
allowedTypes={ POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<Button
id={ videoPosterId }
className={ classnames(
'video-block__poster-image',
{
'editor-post-featured-image__toggle': ! poster,
'editor-post-featured-image__preview': poster,
},
) }
onClick={ open }
aria-label={ ! poster ? null : __( 'Replace Poster Image', 'amp' ) }
>
{ poster && (
<ResponsiveWrapper
naturalWidth={ width }
naturalHeight={ height }
>
<img src={ poster } alt="" />
</ResponsiveWrapper>
) }
{ ! poster &&
__( 'Set Poster Image', 'amp' )
}
</Button>
) }
/>
</BaseControl>
</MediaUploadCheck>
) }
{
isExcessiveVideoSize && (
<Notice status="warning" isDismissible={ false } >
{
sprintf(
/* translators: %d: the number of recommended megabytes per second */
__( 'A video size of less than %d MB per second is recommended.', 'amp' ),
VIDEO_ALLOWED_MEGABYTES_PER_SECOND,
)
}
{ ' ' }
{
sprintf(
/* translators: %d: the number of actual megabytes per second */
__( 'The selected video is %d MB per second.', 'amp' ),
Math.round( videoBytesPerSecond / MEGABYTE_IN_BYTES ),
)
}
</Notice>
)
}
</PanelBody>
</InspectorControls>
<figure className="wp-block-video">
<video
autoPlay
muted
aria-label={ ampAriaLabel }
loop={ loop }
controls={ ! loop }
poster={ poster }
ref={ videoPlayer }
src={ src }
onLoadedMetadata={ onLoadedMetadata }
/>
{ ( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
tagName="figcaption"
placeholder={ __( 'Write caption…', 'amp' ) }
value={ caption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
inlineToolbar
/>
) }
</figure>
</>
);
};
CustomVideoBlockEdit.propTypes = {
attributes: PropTypes.shape( {
caption: PropTypes.string,
controls: PropTypes.bool,
loop: PropTypes.bool,
ampAriaLabel: PropTypes.string,
id: PropTypes.number,
poster: PropTypes.string,
src: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
} ),
className: PropTypes.string,
instanceId: PropTypes.number,
isSelected: PropTypes.bool,
noticeUI: PropTypes.oneOfType( [ PropTypes.func, PropTypes.bool ] ),
noticeOperations: PropTypes.object,
setAttributes: PropTypes.func,
};
export default compose( [
withNotices,
withInstanceId,
] )( CustomVideoBlockEdit );