(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents ) { const { __ } = wpI18n; const { Component, Fragment } = wpElement; const { registerBlockType } = wpBlocks; const { InspectorControls, BlockControls, PanelColorSettings, MediaUpload } = wpEditor; const { RangeControl, PanelBody, ToggleControl, BaseControl, TextControl, Button, IconButton, Dashicon, Spinner, Toolbar } = wpComponents; const PLAY_BUTTON_STYLE = { normal: [ , ], circleFill: [ , ], circleOutline: [ , ], videoCam: [ , ], squareCurved: [ , ], starSticker: [ , ], }; class AdvVideo extends Component { constructor() { super( ...arguments ); this.state = { fetching: false, }; this.fetchVideoInfo = this.fetchVideoInfo.bind( this ); } componentWillMount() { const { attributes, setAttributes } = this.props; const currentBlockConfig = advgbDefaultConfig['advgb-video']; // No override attributes of blocks inserted before if (attributes.changed !== true) { if (typeof currentBlockConfig === 'object' && currentBlockConfig !== null) { Object.keys(currentBlockConfig).map((attribute) => { if (typeof attributes[attribute] === 'boolean') { attributes[attribute] = !!currentBlockConfig[attribute]; } else { attributes[attribute] = currentBlockConfig[attribute]; } }); } // Finally set changed attribute to true, so we don't modify anything again setAttributes( { changed: true } ); } } fetchVideoInfo() { const { attributes, setAttributes } = this.props; const { videoID, poster, posterID } = attributes; let realID = videoID; if (!!videoID) { this.setState( { fetching: true } ); let url = ''; if (videoID.match( /^\d+$/g )) { url = `https://vimeo.com/${videoID}` } else { url = `https://www.youtube.com/watch?v=${videoID}` } if (videoID.indexOf( 'http' ) > -1) { url = videoID; } if (videoID.match( /youtube.com/ )) { realID = videoID.split( 'v=' ); realID = realID[1]; } else if (videoID.match( /youtu.be|vimeo.com/ )) { realID = videoID.split( '/' ); realID = realID[ realID.length - 1 ]; } if (!realID) realID = ''; if (realID.indexOf( '&' ) > -1) realID = realID.substring( 0, realID.indexOf( '&' ) ); wp.apiFetch( { path: wp.url.addQueryArgs(`/oembed/1.0/proxy?url=${ encodeURIComponent( url ) }`) } ).then( (obj) => { this.setState( { fetching: false } ); if (!!obj.title && !!obj.provider_name) { setAttributes( { videoTitle: obj.title, poster: !!posterID ? poster : obj.thumbnail_url, } ); switch (obj.provider_name) { case 'YouTube': setAttributes( { videoSourceType: 'youtube', videoURL: `https://www.youtube.com/embed/${realID}?rel=0&wmode=transparent`, } ); break; case 'Vimeo': setAttributes( { videoSourceType: 'vimeo', videoURL: `https://player.vimeo.com/video/${realID}`, } ); break; default: break; } } else { setAttributes( { videoTitle: 'ADVGB_FAIL_TO_LOAD', poster: '', } ); } } ).catch( ( error ) => { this.setState( { fetching: false } ); setAttributes( { videoTitle: 'ADVGB_FAIL_TO_LOAD', poster: '', } ); } ) } } render() { const { isSelected, attributes, setAttributes } = this.props; const { videoURL, videoID, videoSourceType, videoTitle, videoFullWidth, videoWidth, videoHeight, playButtonIcon, playButtonSize, playButtonColor, overlayColor, poster, posterID, openInLightbox, } = attributes; const blockClassName = [ 'advgb-video-block', !!openInLightbox && !!videoURL && 'advgb-video-lightbox', ].filter( Boolean ).join( ' ' ); const videoWrapperClass = [ 'advgb-video-wrapper', !!videoFullWidth && 'full-width', !openInLightbox && 'no-lightbox', ].filter( Boolean ).join( ' ' ); const videoHostIcon = { youtube: ( ), vimeo: ( ), local: ( ), }; return ( { ( (!!poster && openInLightbox) || ( !openInLightbox && videoSourceType === 'local' ) ) && setAttributes( { poster: image.url, posterID: image.id } ) } render={ ( { open } ) => ( ) } /> setAttributes( { poster: undefined, posterID: undefined } ) } /> } setAttributes( { openInLightbox: !openInLightbox } ) } /> setAttributes( { videoFullWidth: !videoFullWidth } ) } /> {!videoFullWidth && setAttributes( { videoWidth: value } ) } /> } setAttributes( { videoHeight: value } ) } /> {!!openInLightbox && setAttributes( { overlayColor: value === undefined ? '#EEEEEE' : value } ), }, { label: __( 'Play Button Color' ), value: playButtonColor, onChange: ( value ) => setAttributes( { playButtonColor: value === undefined ? '#fff' : value } ), }, ] } />
{Object.keys( PLAY_BUTTON_STYLE ).map( ( key, index ) => (
setAttributes( { playButtonIcon: key } ) }> { PLAY_BUTTON_STYLE[key] }
) ) }
setAttributes( { playButtonSize: value } ) } />
}
{!!openInLightbox &&
{!poster && setAttributes( { poster: media.url, posterID: media.id } ) } value={ posterID } render={ ( { open } ) => ( ) } /> }
{PLAY_BUTTON_STYLE[playButtonIcon]}
} {!openInLightbox && (
{( (videoSourceType === 'youtube' || videoSourceType === 'vimeo') &&