Skip to content

Commit

Permalink
Gallery Block: adding buttons to remove image from galleries inline
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 4, 2017
1 parent 51c2765 commit 2b73158
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
18 changes: 18 additions & 0 deletions blocks/library/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,21 @@
.editor-visual-editor__block[data-type="core/gallery"] .editor-visual-editor__block-edit {
overflow: hidden;
}

.blocks-gallery-image {
position: relative;
}

.blocks-gallery-image__remove {
position: absolute;
top: 5px;
right: 5px;
padding: 0;
border: 1px solid $light-gray-900;
background: $white;
opacity: 0.8;

&:hover {
opacity: 1;
}
}
13 changes: 13 additions & 0 deletions blocks/library/gallery/gallery-image.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress Dependencies
*/
import { IconButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function GalleryImage( props ) {
let href = null;
Expand All @@ -14,6 +19,14 @@ export default function GalleryImage( props ) {

return (
<figure className="blocks-gallery-image">
{ props.edit &&
<IconButton
icon="no-alt"
onClick={ props.onRemove }
className="blocks-gallery-image__remove"
label={ __( 'Remove Image' ) }
/>
}
{ href ? <a href={ href }>{ image }</a> : image }
</figure>
);
Expand Down
14 changes: 10 additions & 4 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External Dependencies
*/
import { filter } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -80,12 +85,13 @@ registerBlockType( 'core/gallery', {
const setColumnsNumber = ( value ) => setAttributes( { columns: value } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } );

const onSelectImages = ( imgs ) => setAttributes( { images: imgs } );

const uploadFromFiles = ( event ) => {
mediaUpload( event.target.files, setAttributes, isGallery );
};
const removeImage = ( index ) => () => {
setAttributes( { images: filter( images, ( img, i ) => index !== i ) } );
};

const controls = (
focus && (
Expand Down Expand Up @@ -179,8 +185,8 @@ registerBlockType( 'core/gallery', {
</InspectorControls>
),
<div key="gallery" className={ `${ className } align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` }>
{ images.map( ( img ) => (
<GalleryImage key={ img.url } img={ img } />
{ images.map( ( img, index ) => (
<GalleryImage key={ img.url } img={ img } edit onRemove={ removeImage( index ) } />
) ) }
</div>,
];
Expand Down

0 comments on commit 2b73158

Please sign in to comment.