Skip to content

Commit

Permalink
Add support for optional links in image blocks (#1772)
Browse files Browse the repository at this point in the history
* Create UrlInput component.
* Add support for href value to image blocks.
* Polish link input positioning.
* Handle already set url and dismiss link input when submitting link with enter.
* Code style cleanup.
  • Loading branch information
mtias authored Jul 7, 2017
1 parent 41b0221 commit 4dc462c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
17 changes: 13 additions & 4 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TextControl from '../../inspector-controls/text-control';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import BlockDescription from '../../block-description';
import UrlInput from '../../url-input';

const { attr, children } = query;

Expand All @@ -30,6 +31,7 @@ registerBlockType( 'core/image', {
url: attr( 'img', 'src' ),
alt: attr( 'img', 'alt' ),
caption: children( 'figcaption' ),
href: attr( 'a', 'href' ),
},

transforms: {
Expand Down Expand Up @@ -57,13 +59,14 @@ registerBlockType( 'core/image', {
},

edit( { attributes, setAttributes, focus, setFocus, className } ) {
const { url, alt, caption, align, id } = attributes;
const { url, alt, caption, align, id, href } = attributes;
const updateAlt = ( newAlt ) => setAttributes( { alt: newAlt } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSelectImage = ( media ) => {
setAttributes( { url: media.url, alt: media.alt, caption: media.caption, id: media.id } );
};
const uploadButtonProps = { isLarge: true };
const onSetHref = ( event ) => setAttributes( { href: event.target.value } );

const controls = (
focus && (
Expand All @@ -88,6 +91,7 @@ registerBlockType( 'core/image', {
<Dashicon icon="edit" />
</MediaUploadButton>
</li>
<UrlInput onChange={ onSetHref } url={ href } />
</Toolbar>
</BlockControls>
)
Expand Down Expand Up @@ -149,17 +153,22 @@ registerBlockType( 'core/image', {
},

save( { attributes } ) {
const { url, alt, caption, align = 'none' } = attributes;
const { url, alt, caption, align = 'none', href } = attributes;
const needsWrapper = [ 'wide', 'full' ].indexOf( align ) !== -1;

// If there's no caption set only save the image element.
if ( ! needsWrapper && ( ! caption || ! caption.length ) ) {
return <img src={ url } alt={ alt } className={ `align${ align }` } />;
const imageWithAlignment = <img src={ url } alt={ alt } className={ `align${ align }` } />;
return href
? <a href={ href }>{ imageWithAlignment }</a>
: imageWithAlignment;
}

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

return (
<figure className={ `align${ align }` }>
<img src={ url } alt={ alt } />
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && !! caption.length && <figcaption>{ caption }</figcaption> }
</figure>
);
Expand Down
7 changes: 7 additions & 0 deletions blocks/library/image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
width: 100%;
}
}

.editor-visual-editor__block[data-type="core/image"] {
.editable-format-toolbar__link-modal {
top: 0;
left: 0;
}
}
66 changes: 66 additions & 0 deletions blocks/url-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import './style.scss';
import { __ } from 'i18n';
import { Component } from 'element';
import { IconButton } from 'components';

class UrlInput extends Component {
constructor() {
super( ...arguments );
this.toggle = this.toggle.bind( this );
this.submitLink = this.submitLink.bind( this );
this.state = {
expanded: false,
};
}

toggle() {
this.setState( { expanded: ! this.state.expanded } );
}

submitLink( event ) {
event.preventDefault();
this.toggle();
}

render() {
const { url, onChange } = this.props;
const { expanded } = this.state;

return (
<li className="components-url-input">
<IconButton
icon="admin-links"
onClick={ this.toggle }
className={ classnames( 'components-toolbar__control', {
'is-active': url,
} ) }
/>
{ expanded &&
<form
className="editable-format-toolbar__link-modal"
onSubmit={ this.submitLink }>
<IconButton className="components-url-input__back" icon="arrow-left-alt" onClick={ this.toggle } />
<input
className="editable-format-toolbar__link-input"
type="url"
value={ url }
onChange={ onChange }
placeholder={ __( 'Paste URL or type' ) }
/>
<IconButton icon="editor-break" type="submit" />
</form>
}
</li>
);
}
}

export default UrlInput;
18 changes: 18 additions & 0 deletions blocks/url-input/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ul.components-toolbar > li.components-url-input {
position: inherit; // let the dialog position according to parent
}

.components-url-input .components-url-input__back {
margin-right: 4px;
overflow: visible;

&::after {
content: '';
position: absolute;
display: block;
width: 1px;
height: 24px;
right: -1px;
background: $light-gray-500;
}
}

0 comments on commit 4dc462c

Please sign in to comment.