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 "Open in new window" option to links #2628

Merged
merged 9 commits into from
Sep 5, 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
35 changes: 32 additions & 3 deletions blocks/editable/format-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { keycodes } from '@wordpress/utils';
import './style.scss';
import UrlInput from '../../url-input';
import { filterURLForDisplay } from '../../../editor/utils/url';
import ToggleControl from '../../inspector-controls/toggle-control';

const { ESCAPE } = keycodes;

Expand Down Expand Up @@ -39,10 +40,11 @@ const DEFAULT_CONTROLS = [ 'bold', 'italic', 'strikethrough', 'link' ];
class FormatToolbar extends Component {
constructor() {
super( ...arguments );

this.state = {
isAddingLink: false,
isEditingLink: false,
settingsVisible: false,
opensInNewWindow: false,
newLinkValue: '',
};

Expand All @@ -52,6 +54,8 @@ class FormatToolbar extends Component {
this.submitLink = this.submitLink.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.onChangeLinkValue = this.onChangeLinkValue.bind( this );
this.toggleLinkSettingsVisibility = this.toggleLinkSettingsVisibility.bind( this );
this.setLinkTarget = this.setLinkTarget.bind( this );
}

componentDidMount() {
Expand All @@ -76,6 +80,8 @@ class FormatToolbar extends Component {
this.setState( {
isAddingLink: false,
isEditingLink: false,
settingsVisible: false,
opensInNewWindow: !! nextProps.formats.link && !! nextProps.formats.link.target,
newLinkValue: '',
} );
}
Expand All @@ -93,6 +99,16 @@ class FormatToolbar extends Component {
};
}

toggleLinkSettingsVisibility() {
this.setState( ( state ) => ( { settingsVisible: ! state.settingsVisible } ) );
}

setLinkTarget( event ) {
const opensInNewWindow = event.target.checked;
this.setState( { opensInNewWindow } );
this.props.onChange( { link: { value: this.props.formats.link.value, target: opensInNewWindow ? '_blank' : '' } } );
}

addLink() {
this.setState( { isEditingLink: false, isAddingLink: true, newLinkValue: '' } );
}
Expand All @@ -109,15 +125,15 @@ class FormatToolbar extends Component {

submitLink( event ) {
event.preventDefault();
this.props.onChange( { link: { value: this.state.newLinkValue } } );
this.props.onChange( { link: { value: this.state.newLinkValue, target: this.state.opensInNewWindow ? '_blank' : '' } } );
if ( this.state.isAddingLink ) {
this.props.speak( __( 'Link inserted.' ), 'assertive' );
}
}

render() {
const { formats, focusPosition, enabledControls = DEFAULT_CONTROLS } = this.props;
const { isAddingLink, isEditingLink, newLinkValue } = this.state;
const { isAddingLink, isEditingLink, newLinkValue, settingsVisible, opensInNewWindow } = this.state;
const linkStyle = focusPosition
? { position: 'absolute', ...focusPosition }
: null;
Expand All @@ -130,6 +146,15 @@ class FormatToolbar extends Component {
isActive: !! formats[ control.format ],
} ) );

const linkSettings = settingsVisible && (
<fieldset className="blocks-format-toolbar__link-settings">
<ToggleControl
label={ __( 'Open in new window' ) }
checked={ opensInNewWindow }
onChange={ this.setLinkTarget } />
</fieldset>
);

if ( enabledControls.indexOf( 'link' ) !== -1 ) {
toolbarControls.push( {
icon: 'admin-links',
Expand All @@ -151,6 +176,8 @@ class FormatToolbar extends Component {
<UrlInput value={ newLinkValue } onChange={ this.onChangeLinkValue } />
<IconButton icon="editor-break" label={ __( 'Apply' ) } type="submit" />
<IconButton icon="editor-unlink" label={ __( 'Remove link' ) } onClick={ this.dropLink } />
<IconButton icon="admin-generic" onClick={ this.toggleLinkSettingsVisibility } aria-expanded={ settingsVisible } />
{ linkSettings }
</form>
}

Expand All @@ -165,6 +192,8 @@ class FormatToolbar extends Component {
</a>
<IconButton icon="edit" label={ __( 'Edit' ) } onClick={ this.editLink } />
<IconButton icon="editor-unlink" label={ __( 'Remove link' ) } onClick={ this.dropLink } />
<IconButton icon="admin-generic" onClick={ this.toggleLinkSettingsVisibility } aria-expanded={ settingsVisible } />
{ linkSettings }
</div>
}
</div>
Expand Down
17 changes: 16 additions & 1 deletion blocks/editable/format-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
box-shadow: 0px 3px 20px rgba( 18, 24, 30, .1 ), 0px 1px 3px rgba( 18, 24, 30, .1 );
border: 1px solid #e0e5e9;
background: #fff;
width: 300px;
width: 305px;
display: inline-flex;
flex-wrap: wrap;
align-items: center;
font-family: $default-font;
font-size: $default-font-size;
line-height: $default-line-height;
z-index: z-index( '.blocks-format-toolbar__link-modal' );

.blocks-url-input {
width: auto;
}
}

.blocks-format-toolbar__link-value {
Expand All @@ -26,3 +31,13 @@
@include long-content-fade( $size: 40% );
}
}

.blocks-format-toolbar__link-settings {
border-top: 1px solid $light-gray-500;
width: 100%;
padding: 10px 8px;

.blocks-base-control {
margin: 0;
}
}
4 changes: 2 additions & 2 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export default class Editable extends Component {
const formats = {};
const link = find( parents, ( node ) => node.nodeName.toLowerCase() === 'a' );
if ( link ) {
formats.link = { value: link.getAttribute( 'href' ) || '', node: link };
formats.link = { value: link.getAttribute( 'href' ) || '', target: link.getAttribute( 'target' ) || '', node: link };
}
const activeFormats = this.editor.formatter.matchAll( [ 'bold', 'italic', 'strikethrough' ] );
activeFormats.forEach( ( activeFormat ) => formats[ activeFormat ] = true );
Expand Down Expand Up @@ -542,7 +542,7 @@ export default class Editable extends Component {
if ( ! anchor ) {
this.removeFormat( 'link' );
}
this.applyFormat( 'link', { href: formatValue.value }, anchor );
this.applyFormat( 'link', { href: formatValue.value, target: formatValue.target }, anchor );
} else {
this.editor.execCommand( 'Unlink' );
}
Expand Down