Skip to content

Commit

Permalink
adds simple logic that keeps authored captions in galleries (#15004)
Browse files Browse the repository at this point in the history
* adds simple logic that keeps authored captions in galleries

* adds caption updating if changed in gallery, refactores some code

* refactor code so as to avoig adding data to the DOM and updated the fixtures

* updated new deprecated fixture

* removes manual fixture edits

* moves attachment captions to state

* fixed a problem caused by rebasing
  • Loading branch information
draganescu authored and gziolo committed Aug 29, 2019
1 parent 1254c27 commit 79eda1d
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 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 { every, filter, forEach, map } from 'lodash';
import { filter, forEach, map, find, every } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -63,6 +63,7 @@ class GalleryEdit extends Component {

this.state = {
selectedImage: null,
attachmentCaptions: null,
};
}

Expand Down Expand Up @@ -129,11 +130,46 @@ class GalleryEdit extends Component {
};
}

onSelectImages( images ) {
const { columns } = this.props.attributes;
selectCaption( newImage, images, attachmentCaptions ) {
const currentImage = find(
images, { id: newImage.id }
);

const currentImageCaption = currentImage ? currentImage.caption : newImage.caption;

if ( ! attachmentCaptions ) {
return currentImageCaption;
}

const attachment = find(
attachmentCaptions, { id: newImage.id }
);

// if the attachment caption is updated
if ( attachment && ( attachment.caption !== newImage.caption ) ) {
return newImage.caption;
}

return currentImageCaption;
}

onSelectImages( newImages ) {
const { columns, images } = this.props.attributes;
const { attachmentCaptions } = this.state;
this.setState(
{
attachmentCaptions: newImages.map( ( newImage ) => ( {
id: newImage.id,
caption: newImage.caption,
} ) ),
}
);
this.setAttributes( {
images: images.map( ( image ) => pickRelevantMediaFiles( image ) ),
columns: columns ? Math.min( images.length, columns ) : columns,
images: newImages.map( ( newImage ) => ( {
...pickRelevantMediaFiles( newImage ),
caption: this.selectCaption( newImage, images, attachmentCaptions ),
} ) ),
columns: columns ? Math.min( newImages.length, columns ) : columns,
} );
}

Expand Down

0 comments on commit 79eda1d

Please sign in to comment.