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

Implement nesting in cover image block #5452

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions blocks/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import { withContext } from '@wordpress/components';

function InnerBlocks( { BlockList, layouts } ) {
return <BlockList layouts={ layouts } />;
function InnerBlocks( { BlockList, layouts, allowedBlocks, template } ) {
return <BlockList { ...{ layouts, allowedBlocks, template } } />;
}

InnerBlocks = withContext( 'BlockList' )()( InnerBlocks );
Expand Down
46 changes: 44 additions & 2 deletions core-blocks/cover-image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,52 @@
}

&.has-left-content .block-rich-text__inline-toolbar {
justify-content: flex-start;
display: inline-block;
}

&.has-right-content .block-rich-text__inline-toolbar{
justify-content: flex-end;
display: inline-block;
}

.editor-block-list__layout {
width: 100%;
}

.wp-block-cover-image__inner-container {
width: calc( 100% - 70px );
margin-left: auto;
margin-right: auto;
// avoid text align inherit from cover image align.
text-align: left;

.editor-inserter-with-shortcuts .components-icon-button,
.editor-block-list__empty-block-inserter .components-icon-button {
color: $white;
}
.editor-default-block-appender {
&:hover {
.components-icon-button {
color: $white;
}
}

.components-icon-button {
color: $light-gray-900;
}
}


.editor-block-list__insertion-point-inserter:before {
background: $white;
}

p.wp-block-subhead {
color: $light-gray-100;
}

.components-draggable__clone > .editor-block-list__block > .editor-block-list__block-draggable,
.editor-block-list__block-draggable > .editor-block-list__block-draggable-inner {
background-color: rgba( $white, 0.3 );
}
}
}
114 changes: 70 additions & 44 deletions core-blocks/cover-image/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -17,8 +17,8 @@ import {
BlockAlignmentToolbar,
ImagePlaceholder,
MediaUpload,
AlignmentToolbar,
RichText,
InnerBlocks,
} from '@wordpress/blocks';

