@wordpress/block-library
Version:
Block library for the WordPress editor.
131 lines (121 loc) • 2.92 kB
JavaScript
/**
* WordPress dependencies
*/
import { IconButton, ResizableBox, Toolbar } from '@wordpress/components';
import {
BlockControls,
BlockIcon,
MediaPlaceholder,
MediaUpload,
} from '@wordpress/block-editor';
import { Component, Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import icon from './media-container-icon';
/**
* Constants
*/
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ];
class MediaContainer extends Component {
renderToolbarEditButton() {
const { mediaId, onSelectMedia } = this.props;
return (
<BlockControls>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ mediaId }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</BlockControls>
);
}
renderImage() {
const { mediaAlt, mediaUrl, className } = this.props;
return (
<Fragment>
{ this.renderToolbarEditButton() }
<figure className={ className }>
<img src={ mediaUrl } alt={ mediaAlt } />
</figure>
</Fragment>
);
}
renderVideo() {
const { mediaUrl, className } = this.props;
return (
<Fragment>
{ this.renderToolbarEditButton() }
<figure className={ className }>
<video controls src={ mediaUrl } />
</figure>
</Fragment>
);
}
renderPlaceholder() {
const { onSelectMedia, className } = this.props;
return (
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
labels={ {
title: __( 'Media area' ),
} }
className={ className }
onSelect={ onSelectMedia }
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
/>
);
}
render() {
const { mediaPosition, mediaUrl, mediaType, mediaWidth, commitWidthChange, onWidthChange } = this.props;
if ( mediaType && mediaUrl ) {
const onResize = ( event, direction, elt ) => {
onWidthChange( parseInt( elt.style.width ) );
};
const onResizeStop = ( event, direction, elt ) => {
commitWidthChange( parseInt( elt.style.width ) );
};
const enablePositions = {
right: mediaPosition === 'left',
left: mediaPosition === 'right',
};
let mediaElement = null;
switch ( mediaType ) {
case 'image':
mediaElement = this.renderImage();
break;
case 'video':
mediaElement = this.renderVideo();
break;
}
return (
<ResizableBox
className="editor-media-container__resizer"
size={ { width: mediaWidth + '%' } }
minWidth="10%"
maxWidth="100%"
enable={ enablePositions }
onResize={ onResize }
onResizeStop={ onResizeStop }
axis="x"
>
{ mediaElement }
</ResizableBox>
);
}
return this.renderPlaceholder();
}
}
export default MediaContainer;