| import { Gridicon } from '@automattic/components'; |
| import { localize } from 'i18n-calypso'; |
| import PropTypes from 'prop-types'; |
| import { Component } from 'react'; |
| import { connect } from 'react-redux'; |
| import DropZone from 'calypso/components/drop-zone'; |
| import { withAddMedia } from 'calypso/data/media/with-add-media'; |
| import { bumpStat } from 'calypso/lib/analytics/mc'; |
| import { userCan } from 'calypso/lib/site/utils'; |
| import { getEditorPostId } from 'calypso/state/editor/selectors'; |
| import { clearMediaItemErrors } from 'calypso/state/media/actions'; |
|
|
| const noop = () => {}; |
|
|
| class MediaLibraryDropZone extends Component { |
| static displayName = 'MediaLibraryDropZone'; |
|
|
| static propTypes = { |
| site: PropTypes.object, |
| fullScreen: PropTypes.bool, |
| onAddMedia: PropTypes.func, |
| trackStats: PropTypes.bool, |
| addMedia: PropTypes.func, |
| }; |
|
|
| static defaultProps = { |
| fullScreen: true, |
| onAddMedia: noop, |
| trackStats: true, |
| }; |
|
|
| uploadFiles = ( files ) => { |
| if ( ! this.props.site || ! userCan( 'upload_files', this.props.site ) ) { |
| return; |
| } |
|
|
| this.props.clearMediaItemErrors( this.props.site.ID ); |
| this.props.addMedia( files, this.props.site, this.props.postId ); |
| this.props.onAddMedia(); |
|
|
| if ( this.props.trackStats ) { |
| bumpStat( 'editor_upload_via', 'drop' ); |
| } |
| }; |
|
|
| isValidTransfer = ( transfer ) => { |
| if ( ! transfer ) { |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| if ( transfer.mozSourceNode ) { |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| return ! transfer.types || -1 !== Array.prototype.indexOf.call( transfer.types, 'Files' ); |
| }; |
|
|
| render() { |
| const { site, fullScreen, translate } = this.props; |
| const canUploadFiles = userCan( 'upload_files', site ); |
| const textLabel = ! canUploadFiles |
| ? translate( 'You are not authorized to upload files to this site' ) |
| : null; |
| const icon = ! canUploadFiles ? ( |
| <Gridicon icon="cross" size={ 48 } /> |
| ) : ( |
| <Gridicon icon="cloud-upload" size={ 48 } /> |
| ); |
| return ( |
| <DropZone |
| fullScreen={ fullScreen } |
| onVerifyValidTransfer={ this.isValidTransfer } |
| onFilesDrop={ this.uploadFiles } |
| textLabel={ textLabel } |
| icon={ icon } |
| /> |
| ); |
| } |
| } |
|
|
| export default connect( |
| ( state ) => ( { |
| postId: getEditorPostId( state ), |
| } ), |
| { clearMediaItemErrors } |
| )( localize( withAddMedia( MediaLibraryDropZone ) ) ); |
|
|