diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 63718286e9bb87..1daa92dc882576 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -285,6 +285,10 @@ _Related_ - +# **MediaFlow** + +Undocumented declaration. + # **MediaPlaceholder** _Related_ diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index dc38643a6519b9..1cba67d2d682aa 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -19,6 +19,7 @@ export { default as ContrastChecker } from './contrast-checker'; export { default as InnerBlocks } from './inner-blocks'; export { default as InspectorAdvancedControls } from './inspector-advanced-controls'; export { default as InspectorControls } from './inspector-controls'; +export { default as MediaFlow } from './media-flow'; export { default as MediaPlaceholder } from './media-placeholder'; export { default as MediaUpload } from './media-upload'; export { default as MediaUploadCheck } from './media-upload/check'; diff --git a/packages/block-editor/src/components/media-flow/index.js b/packages/block-editor/src/components/media-flow/index.js new file mode 100644 index 00000000000000..9a02c0e19fcf78 --- /dev/null +++ b/packages/block-editor/src/components/media-flow/index.js @@ -0,0 +1,78 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + +/** + * Internal dependencies + */ +import BlockControls from '../block-controls'; +import { MediaPlaceholder } from '../media-placeholder'; +import BlockIcon from '../block-icon'; + +/** + * WordPress dependencies + */ +import { Fragment, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { + Toolbar, + IconButton, + Path, + Rect, + SVG, +} from '@wordpress/components'; + +const ALLOWED_MEDIA_TYPES = [ 'audio' ]; + +const MediaFlow = ( { mediaURL, onSelectMedia, onSelectURL, children } ) => { + const [ isEditing, setIsEditing ] = useState( ! mediaURL ); + + const editImageIcon = ( ); + + const selectMedia = ( media ) => { + onSelectMedia( media ); + setIsEditing( ! isEditing ); + }; + + const selectURL = ( URL ) => { + onSelectURL( URL ); + setIsEditing( ! isEditing ); + }; + + const editMediaButton = ( + + + setIsEditing( ! mediaURL ? true : ! isEditing ) } + icon={ editImageIcon } + /> + + + ); + + // const editMediaButton2 = (

Test

); + + const mediaPlaceholder = ( + } + onCancel={ mediaURL && setIsEditing } + onSelect={ selectMedia } + onSelectURL={ selectURL } + accept="audio/*" + allowedTypes={ ALLOWED_MEDIA_TYPES } + /> + ); + + return ( + + { editMediaButton } + { ! isEditing && children } + { isEditing && mediaPlaceholder } + + ); +}; + +export default MediaFlow; diff --git a/packages/block-library/src/audio/edit.js b/packages/block-library/src/audio/edit.js index 164b8c22c9a7ba..cbc0d30bd7a06e 100644 --- a/packages/block-library/src/audio/edit.js +++ b/packages/block-library/src/audio/edit.js @@ -1,3 +1,9 @@ + +/** + * Internal dependencies + */ +import { createUpgradedEmbedBlock } from '../embed/util'; + /** * WordPress dependencies */ @@ -5,34 +11,20 @@ import { getBlobByURL, isBlobURL } from '@wordpress/blob'; import { compose } from '@wordpress/compose'; import { Disabled, - IconButton, PanelBody, SelectControl, ToggleControl, - Toolbar, withNotices, } from '@wordpress/components'; import { - BlockControls, - BlockIcon, InspectorControls, - MediaPlaceholder, RichText, + MediaFlow, } from '@wordpress/block-editor'; import { Component } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { withSelect } from '@wordpress/data'; -/** - * Internal dependencies - */ -import icon from './icon'; - -/** - * Internal dependencies - */ -import { createUpgradedEmbedBlock } from '../embed/util'; - const ALLOWED_MEDIA_TYPES = [ 'audio' ]; class AudioEdit extends Component { @@ -117,17 +109,12 @@ class AudioEdit extends Component { render() { const { autoplay, caption, loop, preload, src } = this.props.attributes; - const { setAttributes, isSelected, className, noticeUI } = this.props; - const { editing } = this.state; - const switchToEditing = () => { - this.setState( { editing: true } ); - }; + const { setAttributes, isSelected, className } = this.props; const onSelectAudio = ( media ) => { if ( ! media || ! media.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 } ); - switchToEditing(); return; } // sets the block's attribute and updates the edit component from the @@ -135,35 +122,12 @@ class AudioEdit extends Component { setAttributes( { src: media.url, id: media.id } ); this.setState( { src: media.url, editing: false } ); }; - if ( editing ) { - return ( - } - className={ className } - onSelect={ onSelectAudio } - onSelectURL={ this.onSelectURL } - accept="audio/*" - allowedTypes={ ALLOWED_MEDIA_TYPES } - value={ this.props.attributes } - notices={ noticeUI } - onError={ this.onUploadError } - /> - ); - } - /* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */ return ( - <> - - - - - + ) } - + ); /* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */ }