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

Image Block: Integrate with the media upload modal #802

Merged
merged 6 commits into from
May 17, 2017
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
13 changes: 10 additions & 3 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* WordPress dependencies
*/
import Button from 'components/button';
import Placeholder from 'components/placeholder';

/**
Expand All @@ -10,6 +9,7 @@ import Placeholder from 'components/placeholder';
import './style.scss';
import { registerBlock, query } from '../../api';
import Editable from '../../editable';
import MediaUploadButton from '../../media-upload-button';

const { attr, children } = query;

Expand Down Expand Up @@ -78,15 +78,22 @@ registerBlock( 'core/image', {
const { url, alt, caption } = attributes;

if ( ! url ) {
const uploadButtonProps = { isLarge: true };
const setMediaUrl = ( media ) => setAttributes( { url: media.url } );
return (
<Placeholder
instructions={ wp.i18n.__( 'Drag image here or insert from media library' ) }
icon="format-image"
label={ wp.i18n.__( 'Image' ) }
className="blocks-image">
<Button isLarge>
<MediaUploadButton
buttonProps={ uploadButtonProps }
onSelect={ setMediaUrl }
type="image"
auto-open
>
{ wp.i18n.__( 'Insert from Media Library' ) }
</Button>
</MediaUploadButton>
</Placeholder>
);
}
Expand Down
61 changes: 61 additions & 0 deletions blocks/media-upload-button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* WordPress dependencies
*/
import { Component } from 'element';
import { __ } from 'i18n';
import Button from 'components/button';

class MediaUploadButton extends Component {
constructor( { multiple = false, type } ) {
super( ...arguments );
this.openModal = this.openModal.bind( this );
this.onSelect = this.onSelect.bind( this );
const frameConfig = {
title: __( 'Select or Upload a media' ),
button: {
text: __( 'Select' ),
},
multiple,
};
if ( !! type ) {
frameConfig.library = { type };
}
this.frame = wp.media( frameConfig );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to clean up this frame when the component unmounts? Maybe via the view.remove() function?

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/js/media/views/view.js?rev=33337#L52


// When an image is selected in the media frame...
this.frame.on( 'select', this.onSelect );
}

componentDidMount() {
if ( !! this.props[ 'auto-open' ] ) {
this.frame.open();
}
}

componentWillUnmount() {
this.frame.remove();
}

onSelect() {
const { onSelect, multiple = false } = this.props;
// Get media attachment details from the frame state
const attachment = this.frame.state().get( 'selection' ).toJSON();
onSelect( multiple ? attachment : attachment[ 0 ] );
}

openModal() {
this.frame.open();
}

render() {
const { children, buttonProps } = this.props;

return (
<Button onClick={ this.openModal } { ...buttonProps }>
{ children }
</Button>
);
}
}

export default MediaUploadButton;
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function gutenberg_scripts_and_styles( $hook ) {
/**
* Scripts
*/
wp_enqueue_media();

// The editor code itself.
wp_enqueue_script(
Expand Down