/**
Expand All @@ -30,21 +30,12 @@ import './style.scss';
const validAlignments = [ 'left', 'center', 'right', 'wide', 'full' ];

const blockAttributes = {
title: {
type: 'array',
source: 'children',
selector: 'p',
},
url: {
type: 'string',
},
align: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
Expand Down Expand Up @@ -99,8 +90,8 @@ export const settings = {
}
},

edit( { attributes, setAttributes, isSelected, className } ) {
const { url, title, align, contentAlign, id, hasParallax, dimRatio } = attributes;
edit( { attributes, setAttributes, className } ) {
const { align, url, id, hasParallax, dimRatio } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSelectImage = ( media ) => setAttributes( { url: media.url, id: media.id } );
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
Expand All @@ -109,7 +100,6 @@ export const settings = {
const style = backgroundImageStyles( url );
const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
Expand All @@ -124,12 +114,6 @@ export const settings = {
value={ align }
onChange={ updateAlignment }
/>
<AlignmentToolbar
value={ contentAlign }
onChange={ ( nextAlign ) => {
setAttributes( { contentAlign: nextAlign } );
} }
/>
<Toolbar>
<MediaUpload
onSelect={ onSelectImage }
Expand Down Expand Up @@ -169,16 +153,8 @@ export const settings = {
);

if ( ! url ) {
const hasTitle = ! isEmpty( title );
const icon = hasTitle ? undefined : 'format-image';
const label = hasTitle ? (
<RichText
tagName="h2"
value={ title }
onChange={ ( value ) => setAttributes( { title: value } ) }
inlineToolbar
/>
) : __( 'Cover Image' );
const icon = 'format-image';
const label = __( 'Cover Image' );

return (
<Fragment>
Expand All @@ -198,45 +174,95 @@ export const settings = {
style={ style }
className={ classes }
>
{ title || isSelected ? (
<RichText
tagName="p"
className="wp-block-cover-image-text"
placeholder={ __( 'Write title…' ) }
value={ title }
onChange={ ( value ) => setAttributes( { title: value } ) }
inlineToolbar
<div className="wp-block-cover-image__inner-container">
<InnerBlocks
template={ [
[ 'core/paragraph', {
align: 'center',
fontSize: 'large',
placeholder: __( 'Write title…' ),
textColor: '#fff',
} ],
] }
allowedBlocks={ [ 'core/button', 'core/heading', 'core/paragraph', 'core/subhead' ] }
/>
) : null }
</div>
</div>
</Fragment>
);
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
const { url, hasParallax, dimRatio, align } = attributes;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ `has-${ contentAlign }-content` ]: contentAlign !== 'center',
},
align ? `align${ align }` : null,
);

return (
<div className={ classes } style={ style }>
{ title && title.length > 0 && (
<RichText.Content tagName="p" className="wp-block-cover-image-text" value={ title } />
) }
<div className="wp-block-cover-image__inner-container">
<InnerBlocks.Content />
</div>
</div>
);
},

deprecated: [ {
attributes: {
...blockAttributes,
title: {
type: 'array',
source: 'children',
selector: 'p',
},
contentAlign: {
type: 'string',
default: 'center',
},
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ `has-${ contentAlign }-content` ]: contentAlign !== 'center',
},
align ? `align${ align }` : null,
);

return (
<div className={ classes } style={ style }>
{ title && title.length > 0 && (
<RichText.Content tagName="p" className="wp-block-cover-image-text" value={ title } />
) }
</div>
);
},

migrate( attributes ) {
return [
omit( attributes, [ 'title', 'contentAlign' ] ),
[ createBlock( 'core/paragraph', {
content: attributes.title,
align: attributes.contentAlign,
fontSize: 'large',
placeholder: __( 'Write title…' ),
} ) ],
];
},
}, {
attributes: {
...blockAttributes,
title: {
Expand Down
14 changes: 14 additions & 0 deletions core-blocks/cover-image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@
max-width: $content-width / 2;
width: 100%;
}

.wp-block-cover-image__inner-container {
width: calc( 100% - 70px );
color: $light-gray-100;
z-index: z-index( '.wp-block-cover-image__inner-container' );
p {
margin-top: 0;
margin-bottom: 0;
}
}

h1, h2, h3, h4, h5, h6, .wp-block-subhead {
color: inherit;
}
}
12 changes: 8 additions & 4 deletions core-blocks/test/fixtures/core__cover-image.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<div class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
<!-- wp:cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","id":8398,"dimRatio": 40} -->
<div class="wp-block-cover-image has-background-dim has-background-dim-40" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<div class="wp-block-cover-image__inner-container">
<!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p style="text-align:center" class="is-large-text">Paragraph 1</p>
<!-- /wp:paragraph -->
</div>
</div>
<!-- /wp:core/cover-image -->
<!-- /wp:cover-image -->
26 changes: 20 additions & 6 deletions core-blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@
"name": "core/cover-image",
"isValid": true,
"attributes": {
"title": [
"Guten Berg!"
],
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"contentAlign": "center",
"id": 8398,
"hasParallax": false,
"dimRatio": 40
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <p class=\"wp-block-cover-image-text\">Guten Berg!</p>\n</div>"
"innerBlocks": [
{
"uid": "_uid_0",
"name": "core/paragraph",
"isValid": true,
"attributes": {
"content": [
"Paragraph 1"
],
"align": "center",
"dropCap": false,
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": [],
"originalContent": "<p style=\"text-align:center\" class=\"is-large-text\">Paragraph 1</p>"
}
],
"originalContent": "<div class=\"wp-block-cover-image has-background-dim has-background-dim-40\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <div class=\"wp-block-cover-image__inner-container\">\n\t\t\n </div>\n</div>"
}
]
16 changes: 14 additions & 2 deletions core-blocks/test/fixtures/core__cover-image.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
"blockName": "core/cover-image",
"attrs": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"id": 8398,
"dimRatio": 40
},
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <p class=\"wp-block-cover-image-text\">Guten Berg!</p>\n</div>\n"
"innerBlocks": [
{
"blockName": "core/paragraph",
"attrs": {
"align": "center",
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": [],
"innerHTML": "\n\t\t<p style=\"text-align:center\" class=\"is-large-text\">Paragraph 1</p>\n\t\t"
}
],
"innerHTML": "\n<div class=\"wp-block-cover-image has-background-dim has-background-dim-40\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <div class=\"wp-block-cover-image__inner-container\">\n\t\t\n </div>\n</div>\n"
},
{
"attrs": {},
Expand Down
8 changes: 6 additions & 2 deletions core-blocks/test/fixtures/core__cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- wp:cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<!-- wp:cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","id":8398,"dimRatio":40} -->
<div class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
<div class="wp-block-cover-image__inner-container">
<!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p style="text-align:center" class="is-large-text">Paragraph 1</p>
<!-- /wp:paragraph -->
</div>
</div>
<!-- /wp:cover-image -->
2 changes: 2 additions & 0 deletions edit-post/assets/stylesheets/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ $z-layers: (
'.components-autocomplete__results': 1000000,

'.skip-to-selected-block': 100000,

'.wp-block-cover-image__inner-container': 0,
);

@function z-index( $key ) {
Expand Down
2 changes: 1 addition & 1 deletion editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export class BlockListBlock extends Component {
{ showSideInserter && (
<Fragment>
<div className="editor-block-list__side-inserter">
<InserterWithShortcuts uid={ uid } layout={ layout } onToggle={ this.selectOnOpen } />
<InserterWithShortcuts uid={ uid } rootUID={ rootUID } layout={ layout } onToggle={ this.selectOnOpen } />
</div>
<div className="editor-block-list__empty-block-inserter">
<Inserter
Expand Down
Loading