Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x | /**
* External dependencies
*/
import { PlayIcon } from '@automattic/jetpack-shared-extension-utils/icons';
/**
* WordPress dependencies
*/
import { MediaUpload } from '@wordpress/block-editor';
import { BaseControl, Button, TextControl, RangeControl } from '@wordpress/components';
import { createInterpolateElement, useEffect, useRef, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Icon } from '@wordpress/icons';
import clsx from 'clsx';
import './style.scss';
const VIDEO_POSTER_ALLOWED_MEDIA_TYPES = [ 'image' ];
const PosterImageWrapper = props => {
const { videoPosterImageUrl } = props;
return (
<div className="resumable-upload__editor-thumb-placeholder">
{ videoPosterImageUrl ? (
<>
<img src={ videoPosterImageUrl } alt="Poster" />
<Icon className="uploading-editor__play-icon" icon={ PlayIcon } />
</>
) : (
<span>No Poster Selected</span>
) }
</div>
);
};
export const PosterSelector = props => {
const { onSelectPoster, onRemovePoster } = props;
return (
<BaseControl className={ clsx( props.className, 'editor-video-poster-control' ) }>
<PosterImageWrapper { ...props } />
<MediaUpload
title={ __( 'Select Poster Image', 'jetpack' ) }
onSelect={ onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<div className="poster-selector__buttons">
<Button onClick={ onRemovePoster } variant="link" isDestructive>
{ __( 'Remove Poster Image', 'jetpack' ) }
</Button>
<Button variant="secondary" onClick={ open }>
{ __( 'Select Poster Image', 'jetpack' ) }
</Button>
</div>
) }
/>
</BaseControl>
);
};
export const UploadingEditor = props => {
const {
file,
onSelectPoster,
onRemovePoster,
videoPosterImageData,
title,
onChangeTitle,
onVideoFrameSelected,
} = props;
const [ maxDuration, setMaxDuration ] = useState( 0 );
const [ canDisplayThumbnailScrubber, setCanDisplayThumbnailScrubber ] = useState( true );
const videoPlayer = useRef( null );
const posterImageLink = useRef( null );
const onVideoError = () => {
setCanDisplayThumbnailScrubber( false );
};
const onVideoLoad = event => {
if ( ! event.target.videoHeight ) {
onVideoError();
}
};
useEffect( () => {
if ( null === file ) {
return;
}
if ( videoPlayer ) {
videoPlayer.current.src = URL.createObjectURL( file );
}
}, [ file, videoPlayer ] );
const onDurationChange = event => {
const newDuration = event.target.duration;
setMaxDuration( newDuration );
if ( videoPlayer.current ) {
videoPlayer.current.currentTime = newDuration / 2;
}
};
const onRangeChange = newRangeValue => {
onVideoFrameSelected( newRangeValue * 1000 );
if ( videoPlayer.current ) {
videoPlayer.current.currentTime = newRangeValue;
}
};
// If we have a poster image, hide the video but keep it in the dom
const posterSelectedStyle = {};
if ( videoPosterImageData ) {
posterSelectedStyle.display = 'none';
}
return (
<>
<div className="uploading-editor">
<div className="uploading-editor__summary"></div>
<div className="uploading-editor__fields">
<TextControl
label={ __( 'Video title', 'jetpack' ) }
className="uploading-editor__title"
onChange={ onChangeTitle }
value={ title }
__nextHasNoMarginBottom={ true }
__next40pxDefaultSize={ true }
/>
<div className="uploading-editor__content">
<BaseControl __nextHasNoMarginBottom={ true }>
<BaseControl.VisualLabel>
{ __( 'Video poster (optional)', 'jetpack' ) }
</BaseControl.VisualLabel>
{ canDisplayThumbnailScrubber ? (
<>
<div className="uploading-editor__video-container">
<div className="uploading-editor__video-poster-wrapper">
{ videoPosterImageData && (
<PosterSelector
onSelectPoster={ onSelectPoster }
onRemovePoster={ onRemovePoster }
videoPosterImageUrl={ videoPosterImageData.url }
/>
) }
<video
ref={ videoPlayer }
muted
className="uploading-editor__video"
onDurationChange={ onDurationChange }
onError={ onVideoError }
onLoadedMetadata={ onVideoLoad }
style={ posterSelectedStyle }
/>
</div>
{ ! videoPosterImageData && (
<Icon className="uploading-editor__play-icon" icon={ PlayIcon } />
) }
</div>
<span style={ posterSelectedStyle }>
<RangeControl
className="uploading-editor__range"
min="0"
step="0.1"
max={ maxDuration }
showTooltip={ false }
withInputField={ false }
onChange={ onRangeChange }
__nextHasNoMarginBottom={ true }
__next40pxDefaultSize={ true }
/>
</span>
<span className="uploading-editor__scrubber-help" style={ posterSelectedStyle }>
{ createInterpolateElement(
__(
'This is how the video will look. Use the slider to choose a poster or <a>select a custom one</a>.',
'jetpack'
),
{
a: (
<MediaUpload
title={ __( 'Select Poster Image', 'jetpack' ) }
onSelect={ onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<a
className="uploading-editor__upload-link"
onClick={ open }
onKeyDown={ open }
ref={ posterImageLink }
role="button"
tabIndex={ 0 }
>
{ __( 'select a custom one', 'jetpack' ) }
</a>
) }
/>
),
}
) }
</span>
</>
) : (
<PosterSelector
onSelectPoster={ onSelectPoster }
videoPosterImageUrl={ videoPosterImageData ? videoPosterImageData.url : null }
/>
) }
</BaseControl>
</div>
</div>
</div>
</>
);
};
|