(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 } ) }>
) ) }
setAttributes( { playButtonSize: value } ) }
/>
}
{!!openInLightbox &&
{!poster &&
setAttributes( { poster: media.url, posterID: media.id } ) }
value={ posterID }
render={ ( { open } ) => (
) }
/>
}
}
{!openInLightbox && (
{( (videoSourceType === 'youtube' || videoSourceType === 'vimeo') &&
)
|| (videoSourceType === 'local' &&
)
|| !videoSourceType &&
}
) }
{isSelected &&
{
setAttributes( { videoID: value, videoURL: '', videoTitle: undefined, videoSourceType: '' } );
} }
/>
{ __( 'or use' ) }
setAttributes( { videoURL: video.url, videoID: video.id, videoTitle: video.title, videoSourceType: 'local' } ) }
render={ ( { open } ) => (
) }
/>
{ __( 'Current Video' ) }:
{videoHostIcon[videoSourceType] || ( this.state.fetching && ) }
{
( videoTitle === 'ADVGB_FAIL_TO_LOAD' && { __( 'Wrong video URL/ID. Please try another.' ) } )
|| videoTitle
|| __( 'Not selected yet.' )
}
}
)
}
}
const advVideoBlockIcon = (
);
const blockAttrs = {
videoURL: {
type: 'string',
},
videoID: {
type: 'string',
},
videoSourceType: {
type: 'string',
},
videoTitle: {
type: 'string',
},
videoFullWidth: {
type: 'boolean',
default: true,
},
videoWidth: {
type: 'number',
},
videoHeight: {
type: 'number',
default: 450,
},
playButtonIcon: {
type: 'string',
default: 'normal'
},
playButtonSize: {
type: 'number',
default: 80,
},
playButtonColor: {
type: 'string',
default: '#fff',
},
overlayColor: {
type: 'string',
default: '#EEEEEE',
},
poster: {
type: 'string',
},
posterID: {
type: 'number',
},
openInLightbox: {
type: 'boolean',
default: true,
},
changed: {
type: 'boolean',
default: false,
},
};
registerBlockType( 'advgb/video', {
title: __( 'Advanced Video' ),
description: __( 'Powerful block for insert and embed video.' ),
icon: {
src: advVideoBlockIcon,
foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined,
},
category: 'advgb-category',
keywords: [ __( 'video' ), __( 'embed' ), __( 'media' ) ],
attributes: blockAttrs,
edit: AdvVideo,
save: function ( { attributes } ) {
const {
videoURL,
videoSourceType,
videoTitle,
videoFullWidth,
videoWidth,
videoHeight,
playButtonIcon,
playButtonSize,
playButtonColor,
overlayColor,
poster,
openInLightbox,
} = attributes;
const blockClassName = [
'advgb-video-block',
!!videoFullWidth && 'full-width',
!!openInLightbox && !!videoURL && 'advgb-video-lightbox',
].filter( Boolean ).join( ' ' );
const videoWrapperClass = [
'advgb-video-wrapper',
!!videoFullWidth && 'full-width',
!openInLightbox && 'no-lightbox',
].filter( Boolean ).join( ' ' );
return (
{!openInLightbox && (
{( (videoSourceType === 'youtube' || videoSourceType === 'vimeo') &&
)
|| (videoSourceType === 'local' &&
)
|| !videoSourceType &&
}
) }
{!!openInLightbox &&
}
);
},
deprecated: [
{
attributes: blockAttrs,
save: function ( { attributes } ) {
const {
videoURL,
videoSourceType,
videoTitle,
videoFullWidth,
videoWidth,
videoHeight,
playButtonIcon,
playButtonSize,
playButtonColor,
overlayColor,
poster,
openInLightbox,
} = attributes;
const blockClassName = [
'advgb-video-block',
!!videoFullWidth && 'full-width',
!!openInLightbox && !!videoURL && 'advgb-video-lightbox',
].filter( Boolean ).join( ' ' );
const videoWrapperClass = [
'advgb-video-wrapper',
!!videoFullWidth && 'full-width',
].filter( Boolean ).join( ' ' );
return (
{!openInLightbox && (
( (videoSourceType === 'youtube' || videoSourceType === 'vimeo') &&
)
|| (videoSourceType === 'local' &&
)
|| !videoSourceType &&
) }
{!!openInLightbox &&
}
);
},
}
],
} );
})( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components );