-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
079e770
Add Inspector option for setting linkto attribute to Attachment, Medi…
mkaz 2fb30b5
Update labels to match core
mkaz 550ef08
Pass linkto attribute to GalleryImage
mkaz fca483f
Add media link if linkto set
mkaz 4fd1e10
We need link data for linking to attachment page
mkaz 7867cf6
Add attachment link if linkto specifies so
mkaz c4b2773
Switch event to Change not Blur so can Update without needing to clic…
mkaz 47172d1
Refactor logic for displaying link to simplier switch & ternary
mkaz 60aeae8
Move Link To select control below image crop
mkaz a55ccaa
Switch back to onBlur, SelectControl doesnt work with onChange
mkaz 7b6fe4c
Fix typo :(
mkaz b35b2e5
Switch linkto to camelCase linkTo
mkaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -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 } ); | ||
const setColumnsNumber = ( event ) => setAttributes( { columns: event.target.value } ); | ||
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } ); | ||
const { imageCrop = true } = attributes; | ||
const { linkto = 'none' } = attributes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be moved to default attrs:
|
||
const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } ); | ||
|
||
const controls = ( | ||
|
@@ -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 } | ||
|
@@ -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> | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
:)