Skip to content

Commit

Permalink
Chrome: Adding the featured image panel
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 26, 2017
1 parent 484c146 commit 71f1d3a
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/spinner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const spinner = <span className="spinner is-active" />;

export default () => spinner;
146 changes: 146 additions & 0 deletions editor/sidebar/featured-image/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { Component } from 'element';
import { __ } from 'i18n';
import Button from 'components/button';
import PanelBody from 'components/panel/body';
import MediaUploadButton from 'blocks/media-upload-button';
import Spinner from 'components/spinner';

/**
* Internal dependencies
*/
import './style.scss';
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';

class FeaturedImage extends Component {
constructor() {
super( ...arguments );
this.state = {
media: null,
loading: false,
};

// This boolean should be removed when we provide QueryComponents
this.mounted = true;
}

componentDidMount() {
this.fetchMedia();
}

componentDidUpdate( prevProps ) {
if ( prevProps.featuredImageId !== this.props.featuredImageId ) {
this.fetchMedia();
}
}

componentWillUnmount() {
this.mounted = false;
}

fetchMedia() {
if ( ! this.props.featuredImageId ) {
this.setState( {
media: null,
loading: false,
} );
return;
}
this.setState( {
media: null,
loading: true,
} );
const mediaIdToLoad = this.props.featuredImageId;
new wp.api.models.Media( { id: mediaIdToLoad } ).fetch()
.done( ( media ) => {
if ( ! this.mounted || this.props.featuredImageId !== mediaIdToLoad ) {
return;
}
this.setState( {
loading: false,
media,
} );
} )
.fail( () => {
if ( ! this.mounted || this.props.featuredImageId !== mediaIdToLoad ) {
return;
}
this.setState( {
loading: false,
} );
} );
}

render() {
const { featuredImageId, onUpdateImage, onRemoveImage } = this.props;
const { media, loading } = this.state;

return (
<PanelBody title={ __( 'Featured image' ) } initialOpen={ false }>
<div className="editor-featured-image__content">
{ !! featuredImageId &&
<MediaUploadButton
buttonProps={ { className: 'button-link' } }
onSelect={ onUpdateImage }
type="image"
>
{ media &&
<img
className="editor-featured-image__preview"
src={ media.source_url }
alt={ __( 'Featured image' ) }
/>
}
{ loading && <Spinner /> }
</MediaUploadButton>
}
{ !! featuredImageId && media &&
<p className="editor-featured-image__howto">
{ __( 'Click the image to edit or update' ) }
</p>
}
{ ! featuredImageId &&
<MediaUploadButton
buttonProps={ { className: 'editor-featured-image__toggle button-link' } }
onSelect={ onUpdateImage }
type="image"
>
{ wp.i18n.__( 'Set featured image' ) }
</MediaUploadButton>
}
{ !! featuredImageId &&
<Button className="editor-featured-image__toggle button-link" onClick={ onRemoveImage }>
{ wp.i18n.__( 'Remove featured image' ) }
</Button>
}
</div>
</PanelBody>
);
}
}

export default connect(
( state ) => {
return {
featuredImageId: getEditedPostAttribute( state, 'featured_media' ),
};
},
( dispatch ) => {
return {
onUpdateImage( image ) {
dispatch( editPost( { featured_media: image.id } ) );
},
onRemoveImage() {
dispatch( editPost( { featured_media: null } ) );
},
};
}
)( FeaturedImage );
32 changes: 32 additions & 0 deletions editor/sidebar/featured-image/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.editor-featured-image__content {
padding: 10px 0 0;

.spinner {
margin: 0;
}
}

.editor-featured-image__toggle {
text-decoration: underline;
color: $blue-wordpress;

&:focus {
box-shadow: none;
outline: none;
}

&:hover {
color: $blue-medium;
}
}

.editor-featured-image__preview {
display: block;
max-width: 100%;
}

.editor-featured-image__howto {
color: $dark-gray-300;
font-style: italic;
margin: 10px 0;
}
2 changes: 2 additions & 0 deletions editor/sidebar/post-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import IconButton from 'components/icon-button';
import './style.scss';
import PostStatus from '../post-status';
import PostExcerpt from '../post-excerpt';
import FeaturedImage from '../featured-image';

const PostSettings = ( { toggleSidebar } ) => {
return (
Expand All @@ -32,6 +33,7 @@ const PostSettings = ( { toggleSidebar } ) => {
</PanelHeader>
<PostStatus />
<PostExcerpt />
<FeaturedImage />
</Panel>
);
};
Expand Down

0 comments on commit 71f1d3a

Please sign in to comment.