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

[Enhancement] Introduce Link Target in Image Block #9520

Merged
merged 1 commit into from
Oct 24, 2018
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
23 changes: 15 additions & 8 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
TextareaControl,
Toolbar,
withNotices,
ToggleControl,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -266,7 +267,7 @@ class ImageEdit extends Component {
render() {
const { isEditing } = this.state;
const { attributes, setAttributes, isLargeViewport, isSelected, className, maxWidth, noticeOperations, noticeUI, toggleSelection, isRTL } = this.props;
const { url, alt, caption, align, id, href, linkDestination, width, height } = attributes;
const { url, alt, caption, align, id, href, linkDestination, width, height, linkTarget } = attributes;
const isExternal = isExternalImage( id, url );

let toolbarEditButton;
Expand Down Expand Up @@ -431,13 +432,19 @@ class ImageEdit extends Component {
onChange={ this.onSetLinkDestination }
/>
{ linkDestination !== LINK_DESTINATION_NONE && (
<TextControl
label={ __( 'Link URL' ) }
value={ href || '' }
onChange={ this.onSetCustomHref }
placeholder={ ! isLinkURLInputDisabled ? 'https://' : undefined }
disabled={ isLinkURLInputDisabled }
/>
<Fragment>
<TextControl
label={ __( 'Link URL' ) }
value={ href || '' }
onChange={ this.onSetCustomHref }
placeholder={ ! isLinkURLInputDisabled ? 'https://' : undefined }
disabled={ isLinkURLInputDisabled }
/>
<ToggleControl
label={ __( 'Open in New Tab' ) }
onChange={ () => setAttributes( { linkTarget: ! linkTarget ? '_blank' : undefined } ) }
checked={ linkTarget === '_blank' } />
</Fragment>
) }
</PanelBody>
</InspectorControls>
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ const blockAttributes = {
type: 'string',
default: 'none',
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'figure > a',
attribute: 'target',
},
};

const imageSchema = {
Expand All @@ -83,7 +89,7 @@ const schema = {
children: {
...imageSchema,
a: {
attributes: [ 'href' ],
attributes: [ 'href', 'target' ],
Copy link
Member

Choose a reason for hiding this comment

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

Does rel need to be here?

Copy link
Member Author

@nfmohit nfmohit Oct 19, 2018

Choose a reason for hiding this comment

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

@aduth I'm honestly not completely sure but according to this comment, it seems the rel attribute gets set correctly when the HTML is copied over to a new paragraph element even without rel attribute added to the array. @talldan Any comments here?

Copy link
Contributor

@talldan talldan Oct 23, 2018

Choose a reason for hiding this comment

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

At the moment rel is ignored by the parser, butrel="noreferrer noopener" is added to any link with target="_blank" by the save function. I think that's a sensible default. I suppose some users might want a finer level of control over the attribute.

I think this is probably ok as a start, the code doesn't preclude more handling of rel being added later.

Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me as someone implementing a block with a schema why the parser would disregard rel. I'm left confused why I should or shouldn't include the attribute here.

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't seem to be something mentioned in the handbook (nothing about raw transformations in general, as far as I could see), so potentially a gap in the information we provide.

children: imageSchema,
},
figcaption: {
Expand Down Expand Up @@ -204,7 +210,7 @@ export const settings = {
edit,

save( { attributes } ) {
const { url, alt, caption, align, href, width, height, id } = attributes;
const { url, alt, caption, align, href, width, height, id, linkTarget } = attributes;

const classes = classnames( {
[ `align${ align }` ]: align,
Expand All @@ -223,7 +229,7 @@ export const settings = {

const figure = (
<Fragment>
{ href ? <a href={ href }>{ image }</a> : image }
{ href ? <a href={ href } target={ linkTarget } rel={ linkTarget === '_blank' ? 'noreferrer noopener' : undefined }>{ image }</a> : image }
{ ! RichText.isEmpty( caption ) && <RichText.Content tagName="figcaption" value={ caption } /> }
</Fragment>
);
Expand Down