Skip to content

Commit

Permalink
adds caption updating if changed in gallery, refactores some code
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed May 3, 2019
1 parent dc4f03e commit 66c5bb3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"type": "string",
"source": "html",
"selector": "figcaption"
},
"attachmentCaption": {
"type": "string"
}
}
},
Expand Down
32 changes: 20 additions & 12 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { filter, map } from 'lodash';
import { filter, map, get, groupBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -96,21 +96,29 @@ class GalleryEdit extends Component {
};
}

selectCaption( gallery, attachment ) {
const savedAttachmentCaption = get(
gallery, [ attachment.id, 0, 'attachmentCaption' ], ''
);
if ( savedAttachmentCaption !== attachment.caption ) {
return attachment.caption;
}
return get(
gallery, [ attachment.id, 0, 'caption' ], attachment.caption
);
}

onSelectImages( newImages ) {
const { columns, images } = this.props.attributes;

const galleryImages = groupBy( images, 'id' );

this.setAttributes( {
images: newImages.map( ( image ) => {
const newImage = pickRelevantMediaFiles( image );
let oldImage = filter( images, { id: newImage.id } );
if ( oldImage.length > 0 ) {
oldImage = oldImage.reduce( ( img ) => img );
if ( oldImage.caption !== '' ) {
newImage.caption = oldImage.caption;
}
}
return newImage;
} ),
images: newImages.map( ( attachment ) => ( {
...pickRelevantMediaFiles( attachment ),
caption: this.selectCaption( galleryImages, attachment ),
attachmentCaption: attachment.caption,
} ) ),
columns: columns ? Math.min( newImages.length, columns ) : columns,
} );
}
Expand Down

0 comments on commit 66c5bb3

Please sign in to comment.