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

Allow inserting a link with no text selected #6886

Merged
merged 4 commits into from
May 29, 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
2 changes: 1 addition & 1 deletion editor/components/rich-text/format-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class FormatToolbar extends Component {

setLinkTarget( opensInNewWindow ) {
this.setState( { opensInNewWindow } );
if ( this.props.formats.link ) {
if ( this.props.formats.link && ! this.props.formats.link.isAdding ) {
this.props.onChange( { link: {
value: this.props.formats.link.value,
target: opensInNewWindow ? '_blank' : null,
Expand Down
19 changes: 14 additions & 5 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,12 +809,21 @@ export class RichText extends Component {
return;
}

const anchor = this.editor.dom.getParent( this.editor.selection.getNode(), 'a' );
if ( ! anchor ) {
this.removeFormat( 'link' );
const { value: href, target } = formatValue;

if ( ! this.isFormatActive( 'link' ) && this.editor.selection.isCollapsed() ) {
// When no link or text is selected, insert a link with the URL as its text
const anchorHTML = this.editor.dom.createHTML(
'a',
{ href, target },
this.editor.dom.encode( href )
);
this.editor.insertContent( anchorHTML );
} else {
// Use built-in TinyMCE command turn the selection into a link. This takes
// care of deleting any existing links within the selection
this.editor.execCommand( 'mceInsertLink', false, { href, target } );
}
const { value: href, ...params } = formatValue;
this.applyFormat( 'link', { href, ...params }, anchor );
} else {
this.editor.execCommand( 'Unlink' );
}
Expand Down