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 Apr 19, 2019
1 parent 91bcef3 commit 16efe35
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, pick, map, get } from 'lodash';
import { filter, pick, map, get, groupBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -105,21 +105,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 16efe35

Please sign in to comment.