Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the MediaUploadCheck component before each Upload component #11924

Merged
merged 2 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BlockAlignmentToolbar,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
AlignmentToolbar,
PanelColorSettings,
RichText,
Expand Down Expand Up @@ -259,21 +260,23 @@ export const settings = {
setAttributes( { contentAlign: nextAlign } );
} }
/>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
</Fragment>
) }
</BlockControls>
Expand Down
31 changes: 17 additions & 14 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Component, Fragment } from '@wordpress/element';
import {
MediaUpload,
MediaPlaceholder,
MediaUploadCheck,
BlockControls,
RichText,
mediaUpload,
Expand Down Expand Up @@ -165,20 +166,22 @@ class FileEdit extends Component {
} }
/>
<BlockControls>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectFile }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit file' ) }
onClick={ open }
icon="edit"
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectFile }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit file' ) }
onClick={ open }
icon="edit"
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
</BlockControls>
<div className={ classes }>
<div className={ `${ className }__content-wrapper` }>
Expand Down
33 changes: 18 additions & 15 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
BlockAlignmentToolbar,
mediaUpload,
} from '@wordpress/editor';
Expand Down Expand Up @@ -328,21 +329,23 @@ class ImageEdit extends Component {
);
} else {
toolbarEditButton = (
<Toolbar>
<MediaUpload
onSelect={ this.onSelectImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit image' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit image' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
);
}
}
Expand Down
49 changes: 26 additions & 23 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
RichText,
mediaUpload,
} from '@wordpress/editor';
Expand Down Expand Up @@ -207,30 +208,32 @@ class VideoEdit extends Component {
{ value: 'none', label: __( 'None' ) },
] }
/>
<BaseControl
className="editor-video-poster-control"
label={ __( 'Poster Image' ) }
>
<MediaUpload
title={ __( 'Select Poster Image' ) }
onSelect={ this.onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<Button
isDefault
onClick={ open }
ref={ this.posterImageButton }
>
{ ! this.props.attributes.poster ? __( 'Select Poster Image' ) : __( 'Replace image' ) }
<MediaUploadCheck>
<BaseControl
className="editor-video-poster-control"
label={ __( 'Poster Image' ) }
>
<MediaUpload
title={ __( 'Select Poster Image' ) }
onSelect={ this.onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<Button
isDefault
onClick={ open }
ref={ this.posterImageButton }
>
{ ! this.props.attributes.poster ? __( 'Select Poster Image' ) : __( 'Replace image' ) }
</Button>
) }
/>
{ !! this.props.attributes.poster &&
<Button onClick={ this.onRemovePoster } isLink isDestructive>
{ __( 'Remove Poster Image' ) }
</Button>
) }
/>
{ !! this.props.attributes.poster &&
<Button onClick={ this.onRemovePoster } isLink isDestructive>
{ __( 'Remove Poster Image' ) }
</Button>
}
</BaseControl>
}
</BaseControl>
</MediaUploadCheck>
</PanelBody>
</InspectorControls>
<figure className={ className }>
Expand Down
23 changes: 15 additions & 8 deletions packages/editor/src/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import MediaUploadCheck from '../media-upload/check';

const parseDropEvent = ( event ) => {
let result = {
srcRootClientId: null,
Expand Down Expand Up @@ -111,14 +116,16 @@ class BlockDropZone extends Component {
const isAppender = index === undefined;

return (
<DropZone
className={ classnames( 'editor-block-drop-zone', {
'is-appender': isAppender,
} ) }
onFilesDrop={ this.onFilesDrop }
onHTMLDrop={ this.onHTMLDrop }
onDrop={ this.onDrop }
/>
<MediaUploadCheck>
<DropZone
className={ classnames( 'editor-block-drop-zone', {
'is-appender': isAppender,
} ) }
onFilesDrop={ this.onFilesDrop }
onHTMLDrop={ this.onHTMLDrop }
onDrop={ this.onDrop }
/>
</MediaUploadCheck>
);
}
}
Expand Down
25 changes: 14 additions & 11 deletions packages/editor/src/components/media-upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ You can check how this component is implemented for the edit post page using `wp

## Usage

To make sure the current user has Upload permissions, you need to wrap the MediaUpload component into the MediaUploadCheck one.

```jsx
import { Button } from '@wordpress/components';
import { MediaUpload } from '@wordpress/editor';
import { MediaUpload, MediaUploadCheck } from '@wordpress/editor';

const ALLOWED_MEDIA_TYPES = [ 'audio' ];

function MyMediaUploader() {
return (
<MediaUpload
onSelect={ ( media ) => console.log( 'selected ' + media.length ) }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ mediaId }
render={ ( { open } ) => (
<Button onClick={ open }>
Open Media Library
</Button>
) }
/>
<MediaUploadCheck>
<MediaUpload
onSelect={ ( media ) => console.log( 'selected ' + media.length ) }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ mediaId }
render={ ( { open } ) => (
<Button onClick={ open }>
Open Media Library
</Button>
) }
/>
</MediaUploadCheck>
);
}
```
Expand Down