Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery: Add link to option #2164

Merged
merged 12 commits into from
Aug 7, 2017
14 changes: 13 additions & 1 deletion blocks/library/gallery/gallery-image.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

export default function GalleryImage( props ) {
let href = null;
switch ( props.linkto ) {
case 'media':
href = props.img.url;
break;
case 'attacment':
href = props.img.link;
break;
}

const image = <img src={ props.img.url } alt={ props.img.alt } />;

return (
<figure className="blocks-gallery-image">
<img src={ props.img.url } alt={ props.img.alt } />
{ href ? <a href={ href }>{ image }</a> : image }
</figure>
);
}
18 changes: 16 additions & 2 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ import MediaUploadButton from '../../media-upload-button';
import InspectorControls from '../../inspector-controls';
import RangeControl from '../../inspector-controls/range-control';
import ToggleControl from '../../inspector-controls/toggle-control';
import SelectControl from '../../inspector-controls/select-control';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import GalleryImage from './gallery-image';
import BlockDescription from '../../block-description';

const MAX_COLUMNS = 8;
const linkOptions = [
{ value: 'attachment', label: __( 'Attachment Page' ) },
{ value: 'media', label: __( 'Media File' ) },
{ value: 'none', label: __( 'None' ) },
];

const editMediaLibrary = ( attributes, setAttributes ) => {
const frameConfig = {
Expand Down Expand Up @@ -71,9 +77,11 @@ registerBlockType( 'core/gallery', {

edit( { attributes, setAttributes, focus, className } ) {
const { images = [], columns = defaultColumnsNumber( attributes ), align = 'none' } = attributes;
const setLinkto = ( value ) => setAttributes( { linkto: value } );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny thing, but probably should camel case the linkTo :)

const setColumnsNumber = ( event ) => setAttributes( { columns: event.target.value } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const { imageCrop = true } = attributes;
const { linkto = 'none' } = attributes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be moved to default attrs:

	defaultAttributes: {
		dropCap: false,
	},

const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } );

const controls = (
Expand Down Expand Up @@ -127,6 +135,12 @@ registerBlockType( 'core/gallery', {
<p>{ __( 'Image galleries are a great way to share groups of pictures on your site.' ) }</p>
</BlockDescription>
<h3>{ __( 'Gallery Settings' ) }</h3>
<SelectControl
label={ __( 'Link to' ) }
selected={ linkto }
onChange={ setLinkto }
options={ linkOptions }
/>
<RangeControl
label={ __( 'Columns' ) }
value={ columns }
Expand All @@ -150,11 +164,11 @@ registerBlockType( 'core/gallery', {
},

save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align = 'none', imageCrop = true } = attributes;
const { images, columns = defaultColumnsNumber( attributes ), align = 'none', imageCrop = true, linkto = 'none' } = attributes;
return (
<div className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( img ) => (
<GalleryImage key={ img.url } img={ img } />
<GalleryImage key={ img.url } img={ img } linkto={ linkto } />
) ) }
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion blocks/media-upload-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const getGalleryDetailsMediaFrame = () => {
// the media library image object contains numerous attributes
// we only need this set to display the image in the library
const slimImageObject = ( img ) => {
const attrSet = [ 'sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt' ];
const attrSet = [ 'sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link' ];
return pick( img, attrSet );
};

Expand Down