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

Update gallery markup for using list #3441

Closed
wants to merge 7 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/library/gallery/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class GalleryBlock extends Component {
/>
</InspectorControls>
),
<div key="gallery" className={ `${ className } align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` }>
<ul key="gallery" className={ `${ className } align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` }>
{ dropZone }
{ images.map( ( img, index ) => (
<GalleryImage
Expand All @@ -238,7 +238,7 @@ class GalleryBlock extends Component {
setAttributes={ ( attrs ) => this.setImageAttributes( index, attrs ) }
/>
) ) }
</div>,
</ul>,
];
}
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/gallery/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.blocks-gallery-image {
.blocks-gallery-item {
position: relative;

&.is-selected {
Expand Down
30 changes: 16 additions & 14 deletions blocks/library/gallery/gallery-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,28 @@ class GalleryImage extends Component {

const img = url ? <img src={ url } alt={ alt } data-id={ id } /> : <Spinner />;

const className = classnames( 'blocks-gallery-image', {
const className = classnames( 'blocks-gallery-item', {
'is-selected': isSelected,
} );

// Disable reason: Each block can be selected by clicking on it and we should keep the same saved markup
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
<figure className={ className } onClick={ onClick }>
{ isSelected &&
<div className="blocks-gallery-image__inline-menu">
<IconButton
icon="no-alt"
onClick={ onRemove }
className="blocks-gallery-image__remove"
label={ __( 'Remove Image' ) }
/>
</div>
}
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
<li className={ className } onClick={ onClick }>
<figure>
{ isSelected &&
<div className="blocks-gallery-image__inline-menu">
<IconButton
icon="no-alt"
onClick={ onRemove }
className="blocks-gallery-image__remove"
label={ __( 'Remove Image' ) }
/>
</div>
}
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
</li>
);
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
Expand Down
141 changes: 99 additions & 42 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,52 @@ import './style.scss';
import { registerBlockType, createBlock } from '../../api';
import { default as GalleryBlock, defaultColumnsNumber } from './block';

const galleryBlockAttributes = {
align: {
type: 'string',
default: 'none',
},
images: {
type: 'array',
default: [],
source: 'query',
selector: 'ul.wp-block-gallery .blocks-gallery-item img',
query: {
url: {
source: 'attribute',
attribute: 'src',
},
alt: {
source: 'attribute',
attribute: 'alt',
},
id: {
source: 'attribute',
attribute: 'data-id',
},
},
},
columns: {
type: 'number',
},
imageCrop: {
type: 'boolean',
default: true,
},
linkTo: {
type: 'string',
default: 'none',
},
};

registerBlockType( 'core/gallery', {
title: __( 'Gallery' ),
description: __( 'Image galleries are a great way to share groups of pictures on your site.' ),
icon: 'format-gallery',
category: 'common',
keywords: [ __( 'images' ), __( 'photos' ) ],

attributes: {
align: {
type: 'string',
default: 'none',
},
images: {
type: 'array',
default: [],
source: 'query',
selector: 'div.wp-block-gallery figure.blocks-gallery-image img',
query: {
url: {
source: 'attribute',
attribute: 'src',
},
alt: {
source: 'attribute',
attribute: 'alt',
},
id: {
source: 'attribute',
attribute: 'data-id',
},
},
},
columns: {
type: 'number',
},
imageCrop: {
type: 'boolean',
default: true,
},
linkTo: {
type: 'string',
default: 'none',
},
},
attributes: galleryBlockAttributes,

transforms: {
from: [
Expand Down Expand Up @@ -150,7 +152,7 @@ registerBlockType( 'core/gallery', {
save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
return (
<div className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
<ul className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( image ) => {
let href;

Expand All @@ -166,13 +168,68 @@ registerBlockType( 'core/gallery', {
const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } />;

return (
<figure key={ image.id || image.url } className="blocks-gallery-image">
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
<li key={ image.id || image.url } className="blocks-gallery-item">
<figure>
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
</li>
);
} ) }
</div>
</ul>
);
},

deprecated: [ {
attributes: {
...galleryBlockAttributes,
images: {
type: 'array',
default: [],
source: 'query',
selector: 'div.wp-block-gallery figure.blocks-gallery-image img',
query: {
url: {
source: 'attribute',
attribute: 'src',
},
alt: {
source: 'attribute',
attribute: 'alt',
},
id: {
source: 'attribute',
attribute: 'data-id',
},
},
},
},

save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
return (
<div className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( image ) => {
let href;

switch ( linkTo ) {
case 'media':
href = image.url;
break;
case 'attachment':
href = image.link;
break;
}

const img = <img src={ image.url } alt={ image.alt } data-id={ image.id } />;

return (
<figure key={ image.id || image.url } className="blocks-gallery-image">
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
);
} ) }
</div>
);
},
} ],
} );
56 changes: 20 additions & 36 deletions blocks/library/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,56 @@
.wp-block-gallery.aligncenter {
display: flex;
flex-wrap: wrap;
list-style-type: none;
margin-left: -16px;
margin-top: 0;

.blocks-gallery-image {
.blocks-gallery-item {
margin: 8px;
display: flex;
flex-grow: 1;
flex-direction: column;
justify-content: center;

figure {
height: 100%;
margin: 0;
}

img {
display: block;
max-width: 100%;
height: auto;
}
}

// Cropped
&.is-cropped .blocks-gallery-image {
&.is-cropped .blocks-gallery-item {
img {
flex: 1;
width: 100%;
height: 100%;
object-fit: cover;

}

// Alas, IE11+ doesn't support object-fit
_:-ms-lang(x), img {
_:-ms-lang(x), figure {
height: auto;
width: auto;
}
}

&.columns-1 figure {
width: calc(100% / 1 - 16px);
}
&.columns-2 figure {
// Responsive fallback value, 2 columns
& .blocks-gallery-item {
width: calc(100% / 2 - 16px);
}

// Responsive fallback value, 2 columns
&.columns-3 figure,
&.columns-4 figure,
&.columns-5 figure,
&.columns-6 figure,
&.columns-7 figure,
&.columns-8 figure {
width: calc(100% / 2 - 16px);
&.columns-1 .blocks-gallery-item {
width: calc(100% / 1 - 16px);
}

@include break-small {
&.columns-3 figure {
width: calc(100% / 3 - 16px);
}
&.columns-4 figure {
width: calc(100% / 4 - 16px);
}
&.columns-5 figure {
width: calc(100% / 5 - 16px);
}
&.columns-6 figure {
width: calc(100% / 6 - 16px);
}
&.columns-7 figure {
width: calc(100% / 7 - 16px);
}
&.columns-8 figure {
width: calc(100% / 8 - 16px);
@for $i from 3 through 8 {
&.columns-#{ $i } .blocks-gallery-item {
width: calc(100% / #{ $i } - 16px );
}
}
}
}
24 changes: 16 additions & 8 deletions blocks/test/fixtures/core__gallery.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<!-- wp:core/gallery -->
<div class="wp-block-gallery alignnone columns-2 is-cropped">
<figure class="blocks-gallery-image">
<img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" />
</figure>
<figure class="blocks-gallery-image">
<img src="http://google.com/hi.png" alt="title" />
</figure>
</div>
<<<<<<< HEAD
<ul class="wp-block-gallery alignnone columns-2 is-cropped">
=======
<ul class="wp-block-gallery">
>>>>>>> Change all classes to blocks-gallery-item, including json.
<li class="blocks-gallery-item">
<figure>
<img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" />
</figure>
</li>
<li class="blocks-gallery-item">
<figure>
<img src="http://google.com/hi.png" alt="title" />
</figure>
</li>
</ul>
<!-- /wp:core/gallery -->
6 changes: 5 additions & 1 deletion blocks/test/fixtures/core__gallery.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"imageCrop": true,
"linkTo": "none"
},
"originalContent": "<div class=\"wp-block-gallery alignnone columns-2 is-cropped\">\n\t<figure class=\"blocks-gallery-image\">\n\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t</figure>\n\t<figure class=\"blocks-gallery-image\">\n\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t</figure>\n</div>"
<<<<<<< HEAD
"originalContent": "<ul class=\"wp-block-gallery alignnone columns-2 is-cropped\">\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n</ul>"
=======
"originalContent": "<ul class=\"wp-block-gallery\">\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n</ul>"
>>>>>>> Change all classes to blocks-gallery-item, including json.
}
]
6 changes: 5 additions & 1 deletion blocks/test/fixtures/core__gallery.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"blockName": "core/gallery",
"attrs": null,
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-gallery alignnone columns-2 is-cropped\">\n\t<figure class=\"blocks-gallery-image\">\n\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t</figure>\n\t<figure class=\"blocks-gallery-image\">\n\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t</figure>\n</div>\n"
<<<<<<< HEAD
"innerHTML": "\n<ul class=\"wp-block-gallery alignnone columns-2 is-cropped\">\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n</ul>\n"
=======
"innerHTML": "\n<ul class=\"wp-block-gallery\">\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"https://cldup.com/uuUqE_dXzy.jpg\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n\t<li class=\"blocks-gallery-item\">\n\t\t<figure>\n\t\t\t<img src=\"http://google.com/hi.png\" alt=\"title\" />\n\t\t</figure>\n\t</li>\n</ul>\n"
>>>>>>> Change all classes to blocks-gallery-item, including json.
},
{
"attrs": {},
Expand Down
Loading