diff --git a/packages/block-editor/src/components/media-upload/add-from-url-bottom-sheet.native.js b/packages/block-editor/src/components/media-upload/add-from-url-bottom-sheet.native.js new file mode 100644 index 0000000000000..6ea79c0b426a7 --- /dev/null +++ b/packages/block-editor/src/components/media-upload/add-from-url-bottom-sheet.native.js @@ -0,0 +1,46 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { LinkSettingsNavigation } from '@wordpress/components'; +import { useState } from '@wordpress/element'; +import { isURL } from '@wordpress/url'; +import { useDispatch } from '@wordpress/data'; +import { store as noticesStore } from '@wordpress/notices'; + +const linkSettingsOptions = { + url: { + label: __( 'Insert from URL' ), + placeholder: __( 'Add link' ), + autoFocus: true, + autoFill: true, + }, +}; + +const AddFromUrlBottomSheet = ( { value, onClose, isVisible } ) => { + const [ url, setUrl ] = useState( value ); + const { createErrorNotice } = useDispatch( noticesStore ); + + function onCloseWithUrl() { + if ( isURL( url ) ) { + onClose( { url } ); + } else { + createErrorNotice( __( 'Invalid URL. Please enter a valid URL.' ) ); + onClose( {} ); + } + } + + return ( + setUrl( attributes.url ) } + onClose={ onCloseWithUrl } + options={ linkSettingsOptions } + withBottomSheet + showIcon + /> + ); +}; + +export default AddFromUrlBottomSheet; diff --git a/packages/block-editor/src/components/media-upload/index.native.js b/packages/block-editor/src/components/media-upload/index.native.js index 5871d0e9a31c0..8321d498e18e5 100644 --- a/packages/block-editor/src/components/media-upload/index.native.js +++ b/packages/block-editor/src/components/media-upload/index.native.js @@ -1,12 +1,14 @@ +/** + * Internal dependencies + */ +import AddFromUrlBottomSheet from './add-from-url-bottom-sheet'; /** * External dependencies */ -import { Platform } from 'react-native'; +import { View, Platform } from 'react-native'; import { delay } from 'lodash'; -import prompt from 'react-native-prompt-android'; - /** * WordPress dependencies */ @@ -46,6 +48,7 @@ export class MediaUpload extends Component { this.getAllSources = this.getAllSources.bind( this ); this.state = { otherMediaOptions: [], + isAddFromUrlBottomsheetVisible: false, }; } @@ -178,31 +181,10 @@ export class MediaUpload extends Component { } onPickerSelect( value ) { - const { - allowedTypes = [], - onSelect, - onSelectURL, - multiple = false, - } = this.props; + const { allowedTypes = [], onSelect, multiple = false } = this.props; if ( value === 'URL' ) { - prompt( - __( 'Type a URL' ), // title - undefined, // message - [ - { - text: __( 'Cancel' ), - style: 'cancel', - }, - { - text: __( 'Apply' ), - onPress: ( url ) => onSelectURL( url ), - }, - ], // buttons - 'plain-text', // type - undefined, // defaultValue - 'url' // keyboardType - ); + this.setState( { isAddFromUrlBottomsheetVisible: true } ); return; } @@ -221,7 +203,12 @@ export class MediaUpload extends Component { } render() { - const { allowedTypes = [], isReplacingMedia, multiple } = this.props; + const { + allowedTypes = [], + isReplacingMedia, + multiple, + onSelectURL, + } = this.props; const isOneType = allowedTypes.length === 1; const isImage = isOneType && allowedTypes.includes( MEDIA_TYPE_IMAGE ); const isVideo = isOneType && allowedTypes.includes( MEDIA_TYPE_VIDEO ); @@ -270,13 +257,26 @@ export class MediaUpload extends Component { } const getMediaOptions = () => ( - ( this.picker = instance ) } - options={ this.getMediaOptionsItems() } - onChange={ this.onPickerSelect } - /> + + { this.state.isAddFromUrlBottomsheetVisible && ( + { + this.setState( { + isAddFromUrlBottomsheetVisible: false, + } ); + onSelectURL( url ); + } } + isVisible={ this.state.isAddFromUrlBottomsheetVisible } + /> + ) } + ( this.picker = instance ) } + options={ this.getMediaOptionsItems() } + onChange={ this.onPickerSelect } + /> + ); return this.props.render( { diff --git a/packages/block-library/src/audio/edit.native.js b/packages/block-library/src/audio/edit.native.js index 9311e80f9fc82..912c5e722aa8c 100644 --- a/packages/block-library/src/audio/edit.native.js +++ b/packages/block-library/src/audio/edit.native.js @@ -31,7 +31,6 @@ import { audio as icon, replace } from '@wordpress/icons'; import { useState } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { store as noticesStore } from '@wordpress/notices'; -import { isURL } from '@wordpress/url'; /** * Internal dependencies @@ -78,11 +77,7 @@ function AudioEdit( { function onSelectURL( newSrc ) { if ( newSrc !== src ) { - if ( isURL( newSrc ) ) { - setAttributes( { src: newSrc, id: undefined } ); - } else { - createErrorNotice( __( 'Invalid URL. Audio file not found.' ) ); - } + setAttributes( { src: newSrc, id: undefined } ); } }