Skip to content

Commit

Permalink
Fix URL link editing
Browse files Browse the repository at this point in the history
Fixes a bug that prevented the user from selecting a link which has text
that looks like a URL and clicking Edit. We accomplish this by
distinguishing between editing a link (isEditingLink) and adding a new
link (isAddingLink).
  • Loading branch information
noisysocks committed Aug 21, 2018
1 parent 60b8d44 commit beee381
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/editor/src/components/rich-text/format-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function computeDerivedState( props ) {
settingsVisible: false,
opensInNewWindow: !! props.formats.link && !! props.formats.link.target,
linkValue: '',
isEditingLink: false,
};
}

Expand Down Expand Up @@ -151,8 +152,7 @@ class FormatToolbar extends Component {

editLink( event ) {
event.preventDefault();
this.props.onChange( { link: { ...this.props.formats.link, isAdding: true } } );
this.setState( { linkValue: this.props.formats.link.value } );
this.setState( { linkValue: this.props.formats.link.value, isEditingLink: true } );
}

submitLink( event ) {
Expand All @@ -165,7 +165,7 @@ class FormatToolbar extends Component {
value,
} } );

this.setState( { linkValue: value } );
this.setState( { linkValue: value, isEditingLink: false } );
if ( ! this.props.formats.link.value ) {
this.props.speak( __( 'Link added.' ), 'assertive' );
}
Expand All @@ -177,7 +177,7 @@ class FormatToolbar extends Component {

render() {
const { formats, enabledControls = DEFAULT_CONTROLS, customControls = [], selectedNodeId } = this.props;
const { linkValue, settingsVisible, opensInNewWindow } = this.state;
const { linkValue, settingsVisible, opensInNewWindow, isEditingLink } = this.state;
const isAddingLink = formats.link && formats.link.isAdding;

const toolbarControls = FORMATTING_CONTROLS.concat( customControls )
Expand All @@ -202,6 +202,13 @@ class FormatToolbar extends Component {
};
} );

let linkUIToShow = 'none';
if ( isAddingLink || isEditingLink ) {
linkUIToShow = 'editing';
} else if ( formats.link ) {
linkUIToShow = 'previewing';
}

const linkSettings = settingsVisible && (
<div className="editor-format-toolbar__link-modal-line editor-format-toolbar__link-settings">
<ToggleControl
Expand All @@ -215,17 +222,17 @@ class FormatToolbar extends Component {
<div className="editor-format-toolbar">
<Toolbar controls={ toolbarControls } />

{ ( isAddingLink || formats.link ) && (
{ linkUIToShow !== 'none' && (
<Fill name="RichText.Siblings">
<PositionedAtSelection className="editor-format-toolbar__link-container">
<Popover
position="bottom center"
focusOnMount={ isAddingLink ? 'firstElement' : false }
key={ selectedNodeId /* Used to force rerender on change */ }
>
{ isAddingLink && (
// Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
{ linkUIToShow === 'editing' && (
// Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
<form
className="editor-format-toolbar__link-modal"
onKeyPress={ stopKeyPropagation }
Expand All @@ -244,12 +251,12 @@ class FormatToolbar extends Component {
</div>
{ linkSettings }
</form>
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */
) }

{ formats.link && ! isAddingLink && (
// Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar
/* eslint-disable jsx-a11y/no-static-element-interactions */
{ linkUIToShow === 'previewing' && (
// Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar
/* eslint-disable jsx-a11y/no-static-element-interactions */
<div
className="editor-format-toolbar__link-modal"
onKeyPress={ stopKeyPropagation }
Expand All @@ -272,7 +279,7 @@ class FormatToolbar extends Component {
</div>
{ linkSettings }
</div>
/* eslint-enable jsx-a11y/no-static-element-interactions */
/* eslint-enable jsx-a11y/no-static-element-interactions */
) }
</Popover>
</PositionedAtSelection>
Expand Down

0 comments on commit beee381

Please sign in to comment.