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

Add optional link to image blocks #1772

Merged
merged 6 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if url should be managed in state, and the parent component instead binds to onSubmit.

Maybe also, should url be reset when toggling the expanded state?

My thinking is "url" is state relevant only so long as the input is expanded.

Copy link
Member Author

Choose a reason for hiding this comment

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

I had that initially, but we also need it when collapsed to show the button state (dark gray), so it seems a prop is more accurate.

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;
}
}