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

Clean up empty links placeholders #1446

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 19 additions & 13 deletions blocks/editable/format-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FormatToolbar extends Component {
constructor( props ) {
super( ...arguments );
this.state = {
linkValue: props.formats.link ? props.formats.link.value : '',
linkValue: props.formats.link,
isEditingLink: false,
};
this.addLink = this.addLink.bind( this );
Expand Down Expand Up @@ -64,15 +64,21 @@ class FormatToolbar extends Component {

componentWillReceiveProps( nextProps ) {
const newState = {
linkValue: nextProps.formats.link ? nextProps.formats.link.value : '',
linkValue: nextProps.formats.link,
};

if (
! this.props.formats.link ||
! nextProps.formats.link ||
this.props.formats.link.node !== nextProps.formats.link.node
nextProps.formats.link !== '#' &&
this.props.formats.link !== nextProps.formats.link
) {
newState.isEditingLink = false;

// Clean up link placeholders.
if ( ! nextProps.formats.link ) {
this.props.onChange( { link: undefined } );
}
}

this.setState( newState );
}

Expand All @@ -86,10 +92,8 @@ class FormatToolbar extends Component {

addLink() {
if ( ! this.props.formats.link ) {
this.props.onChange( { link: { value: '' } } );

// Debounce the call to avoid the reset in willReceiveProps
this.editTimeout = setTimeout( () => this.setState( { isEditingLink: true } ) );
this.props.onChange( { link: '#' } );
this.setState( { isEditingLink: true } );
}
}

Expand All @@ -106,7 +110,7 @@ class FormatToolbar extends Component {

submitLink( event ) {
event.preventDefault();
this.props.onChange( { link: { value: this.state.linkValue } } );
this.props.onChange( { link: this.state.linkValue } );
this.setState( {
isEditingLink: false,
} );
Expand Down Expand Up @@ -141,12 +145,14 @@ class FormatToolbar extends Component {
} );
}

const isEditingLink = this.state.isEditingLink || this.state.linkValue === '#';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line causing the focus error. I can't understand why but if you drop the || this.state.linkValue === '#' it will work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason behind adding this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this like is not needed anymore, but it doesn't fix the issue for me. The input field still won't autofocus.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's this line combined with the setTimeout


/* eslint-disable jsx-a11y/no-autofocus */
return (
<div className="editable-format-toolbar">
<Toolbar controls={ toolbarControls } />

{ !! formats.link && this.state.isEditingLink &&
{ !! formats.link && isEditingLink &&
<form
className="editable-format-toolbar__link-modal"
style={ linkStyle }
Expand All @@ -156,7 +162,7 @@ class FormatToolbar extends Component {
className="editable-format-toolbar__link-input"
type="url"
required
value={ this.state.linkValue }
value={ this.state.linkValue === '#' ? '' : this.state.linkValue }
onChange={ this.updateLinkValue }
placeholder={ __( 'Paste URL or type' ) }
/>
Expand All @@ -165,7 +171,7 @@ class FormatToolbar extends Component {
</form>
}

{ !! formats.link && ! this.state.isEditingLink &&
{ !! formats.link && ! isEditingLink &&
<div className="editable-format-toolbar__link-modal" style={ linkStyle }>
<a className="editable-format-toolbar__link-value" href="" onClick={ this.editLink }>
{ this.state.linkValue && decodeURI( this.state.linkValue ) }
Expand Down
47 changes: 26 additions & 21 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,15 @@ export default class Editable extends Component {
}

onNodeChange( { element, parents } ) {
const formats = {};
const link = find( parents, ( node ) => node.nodeName.toLowerCase() === 'a' );
if ( link ) {
formats.link = { value: link.getAttribute( 'href' ) || '', link };
}
const activeFormats = this.editor.formatter.matchAll( [ 'bold', 'italic', 'strikethrough' ] );
activeFormats.forEach( ( activeFormat ) => formats[ activeFormat ] = true );

const focusPosition = this.getRelativePosition( element );
const bookmark = this.editor.selection.getBookmark( 2, true );
const link = find( parents, ( node ) => node.nodeName === 'A' );
const formats = this.editor.formatter
.matchAll( [ 'bold', 'italic', 'strikethrough' ] )
.reduce( ( acc, format ) => ( { acc, [ format ]: true } ), {} );

formats.link = link ? link.getAttribute( 'href' ) || '' : '';

this.setState( { bookmark, formats, focusPosition } );
}

Expand Down Expand Up @@ -405,23 +404,29 @@ export default class Editable extends Component {
}

forEach( formats, ( formatValue, format ) => {
const isActive = this.isFormatActive( format );
const editor = this.editor;

if ( format === 'link' ) {
if ( formatValue !== undefined ) {
const anchor = this.editor.dom.getParent( this.editor.selection.getNode(), 'a' );
if ( ! anchor ) {
this.editor.formatter.remove( 'link' );
if ( ! formatValue ) {
if ( isActive ) {
editor.execCommand( 'Unlink' );
} else {
// Clean up link placeholders.
editor.$( 'a' ).each( function( i, element ) {
if ( element.getAttribute( 'href' ) === '#' ) {
editor.dom.remove( element, true );
}
} );
}
this.editor.formatter.apply( 'link', { href: formatValue.value }, anchor );
} else {
this.editor.execCommand( 'Unlink' );
}
} else {
const isActive = this.isFormatActive( format );
if ( isActive && ! formatValue ) {
this.editor.formatter.remove( format );
} else if ( ! isActive && formatValue ) {
this.editor.formatter.apply( format );
const anchor = this.editor.dom.getParent( this.editor.selection.getNode(), 'a' );
this.editor.formatter.apply( 'link', { href: formatValue }, anchor );
}
} else if ( isActive && ! formatValue ) {
this.editor.formatter.remove( format );
} else if ( ! isActive && formatValue ) {
this.editor.formatter.apply( format );
}
} );

Expand Down