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

Fix: Image using an external source don't transform to a gallery #16548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/block-library/src/gallery/edit.js
Expand Up @@ -2,7 +2,14 @@
* External dependencies
*/
import classnames from 'classnames';
import { filter, forEach, map, find, every } from 'lodash';
import {
every,
filter,
find,
forEach,
map,
some,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -234,14 +241,26 @@ class GalleryEdit extends Component {
}

render() {
const { attributes, isSelected, className, noticeUI } = this.props;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
const {
attributes,
className,
isSelected,
noticeUI,
} = this.props;
const {
align,
columns = defaultColumnsNumber( attributes ),
imageCrop,
images,
linkTo,
} = attributes;

const hasImages = !! images.length;
const hasImagesWithId = hasImages && some( images, ( { id } ) => id );

const mediaPlaceholder = (
<MediaPlaceholder
addToGallery={ hasImages }
addToGallery={ hasImagesWithId }
isAppender={ hasImages }
className={ className }
dropZoneUIOnly={ hasImages && ! isSelected }
Expand All @@ -254,7 +273,7 @@ class GalleryEdit extends Component {
accept="image/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
multiple
value={ hasImages ? images : undefined }
value={ hasImagesWithId ? images : undefined }
onError={ this.onUploadError }
notices={ hasImages ? undefined : noticeUI }
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/gallery/transforms.js
Expand Up @@ -36,7 +36,7 @@ const transforms = {
// Loop through all the images and check if they have the same align.
align = every( attributes, [ 'align', align ] ) ? align : undefined;

const validImages = filter( attributes, ( { id, url } ) => id && url );
const validImages = filter( attributes, ( { url } ) => url );

return createBlock( 'core/gallery', {
images: validImages.map( ( { id, url, alt, caption } ) => ( {
Expand Down
Expand Up @@ -91,8 +91,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand Down Expand Up @@ -127,8 +127,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__attachment-link into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand All @@ -155,8 +155,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__center-caption into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[],\\"align\\":\\"center\\"} -->
<ul class=\\"wp-block-gallery aligncenter columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null],\\"align\\":\\"center\\"} -->
<ul class=\\"wp-block-gallery aligncenter columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/><figcaption>Give it a try. Press the \\"really wide\\" button on the image toolbar.</figcaption></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand All @@ -183,8 +183,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__custom-link into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand All @@ -211,8 +211,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__custom-link-class into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand All @@ -239,8 +239,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__custom-link-rel into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand All @@ -267,8 +267,8 @@ exports[`Block transforms correctly transform block Image in fixture core__image
`;

exports[`Block transforms correctly transform block Image in fixture core__image__media-link into the Gallery block 1`] = `
"<!-- wp:gallery {\\"ids\\":[]} -->
<ul class=\\"wp-block-gallery columns-0 is-cropped\\"></ul>
"<!-- wp:gallery {\\"ids\\":[null]} -->
<ul class=\\"wp-block-gallery columns-1 is-cropped\\"><li class=\\"blocks-gallery-item\\"><figure><img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\\" alt=\\"\\"/></figure></li></ul>
<!-- /wp:gallery -->"
`;

Expand Down