-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add Cover block; Deprecate cover image; #10639
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,247 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
IconButton, | ||
PanelBody, | ||
RangeControl, | ||
ToggleControl, | ||
Toolbar, | ||
withNotices, | ||
} from '@wordpress/components'; | ||
import { Fragment, Component } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { compose } from '@wordpress/compose'; | ||
import { | ||
BlockControls, | ||
InspectorControls, | ||
InnerBlocks, | ||
MediaPlaceholder, | ||
MediaUpload, | ||
PanelColorSettings, | ||
withColors, | ||
} from '@wordpress/editor'; | ||
|
||
const INNER_BLOCKS_TEMPLATE = [ | ||
[ 'core/paragraph', { | ||
align: 'center', | ||
fontSize: 'large', | ||
placeholder: __( 'Write title…' ), | ||
} ], | ||
]; | ||
const INNER_BLOCKS_ALLOWED_BLOCKS = [ | ||
'core/button', | ||
'core/heading', | ||
'core/paragraph', | ||
]; | ||
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ]; | ||
export const IMAGE_BACKGROUND_TYPE = 'image'; | ||
export const VIDEO_BACKGROUND_TYPE = 'video'; | ||
|
||
class CoverEdit extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.onSelectMedia = this.onSelectMedia.bind( this ); | ||
} | ||
|
||
onSelectMedia( media ) { | ||
const { setAttributes } = this.props; | ||
if ( ! media || ! media.url ) { | ||
setAttributes( { url: undefined, id: undefined } ); | ||
return; | ||
} | ||
let mediaType; | ||
// for media selections originated from a file upload. | ||
if ( media.media_type ) { | ||
if ( media.media_type === IMAGE_BACKGROUND_TYPE ) { | ||
mediaType = IMAGE_BACKGROUND_TYPE; | ||
} else { | ||
// only images and videos are accepted so if the media_type is not an image we can assume it is a video. | ||
// video contain the media type of 'file' in the object returned from the rest api. | ||
mediaType = VIDEO_BACKGROUND_TYPE; | ||
} | ||
} else { // for media selections originated from existing files in the media library. | ||
mediaType = media.type; | ||
} | ||
if ( mediaType ) { | ||
setAttributes( { | ||
url: media.url, | ||
id: media.id, | ||
backgroundType: mediaType, | ||
} ); | ||
return; | ||
} | ||
setAttributes( { url: media.url, id: media.id } ); | ||
} | ||
|
||
render() { | ||
const { | ||
attributes, | ||
className, | ||
noticeOperations, | ||
noticeUI, | ||
overlayColor, | ||
setAttributes, | ||
setOverlayColor, | ||
} = this.props; | ||
|
||
const { | ||
backgroundType, | ||
dimRatio, | ||
hasParallax, | ||
id, | ||
url, | ||
} = attributes; | ||
|
||
const toggleParallax = () => setAttributes( { | ||
hasParallax: ! hasParallax, | ||
} ); | ||
const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } ); | ||
|
||
const style = { | ||
...( | ||
backgroundType === IMAGE_BACKGROUND_TYPE ? | ||
backgroundImageStyles( url ) : | ||
{} | ||
), | ||
backgroundColor: overlayColor.color, | ||
}; | ||
|
||
const classes = classnames( | ||
className, | ||
dimRatioToClass( dimRatio ), | ||
{ | ||
'has-background-dim': dimRatio !== 0, | ||
'has-parallax': hasParallax, | ||
} | ||
); | ||
|
||
const controls = ( | ||
<Fragment> | ||
{ !! url && ( | ||
<BlockControls> | ||
<Toolbar> | ||
<MediaUpload | ||
onSelect={ this.onSelectMedia } | ||
allowedTypes={ ALLOWED_MEDIA_TYPES } | ||
value={ id } | ||
render={ ( { open } ) => ( | ||
<IconButton | ||
className="components-toolbar__control" | ||
label={ __( 'Edit media' ) } | ||
icon="edit" | ||
onClick={ open } | ||
/> | ||
) } | ||
/> | ||
</Toolbar> | ||
</BlockControls> | ||
) } | ||
{ !! url && ( | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Cover Settings' ) }> | ||
{ IMAGE_BACKGROUND_TYPE === backgroundType && ( | ||
<ToggleControl | ||
label={ __( 'Fixed Background' ) } | ||
checked={ hasParallax } | ||
onChange={ toggleParallax } | ||
/> | ||
) } | ||
<PanelColorSettings | ||
title={ __( 'Overlay' ) } | ||
initialOpen={ true } | ||
colorSettings={ [ { | ||
value: overlayColor.color, | ||
onChange: setOverlayColor, | ||
label: __( 'Overlay Color' ), | ||
} ] } | ||
> | ||
<RangeControl | ||
label={ __( 'Background Opacity' ) } | ||
value={ dimRatio } | ||
onChange={ setDimRatio } | ||
min={ 0 } | ||
max={ 100 } | ||
step={ 10 } | ||
/> | ||
</PanelColorSettings> | ||
</PanelBody> | ||
</InspectorControls> | ||
) } | ||
</Fragment> | ||
); | ||
|
||
if ( ! url ) { | ||
const icon = 'format-image'; | ||
const label = ( 'Cover' ); | ||
|
||
return ( | ||
<Fragment> | ||
{ controls } | ||
<MediaPlaceholder | ||
icon={ icon } | ||
className={ className } | ||
labels={ { | ||
title: label, | ||
name: __( 'an image or a video' ), | ||
} } | ||
onSelect={ this.onSelectMedia } | ||
accept="image/*,video/*" | ||
allowedTypes={ ALLOWED_MEDIA_TYPES } | ||
notices={ noticeUI } | ||
onError={ noticeOperations.createErrorNotice } | ||
/> | ||
</Fragment> | ||
); | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
{ controls } | ||
<div | ||
data-url={ url } | ||
style={ style } | ||
className={ classes } | ||
> | ||
{ VIDEO_BACKGROUND_TYPE === backgroundType && url && ( | ||
<video | ||
className="wp-block-cover__video-background" | ||
autoPlay | ||
muted | ||
loop | ||
src={ url } | ||
/> | ||
) } | ||
<div className="wp-block-cover__inner-container"> | ||
<InnerBlocks | ||
template={ INNER_BLOCKS_TEMPLATE } | ||
allowedBlocks={ INNER_BLOCKS_ALLOWED_BLOCKS } | ||
/> | ||
</div> | ||
</div> | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withColors( { overlayColor: 'background-color' } ), | ||
withNotices, | ||
] )( CoverEdit ); | ||
|
||
export function dimRatioToClass( ratio ) { | ||
return ( ratio === 0 || ratio === 50 ) ? | ||
null : | ||
'has-background-dim-' + ( 10 * Math.round( ratio / 10 ) ); | ||
} | ||
|
||
export function backgroundImageStyles( url ) { | ||
return url ? | ||
{ backgroundImage: `url(${ url })` } : | ||
{}; | ||
} |
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 @@ | ||
.wp-block-cover__inner-container .editor-block-list__block { | ||
color: inherit; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this constant use CAPS_CASE like the constants in
edit.js
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, I just noticed this is the deprecated block. Well, I guess the point still applies, but it isn't really important. 😛