Skip to content

Commit

Permalink
Force Link UI to appear below a link if one is selected
Browse files Browse the repository at this point in the history
Add custom logic that makes the FormatToolbar render the Link UI under
the link node if a link is focused. If something else is focused,
position the Link UI underneath the cursor or selection. This stops the
Link UI from dancing around when you click on a link.
  • Loading branch information
noisysocks committed Apr 16, 2018
1 parent ab7a429 commit c2d99d8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export class RichText extends Component {
if ( document.activeElement !== this.editor.getBody() ) {
return;
}

const formatNames = this.props.formattingControls;
const formats = this.editor.formatter.matchAll( formatNames ).reduce( ( accFormats, activeFormat ) => {
accFormats[ activeFormat ] = {
Expand All @@ -666,7 +667,15 @@ export class RichText extends Component {
return accFormats;
}, {} );

const rect = getRectangleFromRange( this.editor.selection.getRng() );
let rect;
const selectedAnchor = find( parents, ( node ) => node.tagName === 'A' );
if ( selectedAnchor ) {
// If we selected a link, position the Link UI below the link
rect = selectedAnchor.getBoundingClientRect();
} else {
// Otherwise, position the Link UI below the cursor or text selection
rect = getRectangleFromRange( this.editor.selection.getRng() );
}
const focusPosition = this.getFocusPosition( rect );

this.setState( { formats, focusPosition, selectedNodeId: this.state.selectedNodeId + 1 } );
Expand Down

0 comments on commit c2d99d8

Please sign in to comment.