-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chrome: Adding the featured image panel
- Loading branch information
1 parent
f1d1296
commit b19a55e
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const spinner = <span className="spinner is-active" />; | ||
|
||
export default () => spinner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters