Skip to content

Commit

Permalink
Add image count so we can work out default columns as innerBlocks not…
Browse files Browse the repository at this point in the history
… available at point of block validation
  • Loading branch information
Glen Davies committed Nov 4, 2020
1 parent 1534386 commit 38ab027
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"isListItem": {
"type": "boolean",
"default": true
},
"imageCount": {
"type": "number",
"default": 0
}
},
"providesContext": {
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ function GalleryEdit( props ) {
} = props;

const {
imageCount,
linkTarget,
linkTo,
columns = defaultColumnsNumber( images ),
columns = defaultColumnsNumber( imageCount ),
sizeSlug,
imageUploads,
imageCrop,
Expand Down Expand Up @@ -122,6 +123,12 @@ function GalleryEdit( props ) {
return newImages;
} );

useEffect( () => {
if ( images.length !== imageCount ) {
setAttributes( { imageCount: images.length } );
}
}, [ images ] );

const imageSizeOptions = useImageSizes( images, isSelected, getSettings );

const { replaceInnerBlocks, updateBlockAttributes } = useDispatch(
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/gallery/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export const Gallery = ( props ) => {
mediaPlaceholder,
insertBlocksAfter,
blockProps,
images,
} = props;

const {
imageCount,
align,
columns = defaultColumnsNumber( images ),
columns = defaultColumnsNumber( imageCount ),
caption,
imageCrop,
} = attributes;
Expand Down
10 changes: 3 additions & 7 deletions packages/block-library/src/gallery/save.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -13,9 +8,10 @@ import { RichText, useBlockProps, InnerBlocks } from '@wordpress/block-editor';
*/
import { defaultColumnsNumber } from './shared';

export default function save( { attributes, innerBlocks } ) {
export default function save( { attributes } ) {
const {
columns = defaultColumnsNumber( innerBlocks ),
imageCount,
columns = defaultColumnsNumber( imageCount ),
imageCrop,
caption,
} = attributes;
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/gallery/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { get, pick } from 'lodash';
*/
import { __ } from '@wordpress/i18n';

export function defaultColumnsNumber( images ) {
return images?.length ? Math.min( 3, images.length ) : 3;
export function defaultColumnsNumber( imageCount ) {
return imageCount ? Math.min( 3, imageCount ) : 3;
}

export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => {
Expand Down

0 comments on commit 38ab027

Please sign in to comment.