Skip to content

Commit

Permalink
Bring branch again.
Browse files Browse the repository at this point in the history
  • Loading branch information
samikeijonen committed Feb 22, 2018
1 parent 38be3c9 commit 77cd75d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 60 deletions.
2 changes: 2 additions & 0 deletions blocks/library/cover-image/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.wp-block-cover-image {
margin: 0;

.blocks-rich-text__tinymce[data-is-empty="true"]:before {
position: inherit;
}
Expand Down
117 changes: 77 additions & 40 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ import InspectorControls from '../../inspector-controls';

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',
},
hasParallax: {
type: 'boolean',
default: false,
},
dimRatio: {
type: 'number',
default: 50,
},
};

export const name = 'core/cover-image';

export const settings = {
Expand All @@ -37,34 +66,7 @@ export const settings = {

category: 'common',

attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
url: {
type: 'string',
},
align: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
hasParallax: {
type: 'boolean',
default: false,
},
dimRatio: {
type: 'number',
default: 50,
},
},
attributes: blockAttributes,

transforms: {
from: [
Expand Down Expand Up @@ -101,9 +103,7 @@ export const settings = {
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } );

const style = url ?
{ backgroundImage: `url(${ url })` } :
undefined;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
Expand Down Expand Up @@ -190,33 +190,33 @@ export const settings = {

return [
controls,
<section
<div
key="preview"
data-url={ url }
style={ style }
className={ classes }
>
{ title || isSelected ? (
<RichText
tagName="h2"
tagName="p"
className="wp-block-cover-image-text"
placeholder={ __( 'Write title…' ) }
value={ title }
onChange={ ( value ) => setAttributes( { title: value } ) }
isSelected={ isSelected }
inlineToolbar
/>
) : null }
</section>,
</div>,
];
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
const style = url ?
{ backgroundImage: `url(${ url })` } :
undefined;
const style = backgroundImageStyles( url );
const classes = classnames(
className,
align ? `align${ align }` : null,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
Expand All @@ -227,15 +227,52 @@ export const settings = {
);

return (
<section className={ classes } style={ style }>
<h2>{ title }</h2>
</section>
<div className={ classes } style={ style }>
<p className="wp-block-cover-image-text">{ title }</p>
</div>
);
},

deprecated: [ {
attributes: {
...blockAttributes,
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
},

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

return (
<section className={ classes } style={ style }>
<h2>{ title }</h2>
</section>
);
},
} ],
};

function dimRatioToClass( ratio ) {
return ( ratio === 0 || ratio === 50 ) ?
null :
'has-background-dim-' + ( 10 * Math.round( ratio / 10 ) );
}

function backgroundImageStyles( url ) {
return url ?
{ backgroundImage: `url(${ url })` } :
undefined;
}
22 changes: 14 additions & 8 deletions blocks/library/cover-image/style.scss
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
.wp-block-cover-image {
position: relative;
background-size: cover;
height: 430px;
min-height: 430px;
width: 100%;
margin: 0;
margin: 0 0 1.5em 0;
display: flex;
justify-content: center;
align-items: center;

&.has-left-content {
justify-content: flex-start;
h2 {

h2,
.wp-block-cover-image-text {
margin-left: 0;
text-align: left;
}
}

&.has-right-content {
justify-content: flex-end;
h2 {

h2,
.wp-block-cover-image-text {
margin-right: 0;
text-align: right;
}
}

h2 {
h2,
.wp-block-cover-image-text {
color: white;
font-size: 24pt;
line-height: 1em;
font-size: 2em;
line-height: 1.25;
z-index: 1;
margin-bottom: 0;
max-width: $visual-editor-max-width;
padding: $block-padding;
text-align: center;
Expand All @@ -45,7 +51,7 @@
left: 0;
bottom: 0;
right: 0;
background: rgba( black, 0.5 );
background-color: rgba( black, 0.5 );
}

@for $i from 1 through 10 {
Expand Down
8 changes: 4 additions & 4 deletions blocks/test/fixtures/core__cover-image.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<section class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<h2>Guten Berg!</h2>
</section>
<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg"} -->
<div class="wp-block-cover-image has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
</div>
<!-- /wp:core/cover-image -->
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"dimRatio": 40
},
"innerBlocks": [],
"originalContent": "<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <h2>Guten Berg!</h2>\n</section>"
"originalContent": "<div class=\"wp-block-cover-image 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>"
}
]
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__cover-image.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"blockName": "core/cover-image",
"attrs": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"dimRatio": 40
"dimRatio": 50
},
"innerBlocks": [],
"innerHTML": "\n<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <h2>Guten Berg!</h2>\n</section>\n"
"innerHTML": "\n<div class=\"wp-block-cover-image 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"
},
{
"attrs": {},
Expand Down
8 changes: 4 additions & 4 deletions blocks/test/fixtures/core__cover-image.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} -->
<section class="wp-block-cover-image has-background-dim-40 has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<h2>Guten Berg!</h2>
</section>
<!-- wp:cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg"} -->
<div class="wp-block-cover-image has-background-dim" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)">
<p class="wp-block-cover-image-text">Guten Berg!</p>
</div>
<!-- /wp:cover-image -->
2 changes: 1 addition & 1 deletion post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ window._wpGutenbergPost.title = {
window._wpGutenbergPost.content = {
raw: [
'<!-- wp:cover-image {"url":"https://cldup.com/Fz-ASbo2s3.jpg","align":"wide"} -->',
'<section class="wp-block-cover-image has-background-dim alignwide" style="background-image:url(https://cldup.com/Fz-ASbo2s3.jpg)"><h2>Of Mountains &amp; Printing Presses</h2></section>',
'<div class="wp-block-cover-image has-background-dim alignwide" style="background-image:url(https://cldup.com/Fz-ASbo2s3.jpg)"><p>Of Mountains &amp; Printing Presses</p></div>',
'<!-- /wp:cover-image -->',

'<!-- wp:paragraph -->',
Expand Down

0 comments on commit 77cd75d

Please sign in to comment.