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 12, 2018
1 parent ab7a429 commit 3003793
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,11 @@ export class RichText extends Component {
);
}

onNodeChange( { parents } ) {
onNodeChange( { element, parents } ) {
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 3003793

Please sign in to comment.