Skip to content

Commit

Permalink
Add slimImageObjects method to reduce images to just attributes we need
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaz committed Jun 30, 2017
1 parent 82eebe3 commit 118db19
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ const editMediaLibrary = ( attributes, setAttributes ) => {
editFrame.open( 'gutenberg-gallery' );
};

// the media library image object contains numerous attributes
// we only need this set to display the image in the library
const slimImageObjects = ( imgs ) => {
const attrSet = [ 'sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt' ];
const newImgs = [];
imgs.forEach( ( img ) => {
newImgs.push( _.pick( img, attrSet ) );
} );
return newImgs;
};

function defaultColumnsNumber( attributes ) {
attributes.images = attributes.images || [];
return Math.min( 3, attributes.images.length );
Expand All @@ -59,14 +70,6 @@ registerBlockType( 'core/gallery', {
icon: 'format-gallery',
category: 'common',

attributes: {
images:
query( 'div.wp-block-gallery figure.blocks-gallery-image img', {
url: attr( 'src' ),
alt: attr( 'alt' ),
} ) || [],
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand Down Expand Up @@ -101,7 +104,7 @@ registerBlockType( 'core/gallery', {
);

if ( images.length === 0 ) {
const setMediaUrl = ( imgs ) => setAttributes( { images: imgs } );
const setMediaUrl = ( imgs ) => setAttributes( { images: slimImageObjects( imgs ) } );
const uploadButtonProps = { isLarge: true };

return [
Expand Down

0 comments on commit 118db19

Please sign in to comment.